blob: c0b1bfcedec11b241dad1e51ddeefa5ccece30ac [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")
54 self.check_expr("lambda: 0")
55 self.check_expr("lambda x: 0")
56 self.check_expr("lambda *y: 0")
57 self.check_expr("lambda *y, **z: 0")
58 self.check_expr("lambda **z: 0")
59 self.check_expr("lambda x, y: 0")
60 self.check_expr("lambda foo=bar: 0")
61 self.check_expr("lambda foo=bar, spaz=nifty+spit: 0")
62 self.check_expr("lambda foo=bar, **z: 0")
63 self.check_expr("lambda foo=bar, blaz=blat+2, **z: 0")
64 self.check_expr("lambda foo=bar, blaz=blat+2, *y, **z: 0")
65 self.check_expr("lambda x, *y, **z: 0")
Fred Drake79ca79d2000-08-21 22:30:53 +000066
Fred Drake58422e52001-06-04 03:56:24 +000067 def test_print(self):
68 self.check_suite("print")
69 self.check_suite("print 1")
70 self.check_suite("print 1,")
71 self.check_suite("print >>fp")
72 self.check_suite("print >>fp, 1")
73 self.check_suite("print >>fp, 1,")
Fred Drake79ca79d2000-08-21 22:30:53 +000074
Fred Drake58422e52001-06-04 03:56:24 +000075 def test_simple_expression(self):
76 # expr_stmt
77 self.check_suite("a")
Fred Drake79ca79d2000-08-21 22:30:53 +000078
Fred Drake58422e52001-06-04 03:56:24 +000079 def test_simple_assignments(self):
80 self.check_suite("a = b")
81 self.check_suite("a = b = c = d = e")
Fred Drake28f739a2000-08-25 22:42:40 +000082
Fred Drake58422e52001-06-04 03:56:24 +000083 def test_simple_augmented_assignments(self):
84 self.check_suite("a += b")
85 self.check_suite("a -= b")
86 self.check_suite("a *= b")
87 self.check_suite("a /= b")
88 self.check_suite("a %= b")
89 self.check_suite("a &= b")
90 self.check_suite("a |= b")
91 self.check_suite("a ^= b")
92 self.check_suite("a <<= b")
93 self.check_suite("a >>= b")
94 self.check_suite("a **= b")
Fred Drakee3fb18c2001-01-07 06:02:19 +000095
Fred Drake58422e52001-06-04 03:56:24 +000096 def test_function_defs(self):
97 self.check_suite("def f(): pass")
98 self.check_suite("def f(*args): pass")
99 self.check_suite("def f(*args, **kw): pass")
100 self.check_suite("def f(**kw): pass")
101 self.check_suite("def f(foo=bar): pass")
102 self.check_suite("def f(foo=bar, *args): pass")
103 self.check_suite("def f(foo=bar, *args, **kw): pass")
104 self.check_suite("def f(foo=bar, **kw): pass")
Fred Drakee3fb18c2001-01-07 06:02:19 +0000105
Fred Drake58422e52001-06-04 03:56:24 +0000106 self.check_suite("def f(a, b): pass")
107 self.check_suite("def f(a, b, *args): pass")
108 self.check_suite("def f(a, b, *args, **kw): pass")
109 self.check_suite("def f(a, b, **kw): pass")
110 self.check_suite("def f(a, b, foo=bar): pass")
111 self.check_suite("def f(a, b, foo=bar, *args): pass")
112 self.check_suite("def f(a, b, foo=bar, *args, **kw): pass")
113 self.check_suite("def f(a, b, foo=bar, **kw): pass")
Fred Drakee3fb18c2001-01-07 06:02:19 +0000114
Fred Drake58422e52001-06-04 03:56:24 +0000115 def test_import_from_statement(self):
116 self.check_suite("from sys.path import *")
117 self.check_suite("from sys.path import dirname")
118 self.check_suite("from sys.path import dirname as my_dirname")
119 self.check_suite("from sys.path import dirname, basename")
120 self.check_suite(
121 "from sys.path import dirname as my_dirname, basename")
122 self.check_suite(
123 "from sys.path import dirname, basename as my_basename")
Fred Drakee3fb18c2001-01-07 06:02:19 +0000124
Fred Drake58422e52001-06-04 03:56:24 +0000125 def test_basic_import_statement(self):
126 self.check_suite("import sys")
127 self.check_suite("import sys as system")
128 self.check_suite("import sys, math")
129 self.check_suite("import sys as system, math")
130 self.check_suite("import sys, math as my_math")
Fred Drake79ca79d2000-08-21 22:30:53 +0000131
132#
133# Second, we take *invalid* trees and make sure we get ParserError
134# rejections for them.
135#
136
Fred Drake58422e52001-06-04 03:56:24 +0000137class IllegalSyntaxTestCase(unittest.TestCase):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000138
Fred Drake58422e52001-06-04 03:56:24 +0000139 def check_bad_tree(self, tree, label):
140 try:
Fred Drake6e4f2c02001-07-17 19:33:25 +0000141 parser.sequence2st(tree)
Fred Drake58422e52001-06-04 03:56:24 +0000142 except parser.ParserError:
143 pass
144 else:
145 self.fail("did not detect invalid tree for %r" % label)
Fred Drake79ca79d2000-08-21 22:30:53 +0000146
Fred Drake58422e52001-06-04 03:56:24 +0000147 def test_junk(self):
148 # not even remotely valid:
149 self.check_bad_tree((1, 2, 3), "<junk>")
150
Fred Drakecf580c72001-07-17 03:01:29 +0000151 def test_illegal_yield_1(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000152 # Illegal yield statement: def f(): return 1; yield 1
Fred Drakecf580c72001-07-17 03:01:29 +0000153 tree = \
154 (257,
155 (264,
156 (285,
157 (259,
158 (1, 'def'),
159 (1, 'f'),
160 (260, (7, '('), (8, ')')),
161 (11, ':'),
162 (291,
163 (4, ''),
164 (5, ''),
165 (264,
166 (265,
167 (266,
168 (272,
169 (275,
170 (1, 'return'),
171 (313,
172 (292,
173 (293,
174 (294,
175 (295,
176 (297,
177 (298,
178 (299,
179 (300,
180 (301,
181 (302, (303, (304, (305, (2, '1')))))))))))))))))),
182 (264,
183 (265,
184 (266,
185 (272,
186 (276,
187 (1, 'yield'),
188 (313,
189 (292,
190 (293,
191 (294,
192 (295,
193 (297,
194 (298,
195 (299,
196 (300,
197 (301,
198 (302,
199 (303, (304, (305, (2, '1')))))))))))))))))),
200 (4, ''))),
201 (6, ''))))),
202 (4, ''),
203 (0, ''))))
204 self.check_bad_tree(tree, "def f():\n return 1\n yield 1")
205
206 def test_illegal_yield_2(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000207 # Illegal return in generator: def f(): return 1; yield 1
Fred Drakecf580c72001-07-17 03:01:29 +0000208 tree = \
209 (257,
210 (264,
211 (265,
212 (266,
213 (278,
214 (1, 'from'),
215 (281, (1, '__future__')),
216 (1, 'import'),
217 (279, (1, 'generators')))),
218 (4, ''))),
219 (264,
220 (285,
221 (259,
222 (1, 'def'),
223 (1, 'f'),
224 (260, (7, '('), (8, ')')),
225 (11, ':'),
226 (291,
227 (4, ''),
228 (5, ''),
229 (264,
230 (265,
231 (266,
232 (272,
233 (275,
234 (1, 'return'),
235 (313,
236 (292,
237 (293,
238 (294,
239 (295,
240 (297,
241 (298,
242 (299,
243 (300,
244 (301,
245 (302, (303, (304, (305, (2, '1')))))))))))))))))),
246 (264,
247 (265,
248 (266,
249 (272,
250 (276,
251 (1, 'yield'),
252 (313,
253 (292,
254 (293,
255 (294,
256 (295,
257 (297,
258 (298,
259 (299,
260 (300,
261 (301,
262 (302,
263 (303, (304, (305, (2, '1')))))))))))))))))),
264 (4, ''))),
265 (6, ''))))),
266 (4, ''),
267 (0, ''))))
268 self.check_bad_tree(tree, "def f():\n return 1\n yield 1")
269
Fred Drake58422e52001-06-04 03:56:24 +0000270 def test_print_chevron_comma(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000271 # Illegal input: print >>fp,
Fred Drake58422e52001-06-04 03:56:24 +0000272 tree = \
273 (257,
274 (264,
275 (265,
276 (266,
277 (268,
278 (1, 'print'),
279 (35, '>>'),
280 (290,
281 (291,
282 (292,
283 (293,
284 (295,
285 (296,
286 (297,
287 (298, (299, (300, (301, (302, (303, (1, 'fp')))))))))))))),
288 (12, ','))),
289 (4, ''))),
290 (0, ''))
291 self.check_bad_tree(tree, "print >>fp,")
292
293 def test_a_comma_comma_c(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000294 # Illegal input: a,,c
Fred Drake58422e52001-06-04 03:56:24 +0000295 tree = \
296 (258,
297 (311,
298 (290,
299 (291,
300 (292,
301 (293,
302 (295,
303 (296,
304 (297,
305 (298, (299, (300, (301, (302, (303, (1, 'a')))))))))))))),
306 (12, ','),
307 (12, ','),
308 (290,
309 (291,
310 (292,
311 (293,
312 (295,
313 (296,
314 (297,
315 (298, (299, (300, (301, (302, (303, (1, 'c'))))))))))))))),
316 (4, ''),
317 (0, ''))
318 self.check_bad_tree(tree, "a,,c")
319
320 def test_illegal_operator(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000321 # Illegal input: a $= b
Fred Drake58422e52001-06-04 03:56:24 +0000322 tree = \
323 (257,
324 (264,
325 (265,
326 (266,
327 (267,
328 (312,
329 (291,
330 (292,
331 (293,
332 (294,
333 (296,
334 (297,
335 (298,
336 (299,
337 (300, (301, (302, (303, (304, (1, 'a'))))))))))))))),
338 (268, (37, '$=')),
339 (312,
340 (291,
341 (292,
342 (293,
343 (294,
344 (296,
345 (297,
346 (298,
347 (299,
348 (300, (301, (302, (303, (304, (1, 'b'))))))))))))))))),
349 (4, ''))),
350 (0, ''))
351 self.check_bad_tree(tree, "a $= b")
Fred Drake79ca79d2000-08-21 22:30:53 +0000352
353
Fred Drake2e2be372001-09-20 21:33:42 +0000354def test_main():
355 loader = unittest.TestLoader()
356 suite = unittest.TestSuite()
357 suite.addTest(loader.loadTestsFromTestCase(RoundtripLegalSyntaxTestCase))
358 suite.addTest(loader.loadTestsFromTestCase(IllegalSyntaxTestCase))
359 test_support.run_suite(suite)
360
361
362if __name__ == "__main__":
363 test_main()