blob: ddb58b5e8d2e68ff5dcadae217701670ff72ef5b [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)
Guido van Rossumb940e112007-01-10 16:19:56 +000018 except parser.ParserError as why:
Anthony Baxterc2a5a632004-08-02 06:10:11 +000019 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]")
Thomas Wouters49fd7fa2006-04-21 10:40:58 +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_simple_expression(self):
89 # expr_stmt
90 self.check_suite("a")
Fred Drake79ca79d2000-08-21 22:30:53 +000091
Fred Drake58422e52001-06-04 03:56:24 +000092 def test_simple_assignments(self):
93 self.check_suite("a = b")
94 self.check_suite("a = b = c = d = e")
Fred Drake28f739a2000-08-25 22:42:40 +000095
Fred Drake58422e52001-06-04 03:56:24 +000096 def test_simple_augmented_assignments(self):
97 self.check_suite("a += b")
98 self.check_suite("a -= b")
99 self.check_suite("a *= b")
100 self.check_suite("a /= b")
Michael W. Hudson5e83b7a2003-01-29 14:20:23 +0000101 self.check_suite("a //= b")
Fred Drake58422e52001-06-04 03:56:24 +0000102 self.check_suite("a %= b")
103 self.check_suite("a &= b")
104 self.check_suite("a |= b")
105 self.check_suite("a ^= b")
106 self.check_suite("a <<= b")
107 self.check_suite("a >>= b")
108 self.check_suite("a **= b")
Fred Drakee3fb18c2001-01-07 06:02:19 +0000109
Fred Drake58422e52001-06-04 03:56:24 +0000110 def test_function_defs(self):
111 self.check_suite("def f(): pass")
112 self.check_suite("def f(*args): pass")
113 self.check_suite("def f(*args, **kw): pass")
114 self.check_suite("def f(**kw): pass")
115 self.check_suite("def f(foo=bar): pass")
116 self.check_suite("def f(foo=bar, *args): pass")
117 self.check_suite("def f(foo=bar, *args, **kw): pass")
118 self.check_suite("def f(foo=bar, **kw): pass")
Fred Drakee3fb18c2001-01-07 06:02:19 +0000119
Fred Drake58422e52001-06-04 03:56:24 +0000120 self.check_suite("def f(a, b): pass")
121 self.check_suite("def f(a, b, *args): pass")
122 self.check_suite("def f(a, b, *args, **kw): pass")
123 self.check_suite("def f(a, b, **kw): pass")
124 self.check_suite("def f(a, b, foo=bar): pass")
125 self.check_suite("def f(a, b, foo=bar, *args): pass")
126 self.check_suite("def f(a, b, foo=bar, *args, **kw): pass")
127 self.check_suite("def f(a, b, foo=bar, **kw): pass")
Fred Drakee3fb18c2001-01-07 06:02:19 +0000128
Anthony Baxterc2a5a632004-08-02 06:10:11 +0000129 self.check_suite("@staticmethod\n"
130 "def f(): pass")
131 self.check_suite("@staticmethod\n"
132 "@funcattrs(x, y)\n"
133 "def f(): pass")
134 self.check_suite("@funcattrs()\n"
135 "def f(): pass")
136
Brett Cannonf4189912005-04-09 02:30:16 +0000137 def test_class_defs(self):
138 self.check_suite("class foo():pass")
Tim Peterse8906822005-04-20 17:45:13 +0000139
Fred Drake58422e52001-06-04 03:56:24 +0000140 def test_import_from_statement(self):
141 self.check_suite("from sys.path import *")
142 self.check_suite("from sys.path import dirname")
Anthony Baxter1a4ddae2004-08-31 10:07:13 +0000143 self.check_suite("from sys.path import (dirname)")
144 self.check_suite("from sys.path import (dirname,)")
Fred Drake58422e52001-06-04 03:56:24 +0000145 self.check_suite("from sys.path import dirname as my_dirname")
Anthony Baxter1a4ddae2004-08-31 10:07:13 +0000146 self.check_suite("from sys.path import (dirname as my_dirname)")
147 self.check_suite("from sys.path import (dirname as my_dirname,)")
Fred Drake58422e52001-06-04 03:56:24 +0000148 self.check_suite("from sys.path import dirname, basename")
Anthony Baxter1a4ddae2004-08-31 10:07:13 +0000149 self.check_suite("from sys.path import (dirname, basename)")
150 self.check_suite("from sys.path import (dirname, basename,)")
Fred Drake58422e52001-06-04 03:56:24 +0000151 self.check_suite(
152 "from sys.path import dirname as my_dirname, basename")
153 self.check_suite(
Anthony Baxter1a4ddae2004-08-31 10:07:13 +0000154 "from sys.path import (dirname as my_dirname, basename)")
155 self.check_suite(
156 "from sys.path import (dirname as my_dirname, basename,)")
157 self.check_suite(
Fred Drake58422e52001-06-04 03:56:24 +0000158 "from sys.path import dirname, basename as my_basename")
Anthony Baxter1a4ddae2004-08-31 10:07:13 +0000159 self.check_suite(
160 "from sys.path import (dirname, basename as my_basename)")
161 self.check_suite(
162 "from sys.path import (dirname, basename as my_basename,)")
Fred Drakee3fb18c2001-01-07 06:02:19 +0000163
Fred Drake58422e52001-06-04 03:56:24 +0000164 def test_basic_import_statement(self):
165 self.check_suite("import sys")
166 self.check_suite("import sys as system")
167 self.check_suite("import sys, math")
168 self.check_suite("import sys as system, math")
169 self.check_suite("import sys, math as my_math")
Fred Drake79ca79d2000-08-21 22:30:53 +0000170
Neal Norwitz9caf9c02003-02-10 01:54:06 +0000171 def test_pep263(self):
172 self.check_suite("# -*- coding: iso-8859-1 -*-\n"
173 "pass\n")
174
175 def test_assert(self):
176 self.check_suite("assert alo < ahi and blo < bhi\n")
177
Thomas Wouters89f507f2006-12-13 04:49:30 +0000178 def test_position(self):
179 # An absolutely minimal test of position information. Better
180 # tests would be a big project.
181 code = "def f(x):\n return x + 1\n"
182 st1 = parser.suite(code)
183 st2 = st1.totuple(line_info=1, col_info=1)
184
185 def walk(tree):
186 node_type = tree[0]
187 next = tree[1]
188 if isinstance(next, tuple):
189 for elt in tree[1:]:
190 for x in walk(elt):
191 yield x
192 else:
193 yield tree
194
195 terminals = list(walk(st2))
196 self.assertEqual([
197 (1, 'def', 1, 0),
198 (1, 'f', 1, 4),
199 (7, '(', 1, 5),
200 (1, 'x', 1, 6),
201 (8, ')', 1, 7),
202 (11, ':', 1, 8),
203 (4, '', 1, 9),
204 (5, '', 2, -1),
205 (1, 'return', 2, 4),
206 (1, 'x', 2, 11),
207 (14, '+', 2, 13),
208 (2, '1', 2, 15),
209 (4, '', 2, 16),
210 (6, '', 2, -1),
211 (4, '', 2, -1),
212 (0, '', 2, -1)],
213 terminals)
214
215
Fred Drake79ca79d2000-08-21 22:30:53 +0000216#
217# Second, we take *invalid* trees and make sure we get ParserError
218# rejections for them.
219#
220
Fred Drake58422e52001-06-04 03:56:24 +0000221class IllegalSyntaxTestCase(unittest.TestCase):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000222
Fred Drake58422e52001-06-04 03:56:24 +0000223 def check_bad_tree(self, tree, label):
224 try:
Fred Drake6e4f2c02001-07-17 19:33:25 +0000225 parser.sequence2st(tree)
Fred Drake58422e52001-06-04 03:56:24 +0000226 except parser.ParserError:
227 pass
228 else:
229 self.fail("did not detect invalid tree for %r" % label)
Fred Drake79ca79d2000-08-21 22:30:53 +0000230
Fred Drake58422e52001-06-04 03:56:24 +0000231 def test_junk(self):
232 # not even remotely valid:
233 self.check_bad_tree((1, 2, 3), "<junk>")
234
Fred Drakecf580c72001-07-17 03:01:29 +0000235 def test_illegal_yield_1(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000236 # Illegal yield statement: def f(): return 1; yield 1
Fred Drakecf580c72001-07-17 03:01:29 +0000237 tree = \
238 (257,
239 (264,
240 (285,
241 (259,
242 (1, 'def'),
243 (1, 'f'),
244 (260, (7, '('), (8, ')')),
245 (11, ':'),
246 (291,
247 (4, ''),
248 (5, ''),
249 (264,
250 (265,
251 (266,
252 (272,
253 (275,
254 (1, 'return'),
255 (313,
256 (292,
257 (293,
258 (294,
259 (295,
260 (297,
261 (298,
262 (299,
263 (300,
264 (301,
265 (302, (303, (304, (305, (2, '1')))))))))))))))))),
266 (264,
267 (265,
268 (266,
269 (272,
270 (276,
271 (1, 'yield'),
272 (313,
273 (292,
274 (293,
275 (294,
276 (295,
277 (297,
278 (298,
279 (299,
280 (300,
281 (301,
282 (302,
283 (303, (304, (305, (2, '1')))))))))))))))))),
284 (4, ''))),
285 (6, ''))))),
286 (4, ''),
287 (0, ''))))
288 self.check_bad_tree(tree, "def f():\n return 1\n yield 1")
289
290 def test_illegal_yield_2(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000291 # Illegal return in generator: def f(): return 1; yield 1
Fred Drakecf580c72001-07-17 03:01:29 +0000292 tree = \
293 (257,
294 (264,
295 (265,
296 (266,
297 (278,
298 (1, 'from'),
299 (281, (1, '__future__')),
300 (1, 'import'),
301 (279, (1, 'generators')))),
302 (4, ''))),
303 (264,
304 (285,
305 (259,
306 (1, 'def'),
307 (1, 'f'),
308 (260, (7, '('), (8, ')')),
309 (11, ':'),
310 (291,
311 (4, ''),
312 (5, ''),
313 (264,
314 (265,
315 (266,
316 (272,
317 (275,
318 (1, 'return'),
319 (313,
320 (292,
321 (293,
322 (294,
323 (295,
324 (297,
325 (298,
326 (299,
327 (300,
328 (301,
329 (302, (303, (304, (305, (2, '1')))))))))))))))))),
330 (264,
331 (265,
332 (266,
333 (272,
334 (276,
335 (1, 'yield'),
336 (313,
337 (292,
338 (293,
339 (294,
340 (295,
341 (297,
342 (298,
343 (299,
344 (300,
345 (301,
346 (302,
347 (303, (304, (305, (2, '1')))))))))))))))))),
348 (4, ''))),
349 (6, ''))))),
350 (4, ''),
351 (0, ''))))
352 self.check_bad_tree(tree, "def f():\n return 1\n yield 1")
353
Fred Drake58422e52001-06-04 03:56:24 +0000354 def test_a_comma_comma_c(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000355 # Illegal input: a,,c
Fred Drake58422e52001-06-04 03:56:24 +0000356 tree = \
357 (258,
358 (311,
359 (290,
360 (291,
361 (292,
362 (293,
363 (295,
364 (296,
365 (297,
366 (298, (299, (300, (301, (302, (303, (1, 'a')))))))))))))),
367 (12, ','),
368 (12, ','),
369 (290,
370 (291,
371 (292,
372 (293,
373 (295,
374 (296,
375 (297,
376 (298, (299, (300, (301, (302, (303, (1, 'c'))))))))))))))),
377 (4, ''),
378 (0, ''))
379 self.check_bad_tree(tree, "a,,c")
380
381 def test_illegal_operator(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000382 # Illegal input: a $= b
Fred Drake58422e52001-06-04 03:56:24 +0000383 tree = \
384 (257,
385 (264,
386 (265,
387 (266,
388 (267,
389 (312,
390 (291,
391 (292,
392 (293,
393 (294,
394 (296,
395 (297,
396 (298,
397 (299,
398 (300, (301, (302, (303, (304, (1, 'a'))))))))))))))),
399 (268, (37, '$=')),
400 (312,
401 (291,
402 (292,
403 (293,
404 (294,
405 (296,
406 (297,
407 (298,
408 (299,
409 (300, (301, (302, (303, (304, (1, 'b'))))))))))))))))),
410 (4, ''))),
411 (0, ''))
412 self.check_bad_tree(tree, "a $= b")
Fred Drake79ca79d2000-08-21 22:30:53 +0000413
Neal Norwitz9caf9c02003-02-10 01:54:06 +0000414 def test_malformed_global(self):
415 #doesn't have global keyword in ast
416 tree = (257,
417 (264,
418 (265,
419 (266,
420 (282, (1, 'foo'))), (4, ''))),
421 (4, ''),
Tim Petersf2715e02003-02-19 02:35:07 +0000422 (0, ''))
Neal Norwitz9caf9c02003-02-10 01:54:06 +0000423 self.check_bad_tree(tree, "malformed global ast")
Fred Drake79ca79d2000-08-21 22:30:53 +0000424
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000425
426class CompileTestCase(unittest.TestCase):
427
428 # These tests are very minimal. :-(
429
430 def test_compile_expr(self):
431 st = parser.expr('2 + 3')
432 code = parser.compilest(st)
433 self.assertEquals(eval(code), 5)
434
435 def test_compile_suite(self):
436 st = parser.suite('x = 2; y = x + 3')
437 code = parser.compilest(st)
438 globs = {}
Georg Brandl7cae87c2006-09-06 06:51:57 +0000439 exec(code, globs)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000440 self.assertEquals(globs['y'], 5)
441
442 def test_compile_error(self):
443 st = parser.suite('1 = 3 + 4')
444 self.assertRaises(SyntaxError, parser.compilest, st)
445
Fred Drake2e2be372001-09-20 21:33:42 +0000446def test_main():
Walter Dörwald21d3a322003-05-01 17:45:56 +0000447 test_support.run_unittest(
448 RoundtripLegalSyntaxTestCase,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000449 IllegalSyntaxTestCase,
450 CompileTestCase,
Walter Dörwald21d3a322003-05-01 17:45:56 +0000451 )
Fred Drake2e2be372001-09-20 21:33:42 +0000452
453
454if __name__ == "__main__":
455 test_main()