]>
git.proxmox.com Git - mirror_edk2.git/blob - AppPkg/Applications/Python/Python-2.7.2/Lib/lib2to3/tests/test_parser.py
c3b8f98708dfeb856cee5dec947beba8694c5919
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.
9 from __future__
import with_statement
13 from .support
import driver
, test_dir
20 from lib2to3
.pgen2
import tokenize
21 from ..pgen2
.parse
import ParseError
22 from lib2to3
.pygram
import python_symbols
as syms
25 class TestDriver(support
.TestCase
):
27 def test_formfeed(self
):
28 s
= """print 1\n\x0Cprint 2\n"""
29 t
= driver
.parse_string(s
)
30 self
.assertEqual(t
.children
[0].children
[0].type, syms
.print_stmt
)
31 self
.assertEqual(t
.children
[1].children
[0].type, syms
.print_stmt
)
34 class GrammarTest(support
.TestCase
):
35 def validate(self
, code
):
36 support
.parse_string(code
)
38 def invalid_syntax(self
, code
):
44 raise AssertionError("Syntax shouldn't have been valid")
47 class TestRaiseChanges(GrammarTest
):
48 def test_2x_style_1(self
):
49 self
.validate("raise")
51 def test_2x_style_2(self
):
52 self
.validate("raise E, V")
54 def test_2x_style_3(self
):
55 self
.validate("raise E, V, T")
57 def test_2x_style_invalid_1(self
):
58 self
.invalid_syntax("raise E, V, T, Z")
60 def test_3x_style(self
):
61 self
.validate("raise E1 from E2")
63 def test_3x_style_invalid_1(self
):
64 self
.invalid_syntax("raise E, V from E1")
66 def test_3x_style_invalid_2(self
):
67 self
.invalid_syntax("raise E from E1, E2")
69 def test_3x_style_invalid_3(self
):
70 self
.invalid_syntax("raise from E1, E2")
72 def test_3x_style_invalid_4(self
):
73 self
.invalid_syntax("raise E from")
76 # Adapated from Python 3's Lib/test/test_grammar.py:GrammarTests.testFuncdef
77 class TestFunctionAnnotations(GrammarTest
):
79 self
.validate("""def f(x) -> list: pass""")
82 self
.validate("""def f(x:int): pass""")
85 self
.validate("""def f(*x:str): pass""")
88 self
.validate("""def f(**x:float): pass""")
91 self
.validate("""def f(x, y:1+2): pass""")
94 self
.validate("""def f(a, (b:1, c:2, d)): pass""")
97 self
.validate("""def f(a, (b:1, c:2, d), e:3=4, f=5, *g:6): pass""")
100 s
= """def f(a, (b:1, c:2, d), e:3=4, f=5,
101 *g:6, h:7, i=8, j:9=10, **k:11) -> 12: pass"""
105 class TestExcept(GrammarTest
):
123 # Adapted from Python 3's Lib/test/test_grammar.py:GrammarTests.testAtoms
124 class TestSetLiteral(GrammarTest
):
126 self
.validate("""x = {'one'}""")
129 self
.validate("""x = {'one', 1,}""")
132 self
.validate("""x = {'one', 'two', 'three'}""")
135 self
.validate("""x = {2, 3, 4,}""")
138 class TestNumericLiterals(GrammarTest
):
139 def test_new_octal_notation(self
):
140 self
.validate("""0o7777777777777""")
141 self
.invalid_syntax("""0o7324528887""")
143 def test_new_binary_notation(self
):
144 self
.validate("""0b101010""")
145 self
.invalid_syntax("""0b0101021""")
148 class TestClassDef(GrammarTest
):
149 def test_new_syntax(self
):
150 self
.validate("class B(t=7): pass")
151 self
.validate("class B(t, *args): pass")
152 self
.validate("class B(t, **kwargs): pass")
153 self
.validate("class B(t, *args, **kwargs): pass")
154 self
.validate("class B(t, y=9, *args, **kwargs): pass")
157 class TestParserIdempotency(support
.TestCase
):
159 """A cut-down version of pytree_idempotency.py."""
161 def test_all_project_files(self
):
162 if sys
.platform
.startswith("win"):
163 # XXX something with newlines goes wrong on Windows.
165 for filepath
in support
.all_project_files():
166 with
open(filepath
, "rb") as fp
:
167 encoding
= tokenize
.detect_encoding(fp
.readline
)[0]
168 self
.assertTrue(encoding
is not None,
169 "can't detect encoding for %s" % filepath
)
170 with
open(filepath
, "r") as fp
:
172 source
= source
.decode(encoding
)
173 tree
= driver
.parse_string(source
)
175 if diff(filepath
, new
, encoding
):
176 self
.fail("Idempotency failed: %s" % filepath
)
178 def test_extended_unpacking(self
):
179 driver
.parse_string("a, *b, c = x\n")
180 driver
.parse_string("[*a, b] = x\n")
181 driver
.parse_string("(z, *y, w) = m\n")
182 driver
.parse_string("for *z, m in d: pass\n")
184 class TestLiterals(GrammarTest
):
186 def validate(self
, s
):
187 driver
.parse_string(support
.dedent(s
) + "\n\n")
189 def test_multiline_bytes_literals(self
):
191 md5test(b"\xaa" * 80,
192 (b"Test Using Larger Than Block-Size Key "
193 b"and Larger Than One Block-Size Data"),
194 "6f630fad67cda0ee1fb1f562db3aa53e")
198 def test_multiline_bytes_tripquote_literals(self
):
201 <?xml version="1.0" encoding="UTF-8"?>
202 <!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN">
207 def test_multiline_str_literals(self
):
210 ("Test Using Larger Than Block-Size Key "
211 "and Larger Than One Block-Size Data"),
212 "6f630fad67cda0ee1fb1f562db3aa53e")
217 def diff(fn
, result
, encoding
):
220 f
.write(result
.encode(encoding
))
224 fn
= fn
.replace('"', '\\"')
225 return os
.system('diff -u "%s" @' % fn
)