blob: d76db6ce686324726bea3c491d9c15332c46170d [file] [log] [blame]
Fred Drake79ca79d2000-08-21 22:30:53 +00001import parser
Benjamin Peterson6f08e852008-11-03 15:19:35 +00002import os
Fred Drake58422e52001-06-04 03:56:24 +00003import unittest
Barry Warsaw04f357c2002-07-23 19:04:11 +00004from test import test_support
Fred Drake79ca79d2000-08-21 22:30:53 +00005
6#
7# First, we test that we can generate trees from valid source fragments,
8# and that these valid trees are indeed allowed by the tree-loading side
9# of the parser module.
10#
11
Fred Drake58422e52001-06-04 03:56:24 +000012class RoundtripLegalSyntaxTestCase(unittest.TestCase):
Guido van Rossum32c2ae72002-08-22 19:45:32 +000013
Fred Drake58422e52001-06-04 03:56:24 +000014 def roundtrip(self, f, s):
15 st1 = f(s)
16 t = st1.totuple()
17 try:
Fred Drake6e4f2c02001-07-17 19:33:25 +000018 st2 = parser.sequence2st(t)
Anthony Baxterc2a5a632004-08-02 06:10:11 +000019 except parser.ParserError, why:
20 self.fail("could not roundtrip %r: %s" % (s, why))
Fred Drake79ca79d2000-08-21 22:30:53 +000021
Fred Drake58422e52001-06-04 03:56:24 +000022 self.assertEquals(t, st2.totuple(),
23 "could not re-generate syntax tree")
Fred Drake28f739a2000-08-25 22:42:40 +000024
Fred Drake58422e52001-06-04 03:56:24 +000025 def check_expr(self, s):
26 self.roundtrip(parser.expr, s)
Fred Drake28f739a2000-08-25 22:42:40 +000027
Fred Drake58422e52001-06-04 03:56:24 +000028 def check_suite(self, s):
29 self.roundtrip(parser.suite, s)
Fred Drake28f739a2000-08-25 22:42:40 +000030
Fred Drakecf580c72001-07-17 03:01:29 +000031 def test_yield_statement(self):
Tim Peters496563a2002-04-01 00:28:59 +000032 self.check_suite("def f(): yield 1")
Phillip J. Eby0d6615f2005-08-02 00:46:46 +000033 self.check_suite("def f(): yield")
34 self.check_suite("def f(): x += yield")
35 self.check_suite("def f(): x = yield 1")
36 self.check_suite("def f(): x = y = yield 1")
37 self.check_suite("def f(): x = yield")
38 self.check_suite("def f(): x = y = yield")
39 self.check_suite("def f(): 1 + (yield)*2")
40 self.check_suite("def f(): (yield 1)*2")
Tim Peters496563a2002-04-01 00:28:59 +000041 self.check_suite("def f(): return; yield 1")
42 self.check_suite("def f(): yield 1; return")
43 self.check_suite("def f():\n"
Fred Drakecf580c72001-07-17 03:01:29 +000044 " for x in range(30):\n"
45 " yield x\n")
Phillip J. Eby0d6615f2005-08-02 00:46:46 +000046 self.check_suite("def f():\n"
47 " if (yield):\n"
48 " yield x\n")
Fred Drakecf580c72001-07-17 03:01:29 +000049
Fred Drake58422e52001-06-04 03:56:24 +000050 def test_expressions(self):
51 self.check_expr("foo(1)")
52 self.check_expr("[1, 2, 3]")
53 self.check_expr("[x**3 for x in range(20)]")
54 self.check_expr("[x**3 for x in range(20) if x % 3]")
Neal Norwitzd3a91622006-04-12 05:27:46 +000055 self.check_expr("[x**3 for x in range(20) if x % 2 if x % 3]")
56 self.check_expr("list(x**3 for x in range(20))")
57 self.check_expr("list(x**3 for x in range(20) if x % 3)")
58 self.check_expr("list(x**3 for x in range(20) if x % 2 if x % 3)")
Fred Drake58422e52001-06-04 03:56:24 +000059 self.check_expr("foo(*args)")
60 self.check_expr("foo(*args, **kw)")
61 self.check_expr("foo(**kw)")
62 self.check_expr("foo(key=value)")
63 self.check_expr("foo(key=value, *args)")
64 self.check_expr("foo(key=value, *args, **kw)")
65 self.check_expr("foo(key=value, **kw)")
66 self.check_expr("foo(a, b, c, *args)")
67 self.check_expr("foo(a, b, c, *args, **kw)")
68 self.check_expr("foo(a, b, c, **kw)")
69 self.check_expr("foo + bar")
Michael W. Hudson5e83b7a2003-01-29 14:20:23 +000070 self.check_expr("foo - bar")
71 self.check_expr("foo * bar")
72 self.check_expr("foo / bar")
73 self.check_expr("foo // bar")
Fred Drake58422e52001-06-04 03:56:24 +000074 self.check_expr("lambda: 0")
75 self.check_expr("lambda x: 0")
76 self.check_expr("lambda *y: 0")
77 self.check_expr("lambda *y, **z: 0")
78 self.check_expr("lambda **z: 0")
79 self.check_expr("lambda x, y: 0")
80 self.check_expr("lambda foo=bar: 0")
81 self.check_expr("lambda foo=bar, spaz=nifty+spit: 0")
82 self.check_expr("lambda foo=bar, **z: 0")
83 self.check_expr("lambda foo=bar, blaz=blat+2, **z: 0")
84 self.check_expr("lambda foo=bar, blaz=blat+2, *y, **z: 0")
85 self.check_expr("lambda x, *y, **z: 0")
Raymond Hettinger354433a2004-05-19 08:20:33 +000086 self.check_expr("(x for x in range(10))")
87 self.check_expr("foo(x for x in range(10))")
Fred Drake79ca79d2000-08-21 22:30:53 +000088
Fred Drake58422e52001-06-04 03:56:24 +000089 def test_print(self):
90 self.check_suite("print")
91 self.check_suite("print 1")
92 self.check_suite("print 1,")
93 self.check_suite("print >>fp")
94 self.check_suite("print >>fp, 1")
95 self.check_suite("print >>fp, 1,")
Fred Drake79ca79d2000-08-21 22:30:53 +000096
Fred Drake58422e52001-06-04 03:56:24 +000097 def test_simple_expression(self):
98 # expr_stmt
99 self.check_suite("a")
Fred Drake79ca79d2000-08-21 22:30:53 +0000100
Fred Drake58422e52001-06-04 03:56:24 +0000101 def test_simple_assignments(self):
102 self.check_suite("a = b")
103 self.check_suite("a = b = c = d = e")
Fred Drake28f739a2000-08-25 22:42:40 +0000104
Fred Drake58422e52001-06-04 03:56:24 +0000105 def test_simple_augmented_assignments(self):
106 self.check_suite("a += b")
107 self.check_suite("a -= b")
108 self.check_suite("a *= b")
109 self.check_suite("a /= b")
Michael W. Hudson5e83b7a2003-01-29 14:20:23 +0000110 self.check_suite("a //= b")
Fred Drake58422e52001-06-04 03:56:24 +0000111 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")
117 self.check_suite("a **= b")
Fred Drakee3fb18c2001-01-07 06:02:19 +0000118
Fred Drake58422e52001-06-04 03:56:24 +0000119 def test_function_defs(self):
120 self.check_suite("def f(): pass")
121 self.check_suite("def f(*args): pass")
122 self.check_suite("def f(*args, **kw): pass")
123 self.check_suite("def f(**kw): pass")
124 self.check_suite("def f(foo=bar): pass")
125 self.check_suite("def f(foo=bar, *args): pass")
126 self.check_suite("def f(foo=bar, *args, **kw): pass")
127 self.check_suite("def f(foo=bar, **kw): pass")
Fred Drakee3fb18c2001-01-07 06:02:19 +0000128
Fred Drake58422e52001-06-04 03:56:24 +0000129 self.check_suite("def f(a, b): pass")
130 self.check_suite("def f(a, b, *args): pass")
131 self.check_suite("def f(a, b, *args, **kw): pass")
132 self.check_suite("def f(a, b, **kw): pass")
133 self.check_suite("def f(a, b, foo=bar): pass")
134 self.check_suite("def f(a, b, foo=bar, *args): pass")
135 self.check_suite("def f(a, b, foo=bar, *args, **kw): pass")
136 self.check_suite("def f(a, b, foo=bar, **kw): pass")
Fred Drakee3fb18c2001-01-07 06:02:19 +0000137
Anthony Baxterc2a5a632004-08-02 06:10:11 +0000138 self.check_suite("@staticmethod\n"
139 "def f(): pass")
140 self.check_suite("@staticmethod\n"
141 "@funcattrs(x, y)\n"
142 "def f(): pass")
143 self.check_suite("@funcattrs()\n"
144 "def f(): pass")
145
Brett Cannonf4189912005-04-09 02:30:16 +0000146 def test_class_defs(self):
147 self.check_suite("class foo():pass")
Tim Peterse8906822005-04-20 17:45:13 +0000148
Fred Drake58422e52001-06-04 03:56:24 +0000149 def test_import_from_statement(self):
150 self.check_suite("from sys.path import *")
151 self.check_suite("from sys.path import dirname")
Anthony Baxter1a4ddae2004-08-31 10:07:13 +0000152 self.check_suite("from sys.path import (dirname)")
153 self.check_suite("from sys.path import (dirname,)")
Fred Drake58422e52001-06-04 03:56:24 +0000154 self.check_suite("from sys.path import dirname as my_dirname")
Anthony Baxter1a4ddae2004-08-31 10:07:13 +0000155 self.check_suite("from sys.path import (dirname as my_dirname)")
156 self.check_suite("from sys.path import (dirname as my_dirname,)")
Fred Drake58422e52001-06-04 03:56:24 +0000157 self.check_suite("from sys.path import dirname, basename")
Anthony Baxter1a4ddae2004-08-31 10:07:13 +0000158 self.check_suite("from sys.path import (dirname, basename)")
159 self.check_suite("from sys.path import (dirname, basename,)")
Fred Drake58422e52001-06-04 03:56:24 +0000160 self.check_suite(
161 "from sys.path import dirname as my_dirname, basename")
162 self.check_suite(
Anthony Baxter1a4ddae2004-08-31 10:07:13 +0000163 "from sys.path import (dirname as my_dirname, basename)")
164 self.check_suite(
165 "from sys.path import (dirname as my_dirname, basename,)")
166 self.check_suite(
Fred Drake58422e52001-06-04 03:56:24 +0000167 "from sys.path import dirname, basename as my_basename")
Anthony Baxter1a4ddae2004-08-31 10:07:13 +0000168 self.check_suite(
169 "from sys.path import (dirname, basename as my_basename)")
170 self.check_suite(
171 "from sys.path import (dirname, basename as my_basename,)")
Benjamin Peterson6f08e852008-11-03 15:19:35 +0000172 self.check_suite("from .bogus import x")
Fred Drakee3fb18c2001-01-07 06:02:19 +0000173
Fred Drake58422e52001-06-04 03:56:24 +0000174 def test_basic_import_statement(self):
175 self.check_suite("import sys")
176 self.check_suite("import sys as system")
177 self.check_suite("import sys, math")
178 self.check_suite("import sys as system, math")
179 self.check_suite("import sys, math as my_math")
Fred Drake79ca79d2000-08-21 22:30:53 +0000180
Neal Norwitz9caf9c02003-02-10 01:54:06 +0000181 def test_pep263(self):
182 self.check_suite("# -*- coding: iso-8859-1 -*-\n"
183 "pass\n")
184
185 def test_assert(self):
186 self.check_suite("assert alo < ahi and blo < bhi\n")
187
Fred Drake79ca79d2000-08-21 22:30:53 +0000188#
189# Second, we take *invalid* trees and make sure we get ParserError
190# rejections for them.
191#
192
Fred Drake58422e52001-06-04 03:56:24 +0000193class IllegalSyntaxTestCase(unittest.TestCase):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000194
Fred Drake58422e52001-06-04 03:56:24 +0000195 def check_bad_tree(self, tree, label):
196 try:
Fred Drake6e4f2c02001-07-17 19:33:25 +0000197 parser.sequence2st(tree)
Fred Drake58422e52001-06-04 03:56:24 +0000198 except parser.ParserError:
199 pass
200 else:
201 self.fail("did not detect invalid tree for %r" % label)
Fred Drake79ca79d2000-08-21 22:30:53 +0000202
Fred Drake58422e52001-06-04 03:56:24 +0000203 def test_junk(self):
204 # not even remotely valid:
205 self.check_bad_tree((1, 2, 3), "<junk>")
206
Fred Drakecf580c72001-07-17 03:01:29 +0000207 def test_illegal_yield_1(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000208 # Illegal yield statement: def f(): return 1; yield 1
Fred Drakecf580c72001-07-17 03:01:29 +0000209 tree = \
210 (257,
211 (264,
212 (285,
213 (259,
214 (1, 'def'),
215 (1, 'f'),
216 (260, (7, '('), (8, ')')),
217 (11, ':'),
218 (291,
219 (4, ''),
220 (5, ''),
221 (264,
222 (265,
223 (266,
224 (272,
225 (275,
226 (1, 'return'),
227 (313,
228 (292,
229 (293,
230 (294,
231 (295,
232 (297,
233 (298,
234 (299,
235 (300,
236 (301,
237 (302, (303, (304, (305, (2, '1')))))))))))))))))),
238 (264,
239 (265,
240 (266,
241 (272,
242 (276,
243 (1, 'yield'),
244 (313,
245 (292,
246 (293,
247 (294,
248 (295,
249 (297,
250 (298,
251 (299,
252 (300,
253 (301,
254 (302,
255 (303, (304, (305, (2, '1')))))))))))))))))),
256 (4, ''))),
257 (6, ''))))),
258 (4, ''),
259 (0, ''))))
260 self.check_bad_tree(tree, "def f():\n return 1\n yield 1")
261
262 def test_illegal_yield_2(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000263 # Illegal return in generator: def f(): return 1; yield 1
Fred Drakecf580c72001-07-17 03:01:29 +0000264 tree = \
265 (257,
266 (264,
267 (265,
268 (266,
269 (278,
270 (1, 'from'),
271 (281, (1, '__future__')),
272 (1, 'import'),
273 (279, (1, 'generators')))),
274 (4, ''))),
275 (264,
276 (285,
277 (259,
278 (1, 'def'),
279 (1, 'f'),
280 (260, (7, '('), (8, ')')),
281 (11, ':'),
282 (291,
283 (4, ''),
284 (5, ''),
285 (264,
286 (265,
287 (266,
288 (272,
289 (275,
290 (1, 'return'),
291 (313,
292 (292,
293 (293,
294 (294,
295 (295,
296 (297,
297 (298,
298 (299,
299 (300,
300 (301,
301 (302, (303, (304, (305, (2, '1')))))))))))))))))),
302 (264,
303 (265,
304 (266,
305 (272,
306 (276,
307 (1, 'yield'),
308 (313,
309 (292,
310 (293,
311 (294,
312 (295,
313 (297,
314 (298,
315 (299,
316 (300,
317 (301,
318 (302,
319 (303, (304, (305, (2, '1')))))))))))))))))),
320 (4, ''))),
321 (6, ''))))),
322 (4, ''),
323 (0, ''))))
324 self.check_bad_tree(tree, "def f():\n return 1\n yield 1")
325
Fred Drake58422e52001-06-04 03:56:24 +0000326 def test_print_chevron_comma(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000327 # Illegal input: print >>fp,
Fred Drake58422e52001-06-04 03:56:24 +0000328 tree = \
329 (257,
330 (264,
331 (265,
332 (266,
333 (268,
334 (1, 'print'),
335 (35, '>>'),
336 (290,
337 (291,
338 (292,
339 (293,
340 (295,
341 (296,
342 (297,
343 (298, (299, (300, (301, (302, (303, (1, 'fp')))))))))))))),
344 (12, ','))),
345 (4, ''))),
346 (0, ''))
347 self.check_bad_tree(tree, "print >>fp,")
348
349 def test_a_comma_comma_c(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000350 # Illegal input: a,,c
Fred Drake58422e52001-06-04 03:56:24 +0000351 tree = \
352 (258,
353 (311,
354 (290,
355 (291,
356 (292,
357 (293,
358 (295,
359 (296,
360 (297,
361 (298, (299, (300, (301, (302, (303, (1, 'a')))))))))))))),
362 (12, ','),
363 (12, ','),
364 (290,
365 (291,
366 (292,
367 (293,
368 (295,
369 (296,
370 (297,
371 (298, (299, (300, (301, (302, (303, (1, 'c'))))))))))))))),
372 (4, ''),
373 (0, ''))
374 self.check_bad_tree(tree, "a,,c")
375
376 def test_illegal_operator(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000377 # Illegal input: a $= b
Fred Drake58422e52001-06-04 03:56:24 +0000378 tree = \
379 (257,
380 (264,
381 (265,
382 (266,
383 (267,
384 (312,
385 (291,
386 (292,
387 (293,
388 (294,
389 (296,
390 (297,
391 (298,
392 (299,
393 (300, (301, (302, (303, (304, (1, 'a'))))))))))))))),
394 (268, (37, '$=')),
395 (312,
396 (291,
397 (292,
398 (293,
399 (294,
400 (296,
401 (297,
402 (298,
403 (299,
404 (300, (301, (302, (303, (304, (1, 'b'))))))))))))))))),
405 (4, ''))),
406 (0, ''))
407 self.check_bad_tree(tree, "a $= b")
Fred Drake79ca79d2000-08-21 22:30:53 +0000408
Neal Norwitz9caf9c02003-02-10 01:54:06 +0000409 def test_malformed_global(self):
410 #doesn't have global keyword in ast
411 tree = (257,
412 (264,
413 (265,
414 (266,
415 (282, (1, 'foo'))), (4, ''))),
416 (4, ''),
Tim Petersf2715e02003-02-19 02:35:07 +0000417 (0, ''))
Neal Norwitz9caf9c02003-02-10 01:54:06 +0000418 self.check_bad_tree(tree, "malformed global ast")
Fred Drake79ca79d2000-08-21 22:30:53 +0000419
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000420
421class CompileTestCase(unittest.TestCase):
422
423 # These tests are very minimal. :-(
424
425 def test_compile_expr(self):
426 st = parser.expr('2 + 3')
427 code = parser.compilest(st)
428 self.assertEquals(eval(code), 5)
429
430 def test_compile_suite(self):
431 st = parser.suite('x = 2; y = x + 3')
432 code = parser.compilest(st)
433 globs = {}
434 exec code in globs
435 self.assertEquals(globs['y'], 5)
436
437 def test_compile_error(self):
438 st = parser.suite('1 = 3 + 4')
439 self.assertRaises(SyntaxError, parser.compilest, st)
440
Fred Drake2e2be372001-09-20 21:33:42 +0000441def test_main():
Walter Dörwald21d3a322003-05-01 17:45:56 +0000442 test_support.run_unittest(
443 RoundtripLegalSyntaxTestCase,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000444 IllegalSyntaxTestCase,
445 CompileTestCase,
Walter Dörwald21d3a322003-05-01 17:45:56 +0000446 )
Fred Drake2e2be372001-09-20 21:33:42 +0000447
448
449if __name__ == "__main__":
450 test_main()