blob: 071d3622df567429f34bdcc8a7181ec278a63f4c [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")
Tim Peterse8906822005-04-20 17:45:13 +0000152
Fred Drake58422e52001-06-04 03:56:24 +0000153 def test_import_from_statement(self):
154 self.check_suite("from sys.path import *")
155 self.check_suite("from sys.path import dirname")
Anthony Baxter1a4ddae2004-08-31 10:07:13 +0000156 self.check_suite("from sys.path import (dirname)")
157 self.check_suite("from sys.path import (dirname,)")
Fred Drake58422e52001-06-04 03:56:24 +0000158 self.check_suite("from sys.path import dirname as my_dirname")
Anthony Baxter1a4ddae2004-08-31 10:07:13 +0000159 self.check_suite("from sys.path import (dirname as my_dirname)")
160 self.check_suite("from sys.path import (dirname as my_dirname,)")
Fred Drake58422e52001-06-04 03:56:24 +0000161 self.check_suite("from sys.path import dirname, basename")
Anthony Baxter1a4ddae2004-08-31 10:07:13 +0000162 self.check_suite("from sys.path import (dirname, basename)")
163 self.check_suite("from sys.path import (dirname, basename,)")
Fred Drake58422e52001-06-04 03:56:24 +0000164 self.check_suite(
165 "from sys.path import dirname as my_dirname, basename")
166 self.check_suite(
Anthony Baxter1a4ddae2004-08-31 10:07:13 +0000167 "from sys.path import (dirname as my_dirname, basename)")
168 self.check_suite(
169 "from sys.path import (dirname as my_dirname, basename,)")
170 self.check_suite(
Fred Drake58422e52001-06-04 03:56:24 +0000171 "from sys.path import dirname, basename as my_basename")
Anthony Baxter1a4ddae2004-08-31 10:07:13 +0000172 self.check_suite(
173 "from sys.path import (dirname, basename as my_basename)")
174 self.check_suite(
175 "from sys.path import (dirname, basename as my_basename,)")
Benjamin Petersonc0747cf2008-11-03 20:31:38 +0000176 self.check_suite("from .bogus import x")
Fred Drakee3fb18c2001-01-07 06:02:19 +0000177
Fred Drake58422e52001-06-04 03:56:24 +0000178 def test_basic_import_statement(self):
179 self.check_suite("import sys")
180 self.check_suite("import sys as system")
181 self.check_suite("import sys, math")
182 self.check_suite("import sys as system, math")
183 self.check_suite("import sys, math as my_math")
Fred Drake79ca79d2000-08-21 22:30:53 +0000184
Neal Norwitz9caf9c02003-02-10 01:54:06 +0000185 def test_pep263(self):
186 self.check_suite("# -*- coding: iso-8859-1 -*-\n"
187 "pass\n")
188
189 def test_assert(self):
190 self.check_suite("assert alo < ahi and blo < bhi\n")
191
Benjamin Peterson4469d0c2008-11-30 22:46:23 +0000192 def test_with(self):
193 self.check_suite("with open('x'): pass\n")
194 self.check_suite("with open('x') as f: pass\n")
Georg Brandl0c315622009-05-25 21:10:36 +0000195 self.check_suite("with open('x') as f, open('y') as g: pass\n")
Benjamin Peterson4469d0c2008-11-30 22:46:23 +0000196
Georg Brandleee31162008-12-07 15:15:22 +0000197 def test_try_stmt(self):
198 self.check_suite("try: pass\nexcept: pass\n")
199 self.check_suite("try: pass\nfinally: pass\n")
200 self.check_suite("try: pass\nexcept A: pass\nfinally: pass\n")
201 self.check_suite("try: pass\nexcept A: pass\nexcept: pass\n"
202 "finally: pass\n")
203 self.check_suite("try: pass\nexcept: pass\nelse: pass\n")
204 self.check_suite("try: pass\nexcept: pass\nelse: pass\n"
205 "finally: pass\n")
206
Thomas Wouters89f507f2006-12-13 04:49:30 +0000207 def test_position(self):
208 # An absolutely minimal test of position information. Better
209 # tests would be a big project.
Benjamin Peterson8f326b22009-12-13 02:10:36 +0000210 code = "def f(x):\n return x + 1"
Thomas Wouters89f507f2006-12-13 04:49:30 +0000211 st1 = parser.suite(code)
212 st2 = st1.totuple(line_info=1, col_info=1)
213
214 def walk(tree):
215 node_type = tree[0]
216 next = tree[1]
217 if isinstance(next, tuple):
218 for elt in tree[1:]:
219 for x in walk(elt):
220 yield x
221 else:
222 yield tree
223
224 terminals = list(walk(st2))
225 self.assertEqual([
226 (1, 'def', 1, 0),
227 (1, 'f', 1, 4),
228 (7, '(', 1, 5),
229 (1, 'x', 1, 6),
230 (8, ')', 1, 7),
231 (11, ':', 1, 8),
232 (4, '', 1, 9),
233 (5, '', 2, -1),
234 (1, 'return', 2, 4),
235 (1, 'x', 2, 11),
236 (14, '+', 2, 13),
237 (2, '1', 2, 15),
238 (4, '', 2, 16),
Benjamin Peterson8f326b22009-12-13 02:10:36 +0000239 (6, '', 2, -1),
240 (4, '', 2, -1),
241 (0, '', 2, -1)],
Thomas Wouters89f507f2006-12-13 04:49:30 +0000242 terminals)
243
Benjamin Peterson4905e802009-09-27 02:43:28 +0000244 def test_extended_unpacking(self):
245 self.check_suite("*a = y")
246 self.check_suite("x, *b, = m")
247 self.check_suite("[*a, *b] = y")
248 self.check_suite("for [*x, b] in x: pass")
249
Thomas Wouters89f507f2006-12-13 04:49:30 +0000250
Fred Drake79ca79d2000-08-21 22:30:53 +0000251#
252# Second, we take *invalid* trees and make sure we get ParserError
253# rejections for them.
254#
255
Fred Drake58422e52001-06-04 03:56:24 +0000256class IllegalSyntaxTestCase(unittest.TestCase):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000257
Fred Drake58422e52001-06-04 03:56:24 +0000258 def check_bad_tree(self, tree, label):
259 try:
Fred Drake6e4f2c02001-07-17 19:33:25 +0000260 parser.sequence2st(tree)
Fred Drake58422e52001-06-04 03:56:24 +0000261 except parser.ParserError:
262 pass
263 else:
264 self.fail("did not detect invalid tree for %r" % label)
Fred Drake79ca79d2000-08-21 22:30:53 +0000265
Fred Drake58422e52001-06-04 03:56:24 +0000266 def test_junk(self):
267 # not even remotely valid:
268 self.check_bad_tree((1, 2, 3), "<junk>")
269
Fred Drakecf580c72001-07-17 03:01:29 +0000270 def test_illegal_yield_1(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000271 # Illegal yield statement: def f(): return 1; yield 1
Fred Drakecf580c72001-07-17 03:01:29 +0000272 tree = \
273 (257,
274 (264,
275 (285,
276 (259,
277 (1, 'def'),
278 (1, 'f'),
279 (260, (7, '('), (8, ')')),
280 (11, ':'),
281 (291,
282 (4, ''),
283 (5, ''),
284 (264,
285 (265,
286 (266,
287 (272,
288 (275,
289 (1, 'return'),
290 (313,
291 (292,
292 (293,
293 (294,
294 (295,
295 (297,
296 (298,
297 (299,
298 (300,
299 (301,
300 (302, (303, (304, (305, (2, '1')))))))))))))))))),
301 (264,
302 (265,
303 (266,
304 (272,
305 (276,
306 (1, 'yield'),
307 (313,
308 (292,
309 (293,
310 (294,
311 (295,
312 (297,
313 (298,
314 (299,
315 (300,
316 (301,
317 (302,
318 (303, (304, (305, (2, '1')))))))))))))))))),
319 (4, ''))),
320 (6, ''))))),
321 (4, ''),
322 (0, ''))))
323 self.check_bad_tree(tree, "def f():\n return 1\n yield 1")
324
325 def test_illegal_yield_2(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000326 # Illegal return in generator: def f(): return 1; yield 1
Fred Drakecf580c72001-07-17 03:01:29 +0000327 tree = \
328 (257,
329 (264,
330 (265,
331 (266,
332 (278,
333 (1, 'from'),
334 (281, (1, '__future__')),
335 (1, 'import'),
336 (279, (1, 'generators')))),
337 (4, ''))),
338 (264,
339 (285,
340 (259,
341 (1, 'def'),
342 (1, 'f'),
343 (260, (7, '('), (8, ')')),
344 (11, ':'),
345 (291,
346 (4, ''),
347 (5, ''),
348 (264,
349 (265,
350 (266,
351 (272,
352 (275,
353 (1, 'return'),
354 (313,
355 (292,
356 (293,
357 (294,
358 (295,
359 (297,
360 (298,
361 (299,
362 (300,
363 (301,
364 (302, (303, (304, (305, (2, '1')))))))))))))))))),
365 (264,
366 (265,
367 (266,
368 (272,
369 (276,
370 (1, 'yield'),
371 (313,
372 (292,
373 (293,
374 (294,
375 (295,
376 (297,
377 (298,
378 (299,
379 (300,
380 (301,
381 (302,
382 (303, (304, (305, (2, '1')))))))))))))))))),
383 (4, ''))),
384 (6, ''))))),
385 (4, ''),
386 (0, ''))))
387 self.check_bad_tree(tree, "def f():\n return 1\n yield 1")
388
Fred Drake58422e52001-06-04 03:56:24 +0000389 def test_a_comma_comma_c(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000390 # Illegal input: a,,c
Fred Drake58422e52001-06-04 03:56:24 +0000391 tree = \
392 (258,
393 (311,
394 (290,
395 (291,
396 (292,
397 (293,
398 (295,
399 (296,
400 (297,
401 (298, (299, (300, (301, (302, (303, (1, 'a')))))))))))))),
402 (12, ','),
403 (12, ','),
404 (290,
405 (291,
406 (292,
407 (293,
408 (295,
409 (296,
410 (297,
411 (298, (299, (300, (301, (302, (303, (1, 'c'))))))))))))))),
412 (4, ''),
413 (0, ''))
414 self.check_bad_tree(tree, "a,,c")
415
416 def test_illegal_operator(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000417 # Illegal input: a $= b
Fred Drake58422e52001-06-04 03:56:24 +0000418 tree = \
419 (257,
420 (264,
421 (265,
422 (266,
423 (267,
424 (312,
425 (291,
426 (292,
427 (293,
428 (294,
429 (296,
430 (297,
431 (298,
432 (299,
433 (300, (301, (302, (303, (304, (1, 'a'))))))))))))))),
434 (268, (37, '$=')),
435 (312,
436 (291,
437 (292,
438 (293,
439 (294,
440 (296,
441 (297,
442 (298,
443 (299,
444 (300, (301, (302, (303, (304, (1, 'b'))))))))))))))))),
445 (4, ''))),
446 (0, ''))
447 self.check_bad_tree(tree, "a $= b")
Fred Drake79ca79d2000-08-21 22:30:53 +0000448
Neal Norwitz9caf9c02003-02-10 01:54:06 +0000449 def test_malformed_global(self):
450 #doesn't have global keyword in ast
451 tree = (257,
452 (264,
453 (265,
454 (266,
455 (282, (1, 'foo'))), (4, ''))),
456 (4, ''),
Tim Petersf2715e02003-02-19 02:35:07 +0000457 (0, ''))
Neal Norwitz9caf9c02003-02-10 01:54:06 +0000458 self.check_bad_tree(tree, "malformed global ast")
Fred Drake79ca79d2000-08-21 22:30:53 +0000459
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000460
461class CompileTestCase(unittest.TestCase):
462
463 # These tests are very minimal. :-(
464
465 def test_compile_expr(self):
466 st = parser.expr('2 + 3')
467 code = parser.compilest(st)
468 self.assertEquals(eval(code), 5)
469
470 def test_compile_suite(self):
471 st = parser.suite('x = 2; y = x + 3')
472 code = parser.compilest(st)
473 globs = {}
Georg Brandl7cae87c2006-09-06 06:51:57 +0000474 exec(code, globs)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000475 self.assertEquals(globs['y'], 5)
476
477 def test_compile_error(self):
478 st = parser.suite('1 = 3 + 4')
479 self.assertRaises(SyntaxError, parser.compilest, st)
480
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000481 def test_compile_badunicode(self):
Guido van Rossum7eb6ca52007-07-18 21:00:22 +0000482 st = parser.suite('a = "\\U12345678"')
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000483 self.assertRaises(SyntaxError, parser.compilest, st)
Guido van Rossum7eb6ca52007-07-18 21:00:22 +0000484 st = parser.suite('a = "\\u1"')
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000485 self.assertRaises(SyntaxError, parser.compilest, st)
486
Mark Dickinsond35a32e2010-06-17 12:33:22 +0000487 def test_issue_9011(self):
488 # Issue 9011: compilation of an unary minus expression changed
489 # the meaning of the ST, so that a second compilation produced
490 # incorrect results.
491 st = parser.expr('-3')
492 code1 = parser.compilest(st)
493 self.assertEqual(eval(code1), -3)
494 code2 = parser.compilest(st)
495 self.assertEqual(eval(code2), -3)
496
Christian Heimes90c3d9b2008-02-23 13:18:03 +0000497class ParserStackLimitTestCase(unittest.TestCase):
Mark Dickinsond35a32e2010-06-17 12:33:22 +0000498 """try to push the parser to/over its limits.
Christian Heimes90c3d9b2008-02-23 13:18:03 +0000499 see http://bugs.python.org/issue1881 for a discussion
500 """
501 def _nested_expression(self, level):
502 return "["*level+"]"*level
503
504 def test_deeply_nested_list(self):
505 # XXX used to be 99 levels in 2.x
506 e = self._nested_expression(93)
507 st = parser.expr(e)
508 st.compile()
509
510 def test_trigger_memory_error(self):
511 e = self._nested_expression(100)
Christian Heimesb186d002008-03-18 15:15:01 +0000512 print("Expecting 's_push: parser stack overflow' in next line",
513 file=sys.stderr)
Antoine Pitrou88909542009-06-29 13:54:42 +0000514 sys.stderr.flush()
Christian Heimes90c3d9b2008-02-23 13:18:03 +0000515 self.assertRaises(MemoryError, parser.expr, e)
516
Mark Dickinson211c6252009-02-01 10:28:51 +0000517class STObjectTestCase(unittest.TestCase):
518 """Test operations on ST objects themselves"""
519
520 def test_comparisons(self):
521 # ST objects should support order and equality comparisons
522 st1 = parser.expr('2 + 3')
523 st2 = parser.suite('x = 2; y = x + 3')
524 st3 = parser.expr('list(x**3 for x in range(20))')
525 st1_copy = parser.expr('2 + 3')
526 st2_copy = parser.suite('x = 2; y = x + 3')
527 st3_copy = parser.expr('list(x**3 for x in range(20))')
528
529 # exercise fast path for object identity
530 self.assertEquals(st1 == st1, True)
531 self.assertEquals(st2 == st2, True)
532 self.assertEquals(st3 == st3, True)
533 # slow path equality
534 self.assertEqual(st1, st1_copy)
535 self.assertEqual(st2, st2_copy)
536 self.assertEqual(st3, st3_copy)
537 self.assertEquals(st1 == st2, False)
538 self.assertEquals(st1 == st3, False)
539 self.assertEquals(st2 == st3, False)
540 self.assertEquals(st1 != st1, False)
541 self.assertEquals(st2 != st2, False)
542 self.assertEquals(st3 != st3, False)
543 self.assertEquals(st1 != st1_copy, False)
544 self.assertEquals(st2 != st2_copy, False)
545 self.assertEquals(st3 != st3_copy, False)
546 self.assertEquals(st2 != st1, True)
547 self.assertEquals(st1 != st3, True)
548 self.assertEquals(st3 != st2, True)
549 # we don't particularly care what the ordering is; just that
550 # it's usable and self-consistent
551 self.assertEquals(st1 < st2, not (st2 <= st1))
552 self.assertEquals(st1 < st3, not (st3 <= st1))
553 self.assertEquals(st2 < st3, not (st3 <= st2))
554 self.assertEquals(st1 < st2, st2 > st1)
555 self.assertEquals(st1 < st3, st3 > st1)
556 self.assertEquals(st2 < st3, st3 > st2)
557 self.assertEquals(st1 <= st2, st2 >= st1)
558 self.assertEquals(st3 <= st1, st1 >= st3)
559 self.assertEquals(st2 <= st3, st3 >= st2)
560 # transitivity
561 bottom = min(st1, st2, st3)
562 top = max(st1, st2, st3)
563 mid = sorted([st1, st2, st3])[1]
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000564 self.assertTrue(bottom < mid)
565 self.assertTrue(bottom < top)
566 self.assertTrue(mid < top)
567 self.assertTrue(bottom <= mid)
568 self.assertTrue(bottom <= top)
569 self.assertTrue(mid <= top)
570 self.assertTrue(bottom <= bottom)
571 self.assertTrue(mid <= mid)
572 self.assertTrue(top <= top)
Mark Dickinson211c6252009-02-01 10:28:51 +0000573 # interaction with other types
574 self.assertEquals(st1 == 1588.602459, False)
575 self.assertEquals('spanish armada' != st2, True)
576 self.assertRaises(TypeError, operator.ge, st3, None)
577 self.assertRaises(TypeError, operator.le, False, st1)
578 self.assertRaises(TypeError, operator.lt, st1, 1815)
579 self.assertRaises(TypeError, operator.gt, b'waterloo', st2)
580
581
582 # XXX tests for pickling and unpickling of ST objects should go here
583
584
Fred Drake2e2be372001-09-20 21:33:42 +0000585def test_main():
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000586 support.run_unittest(
Walter Dörwald21d3a322003-05-01 17:45:56 +0000587 RoundtripLegalSyntaxTestCase,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000588 IllegalSyntaxTestCase,
589 CompileTestCase,
Christian Heimes90c3d9b2008-02-23 13:18:03 +0000590 ParserStackLimitTestCase,
Mark Dickinson211c6252009-02-01 10:28:51 +0000591 STObjectTestCase,
Walter Dörwald21d3a322003-05-01 17:45:56 +0000592 )
Fred Drake2e2be372001-09-20 21:33:42 +0000593
594
595if __name__ == "__main__":
596 test_main()