blob: 3a7bbe95e4e36e74d057843472efa9fca164783b [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
Neal Norwitz9caf9c02003-02-10 01:54:06 +0000193 def test_pep263(self):
194 self.check_suite("# -*- coding: iso-8859-1 -*-\n"
195 "pass\n")
196
197 def test_assert(self):
198 self.check_suite("assert alo < ahi and blo < bhi\n")
199
Benjamin Peterson4469d0c2008-11-30 22:46:23 +0000200 def test_with(self):
201 self.check_suite("with open('x'): pass\n")
202 self.check_suite("with open('x') as f: pass\n")
Georg Brandl0c315622009-05-25 21:10:36 +0000203 self.check_suite("with open('x') as f, open('y') as g: pass\n")
Benjamin Peterson4469d0c2008-11-30 22:46:23 +0000204
Georg Brandleee31162008-12-07 15:15:22 +0000205 def test_try_stmt(self):
206 self.check_suite("try: pass\nexcept: pass\n")
207 self.check_suite("try: pass\nfinally: pass\n")
208 self.check_suite("try: pass\nexcept A: pass\nfinally: pass\n")
209 self.check_suite("try: pass\nexcept A: pass\nexcept: pass\n"
210 "finally: pass\n")
211 self.check_suite("try: pass\nexcept: pass\nelse: pass\n")
212 self.check_suite("try: pass\nexcept: pass\nelse: pass\n"
213 "finally: pass\n")
214
Thomas Wouters89f507f2006-12-13 04:49:30 +0000215 def test_position(self):
216 # An absolutely minimal test of position information. Better
217 # tests would be a big project.
218 code = "def f(x):\n return x + 1\n"
219 st1 = parser.suite(code)
220 st2 = st1.totuple(line_info=1, col_info=1)
221
222 def walk(tree):
223 node_type = tree[0]
224 next = tree[1]
225 if isinstance(next, tuple):
226 for elt in tree[1:]:
227 for x in walk(elt):
228 yield x
229 else:
230 yield tree
231
232 terminals = list(walk(st2))
233 self.assertEqual([
234 (1, 'def', 1, 0),
235 (1, 'f', 1, 4),
236 (7, '(', 1, 5),
237 (1, 'x', 1, 6),
238 (8, ')', 1, 7),
239 (11, ':', 1, 8),
240 (4, '', 1, 9),
241 (5, '', 2, -1),
242 (1, 'return', 2, 4),
243 (1, 'x', 2, 11),
244 (14, '+', 2, 13),
245 (2, '1', 2, 15),
246 (4, '', 2, 16),
247 (6, '', 2, -1),
248 (4, '', 2, -1),
249 (0, '', 2, -1)],
250 terminals)
251
252
Fred Drake79ca79d2000-08-21 22:30:53 +0000253#
254# Second, we take *invalid* trees and make sure we get ParserError
255# rejections for them.
256#
257
Fred Drake58422e52001-06-04 03:56:24 +0000258class IllegalSyntaxTestCase(unittest.TestCase):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000259
Fred Drake58422e52001-06-04 03:56:24 +0000260 def check_bad_tree(self, tree, label):
261 try:
Fred Drake6e4f2c02001-07-17 19:33:25 +0000262 parser.sequence2st(tree)
Fred Drake58422e52001-06-04 03:56:24 +0000263 except parser.ParserError:
264 pass
265 else:
266 self.fail("did not detect invalid tree for %r" % label)
Fred Drake79ca79d2000-08-21 22:30:53 +0000267
Fred Drake58422e52001-06-04 03:56:24 +0000268 def test_junk(self):
269 # not even remotely valid:
270 self.check_bad_tree((1, 2, 3), "<junk>")
271
Fred Drakecf580c72001-07-17 03:01:29 +0000272 def test_illegal_yield_1(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000273 # Illegal yield statement: def f(): return 1; yield 1
Fred Drakecf580c72001-07-17 03:01:29 +0000274 tree = \
275 (257,
276 (264,
277 (285,
278 (259,
279 (1, 'def'),
280 (1, 'f'),
281 (260, (7, '('), (8, ')')),
282 (11, ':'),
283 (291,
284 (4, ''),
285 (5, ''),
286 (264,
287 (265,
288 (266,
289 (272,
290 (275,
291 (1, 'return'),
292 (313,
293 (292,
294 (293,
295 (294,
296 (295,
297 (297,
298 (298,
299 (299,
300 (300,
301 (301,
302 (302, (303, (304, (305, (2, '1')))))))))))))))))),
303 (264,
304 (265,
305 (266,
306 (272,
307 (276,
308 (1, 'yield'),
309 (313,
310 (292,
311 (293,
312 (294,
313 (295,
314 (297,
315 (298,
316 (299,
317 (300,
318 (301,
319 (302,
320 (303, (304, (305, (2, '1')))))))))))))))))),
321 (4, ''))),
322 (6, ''))))),
323 (4, ''),
324 (0, ''))))
325 self.check_bad_tree(tree, "def f():\n return 1\n yield 1")
326
327 def test_illegal_yield_2(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000328 # Illegal return in generator: def f(): return 1; yield 1
Fred Drakecf580c72001-07-17 03:01:29 +0000329 tree = \
330 (257,
331 (264,
332 (265,
333 (266,
334 (278,
335 (1, 'from'),
336 (281, (1, '__future__')),
337 (1, 'import'),
338 (279, (1, 'generators')))),
339 (4, ''))),
340 (264,
341 (285,
342 (259,
343 (1, 'def'),
344 (1, 'f'),
345 (260, (7, '('), (8, ')')),
346 (11, ':'),
347 (291,
348 (4, ''),
349 (5, ''),
350 (264,
351 (265,
352 (266,
353 (272,
354 (275,
355 (1, 'return'),
356 (313,
357 (292,
358 (293,
359 (294,
360 (295,
361 (297,
362 (298,
363 (299,
364 (300,
365 (301,
366 (302, (303, (304, (305, (2, '1')))))))))))))))))),
367 (264,
368 (265,
369 (266,
370 (272,
371 (276,
372 (1, 'yield'),
373 (313,
374 (292,
375 (293,
376 (294,
377 (295,
378 (297,
379 (298,
380 (299,
381 (300,
382 (301,
383 (302,
384 (303, (304, (305, (2, '1')))))))))))))))))),
385 (4, ''))),
386 (6, ''))))),
387 (4, ''),
388 (0, ''))))
389 self.check_bad_tree(tree, "def f():\n return 1\n yield 1")
390
Fred Drake58422e52001-06-04 03:56:24 +0000391 def test_a_comma_comma_c(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000392 # Illegal input: a,,c
Fred Drake58422e52001-06-04 03:56:24 +0000393 tree = \
394 (258,
395 (311,
396 (290,
397 (291,
398 (292,
399 (293,
400 (295,
401 (296,
402 (297,
403 (298, (299, (300, (301, (302, (303, (1, 'a')))))))))))))),
404 (12, ','),
405 (12, ','),
406 (290,
407 (291,
408 (292,
409 (293,
410 (295,
411 (296,
412 (297,
413 (298, (299, (300, (301, (302, (303, (1, 'c'))))))))))))))),
414 (4, ''),
415 (0, ''))
416 self.check_bad_tree(tree, "a,,c")
417
418 def test_illegal_operator(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000419 # Illegal input: a $= b
Fred Drake58422e52001-06-04 03:56:24 +0000420 tree = \
421 (257,
422 (264,
423 (265,
424 (266,
425 (267,
426 (312,
427 (291,
428 (292,
429 (293,
430 (294,
431 (296,
432 (297,
433 (298,
434 (299,
435 (300, (301, (302, (303, (304, (1, 'a'))))))))))))))),
436 (268, (37, '$=')),
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, 'b'))))))))))))))))),
447 (4, ''))),
448 (0, ''))
449 self.check_bad_tree(tree, "a $= b")
Fred Drake79ca79d2000-08-21 22:30:53 +0000450
Neal Norwitz9caf9c02003-02-10 01:54:06 +0000451 def test_malformed_global(self):
452 #doesn't have global keyword in ast
453 tree = (257,
454 (264,
455 (265,
456 (266,
457 (282, (1, 'foo'))), (4, ''))),
458 (4, ''),
Tim Petersf2715e02003-02-19 02:35:07 +0000459 (0, ''))
Neal Norwitz9caf9c02003-02-10 01:54:06 +0000460 self.check_bad_tree(tree, "malformed global ast")
Fred Drake79ca79d2000-08-21 22:30:53 +0000461
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000462
463class CompileTestCase(unittest.TestCase):
464
465 # These tests are very minimal. :-(
466
467 def test_compile_expr(self):
468 st = parser.expr('2 + 3')
469 code = parser.compilest(st)
470 self.assertEquals(eval(code), 5)
471
472 def test_compile_suite(self):
473 st = parser.suite('x = 2; y = x + 3')
474 code = parser.compilest(st)
475 globs = {}
Georg Brandl7cae87c2006-09-06 06:51:57 +0000476 exec(code, globs)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000477 self.assertEquals(globs['y'], 5)
478
479 def test_compile_error(self):
480 st = parser.suite('1 = 3 + 4')
481 self.assertRaises(SyntaxError, parser.compilest, st)
482
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000483 def test_compile_badunicode(self):
Guido van Rossum7eb6ca52007-07-18 21:00:22 +0000484 st = parser.suite('a = "\\U12345678"')
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000485 self.assertRaises(SyntaxError, parser.compilest, st)
Guido van Rossum7eb6ca52007-07-18 21:00:22 +0000486 st = parser.suite('a = "\\u1"')
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000487 self.assertRaises(SyntaxError, parser.compilest, st)
488
Christian Heimes90c3d9b2008-02-23 13:18:03 +0000489class ParserStackLimitTestCase(unittest.TestCase):
Mark Dickinsona58eed92010-06-17 12:37:17 +0000490 """try to push the parser to/over its limits.
Christian Heimes90c3d9b2008-02-23 13:18:03 +0000491 see http://bugs.python.org/issue1881 for a discussion
492 """
493 def _nested_expression(self, level):
494 return "["*level+"]"*level
495
496 def test_deeply_nested_list(self):
497 # XXX used to be 99 levels in 2.x
498 e = self._nested_expression(93)
499 st = parser.expr(e)
500 st.compile()
501
502 def test_trigger_memory_error(self):
503 e = self._nested_expression(100)
Christian Heimesb186d002008-03-18 15:15:01 +0000504 print("Expecting 's_push: parser stack overflow' in next line",
505 file=sys.stderr)
Christian Heimes90c3d9b2008-02-23 13:18:03 +0000506 self.assertRaises(MemoryError, parser.expr, e)
507
Mark Dickinson211c6252009-02-01 10:28:51 +0000508class STObjectTestCase(unittest.TestCase):
509 """Test operations on ST objects themselves"""
510
511 def test_comparisons(self):
512 # ST objects should support order and equality comparisons
513 st1 = parser.expr('2 + 3')
514 st2 = parser.suite('x = 2; y = x + 3')
515 st3 = parser.expr('list(x**3 for x in range(20))')
516 st1_copy = parser.expr('2 + 3')
517 st2_copy = parser.suite('x = 2; y = x + 3')
518 st3_copy = parser.expr('list(x**3 for x in range(20))')
519
520 # exercise fast path for object identity
521 self.assertEquals(st1 == st1, True)
522 self.assertEquals(st2 == st2, True)
523 self.assertEquals(st3 == st3, True)
524 # slow path equality
525 self.assertEqual(st1, st1_copy)
526 self.assertEqual(st2, st2_copy)
527 self.assertEqual(st3, st3_copy)
528 self.assertEquals(st1 == st2, False)
529 self.assertEquals(st1 == st3, False)
530 self.assertEquals(st2 == st3, False)
531 self.assertEquals(st1 != st1, False)
532 self.assertEquals(st2 != st2, False)
533 self.assertEquals(st3 != st3, False)
534 self.assertEquals(st1 != st1_copy, False)
535 self.assertEquals(st2 != st2_copy, False)
536 self.assertEquals(st3 != st3_copy, False)
537 self.assertEquals(st2 != st1, True)
538 self.assertEquals(st1 != st3, True)
539 self.assertEquals(st3 != st2, True)
540 # we don't particularly care what the ordering is; just that
541 # it's usable and self-consistent
542 self.assertEquals(st1 < st2, not (st2 <= st1))
543 self.assertEquals(st1 < st3, not (st3 <= st1))
544 self.assertEquals(st2 < st3, not (st3 <= st2))
545 self.assertEquals(st1 < st2, st2 > st1)
546 self.assertEquals(st1 < st3, st3 > st1)
547 self.assertEquals(st2 < st3, st3 > st2)
548 self.assertEquals(st1 <= st2, st2 >= st1)
549 self.assertEquals(st3 <= st1, st1 >= st3)
550 self.assertEquals(st2 <= st3, st3 >= st2)
551 # transitivity
552 bottom = min(st1, st2, st3)
553 top = max(st1, st2, st3)
554 mid = sorted([st1, st2, st3])[1]
Georg Brandlab91fde2009-08-13 08:51:18 +0000555 self.assertTrue(bottom < mid)
556 self.assertTrue(bottom < top)
557 self.assertTrue(mid < top)
558 self.assertTrue(bottom <= mid)
559 self.assertTrue(bottom <= top)
560 self.assertTrue(mid <= top)
561 self.assertTrue(bottom <= bottom)
562 self.assertTrue(mid <= mid)
563 self.assertTrue(top <= top)
Mark Dickinson211c6252009-02-01 10:28:51 +0000564 # interaction with other types
565 self.assertEquals(st1 == 1588.602459, False)
566 self.assertEquals('spanish armada' != st2, True)
567 self.assertRaises(TypeError, operator.ge, st3, None)
568 self.assertRaises(TypeError, operator.le, False, st1)
569 self.assertRaises(TypeError, operator.lt, st1, 1815)
570 self.assertRaises(TypeError, operator.gt, b'waterloo', st2)
571
572
573 # XXX tests for pickling and unpickling of ST objects should go here
574
575
Fred Drake2e2be372001-09-20 21:33:42 +0000576def test_main():
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000577 support.run_unittest(
Walter Dörwald21d3a322003-05-01 17:45:56 +0000578 RoundtripLegalSyntaxTestCase,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000579 IllegalSyntaxTestCase,
580 CompileTestCase,
Christian Heimes90c3d9b2008-02-23 13:18:03 +0000581 ParserStackLimitTestCase,
Mark Dickinson211c6252009-02-01 10:28:51 +0000582 STObjectTestCase,
Walter Dörwald21d3a322003-05-01 17:45:56 +0000583 )
Fred Drake2e2be372001-09-20 21:33:42 +0000584
585
586if __name__ == "__main__":
587 test_main()