diff --git a/tested/oracles/value.py b/tested/oracles/value.py index 08c33927..e86d0c3a 100644 --- a/tested/oracles/value.py +++ b/tested/oracles/value.py @@ -233,9 +233,10 @@ def _check_data_type( prepared_actual.data if isinstance(prepared_actual.data, list) else [] ) prepared_elements = [] - for expected_element, actual_element in zip(expected_elements, actual_elements): + for i, expected_element in enumerate(expected_elements): expected_key, expected_value = expected_element.key, expected_element.value assert isinstance(expected_key, Value) and isinstance(expected_value, Value) + actual_element = actual_elements[i] if i < len(actual_elements) else None # Handle the case where the elements are not part of a map. # For example, the actual value is a list. diff --git a/tests/test_oracles_builtin.py b/tests/test_oracles_builtin.py index 1db3688b..7e21ffa0 100644 --- a/tests/test_oracles_builtin.py +++ b/tests/test_oracles_builtin.py @@ -769,3 +769,174 @@ def test_map_and_list_works(tmp_path: Path, pytestconfig: pytest.Config): config = oracle_config(tmp_path, pytestconfig, language="javascript") result = evaluate_value(config, channel, actual_value) assert result.result.enum == Status.WRONG + + +def test_empty_map_is_not_accepted(tmp_path: Path, pytestconfig: pytest.Config): + 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(ObjectType(type=BasicObjectTypes.MAP, data=[])) + config = oracle_config(tmp_path, pytestconfig, language="javascript") + result = evaluate_value(config, channel, actual_value) + assert result.result.enum == Status.WRONG + + +def test_smaller_map_is_not_accepted(tmp_path: Path, pytestconfig: pytest.Config): + 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( + ObjectType( + type=BasicObjectTypes.MAP, + data=[ + ObjectKeyValuePair( + key=StringType(type=BasicStringTypes.TEXT, data="a"), + value=StringType(type=BasicStringTypes.TEXT, data="b"), + ), + ], + ) + ) + config = oracle_config(tmp_path, pytestconfig, language="javascript") + result = evaluate_value(config, channel, actual_value) + assert result.result.enum == Status.WRONG + + +def test_bigger_map_is_not_accepted(tmp_path: Path, pytestconfig: pytest.Config): + 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( + 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"), + ), + ObjectKeyValuePair( + key=StringType(type=BasicStringTypes.TEXT, data="b"), + 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_exact_map_is_accepted(tmp_path: Path, pytestconfig: pytest.Config): + 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( + 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.CORRECT + + +def test_wrong_map_is_accepted(tmp_path: Path, pytestconfig: pytest.Config): + 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( + 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="wrong"), + ), + ], + ) + ) + config = oracle_config(tmp_path, pytestconfig, language="javascript") + result = evaluate_value(config, channel, actual_value) + assert result.result.enum == Status.WRONG