blob: ade74311a5d2e775a5654f1f5d5242479b7e0c9c [file] [log] [blame]
Fred Drake79ca79d2000-08-21 22:30:53 +00001import parser
Fred Drake58422e52001-06-04 03:56:24 +00002import unittest
Christian Heimesb186d002008-03-18 15:15:01 +00003import sys
Mark Dickinson211c6252009-02-01 10:28:51 +00004import operator
Benjamin Petersonee8712c2008-05-20 21:35:26 +00005from test import support
Fred Drake79ca79d2000-08-21 22:30:53 +00006
7#
8# First, we test that we can generate trees from valid source fragments,
9# and that these valid trees are indeed allowed by the tree-loading side
10# of the parser module.
11#
12
Fred Drake58422e52001-06-04 03:56:24 +000013class RoundtripLegalSyntaxTestCase(unittest.TestCase):
Guido van Rossum32c2ae72002-08-22 19:45:32 +000014
Fred Drake58422e52001-06-04 03:56:24 +000015 def roundtrip(self, f, s):
16 st1 = f(s)
17 t = st1.totuple()
18 try:
Fred Drake6e4f2c02001-07-17 19:33:25 +000019 st2 = parser.sequence2st(t)
Guido van Rossumb940e112007-01-10 16:19:56 +000020 except parser.ParserError as why:
Anthony Baxterc2a5a632004-08-02 06:10:11 +000021 self.fail("could not roundtrip %r: %s" % (s, why))
Fred Drake79ca79d2000-08-21 22:30:53 +000022
Fred Drake58422e52001-06-04 03:56:24 +000023 self.assertEquals(t, st2.totuple(),
24 "could not re-generate syntax tree")
Fred Drake28f739a2000-08-25 22:42:40 +000025
Fred Drake58422e52001-06-04 03:56:24 +000026 def check_expr(self, s):
27 self.roundtrip(parser.expr, s)
Fred Drake28f739a2000-08-25 22:42:40 +000028
Benjamin Petersonf216c942008-10-31 02:28:05 +000029 def test_flags_passed(self):
30 # The unicode literals flags has to be passed from the paser to AST
31 # generation.
32 suite = parser.suite("from __future__ import unicode_literals; x = ''")
33 code = suite.compile()
34 scope = {}
35 exec(code, {}, scope)
Ezio Melottie9615932010-01-24 19:26:24 +000036 self.assertIsInstance(scope["x"], str)
Benjamin Petersonf216c942008-10-31 02:28:05 +000037
Fred Drake58422e52001-06-04 03:56:24 +000038 def check_suite(self, s):
39 self.roundtrip(parser.suite, s)
Fred Drake28f739a2000-08-25 22:42:40 +000040
Fred Drakecf580c72001-07-17 03:01:29 +000041 def test_yield_statement(self):
Tim Peters496563a2002-04-01 00:28:59 +000042 self.check_suite("def f(): yield 1")
Phillip J. Eby0d6615f2005-08-02 00:46:46 +000043 self.check_suite("def f(): yield")
44 self.check_suite("def f(): x += yield")
45 self.check_suite("def f(): x = yield 1")
46 self.check_suite("def f(): x = y = yield 1")
47 self.check_suite("def f(): x = yield")
48 self.check_suite("def f(): x = y = yield")
49 self.check_suite("def f(): 1 + (yield)*2")
50 self.check_suite("def f(): (yield 1)*2")
Tim Peters496563a2002-04-01 00:28:59 +000051 self.check_suite("def f(): return; yield 1")
52 self.check_suite("def f(): yield 1; return")
53 self.check_suite("def f():\n"
Fred Drakecf580c72001-07-17 03:01:29 +000054 " for x in range(30):\n"
55 " yield x\n")
Phillip J. Eby0d6615f2005-08-02 00:46:46 +000056 self.check_suite("def f():\n"
57 " if (yield):\n"
58 " yield x\n")
Fred Drakecf580c72001-07-17 03:01:29 +000059
Fred Drake58422e52001-06-04 03:56:24 +000060 def test_expressions(self):
61 self.check_expr("foo(1)")
62 self.check_expr("[1, 2, 3]")
63 self.check_expr("[x**3 for x in range(20)]")
64 self.check_expr("[x**3 for x in range(20) if x % 3]")
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000065 self.check_expr("[x**3 for x in range(20) if x % 2 if x % 3]")
66 self.check_expr("list(x**3 for x in range(20))")
67 self.check_expr("list(x**3 for x in range(20) if x % 3)")
68 self.check_expr("list(x**3 for x in range(20) if x % 2 if x % 3)")
Fred Drake58422e52001-06-04 03:56:24 +000069 self.check_expr("foo(*args)")
70 self.check_expr("foo(*args, **kw)")
71 self.check_expr("foo(**kw)")
72 self.check_expr("foo(key=value)")
73 self.check_expr("foo(key=value, *args)")
74 self.check_expr("foo(key=value, *args, **kw)")
75 self.check_expr("foo(key=value, **kw)")
76 self.check_expr("foo(a, b, c, *args)")
77 self.check_expr("foo(a, b, c, *args, **kw)")
78 self.check_expr("foo(a, b, c, **kw)")
Benjamin Peterson3938a902008-08-20 02:33:00 +000079 self.check_expr("foo(a, *args, keyword=23)")
Fred Drake58422e52001-06-04 03:56:24 +000080 self.check_expr("foo + bar")
Michael W. Hudson5e83b7a2003-01-29 14:20:23 +000081 self.check_expr("foo - bar")
82 self.check_expr("foo * bar")
83 self.check_expr("foo / bar")
84 self.check_expr("foo // bar")
Fred Drake58422e52001-06-04 03:56:24 +000085 self.check_expr("lambda: 0")
86 self.check_expr("lambda x: 0")
87 self.check_expr("lambda *y: 0")
88 self.check_expr("lambda *y, **z: 0")
89 self.check_expr("lambda **z: 0")
90 self.check_expr("lambda x, y: 0")
91 self.check_expr("lambda foo=bar: 0")
92 self.check_expr("lambda foo=bar, spaz=nifty+spit: 0")
93 self.check_expr("lambda foo=bar, **z: 0")
94 self.check_expr("lambda foo=bar, blaz=blat+2, **z: 0")
95 self.check_expr("lambda foo=bar, blaz=blat+2, *y, **z: 0")
96 self.check_expr("lambda x, *y, **z: 0")
Raymond Hettinger354433a2004-05-19 08:20:33 +000097 self.check_expr("(x for x in range(10))")
98 self.check_expr("foo(x for x in range(10))")
Fred Drake79ca79d2000-08-21 22:30:53 +000099
Fred Drake58422e52001-06-04 03:56:24 +0000100 def test_simple_expression(self):
101 # expr_stmt
102 self.check_suite("a")
Fred Drake79ca79d2000-08-21 22:30:53 +0000103
Fred Drake58422e52001-06-04 03:56:24 +0000104 def test_simple_assignments(self):
105 self.check_suite("a = b")
106 self.check_suite("a = b = c = d = e")
Fred Drake28f739a2000-08-25 22:42:40 +0000107
Fred Drake58422e52001-06-04 03:56:24 +0000108 def test_simple_augmented_assignments(self):
109 self.check_suite("a += b")
110 self.check_suite("a -= b")
111 self.check_suite("a *= b")
112 self.check_suite("a /= b")
Michael W. Hudson5e83b7a2003-01-29 14:20:23 +0000113 self.check_suite("a //= b")
Fred Drake58422e52001-06-04 03:56:24 +0000114 self.check_suite("a %= b")
115 self.check_suite("a &= b")
116 self.check_suite("a |= b")
117 self.check_suite("a ^= b")
118 self.check_suite("a <<= b")
119 self.check_suite("a >>= b")
120 self.check_suite("a **= b")
Fred Drakee3fb18c2001-01-07 06:02:19 +0000121
Fred Drake58422e52001-06-04 03:56:24 +0000122 def test_function_defs(self):
123 self.check_suite("def f(): pass")
124 self.check_suite("def f(*args): pass")
125 self.check_suite("def f(*args, **kw): pass")
126 self.check_suite("def f(**kw): pass")
127 self.check_suite("def f(foo=bar): pass")
128 self.check_suite("def f(foo=bar, *args): pass")
129 self.check_suite("def f(foo=bar, *args, **kw): pass")
130 self.check_suite("def f(foo=bar, **kw): pass")
Fred Drakee3fb18c2001-01-07 06:02:19 +0000131
Fred Drake58422e52001-06-04 03:56:24 +0000132 self.check_suite("def f(a, b): pass")
133 self.check_suite("def f(a, b, *args): pass")
134 self.check_suite("def f(a, b, *args, **kw): pass")
135 self.check_suite("def f(a, b, **kw): pass")
136 self.check_suite("def f(a, b, foo=bar): pass")
137 self.check_suite("def f(a, b, foo=bar, *args): pass")
138 self.check_suite("def f(a, b, foo=bar, *args, **kw): pass")
139 self.check_suite("def f(a, b, foo=bar, **kw): pass")
Fred Drakee3fb18c2001-01-07 06:02:19 +0000140
Anthony Baxterc2a5a632004-08-02 06:10:11 +0000141 self.check_suite("@staticmethod\n"
142 "def f(): pass")
143 self.check_suite("@staticmethod\n"
144 "@funcattrs(x, y)\n"
145 "def f(): pass")
146 self.check_suite("@funcattrs()\n"
147 "def f(): pass")
148
Brett Cannonf4189912005-04-09 02:30:16 +0000149 def test_class_defs(self):
150 self.check_suite("class foo():pass")
Guido van Rossumfc158e22007-11-15 19:17:28 +0000151 self.check_suite("class foo(object):pass")
Mark Dickinson2bd61a92010-07-04 16:37:31 +0000152 self.check_suite("@class_decorator\n"
153 "class foo():pass")
154 self.check_suite("@class_decorator(arg)\n"
155 "class foo():pass")
156 self.check_suite("@decorator1\n"
157 "@decorator2\n"
158 "class foo():pass")
Tim Peterse8906822005-04-20 17:45:13 +0000159
Fred Drake58422e52001-06-04 03:56:24 +0000160 def test_import_from_statement(self):
161 self.check_suite("from sys.path import *")
162 self.check_suite("from sys.path import dirname")
Anthony Baxter1a4ddae2004-08-31 10:07:13 +0000163 self.check_suite("from sys.path import (dirname)")
164 self.check_suite("from sys.path import (dirname,)")
Fred Drake58422e52001-06-04 03:56:24 +0000165 self.check_suite("from sys.path import dirname as my_dirname")
Anthony Baxter1a4ddae2004-08-31 10:07:13 +0000166 self.check_suite("from sys.path import (dirname as my_dirname)")
167 self.check_suite("from sys.path import (dirname as my_dirname,)")
Fred Drake58422e52001-06-04 03:56:24 +0000168 self.check_suite("from sys.path import dirname, basename")
Anthony Baxter1a4ddae2004-08-31 10:07:13 +0000169 self.check_suite("from sys.path import (dirname, basename)")
170 self.check_suite("from sys.path import (dirname, basename,)")
Fred Drake58422e52001-06-04 03:56:24 +0000171 self.check_suite(
172 "from sys.path import dirname as my_dirname, basename")
173 self.check_suite(
Anthony Baxter1a4ddae2004-08-31 10:07:13 +0000174 "from sys.path import (dirname as my_dirname, basename)")
175 self.check_suite(
176 "from sys.path import (dirname as my_dirname, basename,)")
177 self.check_suite(
Fred Drake58422e52001-06-04 03:56:24 +0000178 "from sys.path import dirname, basename as my_basename")
Anthony Baxter1a4ddae2004-08-31 10:07:13 +0000179 self.check_suite(
180 "from sys.path import (dirname, basename as my_basename)")
181 self.check_suite(
182 "from sys.path import (dirname, basename as my_basename,)")
Benjamin Petersonc0747cf2008-11-03 20:31:38 +0000183 self.check_suite("from .bogus import x")
Fred Drakee3fb18c2001-01-07 06:02:19 +0000184
Fred Drake58422e52001-06-04 03:56:24 +0000185 def test_basic_import_statement(self):
186 self.check_suite("import sys")
187 self.check_suite("import sys as system")
188 self.check_suite("import sys, math")
189 self.check_suite("import sys as system, math")
190 self.check_suite("import sys, math as my_math")
Fred Drake79ca79d2000-08-21 22:30:53 +0000191
Mark Dickinson2cc8a5e2010-07-04 18:11:51 +0000192 def test_relative_imports(self):
193 self.check_suite("from . import name")
194 self.check_suite("from .. import name")
195 self.check_suite("from .pkg import name")
196 self.check_suite("from ..pkg import name")
197
Neal Norwitz9caf9c02003-02-10 01:54:06 +0000198 def test_pep263(self):
199 self.check_suite("# -*- coding: iso-8859-1 -*-\n"
200 "pass\n")
201
202 def test_assert(self):
203 self.check_suite("assert alo < ahi and blo < bhi\n")
204
Benjamin Peterson4469d0c2008-11-30 22:46:23 +0000205 def test_with(self):
206 self.check_suite("with open('x'): pass\n")
207 self.check_suite("with open('x') as f: pass\n")
Georg Brandl0c315622009-05-25 21:10:36 +0000208 self.check_suite("with open('x') as f, open('y') as g: pass\n")
Benjamin Peterson4469d0c2008-11-30 22:46:23 +0000209
Georg Brandleee31162008-12-07 15:15:22 +0000210 def test_try_stmt(self):
211 self.check_suite("try: pass\nexcept: pass\n")
212 self.check_suite("try: pass\nfinally: pass\n")
213 self.check_suite("try: pass\nexcept A: pass\nfinally: pass\n")
214 self.check_suite("try: pass\nexcept A: pass\nexcept: pass\n"
215 "finally: pass\n")
216 self.check_suite("try: pass\nexcept: pass\nelse: pass\n")
217 self.check_suite("try: pass\nexcept: pass\nelse: pass\n"
218 "finally: pass\n")
219
Thomas Wouters89f507f2006-12-13 04:49:30 +0000220 def test_position(self):
221 # An absolutely minimal test of position information. Better
222 # tests would be a big project.
Benjamin Peterson8f326b22009-12-13 02:10:36 +0000223 code = "def f(x):\n return x + 1"
Thomas Wouters89f507f2006-12-13 04:49:30 +0000224 st1 = parser.suite(code)
225 st2 = st1.totuple(line_info=1, col_info=1)
226
227 def walk(tree):
228 node_type = tree[0]
229 next = tree[1]
230 if isinstance(next, tuple):
231 for elt in tree[1:]:
232 for x in walk(elt):
233 yield x
234 else:
235 yield tree
236
237 terminals = list(walk(st2))
238 self.assertEqual([
239 (1, 'def', 1, 0),
240 (1, 'f', 1, 4),
241 (7, '(', 1, 5),
242 (1, 'x', 1, 6),
243 (8, ')', 1, 7),
244 (11, ':', 1, 8),
245 (4, '', 1, 9),
246 (5, '', 2, -1),
247 (1, 'return', 2, 4),
248 (1, 'x', 2, 11),
249 (14, '+', 2, 13),
250 (2, '1', 2, 15),
251 (4, '', 2, 16),
Benjamin Peterson8f326b22009-12-13 02:10:36 +0000252 (6, '', 2, -1),
253 (4, '', 2, -1),
254 (0, '', 2, -1)],
Thomas Wouters89f507f2006-12-13 04:49:30 +0000255 terminals)
256
Benjamin Peterson4905e802009-09-27 02:43:28 +0000257 def test_extended_unpacking(self):
258 self.check_suite("*a = y")
259 self.check_suite("x, *b, = m")
260 self.check_suite("[*a, *b] = y")
261 self.check_suite("for [*x, b] in x: pass")
262
Thomas Wouters89f507f2006-12-13 04:49:30 +0000263
Fred Drake79ca79d2000-08-21 22:30:53 +0000264#
265# Second, we take *invalid* trees and make sure we get ParserError
266# rejections for them.
267#
268
Fred Drake58422e52001-06-04 03:56:24 +0000269class IllegalSyntaxTestCase(unittest.TestCase):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000270
Fred Drake58422e52001-06-04 03:56:24 +0000271 def check_bad_tree(self, tree, label):
272 try:
Fred Drake6e4f2c02001-07-17 19:33:25 +0000273 parser.sequence2st(tree)
Fred Drake58422e52001-06-04 03:56:24 +0000274 except parser.ParserError:
275 pass
276 else:
277 self.fail("did not detect invalid tree for %r" % label)
Fred Drake79ca79d2000-08-21 22:30:53 +0000278
Fred Drake58422e52001-06-04 03:56:24 +0000279 def test_junk(self):
280 # not even remotely valid:
281 self.check_bad_tree((1, 2, 3), "<junk>")
282
Fred Drakecf580c72001-07-17 03:01:29 +0000283 def test_illegal_yield_1(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000284 # Illegal yield statement: def f(): return 1; yield 1
Fred Drakecf580c72001-07-17 03:01:29 +0000285 tree = \
286 (257,
287 (264,
288 (285,
289 (259,
290 (1, 'def'),
291 (1, 'f'),
292 (260, (7, '('), (8, ')')),
293 (11, ':'),
294 (291,
295 (4, ''),
296 (5, ''),
297 (264,
298 (265,
299 (266,
300 (272,
301 (275,
302 (1, 'return'),
303 (313,
304 (292,
305 (293,
306 (294,
307 (295,
308 (297,
309 (298,
310 (299,
311 (300,
312 (301,
313 (302, (303, (304, (305, (2, '1')))))))))))))))))),
314 (264,
315 (265,
316 (266,
317 (272,
318 (276,
319 (1, 'yield'),
320 (313,
321 (292,
322 (293,
323 (294,
324 (295,
325 (297,
326 (298,
327 (299,
328 (300,
329 (301,
330 (302,
331 (303, (304, (305, (2, '1')))))))))))))))))),
332 (4, ''))),
333 (6, ''))))),
334 (4, ''),
335 (0, ''))))
336 self.check_bad_tree(tree, "def f():\n return 1\n yield 1")
337
338 def test_illegal_yield_2(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000339 # Illegal return in generator: def f(): return 1; yield 1
Fred Drakecf580c72001-07-17 03:01:29 +0000340 tree = \
341 (257,
342 (264,
343 (265,
344 (266,
345 (278,
346 (1, 'from'),
347 (281, (1, '__future__')),
348 (1, 'import'),
349 (279, (1, 'generators')))),
350 (4, ''))),
351 (264,
352 (285,
353 (259,
354 (1, 'def'),
355 (1, 'f'),
356 (260, (7, '('), (8, ')')),
357 (11, ':'),
358 (291,
359 (4, ''),
360 (5, ''),
361 (264,
362 (265,
363 (266,
364 (272,
365 (275,
366 (1, 'return'),
367 (313,
368 (292,
369 (293,
370 (294,
371 (295,
372 (297,
373 (298,
374 (299,
375 (300,
376 (301,
377 (302, (303, (304, (305, (2, '1')))))))))))))))))),
378 (264,
379 (265,
380 (266,
381 (272,
382 (276,
383 (1, 'yield'),
384 (313,
385 (292,
386 (293,
387 (294,
388 (295,
389 (297,
390 (298,
391 (299,
392 (300,
393 (301,
394 (302,
395 (303, (304, (305, (2, '1')))))))))))))))))),
396 (4, ''))),
397 (6, ''))))),
398 (4, ''),
399 (0, ''))))
400 self.check_bad_tree(tree, "def f():\n return 1\n yield 1")
401
Fred Drake58422e52001-06-04 03:56:24 +0000402 def test_a_comma_comma_c(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000403 # Illegal input: a,,c
Fred Drake58422e52001-06-04 03:56:24 +0000404 tree = \
405 (258,
406 (311,
407 (290,
408 (291,
409 (292,
410 (293,
411 (295,
412 (296,
413 (297,
414 (298, (299, (300, (301, (302, (303, (1, 'a')))))))))))))),
415 (12, ','),
416 (12, ','),
417 (290,
418 (291,
419 (292,
420 (293,
421 (295,
422 (296,
423 (297,
424 (298, (299, (300, (301, (302, (303, (1, 'c'))))))))))))))),
425 (4, ''),
426 (0, ''))
427 self.check_bad_tree(tree, "a,,c")
428
429 def test_illegal_operator(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000430 # Illegal input: a $= b
Fred Drake58422e52001-06-04 03:56:24 +0000431 tree = \
432 (257,
433 (264,
434 (265,
435 (266,
436 (267,
437 (312,
438 (291,
439 (292,
440 (293,
441 (294,
442 (296,
443 (297,
444 (298,
445 (299,
446 (300, (301, (302, (303, (304, (1, 'a'))))))))))))))),
447 (268, (37, '$=')),
448 (312,
449 (291,
450 (292,
451 (293,
452 (294,
453 (296,
454 (297,
455 (298,
456 (299,
457 (300, (301, (302, (303, (304, (1, 'b'))))))))))))))))),
458 (4, ''))),
459 (0, ''))
460 self.check_bad_tree(tree, "a $= b")
Fred Drake79ca79d2000-08-21 22:30:53 +0000461
Neal Norwitz9caf9c02003-02-10 01:54:06 +0000462 def test_malformed_global(self):
463 #doesn't have global keyword in ast
464 tree = (257,
465 (264,
466 (265,
467 (266,
468 (282, (1, 'foo'))), (4, ''))),
469 (4, ''),
Tim Petersf2715e02003-02-19 02:35:07 +0000470 (0, ''))
Neal Norwitz9caf9c02003-02-10 01:54:06 +0000471 self.check_bad_tree(tree, "malformed global ast")
Fred Drake79ca79d2000-08-21 22:30:53 +0000472
Mark Dickinson2cc8a5e2010-07-04 18:11:51 +0000473 def test_missing_import_source(self):
474 # from import a
475 tree = \
476 (257,
477 (267,
478 (268,
479 (269,
480 (281,
481 (283, (1, 'from'), (1, 'import'),
482 (286, (284, (1, 'fred')))))),
483 (4, ''))),
484 (4, ''), (0, ''))
485 self.check_bad_tree(tree, "from import a")
486
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000487
488class CompileTestCase(unittest.TestCase):
489
490 # These tests are very minimal. :-(
491
492 def test_compile_expr(self):
493 st = parser.expr('2 + 3')
494 code = parser.compilest(st)
495 self.assertEquals(eval(code), 5)
496
497 def test_compile_suite(self):
498 st = parser.suite('x = 2; y = x + 3')
499 code = parser.compilest(st)
500 globs = {}
Georg Brandl7cae87c2006-09-06 06:51:57 +0000501 exec(code, globs)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000502 self.assertEquals(globs['y'], 5)
503
504 def test_compile_error(self):
505 st = parser.suite('1 = 3 + 4')
506 self.assertRaises(SyntaxError, parser.compilest, st)
507
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000508 def test_compile_badunicode(self):
Guido van Rossum7eb6ca52007-07-18 21:00:22 +0000509 st = parser.suite('a = "\\U12345678"')
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000510 self.assertRaises(SyntaxError, parser.compilest, st)
Guido van Rossum7eb6ca52007-07-18 21:00:22 +0000511 st = parser.suite('a = "\\u1"')
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000512 self.assertRaises(SyntaxError, parser.compilest, st)
513
Mark Dickinsond35a32e2010-06-17 12:33:22 +0000514 def test_issue_9011(self):
515 # Issue 9011: compilation of an unary minus expression changed
516 # the meaning of the ST, so that a second compilation produced
517 # incorrect results.
518 st = parser.expr('-3')
519 code1 = parser.compilest(st)
520 self.assertEqual(eval(code1), -3)
521 code2 = parser.compilest(st)
522 self.assertEqual(eval(code2), -3)
523
Christian Heimes90c3d9b2008-02-23 13:18:03 +0000524class ParserStackLimitTestCase(unittest.TestCase):
Mark Dickinsond35a32e2010-06-17 12:33:22 +0000525 """try to push the parser to/over its limits.
Christian Heimes90c3d9b2008-02-23 13:18:03 +0000526 see http://bugs.python.org/issue1881 for a discussion
527 """
528 def _nested_expression(self, level):
529 return "["*level+"]"*level
530
531 def test_deeply_nested_list(self):
532 # XXX used to be 99 levels in 2.x
533 e = self._nested_expression(93)
534 st = parser.expr(e)
535 st.compile()
536
537 def test_trigger_memory_error(self):
538 e = self._nested_expression(100)
Christian Heimesb186d002008-03-18 15:15:01 +0000539 print("Expecting 's_push: parser stack overflow' in next line",
540 file=sys.stderr)
Antoine Pitrou88909542009-06-29 13:54:42 +0000541 sys.stderr.flush()
Christian Heimes90c3d9b2008-02-23 13:18:03 +0000542 self.assertRaises(MemoryError, parser.expr, e)
543
Mark Dickinson211c6252009-02-01 10:28:51 +0000544class STObjectTestCase(unittest.TestCase):
545 """Test operations on ST objects themselves"""
546
547 def test_comparisons(self):
548 # ST objects should support order and equality comparisons
549 st1 = parser.expr('2 + 3')
550 st2 = parser.suite('x = 2; y = x + 3')
551 st3 = parser.expr('list(x**3 for x in range(20))')
552 st1_copy = parser.expr('2 + 3')
553 st2_copy = parser.suite('x = 2; y = x + 3')
554 st3_copy = parser.expr('list(x**3 for x in range(20))')
555
556 # exercise fast path for object identity
557 self.assertEquals(st1 == st1, True)
558 self.assertEquals(st2 == st2, True)
559 self.assertEquals(st3 == st3, True)
560 # slow path equality
561 self.assertEqual(st1, st1_copy)
562 self.assertEqual(st2, st2_copy)
563 self.assertEqual(st3, st3_copy)
564 self.assertEquals(st1 == st2, False)
565 self.assertEquals(st1 == st3, False)
566 self.assertEquals(st2 == st3, False)
567 self.assertEquals(st1 != st1, False)
568 self.assertEquals(st2 != st2, False)
569 self.assertEquals(st3 != st3, False)
570 self.assertEquals(st1 != st1_copy, False)
571 self.assertEquals(st2 != st2_copy, False)
572 self.assertEquals(st3 != st3_copy, False)
573 self.assertEquals(st2 != st1, True)
574 self.assertEquals(st1 != st3, True)
575 self.assertEquals(st3 != st2, True)
576 # we don't particularly care what the ordering is; just that
577 # it's usable and self-consistent
578 self.assertEquals(st1 < st2, not (st2 <= st1))
579 self.assertEquals(st1 < st3, not (st3 <= st1))
580 self.assertEquals(st2 < st3, not (st3 <= st2))
581 self.assertEquals(st1 < st2, st2 > st1)
582 self.assertEquals(st1 < st3, st3 > st1)
583 self.assertEquals(st2 < st3, st3 > st2)
584 self.assertEquals(st1 <= st2, st2 >= st1)
585 self.assertEquals(st3 <= st1, st1 >= st3)
586 self.assertEquals(st2 <= st3, st3 >= st2)
587 # transitivity
588 bottom = min(st1, st2, st3)
589 top = max(st1, st2, st3)
590 mid = sorted([st1, st2, st3])[1]
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000591 self.assertTrue(bottom < mid)
592 self.assertTrue(bottom < top)
593 self.assertTrue(mid < top)
594 self.assertTrue(bottom <= mid)
595 self.assertTrue(bottom <= top)
596 self.assertTrue(mid <= top)
597 self.assertTrue(bottom <= bottom)
598 self.assertTrue(mid <= mid)
599 self.assertTrue(top <= top)
Mark Dickinson211c6252009-02-01 10:28:51 +0000600 # interaction with other types
601 self.assertEquals(st1 == 1588.602459, False)
602 self.assertEquals('spanish armada' != st2, True)
603 self.assertRaises(TypeError, operator.ge, st3, None)
604 self.assertRaises(TypeError, operator.le, False, st1)
605 self.assertRaises(TypeError, operator.lt, st1, 1815)
606 self.assertRaises(TypeError, operator.gt, b'waterloo', st2)
607
608
609 # XXX tests for pickling and unpickling of ST objects should go here
610
611
Fred Drake2e2be372001-09-20 21:33:42 +0000612def test_main():
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000613 support.run_unittest(
Walter Dörwald21d3a322003-05-01 17:45:56 +0000614 RoundtripLegalSyntaxTestCase,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000615 IllegalSyntaxTestCase,
616 CompileTestCase,
Christian Heimes90c3d9b2008-02-23 13:18:03 +0000617 ParserStackLimitTestCase,
Mark Dickinson211c6252009-02-01 10:28:51 +0000618 STObjectTestCase,
Walter Dörwald21d3a322003-05-01 17:45:56 +0000619 )
Fred Drake2e2be372001-09-20 21:33:42 +0000620
621
622if __name__ == "__main__":
623 test_main()