Skip to content

Commit

Permalink
Merge pull request #565 from dodona-edu/fix/do-not-accept-empty-maps
Browse files Browse the repository at this point in the history
Fix empty map being accepted
  • Loading branch information
jorg-vr authored Dec 4, 2024
2 parents c579025 + 82f67d2 commit c018623
Show file tree
Hide file tree
Showing 2 changed files with 173 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
171 changes: 171 additions & 0 deletions tests/test_oracles_builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit c018623

Please sign in to comment.