From a2321f2cf607a524fbe6ea9bf76b76b807d637ab Mon Sep 17 00:00:00 2001 From: Niko Strijbol Date: Mon, 15 Apr 2024 15:16:38 +0200 Subject: [PATCH] Fix bug in value comparator --- tested/oracles/value.py | 16 ++++++++--- tests/test_oracles.py | 61 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 4 deletions(-) diff --git a/tested/oracles/value.py b/tested/oracles/value.py index c0be00c6..9f22c44c 100644 --- a/tested/oracles/value.py +++ b/tested/oracles/value.py @@ -201,7 +201,8 @@ def _check_data_type( prepared_elements = [] for expected_element, actual_element in zip(expected_elements, actual_elements): assert expected_element is None or isinstance(expected_element, Value) - assert actual_element is None or isinstance(actual_element, Value) + if not isinstance(actual_element, Value): + actual_element = None element_valid, prepared_element = _check_data_type( bundle, expected_element, actual_element ) @@ -232,11 +233,18 @@ def _check_data_type( ) prepared_elements = [] for expected_element, actual_element in zip(expected_elements, actual_elements): - assert isinstance(actual_element, ObjectKeyValuePair) - actual_key, actual_value = actual_element.key, actual_element.value - assert isinstance(actual_key, Value) and isinstance(actual_value, Value) expected_key, expected_value = expected_element.key, expected_element.value assert isinstance(expected_key, Value) and isinstance(expected_value, Value) + + # Handle the case where the elements are not part of a map. + # For example, the actual value is a list. + if not isinstance(actual_element, ObjectKeyValuePair): + actual_key = None + actual_value = None + else: + actual_key, actual_value = actual_element.key, actual_element.value + assert isinstance(actual_key, Value) and isinstance(actual_value, Value) + key_valid, prepared_key = _check_data_type(bundle, expected_key, actual_key) value_valid, prepared_value = _check_data_type( bundle, expected_value, actual_value diff --git a/tests/test_oracles.py b/tests/test_oracles.py index b0c033aa..d109f310 100644 --- a/tests/test_oracles.py +++ b/tests/test_oracles.py @@ -661,3 +661,64 @@ def test_values_identical_list_is_detected(tmp_path: Path, pytestconfig): config = oracle_config(tmp_path, pytestconfig, language="python") result = evaluate_value(config, channel, actual_value) assert result.result.enum == Status.CORRECT + + +def test_list_and_map_works(tmp_path: Path, pytestconfig): + channel = ValueOutputChannel( + value=SequenceType( + type=BasicSequenceTypes.SEQUENCE, + data=[ + StringType(type=BasicStringTypes.TEXT, data="S", diagnostic=None), + StringType(type=BasicStringTypes.TEXT, data="S", diagnostic=None), + ], + ) + ) + actual_value = get_converter().dumps( + ObjectType( + type=BasicObjectTypes.MAP, + data=[ + ObjectKeyValuePair( + key=StringType(type=BasicStringTypes.TEXT, data="a"), + value=StringType(type=BasicStringTypes.TEXT, data="b"), + ), + ObjectKeyValuePair( + key=StringType(type=BasicStringTypes.TEXT, data="c"), + value=StringType(type=BasicStringTypes.TEXT, data="d"), + ), + ], + ) + ) + config = oracle_config(tmp_path, pytestconfig, language="javascript") + result = evaluate_value(config, channel, actual_value) + assert result.result.enum == Status.WRONG + + +def test_map_and_list_works(tmp_path: Path, pytestconfig): + channel = ValueOutputChannel( + value=ObjectType( + type=BasicObjectTypes.MAP, + data=[ + ObjectKeyValuePair( + key=StringType(type=BasicStringTypes.TEXT, data="a"), + value=StringType(type=BasicStringTypes.TEXT, data="b"), + ), + ObjectKeyValuePair( + key=StringType(type=BasicStringTypes.TEXT, data="c"), + value=StringType(type=BasicStringTypes.TEXT, data="d"), + ), + ], + ) + ) + + actual_value = get_converter().dumps( + SequenceType( + type=BasicSequenceTypes.SEQUENCE, + data=[ + StringType(type=BasicStringTypes.TEXT, data="S", diagnostic=None), + StringType(type=BasicStringTypes.TEXT, data="S", diagnostic=None), + ], + ) + ) + config = oracle_config(tmp_path, pytestconfig, language="javascript") + result = evaluate_value(config, channel, actual_value) + assert result.result.enum == Status.WRONG