Mercurial > cpython-lru_cache-weakref
annotate Lib/lib2to3/tests/test_parser.py @ 79675:c0a2294de787 2.7
Issue #16199: Backport 2to3 typo fix from 3.2.
| author | Chris Jerdonek <chris.jerdonek@gmail.com> |
|---|---|
| date | Thu, 11 Oct 2012 16:15:46 -0700 |
| parents | b36cb4602e21 |
| children | 5295ed192ffd |
| rev | line source |
|---|---|
|
58737
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
1 """Test suite for 2to3's parser and grammar files. |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
2 |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
3 This is the place to add tests for changes to 2to3's grammar, such as those |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
4 merging the grammars for Python 2 and 3. In addition to specific tests for |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
5 parts of the grammar we've changed, we also make sure we can parse the |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
6 test_grammar.py files from both Python 2 and Python 3. |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
7 """ |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
8 |
|
61376
e92fb95f2317
Merged revisions 80934 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
59922
diff
changeset
|
9 from __future__ import with_statement |
|
e92fb95f2317
Merged revisions 80934 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
59922
diff
changeset
|
10 |
|
58737
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
11 # Testing imports |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
12 from . import support |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
13 from .support import driver, test_dir |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
14 |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
15 # Python imports |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
16 import os |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
17 import sys |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
18 |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
19 # Local imports |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
20 from lib2to3.pgen2 import tokenize |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
21 from ..pgen2.parse import ParseError |
|
68045
a58129cdd365
Merged revisions 88535,88661 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
61375
diff
changeset
|
22 from lib2to3.pygram import python_symbols as syms |
|
a58129cdd365
Merged revisions 88535,88661 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
61375
diff
changeset
|
23 |
|
a58129cdd365
Merged revisions 88535,88661 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
61375
diff
changeset
|
24 |
|
a58129cdd365
Merged revisions 88535,88661 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
61375
diff
changeset
|
25 class TestDriver(support.TestCase): |
|
a58129cdd365
Merged revisions 88535,88661 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
61375
diff
changeset
|
26 |
|
a58129cdd365
Merged revisions 88535,88661 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
61375
diff
changeset
|
27 def test_formfeed(self): |
|
a58129cdd365
Merged revisions 88535,88661 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
61375
diff
changeset
|
28 s = """print 1\n\x0Cprint 2\n""" |
|
a58129cdd365
Merged revisions 88535,88661 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
61375
diff
changeset
|
29 t = driver.parse_string(s) |
|
a58129cdd365
Merged revisions 88535,88661 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
61375
diff
changeset
|
30 self.assertEqual(t.children[0].children[0].type, syms.print_stmt) |
|
a58129cdd365
Merged revisions 88535,88661 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
61375
diff
changeset
|
31 self.assertEqual(t.children[1].children[0].type, syms.print_stmt) |
|
58737
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
32 |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
33 |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
34 class GrammarTest(support.TestCase): |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
35 def validate(self, code): |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
36 support.parse_string(code) |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
37 |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
38 def invalid_syntax(self, code): |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
39 try: |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
40 self.validate(code) |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
41 except ParseError: |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
42 pass |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
43 else: |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
44 raise AssertionError("Syntax shouldn't have been valid") |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
45 |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
46 |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
47 class TestRaiseChanges(GrammarTest): |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
48 def test_2x_style_1(self): |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
49 self.validate("raise") |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
50 |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
51 def test_2x_style_2(self): |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
52 self.validate("raise E, V") |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
53 |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
54 def test_2x_style_3(self): |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
55 self.validate("raise E, V, T") |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
56 |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
57 def test_2x_style_invalid_1(self): |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
58 self.invalid_syntax("raise E, V, T, Z") |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
59 |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
60 def test_3x_style(self): |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
61 self.validate("raise E1 from E2") |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
62 |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
63 def test_3x_style_invalid_1(self): |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
64 self.invalid_syntax("raise E, V from E1") |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
65 |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
66 def test_3x_style_invalid_2(self): |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
67 self.invalid_syntax("raise E from E1, E2") |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
68 |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
69 def test_3x_style_invalid_3(self): |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
70 self.invalid_syntax("raise from E1, E2") |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
71 |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
72 def test_3x_style_invalid_4(self): |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
73 self.invalid_syntax("raise E from") |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
74 |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
75 |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
76 # Adapated from Python 3's Lib/test/test_grammar.py:GrammarTests.testFuncdef |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
77 class TestFunctionAnnotations(GrammarTest): |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
78 def test_1(self): |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
79 self.validate("""def f(x) -> list: pass""") |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
80 |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
81 def test_2(self): |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
82 self.validate("""def f(x:int): pass""") |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
83 |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
84 def test_3(self): |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
85 self.validate("""def f(*x:str): pass""") |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
86 |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
87 def test_4(self): |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
88 self.validate("""def f(**x:float): pass""") |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
89 |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
90 def test_5(self): |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
91 self.validate("""def f(x, y:1+2): pass""") |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
92 |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
93 def test_6(self): |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
94 self.validate("""def f(a, (b:1, c:2, d)): pass""") |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
95 |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
96 def test_7(self): |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
97 self.validate("""def f(a, (b:1, c:2, d), e:3=4, f=5, *g:6): pass""") |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
98 |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
99 def test_8(self): |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
100 s = """def f(a, (b:1, c:2, d), e:3=4, f=5, |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
101 *g:6, h:7, i=8, j:9=10, **k:11) -> 12: pass""" |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
102 self.validate(s) |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
103 |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
104 |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
105 class TestExcept(GrammarTest): |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
106 def test_new(self): |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
107 s = """ |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
108 try: |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
109 x |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
110 except E as N: |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
111 y""" |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
112 self.validate(s) |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
113 |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
114 def test_old(self): |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
115 s = """ |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
116 try: |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
117 x |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
118 except E, N: |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
119 y""" |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
120 self.validate(s) |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
121 |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
122 |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
123 # Adapted from Python 3's Lib/test/test_grammar.py:GrammarTests.testAtoms |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
124 class TestSetLiteral(GrammarTest): |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
125 def test_1(self): |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
126 self.validate("""x = {'one'}""") |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
127 |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
128 def test_2(self): |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
129 self.validate("""x = {'one', 1,}""") |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
130 |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
131 def test_3(self): |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
132 self.validate("""x = {'one', 'two', 'three'}""") |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
133 |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
134 def test_4(self): |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
135 self.validate("""x = {2, 3, 4,}""") |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
136 |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
137 |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
138 class TestNumericLiterals(GrammarTest): |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
139 def test_new_octal_notation(self): |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
140 self.validate("""0o7777777777777""") |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
141 self.invalid_syntax("""0o7324528887""") |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
142 |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
143 def test_new_binary_notation(self): |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
144 self.validate("""0b101010""") |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
145 self.invalid_syntax("""0b0101021""") |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
146 |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
147 |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
148 class TestClassDef(GrammarTest): |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
149 def test_new_syntax(self): |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
150 self.validate("class B(t=7): pass") |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
151 self.validate("class B(t, *args): pass") |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
152 self.validate("class B(t, **kwargs): pass") |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
153 self.validate("class B(t, *args, **kwargs): pass") |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
154 self.validate("class B(t, y=9, *args, **kwargs): pass") |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
155 |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
156 |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
157 class TestParserIdempotency(support.TestCase): |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
158 |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
159 """A cut-down version of pytree_idempotency.py.""" |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
160 |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
161 def test_all_project_files(self): |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
162 if sys.platform.startswith("win"): |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
163 # XXX something with newlines goes wrong on Windows. |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
164 return |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
165 for filepath in support.all_project_files(): |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
166 with open(filepath, "rb") as fp: |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
167 encoding = tokenize.detect_encoding(fp.readline)[0] |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
168 self.assertTrue(encoding is not None, |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
169 "can't detect encoding for %s" % filepath) |
|
61376
e92fb95f2317
Merged revisions 80934 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
59922
diff
changeset
|
170 with open(filepath, "r") as fp: |
|
58737
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
171 source = fp.read() |
|
61376
e92fb95f2317
Merged revisions 80934 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
59922
diff
changeset
|
172 source = source.decode(encoding) |
|
58737
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
173 tree = driver.parse_string(source) |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
174 new = unicode(tree) |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
175 if diff(filepath, new, encoding): |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
176 self.fail("Idempotency failed: %s" % filepath) |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
177 |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
178 def test_extended_unpacking(self): |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
179 driver.parse_string("a, *b, c = x\n") |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
180 driver.parse_string("[*a, b] = x\n") |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
181 driver.parse_string("(z, *y, w) = m\n") |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
182 driver.parse_string("for *z, m in d: pass\n") |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
183 |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
184 class TestLiterals(GrammarTest): |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
185 |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
186 def validate(self, s): |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
187 driver.parse_string(support.dedent(s) + "\n\n") |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
188 |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
189 def test_multiline_bytes_literals(self): |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
190 s = """ |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
191 md5test(b"\xaa" * 80, |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
192 (b"Test Using Larger Than Block-Size Key " |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
193 b"and Larger Than One Block-Size Data"), |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
194 "6f630fad67cda0ee1fb1f562db3aa53e") |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
195 """ |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
196 self.validate(s) |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
197 |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
198 def test_multiline_bytes_tripquote_literals(self): |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
199 s = ''' |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
200 b""" |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
201 <?xml version="1.0" encoding="UTF-8"?> |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
202 <!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN"> |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
203 """ |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
204 ''' |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
205 self.validate(s) |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
206 |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
207 def test_multiline_str_literals(self): |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
208 s = """ |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
209 md5test("\xaa" * 80, |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
210 ("Test Using Larger Than Block-Size Key " |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
211 "and Larger Than One Block-Size Data"), |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
212 "6f630fad67cda0ee1fb1f562db3aa53e") |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
213 """ |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
214 self.validate(s) |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
215 |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
216 |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
217 def diff(fn, result, encoding): |
|
61376
e92fb95f2317
Merged revisions 80934 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
59922
diff
changeset
|
218 f = open("@", "w") |
|
58737
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
219 try: |
|
61376
e92fb95f2317
Merged revisions 80934 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
59922
diff
changeset
|
220 f.write(result.encode(encoding)) |
|
58737
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
221 finally: |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
222 f.close() |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
223 try: |
|
59922
5b679c249e3c
Merged revisions 79138-79139,79306 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
58737
diff
changeset
|
224 fn = fn.replace('"', '\\"') |
|
5b679c249e3c
Merged revisions 79138-79139,79306 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
58737
diff
changeset
|
225 return os.system('diff -u "%s" @' % fn) |
|
58737
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
226 finally: |
|
fab380674959
copy lib2to3 from the trunk
Benjamin Peterson <benjamin@python.org>
parents:
diff
changeset
|
227 os.remove("@") |
