Skip to content

Commit

Permalink
add test for unit-test
Browse files Browse the repository at this point in the history
  • Loading branch information
BrentBlanckaert committed Dec 11, 2024
1 parent c1114bc commit c61b563
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 17 deletions.
36 changes: 20 additions & 16 deletions tested/nat_translation.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import sys

Check notice

Code scanning / CodeQL

Unused import Note test

Import of 'sys' is not used.

import yaml

from tested.dsl.translate_parser import (
_parse_yaml,
YamlObject,
YamlDict,
ReturnOracle,
ExpressionString,
_validate_testcase_combinations,
NaturalLanguageMap,
ReturnOracle,
YamlDict,
YamlObject,
_parse_yaml,
_validate_testcase_combinations,
)


Expand Down Expand Up @@ -118,9 +119,11 @@ def translate_contexts(contexts: list, language: str) -> list:
result = []
for context in contexts:
assert isinstance(context, dict)
raw_testcases = context.get("script", context.get("testcases"))
key_to_set = "script" if "script" in context else "testcases"
raw_testcases = context.get(key_to_set)
assert isinstance(raw_testcases, list)
result.append(translate_testcases(raw_testcases, language))
context[key_to_set] = translate_testcases(raw_testcases, language)
result.append(context)

return result

Expand Down Expand Up @@ -196,12 +199,13 @@ def expression_representer(dumper, data):
return yaml.dump(yaml_object, sort_keys=False)


if __name__ == "__main__":
n = len(sys.argv)
assert n > 1, "Expected atleast two argument (path to yaml file and language)."

path = sys.argv[1]
lang = sys.argv[2]
new_yaml = parse_yaml(path)
translated_dsl = translate_dsl(new_yaml, lang)
print(convert_to_yaml(translated_dsl))
# if __name__ == "__main__":
# n = len(sys.argv)
# assert n > 1, "Expected atleast two argument (path to yaml file and language)."
#
# path = sys.argv[1]
# lang = sys.argv[2]
# new_yaml = parse_yaml(path)
# print(new_yaml)
# translated_dsl = translate_dsl(new_yaml, lang)
# print(convert_to_yaml(translated_dsl))

Check notice

Code scanning / CodeQL

Commented-out code Note test

This comment appears to contain commented-out code.
63 changes: 62 additions & 1 deletion tests/test_dsl_yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
StringTypes,
)
from tested.dsl import parse_dsl, translate_to_test_suite
from tested.dsl.translate_parser import load_schema_validator
from tested.dsl.translate_parser import load_schema_validator, _parse_yaml
from tested.nat_translation import translate_dsl, convert_to_yaml
from tested.serialisation import (
FunctionCall,
NumberType,
Expand Down Expand Up @@ -1319,3 +1320,63 @@ def test_editor_json_schema_is_valid():
validator = load_schema_validator("schema.json")
assert isinstance(validator.schema, dict)
validator.check_schema(validator.schema)

def test_natural_translate_unit_test():
# Everywhere where !natural_language is used, it is mandatory to do so.
# Everywhere else it isn't.
yaml_str = """- tab:
en: "counting"
nl: "tellen"
contexts:
- testcases:
- statement: !natural_language
en: 'result = trying(10)'
nl: 'resultaat = proberen(10)'
- expression: !natural_language
en: 'count_words(result)'
nl: 'tel_woorden(resultaat)'
return: !natural_language
en: 'The result is 10'
nl: 'Het resultaat is 10'
- expression: !natural_language
en: !expression "count"
nl: !expression "tellen"
return: !natural_language
en: 'count'
nl: 'tellen'
- expression: 'ok(10)'
return: !oracle
value: !natural_language
en: "The value 10 is OK!"
nl: "De waarde 10 is OK!"
oracle: "custom_check"
file: "test.py"
name: "evaluate_test"
arguments:
en: ["The value", "is OK!", "is not OK!"]
nl: ["De waarde", "is OK!", "is niet OK!"]
"""
translated_yaml_str = """- tab: counting
contexts:
- testcases:
- statement: result = trying(10)
- expression: count_words(result)
return: The result is 10
- expression: !expression 'count'
return: count
- expression: ok(10)
return: !oracle
value: The value 10 is OK!
oracle: custom_check
file: test.py
name: evaluate_test
arguments:
- The value
- is OK!
- is not OK!
"""
parsed_yaml = _parse_yaml(yaml_str)
translated_dsl = translate_dsl(parsed_yaml, "en")
translated_yaml = convert_to_yaml(translated_dsl)
print(translated_yaml)
assert translated_yaml == translated_yaml_str

0 comments on commit c61b563

Please sign in to comment.