Skip to content

Commit f787f38

Browse files
authored
Merge pull request #585 from dodona-edu/fix/programming-language
Fix global definition of programming language
2 parents b705a9f + 5c47e90 commit f787f38

File tree

2 files changed

+48
-16
lines changed

2 files changed

+48
-16
lines changed

tested/dsl/translate_parser.py

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -187,19 +187,29 @@ def is_expression(_checker: TypeChecker, instance: Any) -> bool:
187187
return isinstance(instance, ExpressionString)
188188

189189

190-
def test(value: object) -> bool:
191-
if not isinstance(value, str):
192-
return False
193-
import ast
194-
195-
ast.parse(value)
196-
return True
197-
198-
199-
def load_schema_validator(file: str = "schema-strict.json") -> Validator:
190+
def load_schema_validator(
191+
dsl_object: YamlObject = None, file: str = "schema-strict.json"
192+
) -> Validator:
200193
"""
201194
Load the JSON Schema validator used to check DSL test suites.
202195
"""
196+
# if the programming language is set in the root, tested_dsl_expressions don't need to be parseable
197+
language_present = (
198+
dsl_object is not None
199+
and isinstance(dsl_object, dict)
200+
and "language" in dsl_object
201+
)
202+
203+
def validate_tested_dsl_expression(value: object) -> bool:
204+
if not isinstance(value, str):
205+
return False
206+
if language_present:
207+
return True
208+
import ast
209+
210+
ast.parse(value)
211+
return True
212+
203213
path_to_schema = Path(__file__).parent / file
204214
with open(path_to_schema, "r") as schema_file:
205215
schema_object = json.load(schema_file)
@@ -209,14 +219,13 @@ def load_schema_validator(file: str = "schema-strict.json") -> Validator:
209219
"oracle", is_oracle
210220
).redefine("expression", is_expression)
211221
format_checker = original_validator.FORMAT_CHECKER
212-
format_checker.checks("tested-dsl-expression", SyntaxError)(test)
222+
format_checker.checks("tested-dsl-expression", SyntaxError)(
223+
validate_tested_dsl_expression
224+
)
213225
tested_validator = extend_validator(original_validator, type_checker=type_checker)
214226
return tested_validator(schema_object, format_checker=format_checker)
215227

216228

217-
_SCHEMA_VALIDATOR = load_schema_validator()
218-
219-
220229
class DslValidationError(ValueError):
221230
pass
222231

@@ -310,7 +319,7 @@ def _validate_dsl(dsl_object: YamlObject):
310319
:param dsl_object: The object to validate.
311320
:return: True if valid, False otherwise.
312321
"""
313-
errors = list(_SCHEMA_VALIDATOR.iter_errors(dsl_object))
322+
errors = list(load_schema_validator(dsl_object).iter_errors(dsl_object))
314323
if len(errors) == 1:
315324
message = (
316325
"Validating the DSL resulted in an error. "

tests/test_dsl_yaml.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1304,6 +1304,29 @@ def test_empty_text_data_newlines():
13041304
assert actual_stderr == ""
13051305

13061306

1307+
def test_programming_language_can_be_globally_configured():
1308+
yaml_str = """
1309+
namespace: "Numbers"
1310+
language: "java"
1311+
tabs:
1312+
- tab: "Numbers.oddValues"
1313+
testcases:
1314+
- expression: "Numbers.oddValues(new int[]{1, 2, 3, 4, 5, 6, 7, 8})"
1315+
return: [1, 3, 5, 7]
1316+
"""
1317+
json_str = translate_to_test_suite(yaml_str)
1318+
suite = parse_test_suite(json_str)
1319+
assert len(suite.tabs) == 1
1320+
tab = suite.tabs[0]
1321+
assert len(tab.contexts) == 1
1322+
context = tab.contexts[0]
1323+
assert len(context.testcases) == 1
1324+
testcase = context.testcases[0]
1325+
assert isinstance(testcase.input, LanguageLiterals)
1326+
assert testcase.input.type == "expression"
1327+
assert testcase.input.literals.keys() == {"java"}
1328+
1329+
13071330
def test_strict_json_schema_is_valid():
13081331
path_to_schema = Path(__file__).parent / "tested-draft7.json"
13091332
with open(path_to_schema, "r") as schema_file:
@@ -1316,6 +1339,6 @@ def test_strict_json_schema_is_valid():
13161339

13171340

13181341
def test_editor_json_schema_is_valid():
1319-
validator = load_schema_validator("schema.json")
1342+
validator = load_schema_validator(file="schema.json")
13201343
assert isinstance(validator.schema, dict)
13211344
validator.check_schema(validator.schema)

0 commit comments

Comments
 (0)