blob: 5df1e4613140122a8ed7446d215ff30b7a56ea5d [file] [log] [blame]
Fred Drake79ca79d2000-08-21 22:30:53 +00001import parser
Fred Drake58422e52001-06-04 03:56:24 +00002import test_support
3import unittest
Fred Drake79ca79d2000-08-21 22:30:53 +00004
5#
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.
9#
10
Fred Drake58422e52001-06-04 03:56:24 +000011class RoundtripLegalSyntaxTestCase(unittest.TestCase):
12 def roundtrip(self, f, s):
13 st1 = f(s)
14 t = st1.totuple()
15 try:
Fred Drake6e4f2c02001-07-17 19:33:25 +000016 st2 = parser.sequence2st(t)
Fred Drake58422e52001-06-04 03:56:24 +000017 except parser.ParserError:
18 self.fail("could not roundtrip %r" % s)
Fred Drake79ca79d2000-08-21 22:30:53 +000019
Fred Drake58422e52001-06-04 03:56:24 +000020 self.assertEquals(t, st2.totuple(),
21 "could not re-generate syntax tree")
Fred Drake28f739a2000-08-25 22:42:40 +000022
Fred Drake58422e52001-06-04 03:56:24 +000023 def check_expr(self, s):
24 self.roundtrip(parser.expr, s)
Fred Drake28f739a2000-08-25 22:42:40 +000025
Fred Drake58422e52001-06-04 03:56:24 +000026 def check_suite(self, s):
27 self.roundtrip(parser.suite, s)
Fred Drake28f739a2000-08-25 22:42:40 +000028
Fred Drakecf580c72001-07-17 03:01:29 +000029 def test_yield_statement(self):
Tim Peters496563a2002-04-01 00:28:59 +000030 self.check_suite("def f(): yield 1")
31 self.check_suite("def f(): return; yield 1")
32 self.check_suite("def f(): yield 1; return")
33 self.check_suite("def f():\n"
Fred Drakecf580c72001-07-17 03:01:29 +000034 " for x in range(30):\n"
35 " yield x\n")
36
Fred Drake58422e52001-06-04 03:56:24 +000037 def test_expressions(self):
38 self.check_expr("foo(1)")
39 self.check_expr("[1, 2, 3]")
40 self.check_expr("[x**3 for x in range(20)]")
41 self.check_expr("[x**3 for x in range(20) if x % 3]")
42 self.check_expr("foo(*args)")
43 self.check_expr("foo(*args, **kw)")
44 self.check_expr("foo(**kw)")
45 self.check_expr("foo(key=value)")
46 self.check_expr("foo(key=value, *args)")
47 self.check_expr("foo(key=value, *args, **kw)")
48 self.check_expr("foo(key=value, **kw)")
49 self.check_expr("foo(a, b, c, *args)")
50 self.check_expr("foo(a, b, c, *args, **kw)")
51 self.check_expr("foo(a, b, c, **kw)")
52 self.check_expr("foo + bar")
53 self.check_expr("lambda: 0")
54 self.check_expr("lambda x: 0")
55 self.check_expr("lambda *y: 0")
56 self.check_expr("lambda *y, **z: 0")
57 self.check_expr("lambda **z: 0")
58 self.check_expr("lambda x, y: 0")
59 self.check_expr("lambda foo=bar: 0")
60 self.check_expr("lambda foo=bar, spaz=nifty+spit: 0")
61 self.check_expr("lambda foo=bar, **z: 0")
62 self.check_expr("lambda foo=bar, blaz=blat+2, **z: 0")
63 self.check_expr("lambda foo=bar, blaz=blat+2, *y, **z: 0")
64 self.check_expr("lambda x, *y, **z: 0")
Fred Drake79ca79d2000-08-21 22:30:53 +000065
Fred Drake58422e52001-06-04 03:56:24 +000066 def test_print(self):
67 self.check_suite("print")
68 self.check_suite("print 1")
69 self.check_suite("print 1,")
70 self.check_suite("print >>fp")
71 self.check_suite("print >>fp, 1")
72 self.check_suite("print >>fp, 1,")
Fred Drake79ca79d2000-08-21 22:30:53 +000073
Fred Drake58422e52001-06-04 03:56:24 +000074 def test_simple_expression(self):
75 # expr_stmt
76 self.check_suite("a")
Fred Drake79ca79d2000-08-21 22:30:53 +000077
Fred Drake58422e52001-06-04 03:56:24 +000078 def test_simple_assignments(self):
79 self.check_suite("a = b")
80 self.check_suite("a = b = c = d = e")
Fred Drake28f739a2000-08-25 22:42:40 +000081
Fred Drake58422e52001-06-04 03:56:24 +000082 def test_simple_augmented_assignments(self):
83 self.check_suite("a += b")
84 self.check_suite("a -= b")
85 self.check_suite("a *= b")
86 self.check_suite("a /= b")
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")
Fred Drakee3fb18c2001-01-07 06:02:19 +000094
Fred Drake58422e52001-06-04 03:56:24 +000095 def test_function_defs(self):
96 self.check_suite("def f(): pass")
97 self.check_suite("def f(*args): pass")
98 self.check_suite("def f(*args, **kw): pass")
99 self.check_suite("def f(**kw): pass")
100 self.check_suite("def f(foo=bar): pass")
101 self.check_suite("def f(foo=bar, *args): pass")
102 self.check_suite("def f(foo=bar, *args, **kw): pass")
103 self.check_suite("def f(foo=bar, **kw): pass")
Fred Drakee3fb18c2001-01-07 06:02:19 +0000104
Fred Drake58422e52001-06-04 03:56:24 +0000105 self.check_suite("def f(a, b): pass")
106 self.check_suite("def f(a, b, *args): pass")
107 self.check_suite("def f(a, b, *args, **kw): pass")
108 self.check_suite("def f(a, b, **kw): pass")
109 self.check_suite("def f(a, b, foo=bar): pass")
110 self.check_suite("def f(a, b, foo=bar, *args): pass")
111 self.check_suite("def f(a, b, foo=bar, *args, **kw): pass")
112 self.check_suite("def f(a, b, foo=bar, **kw): pass")
Fred Drakee3fb18c2001-01-07 06:02:19 +0000113
Fred Drake58422e52001-06-04 03:56:24 +0000114 def test_import_from_statement(self):
115 self.check_suite("from sys.path import *")
116 self.check_suite("from sys.path import dirname")
117 self.check_suite("from sys.path import dirname as my_dirname")
118 self.check_suite("from sys.path import dirname, basename")
119 self.check_suite(
120 "from sys.path import dirname as my_dirname, basename")
121 self.check_suite(
122 "from sys.path import dirname, basename as my_basename")
Fred Drakee3fb18c2001-01-07 06:02:19 +0000123
Fred Drake58422e52001-06-04 03:56:24 +0000124 def test_basic_import_statement(self):
125 self.check_suite("import sys")
126 self.check_suite("import sys as system")
127 self.check_suite("import sys, math")
128 self.check_suite("import sys as system, math")
129 self.check_suite("import sys, math as my_math")
Fred Drake79ca79d2000-08-21 22:30:53 +0000130
131#
132# Second, we take *invalid* trees and make sure we get ParserError
133# rejections for them.
134#
135
Fred Drake58422e52001-06-04 03:56:24 +0000136class IllegalSyntaxTestCase(unittest.TestCase):
137 def check_bad_tree(self, tree, label):
138 try:
Fred Drake6e4f2c02001-07-17 19:33:25 +0000139 parser.sequence2st(tree)
Fred Drake58422e52001-06-04 03:56:24 +0000140 except parser.ParserError:
141 pass
142 else:
143 self.fail("did not detect invalid tree for %r" % label)
Fred Drake79ca79d2000-08-21 22:30:53 +0000144
Fred Drake58422e52001-06-04 03:56:24 +0000145 def test_junk(self):
146 # not even remotely valid:
147 self.check_bad_tree((1, 2, 3), "<junk>")
148
Fred Drakecf580c72001-07-17 03:01:29 +0000149 def test_illegal_yield_1(self):
150 """Illegal yield statement: def f(): return 1; yield 1"""
151 tree = \
152 (257,
153 (264,
154 (285,
155 (259,
156 (1, 'def'),
157 (1, 'f'),
158 (260, (7, '('), (8, ')')),
159 (11, ':'),
160 (291,
161 (4, ''),
162 (5, ''),
163 (264,
164 (265,
165 (266,
166 (272,
167 (275,
168 (1, 'return'),
169 (313,
170 (292,
171 (293,
172 (294,
173 (295,
174 (297,
175 (298,
176 (299,
177 (300,
178 (301,
179 (302, (303, (304, (305, (2, '1')))))))))))))))))),
180 (264,
181 (265,
182 (266,
183 (272,
184 (276,
185 (1, 'yield'),
186 (313,
187 (292,
188 (293,
189 (294,
190 (295,
191 (297,
192 (298,
193 (299,
194 (300,
195 (301,
196 (302,
197 (303, (304, (305, (2, '1')))))))))))))))))),
198 (4, ''))),
199 (6, ''))))),
200 (4, ''),
201 (0, ''))))
202 self.check_bad_tree(tree, "def f():\n return 1\n yield 1")
203
204 def test_illegal_yield_2(self):
205 """Illegal return in generator: def f(): return 1; yield 1"""
206 tree = \
207 (257,
208 (264,
209 (265,
210 (266,
211 (278,
212 (1, 'from'),
213 (281, (1, '__future__')),
214 (1, 'import'),
215 (279, (1, 'generators')))),
216 (4, ''))),
217 (264,
218 (285,
219 (259,
220 (1, 'def'),
221 (1, 'f'),
222 (260, (7, '('), (8, ')')),
223 (11, ':'),
224 (291,
225 (4, ''),
226 (5, ''),
227 (264,
228 (265,
229 (266,
230 (272,
231 (275,
232 (1, 'return'),
233 (313,
234 (292,
235 (293,
236 (294,
237 (295,
238 (297,
239 (298,
240 (299,
241 (300,
242 (301,
243 (302, (303, (304, (305, (2, '1')))))))))))))))))),
244 (264,
245 (265,
246 (266,
247 (272,
248 (276,
249 (1, 'yield'),
250 (313,
251 (292,
252 (293,
253 (294,
254 (295,
255 (297,
256 (298,
257 (299,
258 (300,
259 (301,
260 (302,
261 (303, (304, (305, (2, '1')))))))))))))))))),
262 (4, ''))),
263 (6, ''))))),
264 (4, ''),
265 (0, ''))))
266 self.check_bad_tree(tree, "def f():\n return 1\n yield 1")
267
Fred Drake58422e52001-06-04 03:56:24 +0000268 def test_print_chevron_comma(self):
Fred Drakecf580c72001-07-17 03:01:29 +0000269 """Illegal input: print >>fp,"""
Fred Drake58422e52001-06-04 03:56:24 +0000270 tree = \
271 (257,
272 (264,
273 (265,
274 (266,
275 (268,
276 (1, 'print'),
277 (35, '>>'),
278 (290,
279 (291,
280 (292,
281 (293,
282 (295,
283 (296,
284 (297,
285 (298, (299, (300, (301, (302, (303, (1, 'fp')))))))))))))),
286 (12, ','))),
287 (4, ''))),
288 (0, ''))
289 self.check_bad_tree(tree, "print >>fp,")
290
291 def test_a_comma_comma_c(self):
292 """Illegal input: a,,c"""
293 tree = \
294 (258,
295 (311,
296 (290,
297 (291,
298 (292,
299 (293,
300 (295,
301 (296,
302 (297,
303 (298, (299, (300, (301, (302, (303, (1, 'a')))))))))))))),
304 (12, ','),
305 (12, ','),
306 (290,
307 (291,
308 (292,
309 (293,
310 (295,
311 (296,
312 (297,
313 (298, (299, (300, (301, (302, (303, (1, 'c'))))))))))))))),
314 (4, ''),
315 (0, ''))
316 self.check_bad_tree(tree, "a,,c")
317
318 def test_illegal_operator(self):
319 """Illegal input: a $= b"""
320 tree = \
321 (257,
322 (264,
323 (265,
324 (266,
325 (267,
326 (312,
327 (291,
328 (292,
329 (293,
330 (294,
331 (296,
332 (297,
333 (298,
334 (299,
335 (300, (301, (302, (303, (304, (1, 'a'))))))))))))))),
336 (268, (37, '$=')),
337 (312,
338 (291,
339 (292,
340 (293,
341 (294,
342 (296,
343 (297,
344 (298,
345 (299,
346 (300, (301, (302, (303, (304, (1, 'b'))))))))))))))))),
347 (4, ''))),
348 (0, ''))
349 self.check_bad_tree(tree, "a $= b")
Fred Drake79ca79d2000-08-21 22:30:53 +0000350
351
Fred Drake2e2be372001-09-20 21:33:42 +0000352def test_main():
353 loader = unittest.TestLoader()
354 suite = unittest.TestSuite()
355 suite.addTest(loader.loadTestsFromTestCase(RoundtripLegalSyntaxTestCase))
356 suite.addTest(loader.loadTestsFromTestCase(IllegalSyntaxTestCase))
357 test_support.run_suite(suite)
358
359
360if __name__ == "__main__":
361 test_main()