blob: 329b25807f665ba0500f9e069cd65ef51d1e2f11 [file] [log] [blame]
Guido van Rossum3bead091992-01-27 17:00:37 +00001# Python test set -- part 1, grammar.
2# This just tests whether the parser accepts them all.
3
Benjamin Petersonee8712c2008-05-20 21:35:26 +00004from test.support import run_unittest, check_syntax_error
Thomas Wouters89f507f2006-12-13 04:49:30 +00005import unittest
Jeremy Hylton7d3dff22001-10-10 01:45:02 +00006import sys
Thomas Wouters89f507f2006-12-13 04:49:30 +00007# testing import *
8from sys import *
Guido van Rossum3bead091992-01-27 17:00:37 +00009
Florent Xicluna9b86b9a2010-03-19 19:00:44 +000010
Thomas Wouters89f507f2006-12-13 04:49:30 +000011class TokenTests(unittest.TestCase):
Guido van Rossum3bead091992-01-27 17:00:37 +000012
Thomas Wouters89f507f2006-12-13 04:49:30 +000013 def testBackslash(self):
14 # Backslash means line continuation:
15 x = 1 \
16 + 1
Florent Xicluna9b86b9a2010-03-19 19:00:44 +000017 self.assertEqual(x, 2, 'backslash for line continuation')
Guido van Rossum3bead091992-01-27 17:00:37 +000018
Thomas Wouters89f507f2006-12-13 04:49:30 +000019 # Backslash does not means continuation in comments :\
20 x = 0
Florent Xicluna9b86b9a2010-03-19 19:00:44 +000021 self.assertEqual(x, 0, 'backslash ending comment')
Guido van Rossum3bead091992-01-27 17:00:37 +000022
Thomas Wouters89f507f2006-12-13 04:49:30 +000023 def testPlainIntegers(self):
Florent Xicluna9b86b9a2010-03-19 19:00:44 +000024 self.assertEqual(type(000), type(0))
25 self.assertEqual(0xff, 255)
26 self.assertEqual(0o377, 255)
27 self.assertEqual(2147483647, 0o17777777777)
28 self.assertEqual(0b1001, 9)
Georg Brandlfceab5a2008-01-19 20:08:23 +000029 # "0x" is not a valid literal
30 self.assertRaises(SyntaxError, eval, "0x")
Christian Heimesa37d4c62007-12-04 23:02:19 +000031 from sys import maxsize
32 if maxsize == 2147483647:
Florent Xicluna9b86b9a2010-03-19 19:00:44 +000033 self.assertEqual(-2147483647-1, -0o20000000000)
Thomas Wouters89f507f2006-12-13 04:49:30 +000034 # XXX -2147483648
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000035 self.assertTrue(0o37777777777 > 0)
36 self.assertTrue(0xffffffff > 0)
37 self.assertTrue(0b1111111111111111111111111111111 > 0)
Guido van Rossumcd16bf62007-06-13 18:07:49 +000038 for s in ('2147483648', '0o40000000000', '0x100000000',
39 '0b10000000000000000000000000000000'):
Thomas Wouters89f507f2006-12-13 04:49:30 +000040 try:
41 x = eval(s)
42 except OverflowError:
43 self.fail("OverflowError on huge integer literal %r" % s)
Christian Heimesa37d4c62007-12-04 23:02:19 +000044 elif maxsize == 9223372036854775807:
Florent Xicluna9b86b9a2010-03-19 19:00:44 +000045 self.assertEqual(-9223372036854775807-1, -0o1000000000000000000000)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000046 self.assertTrue(0o1777777777777777777777 > 0)
47 self.assertTrue(0xffffffffffffffff > 0)
48 self.assertTrue(0b11111111111111111111111111111111111111111111111111111111111111 > 0)
Guido van Rossumcd16bf62007-06-13 18:07:49 +000049 for s in '9223372036854775808', '0o2000000000000000000000', \
50 '0x10000000000000000', \
51 '0b100000000000000000000000000000000000000000000000000000000000000':
Thomas Wouters89f507f2006-12-13 04:49:30 +000052 try:
53 x = eval(s)
54 except OverflowError:
55 self.fail("OverflowError on huge integer literal %r" % s)
56 else:
Christian Heimesa37d4c62007-12-04 23:02:19 +000057 self.fail('Weird maxsize value %r' % maxsize)
Guido van Rossum3bead091992-01-27 17:00:37 +000058
Thomas Wouters89f507f2006-12-13 04:49:30 +000059 def testLongIntegers(self):
Guido van Rossume2a383d2007-01-15 16:59:06 +000060 x = 0
Guido van Rossume2a383d2007-01-15 16:59:06 +000061 x = 0xffffffffffffffff
Guido van Rossumcd16bf62007-06-13 18:07:49 +000062 x = 0Xffffffffffffffff
63 x = 0o77777777777777777
64 x = 0O77777777777777777
Guido van Rossume2a383d2007-01-15 16:59:06 +000065 x = 123456789012345678901234567890
Guido van Rossumcd16bf62007-06-13 18:07:49 +000066 x = 0b100000000000000000000000000000000000000000000000000000000000000000000
67 x = 0B111111111111111111111111111111111111111111111111111111111111111111111
Guido van Rossum3bead091992-01-27 17:00:37 +000068
Thomas Wouters89f507f2006-12-13 04:49:30 +000069 def testFloats(self):
70 x = 3.14
71 x = 314.
72 x = 0.314
73 # XXX x = 000.314
74 x = .314
75 x = 3e14
76 x = 3E14
77 x = 3e-14
78 x = 3e+14
79 x = 3.e14
80 x = .3e14
81 x = 3.1e4
Guido van Rossum3bead091992-01-27 17:00:37 +000082
Thomas Wouters89f507f2006-12-13 04:49:30 +000083 def testStringLiterals(self):
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000084 x = ''; y = ""; self.assertTrue(len(x) == 0 and x == y)
85 x = '\''; y = "'"; self.assertTrue(len(x) == 1 and x == y and ord(x) == 39)
86 x = '"'; y = "\""; self.assertTrue(len(x) == 1 and x == y and ord(x) == 34)
Thomas Wouters89f507f2006-12-13 04:49:30 +000087 x = "doesn't \"shrink\" does it"
88 y = 'doesn\'t "shrink" does it'
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000089 self.assertTrue(len(x) == 24 and x == y)
Thomas Wouters89f507f2006-12-13 04:49:30 +000090 x = "does \"shrink\" doesn't it"
91 y = 'does "shrink" doesn\'t it'
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000092 self.assertTrue(len(x) == 24 and x == y)
Thomas Wouters89f507f2006-12-13 04:49:30 +000093 x = """
Guido van Rossumb6775db1994-08-01 11:34:53 +000094The "quick"
95brown fox
96jumps over
97the 'lazy' dog.
98"""
Thomas Wouters89f507f2006-12-13 04:49:30 +000099 y = '\nThe "quick"\nbrown fox\njumps over\nthe \'lazy\' dog.\n'
Florent Xicluna9b86b9a2010-03-19 19:00:44 +0000100 self.assertEqual(x, y)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000101 y = '''
Guido van Rossumb6775db1994-08-01 11:34:53 +0000102The "quick"
103brown fox
104jumps over
105the 'lazy' dog.
Thomas Wouters89f507f2006-12-13 04:49:30 +0000106'''
Florent Xicluna9b86b9a2010-03-19 19:00:44 +0000107 self.assertEqual(x, y)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000108 y = "\n\
Guido van Rossumb6775db1994-08-01 11:34:53 +0000109The \"quick\"\n\
110brown fox\n\
111jumps over\n\
112the 'lazy' dog.\n\
Thomas Wouters89f507f2006-12-13 04:49:30 +0000113"
Florent Xicluna9b86b9a2010-03-19 19:00:44 +0000114 self.assertEqual(x, y)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000115 y = '\n\
Guido van Rossumb6775db1994-08-01 11:34:53 +0000116The \"quick\"\n\
117brown fox\n\
118jumps over\n\
119the \'lazy\' dog.\n\
Thomas Wouters89f507f2006-12-13 04:49:30 +0000120'
Florent Xicluna9b86b9a2010-03-19 19:00:44 +0000121 self.assertEqual(x, y)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000122
123 def testEllipsis(self):
124 x = ...
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000125 self.assertTrue(x is Ellipsis)
Georg Brandldde00282007-03-18 19:01:53 +0000126 self.assertRaises(SyntaxError, eval, ".. .")
Thomas Wouters89f507f2006-12-13 04:49:30 +0000127
128class GrammarTests(unittest.TestCase):
129
130 # single_input: NEWLINE | simple_stmt | compound_stmt NEWLINE
131 # XXX can't test in a script -- this rule is only used when interactive
132
133 # file_input: (NEWLINE | stmt)* ENDMARKER
134 # Being tested as this very moment this very module
135
136 # expr_input: testlist NEWLINE
137 # XXX Hard to test -- used only in calls to input()
138
139 def testEvalInput(self):
140 # testlist ENDMARKER
141 x = eval('1, 0 or 1')
142
143 def testFuncdef(self):
Neal Norwitzc1505362006-12-28 06:47:50 +0000144 ### [decorators] 'def' NAME parameters ['->' test] ':' suite
145 ### decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE
146 ### decorators: decorator+
147 ### parameters: '(' [typedargslist] ')'
148 ### typedargslist: ((tfpdef ['=' test] ',')*
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000149 ### ('*' [tfpdef] (',' tfpdef ['=' test])* [',' '**' tfpdef] | '**' tfpdef)
Neal Norwitzc1505362006-12-28 06:47:50 +0000150 ### | tfpdef ['=' test] (',' tfpdef ['=' test])* [','])
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000151 ### tfpdef: NAME [':' test]
Neal Norwitzc1505362006-12-28 06:47:50 +0000152 ### varargslist: ((vfpdef ['=' test] ',')*
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000153 ### ('*' [vfpdef] (',' vfpdef ['=' test])* [',' '**' vfpdef] | '**' vfpdef)
Neal Norwitzc1505362006-12-28 06:47:50 +0000154 ### | vfpdef ['=' test] (',' vfpdef ['=' test])* [','])
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000155 ### vfpdef: NAME
Thomas Wouters89f507f2006-12-13 04:49:30 +0000156 def f1(): pass
157 f1()
158 f1(*())
159 f1(*(), **{})
160 def f2(one_argument): pass
161 def f3(two, arguments): pass
Florent Xicluna9b86b9a2010-03-19 19:00:44 +0000162 self.assertEqual(f2.__code__.co_varnames, ('one_argument',))
163 self.assertEqual(f3.__code__.co_varnames, ('two', 'arguments'))
Thomas Wouters89f507f2006-12-13 04:49:30 +0000164 def a1(one_arg,): pass
165 def a2(two, args,): pass
166 def v0(*rest): pass
167 def v1(a, *rest): pass
168 def v2(a, b, *rest): pass
Thomas Wouters89f507f2006-12-13 04:49:30 +0000169
170 f1()
171 f2(1)
172 f2(1,)
173 f3(1, 2)
174 f3(1, 2,)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000175 v0()
176 v0(1)
177 v0(1,)
178 v0(1,2)
179 v0(1,2,3,4,5,6,7,8,9,0)
180 v1(1)
181 v1(1,)
182 v1(1,2)
183 v1(1,2,3)
184 v1(1,2,3,4,5,6,7,8,9,0)
185 v2(1,2)
186 v2(1,2,3)
187 v2(1,2,3,4)
188 v2(1,2,3,4,5,6,7,8,9,0)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000189
Thomas Wouters89f507f2006-12-13 04:49:30 +0000190 def d01(a=1): pass
191 d01()
192 d01(1)
193 d01(*(1,))
194 d01(**{'a':2})
195 def d11(a, b=1): pass
196 d11(1)
197 d11(1, 2)
198 d11(1, **{'b':2})
199 def d21(a, b, c=1): pass
200 d21(1, 2)
201 d21(1, 2, 3)
202 d21(*(1, 2, 3))
203 d21(1, *(2, 3))
204 d21(1, 2, *(3,))
205 d21(1, 2, **{'c':3})
206 def d02(a=1, b=2): pass
207 d02()
208 d02(1)
209 d02(1, 2)
210 d02(*(1, 2))
211 d02(1, *(2,))
212 d02(1, **{'b':2})
213 d02(**{'a': 1, 'b': 2})
214 def d12(a, b=1, c=2): pass
215 d12(1)
216 d12(1, 2)
217 d12(1, 2, 3)
218 def d22(a, b, c=1, d=2): pass
219 d22(1, 2)
220 d22(1, 2, 3)
221 d22(1, 2, 3, 4)
222 def d01v(a=1, *rest): pass
223 d01v()
224 d01v(1)
225 d01v(1, 2)
226 d01v(*(1, 2, 3, 4))
227 d01v(*(1,))
228 d01v(**{'a':2})
229 def d11v(a, b=1, *rest): pass
230 d11v(1)
231 d11v(1, 2)
232 d11v(1, 2, 3)
233 def d21v(a, b, c=1, *rest): pass
234 d21v(1, 2)
235 d21v(1, 2, 3)
236 d21v(1, 2, 3, 4)
237 d21v(*(1, 2, 3, 4))
238 d21v(1, 2, **{'c': 3})
239 def d02v(a=1, b=2, *rest): pass
240 d02v()
241 d02v(1)
242 d02v(1, 2)
243 d02v(1, 2, 3)
244 d02v(1, *(2, 3, 4))
245 d02v(**{'a': 1, 'b': 2})
246 def d12v(a, b=1, c=2, *rest): pass
247 d12v(1)
248 d12v(1, 2)
249 d12v(1, 2, 3)
250 d12v(1, 2, 3, 4)
251 d12v(*(1, 2, 3, 4))
252 d12v(1, 2, *(3, 4, 5))
253 d12v(1, *(2,), **{'c': 3})
254 def d22v(a, b, c=1, d=2, *rest): pass
255 d22v(1, 2)
256 d22v(1, 2, 3)
257 d22v(1, 2, 3, 4)
258 d22v(1, 2, 3, 4, 5)
259 d22v(*(1, 2, 3, 4))
260 d22v(1, 2, *(3, 4, 5))
261 d22v(1, *(2, 3), **{'d': 4})
Georg Brandld8b690f2008-05-16 17:28:50 +0000262
263 # keyword argument type tests
264 try:
265 str('x', **{b'foo':1 })
266 except TypeError:
267 pass
268 else:
269 self.fail('Bytes should not work as keyword argument names')
Thomas Wouters89f507f2006-12-13 04:49:30 +0000270 # keyword only argument tests
271 def pos0key1(*, key): return key
272 pos0key1(key=100)
273 def pos2key2(p1, p2, *, k1, k2=100): return p1,p2,k1,k2
274 pos2key2(1, 2, k1=100)
275 pos2key2(1, 2, k1=100, k2=200)
276 pos2key2(1, 2, k2=100, k1=200)
277 def pos2key2dict(p1, p2, *, k1=100, k2, **kwarg): return p1,p2,k1,k2,kwarg
278 pos2key2dict(1,2,k2=100,tokwarg1=100,tokwarg2=200)
279 pos2key2dict(1,2,tokwarg1=100,tokwarg2=200, k2=100)
280
Benjamin Peterson2d735bc2008-08-19 20:57:10 +0000281 # keyword arguments after *arglist
282 def f(*args, **kwargs):
283 return args, kwargs
Florent Xicluna9b86b9a2010-03-19 19:00:44 +0000284 self.assertEqual(f(1, x=2, *[3, 4], y=5), ((1, 3, 4),
Benjamin Peterson2d735bc2008-08-19 20:57:10 +0000285 {'x':2, 'y':5}))
286 self.assertRaises(SyntaxError, eval, "f(1, *(2,3), 4)")
287 self.assertRaises(SyntaxError, eval, "f(1, x=2, *(3,4), x=5)")
288
Neal Norwitzc1505362006-12-28 06:47:50 +0000289 # argument annotation tests
290 def f(x) -> list: pass
Florent Xicluna9b86b9a2010-03-19 19:00:44 +0000291 self.assertEqual(f.__annotations__, {'return': list})
Neal Norwitzc1505362006-12-28 06:47:50 +0000292 def f(x:int): pass
Florent Xicluna9b86b9a2010-03-19 19:00:44 +0000293 self.assertEqual(f.__annotations__, {'x': int})
Neal Norwitzc1505362006-12-28 06:47:50 +0000294 def f(*x:str): pass
Florent Xicluna9b86b9a2010-03-19 19:00:44 +0000295 self.assertEqual(f.__annotations__, {'x': str})
Neal Norwitzc1505362006-12-28 06:47:50 +0000296 def f(**x:float): pass
Florent Xicluna9b86b9a2010-03-19 19:00:44 +0000297 self.assertEqual(f.__annotations__, {'x': float})
Neal Norwitzc1505362006-12-28 06:47:50 +0000298 def f(x, y:1+2): pass
Florent Xicluna9b86b9a2010-03-19 19:00:44 +0000299 self.assertEqual(f.__annotations__, {'y': 3})
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000300 def f(a, b:1, c:2, d): pass
Florent Xicluna9b86b9a2010-03-19 19:00:44 +0000301 self.assertEqual(f.__annotations__, {'b': 1, 'c': 2})
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000302 def f(a, b:1, c:2, d, e:3=4, f=5, *g:6): pass
Florent Xicluna9b86b9a2010-03-19 19:00:44 +0000303 self.assertEqual(f.__annotations__,
Neal Norwitzc1505362006-12-28 06:47:50 +0000304 {'b': 1, 'c': 2, 'e': 3, 'g': 6})
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000305 def f(a, b:1, c:2, d, e:3=4, f=5, *g:6, h:7, i=8, j:9=10,
Neal Norwitzc1505362006-12-28 06:47:50 +0000306 **k:11) -> 12: pass
Florent Xicluna9b86b9a2010-03-19 19:00:44 +0000307 self.assertEqual(f.__annotations__,
Neal Norwitzc1505362006-12-28 06:47:50 +0000308 {'b': 1, 'c': 2, 'e': 3, 'g': 6, 'h': 7, 'j': 9,
309 'k': 11, 'return': 12})
Nick Coghlan71011e22007-04-23 11:05:01 +0000310 # Check for SF Bug #1697248 - mixing decorators and a return annotation
311 def null(x): return x
312 @null
313 def f(x) -> list: pass
Florent Xicluna9b86b9a2010-03-19 19:00:44 +0000314 self.assertEqual(f.__annotations__, {'return': list})
Nick Coghlan71011e22007-04-23 11:05:01 +0000315
Guido van Rossum0240b922007-02-26 21:23:50 +0000316 # test MAKE_CLOSURE with a variety of oparg's
317 closure = 1
318 def f(): return closure
319 def f(x=1): return closure
320 def f(*, k=1): return closure
321 def f() -> int: return closure
Neal Norwitzc1505362006-12-28 06:47:50 +0000322
Christian Heimesdd15f6c2008-03-16 00:07:10 +0000323 # Check ast errors in *args and *kwargs
324 check_syntax_error(self, "f(*g(1=2))")
325 check_syntax_error(self, "f(**g(1=2))")
326
Thomas Wouters89f507f2006-12-13 04:49:30 +0000327 def testLambdef(self):
328 ### lambdef: 'lambda' [varargslist] ':' test
329 l1 = lambda : 0
Florent Xicluna9b86b9a2010-03-19 19:00:44 +0000330 self.assertEqual(l1(), 0)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000331 l2 = lambda : a[d] # XXX just testing the expression
Guido van Rossume2a383d2007-01-15 16:59:06 +0000332 l3 = lambda : [2 < x for x in [-1, 3, 0]]
Florent Xicluna9b86b9a2010-03-19 19:00:44 +0000333 self.assertEqual(l3(), [0, 1, 0])
Thomas Wouters89f507f2006-12-13 04:49:30 +0000334 l4 = lambda x = lambda y = lambda z=1 : z : y() : x()
Florent Xicluna9b86b9a2010-03-19 19:00:44 +0000335 self.assertEqual(l4(), 1)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000336 l5 = lambda x, y, z=2: x + y + z
Florent Xicluna9b86b9a2010-03-19 19:00:44 +0000337 self.assertEqual(l5(1, 2), 5)
338 self.assertEqual(l5(1, 2, 3), 6)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000339 check_syntax_error(self, "lambda x: x = 2")
Amaury Forgeot d'Arc35c86582008-06-17 21:11:29 +0000340 check_syntax_error(self, "lambda (None,): None")
Thomas Wouters89f507f2006-12-13 04:49:30 +0000341 l6 = lambda x, y, *, k=20: x+y+k
Florent Xicluna9b86b9a2010-03-19 19:00:44 +0000342 self.assertEqual(l6(1,2), 1+2+20)
343 self.assertEqual(l6(1,2,k=10), 1+2+10)
Guido van Rossumb31c7f71993-11-11 10:31:23 +0000344
345
Thomas Wouters89f507f2006-12-13 04:49:30 +0000346 ### stmt: simple_stmt | compound_stmt
347 # Tested below
Georg Brandl52318d62006-09-06 07:06:08 +0000348
Thomas Wouters89f507f2006-12-13 04:49:30 +0000349 def testSimpleStmt(self):
350 ### simple_stmt: small_stmt (';' small_stmt)* [';']
351 x = 1; pass; del x
352 def foo():
Ezio Melotti13925002011-03-16 11:05:33 +0200353 # verify statements that end with semi-colons
Thomas Wouters89f507f2006-12-13 04:49:30 +0000354 x = 1; pass; del x;
355 foo()
Georg Brandl52318d62006-09-06 07:06:08 +0000356
Guido van Rossumd8faa362007-04-27 19:54:29 +0000357 ### small_stmt: expr_stmt | pass_stmt | del_stmt | flow_stmt | import_stmt | global_stmt | access_stmt
Thomas Wouters89f507f2006-12-13 04:49:30 +0000358 # Tested below
Georg Brandl52318d62006-09-06 07:06:08 +0000359
Thomas Wouters89f507f2006-12-13 04:49:30 +0000360 def testExprStmt(self):
361 # (exprlist '=')* exprlist
362 1
363 1, 2, 3
364 x = 1
365 x = 1, 2, 3
366 x = y = z = 1, 2, 3
367 x, y, z = 1, 2, 3
368 abc = a, b, c = x, y, z = xyz = 1, 2, (3, 4)
Guido van Rossum3bead091992-01-27 17:00:37 +0000369
Thomas Wouters89f507f2006-12-13 04:49:30 +0000370 check_syntax_error(self, "x + 1 = 1")
371 check_syntax_error(self, "a + 1 = b + 2")
Guido van Rossum3bead091992-01-27 17:00:37 +0000372
Thomas Wouters89f507f2006-12-13 04:49:30 +0000373 def testDelStmt(self):
374 # 'del' exprlist
375 abc = [1,2,3]
376 x, y, z = abc
377 xyz = x, y, z
Barry Warsaw7e3e1c12000-10-11 21:26:03 +0000378
Thomas Wouters89f507f2006-12-13 04:49:30 +0000379 del abc
380 del x, y, (z, xyz)
Barry Warsaw9182b452000-08-29 04:57:10 +0000381
Thomas Wouters89f507f2006-12-13 04:49:30 +0000382 def testPassStmt(self):
383 # 'pass'
384 pass
Barry Warsaw9182b452000-08-29 04:57:10 +0000385
Thomas Wouters89f507f2006-12-13 04:49:30 +0000386 # flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
387 # Tested below
Barry Warsaw9182b452000-08-29 04:57:10 +0000388
Thomas Wouters89f507f2006-12-13 04:49:30 +0000389 def testBreakStmt(self):
390 # 'break'
391 while 1: break
Barry Warsaw9182b452000-08-29 04:57:10 +0000392
Thomas Wouters89f507f2006-12-13 04:49:30 +0000393 def testContinueStmt(self):
394 # 'continue'
395 i = 1
396 while i: i = 0; continue
Barry Warsaw9182b452000-08-29 04:57:10 +0000397
Thomas Wouters89f507f2006-12-13 04:49:30 +0000398 msg = ""
399 while not msg:
400 msg = "ok"
401 try:
402 continue
403 msg = "continue failed to continue inside try"
404 except:
405 msg = "continue inside try called except block"
406 if msg != "ok":
407 self.fail(msg)
Barry Warsawefc92ee2000-08-21 15:46:50 +0000408
Thomas Wouters89f507f2006-12-13 04:49:30 +0000409 msg = ""
410 while not msg:
411 msg = "finally block not called"
412 try:
413 continue
414 finally:
415 msg = "ok"
416 if msg != "ok":
417 self.fail(msg)
Guido van Rossum3bead091992-01-27 17:00:37 +0000418
Thomas Wouters89f507f2006-12-13 04:49:30 +0000419 def test_break_continue_loop(self):
420 # This test warrants an explanation. It is a test specifically for SF bugs
421 # #463359 and #462937. The bug is that a 'break' statement executed or
422 # exception raised inside a try/except inside a loop, *after* a continue
423 # statement has been executed in that loop, will cause the wrong number of
424 # arguments to be popped off the stack and the instruction pointer reset to
425 # a very small number (usually 0.) Because of this, the following test
426 # *must* written as a function, and the tracking vars *must* be function
427 # arguments with default values. Otherwise, the test will loop and loop.
Guido van Rossum3bead091992-01-27 17:00:37 +0000428
Thomas Wouters89f507f2006-12-13 04:49:30 +0000429 def test_inner(extra_burning_oil = 1, count=0):
430 big_hippo = 2
431 while big_hippo:
432 count += 1
433 try:
434 if extra_burning_oil and big_hippo == 1:
435 extra_burning_oil -= 1
436 break
437 big_hippo -= 1
438 continue
439 except:
440 raise
441 if count > 2 or big_hippo != 1:
442 self.fail("continue then break in try/except in loop broken!")
443 test_inner()
Guido van Rossum3bead091992-01-27 17:00:37 +0000444
Thomas Wouters89f507f2006-12-13 04:49:30 +0000445 def testReturn(self):
446 # 'return' [testlist]
447 def g1(): return
448 def g2(): return 1
449 g1()
450 x = g2()
451 check_syntax_error(self, "class foo:return 1")
Guido van Rossum3bead091992-01-27 17:00:37 +0000452
Thomas Wouters89f507f2006-12-13 04:49:30 +0000453 def testYield(self):
454 check_syntax_error(self, "class foo:yield 1")
Guido van Rossum3bead091992-01-27 17:00:37 +0000455
Thomas Wouters89f507f2006-12-13 04:49:30 +0000456 def testRaise(self):
457 # 'raise' test [',' test]
Collin Winter828f04a2007-08-31 00:04:24 +0000458 try: raise RuntimeError('just testing')
Thomas Wouters89f507f2006-12-13 04:49:30 +0000459 except RuntimeError: pass
460 try: raise KeyboardInterrupt
461 except KeyboardInterrupt: pass
Jeremy Hylton3faa52e2001-02-01 22:48:12 +0000462
Thomas Wouters89f507f2006-12-13 04:49:30 +0000463 def testImport(self):
464 # 'import' dotted_as_names
465 import sys
466 import time, sys
467 # 'from' dotted_name 'import' ('*' | '(' import_as_names ')' | import_as_names)
468 from time import time
469 from time import (time)
470 # not testable inside a function, but already done at top of the module
471 # from sys import *
472 from sys import path, argv
473 from sys import (path, argv)
474 from sys import (path, argv,)
Tim Peters10fb3862001-02-09 20:17:14 +0000475
Thomas Wouters89f507f2006-12-13 04:49:30 +0000476 def testGlobal(self):
477 # 'global' NAME (',' NAME)*
478 global a
479 global a, b
480 global one, two, three, four, five, six, seven, eight, nine, ten
Thomas Wouters80d373c2001-09-26 12:43:39 +0000481
Benjamin Petersona933e522008-10-24 22:16:39 +0000482 def testNonlocal(self):
483 # 'nonlocal' NAME (',' NAME)*
484 x = 0
485 y = 0
486 def f():
487 nonlocal x
488 nonlocal x, y
489
Thomas Wouters89f507f2006-12-13 04:49:30 +0000490 def testAssert(self):
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000491 # assertTruestmt: 'assert' test [',' test]
Thomas Wouters89f507f2006-12-13 04:49:30 +0000492 assert 1
493 assert 1, 1
494 assert lambda x:x
495 assert 1, lambda x:x+1
Thomas Wouters80d373c2001-09-26 12:43:39 +0000496 try:
Thomas Wouters89f507f2006-12-13 04:49:30 +0000497 assert 0, "msg"
Guido van Rossumb940e112007-01-10 16:19:56 +0000498 except AssertionError as e:
Florent Xicluna9b86b9a2010-03-19 19:00:44 +0000499 self.assertEqual(e.args[0], "msg")
Thomas Wouters89f507f2006-12-13 04:49:30 +0000500 else:
501 if __debug__:
502 self.fail("AssertionError not raised by assert 0")
Thomas Wouters80d373c2001-09-26 12:43:39 +0000503
Thomas Wouters89f507f2006-12-13 04:49:30 +0000504 ### compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef
505 # Tested below
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000506
Thomas Wouters89f507f2006-12-13 04:49:30 +0000507 def testIf(self):
508 # 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
509 if 1: pass
510 if 1: pass
511 else: pass
512 if 0: pass
513 elif 0: pass
514 if 0: pass
515 elif 0: pass
516 elif 0: pass
517 elif 0: pass
518 else: pass
Guido van Rossum3bead091992-01-27 17:00:37 +0000519
Thomas Wouters89f507f2006-12-13 04:49:30 +0000520 def testWhile(self):
521 # 'while' test ':' suite ['else' ':' suite]
522 while 0: pass
523 while 0: pass
524 else: pass
Guido van Rossum3bead091992-01-27 17:00:37 +0000525
Christian Heimes969fe572008-01-25 11:23:10 +0000526 # Issue1920: "while 0" is optimized away,
527 # ensure that the "else" clause is still present.
528 x = 0
529 while 0:
530 x = 1
531 else:
532 x = 2
Florent Xicluna9b86b9a2010-03-19 19:00:44 +0000533 self.assertEqual(x, 2)
Christian Heimes969fe572008-01-25 11:23:10 +0000534
Thomas Wouters89f507f2006-12-13 04:49:30 +0000535 def testFor(self):
536 # 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite]
537 for i in 1, 2, 3: pass
538 for i, j, k in (): pass
539 else: pass
540 class Squares:
541 def __init__(self, max):
542 self.max = max
543 self.sofar = []
544 def __len__(self): return len(self.sofar)
545 def __getitem__(self, i):
546 if not 0 <= i < self.max: raise IndexError
547 n = len(self.sofar)
548 while n <= i:
549 self.sofar.append(n*n)
550 n = n+1
551 return self.sofar[i]
552 n = 0
553 for x in Squares(10): n = n+x
554 if n != 285:
555 self.fail('for over growing sequence')
Guido van Rossum3bead091992-01-27 17:00:37 +0000556
Thomas Wouters89f507f2006-12-13 04:49:30 +0000557 result = []
558 for x, in [(1,), (2,), (3,)]:
559 result.append(x)
560 self.assertEqual(result, [1, 2, 3])
Guido van Rossum3bead091992-01-27 17:00:37 +0000561
Thomas Wouters89f507f2006-12-13 04:49:30 +0000562 def testTry(self):
563 ### try_stmt: 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
564 ### | 'try' ':' suite 'finally' ':' suite
Guido van Rossumb940e112007-01-10 16:19:56 +0000565 ### except_clause: 'except' [expr ['as' expr]]
Thomas Wouters89f507f2006-12-13 04:49:30 +0000566 try:
567 1/0
568 except ZeroDivisionError:
569 pass
570 else:
571 pass
572 try: 1/0
573 except EOFError: pass
Guido van Rossumb940e112007-01-10 16:19:56 +0000574 except TypeError as msg: pass
575 except RuntimeError as msg: pass
Thomas Wouters89f507f2006-12-13 04:49:30 +0000576 except: pass
577 else: pass
578 try: 1/0
579 except (EOFError, TypeError, ZeroDivisionError): pass
580 try: 1/0
Guido van Rossumb940e112007-01-10 16:19:56 +0000581 except (EOFError, TypeError, ZeroDivisionError) as msg: pass
Thomas Wouters89f507f2006-12-13 04:49:30 +0000582 try: pass
583 finally: pass
Jeremy Hyltonf828e2d2001-02-19 15:54:52 +0000584
Thomas Wouters89f507f2006-12-13 04:49:30 +0000585 def testSuite(self):
586 # simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT
587 if 1: pass
588 if 1:
589 pass
590 if 1:
591 #
592 #
593 #
594 pass
595 pass
596 #
597 pass
598 #
Guido van Rossum3bead091992-01-27 17:00:37 +0000599
Thomas Wouters89f507f2006-12-13 04:49:30 +0000600 def testTest(self):
601 ### and_test ('or' and_test)*
602 ### and_test: not_test ('and' not_test)*
603 ### not_test: 'not' not_test | comparison
604 if not 1: pass
605 if 1 and 1: pass
606 if 1 or 1: pass
607 if not not not 1: pass
608 if not 1 and 1 and 1: pass
609 if 1 and 1 or 1 and 1 and 1 or not 1 and 1: pass
Guido van Rossum3bead091992-01-27 17:00:37 +0000610
Thomas Wouters89f507f2006-12-13 04:49:30 +0000611 def testComparison(self):
612 ### comparison: expr (comp_op expr)*
613 ### comp_op: '<'|'>'|'=='|'>='|'<='|'!='|'in'|'not' 'in'|'is'|'is' 'not'
614 if 1: pass
615 x = (1 == 1)
616 if 1 == 1: pass
617 if 1 != 1: pass
618 if 1 < 1: pass
619 if 1 > 1: pass
620 if 1 <= 1: pass
621 if 1 >= 1: pass
622 if 1 is 1: pass
623 if 1 is not 1: pass
624 if 1 in (): pass
625 if 1 not in (): pass
626 if 1 < 1 > 1 == 1 >= 1 <= 1 != 1 in 1 not in 1 is 1 is not 1: pass
Guido van Rossum3bead091992-01-27 17:00:37 +0000627
Thomas Wouters89f507f2006-12-13 04:49:30 +0000628 def testBinaryMaskOps(self):
629 x = 1 & 1
630 x = 1 ^ 1
631 x = 1 | 1
Guido van Rossum3bead091992-01-27 17:00:37 +0000632
Thomas Wouters89f507f2006-12-13 04:49:30 +0000633 def testShiftOps(self):
634 x = 1 << 1
635 x = 1 >> 1
636 x = 1 << 1 >> 1
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000637
Thomas Wouters89f507f2006-12-13 04:49:30 +0000638 def testAdditiveOps(self):
639 x = 1
640 x = 1 + 1
641 x = 1 - 1 - 1
642 x = 1 - 1 + 1 - 1 + 1
Guido van Rossum3bead091992-01-27 17:00:37 +0000643
Thomas Wouters89f507f2006-12-13 04:49:30 +0000644 def testMultiplicativeOps(self):
645 x = 1 * 1
646 x = 1 / 1
647 x = 1 % 1
648 x = 1 / 1 * 1 % 1
Guido van Rossum3bead091992-01-27 17:00:37 +0000649
Thomas Wouters89f507f2006-12-13 04:49:30 +0000650 def testUnaryOps(self):
651 x = +1
652 x = -1
653 x = ~1
654 x = ~1 ^ 1 & 1 | 1 & 1 ^ -1
655 x = -1*1/1 + 1*1 - ---1*1
Guido van Rossum3bead091992-01-27 17:00:37 +0000656
Thomas Wouters89f507f2006-12-13 04:49:30 +0000657 def testSelectors(self):
658 ### trailer: '(' [testlist] ')' | '[' subscript ']' | '.' NAME
659 ### subscript: expr | [expr] ':' [expr]
Guido van Rossum3bead091992-01-27 17:00:37 +0000660
Thomas Wouters89f507f2006-12-13 04:49:30 +0000661 import sys, time
662 c = sys.path[0]
663 x = time.time()
664 x = sys.modules['time'].time()
665 a = '01234'
666 c = a[0]
667 c = a[-1]
668 s = a[0:5]
669 s = a[:5]
670 s = a[0:]
671 s = a[:]
672 s = a[-5:]
673 s = a[:-1]
674 s = a[-4:-3]
675 # A rough test of SF bug 1333982. http://python.org/sf/1333982
676 # The testing here is fairly incomplete.
677 # Test cases should include: commas with 1 and 2 colons
678 d = {}
679 d[1] = 1
680 d[1,] = 2
681 d[1,2] = 3
682 d[1,2,3] = 4
683 L = list(d)
684 L.sort(key=lambda x: x if isinstance(x, tuple) else ())
Florent Xicluna9b86b9a2010-03-19 19:00:44 +0000685 self.assertEqual(str(L), '[1, (1,), (1, 2), (1, 2, 3)]')
Guido van Rossum3bead091992-01-27 17:00:37 +0000686
Thomas Wouters89f507f2006-12-13 04:49:30 +0000687 def testAtoms(self):
688 ### atom: '(' [testlist] ')' | '[' [testlist] ']' | '{' [dictsetmaker] '}' | NAME | NUMBER | STRING
689 ### dictsetmaker: (test ':' test (',' test ':' test)* [',']) | (test (',' test)* [','])
Guido van Rossum3bead091992-01-27 17:00:37 +0000690
Thomas Wouters89f507f2006-12-13 04:49:30 +0000691 x = (1)
692 x = (1 or 2 or 3)
693 x = (1 or 2 or 3, 2, 3)
Guido van Rossum3bead091992-01-27 17:00:37 +0000694
Thomas Wouters89f507f2006-12-13 04:49:30 +0000695 x = []
696 x = [1]
697 x = [1 or 2 or 3]
698 x = [1 or 2 or 3, 2, 3]
699 x = []
Guido van Rossum3bead091992-01-27 17:00:37 +0000700
Thomas Wouters89f507f2006-12-13 04:49:30 +0000701 x = {}
702 x = {'one': 1}
703 x = {'one': 1,}
704 x = {'one' or 'two': 1 or 2}
705 x = {'one': 1, 'two': 2}
706 x = {'one': 1, 'two': 2,}
707 x = {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5, 'six': 6}
Guido van Rossum3bead091992-01-27 17:00:37 +0000708
Thomas Wouters89f507f2006-12-13 04:49:30 +0000709 x = {'one'}
710 x = {'one', 1,}
711 x = {'one', 'two', 'three'}
712 x = {2, 3, 4,}
713
714 x = x
715 x = 'x'
716 x = 123
717
718 ### exprlist: expr (',' expr)* [',']
719 ### testlist: test (',' test)* [',']
720 # These have been exercised enough above
721
722 def testClassdef(self):
723 # 'class' NAME ['(' [testlist] ')'] ':' suite
724 class B: pass
725 class B2(): pass
726 class C1(B): pass
727 class C2(B): pass
728 class D(C1, C2, B): pass
729 class C:
730 def meth1(self): pass
731 def meth2(self, arg): pass
732 def meth3(self, a1, a2): pass
733
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000734 # decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE
735 # decorators: decorator+
736 # decorated: decorators (classdef | funcdef)
737 def class_decorator(x): return x
738 @class_decorator
739 class G: pass
740
741 def testDictcomps(self):
742 # dictorsetmaker: ( (test ':' test (comp_for |
743 # (',' test ':' test)* [','])) |
744 # (test (comp_for | (',' test)* [','])) )
745 nums = [1, 2, 3]
746 self.assertEqual({i:i+1 for i in nums}, {1: 2, 2: 3, 3: 4})
747
Thomas Wouters89f507f2006-12-13 04:49:30 +0000748 def testListcomps(self):
749 # list comprehension tests
750 nums = [1, 2, 3, 4, 5]
751 strs = ["Apple", "Banana", "Coconut"]
752 spcs = [" Apple", " Banana ", "Coco nut "]
753
754 self.assertEqual([s.strip() for s in spcs], ['Apple', 'Banana', 'Coco nut'])
755 self.assertEqual([3 * x for x in nums], [3, 6, 9, 12, 15])
756 self.assertEqual([x for x in nums if x > 2], [3, 4, 5])
757 self.assertEqual([(i, s) for i in nums for s in strs],
758 [(1, 'Apple'), (1, 'Banana'), (1, 'Coconut'),
759 (2, 'Apple'), (2, 'Banana'), (2, 'Coconut'),
760 (3, 'Apple'), (3, 'Banana'), (3, 'Coconut'),
761 (4, 'Apple'), (4, 'Banana'), (4, 'Coconut'),
762 (5, 'Apple'), (5, 'Banana'), (5, 'Coconut')])
763 self.assertEqual([(i, s) for i in nums for s in [f for f in strs if "n" in f]],
764 [(1, 'Banana'), (1, 'Coconut'), (2, 'Banana'), (2, 'Coconut'),
765 (3, 'Banana'), (3, 'Coconut'), (4, 'Banana'), (4, 'Coconut'),
766 (5, 'Banana'), (5, 'Coconut')])
767 self.assertEqual([(lambda a:[a**i for i in range(a+1)])(j) for j in range(5)],
768 [[1], [1, 1], [1, 2, 4], [1, 3, 9, 27], [1, 4, 16, 64, 256]])
769
770 def test_in_func(l):
771 return [0 < x < 3 for x in l if x > 2]
772
773 self.assertEqual(test_in_func(nums), [False, False, False])
774
775 def test_nested_front():
776 self.assertEqual([[y for y in [x, x + 1]] for x in [1,3,5]],
777 [[1, 2], [3, 4], [5, 6]])
778
779 test_nested_front()
780
781 check_syntax_error(self, "[i, s for i in nums for s in strs]")
782 check_syntax_error(self, "[x if y]")
783
784 suppliers = [
785 (1, "Boeing"),
786 (2, "Ford"),
787 (3, "Macdonalds")
788 ]
789
790 parts = [
791 (10, "Airliner"),
792 (20, "Engine"),
793 (30, "Cheeseburger")
794 ]
795
796 suppart = [
797 (1, 10), (1, 20), (2, 20), (3, 30)
798 ]
799
800 x = [
801 (sname, pname)
802 for (sno, sname) in suppliers
803 for (pno, pname) in parts
804 for (sp_sno, sp_pno) in suppart
805 if sno == sp_sno and pno == sp_pno
806 ]
807
808 self.assertEqual(x, [('Boeing', 'Airliner'), ('Boeing', 'Engine'), ('Ford', 'Engine'),
809 ('Macdonalds', 'Cheeseburger')])
810
811 def testGenexps(self):
812 # generator expression tests
813 g = ([x for x in range(10)] for x in range(1))
Georg Brandla18af4e2007-04-21 15:47:16 +0000814 self.assertEqual(next(g), [x for x in range(10)])
Thomas Wouters89f507f2006-12-13 04:49:30 +0000815 try:
Georg Brandla18af4e2007-04-21 15:47:16 +0000816 next(g)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000817 self.fail('should produce StopIteration exception')
818 except StopIteration:
819 pass
820
821 a = 1
822 try:
823 g = (a for d in a)
Georg Brandla18af4e2007-04-21 15:47:16 +0000824 next(g)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000825 self.fail('should produce TypeError')
826 except TypeError:
827 pass
828
829 self.assertEqual(list((x, y) for x in 'abcd' for y in 'abcd'), [(x, y) for x in 'abcd' for y in 'abcd'])
830 self.assertEqual(list((x, y) for x in 'ab' for y in 'xy'), [(x, y) for x in 'ab' for y in 'xy'])
831
832 a = [x for x in range(10)]
833 b = (x for x in (y for y in a))
834 self.assertEqual(sum(b), sum([x for x in range(10)]))
835
836 self.assertEqual(sum(x**2 for x in range(10)), sum([x**2 for x in range(10)]))
837 self.assertEqual(sum(x*x for x in range(10) if x%2), sum([x*x for x in range(10) if x%2]))
838 self.assertEqual(sum(x for x in (y for y in range(10))), sum([x for x in range(10)]))
839 self.assertEqual(sum(x for x in (y for y in (z for z in range(10)))), sum([x for x in range(10)]))
840 self.assertEqual(sum(x for x in [y for y in (z for z in range(10))]), sum([x for x in range(10)]))
841 self.assertEqual(sum(x for x in (y for y in (z for z in range(10) if True)) if True), sum([x for x in range(10)]))
842 self.assertEqual(sum(x for x in (y for y in (z for z in range(10) if True) if False) if True), 0)
843 check_syntax_error(self, "foo(x for x in range(10), 100)")
844 check_syntax_error(self, "foo(100, x for x in range(10))")
845
846 def testComprehensionSpecials(self):
847 # test for outmost iterable precomputation
848 x = 10; g = (i for i in range(x)); x = 5
849 self.assertEqual(len(list(g)), 10)
850
851 # This should hold, since we're only precomputing outmost iterable.
852 x = 10; t = False; g = ((i,j) for i in range(x) if t for j in range(x))
853 x = 5; t = True;
854 self.assertEqual([(i,j) for i in range(10) for j in range(5)], list(g))
855
856 # Grammar allows multiple adjacent 'if's in listcomps and genexps,
857 # even though it's silly. Make sure it works (ifelse broke this.)
858 self.assertEqual([ x for x in range(10) if x % 2 if x % 3 ], [1, 5, 7])
859 self.assertEqual(list(x for x in range(10) if x % 2 if x % 3), [1, 5, 7])
860
861 # verify unpacking single element tuples in listcomp/genexp.
862 self.assertEqual([x for x, in [(4,), (5,), (6,)]], [4, 5, 6])
863 self.assertEqual(list(x for x, in [(7,), (8,), (9,)]), [7, 8, 9])
864
Benjamin Petersonf17ab892009-05-29 21:55:57 +0000865 def test_with_statement(self):
866 class manager(object):
867 def __enter__(self):
868 return (1, 2)
869 def __exit__(self, *args):
870 pass
871
872 with manager():
873 pass
874 with manager() as x:
875 pass
876 with manager() as (x, y):
877 pass
878 with manager(), manager():
879 pass
880 with manager() as x, manager() as y:
881 pass
882 with manager() as x, manager():
883 pass
884
Thomas Wouters89f507f2006-12-13 04:49:30 +0000885 def testIfElseExpr(self):
886 # Test ifelse expressions in various cases
887 def _checkeval(msg, ret):
888 "helper to check that evaluation of expressions is done correctly"
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000889 print(x)
Thomas Wouters89f507f2006-12-13 04:49:30 +0000890 return ret
891
Nick Coghlan650f0d02007-04-15 12:05:43 +0000892 # the next line is not allowed anymore
893 #self.assertEqual([ x() for x in lambda: True, lambda: False if x() ], [True])
Thomas Wouters89f507f2006-12-13 04:49:30 +0000894 self.assertEqual([ x() for x in (lambda: True, lambda: False) if x() ], [True])
895 self.assertEqual([ x(False) for x in (lambda x: False if x else True, lambda x: True if x else False) if x(False) ], [True])
896 self.assertEqual((5 if 1 else _checkeval("check 1", 0)), 5)
897 self.assertEqual((_checkeval("check 2", 0) if 0 else 5), 5)
898 self.assertEqual((5 and 6 if 0 else 1), 1)
899 self.assertEqual(((5 and 6) if 0 else 1), 1)
900 self.assertEqual((5 and (6 if 1 else 1)), 6)
901 self.assertEqual((0 or _checkeval("check 3", 2) if 0 else 3), 3)
902 self.assertEqual((1 or _checkeval("check 4", 2) if 1 else _checkeval("check 5", 3)), 1)
903 self.assertEqual((0 or 5 if 1 else _checkeval("check 6", 3)), 5)
904 self.assertEqual((not 5 if 1 else 1), False)
905 self.assertEqual((not 5 if 0 else 1), 1)
906 self.assertEqual((6 + 1 if 1 else 2), 7)
907 self.assertEqual((6 - 1 if 1 else 2), 5)
908 self.assertEqual((6 * 2 if 1 else 4), 12)
909 self.assertEqual((6 / 2 if 1 else 3), 3)
910 self.assertEqual((6 < 4 if 0 else 2), 2)
Jeremy Hylton7b03bad2006-02-28 17:46:23 +0000911
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000912 def test_paren_evaluation(self):
913 self.assertEqual(16 // (4 // 2), 8)
914 self.assertEqual((16 // 4) // 2, 2)
915 self.assertEqual(16 // 4 // 2, 2)
916 self.assertTrue(False is (2 is 3))
917 self.assertFalse((False is 2) is 3)
918 self.assertFalse(False is 2 is 3)
919
Guido van Rossum3bead091992-01-27 17:00:37 +0000920
Thomas Wouters89f507f2006-12-13 04:49:30 +0000921def test_main():
922 run_unittest(TokenTests, GrammarTests)
Guido van Rossum3bead091992-01-27 17:00:37 +0000923
Thomas Wouters89f507f2006-12-13 04:49:30 +0000924if __name__ == '__main__':
925 test_main()