blob: 7860e7bf83ed0b71f2c08b31215be29d6e634b06 [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")
Fred Drake79ca79d2000-08-21 22:30:53 +000070
Fred Drake58422e52001-06-04 03:56:24 +000071 def test_print(self):
72 self.check_suite("print")
73 self.check_suite("print 1")
74 self.check_suite("print 1,")
75 self.check_suite("print >>fp")
76 self.check_suite("print >>fp, 1")
77 self.check_suite("print >>fp, 1,")
Fred Drake79ca79d2000-08-21 22:30:53 +000078
Fred Drake58422e52001-06-04 03:56:24 +000079 def test_simple_expression(self):
80 # expr_stmt
81 self.check_suite("a")
Fred Drake79ca79d2000-08-21 22:30:53 +000082
Fred Drake58422e52001-06-04 03:56:24 +000083 def test_simple_assignments(self):
84 self.check_suite("a = b")
85 self.check_suite("a = b = c = d = e")
Fred Drake28f739a2000-08-25 22:42:40 +000086
Fred Drake58422e52001-06-04 03:56:24 +000087 def test_simple_augmented_assignments(self):
88 self.check_suite("a += b")
89 self.check_suite("a -= b")
90 self.check_suite("a *= b")
91 self.check_suite("a /= b")
Michael W. Hudson5e83b7a2003-01-29 14:20:23 +000092 self.check_suite("a //= b")
Fred Drake58422e52001-06-04 03:56:24 +000093 self.check_suite("a %= b")
94 self.check_suite("a &= b")
95 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")
Fred Drakee3fb18c2001-01-07 06:02:19 +0000100
Fred Drake58422e52001-06-04 03:56:24 +0000101 def test_function_defs(self):
102 self.check_suite("def f(): pass")
103 self.check_suite("def f(*args): pass")
104 self.check_suite("def f(*args, **kw): pass")
105 self.check_suite("def f(**kw): pass")
106 self.check_suite("def f(foo=bar): pass")
107 self.check_suite("def f(foo=bar, *args): pass")
108 self.check_suite("def f(foo=bar, *args, **kw): pass")
109 self.check_suite("def f(foo=bar, **kw): pass")
Fred Drakee3fb18c2001-01-07 06:02:19 +0000110
Fred Drake58422e52001-06-04 03:56:24 +0000111 self.check_suite("def f(a, b): pass")
112 self.check_suite("def f(a, b, *args): pass")
113 self.check_suite("def f(a, b, *args, **kw): pass")
114 self.check_suite("def f(a, b, **kw): pass")
115 self.check_suite("def f(a, b, foo=bar): pass")
116 self.check_suite("def f(a, b, foo=bar, *args): pass")
117 self.check_suite("def f(a, b, foo=bar, *args, **kw): pass")
118 self.check_suite("def f(a, b, foo=bar, **kw): pass")
Fred Drakee3fb18c2001-01-07 06:02:19 +0000119
Fred Drake58422e52001-06-04 03:56:24 +0000120 def test_import_from_statement(self):
121 self.check_suite("from sys.path import *")
122 self.check_suite("from sys.path import dirname")
123 self.check_suite("from sys.path import dirname as my_dirname")
124 self.check_suite("from sys.path import dirname, basename")
125 self.check_suite(
126 "from sys.path import dirname as my_dirname, basename")
127 self.check_suite(
128 "from sys.path import dirname, basename as my_basename")
Fred Drakee3fb18c2001-01-07 06:02:19 +0000129
Fred Drake58422e52001-06-04 03:56:24 +0000130 def test_basic_import_statement(self):
131 self.check_suite("import sys")
132 self.check_suite("import sys as system")
133 self.check_suite("import sys, math")
134 self.check_suite("import sys as system, math")
135 self.check_suite("import sys, math as my_math")
Fred Drake79ca79d2000-08-21 22:30:53 +0000136
Neal Norwitz9caf9c02003-02-10 01:54:06 +0000137 def test_pep263(self):
138 self.check_suite("# -*- coding: iso-8859-1 -*-\n"
139 "pass\n")
140
141 def test_assert(self):
142 self.check_suite("assert alo < ahi and blo < bhi\n")
143
Fred Drake79ca79d2000-08-21 22:30:53 +0000144#
145# Second, we take *invalid* trees and make sure we get ParserError
146# rejections for them.
147#
148
Fred Drake58422e52001-06-04 03:56:24 +0000149class IllegalSyntaxTestCase(unittest.TestCase):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000150
Fred Drake58422e52001-06-04 03:56:24 +0000151 def check_bad_tree(self, tree, label):
152 try:
Fred Drake6e4f2c02001-07-17 19:33:25 +0000153 parser.sequence2st(tree)
Fred Drake58422e52001-06-04 03:56:24 +0000154 except parser.ParserError:
155 pass
156 else:
157 self.fail("did not detect invalid tree for %r" % label)
Fred Drake79ca79d2000-08-21 22:30:53 +0000158
Fred Drake58422e52001-06-04 03:56:24 +0000159 def test_junk(self):
160 # not even remotely valid:
161 self.check_bad_tree((1, 2, 3), "<junk>")
162
Fred Drakecf580c72001-07-17 03:01:29 +0000163 def test_illegal_yield_1(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000164 # Illegal yield statement: def f(): return 1; yield 1
Fred Drakecf580c72001-07-17 03:01:29 +0000165 tree = \
166 (257,
167 (264,
168 (285,
169 (259,
170 (1, 'def'),
171 (1, 'f'),
172 (260, (7, '('), (8, ')')),
173 (11, ':'),
174 (291,
175 (4, ''),
176 (5, ''),
177 (264,
178 (265,
179 (266,
180 (272,
181 (275,
182 (1, 'return'),
183 (313,
184 (292,
185 (293,
186 (294,
187 (295,
188 (297,
189 (298,
190 (299,
191 (300,
192 (301,
193 (302, (303, (304, (305, (2, '1')))))))))))))))))),
194 (264,
195 (265,
196 (266,
197 (272,
198 (276,
199 (1, 'yield'),
200 (313,
201 (292,
202 (293,
203 (294,
204 (295,
205 (297,
206 (298,
207 (299,
208 (300,
209 (301,
210 (302,
211 (303, (304, (305, (2, '1')))))))))))))))))),
212 (4, ''))),
213 (6, ''))))),
214 (4, ''),
215 (0, ''))))
216 self.check_bad_tree(tree, "def f():\n return 1\n yield 1")
217
218 def test_illegal_yield_2(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000219 # Illegal return in generator: def f(): return 1; yield 1
Fred Drakecf580c72001-07-17 03:01:29 +0000220 tree = \
221 (257,
222 (264,
223 (265,
224 (266,
225 (278,
226 (1, 'from'),
227 (281, (1, '__future__')),
228 (1, 'import'),
229 (279, (1, 'generators')))),
230 (4, ''))),
231 (264,
232 (285,
233 (259,
234 (1, 'def'),
235 (1, 'f'),
236 (260, (7, '('), (8, ')')),
237 (11, ':'),
238 (291,
239 (4, ''),
240 (5, ''),
241 (264,
242 (265,
243 (266,
244 (272,
245 (275,
246 (1, 'return'),
247 (313,
248 (292,
249 (293,
250 (294,
251 (295,
252 (297,
253 (298,
254 (299,
255 (300,
256 (301,
257 (302, (303, (304, (305, (2, '1')))))))))))))))))),
258 (264,
259 (265,
260 (266,
261 (272,
262 (276,
263 (1, 'yield'),
264 (313,
265 (292,
266 (293,
267 (294,
268 (295,
269 (297,
270 (298,
271 (299,
272 (300,
273 (301,
274 (302,
275 (303, (304, (305, (2, '1')))))))))))))))))),
276 (4, ''))),
277 (6, ''))))),
278 (4, ''),
279 (0, ''))))
280 self.check_bad_tree(tree, "def f():\n return 1\n yield 1")
281
Fred Drake58422e52001-06-04 03:56:24 +0000282 def test_print_chevron_comma(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000283 # Illegal input: print >>fp,
Fred Drake58422e52001-06-04 03:56:24 +0000284 tree = \
285 (257,
286 (264,
287 (265,
288 (266,
289 (268,
290 (1, 'print'),
291 (35, '>>'),
292 (290,
293 (291,
294 (292,
295 (293,
296 (295,
297 (296,
298 (297,
299 (298, (299, (300, (301, (302, (303, (1, 'fp')))))))))))))),
300 (12, ','))),
301 (4, ''))),
302 (0, ''))
303 self.check_bad_tree(tree, "print >>fp,")
304
305 def test_a_comma_comma_c(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000306 # Illegal input: a,,c
Fred Drake58422e52001-06-04 03:56:24 +0000307 tree = \
308 (258,
309 (311,
310 (290,
311 (291,
312 (292,
313 (293,
314 (295,
315 (296,
316 (297,
317 (298, (299, (300, (301, (302, (303, (1, 'a')))))))))))))),
318 (12, ','),
319 (12, ','),
320 (290,
321 (291,
322 (292,
323 (293,
324 (295,
325 (296,
326 (297,
327 (298, (299, (300, (301, (302, (303, (1, 'c'))))))))))))))),
328 (4, ''),
329 (0, ''))
330 self.check_bad_tree(tree, "a,,c")
331
332 def test_illegal_operator(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000333 # Illegal input: a $= b
Fred Drake58422e52001-06-04 03:56:24 +0000334 tree = \
335 (257,
336 (264,
337 (265,
338 (266,
339 (267,
340 (312,
341 (291,
342 (292,
343 (293,
344 (294,
345 (296,
346 (297,
347 (298,
348 (299,
349 (300, (301, (302, (303, (304, (1, 'a'))))))))))))))),
350 (268, (37, '$=')),
351 (312,
352 (291,
353 (292,
354 (293,
355 (294,
356 (296,
357 (297,
358 (298,
359 (299,
360 (300, (301, (302, (303, (304, (1, 'b'))))))))))))))))),
361 (4, ''))),
362 (0, ''))
363 self.check_bad_tree(tree, "a $= b")
Fred Drake79ca79d2000-08-21 22:30:53 +0000364
Neal Norwitz9caf9c02003-02-10 01:54:06 +0000365 def test_malformed_global(self):
366 #doesn't have global keyword in ast
367 tree = (257,
368 (264,
369 (265,
370 (266,
371 (282, (1, 'foo'))), (4, ''))),
372 (4, ''),
Tim Petersf2715e02003-02-19 02:35:07 +0000373 (0, ''))
Neal Norwitz9caf9c02003-02-10 01:54:06 +0000374 self.check_bad_tree(tree, "malformed global ast")
Fred Drake79ca79d2000-08-21 22:30:53 +0000375
Fred Drake2e2be372001-09-20 21:33:42 +0000376def test_main():
Walter Dörwald21d3a322003-05-01 17:45:56 +0000377 test_support.run_unittest(
378 RoundtripLegalSyntaxTestCase,
379 IllegalSyntaxTestCase
380 )
Fred Drake2e2be372001-09-20 21:33:42 +0000381
382
383if __name__ == "__main__":
384 test_main()