blob: 978ce5712c4faa12b1647f9a1c015d26250dfea0 [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")
133 self.check_suite("from sys.path import dirname as my_dirname")
134 self.check_suite("from sys.path import dirname, basename")
135 self.check_suite(
136 "from sys.path import dirname as my_dirname, basename")
137 self.check_suite(
138 "from sys.path import dirname, basename as my_basename")
Fred Drakee3fb18c2001-01-07 06:02:19 +0000139
Fred Drake58422e52001-06-04 03:56:24 +0000140 def test_basic_import_statement(self):
141 self.check_suite("import sys")
142 self.check_suite("import sys as system")
143 self.check_suite("import sys, math")
144 self.check_suite("import sys as system, math")
145 self.check_suite("import sys, math as my_math")
Fred Drake79ca79d2000-08-21 22:30:53 +0000146
Neal Norwitz9caf9c02003-02-10 01:54:06 +0000147 def test_pep263(self):
148 self.check_suite("# -*- coding: iso-8859-1 -*-\n"
149 "pass\n")
150
151 def test_assert(self):
152 self.check_suite("assert alo < ahi and blo < bhi\n")
153
Fred Drake79ca79d2000-08-21 22:30:53 +0000154#
155# Second, we take *invalid* trees and make sure we get ParserError
156# rejections for them.
157#
158
Fred Drake58422e52001-06-04 03:56:24 +0000159class IllegalSyntaxTestCase(unittest.TestCase):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000160
Fred Drake58422e52001-06-04 03:56:24 +0000161 def check_bad_tree(self, tree, label):
162 try:
Fred Drake6e4f2c02001-07-17 19:33:25 +0000163 parser.sequence2st(tree)
Fred Drake58422e52001-06-04 03:56:24 +0000164 except parser.ParserError:
165 pass
166 else:
167 self.fail("did not detect invalid tree for %r" % label)
Fred Drake79ca79d2000-08-21 22:30:53 +0000168
Fred Drake58422e52001-06-04 03:56:24 +0000169 def test_junk(self):
170 # not even remotely valid:
171 self.check_bad_tree((1, 2, 3), "<junk>")
172
Fred Drakecf580c72001-07-17 03:01:29 +0000173 def test_illegal_yield_1(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000174 # Illegal yield statement: def f(): return 1; yield 1
Fred Drakecf580c72001-07-17 03:01:29 +0000175 tree = \
176 (257,
177 (264,
178 (285,
179 (259,
180 (1, 'def'),
181 (1, 'f'),
182 (260, (7, '('), (8, ')')),
183 (11, ':'),
184 (291,
185 (4, ''),
186 (5, ''),
187 (264,
188 (265,
189 (266,
190 (272,
191 (275,
192 (1, 'return'),
193 (313,
194 (292,
195 (293,
196 (294,
197 (295,
198 (297,
199 (298,
200 (299,
201 (300,
202 (301,
203 (302, (303, (304, (305, (2, '1')))))))))))))))))),
204 (264,
205 (265,
206 (266,
207 (272,
208 (276,
209 (1, 'yield'),
210 (313,
211 (292,
212 (293,
213 (294,
214 (295,
215 (297,
216 (298,
217 (299,
218 (300,
219 (301,
220 (302,
221 (303, (304, (305, (2, '1')))))))))))))))))),
222 (4, ''))),
223 (6, ''))))),
224 (4, ''),
225 (0, ''))))
226 self.check_bad_tree(tree, "def f():\n return 1\n yield 1")
227
228 def test_illegal_yield_2(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000229 # Illegal return in generator: def f(): return 1; yield 1
Fred Drakecf580c72001-07-17 03:01:29 +0000230 tree = \
231 (257,
232 (264,
233 (265,
234 (266,
235 (278,
236 (1, 'from'),
237 (281, (1, '__future__')),
238 (1, 'import'),
239 (279, (1, 'generators')))),
240 (4, ''))),
241 (264,
242 (285,
243 (259,
244 (1, 'def'),
245 (1, 'f'),
246 (260, (7, '('), (8, ')')),
247 (11, ':'),
248 (291,
249 (4, ''),
250 (5, ''),
251 (264,
252 (265,
253 (266,
254 (272,
255 (275,
256 (1, 'return'),
257 (313,
258 (292,
259 (293,
260 (294,
261 (295,
262 (297,
263 (298,
264 (299,
265 (300,
266 (301,
267 (302, (303, (304, (305, (2, '1')))))))))))))))))),
268 (264,
269 (265,
270 (266,
271 (272,
272 (276,
273 (1, 'yield'),
274 (313,
275 (292,
276 (293,
277 (294,
278 (295,
279 (297,
280 (298,
281 (299,
282 (300,
283 (301,
284 (302,
285 (303, (304, (305, (2, '1')))))))))))))))))),
286 (4, ''))),
287 (6, ''))))),
288 (4, ''),
289 (0, ''))))
290 self.check_bad_tree(tree, "def f():\n return 1\n yield 1")
291
Fred Drake58422e52001-06-04 03:56:24 +0000292 def test_print_chevron_comma(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000293 # Illegal input: print >>fp,
Fred Drake58422e52001-06-04 03:56:24 +0000294 tree = \
295 (257,
296 (264,
297 (265,
298 (266,
299 (268,
300 (1, 'print'),
301 (35, '>>'),
302 (290,
303 (291,
304 (292,
305 (293,
306 (295,
307 (296,
308 (297,
309 (298, (299, (300, (301, (302, (303, (1, 'fp')))))))))))))),
310 (12, ','))),
311 (4, ''))),
312 (0, ''))
313 self.check_bad_tree(tree, "print >>fp,")
314
315 def test_a_comma_comma_c(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000316 # Illegal input: a,,c
Fred Drake58422e52001-06-04 03:56:24 +0000317 tree = \
318 (258,
319 (311,
320 (290,
321 (291,
322 (292,
323 (293,
324 (295,
325 (296,
326 (297,
327 (298, (299, (300, (301, (302, (303, (1, 'a')))))))))))))),
328 (12, ','),
329 (12, ','),
330 (290,
331 (291,
332 (292,
333 (293,
334 (295,
335 (296,
336 (297,
337 (298, (299, (300, (301, (302, (303, (1, 'c'))))))))))))))),
338 (4, ''),
339 (0, ''))
340 self.check_bad_tree(tree, "a,,c")
341
342 def test_illegal_operator(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000343 # Illegal input: a $= b
Fred Drake58422e52001-06-04 03:56:24 +0000344 tree = \
345 (257,
346 (264,
347 (265,
348 (266,
349 (267,
350 (312,
351 (291,
352 (292,
353 (293,
354 (294,
355 (296,
356 (297,
357 (298,
358 (299,
359 (300, (301, (302, (303, (304, (1, 'a'))))))))))))))),
360 (268, (37, '$=')),
361 (312,
362 (291,
363 (292,
364 (293,
365 (294,
366 (296,
367 (297,
368 (298,
369 (299,
370 (300, (301, (302, (303, (304, (1, 'b'))))))))))))))))),
371 (4, ''))),
372 (0, ''))
373 self.check_bad_tree(tree, "a $= b")
Fred Drake79ca79d2000-08-21 22:30:53 +0000374
Neal Norwitz9caf9c02003-02-10 01:54:06 +0000375 def test_malformed_global(self):
376 #doesn't have global keyword in ast
377 tree = (257,
378 (264,
379 (265,
380 (266,
381 (282, (1, 'foo'))), (4, ''))),
382 (4, ''),
Tim Petersf2715e02003-02-19 02:35:07 +0000383 (0, ''))
Neal Norwitz9caf9c02003-02-10 01:54:06 +0000384 self.check_bad_tree(tree, "malformed global ast")
Fred Drake79ca79d2000-08-21 22:30:53 +0000385
Fred Drake2e2be372001-09-20 21:33:42 +0000386def test_main():
Walter Dörwald21d3a322003-05-01 17:45:56 +0000387 test_support.run_unittest(
388 RoundtripLegalSyntaxTestCase,
389 IllegalSyntaxTestCase
390 )
Fred Drake2e2be372001-09-20 21:33:42 +0000391
392
393if __name__ == "__main__":
394 test_main()