Skip to content

Commit

Permalink
fixed some bugs and fixed the tests
Browse files Browse the repository at this point in the history
  • Loading branch information
BrentBlanckaert committed Dec 29, 2024
1 parent 49079a0 commit 4174beb
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 26 deletions.
6 changes: 2 additions & 4 deletions tested/dsl/schema-strict-nat-translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -467,8 +467,7 @@
"$ref" : "#/definitions/exceptionChannel"
}
}
],
"$ref" : "#/definitions/exceptionChannel"
]
},
"files" : {
"description" : "A list of files used in the test suite.",
Expand Down Expand Up @@ -644,8 +643,7 @@
"$ref" : "#/definitions/exceptionChannel"
}
}
],
"$ref" : "#/definitions/exceptionChannel"
]
},
"files" : {
"description" : "A list of files used in the test suite.",
Expand Down
15 changes: 11 additions & 4 deletions tested/dsl/translate_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ class ReturnOracle(dict):
class NaturalLanguageMap(dict):
pass


class ProgrammingLanguageMap(dict):
pass

Expand Down Expand Up @@ -162,7 +163,10 @@ def _natural_language_map(loader: yaml.Loader, node: yaml.Node) -> NaturalLangua
), f"A natural language map must be an object, got {result} which is a {type(result)}."
return NaturalLanguageMap(result)

def _programming_language_map(loader: yaml.Loader, node: yaml.Node) -> ProgrammingLanguageMap:

def _programming_language_map(
loader: yaml.Loader, node: yaml.Node
) -> ProgrammingLanguageMap:
result = _parse_yaml_value(loader, node)
assert isinstance(
result, dict
Expand Down Expand Up @@ -220,6 +224,7 @@ def is_oracle(_checker: TypeChecker, instance: Any) -> bool:
def is_expression(_checker: TypeChecker, instance: Any) -> bool:
return isinstance(instance, ExpressionString)


def is_natural_language_map(_checker: TypeChecker, instance: Any) -> bool:
return isinstance(instance, NaturalLanguageMap)

Check warning on line 229 in tested/dsl/translate_parser.py

View check run for this annotation

Codecov / codecov/patch

tested/dsl/translate_parser.py#L229

Added line #L229 was not covered by tests

Expand All @@ -242,9 +247,11 @@ def load_schema_validator(file: str = "schema-strict.json") -> Validator:
schema_object = json.load(schema_file)

original_validator: Type[Validator] = validator_for(schema_object)
type_checker = original_validator.TYPE_CHECKER.redefine(
"oracle", is_oracle
).redefine("expression", is_expression).redefine("natural_language", is_natural_language_map)
type_checker = (
original_validator.TYPE_CHECKER.redefine("oracle", is_oracle)
.redefine("expression", is_expression)
.redefine("natural_language", is_natural_language_map)
)
format_checker = original_validator.FORMAT_CHECKER
format_checker.checks("tested-dsl-expression", SyntaxError)(test)
tested_validator = extend_validator(original_validator, type_checker=type_checker)
Expand Down
19 changes: 14 additions & 5 deletions tested/nat_translation.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import yaml

from tested.dsl.translate_parser import (
DslValidationError,
ExpressionString,
NaturalLanguageMap,
ProgrammingLanguageMap,
Expand All @@ -11,8 +12,9 @@
YamlObject,
_parse_yaml,
_validate_dsl,
_validate_testcase_combinations, load_schema_validator,
convert_validation_error_to_group, DslValidationError,
_validate_testcase_combinations,
convert_validation_error_to_group,
load_schema_validator,
)


Expand Down Expand Up @@ -40,13 +42,17 @@ def validate_pre_dsl(dsl_object: YamlObject):
message = "Validating the DSL resulted in some errors."
raise ExceptionGroup(message, the_errors)

Check warning on line 43 in tested/nat_translation.py

View check run for this annotation

Codecov / codecov/patch

tested/nat_translation.py#L39-L43

Added lines #L39 - L43 were not covered by tests


def natural_langauge_map_translation(value: YamlObject, language: str):
if isinstance(value, NaturalLanguageMap):
assert language in value
value = value[language]
return value

def translate_input_files(dsl_object: dict, language: str, flattened_stack: dict) -> dict:

def translate_input_files(
dsl_object: dict, language: str, flattened_stack: dict
) -> dict:
if (files := dsl_object.get("files")) is not None:
# Translation map can happen at the top level.
files = natural_langauge_map_translation(files, language)
Expand Down Expand Up @@ -124,9 +130,12 @@ def translate_testcase(
# Program language translation found
if isinstance(expr_stmt, ProgrammingLanguageMap):
expr_stmt = {
k: natural_langauge_map_translation(v, language) for k, v in expr_stmt.items()
k: natural_langauge_map_translation(v, language)
for k, v in expr_stmt.items()
}
elif isinstance(expr_stmt, NaturalLanguageMap): # Natural language translation found
elif isinstance(
expr_stmt, NaturalLanguageMap
): # Natural language translation found
assert language in expr_stmt
expr_stmt = expr_stmt[language]

Expand Down
28 changes: 15 additions & 13 deletions tests/test_dsl_yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -1367,7 +1367,7 @@ def test_natural_translate_unit_test():
oracle: "custom_check"
file: "test.py"
name: "evaluate_test"
arguments:
arguments: !natural_language
en: ["The value", "is OK!", "is not OK!"]
nl: ["Het {result}", "is OK!", "is niet OK!"]
description: !natural_language
Expand All @@ -1391,7 +1391,7 @@ def test_natural_translate_unit_test():
- expression: 'result'
return: '11_{elf}'
description:
description:
description: !natural_language
en: "Eleven_{elf}"
nl: "Elf_{elf}"
format: "code"
Expand All @@ -1401,11 +1401,13 @@ def test_natural_translate_unit_test():
en: "tests(11)"
nl: "testen(11)"
return: 11
- expression:
- expression: !programming_language
javascript: "{animal}_javascript(1 + 1)"
typescript: "{animal}_typescript(1 + 1)"
java: "Submission.{animal}_java(1 + 1)"
python: "{animal}_python(1 + 1)"
python: !natural_language
en: "{animal}_python_en(1 + 1)"
nl: "{animal}_python_nl(1 + 1)"
return: 2
""".strip()
translated_yaml_str = """
Expand Down Expand Up @@ -1447,7 +1449,7 @@ def test_natural_translate_unit_test():
javascript: animals_javascript(1 + 1)
typescript: animals_typescript(1 + 1)
java: Submission.animals_java(1 + 1)
python: animals_python(1 + 1)
python: animals_python_en(1 + 1)
return: 2
""".strip()
parsed_yaml = _parse_yaml(yaml_str)
Expand All @@ -1462,7 +1464,7 @@ def test_natural_translate_io_test():
# Everywhere else it isn't.
yaml_str = """
units:
- unit:
- unit: !natural_language
en: "Arguments"
nl: "Argumenten"
translation:
Expand All @@ -1471,10 +1473,10 @@ def test_natural_translate_io_test():
nl: "gebruiker"
cases:
- script:
- stdin:
- stdin: !natural_language
en: "User_{User}"
nl: "Gebruiker_{User}"
arguments:
arguments: !natural_language
en: [ "input_{User}", "output_{User}" ]
nl: [ "invoer_{User}", "uitvoer_{User}" ]
stdout: !natural_language
Expand All @@ -1486,26 +1488,26 @@ def test_natural_translate_io_test():
exception: !natural_language
en: "Does not look good"
nl: "Ziet er niet goed uit"
- stdin:
- stdin: !natural_language
en: "Friend of {User}"
nl: "Vriend van {User}"
arguments:
arguments: !natural_language
en: [ "input", "output" ]
nl: [ "invoer", "uitvoer" ]
stdout:
data:
data: !natural_language
en: "Hi Friend of {User}"
nl: "Hallo Vriend van {User}"
config:
ignoreWhitespace: true
stderr:
data:
data: !natural_language
en: "Nothing to see here {User}"
nl: "Hier is niets te zien {User}"
config:
ignoreWhitespace: true
exception:
message:
message: !natural_language
en: "Does not look good {User}"
nl: "Ziet er niet goed uit {User}"
types:
Expand Down

0 comments on commit 4174beb

Please sign in to comment.