-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathtranslate_parser.py
More file actions
798 lines (667 loc) · 27.3 KB
/
Copy pathtranslate_parser.py
File metadata and controls
798 lines (667 loc) · 27.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
import json
import sys
import textwrap
from collections.abc import Callable
from decimal import Decimal
from pathlib import Path
from typing import Any, Literal, Type, TypeVar, cast
import yaml
from attrs import define, evolve, field
from jsonschema import TypeChecker
from jsonschema.exceptions import ValidationError
from jsonschema.protocols import Validator
from jsonschema.validators import extend as extend_validator
from jsonschema.validators import validator_for
from tested.datatypes import (
AdvancedNumericTypes,
AllTypes,
BasicBooleanTypes,
BasicNothingTypes,
BasicNumericTypes,
BasicObjectTypes,
BasicSequenceTypes,
BasicStringTypes,
BooleanTypes,
NothingTypes,
NumericTypes,
ObjectTypes,
SequenceTypes,
StringTypes,
resolve_to_basic,
)
from tested.dodona import ExtendedMessage
from tested.dsl.ast_translator import InvalidDslError, extract_comment, parse_string
from tested.parsing import get_converter, suite_to_json
from tested.serialisation import (
BooleanType,
NothingType,
NumberType,
ObjectKeyValuePair,
ObjectType,
SequenceType,
StringType,
Value,
)
from tested.testsuite import (
Context,
CustomCheckOracle,
EmptyChannel,
EvaluationFunction,
ExceptionOutputChannel,
ExitCodeOutputChannel,
ExpectedException,
FileOutputChannel,
FileUrl,
GenericTextOracle,
IgnoredChannel,
LanguageLiterals,
LanguageSpecificOracle,
MainInput,
Output,
Suite,
SupportedLanguage,
Tab,
Testcase,
TextBuiltin,
TextData,
TextOutputChannel,
ValueOutputChannel,
)
from tested.utils import get_args, recursive_dict_merge
YamlDict = dict[str, "YamlObject"]
@define
class TestedType:
value: Any
type: str | AllTypes
class ExpressionString(str):
pass
class ReturnOracle(dict):
pass
class NaturalLanguageMap(dict):
pass
OptionDict = dict[str, int | bool]
YamlObject = (
YamlDict
| list
| bool
| float
| int
| str
| None
| ExpressionString
| ReturnOracle
| NaturalLanguageMap
)
def _convert_language_dictionary(
original: dict[str, str]
) -> dict[SupportedLanguage, str]:
return {SupportedLanguage(k): v for k, v in original.items()}
def _ensure_trailing_newline(text: str) -> str:
if text and not text.endswith("\n"):
return text + "\n"
else:
return text
def _parse_yaml_value(loader: yaml.Loader, node: yaml.Node) -> Any:
if isinstance(node, yaml.MappingNode):
result = loader.construct_mapping(node)
elif isinstance(node, yaml.SequenceNode):
result = loader.construct_sequence(node)
else:
assert isinstance(node, yaml.ScalarNode)
result = loader.construct_scalar(node)
return result
def _custom_type_constructors(loader: yaml.Loader, node: yaml.Node) -> TestedType:
tested_tag = node.tag[1:]
base_result = _parse_yaml_value(loader, node)
return TestedType(type=tested_tag, value=base_result)
def _expression_string(loader: yaml.Loader, node: yaml.Node) -> ExpressionString:
result = _parse_yaml_value(loader, node)
assert isinstance(result, str), f"An expression must be a string, got {result}"
return ExpressionString(result)
def _return_oracle(loader: yaml.Loader, node: yaml.Node) -> ReturnOracle:
result = _parse_yaml_value(loader, node)
assert isinstance(
result, dict
), f"A custom oracle must be an object, got {result} which is a {type(result)}."
return ReturnOracle(result)
def _natural_language_map(loader: yaml.Loader, node: yaml.Node) -> NaturalLanguageMap:
result = _parse_yaml_value(loader, node)
assert isinstance(
result, dict
), f"A natural language map must be an object, got {result} which is a {type(result)}."
return NaturalLanguageMap(result)
def _parse_yaml(yaml_stream: str) -> YamlObject:
"""
Parse a string or stream to YAML.
"""
loader: type[yaml.Loader] = cast(type[yaml.Loader], yaml.CSafeLoader)
for types in get_args(AllTypes):
for actual_type in types:
yaml.add_constructor("!" + actual_type, _custom_type_constructors, loader)
yaml.add_constructor("!expression", _expression_string, loader)
yaml.add_constructor("!oracle", _return_oracle, loader)
yaml.add_constructor("!natural_language", _natural_language_map, loader)
try:
return yaml.load(yaml_stream, loader)
except yaml.MarkedYAMLError as exc:
lines = yaml_stream.splitlines()
if exc.problem_mark is None:
# There is no additional information, so what can we do?
raise exc
sys.stderr.write(
textwrap.dedent(
f"""
YAML error while parsing test suite. This means there is a YAML syntax error.
The YAML parser indicates the problem lies at line {exc.problem_mark.line + 1}, column {exc.problem_mark.column + 1}:
{lines[exc.problem_mark.line]}
{" " * exc.problem_mark.column + "^"}
The error message was:
{exc.problem} {exc.context}
The detailed exception is provided below.
You might also find help by validating your YAML file with a YAML validator.\n
"""
)
)
raise exc
def is_oracle(_checker: TypeChecker, instance: Any) -> bool:
return isinstance(instance, ReturnOracle)
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:
"""
Load the JSON Schema validator used to check DSL test suites.
"""
path_to_schema = Path(__file__).parent / file
with open(path_to_schema, "r") as schema_file:
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)
format_checker = original_validator.FORMAT_CHECKER
format_checker.checks("tested-dsl-expression", SyntaxError)(test)
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
class InvalidYamlError(ValueError):
pass
@define(frozen=True)
class DslContext:
"""
Carries context in each level.
This function will, in essence, make two properties inheritable from the global
and tab context:
- The "config" property, which has config for "stdout", "stderr", and "file".
- The "files" property, which is a list of files.
"""
files: list[FileUrl] = field(factory=list)
config: dict[str, dict] = field(factory=dict)
language: SupportedLanguage | Literal["tested"] = "tested"
def deepen_context(self, new_level: YamlDict | None) -> "DslContext":
"""
Merge certain fields of the new object with the current context, resulting
in a new context for the new level.
:param new_level: The new object from the DSL to get information from.
:return: A new context.
"""
if new_level is None:
return self
the_files = self.files
if "files" in new_level:
assert isinstance(new_level["files"], list)
additional_files = {_convert_file(f) for f in new_level["files"]}
the_files = list(set(self.files) | additional_files)
the_config = self.config
if "config" in new_level:
assert isinstance(new_level["config"], dict)
the_config = recursive_dict_merge(the_config, new_level["config"])
return evolve(self, files=the_files, config=the_config)
def merge_inheritable_with_specific_config(
self, level: YamlDict, config_name: str
) -> dict:
inherited_options = self.config.get(config_name, dict())
specific_options = level.get("config", dict())
assert isinstance(
specific_options, dict
), f"The config options for {config_name} must be a dictionary, not a {type(specific_options)}"
return recursive_dict_merge(inherited_options, specific_options)
def convert_validation_error_to_group(
error: ValidationError,
) -> ExceptionGroup | Exception:
if not error.context and not error.cause:
if len(error.message) > 150:
message = error.message.replace(str(error.instance), "<DSL>")
note = "With <DSL> being: " + textwrap.shorten(str(error.instance), 500)
else:
message = error.message
note = None
converted = DslValidationError(
f"Validation error at {error.json_path}: " + message
)
if note:
converted.add_note(note)
return converted
elif error.cause:
return error.cause
elif error.context:
causes = [convert_validation_error_to_group(x) for x in error.context]
message = f"Validation error at {error.json_path}, caused by a sub-exception."
return ExceptionGroup(message, causes)
else:
return error
def _validate_dsl(dsl_object: YamlObject):
"""
Validate a DSl object.
:param dsl_object: The object to validate.
:return: True if valid, False otherwise.
"""
errors = list(_SCHEMA_VALIDATOR.iter_errors(dsl_object))
if len(errors) == 1:
message = (
"Validating the DSL resulted in an error. "
"The most specific sub-exception is often the culprit. "
)
error = convert_validation_error_to_group(errors[0])
if isinstance(error, ExceptionGroup):
raise ExceptionGroup(message, error.exceptions)
else:
raise DslValidationError(message + str(error)) from error
elif len(errors) > 1:
the_errors = [convert_validation_error_to_group(e) for e in errors]
message = "Validating the DSL resulted in some errors."
raise ExceptionGroup(message, the_errors)
def _tested_type_to_value(tested_type: TestedType) -> Value:
type_enum = get_converter().structure(tested_type.type, AllTypes) # pyright: ignore
if isinstance(type_enum, NumericTypes):
# Some special cases for advanced numeric types.
if type_enum == AdvancedNumericTypes.FIXED_PRECISION:
value = Decimal(tested_type.value)
else:
basic_type = resolve_to_basic(type_enum)
if basic_type == BasicNumericTypes.INTEGER:
value = int(tested_type.value)
elif basic_type == BasicNumericTypes.REAL:
value = float(tested_type.value)
else:
raise ValueError(f"Unknown basic numeric type {type_enum}")
return NumberType(type=type_enum, data=value)
elif isinstance(type_enum, StringTypes):
return StringType(type=type_enum, data=tested_type.value)
elif isinstance(type_enum, BooleanTypes):
return BooleanType(type=type_enum, data=bool(tested_type.value))
elif isinstance(type_enum, NothingTypes):
return NothingType(type=type_enum, data=None)
elif isinstance(type_enum, SequenceTypes):
return SequenceType(
type=type_enum,
data=[_convert_value(part_value) for part_value in tested_type.value],
)
elif isinstance(type_enum, ObjectTypes):
data = []
for key, val in tested_type.value.items():
data.append(
ObjectKeyValuePair(
key=_convert_value(key),
value=_convert_value(val),
)
)
return ObjectType(type=type_enum, data=data)
raise ValueError(f"Unknown type {tested_type.type} with value {tested_type.value}")
def _convert_value(value: YamlObject) -> Value:
if isinstance(value, TestedType):
tested_type = value
else:
# Convert the value into a "TESTed" type.
if value is None:
tested_type = TestedType(value=None, type=BasicNothingTypes.NOTHING)
elif isinstance(value, str):
tested_type = TestedType(value=value, type=BasicStringTypes.TEXT)
elif isinstance(value, bool):
tested_type = TestedType(type=BasicBooleanTypes.BOOLEAN, value=value)
elif isinstance(value, int):
tested_type = TestedType(type=BasicNumericTypes.INTEGER, value=value)
elif isinstance(value, float):
tested_type = TestedType(type=BasicNumericTypes.REAL, value=value)
elif isinstance(value, list):
tested_type = TestedType(type=BasicSequenceTypes.SEQUENCE, value=value)
elif isinstance(value, set):
tested_type = TestedType(type=BasicSequenceTypes.SET, value=value)
elif isinstance(value, dict):
tested_type = TestedType(type=BasicObjectTypes.MAP, value=value)
else:
raise ValueError(f"Unknown type for value {value}.")
return _tested_type_to_value(tested_type)
def _convert_file(link_file: YamlDict) -> FileUrl:
assert isinstance(link_file["name"], str)
assert isinstance(link_file["url"], str)
return FileUrl(name=link_file["name"], url=link_file["url"])
def _convert_evaluation_function(stream: dict) -> EvaluationFunction:
return EvaluationFunction(
file=Path(stream["file"]), name=stream.get("name", "evaluate")
)
def _convert_custom_check_oracle(stream: dict) -> CustomCheckOracle:
converted_args = []
for v in stream.get("arguments", []):
cv = _convert_yaml_value(v)
assert isinstance(cv, Value)
converted_args.append(cv)
languages = stream.get("languages")
return CustomCheckOracle(
function=_convert_evaluation_function(stream),
arguments=converted_args,
languages=set(languages) if languages else None,
)
def _convert_language_specific_oracle(stream: dict) -> LanguageSpecificOracle:
the_functions = dict()
for lang, a_function in stream["functions"].items():
the_functions[SupportedLanguage(lang)] = _convert_evaluation_function(
a_function
)
the_args = dict()
for lang, args in stream.get("arguments", dict()).items():
the_args[SupportedLanguage(lang)] = args
if not set(the_args.keys()).issubset(the_functions.keys()):
raise InvalidDslError(
"Language-specific oracle found with arguments for non-oracle languages.\n\n"
f"You provided check functions for {the_functions.keys()}, but arguments for {the_args.keys()}.\n"
f"This means you have arguments for {the_args.keys() - the_functions.keys()} but no check function!"
)
return LanguageSpecificOracle(functions=the_functions, arguments=the_args)
def _convert_text_output_channel(
stream: YamlObject, context: DslContext, config_name: str
) -> TextOutputChannel:
# Get the config applicable to this level.
# Either attempt to get it from an object, or using the inherited options as is.
if isinstance(stream, str):
config = context.config.get(config_name, dict())
raw_data = stream
else:
assert isinstance(stream, dict)
config = context.merge_inheritable_with_specific_config(stream, config_name)
raw_data = str(stream["data"])
# Normalize the data if necessary.
if config.get("normalizeTrailingNewlines", True):
data = _ensure_trailing_newline(raw_data)
else:
data = raw_data
if isinstance(stream, str):
return TextOutputChannel(data=data, oracle=GenericTextOracle(options=config))
else:
assert isinstance(stream, dict)
if "oracle" not in stream or stream["oracle"] == "builtin":
return TextOutputChannel(
data=data, oracle=GenericTextOracle(options=config)
)
elif stream["oracle"] == "custom_check":
return TextOutputChannel(
data=data, oracle=_convert_custom_check_oracle(stream)
)
raise TypeError(f"Unknown text oracle type: {stream['oracle']}")
def _convert_file_output_channel(
stream: YamlObject, context: DslContext, config_name: str
) -> FileOutputChannel:
assert isinstance(stream, dict)
expected = str(stream["content"])
actual = str(stream["location"])
if "oracle" not in stream or stream["oracle"] == "builtin":
config = context.merge_inheritable_with_specific_config(stream, config_name)
if "mode" not in config:
config["mode"] = "full"
assert config["mode"] in (
"full",
"line",
), f"The file oracle only supports modes full and line, not {config['mode']}"
return FileOutputChannel(
expected_path=expected,
actual_path=actual,
oracle=GenericTextOracle(name=TextBuiltin.FILE, options=config),
)
elif stream["oracle"] == "custom_check":
return FileOutputChannel(
expected_path=expected,
actual_path=actual,
oracle=_convert_custom_check_oracle(stream),
)
raise TypeError(f"Unknown file oracle type: {stream['oracle']}")
def _convert_yaml_value(stream: YamlObject) -> Value | None:
if isinstance(stream, ExpressionString):
# We have an expression string.
value = parse_string(stream, is_return=True)
elif isinstance(stream, (int, float, bool, TestedType, list, set, str, dict)):
# Simple values where no confusion is possible.
value = _convert_value(stream)
else:
return None
assert isinstance(
value, Value
), f"{value} is not of type Value, got {type(value)} instead"
return value
def _convert_advanced_value_output_channel(stream: YamlObject) -> ValueOutputChannel:
if isinstance(stream, ReturnOracle):
return_object = stream
if "oracle" not in return_object or return_object["oracle"] == "builtin":
value = _convert_yaml_value(return_object["value"])
assert isinstance(
value, Value
), "You must specify a value for a return oracle."
return ValueOutputChannel(value=value)
elif return_object["oracle"] == "custom_check":
value = _convert_yaml_value(return_object["value"])
assert isinstance(
value, Value
), "You must specify a value for a return oracle."
return ValueOutputChannel(
value=value,
oracle=_convert_custom_check_oracle(return_object),
)
elif return_object["oracle"] == "specific_check":
return ValueOutputChannel(
oracle=_convert_language_specific_oracle(return_object)
)
raise TypeError(f"Unknown value oracle type: {return_object['oracle']}")
else:
yaml_value = _convert_yaml_value(stream)
return ValueOutputChannel(value=yaml_value)
def _validate_testcase_combinations(testcase: YamlDict):
if ("stdin" in testcase or "arguments" in testcase) and (
"statement" in testcase or "expression" in testcase
):
raise ValueError("A main call cannot contain an expression or a statement.")
if "statement" in testcase and "expression" in testcase:
raise ValueError("A statement and expression as input are mutually exclusive.")
if "statement" in testcase and "return" in testcase:
raise ValueError("A statement cannot have an expected return value.")
def _convert_testcase(testcase: YamlDict, context: DslContext) -> Testcase:
context = context.deepen_context(testcase)
# This is backwards compatability to some extend.
# TODO: remove this at some point.
if "statement" in testcase and "return" in testcase:
testcase["expression"] = testcase.pop("statement")
line_comment = ""
_validate_testcase_combinations(testcase)
if (expr_stmt := testcase.get("statement", testcase.get("expression"))) is not None:
if isinstance(expr_stmt, dict) or context.language != "tested":
if isinstance(expr_stmt, str):
the_dict = {context.language: expr_stmt}
else:
assert isinstance(expr_stmt, dict)
the_dict = expr_stmt
the_dict = {SupportedLanguage(l): cast(str, v) for l, v in the_dict.items()}
if "statement" in testcase:
the_type: Literal["statement", "expression"] = "statement"
else:
the_type: Literal["statement", "expression"] = "expression"
the_input = LanguageLiterals(literals=the_dict, type=the_type)
else:
assert isinstance(expr_stmt, str)
if testcase.get("description") is None:
line_comment = extract_comment(expr_stmt)
the_input = parse_string(expr_stmt)
return_channel = IgnoredChannel.IGNORED if "statement" in testcase else None
else:
if "stdin" in testcase:
assert isinstance(testcase["stdin"], str)
stdin = TextData(data=_ensure_trailing_newline(testcase["stdin"]))
else:
stdin = EmptyChannel.NONE
arguments = testcase.get("arguments", [])
assert isinstance(arguments, list)
the_input = MainInput(stdin=stdin, arguments=arguments)
return_channel = None
output = Output()
if return_channel:
output.result = return_channel
if (stdout := testcase.get("stdout")) is not None:
output.stdout = _convert_text_output_channel(stdout, context, "stdout")
if (file := testcase.get("file")) is not None:
output.file = _convert_file_output_channel(file, context, "file")
if (stderr := testcase.get("stderr")) is not None:
output.stderr = _convert_text_output_channel(stderr, context, "stderr")
if (exception := testcase.get("exception")) is not None:
if isinstance(exception, str):
message = exception
types = None
else:
assert isinstance(exception, dict)
message = exception.get("message")
assert isinstance(message, str)
assert isinstance(exception["types"], dict)
types = _convert_language_dictionary(
cast(dict[str, str], exception["types"])
)
output.exception = ExceptionOutputChannel(
exception=ExpectedException(message=message, types=types)
)
if (exit_code := testcase.get("exit_code")) is not None:
output.exit_code = ExitCodeOutputChannel(value=cast(int, exit_code))
if (result := testcase.get("return")) is not None:
assert not return_channel
output.result = _convert_advanced_value_output_channel(result)
if (description := testcase.get("description")) is not None:
if isinstance(description, str):
the_description = description
else:
assert isinstance(description, dict)
dd = description["description"]
assert isinstance(
dd, str
), f"The description.description field must be a string, got {dd!r}."
df = description.get("format", "text")
assert isinstance(
df, str
), f"The description.format field must be a string, got {df!r}."
the_description = ExtendedMessage(
description=dd,
format=df,
)
else:
the_description = None
return Testcase(
description=the_description,
input=the_input,
output=output,
link_files=context.files,
line_comment=line_comment,
)
def _convert_context(context: YamlDict, dsl_context: DslContext) -> Context:
dsl_context = dsl_context.deepen_context(context)
raw_testcases = context.get("script", context.get("testcases"))
assert isinstance(raw_testcases, list)
testcases = _convert_dsl_list(raw_testcases, dsl_context, _convert_testcase)
return Context(testcases=testcases)
def _convert_tab(tab: YamlDict, context: DslContext) -> Tab:
"""
Translate a DSL tab to a full test suite tab.
:param tab: The tab to translate.
:param context: The context with config for the parent level.
:return: A full tab.
"""
context = context.deepen_context(tab)
name = tab.get("unit", tab.get("tab"))
assert isinstance(name, str)
# The tab can have testcases or contexts.
if "contexts" in tab:
assert isinstance(tab["contexts"], list)
contexts = _convert_dsl_list(tab["contexts"], context, _convert_context)
elif "cases" in tab:
assert "unit" in tab
# We have testcases N.S. / contexts O.S.
assert isinstance(tab["cases"], list)
contexts = _convert_dsl_list(tab["cases"], context, _convert_context)
elif "testcases" in tab:
# We have scripts N.S. / testcases O.S.
assert "tab" in tab
assert isinstance(tab["testcases"], list)
testcases = _convert_dsl_list(tab["testcases"], context, _convert_testcase)
contexts = [Context(testcases=[t]) for t in testcases]
else:
assert "scripts" in tab
assert isinstance(tab["scripts"], list)
testcases = _convert_dsl_list(tab["scripts"], context, _convert_testcase)
contexts = [Context(testcases=[t]) for t in testcases]
return Tab(name=name, contexts=contexts)
T = TypeVar("T")
def _convert_dsl_list(
dsl_list: list, context: DslContext, converter: Callable[[YamlDict, DslContext], T]
) -> list[T]:
"""
Convert a list of YAML objects into a test suite object.
"""
objects = []
for dsl_object in dsl_list:
assert isinstance(dsl_object, dict)
objects.append(converter(dsl_object, context))
return objects
def _convert_dsl(dsl_object: YamlObject) -> Suite:
"""
Translate a DSL test suite into a full test suite.
This function assumes the DSL object has been validated;
errors might not be presented in the best way here.
:param dsl_object: A validated DSL test suite object.
:return: A full test suite.
"""
context = DslContext()
if isinstance(dsl_object, list):
namespace = None
tab_list = dsl_object
else:
assert isinstance(dsl_object, dict)
namespace = dsl_object.get("namespace")
context = context.deepen_context(dsl_object)
tab_list = dsl_object.get("units", dsl_object.get("tabs"))
assert isinstance(tab_list, list)
if (language := dsl_object.get("language", "tested")) != "tested":
language = SupportedLanguage(language)
context = evolve(context, language=language)
tabs = _convert_dsl_list(tab_list, context, _convert_tab)
if namespace:
assert isinstance(namespace, str)
return Suite(tabs=tabs, namespace=namespace)
else:
return Suite(tabs=tabs)
def parse_dsl(dsl_string: str) -> Suite:
"""
Parse a string containing a DSL test suite into our representation,
a test suite.
:param dsl_string: The string containing a DSL.
:return: The parsed and converted test suite.
"""
dsl_object = _parse_yaml(dsl_string)
_validate_dsl(dsl_object)
return _convert_dsl(dsl_object)
def translate_to_test_suite(dsl_string: str) -> str:
"""
Convert a DSL to a test suite.
:param dsl_string: The DSL.
:return: The test suite.
"""
suite = parse_dsl(dsl_string)
return suite_to_json(suite)