blob: 17111b07ae75300393f899499083273bd559a9d1 [file] [log] [blame]
Fred Drake79ca79d2000-08-21 22:30:53 +00001import parser
Benjamin Petersonc0747cf2008-11-03 20:31:38 +00002import os
Fred Drake58422e52001-06-04 03:56:24 +00003import unittest
Christian Heimesb186d002008-03-18 15:15:01 +00004import sys
Mark Dickinson211c6252009-02-01 10:28:51 +00005import operator
Benjamin Petersonee8712c2008-05-20 21:35:26 +00006from test import support
Fred Drake79ca79d2000-08-21 22:30:53 +00007
8#
9# First, we test that we can generate trees from valid source fragments,
10# and that these valid trees are indeed allowed by the tree-loading side
11# of the parser module.
12#
13
Fred Drake58422e52001-06-04 03:56:24 +000014class RoundtripLegalSyntaxTestCase(unittest.TestCase):
Guido van Rossum32c2ae72002-08-22 19:45:32 +000015
Fred Drake58422e52001-06-04 03:56:24 +000016 def roundtrip(self, f, s):
17 st1 = f(s)
18 t = st1.totuple()
19 try:
Fred Drake6e4f2c02001-07-17 19:33:25 +000020 st2 = parser.sequence2st(t)
Guido van Rossumb940e112007-01-10 16:19:56 +000021 except parser.ParserError as why:
Anthony Baxterc2a5a632004-08-02 06:10:11 +000022 self.fail("could not roundtrip %r: %s" % (s, why))
Fred Drake79ca79d2000-08-21 22:30:53 +000023
Fred Drake58422e52001-06-04 03:56:24 +000024 self.assertEquals(t, st2.totuple(),
25 "could not re-generate syntax tree")
Fred Drake28f739a2000-08-25 22:42:40 +000026
Fred Drake58422e52001-06-04 03:56:24 +000027 def check_expr(self, s):
28 self.roundtrip(parser.expr, s)
Fred Drake28f739a2000-08-25 22:42:40 +000029
Benjamin Petersonf216c942008-10-31 02:28:05 +000030 def test_flags_passed(self):
31 # The unicode literals flags has to be passed from the paser to AST
32 # generation.
33 suite = parser.suite("from __future__ import unicode_literals; x = ''")
34 code = suite.compile()
35 scope = {}
36 exec(code, {}, scope)
37 self.assertTrue(isinstance(scope["x"], str))
38
Fred Drake58422e52001-06-04 03:56:24 +000039 def check_suite(self, s):
40 self.roundtrip(parser.suite, s)
Fred Drake28f739a2000-08-25 22:42:40 +000041
Fred Drakecf580c72001-07-17 03:01:29 +000042 def test_yield_statement(self):
Tim Peters496563a2002-04-01 00:28:59 +000043 self.check_suite("def f(): yield 1")
Phillip J. Eby0d6615f2005-08-02 00:46:46 +000044 self.check_suite("def f(): yield")
45 self.check_suite("def f(): x += yield")
46 self.check_suite("def f(): x = yield 1")
47 self.check_suite("def f(): x = y = yield 1")
48 self.check_suite("def f(): x = yield")
49 self.check_suite("def f(): x = y = yield")
50 self.check_suite("def f(): 1 + (yield)*2")
51 self.check_suite("def f(): (yield 1)*2")
Tim Peters496563a2002-04-01 00:28:59 +000052 self.check_suite("def f(): return; yield 1")
53 self.check_suite("def f(): yield 1; return")
54 self.check_suite("def f():\n"
Fred Drakecf580c72001-07-17 03:01:29 +000055 " for x in range(30):\n"
56 " yield x\n")
Phillip J. Eby0d6615f2005-08-02 00:46:46 +000057 self.check_suite("def f():\n"
58 " if (yield):\n"
59 " yield x\n")
Fred Drakecf580c72001-07-17 03:01:29 +000060
Fred Drake58422e52001-06-04 03:56:24 +000061 def test_expressions(self):
62 self.check_expr("foo(1)")
63 self.check_expr("[1, 2, 3]")
64 self.check_expr("[x**3 for x in range(20)]")
65 self.check_expr("[x**3 for x in range(20) if x % 3]")
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000066 self.check_expr("[x**3 for x in range(20) if x % 2 if x % 3]")
67 self.check_expr("list(x**3 for x in range(20))")
68 self.check_expr("list(x**3 for x in range(20) if x % 3)")
69 self.check_expr("list(x**3 for x in range(20) if x % 2 if x % 3)")
Fred Drake58422e52001-06-04 03:56:24 +000070 self.check_expr("foo(*args)")
71 self.check_expr("foo(*args, **kw)")
72 self.check_expr("foo(**kw)")
73 self.check_expr("foo(key=value)")
74 self.check_expr("foo(key=value, *args)")
75 self.check_expr("foo(key=value, *args, **kw)")
76 self.check_expr("foo(key=value, **kw)")
77 self.check_expr("foo(a, b, c, *args)")
78 self.check_expr("foo(a, b, c, *args, **kw)")
79 self.check_expr("foo(a, b, c, **kw)")
Benjamin Peterson3938a902008-08-20 02:33:00 +000080 self.check_expr("foo(a, *args, keyword=23)")
Fred Drake58422e52001-06-04 03:56:24 +000081 self.check_expr("foo + bar")
Michael W. Hudson5e83b7a2003-01-29 14:20:23 +000082 self.check_expr("foo - bar")
83 self.check_expr("foo * bar")
84 self.check_expr("foo / bar")
85 self.check_expr("foo // bar")
Fred Drake58422e52001-06-04 03:56:24 +000086 self.check_expr("lambda: 0")
87 self.check_expr("lambda x: 0")
88 self.check_expr("lambda *y: 0")
89 self.check_expr("lambda *y, **z: 0")
90 self.check_expr("lambda **z: 0")
91 self.check_expr("lambda x, y: 0")
92 self.check_expr("lambda foo=bar: 0")
93 self.check_expr("lambda foo=bar, spaz=nifty+spit: 0")
94 self.check_expr("lambda foo=bar, **z: 0")
95 self.check_expr("lambda foo=bar, blaz=blat+2, **z: 0")
96 self.check_expr("lambda foo=bar, blaz=blat+2, *y, **z: 0")
97 self.check_expr("lambda x, *y, **z: 0")
Raymond Hettinger354433a2004-05-19 08:20:33 +000098 self.check_expr("(x for x in range(10))")
99 self.check_expr("foo(x for x in range(10))")
Fred Drake79ca79d2000-08-21 22:30:53 +0000100
Fred Drake58422e52001-06-04 03:56:24 +0000101 def test_simple_expression(self):
102 # expr_stmt
103 self.check_suite("a")
Fred Drake79ca79d2000-08-21 22:30:53 +0000104
Fred Drake58422e52001-06-04 03:56:24 +0000105 def test_simple_assignments(self):
106 self.check_suite("a = b")
107 self.check_suite("a = b = c = d = e")
Fred Drake28f739a2000-08-25 22:42:40 +0000108
Fred Drake58422e52001-06-04 03:56:24 +0000109 def test_simple_augmented_assignments(self):
110 self.check_suite("a += b")
111 self.check_suite("a -= b")
112 self.check_suite("a *= b")
113 self.check_suite("a /= b")
Michael W. Hudson5e83b7a2003-01-29 14:20:23 +0000114 self.check_suite("a //= b")
Fred Drake58422e52001-06-04 03:56:24 +0000115 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")
121 self.check_suite("a **= b")
Fred Drakee3fb18c2001-01-07 06:02:19 +0000122
Fred Drake58422e52001-06-04 03:56:24 +0000123 def test_function_defs(self):
124 self.check_suite("def f(): pass")
125 self.check_suite("def f(*args): pass")
126 self.check_suite("def f(*args, **kw): pass")
127 self.check_suite("def f(**kw): pass")
128 self.check_suite("def f(foo=bar): pass")
129 self.check_suite("def f(foo=bar, *args): pass")
130 self.check_suite("def f(foo=bar, *args, **kw): pass")
131 self.check_suite("def f(foo=bar, **kw): pass")
Fred Drakee3fb18c2001-01-07 06:02:19 +0000132
Fred Drake58422e52001-06-04 03:56:24 +0000133 self.check_suite("def f(a, b): pass")
134 self.check_suite("def f(a, b, *args): pass")
135 self.check_suite("def f(a, b, *args, **kw): pass")
136 self.check_suite("def f(a, b, **kw): pass")
137 self.check_suite("def f(a, b, foo=bar): pass")
138 self.check_suite("def f(a, b, foo=bar, *args): pass")
139 self.check_suite("def f(a, b, foo=bar, *args, **kw): pass")
140 self.check_suite("def f(a, b, foo=bar, **kw): pass")
Fred Drakee3fb18c2001-01-07 06:02:19 +0000141
Anthony Baxterc2a5a632004-08-02 06:10:11 +0000142 self.check_suite("@staticmethod\n"
143 "def f(): pass")
144 self.check_suite("@staticmethod\n"
145 "@funcattrs(x, y)\n"
146 "def f(): pass")
147 self.check_suite("@funcattrs()\n"
148 "def f(): pass")
149
Brett Cannonf4189912005-04-09 02:30:16 +0000150 def test_class_defs(self):
151 self.check_suite("class foo():pass")
Guido van Rossumfc158e22007-11-15 19:17:28 +0000152 self.check_suite("class foo(object):pass")
Mark Dickinsona441e642010-07-04 16:39:03 +0000153 self.check_suite("@class_decorator\n"
154 "class foo():pass")
155 self.check_suite("@class_decorator(arg)\n"
156 "class foo():pass")
157 self.check_suite("@decorator1\n"
158 "@decorator2\n"
159 "class foo():pass")
Tim Peterse8906822005-04-20 17:45:13 +0000160
Fred Drake58422e52001-06-04 03:56:24 +0000161 def test_import_from_statement(self):
162 self.check_suite("from sys.path import *")
163 self.check_suite("from sys.path import dirname")
Anthony Baxter1a4ddae2004-08-31 10:07:13 +0000164 self.check_suite("from sys.path import (dirname)")
165 self.check_suite("from sys.path import (dirname,)")
Fred Drake58422e52001-06-04 03:56:24 +0000166 self.check_suite("from sys.path import dirname as my_dirname")
Anthony Baxter1a4ddae2004-08-31 10:07:13 +0000167 self.check_suite("from sys.path import (dirname as my_dirname)")
168 self.check_suite("from sys.path import (dirname as my_dirname,)")
Fred Drake58422e52001-06-04 03:56:24 +0000169 self.check_suite("from sys.path import dirname, basename")
Anthony Baxter1a4ddae2004-08-31 10:07:13 +0000170 self.check_suite("from sys.path import (dirname, basename)")
171 self.check_suite("from sys.path import (dirname, basename,)")
Fred Drake58422e52001-06-04 03:56:24 +0000172 self.check_suite(
173 "from sys.path import dirname as my_dirname, basename")
174 self.check_suite(
Anthony Baxter1a4ddae2004-08-31 10:07:13 +0000175 "from sys.path import (dirname as my_dirname, basename)")
176 self.check_suite(
177 "from sys.path import (dirname as my_dirname, basename,)")
178 self.check_suite(
Fred Drake58422e52001-06-04 03:56:24 +0000179 "from sys.path import dirname, basename as my_basename")
Anthony Baxter1a4ddae2004-08-31 10:07:13 +0000180 self.check_suite(
181 "from sys.path import (dirname, basename as my_basename)")
182 self.check_suite(
183 "from sys.path import (dirname, basename as my_basename,)")
Benjamin Petersonc0747cf2008-11-03 20:31:38 +0000184 self.check_suite("from .bogus import x")
Fred Drakee3fb18c2001-01-07 06:02:19 +0000185
Fred Drake58422e52001-06-04 03:56:24 +0000186 def test_basic_import_statement(self):
187 self.check_suite("import sys")
188 self.check_suite("import sys as system")
189 self.check_suite("import sys, math")
190 self.check_suite("import sys as system, math")
191 self.check_suite("import sys, math as my_math")
Fred Drake79ca79d2000-08-21 22:30:53 +0000192
Mark Dickinson1b9b5722010-07-04 18:16:43 +0000193 def test_relative_imports(self):
194 self.check_suite("from . import name")
195 self.check_suite("from .. import name")
196 self.check_suite("from .pkg import name")
197 self.check_suite("from ..pkg import name")
198
Neal Norwitz9caf9c02003-02-10 01:54:06 +0000199 def test_pep263(self):
200 self.check_suite("# -*- coding: iso-8859-1 -*-\n"
201 "pass\n")
202
203 def test_assert(self):
204 self.check_suite("assert alo < ahi and blo < bhi\n")
205
Benjamin Peterson4469d0c2008-11-30 22:46:23 +0000206 def test_with(self):
207 self.check_suite("with open('x'): pass\n")
208 self.check_suite("with open('x') as f: pass\n")
Georg Brandl0c315622009-05-25 21:10:36 +0000209 self.check_suite("with open('x') as f, open('y') as g: pass\n")
Benjamin Peterson4469d0c2008-11-30 22:46:23 +0000210
Georg Brandleee31162008-12-07 15:15:22 +0000211 def test_try_stmt(self):
212 self.check_suite("try: pass\nexcept: pass\n")
213 self.check_suite("try: pass\nfinally: pass\n")
214 self.check_suite("try: pass\nexcept A: pass\nfinally: pass\n")
215 self.check_suite("try: pass\nexcept A: pass\nexcept: pass\n"
216 "finally: pass\n")
217 self.check_suite("try: pass\nexcept: pass\nelse: pass\n")
218 self.check_suite("try: pass\nexcept: pass\nelse: pass\n"
219 "finally: pass\n")
220
Thomas Wouters89f507f2006-12-13 04:49:30 +0000221 def test_position(self):
222 # An absolutely minimal test of position information. Better
223 # tests would be a big project.
224 code = "def f(x):\n return x + 1\n"
225 st1 = parser.suite(code)
226 st2 = st1.totuple(line_info=1, col_info=1)
227
228 def walk(tree):
229 node_type = tree[0]
230 next = tree[1]
231 if isinstance(next, tuple):
232 for elt in tree[1:]:
233 for x in walk(elt):
234 yield x
235 else:
236 yield tree
237
238 terminals = list(walk(st2))
239 self.assertEqual([
240 (1, 'def', 1, 0),
241 (1, 'f', 1, 4),
242 (7, '(', 1, 5),
243 (1, 'x', 1, 6),
244 (8, ')', 1, 7),
245 (11, ':', 1, 8),
246 (4, '', 1, 9),
247 (5, '', 2, -1),
248 (1, 'return', 2, 4),
249 (1, 'x', 2, 11),
250 (14, '+', 2, 13),
251 (2, '1', 2, 15),
252 (4, '', 2, 16),
253 (6, '', 2, -1),
254 (4, '', 2, -1),
255 (0, '', 2, -1)],
256 terminals)
257
258
Fred Drake79ca79d2000-08-21 22:30:53 +0000259#
260# Second, we take *invalid* trees and make sure we get ParserError
261# rejections for them.
262#
263
Fred Drake58422e52001-06-04 03:56:24 +0000264class IllegalSyntaxTestCase(unittest.TestCase):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000265
Fred Drake58422e52001-06-04 03:56:24 +0000266 def check_bad_tree(self, tree, label):
267 try:
Fred Drake6e4f2c02001-07-17 19:33:25 +0000268 parser.sequence2st(tree)
Fred Drake58422e52001-06-04 03:56:24 +0000269 except parser.ParserError:
270 pass
271 else:
272 self.fail("did not detect invalid tree for %r" % label)
Fred Drake79ca79d2000-08-21 22:30:53 +0000273
Fred Drake58422e52001-06-04 03:56:24 +0000274 def test_junk(self):
275 # not even remotely valid:
276 self.check_bad_tree((1, 2, 3), "<junk>")
277
Fred Drakecf580c72001-07-17 03:01:29 +0000278 def test_illegal_yield_1(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000279 # Illegal yield statement: def f(): return 1; yield 1
Fred Drakecf580c72001-07-17 03:01:29 +0000280 tree = \
281 (257,
282 (264,
283 (285,
284 (259,
285 (1, 'def'),
286 (1, 'f'),
287 (260, (7, '('), (8, ')')),
288 (11, ':'),
289 (291,
290 (4, ''),
291 (5, ''),
292 (264,
293 (265,
294 (266,
295 (272,
296 (275,
297 (1, 'return'),
298 (313,
299 (292,
300 (293,
301 (294,
302 (295,
303 (297,
304 (298,
305 (299,
306 (300,
307 (301,
308 (302, (303, (304, (305, (2, '1')))))))))))))))))),
309 (264,
310 (265,
311 (266,
312 (272,
313 (276,
314 (1, 'yield'),
315 (313,
316 (292,
317 (293,
318 (294,
319 (295,
320 (297,
321 (298,
322 (299,
323 (300,
324 (301,
325 (302,
326 (303, (304, (305, (2, '1')))))))))))))))))),
327 (4, ''))),
328 (6, ''))))),
329 (4, ''),
330 (0, ''))))
331 self.check_bad_tree(tree, "def f():\n return 1\n yield 1")
332
333 def test_illegal_yield_2(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000334 # Illegal return in generator: def f(): return 1; yield 1
Fred Drakecf580c72001-07-17 03:01:29 +0000335 tree = \
336 (257,
337 (264,
338 (265,
339 (266,
340 (278,
341 (1, 'from'),
342 (281, (1, '__future__')),
343 (1, 'import'),
344 (279, (1, 'generators')))),
345 (4, ''))),
346 (264,
347 (285,
348 (259,
349 (1, 'def'),
350 (1, 'f'),
351 (260, (7, '('), (8, ')')),
352 (11, ':'),
353 (291,
354 (4, ''),
355 (5, ''),
356 (264,
357 (265,
358 (266,
359 (272,
360 (275,
361 (1, 'return'),
362 (313,
363 (292,
364 (293,
365 (294,
366 (295,
367 (297,
368 (298,
369 (299,
370 (300,
371 (301,
372 (302, (303, (304, (305, (2, '1')))))))))))))))))),
373 (264,
374 (265,
375 (266,
376 (272,
377 (276,
378 (1, 'yield'),
379 (313,
380 (292,
381 (293,
382 (294,
383 (295,
384 (297,
385 (298,
386 (299,
387 (300,
388 (301,
389 (302,
390 (303, (304, (305, (2, '1')))))))))))))))))),
391 (4, ''))),
392 (6, ''))))),
393 (4, ''),
394 (0, ''))))
395 self.check_bad_tree(tree, "def f():\n return 1\n yield 1")
396
Fred Drake58422e52001-06-04 03:56:24 +0000397 def test_a_comma_comma_c(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000398 # Illegal input: a,,c
Fred Drake58422e52001-06-04 03:56:24 +0000399 tree = \
400 (258,
401 (311,
402 (290,
403 (291,
404 (292,
405 (293,
406 (295,
407 (296,
408 (297,
409 (298, (299, (300, (301, (302, (303, (1, 'a')))))))))))))),
410 (12, ','),
411 (12, ','),
412 (290,
413 (291,
414 (292,
415 (293,
416 (295,
417 (296,
418 (297,
419 (298, (299, (300, (301, (302, (303, (1, 'c'))))))))))))))),
420 (4, ''),
421 (0, ''))
422 self.check_bad_tree(tree, "a,,c")
423
424 def test_illegal_operator(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000425 # Illegal input: a $= b
Fred Drake58422e52001-06-04 03:56:24 +0000426 tree = \
427 (257,
428 (264,
429 (265,
430 (266,
431 (267,
432 (312,
433 (291,
434 (292,
435 (293,
436 (294,
437 (296,
438 (297,
439 (298,
440 (299,
441 (300, (301, (302, (303, (304, (1, 'a'))))))))))))))),
442 (268, (37, '$=')),
443 (312,
444 (291,
445 (292,
446 (293,
447 (294,
448 (296,
449 (297,
450 (298,
451 (299,
452 (300, (301, (302, (303, (304, (1, 'b'))))))))))))))))),
453 (4, ''))),
454 (0, ''))
455 self.check_bad_tree(tree, "a $= b")
Fred Drake79ca79d2000-08-21 22:30:53 +0000456
Neal Norwitz9caf9c02003-02-10 01:54:06 +0000457 def test_malformed_global(self):
458 #doesn't have global keyword in ast
459 tree = (257,
460 (264,
461 (265,
462 (266,
463 (282, (1, 'foo'))), (4, ''))),
464 (4, ''),
Tim Petersf2715e02003-02-19 02:35:07 +0000465 (0, ''))
Neal Norwitz9caf9c02003-02-10 01:54:06 +0000466 self.check_bad_tree(tree, "malformed global ast")
Fred Drake79ca79d2000-08-21 22:30:53 +0000467
Mark Dickinson1b9b5722010-07-04 18:16:43 +0000468 def test_missing_import_source(self):
469 # from import fred
470 tree = \
471 (257,
472 (268,
473 (269,
474 (270,
475 (282,
476 (284, (1, 'from'), (1, 'import'),
477 (287, (285, (1, 'fred')))))),
478 (4, ''))),
479 (4, ''), (0, ''))
480 self.check_bad_tree(tree, "from import fred")
481
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000482
483class CompileTestCase(unittest.TestCase):
484
485 # These tests are very minimal. :-(
486
487 def test_compile_expr(self):
488 st = parser.expr('2 + 3')
489 code = parser.compilest(st)
490 self.assertEquals(eval(code), 5)
491
492 def test_compile_suite(self):
493 st = parser.suite('x = 2; y = x + 3')
494 code = parser.compilest(st)
495 globs = {}
Georg Brandl7cae87c2006-09-06 06:51:57 +0000496 exec(code, globs)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000497 self.assertEquals(globs['y'], 5)
498
499 def test_compile_error(self):
500 st = parser.suite('1 = 3 + 4')
501 self.assertRaises(SyntaxError, parser.compilest, st)
502
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000503 def test_compile_badunicode(self):
Guido van Rossum7eb6ca52007-07-18 21:00:22 +0000504 st = parser.suite('a = "\\U12345678"')
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000505 self.assertRaises(SyntaxError, parser.compilest, st)
Guido van Rossum7eb6ca52007-07-18 21:00:22 +0000506 st = parser.suite('a = "\\u1"')
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000507 self.assertRaises(SyntaxError, parser.compilest, st)
508
Christian Heimes90c3d9b2008-02-23 13:18:03 +0000509class ParserStackLimitTestCase(unittest.TestCase):
Mark Dickinsona58eed92010-06-17 12:37:17 +0000510 """try to push the parser to/over its limits.
Christian Heimes90c3d9b2008-02-23 13:18:03 +0000511 see http://bugs.python.org/issue1881 for a discussion
512 """
513 def _nested_expression(self, level):
514 return "["*level+"]"*level
515
516 def test_deeply_nested_list(self):
517 # XXX used to be 99 levels in 2.x
518 e = self._nested_expression(93)
519 st = parser.expr(e)
520 st.compile()
521
522 def test_trigger_memory_error(self):
523 e = self._nested_expression(100)
Christian Heimesb186d002008-03-18 15:15:01 +0000524 print("Expecting 's_push: parser stack overflow' in next line",
525 file=sys.stderr)
Christian Heimes90c3d9b2008-02-23 13:18:03 +0000526 self.assertRaises(MemoryError, parser.expr, e)
527
Mark Dickinson211c6252009-02-01 10:28:51 +0000528class STObjectTestCase(unittest.TestCase):
529 """Test operations on ST objects themselves"""
530
531 def test_comparisons(self):
532 # ST objects should support order and equality comparisons
533 st1 = parser.expr('2 + 3')
534 st2 = parser.suite('x = 2; y = x + 3')
535 st3 = parser.expr('list(x**3 for x in range(20))')
536 st1_copy = parser.expr('2 + 3')
537 st2_copy = parser.suite('x = 2; y = x + 3')
538 st3_copy = parser.expr('list(x**3 for x in range(20))')
539
540 # exercise fast path for object identity
541 self.assertEquals(st1 == st1, True)
542 self.assertEquals(st2 == st2, True)
543 self.assertEquals(st3 == st3, True)
544 # slow path equality
545 self.assertEqual(st1, st1_copy)
546 self.assertEqual(st2, st2_copy)
547 self.assertEqual(st3, st3_copy)
548 self.assertEquals(st1 == st2, False)
549 self.assertEquals(st1 == st3, False)
550 self.assertEquals(st2 == st3, False)
551 self.assertEquals(st1 != st1, False)
552 self.assertEquals(st2 != st2, False)
553 self.assertEquals(st3 != st3, False)
554 self.assertEquals(st1 != st1_copy, False)
555 self.assertEquals(st2 != st2_copy, False)
556 self.assertEquals(st3 != st3_copy, False)
557 self.assertEquals(st2 != st1, True)
558 self.assertEquals(st1 != st3, True)
559 self.assertEquals(st3 != st2, True)
560 # we don't particularly care what the ordering is; just that
561 # it's usable and self-consistent
562 self.assertEquals(st1 < st2, not (st2 <= st1))
563 self.assertEquals(st1 < st3, not (st3 <= st1))
564 self.assertEquals(st2 < st3, not (st3 <= st2))
565 self.assertEquals(st1 < st2, st2 > st1)
566 self.assertEquals(st1 < st3, st3 > st1)
567 self.assertEquals(st2 < st3, st3 > st2)
568 self.assertEquals(st1 <= st2, st2 >= st1)
569 self.assertEquals(st3 <= st1, st1 >= st3)
570 self.assertEquals(st2 <= st3, st3 >= st2)
571 # transitivity
572 bottom = min(st1, st2, st3)
573 top = max(st1, st2, st3)
574 mid = sorted([st1, st2, st3])[1]
Georg Brandlab91fde2009-08-13 08:51:18 +0000575 self.assertTrue(bottom < mid)
576 self.assertTrue(bottom < top)
577 self.assertTrue(mid < top)
578 self.assertTrue(bottom <= mid)
579 self.assertTrue(bottom <= top)
580 self.assertTrue(mid <= top)
581 self.assertTrue(bottom <= bottom)
582 self.assertTrue(mid <= mid)
583 self.assertTrue(top <= top)
Mark Dickinson211c6252009-02-01 10:28:51 +0000584 # interaction with other types
585 self.assertEquals(st1 == 1588.602459, False)
586 self.assertEquals('spanish armada' != st2, True)
587 self.assertRaises(TypeError, operator.ge, st3, None)
588 self.assertRaises(TypeError, operator.le, False, st1)
589 self.assertRaises(TypeError, operator.lt, st1, 1815)
590 self.assertRaises(TypeError, operator.gt, b'waterloo', st2)
591
592
593 # XXX tests for pickling and unpickling of ST objects should go here
594
595
Fred Drake2e2be372001-09-20 21:33:42 +0000596def test_main():
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000597 support.run_unittest(
Walter Dörwald21d3a322003-05-01 17:45:56 +0000598 RoundtripLegalSyntaxTestCase,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000599 IllegalSyntaxTestCase,
600 CompileTestCase,
Christian Heimes90c3d9b2008-02-23 13:18:03 +0000601 ParserStackLimitTestCase,
Mark Dickinson211c6252009-02-01 10:28:51 +0000602 STObjectTestCase,
Walter Dörwald21d3a322003-05-01 17:45:56 +0000603 )
Fred Drake2e2be372001-09-20 21:33:42 +0000604
605
606if __name__ == "__main__":
607 test_main()