Skip to content

Commit

Permalink
Fix empty map being accepted
Browse files Browse the repository at this point in the history
See #562
  • Loading branch information
niknetniko committed Nov 28, 2024
1 parent 5e62a7d commit 486a9ec
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 1 deletion.
3 changes: 2 additions & 1 deletion tested/oracles/value.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
130 changes: 130 additions & 0 deletions tests/test_oracles_builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -769,3 +769,133 @@ 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_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

0 comments on commit 486a9ec

Please sign in to comment.