6 # First, we test that we can generate trees from valid source fragments,
7 # and that these valid trees are indeed allowed by the tree-loading side
8 # of the parser module.
11 class RoundtripLegalSyntaxTestCase(unittest
.TestCase
):
12 def roundtrip(self
, f
, s
):
16 st2
= parser
.sequence2st(t
)
17 except parser
.ParserError
:
18 self
.fail("could not roundtrip %r" % s
)
20 self
.assertEquals(t
, st2
.totuple(),
21 "could not re-generate syntax tree")
23 def check_expr(self
, s
):
24 self
.roundtrip(parser
.expr
, s
)
26 def check_suite(self
, s
):
27 self
.roundtrip(parser
.suite
, s
)
29 def test_yield_statement(self
):
30 self
.check_suite("from __future__ import generators\n"
32 self
.check_suite("from __future__ import generators\n"
33 "def f(): return; yield 1")
34 self
.check_suite("from __future__ import generators\n"
35 "def f(): yield 1; return")
36 self
.check_suite("from __future__ import generators\n"
38 " for x in range(30):\n"
41 def test_expressions(self
):
42 self
.check_expr("foo(1)")
43 self
.check_expr("[1, 2, 3]")
44 self
.check_expr("[x**3 for x in range(20)]")
45 self
.check_expr("[x**3 for x in range(20) if x % 3]")
46 self
.check_expr("foo(*args)")
47 self
.check_expr("foo(*args, **kw)")
48 self
.check_expr("foo(**kw)")
49 self
.check_expr("foo(key=value)")
50 self
.check_expr("foo(key=value, *args)")
51 self
.check_expr("foo(key=value, *args, **kw)")
52 self
.check_expr("foo(key=value, **kw)")
53 self
.check_expr("foo(a, b, c, *args)")
54 self
.check_expr("foo(a, b, c, *args, **kw)")
55 self
.check_expr("foo(a, b, c, **kw)")
56 self
.check_expr("foo + bar")
57 self
.check_expr("lambda: 0")
58 self
.check_expr("lambda x: 0")
59 self
.check_expr("lambda *y: 0")
60 self
.check_expr("lambda *y, **z: 0")
61 self
.check_expr("lambda **z: 0")
62 self
.check_expr("lambda x, y: 0")
63 self
.check_expr("lambda foo=bar: 0")
64 self
.check_expr("lambda foo=bar, spaz=nifty+spit: 0")
65 self
.check_expr("lambda foo=bar, **z: 0")
66 self
.check_expr("lambda foo=bar, blaz=blat+2, **z: 0")
67 self
.check_expr("lambda foo=bar, blaz=blat+2, *y, **z: 0")
68 self
.check_expr("lambda x, *y, **z: 0")
71 self
.check_suite("print")
72 self
.check_suite("print 1")
73 self
.check_suite("print 1,")
74 self
.check_suite("print >>fp")
75 self
.check_suite("print >>fp, 1")
76 self
.check_suite("print >>fp, 1,")
78 def test_simple_expression(self
):
82 def test_simple_assignments(self
):
83 self
.check_suite("a = b")
84 self
.check_suite("a = b = c = d = e")
86 def test_simple_augmented_assignments(self
):
87 self
.check_suite("a += b")
88 self
.check_suite("a -= b")
89 self
.check_suite("a *= b")
90 self
.check_suite("a /= b")
91 self
.check_suite("a %= b")
92 self
.check_suite("a &= b")
93 self
.check_suite("a |= b")
94 self
.check_suite("a ^= b")
95 self
.check_suite("a <<= b")
96 self
.check_suite("a >>= b")
97 self
.check_suite("a **= b")
99 def test_function_defs(self
):
100 self
.check_suite("def f(): pass")
101 self
.check_suite("def f(*args): pass")
102 self
.check_suite("def f(*args, **kw): pass")
103 self
.check_suite("def f(**kw): pass")
104 self
.check_suite("def f(foo=bar): pass")
105 self
.check_suite("def f(foo=bar, *args): pass")
106 self
.check_suite("def f(foo=bar, *args, **kw): pass")
107 self
.check_suite("def f(foo=bar, **kw): pass")
109 self
.check_suite("def f(a, b): pass")
110 self
.check_suite("def f(a, b, *args): pass")
111 self
.check_suite("def f(a, b, *args, **kw): pass")
112 self
.check_suite("def f(a, b, **kw): pass")
113 self
.check_suite("def f(a, b, foo=bar): pass")
114 self
.check_suite("def f(a, b, foo=bar, *args): pass")
115 self
.check_suite("def f(a, b, foo=bar, *args, **kw): pass")
116 self
.check_suite("def f(a, b, foo=bar, **kw): pass")
118 def test_import_from_statement(self
):
119 self
.check_suite("from sys.path import *")
120 self
.check_suite("from sys.path import dirname")
121 self
.check_suite("from sys.path import dirname as my_dirname")
122 self
.check_suite("from sys.path import dirname, basename")
124 "from sys.path import dirname as my_dirname, basename")
126 "from sys.path import dirname, basename as my_basename")
128 def test_basic_import_statement(self
):
129 self
.check_suite("import sys")
130 self
.check_suite("import sys as system")
131 self
.check_suite("import sys, math")
132 self
.check_suite("import sys as system, math")
133 self
.check_suite("import sys, math as my_math")
136 # Second, we take *invalid* trees and make sure we get ParserError
137 # rejections for them.
140 class IllegalSyntaxTestCase(unittest
.TestCase
):
141 def check_bad_tree(self
, tree
, label
):
143 parser
.sequence2st(tree
)
144 except parser
.ParserError
:
147 self
.fail("did not detect invalid tree for %r" % label
)
150 # not even remotely valid:
151 self
.check_bad_tree((1, 2, 3), "<junk>")
153 def test_illegal_yield_1(self
):
154 """Illegal yield statement: def f(): return 1; yield 1"""
162 (260, (7, '('), (8, ')')),
183 (302, (303, (304, (305, (2, '1')))))))))))))))))),
201 (303, (304, (305, (2, '1')))))))))))))))))),
206 self
.check_bad_tree(tree
, "def f():\n return 1\n yield 1")
208 def test_illegal_yield_2(self
):
209 """Illegal return in generator: def f(): return 1; yield 1"""
217 (281, (1, '__future__')),
219 (279, (1, 'generators')))),
226 (260, (7, '('), (8, ')')),
247 (302, (303, (304, (305, (2, '1')))))))))))))))))),
265 (303, (304, (305, (2, '1')))))))))))))))))),
270 self
.check_bad_tree(tree
, "def f():\n return 1\n yield 1")
272 def test_print_chevron_comma(self
):
273 """Illegal input: print >>fp,"""
289 (298, (299, (300, (301, (302, (303, (1, 'fp')))))))))))))),
293 self
.check_bad_tree(tree
, "print >>fp,")
295 def test_a_comma_comma_c(self
):
296 """Illegal input: a,,c"""
307 (298, (299, (300, (301, (302, (303, (1, 'a')))))))))))))),
317 (298, (299, (300, (301, (302, (303, (1, 'c'))))))))))))))),
320 self
.check_bad_tree(tree
, "a,,c")
322 def test_illegal_operator(self
):
323 """Illegal input: a $= b"""
339 (300, (301, (302, (303, (304, (1, 'a'))))))))))))))),
350 (300, (301, (302, (303, (304, (1, 'b'))))))))))))))))),
353 self
.check_bad_tree(tree
, "a $= b")
357 loader
= unittest
.TestLoader()
358 suite
= unittest
.TestSuite()
359 suite
.addTest(loader
.loadTestsFromTestCase(RoundtripLegalSyntaxTestCase
))
360 suite
.addTest(loader
.loadTestsFromTestCase(IllegalSyntaxTestCase
))
361 test_support
.run_suite(suite
)
364 if __name__
== "__main__":