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, why:
19
self.fail("could not roundtrip %r: %s" % (s, why))
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")
70
self.check_expr("(x for x in range(10))")
71
self.check_expr("foo(x for x in range(10))")
74
self.check_suite("print")
75
self.check_suite("print 1")
76
self.check_suite("print 1,")
77
self.check_suite("print >>fp")
78
self.check_suite("print >>fp, 1")
79
self.check_suite("print >>fp, 1,")
81
def test_simple_expression(self):
85
def test_simple_assignments(self):
86
self.check_suite("a = b")
87
self.check_suite("a = b = c = d = e")
89
def test_simple_augmented_assignments(self):
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")
100
self.check_suite("a >>= b")
101
self.check_suite("a **= b")
103
def test_function_defs(self):
104
self.check_suite("def f(): pass")
105
self.check_suite("def f(*args): pass")
106
self.check_suite("def f(*args, **kw): pass")
107
self.check_suite("def f(**kw): pass")
108
self.check_suite("def f(foo=bar): pass")
109
self.check_suite("def f(foo=bar, *args): pass")
110
self.check_suite("def f(foo=bar, *args, **kw): pass")
111
self.check_suite("def f(foo=bar, **kw): pass")
113
self.check_suite("def f(a, b): pass")
114
self.check_suite("def f(a, b, *args): pass")
115
self.check_suite("def f(a, b, *args, **kw): pass")
116
self.check_suite("def f(a, b, **kw): pass")
117
self.check_suite("def f(a, b, foo=bar): pass")
118
self.check_suite("def f(a, b, foo=bar, *args): pass")
119
self.check_suite("def f(a, b, foo=bar, *args, **kw): pass")
120
self.check_suite("def f(a, b, foo=bar, **kw): pass")
122
self.check_suite("@staticmethod\n"
124
self.check_suite("@staticmethod\n"
127
self.check_suite("@funcattrs()\n"
130
def test_import_from_statement(self):
131
self.check_suite("from sys.path import *")
132
self.check_suite("from sys.path import dirname")
133
self.check_suite("from sys.path import (dirname)")
134
self.check_suite("from sys.path import (dirname,)")
135
self.check_suite("from sys.path import dirname as my_dirname")
136
self.check_suite("from sys.path import (dirname as my_dirname)")
137
self.check_suite("from sys.path import (dirname as my_dirname,)")
138
self.check_suite("from sys.path import dirname, basename")
139
self.check_suite("from sys.path import (dirname, basename)")
140
self.check_suite("from sys.path import (dirname, basename,)")
142
"from sys.path import dirname as my_dirname, basename")
144
"from sys.path import (dirname as my_dirname, basename)")
146
"from sys.path import (dirname as my_dirname, basename,)")
148
"from sys.path import dirname, basename as my_basename")
150
"from sys.path import (dirname, basename as my_basename)")
152
"from sys.path import (dirname, basename as my_basename,)")
154
def test_basic_import_statement(self):
155
self.check_suite("import sys")
156
self.check_suite("import sys as system")
157
self.check_suite("import sys, math")
158
self.check_suite("import sys as system, math")
159
self.check_suite("import sys, math as my_math")
161
def test_pep263(self):
162
self.check_suite("# -*- coding: iso-8859-1 -*-\n"
165
def test_assert(self):
166
self.check_suite("assert alo < ahi and blo < bhi\n")
169
# Second, we take *invalid* trees and make sure we get ParserError
170
# rejections for them.
173
class IllegalSyntaxTestCase(unittest.TestCase):
175
def check_bad_tree(self, tree, label):
177
parser.sequence2st(tree)
178
except parser.ParserError:
181
self.fail("did not detect invalid tree for %r" % label)
184
# not even remotely valid:
185
self.check_bad_tree((1, 2, 3), "<junk>")
187
def test_illegal_yield_1(self):
188
# Illegal yield statement: def f(): return 1; yield 1
196
(260, (7, '('), (8, ')')),
217
(302, (303, (304, (305, (2, '1')))))))))))))))))),
235
(303, (304, (305, (2, '1')))))))))))))))))),
240
self.check_bad_tree(tree, "def f():\n return 1\n yield 1")
242
def test_illegal_yield_2(self):
243
# Illegal return in generator: def f(): return 1; yield 1
251
(281, (1, '__future__')),
253
(279, (1, 'generators')))),
260
(260, (7, '('), (8, ')')),
281
(302, (303, (304, (305, (2, '1')))))))))))))))))),
299
(303, (304, (305, (2, '1')))))))))))))))))),
304
self.check_bad_tree(tree, "def f():\n return 1\n yield 1")
306
def test_print_chevron_comma(self):
307
# Illegal input: print >>fp,
323
(298, (299, (300, (301, (302, (303, (1, 'fp')))))))))))))),
327
self.check_bad_tree(tree, "print >>fp,")
329
def test_a_comma_comma_c(self):
330
# Illegal input: a,,c
341
(298, (299, (300, (301, (302, (303, (1, 'a')))))))))))))),
351
(298, (299, (300, (301, (302, (303, (1, 'c'))))))))))))))),
354
self.check_bad_tree(tree, "a,,c")
356
def test_illegal_operator(self):
357
# Illegal input: a $= b
373
(300, (301, (302, (303, (304, (1, 'a'))))))))))))))),
384
(300, (301, (302, (303, (304, (1, 'b'))))))))))))))))),
387
self.check_bad_tree(tree, "a $= b")
389
def test_malformed_global(self):
390
#doesn't have global keyword in ast
395
(282, (1, 'foo'))), (4, ''))),
398
self.check_bad_tree(tree, "malformed global ast")
401
test_support.run_unittest(
402
RoundtripLegalSyntaxTestCase,
406
if __name__ == "__main__":