Skip to content

Commit

Permalink
Fix bug in value comparator
Browse files Browse the repository at this point in the history
  • Loading branch information
niknetniko committed Apr 15, 2024
1 parent 106446b commit a2321f2
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 4 deletions.
16 changes: 12 additions & 4 deletions tested/oracles/value.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down Expand Up @@ -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
Expand Down
61 changes: 61 additions & 0 deletions tests/test_oracles.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit a2321f2

Please sign in to comment.