Castle: The best Real-Time/Embedded/HighTech language EVER. Attempt 2
Revision | 85205067ff095f7a6f6458b47b5b8463aeae1f3a (tree) |
---|---|
Time | 2022-01-18 05:09:49 |
Author | Albert Mietus < albert AT mietus DOT nl > |
Commiter | Albert Mietus < albert AT mietus DOT nl > |
refactored
@@ -23,17 +23,18 @@ | ||
23 | 23 | ast = parse(txt, grammar.term) |
24 | 24 | assert_Term(ast, peg.StrTerm, txt[3:-3], "It's correct value should be without quotes") |
25 | 25 | |
26 | + | |
26 | 27 | def test_regex_RE(): |
27 | 28 | txt='/a reg.ex/' |
28 | 29 | ast = parse(txt, grammar.term) |
29 | 30 | assert_Term(ast, peg.RegExpTerm, txt[1:-1], "It's correct value should be without slahes -- note: the regex itself is a string") |
30 | 31 | |
31 | 32 | |
32 | - | |
33 | 33 | def regex_variants(txt, expect): |
34 | 34 | ast = parse(txt, grammar.term) |
35 | 35 | assert_Term(ast, peg.RegExpTerm, expect, "And the regex-pre/postfix should be removed from the value") |
36 | 36 | |
37 | + | |
37 | 38 | def test_regex_variants(): |
38 | 39 | regex_variants(txt:="""/a reg.ex/""", expect=txt[1:-1]) # Same a test_regex_RE |
39 | 40 | regex_variants(txt:="""/re_slash/""", expect=txt[1:-1]) |
@@ -49,7 +50,6 @@ | ||
49 | 50 | regex_variants(txt:='''r"""re__rstr_d3"""''', expect=txt[4:-3]) |
50 | 51 | |
51 | 52 | |
52 | - | |
53 | 53 | def test_term_as_expressions(): # A term is **ALSO an expressions |
54 | 54 | txt="'a string'" |
55 | 55 | ast = parse(txt, grammar.expressions) |
@@ -57,5 +57,3 @@ | ||
57 | 57 | assert isinstance(ast, peg.Expression), "A (str)term is also an Expression" |
58 | 58 | assert len(ast.value) == 1, "An expression with length==1" |
59 | 59 | assert ast.value[0].value == txt[1:-1], "It's correct value should be without quotes" |
60 | - | |
61 | - |
@@ -5,6 +5,7 @@ | ||
5 | 5 | |
6 | 6 | from . import parse, assert_ID |
7 | 7 | |
8 | + | |
8 | 9 | def test_rule_name(): |
9 | 10 | """The name of a rule is an ID""" |
10 | 11 |
@@ -30,5 +31,3 @@ | ||
30 | 31 | assert isinstance(ast, peg.Expression), "A crossref is also an Expression" |
31 | 32 | assert len(ast.value) == 1, "An expression with length==1" |
32 | 33 | assert_ID(ast.value[0], name=txt, err_message= "The name of the (ID of the) Expression-value is still the same") |
33 | - | |
34 | - |
@@ -10,6 +10,14 @@ | ||
10 | 10 | |
11 | 11 | from . import parse, assert_ID |
12 | 12 | |
13 | + | |
14 | +def assert_Seq(ast, length=None): | |
15 | + assert isinstance(ast, peg.Sequence) | |
16 | + assert isinstance(ast, peg.Expression), "A sequence is aslo an Expression()" | |
17 | + if length: | |
18 | + assert len(ast) == length, f" ... of specified length=={length}" | |
19 | + | |
20 | + | |
13 | 21 | def test_seq_of_one_as_single_expr(): |
14 | 22 | txt = "A" |
15 | 23 | ast = parse(txt, grammar.single_expr) |
@@ -26,22 +34,17 @@ | ||
26 | 34 | txt = "A B" |
27 | 35 | ast = parse(txt, grammar.expressions) |
28 | 36 | |
29 | - assert isinstance(ast, peg.Expression), "Two sequence of two expr is an Expression()" | |
30 | - assert isinstance(ast, peg.Sequence), " ... and a Sequence()" | |
31 | - assert len(ast) == 2, " ... of length==2" | |
32 | - | |
37 | + assert_Seq(ast, 2) | |
33 | 38 | assert isinstance(ast.value, list), "It will be an `arpeggio.SemanticActionResult` which is a subclass of list" |
34 | 39 | assert_ID(ast[0], 'A'), " ... the first one is ID('A')" |
35 | 40 | assert_ID(ast[1], 'B'), "... and the 2nd: ID('B')" |
36 | 41 | |
37 | 42 | |
38 | -def test_seq_of_thre_with_quantification(): | |
43 | +def test_seq_of_three_with_quantification(): | |
39 | 44 | txt = "A? B+ C*" |
40 | 45 | ast = parse(txt, grammar.expressions) |
41 | 46 | |
42 | - assert isinstance(ast, peg.Expression), "Two sequence of two expr is an Expression()" | |
43 | - assert isinstance(ast, peg.Sequence), " ... and a Sequence()" | |
44 | - assert len(ast) == 3, " ... of length==3" | |
47 | + assert_Seq(ast, 3) | |
45 | 48 | |
46 | 49 | assert isinstance(ast[0], peg.Optional) |
47 | 50 | assert isinstance(ast[1], peg.OneOrMore) |