Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 24 additions & 15 deletions tested/dsl/translate_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,19 +187,29 @@ def is_expression(_checker: TypeChecker, instance: Any) -> bool:
return isinstance(instance, ExpressionString)


def test(value: object) -> bool:
if not isinstance(value, str):
return False
import ast

ast.parse(value)
return True


def load_schema_validator(file: str = "schema-strict.json") -> Validator:
def load_schema_validator(
dsl_object: YamlObject = None, file: str = "schema-strict.json"
) -> Validator:
"""
Load the JSON Schema validator used to check DSL test suites.
"""
# if the programming language is set in the root, tested_dsl_expressions don't need to be parseable
language_present = (
dsl_object is not None
and isinstance(dsl_object, dict)
and "language" in dsl_object
)

def validate_tested_dsl_expression(value: object) -> bool:
if not isinstance(value, str):
return False
if language_present:
return True
import ast

ast.parse(value)
return True

path_to_schema = Path(__file__).parent / file
with open(path_to_schema, "r") as schema_file:
schema_object = json.load(schema_file)
Expand All @@ -209,14 +219,13 @@ def load_schema_validator(file: str = "schema-strict.json") -> Validator:
"oracle", is_oracle
).redefine("expression", is_expression)
format_checker = original_validator.FORMAT_CHECKER
format_checker.checks("tested-dsl-expression", SyntaxError)(test)
format_checker.checks("tested-dsl-expression", SyntaxError)(
validate_tested_dsl_expression
)
tested_validator = extend_validator(original_validator, type_checker=type_checker)
return tested_validator(schema_object, format_checker=format_checker)


_SCHEMA_VALIDATOR = load_schema_validator()


class DslValidationError(ValueError):
pass

Expand Down Expand Up @@ -310,7 +319,7 @@ def _validate_dsl(dsl_object: YamlObject):
:param dsl_object: The object to validate.
:return: True if valid, False otherwise.
"""
errors = list(_SCHEMA_VALIDATOR.iter_errors(dsl_object))
errors = list(load_schema_validator(dsl_object).iter_errors(dsl_object))
if len(errors) == 1:
message = (
"Validating the DSL resulted in an error. "
Expand Down
25 changes: 24 additions & 1 deletion tests/test_dsl_yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -1304,6 +1304,29 @@ def test_empty_text_data_newlines():
assert actual_stderr == ""


def test_programming_language_can_be_globally_configured():
yaml_str = """
namespace: "Numbers"
language: "java"
tabs:
- tab: "Numbers.oddValues"
testcases:
- expression: "Numbers.oddValues(new int[]{1, 2, 3, 4, 5, 6, 7, 8})"
return: [1, 3, 5, 7]
"""
json_str = translate_to_test_suite(yaml_str)
suite = parse_test_suite(json_str)
assert len(suite.tabs) == 1
tab = suite.tabs[0]
assert len(tab.contexts) == 1
context = tab.contexts[0]
assert len(context.testcases) == 1
testcase = context.testcases[0]
assert isinstance(testcase.input, LanguageLiterals)
assert testcase.input.type == "expression"
assert testcase.input.literals.keys() == {"java"}


def test_strict_json_schema_is_valid():
path_to_schema = Path(__file__).parent / "tested-draft7.json"
with open(path_to_schema, "r") as schema_file:
Expand All @@ -1316,6 +1339,6 @@ def test_strict_json_schema_is_valid():


def test_editor_json_schema_is_valid():
validator = load_schema_validator("schema.json")
validator = load_schema_validator(file="schema.json")
assert isinstance(validator.schema, dict)
validator.check_schema(validator.schema)
Loading