blob: 9652f6bd1dc9e211b1be578fa800ea2261be1090 [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)
Fred Drake58422e52001-06-04 03:56:24 +000018 except parser.ParserError:
19 self.fail("could not roundtrip %r" % s)
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
Fred Drake58422e52001-06-04 03:56:24 +0000122 def test_import_from_statement(self):
123 self.check_suite("from sys.path import *")
124 self.check_suite("from sys.path import dirname")
125 self.check_suite("from sys.path import dirname as my_dirname")
126 self.check_suite("from sys.path import dirname, basename")
127 self.check_suite(
128 "from sys.path import dirname as my_dirname, basename")
129 self.check_suite(
130 "from sys.path import dirname, basename as my_basename")
Fred Drakee3fb18c2001-01-07 06:02:19 +0000131
Fred Drake58422e52001-06-04 03:56:24 +0000132 def test_basic_import_statement(self):
133 self.check_suite("import sys")
134 self.check_suite("import sys as system")
135 self.check_suite("import sys, math")
136 self.check_suite("import sys as system, math")
137 self.check_suite("import sys, math as my_math")
Fred Drake79ca79d2000-08-21 22:30:53 +0000138
Neal Norwitz9caf9c02003-02-10 01:54:06 +0000139 def test_pep263(self):
140 self.check_suite("# -*- coding: iso-8859-1 -*-\n"
141 "pass\n")
142
143 def test_assert(self):
144 self.check_suite("assert alo < ahi and blo < bhi\n")
145
Fred Drake79ca79d2000-08-21 22:30:53 +0000146#
147# Second, we take *invalid* trees and make sure we get ParserError
148# rejections for them.
149#
150
Fred Drake58422e52001-06-04 03:56:24 +0000151class IllegalSyntaxTestCase(unittest.TestCase):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000152
Fred Drake58422e52001-06-04 03:56:24 +0000153 def check_bad_tree(self, tree, label):
154 try:
Fred Drake6e4f2c02001-07-17 19:33:25 +0000155 parser.sequence2st(tree)
Fred Drake58422e52001-06-04 03:56:24 +0000156 except parser.ParserError:
157 pass
158 else:
159 self.fail("did not detect invalid tree for %r" % label)
Fred Drake79ca79d2000-08-21 22:30:53 +0000160
Fred Drake58422e52001-06-04 03:56:24 +0000161 def test_junk(self):
162 # not even remotely valid:
163 self.check_bad_tree((1, 2, 3), "<junk>")
164
Fred Drakecf580c72001-07-17 03:01:29 +0000165 def test_illegal_yield_1(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000166 # Illegal yield statement: def f(): return 1; yield 1
Fred Drakecf580c72001-07-17 03:01:29 +0000167 tree = \
168 (257,
169 (264,
170 (285,
171 (259,
172 (1, 'def'),
173 (1, 'f'),
174 (260, (7, '('), (8, ')')),
175 (11, ':'),
176 (291,
177 (4, ''),
178 (5, ''),
179 (264,
180 (265,
181 (266,
182 (272,
183 (275,
184 (1, 'return'),
185 (313,
186 (292,
187 (293,
188 (294,
189 (295,
190 (297,
191 (298,
192 (299,
193 (300,
194 (301,
195 (302, (303, (304, (305, (2, '1')))))))))))))))))),
196 (264,
197 (265,
198 (266,
199 (272,
200 (276,
201 (1, 'yield'),
202 (313,
203 (292,
204 (293,
205 (294,
206 (295,
207 (297,
208 (298,
209 (299,
210 (300,
211 (301,
212 (302,
213 (303, (304, (305, (2, '1')))))))))))))))))),
214 (4, ''))),
215 (6, ''))))),
216 (4, ''),
217 (0, ''))))
218 self.check_bad_tree(tree, "def f():\n return 1\n yield 1")
219
220 def test_illegal_yield_2(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000221 # Illegal return in generator: def f(): return 1; yield 1
Fred Drakecf580c72001-07-17 03:01:29 +0000222 tree = \
223 (257,
224 (264,
225 (265,
226 (266,
227 (278,
228 (1, 'from'),
229 (281, (1, '__future__')),
230 (1, 'import'),
231 (279, (1, 'generators')))),
232 (4, ''))),
233 (264,
234 (285,
235 (259,
236 (1, 'def'),
237 (1, 'f'),
238 (260, (7, '('), (8, ')')),
239 (11, ':'),
240 (291,
241 (4, ''),
242 (5, ''),
243 (264,
244 (265,
245 (266,
246 (272,
247 (275,
248 (1, 'return'),
249 (313,
250 (292,
251 (293,
252 (294,
253 (295,
254 (297,
255 (298,
256 (299,
257 (300,
258 (301,
259 (302, (303, (304, (305, (2, '1')))))))))))))))))),
260 (264,
261 (265,
262 (266,
263 (272,
264 (276,
265 (1, 'yield'),
266 (313,
267 (292,
268 (293,
269 (294,
270 (295,
271 (297,
272 (298,
273 (299,
274 (300,
275 (301,
276 (302,
277 (303, (304, (305, (2, '1')))))))))))))))))),
278 (4, ''))),
279 (6, ''))))),
280 (4, ''),
281 (0, ''))))
282 self.check_bad_tree(tree, "def f():\n return 1\n yield 1")
283
Fred Drake58422e52001-06-04 03:56:24 +0000284 def test_print_chevron_comma(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000285 # Illegal input: print >>fp,
Fred Drake58422e52001-06-04 03:56:24 +0000286 tree = \
287 (257,
288 (264,
289 (265,
290 (266,
291 (268,
292 (1, 'print'),
293 (35, '>>'),
294 (290,
295 (291,
296 (292,
297 (293,
298 (295,
299 (296,
300 (297,
301 (298, (299, (300, (301, (302, (303, (1, 'fp')))))))))))))),
302 (12, ','))),
303 (4, ''))),
304 (0, ''))
305 self.check_bad_tree(tree, "print >>fp,")
306
307 def test_a_comma_comma_c(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000308 # Illegal input: a,,c
Fred Drake58422e52001-06-04 03:56:24 +0000309 tree = \
310 (258,
311 (311,
312 (290,
313 (291,
314 (292,
315 (293,
316 (295,
317 (296,
318 (297,
319 (298, (299, (300, (301, (302, (303, (1, 'a')))))))))))))),
320 (12, ','),
321 (12, ','),
322 (290,
323 (291,
324 (292,
325 (293,
326 (295,
327 (296,
328 (297,
329 (298, (299, (300, (301, (302, (303, (1, 'c'))))))))))))))),
330 (4, ''),
331 (0, ''))
332 self.check_bad_tree(tree, "a,,c")
333
334 def test_illegal_operator(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000335 # Illegal input: a $= b
Fred Drake58422e52001-06-04 03:56:24 +0000336 tree = \
337 (257,
338 (264,
339 (265,
340 (266,
341 (267,
342 (312,
343 (291,
344 (292,
345 (293,
346 (294,
347 (296,
348 (297,
349 (298,
350 (299,
351 (300, (301, (302, (303, (304, (1, 'a'))))))))))))))),
352 (268, (37, '$=')),
353 (312,
354 (291,
355 (292,
356 (293,
357 (294,
358 (296,
359 (297,
360 (298,
361 (299,
362 (300, (301, (302, (303, (304, (1, 'b'))))))))))))))))),
363 (4, ''))),
364 (0, ''))
365 self.check_bad_tree(tree, "a $= b")
Fred Drake79ca79d2000-08-21 22:30:53 +0000366
Neal Norwitz9caf9c02003-02-10 01:54:06 +0000367 def test_malformed_global(self):
368 #doesn't have global keyword in ast
369 tree = (257,
370 (264,
371 (265,
372 (266,
373 (282, (1, 'foo'))), (4, ''))),
374 (4, ''),
Tim Petersf2715e02003-02-19 02:35:07 +0000375 (0, ''))
Neal Norwitz9caf9c02003-02-10 01:54:06 +0000376 self.check_bad_tree(tree, "malformed global ast")
Fred Drake79ca79d2000-08-21 22:30:53 +0000377
Fred Drake2e2be372001-09-20 21:33:42 +0000378def test_main():
Walter Dörwald21d3a322003-05-01 17:45:56 +0000379 test_support.run_unittest(
380 RoundtripLegalSyntaxTestCase,
381 IllegalSyntaxTestCase
382 )
Fred Drake2e2be372001-09-20 21:33:42 +0000383
384
385if __name__ == "__main__":
386 test_main()