3 from test
import test_support
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
):
13 def roundtrip(self
, f
, s
):
17 st2
= parser
.sequence2st(t
)
18 except parser
.ParserError
:
19 self
.fail("could not roundtrip %r" % s
)
21 self
.assertEquals(t
, st2
.totuple(),
22 "could not re-generate syntax tree")
24 def check_expr(self
, s
):
25 self
.roundtrip(parser
.expr
, s
)
27 def check_suite(self
, s
):
28 self
.roundtrip(parser
.suite
, s
)
30 def test_yield_statement(self
):
31 self
.check_suite("def f(): yield 1")
32 self
.check_suite("def f(): return; yield 1")
33 self
.check_suite("def f(): yield 1; return")
34 self
.check_suite("def f():\n"
35 " for x in range(30):\n"
38 def test_expressions(self
):
39 self
.check_expr("foo(1)")
40 self
.check_expr("[1, 2, 3]")
41 self
.check_expr("[x**3 for x in range(20)]")
42 self
.check_expr("[x**3 for x in range(20) if x % 3]")
43 self
.check_expr("foo(*args)")
44 self
.check_expr("foo(*args, **kw)")
45 self
.check_expr("foo(**kw)")
46 self
.check_expr("foo(key=value)")
47 self
.check_expr("foo(key=value, *args)")
48 self
.check_expr("foo(key=value, *args, **kw)")
49 self
.check_expr("foo(key=value, **kw)")
50 self
.check_expr("foo(a, b, c, *args)")
51 self
.check_expr("foo(a, b, c, *args, **kw)")
52 self
.check_expr("foo(a, b, c, **kw)")
53 self
.check_expr("foo + bar")
54 self
.check_expr("foo - bar")
55 self
.check_expr("foo * bar")
56 self
.check_expr("foo / bar")
57 self
.check_expr("foo // bar")
58 self
.check_expr("lambda: 0")
59 self
.check_expr("lambda x: 0")
60 self
.check_expr("lambda *y: 0")
61 self
.check_expr("lambda *y, **z: 0")
62 self
.check_expr("lambda **z: 0")
63 self
.check_expr("lambda x, y: 0")
64 self
.check_expr("lambda foo=bar: 0")
65 self
.check_expr("lambda foo=bar, spaz=nifty+spit: 0")
66 self
.check_expr("lambda foo=bar, **z: 0")
67 self
.check_expr("lambda foo=bar, blaz=blat+2, **z: 0")
68 self
.check_expr("lambda foo=bar, blaz=blat+2, *y, **z: 0")
69 self
.check_expr("lambda x, *y, **z: 0")
72 self
.check_suite("print")
73 self
.check_suite("print 1")
74 self
.check_suite("print 1,")
75 self
.check_suite("print >>fp")
76 self
.check_suite("print >>fp, 1")
77 self
.check_suite("print >>fp, 1,")
79 def test_simple_expression(self
):
83 def test_simple_assignments(self
):
84 self
.check_suite("a = b")
85 self
.check_suite("a = b = c = d = e")
87 def test_simple_augmented_assignments(self
):
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")
98 self
.check_suite("a >>= b")
99 self
.check_suite("a **= b")
101 def test_function_defs(self
):
102 self
.check_suite("def f(): pass")
103 self
.check_suite("def f(*args): pass")
104 self
.check_suite("def f(*args, **kw): pass")
105 self
.check_suite("def f(**kw): pass")
106 self
.check_suite("def f(foo=bar): pass")
107 self
.check_suite("def f(foo=bar, *args): pass")
108 self
.check_suite("def f(foo=bar, *args, **kw): pass")
109 self
.check_suite("def f(foo=bar, **kw): pass")
111 self
.check_suite("def f(a, b): pass")
112 self
.check_suite("def f(a, b, *args): pass")
113 self
.check_suite("def f(a, b, *args, **kw): pass")
114 self
.check_suite("def f(a, b, **kw): pass")
115 self
.check_suite("def f(a, b, foo=bar): pass")
116 self
.check_suite("def f(a, b, foo=bar, *args): pass")
117 self
.check_suite("def f(a, b, foo=bar, *args, **kw): pass")
118 self
.check_suite("def f(a, b, foo=bar, **kw): pass")
120 def test_import_from_statement(self
):
121 self
.check_suite("from sys.path import *")
122 self
.check_suite("from sys.path import dirname")
123 self
.check_suite("from sys.path import dirname as my_dirname")
124 self
.check_suite("from sys.path import dirname, basename")
126 "from sys.path import dirname as my_dirname, basename")
128 "from sys.path import dirname, basename as my_basename")
130 def test_basic_import_statement(self
):
131 self
.check_suite("import sys")
132 self
.check_suite("import sys as system")
133 self
.check_suite("import sys, math")
134 self
.check_suite("import sys as system, math")
135 self
.check_suite("import sys, math as my_math")
137 def test_pep263(self
):
138 self
.check_suite("# -*- coding: iso-8859-1 -*-\n"
141 def test_assert(self
):
142 self
.check_suite("assert alo < ahi and blo < bhi\n")
145 # Second, we take *invalid* trees and make sure we get ParserError
146 # rejections for them.
149 class IllegalSyntaxTestCase(unittest
.TestCase
):
151 def check_bad_tree(self
, tree
, label
):
153 parser
.sequence2st(tree
)
154 except parser
.ParserError
:
157 self
.fail("did not detect invalid tree for %r" % label
)
160 # not even remotely valid:
161 self
.check_bad_tree((1, 2, 3), "<junk>")
163 def test_illegal_yield_1(self
):
164 # Illegal yield statement: def f(): return 1; yield 1
172 (260, (7, '('), (8, ')')),
193 (302, (303, (304, (305, (2, '1')))))))))))))))))),
211 (303, (304, (305, (2, '1')))))))))))))))))),
216 self
.check_bad_tree(tree
, "def f():\n return 1\n yield 1")
218 def test_illegal_yield_2(self
):
219 # Illegal return in generator: def f(): return 1; yield 1
227 (281, (1, '__future__')),
229 (279, (1, 'generators')))),
236 (260, (7, '('), (8, ')')),
257 (302, (303, (304, (305, (2, '1')))))))))))))))))),
275 (303, (304, (305, (2, '1')))))))))))))))))),
280 self
.check_bad_tree(tree
, "def f():\n return 1\n yield 1")
282 def test_print_chevron_comma(self
):
283 # Illegal input: print >>fp,
299 (298, (299, (300, (301, (302, (303, (1, 'fp')))))))))))))),
303 self
.check_bad_tree(tree
, "print >>fp,")
305 def test_a_comma_comma_c(self
):
306 # Illegal input: a,,c
317 (298, (299, (300, (301, (302, (303, (1, 'a')))))))))))))),
327 (298, (299, (300, (301, (302, (303, (1, 'c'))))))))))))))),
330 self
.check_bad_tree(tree
, "a,,c")
332 def test_illegal_operator(self
):
333 # Illegal input: a $= b
349 (300, (301, (302, (303, (304, (1, 'a'))))))))))))))),
360 (300, (301, (302, (303, (304, (1, 'b'))))))))))))))))),
363 self
.check_bad_tree(tree
, "a $= b")
365 def test_malformed_global(self
):
366 #doesn't have global keyword in ast
371 (282, (1, 'foo'))), (4, ''))),
374 self
.check_bad_tree(tree
, "malformed global ast")
377 loader
= unittest
.TestLoader()
378 suite
= unittest
.TestSuite()
379 suite
.addTest(loader
.loadTestsFromTestCase(RoundtripLegalSyntaxTestCase
))
380 suite
.addTest(loader
.loadTestsFromTestCase(IllegalSyntaxTestCase
))
381 test_support
.run_suite(suite
)
384 if __name__
== "__main__":