diff --git a/tested/nat_translation.py b/tested/nat_translation.py index 4a4a024b..9f798c80 100644 --- a/tested/nat_translation.py +++ b/tested/nat_translation.py @@ -1,14 +1,15 @@ import sys 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, ) @@ -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 @@ -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)) diff --git a/tests/test_dsl_yaml.py b/tests/test_dsl_yaml.py index 3caef72d..584320ef 100644 --- a/tests/test_dsl_yaml.py +++ b/tests/test_dsl_yaml.py @@ -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, @@ -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