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

Method test_optional_operator

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

Source from the content-addressed store, hash-verified

132 )
133
134 def test_optional_operator(self) -> None:
135 grammar = """
136 start: sum NEWLINE
137 sum: term ('+' term)?
138 term: NUMBER
139 """
140 parser_class = make_parser(grammar)
141 node = parse_string("1 + 2\n", parser_class)
142 self.assertEqual(
143 node,
144 [
145 [
146 TokenInfo(
147 NUMBER, string="1", start=(1, 0), end=(1, 1), line="1 + 2\n"
148 ),
149 [
150 TokenInfo(
151 OP, string="+", start=(1, 2), end=(1, 3), line="1 + 2\n"
152 ),
153 TokenInfo(
154 NUMBER, string="2", start=(1, 4), end=(1, 5), line="1 + 2\n"
155 ),
156 ],
157 ],
158 TokenInfo(
159 NEWLINE, string="\n", start=(1, 5), end=(1, 6), line="1 + 2\n"
160 ),
161 ],
162 )
163 node = parse_string("1\n", parser_class)
164 self.assertEqual(
165 node,
166 [
167 [
168 TokenInfo(NUMBER, string="1", start=(1, 0), end=(1, 1), line="1\n"),
169 None,
170 ],
171 TokenInfo(NEWLINE, string="\n", start=(1, 1), end=(1, 2), line="1\n"),
172 ],
173 )
174
175 def test_optional_literal(self) -> None:
176 grammar = """

Callers

nothing calls this directly

Calls 4

make_parserFunction · 0.90
parse_stringFunction · 0.90
TokenInfoClass · 0.90
assertEqualMethod · 0.45

Tested by

no test coverage detected