blob: 0f8c1d0d0ab6b131dda06d575f20cf5af5d9725b [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")
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"
Fred Drakecf580c72001-07-17 03:01:29 +000035 " for x in range(30):\n"
36 " yield x\n")
37
Fred Drake58422e52001-06-04 03:56:24 +000038 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")
Michael W. Hudson5e83b7a2003-01-29 14:20:23 +000054 self.check_expr("foo - bar")
55 self.check_expr("foo * bar")
56 self.check_expr("foo / bar")
57 self.check_expr("foo // bar")
Fred Drake58422e52001-06-04 03:56:24 +000058 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")
Raymond Hettinger354433a2004-05-19 08:20:33 +000070 self.check_expr("(x for x in range(10))")
71 self.check_expr("foo(x for x in range(10))")
Fred Drake79ca79d2000-08-21 22:30:53 +000072
Fred Drake58422e52001-06-04 03:56:24 +000073 def test_print(self):
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,")
Fred Drake79ca79d2000-08-21 22:30:53 +000080
Fred Drake58422e52001-06-04 03:56:24 +000081 def test_simple_expression(self):
82 # expr_stmt
83 self.check_suite("a")
Fred Drake79ca79d2000-08-21 22:30:53 +000084
Fred Drake58422e52001-06-04 03:56:24 +000085 def test_simple_assignments(self):
86 self.check_suite("a = b")
87 self.check_suite("a = b = c = d = e")
Fred Drake28f739a2000-08-25 22:42:40 +000088
Fred Drake58422e52001-06-04 03:56:24 +000089 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")
Michael W. Hudson5e83b7a2003-01-29 14:20:23 +000094 self.check_suite("a //= b")
Fred Drake58422e52001-06-04 03:56:24 +000095 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")
Fred Drakee3fb18c2001-01-07 06:02:19 +0000102
Fred Drake58422e52001-06-04 03:56:24 +0000103 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")
Fred Drakee3fb18c2001-01-07 06:02:19 +0000112
Fred Drake58422e52001-06-04 03:56:24 +0000113 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")
Fred Drakee3fb18c2001-01-07 06:02:19 +0000121
Anthony Baxterc2a5a632004-08-02 06:10:11 +0000122 self.check_suite("@staticmethod\n"
123 "def f(): pass")
124 self.check_suite("@staticmethod\n"
125 "@funcattrs(x, y)\n"
126 "def f(): pass")
127 self.check_suite("@funcattrs()\n"
128 "def f(): pass")
129
Fred Drake58422e52001-06-04 03:56:24 +0000130 def test_import_from_statement(self):
131 self.check_suite("from sys.path import *")
132 self.check_suite("from sys.path import dirname")
Anthony Baxter1a4ddae2004-08-31 10:07:13 +0000133 self.check_suite("from sys.path import (dirname)")
134 self.check_suite("from sys.path import (dirname,)")
Fred Drake58422e52001-06-04 03:56:24 +0000135 self.check_suite("from sys.path import dirname as my_dirname")
Anthony Baxter1a4ddae2004-08-31 10:07:13 +0000136 self.check_suite("from sys.path import (dirname as my_dirname)")
137 self.check_suite("from sys.path import (dirname as my_dirname,)")
Fred Drake58422e52001-06-04 03:56:24 +0000138 self.check_suite("from sys.path import dirname, basename")
Anthony Baxter1a4ddae2004-08-31 10:07:13 +0000139 self.check_suite("from sys.path import (dirname, basename)")
140 self.check_suite("from sys.path import (dirname, basename,)")
Fred Drake58422e52001-06-04 03:56:24 +0000141 self.check_suite(
142 "from sys.path import dirname as my_dirname, basename")
143 self.check_suite(
Anthony Baxter1a4ddae2004-08-31 10:07:13 +0000144 "from sys.path import (dirname as my_dirname, basename)")
145 self.check_suite(
146 "from sys.path import (dirname as my_dirname, basename,)")
147 self.check_suite(
Fred Drake58422e52001-06-04 03:56:24 +0000148 "from sys.path import dirname, basename as my_basename")
Anthony Baxter1a4ddae2004-08-31 10:07:13 +0000149 self.check_suite(
150 "from sys.path import (dirname, basename as my_basename)")
151 self.check_suite(
152 "from sys.path import (dirname, basename as my_basename,)")
Fred Drakee3fb18c2001-01-07 06:02:19 +0000153
Fred Drake58422e52001-06-04 03:56:24 +0000154 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")
Fred Drake79ca79d2000-08-21 22:30:53 +0000160
Neal Norwitz9caf9c02003-02-10 01:54:06 +0000161 def test_pep263(self):
162 self.check_suite("# -*- coding: iso-8859-1 -*-\n"
163 "pass\n")
164
165 def test_assert(self):
166 self.check_suite("assert alo < ahi and blo < bhi\n")
167
Fred Drake79ca79d2000-08-21 22:30:53 +0000168#
169# Second, we take *invalid* trees and make sure we get ParserError
170# rejections for them.
171#
172
Fred Drake58422e52001-06-04 03:56:24 +0000173class IllegalSyntaxTestCase(unittest.TestCase):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000174
Fred Drake58422e52001-06-04 03:56:24 +0000175 def check_bad_tree(self, tree, label):
176 try:
Fred Drake6e4f2c02001-07-17 19:33:25 +0000177 parser.sequence2st(tree)
Fred Drake58422e52001-06-04 03:56:24 +0000178 except parser.ParserError:
179 pass
180 else:
181 self.fail("did not detect invalid tree for %r" % label)
Fred Drake79ca79d2000-08-21 22:30:53 +0000182
Fred Drake58422e52001-06-04 03:56:24 +0000183 def test_junk(self):
184 # not even remotely valid:
185 self.check_bad_tree((1, 2, 3), "<junk>")
186
Fred Drakecf580c72001-07-17 03:01:29 +0000187 def test_illegal_yield_1(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000188 # Illegal yield statement: def f(): return 1; yield 1
Fred Drakecf580c72001-07-17 03:01:29 +0000189 tree = \
190 (257,
191 (264,
192 (285,
193 (259,
194 (1, 'def'),
195 (1, 'f'),
196 (260, (7, '('), (8, ')')),
197 (11, ':'),
198 (291,
199 (4, ''),
200 (5, ''),
201 (264,
202 (265,
203 (266,
204 (272,
205 (275,
206 (1, 'return'),
207 (313,
208 (292,
209 (293,
210 (294,
211 (295,
212 (297,
213 (298,
214 (299,
215 (300,
216 (301,
217 (302, (303, (304, (305, (2, '1')))))))))))))))))),
218 (264,
219 (265,
220 (266,
221 (272,
222 (276,
223 (1, 'yield'),
224 (313,
225 (292,
226 (293,
227 (294,
228 (295,
229 (297,
230 (298,
231 (299,
232 (300,
233 (301,
234 (302,
235 (303, (304, (305, (2, '1')))))))))))))))))),
236 (4, ''))),
237 (6, ''))))),
238 (4, ''),
239 (0, ''))))
240 self.check_bad_tree(tree, "def f():\n return 1\n yield 1")
241
242 def test_illegal_yield_2(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000243 # Illegal return in generator: def f(): return 1; yield 1
Fred Drakecf580c72001-07-17 03:01:29 +0000244 tree = \
245 (257,
246 (264,
247 (265,
248 (266,
249 (278,
250 (1, 'from'),
251 (281, (1, '__future__')),
252 (1, 'import'),
253 (279, (1, 'generators')))),
254 (4, ''))),
255 (264,
256 (285,
257 (259,
258 (1, 'def'),
259 (1, 'f'),
260 (260, (7, '('), (8, ')')),
261 (11, ':'),
262 (291,
263 (4, ''),
264 (5, ''),
265 (264,
266 (265,
267 (266,
268 (272,
269 (275,
270 (1, 'return'),
271 (313,
272 (292,
273 (293,
274 (294,
275 (295,
276 (297,
277 (298,
278 (299,
279 (300,
280 (301,
281 (302, (303, (304, (305, (2, '1')))))))))))))))))),
282 (264,
283 (265,
284 (266,
285 (272,
286 (276,
287 (1, 'yield'),
288 (313,
289 (292,
290 (293,
291 (294,
292 (295,
293 (297,
294 (298,
295 (299,
296 (300,
297 (301,
298 (302,
299 (303, (304, (305, (2, '1')))))))))))))))))),
300 (4, ''))),
301 (6, ''))))),
302 (4, ''),
303 (0, ''))))
304 self.check_bad_tree(tree, "def f():\n return 1\n yield 1")
305
Fred Drake58422e52001-06-04 03:56:24 +0000306 def test_print_chevron_comma(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000307 # Illegal input: print >>fp,
Fred Drake58422e52001-06-04 03:56:24 +0000308 tree = \
309 (257,
310 (264,
311 (265,
312 (266,
313 (268,
314 (1, 'print'),
315 (35, '>>'),
316 (290,
317 (291,
318 (292,
319 (293,
320 (295,
321 (296,
322 (297,
323 (298, (299, (300, (301, (302, (303, (1, 'fp')))))))))))))),
324 (12, ','))),
325 (4, ''))),
326 (0, ''))
327 self.check_bad_tree(tree, "print >>fp,")
328
329 def test_a_comma_comma_c(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000330 # Illegal input: a,,c
Fred Drake58422e52001-06-04 03:56:24 +0000331 tree = \
332 (258,
333 (311,
334 (290,
335 (291,
336 (292,
337 (293,
338 (295,
339 (296,
340 (297,
341 (298, (299, (300, (301, (302, (303, (1, 'a')))))))))))))),
342 (12, ','),
343 (12, ','),
344 (290,
345 (291,
346 (292,
347 (293,
348 (295,
349 (296,
350 (297,
351 (298, (299, (300, (301, (302, (303, (1, 'c'))))))))))))))),
352 (4, ''),
353 (0, ''))
354 self.check_bad_tree(tree, "a,,c")
355
356 def test_illegal_operator(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000357 # Illegal input: a $= b
Fred Drake58422e52001-06-04 03:56:24 +0000358 tree = \
359 (257,
360 (264,
361 (265,
362 (266,
363 (267,
364 (312,
365 (291,
366 (292,
367 (293,
368 (294,
369 (296,
370 (297,
371 (298,
372 (299,
373 (300, (301, (302, (303, (304, (1, 'a'))))))))))))))),
374 (268, (37, '$=')),
375 (312,
376 (291,
377 (292,
378 (293,
379 (294,
380 (296,
381 (297,
382 (298,
383 (299,
384 (300, (301, (302, (303, (304, (1, 'b'))))))))))))))))),
385 (4, ''))),
386 (0, ''))
387 self.check_bad_tree(tree, "a $= b")
Fred Drake79ca79d2000-08-21 22:30:53 +0000388
Neal Norwitz9caf9c02003-02-10 01:54:06 +0000389 def test_malformed_global(self):
390 #doesn't have global keyword in ast
391 tree = (257,
392 (264,
393 (265,
394 (266,
395 (282, (1, 'foo'))), (4, ''))),
396 (4, ''),
Tim Petersf2715e02003-02-19 02:35:07 +0000397 (0, ''))
Neal Norwitz9caf9c02003-02-10 01:54:06 +0000398 self.check_bad_tree(tree, "malformed global ast")
Fred Drake79ca79d2000-08-21 22:30:53 +0000399
Fred Drake2e2be372001-09-20 21:33:42 +0000400def test_main():
Walter Dörwald21d3a322003-05-01 17:45:56 +0000401 test_support.run_unittest(
402 RoundtripLegalSyntaxTestCase,
403 IllegalSyntaxTestCase
404 )
Fred Drake2e2be372001-09-20 21:33:42 +0000405
406
407if __name__ == "__main__":
408 test_main()