blob: 81708b5309539915a5fb7d7c2c26bd3f377a0c36 [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
137#
138# Second, we take *invalid* trees and make sure we get ParserError
139# rejections for them.
140#
141
Fred Drake58422e52001-06-04 03:56:24 +0000142class IllegalSyntaxTestCase(unittest.TestCase):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000143
Fred Drake58422e52001-06-04 03:56:24 +0000144 def check_bad_tree(self, tree, label):
145 try:
Fred Drake6e4f2c02001-07-17 19:33:25 +0000146 parser.sequence2st(tree)
Fred Drake58422e52001-06-04 03:56:24 +0000147 except parser.ParserError:
148 pass
149 else:
150 self.fail("did not detect invalid tree for %r" % label)
Fred Drake79ca79d2000-08-21 22:30:53 +0000151
Fred Drake58422e52001-06-04 03:56:24 +0000152 def test_junk(self):
153 # not even remotely valid:
154 self.check_bad_tree((1, 2, 3), "<junk>")
155
Fred Drakecf580c72001-07-17 03:01:29 +0000156 def test_illegal_yield_1(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000157 # Illegal yield statement: def f(): return 1; yield 1
Fred Drakecf580c72001-07-17 03:01:29 +0000158 tree = \
159 (257,
160 (264,
161 (285,
162 (259,
163 (1, 'def'),
164 (1, 'f'),
165 (260, (7, '('), (8, ')')),
166 (11, ':'),
167 (291,
168 (4, ''),
169 (5, ''),
170 (264,
171 (265,
172 (266,
173 (272,
174 (275,
175 (1, 'return'),
176 (313,
177 (292,
178 (293,
179 (294,
180 (295,
181 (297,
182 (298,
183 (299,
184 (300,
185 (301,
186 (302, (303, (304, (305, (2, '1')))))))))))))))))),
187 (264,
188 (265,
189 (266,
190 (272,
191 (276,
192 (1, 'yield'),
193 (313,
194 (292,
195 (293,
196 (294,
197 (295,
198 (297,
199 (298,
200 (299,
201 (300,
202 (301,
203 (302,
204 (303, (304, (305, (2, '1')))))))))))))))))),
205 (4, ''))),
206 (6, ''))))),
207 (4, ''),
208 (0, ''))))
209 self.check_bad_tree(tree, "def f():\n return 1\n yield 1")
210
211 def test_illegal_yield_2(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000212 # Illegal return in generator: def f(): return 1; yield 1
Fred Drakecf580c72001-07-17 03:01:29 +0000213 tree = \
214 (257,
215 (264,
216 (265,
217 (266,
218 (278,
219 (1, 'from'),
220 (281, (1, '__future__')),
221 (1, 'import'),
222 (279, (1, 'generators')))),
223 (4, ''))),
224 (264,
225 (285,
226 (259,
227 (1, 'def'),
228 (1, 'f'),
229 (260, (7, '('), (8, ')')),
230 (11, ':'),
231 (291,
232 (4, ''),
233 (5, ''),
234 (264,
235 (265,
236 (266,
237 (272,
238 (275,
239 (1, 'return'),
240 (313,
241 (292,
242 (293,
243 (294,
244 (295,
245 (297,
246 (298,
247 (299,
248 (300,
249 (301,
250 (302, (303, (304, (305, (2, '1')))))))))))))))))),
251 (264,
252 (265,
253 (266,
254 (272,
255 (276,
256 (1, 'yield'),
257 (313,
258 (292,
259 (293,
260 (294,
261 (295,
262 (297,
263 (298,
264 (299,
265 (300,
266 (301,
267 (302,
268 (303, (304, (305, (2, '1')))))))))))))))))),
269 (4, ''))),
270 (6, ''))))),
271 (4, ''),
272 (0, ''))))
273 self.check_bad_tree(tree, "def f():\n return 1\n yield 1")
274
Fred Drake58422e52001-06-04 03:56:24 +0000275 def test_print_chevron_comma(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000276 # Illegal input: print >>fp,
Fred Drake58422e52001-06-04 03:56:24 +0000277 tree = \
278 (257,
279 (264,
280 (265,
281 (266,
282 (268,
283 (1, 'print'),
284 (35, '>>'),
285 (290,
286 (291,
287 (292,
288 (293,
289 (295,
290 (296,
291 (297,
292 (298, (299, (300, (301, (302, (303, (1, 'fp')))))))))))))),
293 (12, ','))),
294 (4, ''))),
295 (0, ''))
296 self.check_bad_tree(tree, "print >>fp,")
297
298 def test_a_comma_comma_c(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000299 # Illegal input: a,,c
Fred Drake58422e52001-06-04 03:56:24 +0000300 tree = \
301 (258,
302 (311,
303 (290,
304 (291,
305 (292,
306 (293,
307 (295,
308 (296,
309 (297,
310 (298, (299, (300, (301, (302, (303, (1, 'a')))))))))))))),
311 (12, ','),
312 (12, ','),
313 (290,
314 (291,
315 (292,
316 (293,
317 (295,
318 (296,
319 (297,
320 (298, (299, (300, (301, (302, (303, (1, 'c'))))))))))))))),
321 (4, ''),
322 (0, ''))
323 self.check_bad_tree(tree, "a,,c")
324
325 def test_illegal_operator(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000326 # Illegal input: a $= b
Fred Drake58422e52001-06-04 03:56:24 +0000327 tree = \
328 (257,
329 (264,
330 (265,
331 (266,
332 (267,
333 (312,
334 (291,
335 (292,
336 (293,
337 (294,
338 (296,
339 (297,
340 (298,
341 (299,
342 (300, (301, (302, (303, (304, (1, 'a'))))))))))))))),
343 (268, (37, '$=')),
344 (312,
345 (291,
346 (292,
347 (293,
348 (294,
349 (296,
350 (297,
351 (298,
352 (299,
353 (300, (301, (302, (303, (304, (1, 'b'))))))))))))))))),
354 (4, ''))),
355 (0, ''))
356 self.check_bad_tree(tree, "a $= b")
Fred Drake79ca79d2000-08-21 22:30:53 +0000357
358
Fred Drake2e2be372001-09-20 21:33:42 +0000359def test_main():
360 loader = unittest.TestLoader()
361 suite = unittest.TestSuite()
362 suite.addTest(loader.loadTestsFromTestCase(RoundtripLegalSyntaxTestCase))
363 suite.addTest(loader.loadTestsFromTestCase(IllegalSyntaxTestCase))
364 test_support.run_suite(suite)
365
366
367if __name__ == "__main__":
368 test_main()