blob: 0ac49da338ca7fec8c1aa6179f1dd7b0eec549b9 [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")
Tim Peterse8906822005-04-20 17:45:13 +0000153
Fred Drake58422e52001-06-04 03:56:24 +0000154 def test_import_from_statement(self):
155 self.check_suite("from sys.path import *")
156 self.check_suite("from sys.path import dirname")
Anthony Baxter1a4ddae2004-08-31 10:07:13 +0000157 self.check_suite("from sys.path import (dirname)")
158 self.check_suite("from sys.path import (dirname,)")
Fred Drake58422e52001-06-04 03:56:24 +0000159 self.check_suite("from sys.path import dirname as my_dirname")
Anthony Baxter1a4ddae2004-08-31 10:07:13 +0000160 self.check_suite("from sys.path import (dirname as my_dirname)")
161 self.check_suite("from sys.path import (dirname as my_dirname,)")
Fred Drake58422e52001-06-04 03:56:24 +0000162 self.check_suite("from sys.path import dirname, basename")
Anthony Baxter1a4ddae2004-08-31 10:07:13 +0000163 self.check_suite("from sys.path import (dirname, basename)")
164 self.check_suite("from sys.path import (dirname, basename,)")
Fred Drake58422e52001-06-04 03:56:24 +0000165 self.check_suite(
166 "from sys.path import dirname as my_dirname, basename")
167 self.check_suite(
Anthony Baxter1a4ddae2004-08-31 10:07:13 +0000168 "from sys.path import (dirname as my_dirname, basename)")
169 self.check_suite(
170 "from sys.path import (dirname as my_dirname, basename,)")
171 self.check_suite(
Fred Drake58422e52001-06-04 03:56:24 +0000172 "from sys.path import dirname, basename as my_basename")
Anthony Baxter1a4ddae2004-08-31 10:07:13 +0000173 self.check_suite(
174 "from sys.path import (dirname, basename as my_basename)")
175 self.check_suite(
176 "from sys.path import (dirname, basename as my_basename,)")
Benjamin Petersonc0747cf2008-11-03 20:31:38 +0000177 self.check_suite("from .bogus import x")
Fred Drakee3fb18c2001-01-07 06:02:19 +0000178
Fred Drake58422e52001-06-04 03:56:24 +0000179 def test_basic_import_statement(self):
180 self.check_suite("import sys")
181 self.check_suite("import sys as system")
182 self.check_suite("import sys, math")
183 self.check_suite("import sys as system, math")
184 self.check_suite("import sys, math as my_math")
Fred Drake79ca79d2000-08-21 22:30:53 +0000185
Neal Norwitz9caf9c02003-02-10 01:54:06 +0000186 def test_pep263(self):
187 self.check_suite("# -*- coding: iso-8859-1 -*-\n"
188 "pass\n")
189
190 def test_assert(self):
191 self.check_suite("assert alo < ahi and blo < bhi\n")
192
Benjamin Peterson4469d0c2008-11-30 22:46:23 +0000193 def test_with(self):
194 self.check_suite("with open('x'): pass\n")
195 self.check_suite("with open('x') as f: pass\n")
Georg Brandl0c315622009-05-25 21:10:36 +0000196 self.check_suite("with open('x') as f, open('y') as g: pass\n")
Benjamin Peterson4469d0c2008-11-30 22:46:23 +0000197
Georg Brandleee31162008-12-07 15:15:22 +0000198 def test_try_stmt(self):
199 self.check_suite("try: pass\nexcept: pass\n")
200 self.check_suite("try: pass\nfinally: pass\n")
201 self.check_suite("try: pass\nexcept A: pass\nfinally: pass\n")
202 self.check_suite("try: pass\nexcept A: pass\nexcept: pass\n"
203 "finally: pass\n")
204 self.check_suite("try: pass\nexcept: pass\nelse: pass\n")
205 self.check_suite("try: pass\nexcept: pass\nelse: pass\n"
206 "finally: pass\n")
207
Thomas Wouters89f507f2006-12-13 04:49:30 +0000208 def test_position(self):
209 # An absolutely minimal test of position information. Better
210 # tests would be a big project.
211 code = "def f(x):\n return x + 1\n"
212 st1 = parser.suite(code)
213 st2 = st1.totuple(line_info=1, col_info=1)
214
215 def walk(tree):
216 node_type = tree[0]
217 next = tree[1]
218 if isinstance(next, tuple):
219 for elt in tree[1:]:
220 for x in walk(elt):
221 yield x
222 else:
223 yield tree
224
225 terminals = list(walk(st2))
226 self.assertEqual([
227 (1, 'def', 1, 0),
228 (1, 'f', 1, 4),
229 (7, '(', 1, 5),
230 (1, 'x', 1, 6),
231 (8, ')', 1, 7),
232 (11, ':', 1, 8),
233 (4, '', 1, 9),
234 (5, '', 2, -1),
235 (1, 'return', 2, 4),
236 (1, 'x', 2, 11),
237 (14, '+', 2, 13),
238 (2, '1', 2, 15),
239 (4, '', 2, 16),
240 (6, '', 2, -1),
241 (4, '', 2, -1),
242 (0, '', 2, -1)],
243 terminals)
244
Benjamin Peterson4905e802009-09-27 02:43:28 +0000245 def test_extended_unpacking(self):
246 self.check_suite("*a = y")
247 self.check_suite("x, *b, = m")
248 self.check_suite("[*a, *b] = y")
249 self.check_suite("for [*x, b] in x: pass")
250
Thomas Wouters89f507f2006-12-13 04:49:30 +0000251
Fred Drake79ca79d2000-08-21 22:30:53 +0000252#
253# Second, we take *invalid* trees and make sure we get ParserError
254# rejections for them.
255#
256
Fred Drake58422e52001-06-04 03:56:24 +0000257class IllegalSyntaxTestCase(unittest.TestCase):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000258
Fred Drake58422e52001-06-04 03:56:24 +0000259 def check_bad_tree(self, tree, label):
260 try:
Fred Drake6e4f2c02001-07-17 19:33:25 +0000261 parser.sequence2st(tree)
Fred Drake58422e52001-06-04 03:56:24 +0000262 except parser.ParserError:
263 pass
264 else:
265 self.fail("did not detect invalid tree for %r" % label)
Fred Drake79ca79d2000-08-21 22:30:53 +0000266
Fred Drake58422e52001-06-04 03:56:24 +0000267 def test_junk(self):
268 # not even remotely valid:
269 self.check_bad_tree((1, 2, 3), "<junk>")
270
Fred Drakecf580c72001-07-17 03:01:29 +0000271 def test_illegal_yield_1(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000272 # Illegal yield statement: def f(): return 1; yield 1
Fred Drakecf580c72001-07-17 03:01:29 +0000273 tree = \
274 (257,
275 (264,
276 (285,
277 (259,
278 (1, 'def'),
279 (1, 'f'),
280 (260, (7, '('), (8, ')')),
281 (11, ':'),
282 (291,
283 (4, ''),
284 (5, ''),
285 (264,
286 (265,
287 (266,
288 (272,
289 (275,
290 (1, 'return'),
291 (313,
292 (292,
293 (293,
294 (294,
295 (295,
296 (297,
297 (298,
298 (299,
299 (300,
300 (301,
301 (302, (303, (304, (305, (2, '1')))))))))))))))))),
302 (264,
303 (265,
304 (266,
305 (272,
306 (276,
307 (1, 'yield'),
308 (313,
309 (292,
310 (293,
311 (294,
312 (295,
313 (297,
314 (298,
315 (299,
316 (300,
317 (301,
318 (302,
319 (303, (304, (305, (2, '1')))))))))))))))))),
320 (4, ''))),
321 (6, ''))))),
322 (4, ''),
323 (0, ''))))
324 self.check_bad_tree(tree, "def f():\n return 1\n yield 1")
325
326 def test_illegal_yield_2(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000327 # Illegal return in generator: def f(): return 1; yield 1
Fred Drakecf580c72001-07-17 03:01:29 +0000328 tree = \
329 (257,
330 (264,
331 (265,
332 (266,
333 (278,
334 (1, 'from'),
335 (281, (1, '__future__')),
336 (1, 'import'),
337 (279, (1, 'generators')))),
338 (4, ''))),
339 (264,
340 (285,
341 (259,
342 (1, 'def'),
343 (1, 'f'),
344 (260, (7, '('), (8, ')')),
345 (11, ':'),
346 (291,
347 (4, ''),
348 (5, ''),
349 (264,
350 (265,
351 (266,
352 (272,
353 (275,
354 (1, 'return'),
355 (313,
356 (292,
357 (293,
358 (294,
359 (295,
360 (297,
361 (298,
362 (299,
363 (300,
364 (301,
365 (302, (303, (304, (305, (2, '1')))))))))))))))))),
366 (264,
367 (265,
368 (266,
369 (272,
370 (276,
371 (1, 'yield'),
372 (313,
373 (292,
374 (293,
375 (294,
376 (295,
377 (297,
378 (298,
379 (299,
380 (300,
381 (301,
382 (302,
383 (303, (304, (305, (2, '1')))))))))))))))))),
384 (4, ''))),
385 (6, ''))))),
386 (4, ''),
387 (0, ''))))
388 self.check_bad_tree(tree, "def f():\n return 1\n yield 1")
389
Fred Drake58422e52001-06-04 03:56:24 +0000390 def test_a_comma_comma_c(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000391 # Illegal input: a,,c
Fred Drake58422e52001-06-04 03:56:24 +0000392 tree = \
393 (258,
394 (311,
395 (290,
396 (291,
397 (292,
398 (293,
399 (295,
400 (296,
401 (297,
402 (298, (299, (300, (301, (302, (303, (1, 'a')))))))))))))),
403 (12, ','),
404 (12, ','),
405 (290,
406 (291,
407 (292,
408 (293,
409 (295,
410 (296,
411 (297,
412 (298, (299, (300, (301, (302, (303, (1, 'c'))))))))))))))),
413 (4, ''),
414 (0, ''))
415 self.check_bad_tree(tree, "a,,c")
416
417 def test_illegal_operator(self):
Guido van Rossum32c2ae72002-08-22 19:45:32 +0000418 # Illegal input: a $= b
Fred Drake58422e52001-06-04 03:56:24 +0000419 tree = \
420 (257,
421 (264,
422 (265,
423 (266,
424 (267,
425 (312,
426 (291,
427 (292,
428 (293,
429 (294,
430 (296,
431 (297,
432 (298,
433 (299,
434 (300, (301, (302, (303, (304, (1, 'a'))))))))))))))),
435 (268, (37, '$=')),
436 (312,
437 (291,
438 (292,
439 (293,
440 (294,
441 (296,
442 (297,
443 (298,
444 (299,
445 (300, (301, (302, (303, (304, (1, 'b'))))))))))))))))),
446 (4, ''))),
447 (0, ''))
448 self.check_bad_tree(tree, "a $= b")
Fred Drake79ca79d2000-08-21 22:30:53 +0000449
Neal Norwitz9caf9c02003-02-10 01:54:06 +0000450 def test_malformed_global(self):
451 #doesn't have global keyword in ast
452 tree = (257,
453 (264,
454 (265,
455 (266,
456 (282, (1, 'foo'))), (4, ''))),
457 (4, ''),
Tim Petersf2715e02003-02-19 02:35:07 +0000458 (0, ''))
Neal Norwitz9caf9c02003-02-10 01:54:06 +0000459 self.check_bad_tree(tree, "malformed global ast")
Fred Drake79ca79d2000-08-21 22:30:53 +0000460
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000461
462class CompileTestCase(unittest.TestCase):
463
464 # These tests are very minimal. :-(
465
466 def test_compile_expr(self):
467 st = parser.expr('2 + 3')
468 code = parser.compilest(st)
469 self.assertEquals(eval(code), 5)
470
471 def test_compile_suite(self):
472 st = parser.suite('x = 2; y = x + 3')
473 code = parser.compilest(st)
474 globs = {}
Georg Brandl7cae87c2006-09-06 06:51:57 +0000475 exec(code, globs)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000476 self.assertEquals(globs['y'], 5)
477
478 def test_compile_error(self):
479 st = parser.suite('1 = 3 + 4')
480 self.assertRaises(SyntaxError, parser.compilest, st)
481
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000482 def test_compile_badunicode(self):
Guido van Rossum7eb6ca52007-07-18 21:00:22 +0000483 st = parser.suite('a = "\\U12345678"')
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000484 self.assertRaises(SyntaxError, parser.compilest, st)
Guido van Rossum7eb6ca52007-07-18 21:00:22 +0000485 st = parser.suite('a = "\\u1"')
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000486 self.assertRaises(SyntaxError, parser.compilest, st)
487
Christian Heimes90c3d9b2008-02-23 13:18:03 +0000488class ParserStackLimitTestCase(unittest.TestCase):
489 """try to push the parser to/over it's limits.
490 see http://bugs.python.org/issue1881 for a discussion
491 """
492 def _nested_expression(self, level):
493 return "["*level+"]"*level
494
495 def test_deeply_nested_list(self):
496 # XXX used to be 99 levels in 2.x
497 e = self._nested_expression(93)
498 st = parser.expr(e)
499 st.compile()
500
501 def test_trigger_memory_error(self):
502 e = self._nested_expression(100)
Christian Heimesb186d002008-03-18 15:15:01 +0000503 print("Expecting 's_push: parser stack overflow' in next line",
504 file=sys.stderr)
Antoine Pitrou88909542009-06-29 13:54:42 +0000505 sys.stderr.flush()
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]
Benjamin Petersonc9c0f202009-06-30 23:06:06 +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()