MCPcopy Index your code
hub / github.com/python/cpython / test_parse_grammar

Method test_parse_grammar

Lib/test/test_peg_generator/test_pegen.py:23–43  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

21
22class TestPegen(unittest.TestCase):
23 def test_parse_grammar(self) -> None:
24 grammar_source = """
25 start: sum NEWLINE
26 sum: t1=term '+' t2=term { action } | term
27 term: NUMBER
28 """
29 expected = """
30 start: sum NEWLINE
31 sum: term '+' term | term
32 term: NUMBER
33 """
34 grammar: Grammar = parse_string(grammar_source, GrammarParser)
35 rules = grammar.rules
36 self.assertEqual(str(grammar), textwrap.dedent(expected).strip())
37 # Check the str() and repr() of a few rules; AST nodes don't support ==.
38 self.assertEqual(str(rules["start"]), "start: sum NEWLINE")
39 self.assertEqual(str(rules["sum"]), "sum: term '+' term | term")
40 expected_repr = (
41 "Rule('term', None, Rhs([Alt([NamedItem(None, NameLeaf('NUMBER'))])]))"
42 )
43 self.assertEqual(repr(rules["term"]), expected_repr)
44
45 def test_repeated_rules(self) -> None:
46 grammar_source = """

Callers

nothing calls this directly

Calls 5

parse_stringFunction · 0.90
strFunction · 0.85
assertEqualMethod · 0.45
stripMethod · 0.45
dedentMethod · 0.45

Tested by

no test coverage detected