1 """Test suite for 2to3's parser and grammar files.
3 This is the place to add tests for changes to 2to3's grammar, such as those
4 merging the grammars for Python 2 and 3. In addition to specific tests for
5 parts of the grammar we've changed, we also make sure we can parse the
6 test_grammar.py files from both Python 2 and Python 3.
11 from .support
import driver
, test_dir
17 from lib2to3
.pgen2
import tokenize
18 from ..pgen2
.parse
import ParseError
21 class GrammarTest(support
.TestCase
):
22 def validate(self
, code
):
23 support
.parse_string(code
)
25 def invalid_syntax(self
, code
):
31 raise AssertionError("Syntax shouldn't have been valid")
34 class TestRaiseChanges(GrammarTest
):
35 def test_2x_style_1(self
):
36 self
.validate("raise")
38 def test_2x_style_2(self
):
39 self
.validate("raise E, V")
41 def test_2x_style_3(self
):
42 self
.validate("raise E, V, T")
44 def test_2x_style_invalid_1(self
):
45 self
.invalid_syntax("raise E, V, T, Z")
47 def test_3x_style(self
):
48 self
.validate("raise E1 from E2")
50 def test_3x_style_invalid_1(self
):
51 self
.invalid_syntax("raise E, V from E1")
53 def test_3x_style_invalid_2(self
):
54 self
.invalid_syntax("raise E from E1, E2")
56 def test_3x_style_invalid_3(self
):
57 self
.invalid_syntax("raise from E1, E2")
59 def test_3x_style_invalid_4(self
):
60 self
.invalid_syntax("raise E from")
63 # Adapated from Python 3's Lib/test/test_grammar.py:GrammarTests.testFuncdef
64 class TestFunctionAnnotations(GrammarTest
):
66 self
.validate("""def f(x) -> list: pass""")
69 self
.validate("""def f(x:int): pass""")
72 self
.validate("""def f(*x:str): pass""")
75 self
.validate("""def f(**x:float): pass""")
78 self
.validate("""def f(x, y:1+2): pass""")
81 self
.validate("""def f(a, (b:1, c:2, d)): pass""")
84 self
.validate("""def f(a, (b:1, c:2, d), e:3=4, f=5, *g:6): pass""")
87 s
= """def f(a, (b:1, c:2, d), e:3=4, f=5,
88 *g:6, h:7, i=8, j:9=10, **k:11) -> 12: pass"""
92 class TestExcept(GrammarTest
):
110 # Adapted from Python 3's Lib/test/test_grammar.py:GrammarTests.testAtoms
111 class TestSetLiteral(GrammarTest
):
113 self
.validate("""x = {'one'}""")
116 self
.validate("""x = {'one', 1,}""")
119 self
.validate("""x = {'one', 'two', 'three'}""")
122 self
.validate("""x = {2, 3, 4,}""")
125 class TestNumericLiterals(GrammarTest
):
126 def test_new_octal_notation(self
):
127 self
.validate("""0o7777777777777""")
128 self
.invalid_syntax("""0o7324528887""")
130 def test_new_binary_notation(self
):
131 self
.validate("""0b101010""")
132 self
.invalid_syntax("""0b0101021""")
135 class TestClassDef(GrammarTest
):
136 def test_new_syntax(self
):
137 self
.validate("class B(t=7): pass")
138 self
.validate("class B(t, *args): pass")
139 self
.validate("class B(t, **kwargs): pass")
140 self
.validate("class B(t, *args, **kwargs): pass")
141 self
.validate("class B(t, y=9, *args, **kwargs): pass")
144 class TestParserIdempotency(support
.TestCase
):
146 """A cut-down version of pytree_idempotency.py."""
148 def test_all_project_files(self
):
149 for filepath
in support
.all_project_files():
150 with
open(filepath
, "rb") as fp
:
151 encoding
= tokenize
.detect_encoding(fp
.readline
)[0]
155 source
= source
.decode(encoding
)
156 tree
= driver
.parse_string(source
)
159 new
= new
.encode(encoding
)
160 if diff(filepath
, new
):
161 self
.fail("Idempotency failed: %s" % filepath
)
163 def test_extended_unpacking(self
):
164 driver
.parse_string("a, *b, c = x\n")
165 driver
.parse_string("[*a, b] = x\n")
166 driver
.parse_string("(z, *y, w) = m\n")
167 driver
.parse_string("for *z, m in d: pass\n")
169 class TestLiterals(GrammarTest
):
171 def validate(self
, s
):
172 driver
.parse_string(support
.dedent(s
) + "\n\n")
174 def test_multiline_bytes_literals(self
):
176 md5test(b"\xaa" * 80,
177 (b"Test Using Larger Than Block-Size Key "
178 b"and Larger Than One Block-Size Data"),
179 "6f630fad67cda0ee1fb1f562db3aa53e")
183 def test_multiline_bytes_tripquote_literals(self
):
186 <?xml version="1.0" encoding="UTF-8"?>
187 <!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN">
192 def test_multiline_str_literals(self
):
195 ("Test Using Larger Than Block-Size Key "
196 "and Larger Than One Block-Size Data"),
197 "6f630fad67cda0ee1fb1f562db3aa53e")
202 def diff(fn
, result
):
209 return os
.system("diff -u %s @" % fn
)