blob: 8aa1657e621a67d756a58f8b7f66d31bf074811e [file] [log] [blame]
Fred Drake79ca79d2000-08-21 22:30:53 +00001import parser
Fred Drake58422e52001-06-04 03:56:24 +00002import unittest
Barry Warsaw04f357c2002-07-23 19:04:11 +00003from test import test_support
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):
Guido van Rossum32c2ae72002-08-22 19:45:32 +000012
Fred Drake58422e52001-06-04 03:56:24 +000013 def roundtrip(self, f, s):
14 st1 = f(s)
15 t = st1.totuple()
16 try:
Fred Drake6e4f2c02001-07-17 19:33:25 +000017 st2 = parser.sequence2st(t)
Anthony Baxterc2a5a632004-08-02 06:10:11 +000018 except parser.ParserError, why:
19 self.fail("could not roundtrip %r: %s" % (s, why))
Fred Drake79ca79d2000-08-21 22:30:53 +000020
Fred Drake58422e52001-06-04 03:56:24 +000021 self.assertEquals(t, st2.totuple(),
22 "could not re-generate syntax tree")
Fred Drake28f739a2000-08-25 22:42:40 +000023
Fred Drake58422e52001-06-04 03:56:24 +000024 def check_expr(self, s):
25 self.roundtrip(parser.expr, s)
Fred Drake28f739a2000-08-25 22:42:40 +000026
Fred Drake58422e52001-06-04 03:56:24 +000027 def check_suite(self, s):
28 self.roundtrip(parser.suite, s)
Fred Drake28f739a2000-08-25 22:42:40 +000029
Fred Drakecf580c72001-07-17 03:01:29 +000030 def test_yield_statement(self):
Tim Peters496563a2002-04-01 00:28:59 +000031 self.check_suite("def f(): yield 1")
Phillip J. Eby0d6615f2005-08-02 00:46:46 +000032 self.check_suite("def f(): yield")
33 self.check_suite("def f(): x += yield")
34 self.check_suite("def f(): x = yield 1")
35 self.check_suite("def f(): x = y = yield 1")
36 self.check_suite("def f(): x = yield")
37 self.check_suite("def f(): x = y = yield")
38 self.check_suite("def f(): 1 + (yield)*2")
39 self.check_suite("def f(): (yield 1)*2")
Tim Peters496563a2002-04-01 00:28:59 +000040 self.check_suite("def f(): return; yield 1")
41 self.check_suite("def f(): yield 1; return")
42 self.check_suite("def f():\n"
Fred Drakecf580c72001-07-17 03:01:29 +000043 " for x in range(30):\n"
44 " yield x\n")
Phillip J. Eby0d6615f2005-08-02 00:46:46 +000045 self.check_suite("def f():\n"
46 " if (yield):\n"
47 " yield x\n")
Fred Drakecf580c72001-07-17 03:01:29 +000048
Fred Drake58422e52001-06-04 03:56:24 +000049 def test_expressions(self):
50 self.check_expr("foo(1)")
51 self.check_expr("[1, 2, 3]")
52 self.check_expr("[x**3 for x in range(20)]")
53 self.check_expr("[x**3 for x in range(20) if x % 3]")
Neal Norwitzd3a91622006-04-12 05:27:46 +000054 self.check_expr("[x**3 for x in range(20) if x % 2 if x % 3]")
55 self.check_expr("list(x**3 for x in range(20))")
56 self.check_expr("list(x**3 for x in range(20) if x % 3)")
57 self.check_expr("list(x**3 for x in range(20) if x % 2 if x % 3)")
Fred Drake58422e52001-06-04 03:56:24 +000058 self.check_expr("foo(*args)")
59 self.check_expr("foo(*args, **kw)")
60 self.check_expr("foo(**kw)")
61 self.check_expr("foo(key=value)")
62 self.check_expr("foo(key=value, *args)")
63 self.check_expr("foo(key=value, *args, **kw)")
64 self.check_expr("foo(key=value, **kw)")
65 self.check_expr("foo(a, b, c, *args)")
66 self.check_expr("foo(a, b, c, *args, **kw)")
67 self.check_expr("foo(a, b, c, **kw)")
68 self.check_expr("foo + bar")
Michael W. Hudson5e83b7a2003-01-29 14:20:23 +000069 self.check_expr("foo - bar")
70 self.check_expr("foo * bar")
71 self.check_expr("foo / bar")
72 self.check_expr("foo // bar")
Fred Drake58422e52001-06-04 03:56:24 +000073 self.check_expr("lambda: 0")
74 self.check_expr("lambda x: 0")
75 self.check_expr("lambda *y: 0")
76 self.check_expr("lambda *y, **z: 0")
77 self.check_expr("lambda **z: 0")
78 self.check_expr("lambda x, y: 0")
79 self.check_expr("lambda foo=bar: 0")
80 self.check_expr("lambda foo=bar, spaz=nifty+spit: 0")
81 self.check_expr("lambda foo=bar, **z: 0")
82 self.check_expr("lambda foo=bar, blaz=blat+2, **z: 0")
83 self.check_expr("lambda foo=bar, blaz=blat+2, *y, **z: 0")
84 self.check_expr("lambda x, *y, **z: 0")
Raymond Hettinger354433a2004-05-19 08:20:33 +000085 self.check_expr("(x for x in range(10))")
86 self.check_expr("foo(x for x in range(10))")
Fred Drake79ca79d2000-08-21 22:30:53 +000087
Fred Drake58422e52001-06-04 03:56:24 +000088 def test_print(self):
89 self.check_suite("print")
90 self.check_suite("print 1")
91 self.check_suite("print 1,")
92 self.check_suite("print >>fp")
93 self.check_suite("print >>fp, 1")
94 self.check_suite("print >>fp, 1,")
Fred Drake79ca79d2000-08-21 22:30:53 +000095
Fred Drake58422e52001-06-04 03:56:24 +000096 def test_simple_expression(self):
97 # expr_stmt
98 self.check_suite("a")
Fred Drake79ca79d2000-08-21 22:30:53 +000099
Fred Drake58422e52001-06-04 03:56:24 +0000100 def test_simple_assignments(self):
101 self.check_suite("a = b")
102 self.check_suite("a = b = c = d = e")
Fred Drake28f739a2000-08-25 22:42:40 +0000103
Fred Drake58422e52001-06-04 03:56:24 +0000104 def test_simple_augmented_assignments(self):
105 self.check_suite("a += b")
106 self.check_suite("a -= b")
107 self.check_suite("a *= b")
108 self.check_suite("a /= b")
Michael W. Hudson5e83b7a2003-01-29 14:20:23 +0000109 self.check_suite("a //= b")
Fred Drake58422e52001-06-04 03:56:24 +0000110 self.check_suite("a %= b")
111 self.check_suite("a &= b")
112 self.check_suite("a |= b")
113 self.check_suite("a ^= b")
114 self.check_suite("a <<= b")
115 self.check_suite("a >>= b")
116 self.check_suite("a **= b")
Fred Drakee3fb18c2001-01-07 06:02:19 +0000117
Fred Drake58422e52001-06-04 03:56:24 +0000118 def test_function_defs(self):
119 self.check_suite("def f(): pass")
120 self.check_suite("def f(*args): pass")
121 self.check_suite("def f(*args, **kw): pass")
122 self.check_suite("def f(**kw): pass")
123 self.check_suite("def f(foo=bar): pass")
124 self.check_suite("def f(foo=bar, *args): pass")
125 self.check_suite("def f(foo=bar, *args, **kw): pass")
126 self.check_suite("def f(foo=bar, **kw): pass")
Fred Drakee3fb18c2001-01-07 06:02:19 +0000127
Fred Drake58422e52001-06-04 03:56:24 +0000128 self.check_suite("def f(a, b): pass")
129 self.check_suite("def f(a, b, *args): pass")
130 self.check_suite("def f(a, b, *args, **kw): pass")
131 self.check_suite("def f(a, b, **kw): pass")
132 self.check_suite("def f(a, b, foo=bar): pass")
133 self.check_suite("def f(a, b, foo=bar, *args): pass")
134 self.check_suite("def f(a, b, foo=bar, *args, **kw): pass")
135 self.check_suite("def f(a, b, foo=bar, **kw): pass")
Fred Drakee3fb18c2001-01-07 06:02:19 +0000136
Anthony Baxterc2a5a632004-08-02 06:10:11 +0000137 self.check_suite("@staticmethod\n"
138 "def f(): pass")
139 self.check_suite("@staticmethod\n"
140 "@funcattrs(x, y)\n"
141 "def f(): pass")
142 self.check_suite("@funcattrs()\n"
143 "def f(): pass")
144
Brett Cannonf4189912005-04-09 02:30:16 +0000145 def test_class_defs(self):
146 self.check_suite("class foo():pass")
Tim Peterse8906822005-04-20 17:45:13 +0000147
Fred Drake58422e52001-06-04 03:56:24 +0000148 def test_import_from_statement(self):
149 self.check_suite("from sys.path import *")
150 self.check_suite("from sys.path import dirname")
Anthony Baxter1a4ddae2004-08-31 10:07:13 +0000151 self.check_suite("from sys.path import (dirname)")
152 self.check_suite("from sys.path import (dirname,)")
Fred Drake58422e52001-06-04 03:56:24 +0000153 self.check_suite("from sys.path import dirname as my_dirname")
Anthony Baxter1a4ddae2004-08-31 10:07:13 +0000154 self.check_suite("from sys.path import (dirname as my_dirname)")
155 self.check_suite("from sys.path import (dirname as my_dirname,)")
Fred Drake58422e52001-06-04 03:56:24 +0000156 self.check_suite("from sys.path import dirname, basename")
Anthony Baxter1a4ddae2004-08-31 10:07:13 +0000157 self.check_suite("from sys.path import (dirname, basename)")
158 self.check_suite("from sys.path import (dirname, basename,)")
Fred Drake58422e52001-06-04 03:56:24 +0000159 self.check_suite(
160 "from sys.path import dirname as my_dirname, basename")
161 self.check_suite(
Anthony Baxter1a4ddae2004-08-31 10:07:13 +0000162 "from sys.path import (dirname as my_dirname, basename)")
163 self.check_suite(
164 "from sys.path import (dirname as my_dirname, basename,)")
165 self.check_suite(
Fred Drake58422e52001-06-04 03:56:24 +0000166 "from sys.path import dirname, basename as my_basename")
Anthony Baxter1a4ddae2004-08-31 10:07:13 +0000167 self.check_suite(
168 "from sys.path import (dirname, basename as my_basename)")
169 self.check_suite(
170 "from sys.path import (dirname, basename as my_basename,)")
Fred Drakee3fb18c2001-01-07 06:02:19 +0000171
Fred Drake58422e52001-06-04 03:56:24 +0000172 def test_basic_import_statement(self):
173 self.check_suite("import sys")
174 self.check_suite("import sys as system")
175 self.check_suite("import sys, math")
176 self.check_suite("import sys as system, math")
177 self.check_suite("import sys, math as my_math")
Fred Drake79ca79d2000-08-21 22:30:53 +0000178
Neal Norwitz9caf9c02003-02-10 01:54:06 +0000179 def test_pep263(self):
180 self.check_suite("# -*- coding: iso-8859-1 -*-\n"
181 "pass\n")
182
183 def test_assert(self):
184 self.check_suite("assert alo < ahi and blo < bhi\n")
185
Fred Drake79ca79d2000-08-21 22:30:53 +0000186#
187# Second, we take *invalid* trees and make sure we get ParserError
188# rejections for them.
189#
190
Fred Drake58422e52001-06-04 03:56:24 +0000191class IllegalSyntaxTestCase(unittest.TestCase):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000192
Fred Drake58422e52001-06-04 03:56:24 +0000193 def check_bad_tree(self, tree, label):
194 try:
Fred Drake6e4f2c02001-07-17 19:33:25 +0000195 parser.sequence2st(tree)
Fred Drake58422e52001-06-04 03:56:24 +0000196 except parser.ParserError:
197 pass
198 else:
199 self.fail("did not detect invalid tree for %r" % label)
Fred Drake79ca79d2000-08-21 22:30:53 +0000200
Fred Drake58422e52001-06-04 03:56:24 +0000201 def test_junk(self):
202 # not even remotely valid:
203 self.check_bad_tree((1, 2, 3), "<junk>")
204
Fred Drakecf580c72001-07-17 03:01:29 +0000205 def test_illegal_yield_1(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000206 # Illegal yield statement: def f(): return 1; yield 1
Fred Drakecf580c72001-07-17 03:01:29 +0000207 tree = \
208 (257,
209 (264,
210 (285,
211 (259,
212 (1, 'def'),
213 (1, 'f'),
214 (260, (7, '('), (8, ')')),
215 (11, ':'),
216 (291,
217 (4, ''),
218 (5, ''),
219 (264,
220 (265,
221 (266,
222 (272,
223 (275,
224 (1, 'return'),
225 (313,
226 (292,
227 (293,
228 (294,
229 (295,
230 (297,
231 (298,
232 (299,
233 (300,
234 (301,
235 (302, (303, (304, (305, (2, '1')))))))))))))))))),
236 (264,
237 (265,
238 (266,
239 (272,
240 (276,
241 (1, 'yield'),
242 (313,
243 (292,
244 (293,
245 (294,
246 (295,
247 (297,
248 (298,
249 (299,
250 (300,
251 (301,
252 (302,
253 (303, (304, (305, (2, '1')))))))))))))))))),
254 (4, ''))),
255 (6, ''))))),
256 (4, ''),
257 (0, ''))))
258 self.check_bad_tree(tree, "def f():\n return 1\n yield 1")
259
260 def test_illegal_yield_2(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000261 # Illegal return in generator: def f(): return 1; yield 1
Fred Drakecf580c72001-07-17 03:01:29 +0000262 tree = \
263 (257,
264 (264,
265 (265,
266 (266,
267 (278,
268 (1, 'from'),
269 (281, (1, '__future__')),
270 (1, 'import'),
271 (279, (1, 'generators')))),
272 (4, ''))),
273 (264,
274 (285,
275 (259,
276 (1, 'def'),
277 (1, 'f'),
278 (260, (7, '('), (8, ')')),
279 (11, ':'),
280 (291,
281 (4, ''),
282 (5, ''),
283 (264,
284 (265,
285 (266,
286 (272,
287 (275,
288 (1, 'return'),
289 (313,
290 (292,
291 (293,
292 (294,
293 (295,
294 (297,
295 (298,
296 (299,
297 (300,
298 (301,
299 (302, (303, (304, (305, (2, '1')))))))))))))))))),
300 (264,
301 (265,
302 (266,
303 (272,
304 (276,
305 (1, 'yield'),
306 (313,
307 (292,
308 (293,
309 (294,
310 (295,
311 (297,
312 (298,
313 (299,
314 (300,
315 (301,
316 (302,
317 (303, (304, (305, (2, '1')))))))))))))))))),
318 (4, ''))),
319 (6, ''))))),
320 (4, ''),
321 (0, ''))))
322 self.check_bad_tree(tree, "def f():\n return 1\n yield 1")
323
Fred Drake58422e52001-06-04 03:56:24 +0000324 def test_print_chevron_comma(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000325 # Illegal input: print >>fp,
Fred Drake58422e52001-06-04 03:56:24 +0000326 tree = \
327 (257,
328 (264,
329 (265,
330 (266,
331 (268,
332 (1, 'print'),
333 (35, '>>'),
334 (290,
335 (291,
336 (292,
337 (293,
338 (295,
339 (296,
340 (297,
341 (298, (299, (300, (301, (302, (303, (1, 'fp')))))))))))))),
342 (12, ','))),
343 (4, ''))),
344 (0, ''))
345 self.check_bad_tree(tree, "print >>fp,")
346
347 def test_a_comma_comma_c(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000348 # Illegal input: a,,c
Fred Drake58422e52001-06-04 03:56:24 +0000349 tree = \
350 (258,
351 (311,
352 (290,
353 (291,
354 (292,
355 (293,
356 (295,
357 (296,
358 (297,
359 (298, (299, (300, (301, (302, (303, (1, 'a')))))))))))))),
360 (12, ','),
361 (12, ','),
362 (290,
363 (291,
364 (292,
365 (293,
366 (295,
367 (296,
368 (297,
369 (298, (299, (300, (301, (302, (303, (1, 'c'))))))))))))))),
370 (4, ''),
371 (0, ''))
372 self.check_bad_tree(tree, "a,,c")
373
374 def test_illegal_operator(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000375 # Illegal input: a $= b
Fred Drake58422e52001-06-04 03:56:24 +0000376 tree = \
377 (257,
378 (264,
379 (265,
380 (266,
381 (267,
382 (312,
383 (291,
384 (292,
385 (293,
386 (294,
387 (296,
388 (297,
389 (298,
390 (299,
391 (300, (301, (302, (303, (304, (1, 'a'))))))))))))))),
392 (268, (37, '$=')),
393 (312,
394 (291,
395 (292,
396 (293,
397 (294,
398 (296,
399 (297,
400 (298,
401 (299,
402 (300, (301, (302, (303, (304, (1, 'b'))))))))))))))))),
403 (4, ''))),
404 (0, ''))
405 self.check_bad_tree(tree, "a $= b")
Fred Drake79ca79d2000-08-21 22:30:53 +0000406
Neal Norwitz9caf9c02003-02-10 01:54:06 +0000407 def test_malformed_global(self):
408 #doesn't have global keyword in ast
409 tree = (257,
410 (264,
411 (265,
412 (266,
413 (282, (1, 'foo'))), (4, ''))),
414 (4, ''),
Tim Petersf2715e02003-02-19 02:35:07 +0000415 (0, ''))
Neal Norwitz9caf9c02003-02-10 01:54:06 +0000416 self.check_bad_tree(tree, "malformed global ast")
Fred Drake79ca79d2000-08-21 22:30:53 +0000417
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000418
419class CompileTestCase(unittest.TestCase):
420
421 # These tests are very minimal. :-(
422
423 def test_compile_expr(self):
424 st = parser.expr('2 + 3')
425 code = parser.compilest(st)
426 self.assertEquals(eval(code), 5)
427
428 def test_compile_suite(self):
429 st = parser.suite('x = 2; y = x + 3')
430 code = parser.compilest(st)
431 globs = {}
432 exec code in globs
433 self.assertEquals(globs['y'], 5)
434
435 def test_compile_error(self):
436 st = parser.suite('1 = 3 + 4')
437 self.assertRaises(SyntaxError, parser.compilest, st)
438
Fred Drake2e2be372001-09-20 21:33:42 +0000439def test_main():
Walter Dörwald21d3a322003-05-01 17:45:56 +0000440 test_support.run_unittest(
441 RoundtripLegalSyntaxTestCase,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000442 IllegalSyntaxTestCase,
443 CompileTestCase,
Walter Dörwald21d3a322003-05-01 17:45:56 +0000444 )
Fred Drake2e2be372001-09-20 21:33:42 +0000445
446
447if __name__ == "__main__":
448 test_main()