Castle: The best Real-Time/Embedded/HighTech language EVER. Attempt 2
Revisão | 1cf678da302c684a3d0b34a7505222c95c1043a1 (tree) |
---|---|
Hora | 2022-01-02 04:32:19 |
Autor | Albert Mietus < albert AT mietus DOT nl > |
Commiter | Albert Mietus < albert AT mietus DOT nl > |
refactored tests: added more assert_XXX()
@@ -1,6 +1,9 @@ | ||
1 | 1 | import visitor |
2 | 2 | import arpeggio |
3 | 3 | |
4 | +import sys; sys.path.append("./../AST/") ; sys.path.append("./../../AST/") | |
5 | +from castle import peg # has the AST clases | |
6 | + | |
4 | 7 | def parse(txt, rule): |
5 | 8 | parser = arpeggio.ParserPython(rule) |
6 | 9 | pt = parser.parse(txt) |
@@ -8,3 +11,10 @@ | ||
8 | 11 | ast = arpeggio.visit_parse_tree(pt, visitor.PegVisitor()) |
9 | 12 | assert ast.position == 0 and ast.position_end == len(txt), f"Also the AST (type={type(ast)}) should include all input" |
10 | 13 | return ast |
14 | + | |
15 | + | |
16 | +def assert_ID(id, name:str=None, err_message="Not correct Name"): | |
17 | + assert name is not None | |
18 | + assert isinstance(id, peg.ID), "The id should be an ID" | |
19 | + peg.ID.validate_or_raise(id) # with correct syntax | |
20 | + assert id.name == name, err_message if err_message else f"Note correct name, expected {name}" |
@@ -7,32 +7,34 @@ | ||
7 | 7 | |
8 | 8 | from . import parse |
9 | 9 | |
10 | +def assert_Term(term, term_type:type(peg.Terminal), value:str=None, err_message="Not correct value"): | |
11 | + assert value is not None | |
12 | + assert isinstance(term, peg.Terminal), "It should be a term ..." | |
13 | + assert isinstance(term, term_type), f"... of the correct kind: {term_type} " | |
14 | + assert term.value == value, err_message if err_message else f"Note correct name, expected {name}" | |
15 | + | |
16 | + | |
10 | 17 | def test_simple_str(): |
11 | 18 | txt="'a string'" |
12 | 19 | ast = parse(txt, grammar.term) |
13 | - assert isinstance(ast, peg.Terminal), "It should be a term ..." | |
14 | - assert isinstance(ast, peg.StrTerm), "... and a str" | |
15 | - assert ast.value == "a string", "It's correct value should be without quotes" | |
20 | + assert_Term(ast, peg.StrTerm, txt[1:-1], "It's correct value should be without quotes") | |
21 | + | |
16 | 22 | |
17 | 23 | def test_simple_str_d3(): |
18 | 24 | txt='"""triple string"""' # A triple double quotes in Castle is also a simple string |
19 | 25 | ast = parse(txt, grammar.term) |
20 | - assert isinstance(ast, peg.Terminal), "It should be a term ..." | |
21 | - assert isinstance(ast, peg.StrTerm), "... and a str" | |
22 | - assert ast.value == "triple string", "It's correct value should be without quotes" | |
26 | + assert_Term(ast, peg.StrTerm, txt[3:-3], "It's correct value should be without quotes") | |
23 | 27 | |
24 | 28 | def test_regex_RE(): |
25 | 29 | txt='/a reg.ex/' |
26 | 30 | ast = parse(txt, grammar.term) |
27 | - assert isinstance(ast, peg.Terminal), "It should be a term ..." | |
28 | - assert isinstance(ast, peg.RegExpTerm), "... and a RegExp" | |
29 | - assert ast.value == 'a reg.ex', "It's correct value should be without slahes -- note: the regex itself is a string" | |
31 | + assert_Term(ast, peg.RegExpTerm, txt[1:-1], "It's correct value should be without slahes -- note: the regex itself is a string") | |
32 | + | |
33 | + | |
30 | 34 | |
31 | 35 | def regex_variants(txt, expect): |
32 | 36 | ast = parse(txt, grammar.term) |
33 | - assert isinstance(ast, peg.Terminal), "It should be a term ..." | |
34 | - assert isinstance(ast, peg.RegExpTerm), "... and a RegExp" | |
35 | - assert ast.value == expect, "And the regex-pre/postfix should be removed from the value" | |
37 | + assert_Term(ast, peg.RegExpTerm, expect, "And the regex-pre/postfix should be removed from the value") | |
36 | 38 | |
37 | 39 | def test_regex_variants(): |
38 | 40 | regex_variants(txt:="""/a reg.ex/""", expect=txt[1:-1]) # Same a test_regex_RE |
@@ -48,13 +50,6 @@ | ||
48 | 50 | regex_variants(txt:='''R"""re__Rstr_d3"""''', expect=txt[4:-3]) |
49 | 51 | regex_variants(txt:='''r"""re__rstr_d3"""''', expect=txt[4:-3]) |
50 | 52 | |
51 | -@pytest.mark.skip(reason="single_expr has no visitor -- as that is now in expressions") | |
52 | -def test_term_as_single_expr(): # A term is **ALSO** a single_expr | |
53 | - txt="'a string'" | |
54 | - ast = parse(txt, grammar.single_expr) | |
55 | - assert isinstance(ast, peg.Expression), "A (str)term is also an Expression" | |
56 | - assert len(ast.value) == 1, "An expression with length==1" | |
57 | - assert ast.value[0].value == txt[1:-1], "It's correct value should be without quotes" | |
58 | 53 | |
59 | 54 | |
60 | 55 | def test_term_as_expressions(): # A term is **ALSO an expressions |
@@ -5,36 +5,33 @@ | ||
5 | 5 | import sys; sys.path.append("./../AST/") ; sys.path.append("./../../AST/") |
6 | 6 | from castle import peg # has the AST clases |
7 | 7 | |
8 | -from . import parse | |
8 | +from . import parse, assert_ID | |
9 | + | |
9 | 10 | |
10 | 11 | def test_rule_name(): |
11 | 12 | """The name of a rule is an ID""" |
13 | + | |
12 | 14 | txt="aName" |
13 | 15 | ast = parse(txt, grammar.rule_name) |
14 | - assert isinstance(ast, peg.ID), "It should be an ID" | |
15 | - assert ast.name == txt | |
16 | + assert_ID(ast, name=txt) | |
17 | + | |
16 | 18 | |
17 | 19 | def test_rule_crossref(): |
18 | 20 | """The rule's expressions can also refer an ID""" |
21 | + | |
19 | 22 | txt="aRef" |
20 | 23 | ast = parse(txt, grammar.rule_crossref) |
21 | - assert isinstance(ast, peg.ID), "It should be an ID" | |
22 | - assert ast.name == txt | |
23 | - | |
24 | - | |
25 | -@pytest.mark.skip(reason="single_expr has no visitor -- as that is now in expressions") | |
26 | -def test_ID_as_single_expr(): | |
27 | - txt="aRef" | |
28 | - ast = parse(txt, grammar.single_expr) | |
29 | - assert isinstance(ast, peg.Expression), "A crossref is also an Expression" | |
30 | - assert len(ast.value) == 1, "An expression with length==1" | |
31 | - assert ast.value[0].name == txt, "The name of the (ID of the) Expression-value is still the same" | |
24 | + assert_ID(ast, name=txt) | |
32 | 25 | |
33 | 26 | |
34 | 27 | def test_ID_as_expressions(): |
28 | + """ An ID is also an expression""" | |
29 | + | |
35 | 30 | txt="aRef" |
36 | 31 | ast = parse(txt, grammar.expressions) |
32 | + | |
37 | 33 | assert isinstance(ast, peg.Expression), "A crossref is also an Expression" |
38 | 34 | assert len(ast.value) == 1, "An expression with length==1" |
39 | - assert ast.value[0].name == txt, "The name of the (ID of the) Expression-value is still the same" | |
35 | + assert_ID(ast.value[0], name=txt, err_message= "The name of the (ID of the) Expression-value is still the same") | |
40 | 36 | |
37 | + |
@@ -5,13 +5,7 @@ | ||
5 | 5 | import sys; sys.path.append("./../AST/") ; sys.path.append("./../../AST/") |
6 | 6 | from castle import peg # has the AST clases |
7 | 7 | |
8 | -from . import parse | |
9 | - | |
10 | -def assert_ID(id, name:str=None, err_message="Not correct Name"): | |
11 | - assert name is not None | |
12 | - assert isinstance(id, peg.ID), "The id should be an ID" | |
13 | - peg.ID.validate_or_raise(id) # with correct syntax | |
14 | - assert id.name == name, err_message if err_message else f"Note correct name, expected {name}" | |
8 | +from . import parse, assert_ID | |
15 | 9 | |
16 | 10 | |
17 | 11 | def test_trivial_rule_with_2IDS(): |