blob: 86cc0843eef4c8d218b1a75c2724f6395f7427f3 [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
Ezio Melottid80b4bf2010-03-17 13:52:48 +00004from test.test_support import run_unittest, check_syntax_error, \
5 check_py3k_warnings
Georg Brandlc6fdec62006-10-28 13:10:17 +00006import unittest
Jeremy Hylton7d3dff22001-10-10 01:45:02 +00007import sys
Georg Brandlc6fdec62006-10-28 13:10:17 +00008# testing import *
9from sys import *
Guido van Rossum3bead091992-01-27 17:00:37 +000010
Florent Xiclunabc27c6a2010-03-19 18:34:55 +000011
Georg Brandlc6fdec62006-10-28 13:10:17 +000012class TokenTests(unittest.TestCase):
Guido van Rossum3bead091992-01-27 17:00:37 +000013
Georg Brandlc6fdec62006-10-28 13:10:17 +000014 def testBackslash(self):
15 # Backslash means line continuation:
16 x = 1 \
17 + 1
Florent Xiclunabc27c6a2010-03-19 18:34:55 +000018 self.assertEqual(x, 2, 'backslash for line continuation')
Guido van Rossum3bead091992-01-27 17:00:37 +000019
Georg Brandlc6fdec62006-10-28 13:10:17 +000020 # Backslash does not means continuation in comments :\
21 x = 0
Florent Xiclunabc27c6a2010-03-19 18:34:55 +000022 self.assertEqual(x, 0, 'backslash ending comment')
Guido van Rossum3bead091992-01-27 17:00:37 +000023
Georg Brandlc6fdec62006-10-28 13:10:17 +000024 def testPlainIntegers(self):
Florent Xiclunabc27c6a2010-03-19 18:34:55 +000025 self.assertEqual(0xff, 255)
26 self.assertEqual(0377, 255)
27 self.assertEqual(2147483647, 017777777777)
Georg Brandl14404b62008-01-19 19:27:05 +000028 # "0x" is not a valid literal
29 self.assertRaises(SyntaxError, eval, "0x")
Georg Brandlc6fdec62006-10-28 13:10:17 +000030 from sys import maxint
31 if maxint == 2147483647:
Florent Xiclunabc27c6a2010-03-19 18:34:55 +000032 self.assertEqual(-2147483647-1, -020000000000)
Georg Brandlc6fdec62006-10-28 13:10:17 +000033 # XXX -2147483648
Benjamin Peterson5c8da862009-06-30 22:57:08 +000034 self.assertTrue(037777777777 > 0)
35 self.assertTrue(0xffffffff > 0)
Georg Brandlc6fdec62006-10-28 13:10:17 +000036 for s in '2147483648', '040000000000', '0x100000000':
37 try:
38 x = eval(s)
39 except OverflowError:
40 self.fail("OverflowError on huge integer literal %r" % s)
41 elif maxint == 9223372036854775807:
Florent Xiclunabc27c6a2010-03-19 18:34:55 +000042 self.assertEqual(-9223372036854775807-1, -01000000000000000000000)
Benjamin Peterson5c8da862009-06-30 22:57:08 +000043 self.assertTrue(01777777777777777777777 > 0)
44 self.assertTrue(0xffffffffffffffff > 0)
Georg Brandlc6fdec62006-10-28 13:10:17 +000045 for s in '9223372036854775808', '02000000000000000000000', \
46 '0x10000000000000000':
47 try:
48 x = eval(s)
49 except OverflowError:
50 self.fail("OverflowError on huge integer literal %r" % s)
51 else:
52 self.fail('Weird maxint value %r' % maxint)
Guido van Rossum3bead091992-01-27 17:00:37 +000053
Georg Brandlc6fdec62006-10-28 13:10:17 +000054 def testLongIntegers(self):
55 x = 0L
56 x = 0l
57 x = 0xffffffffffffffffL
58 x = 0xffffffffffffffffl
59 x = 077777777777777777L
60 x = 077777777777777777l
61 x = 123456789012345678901234567890L
62 x = 123456789012345678901234567890l
Guido van Rossum3bead091992-01-27 17:00:37 +000063
Georg Brandlc6fdec62006-10-28 13:10:17 +000064 def testFloats(self):
65 x = 3.14
66 x = 314.
67 x = 0.314
68 # XXX x = 000.314
69 x = .314
70 x = 3e14
71 x = 3E14
72 x = 3e-14
73 x = 3e+14
74 x = 3.e14
75 x = .3e14
76 x = 3.1e4
Guido van Rossum3bead091992-01-27 17:00:37 +000077
Georg Brandlc6fdec62006-10-28 13:10:17 +000078 def testStringLiterals(self):
Benjamin Peterson5c8da862009-06-30 22:57:08 +000079 x = ''; y = ""; self.assertTrue(len(x) == 0 and x == y)
80 x = '\''; y = "'"; self.assertTrue(len(x) == 1 and x == y and ord(x) == 39)
81 x = '"'; y = "\""; self.assertTrue(len(x) == 1 and x == y and ord(x) == 34)
Georg Brandlc6fdec62006-10-28 13:10:17 +000082 x = "doesn't \"shrink\" does it"
83 y = 'doesn\'t "shrink" does it'
Benjamin Peterson5c8da862009-06-30 22:57:08 +000084 self.assertTrue(len(x) == 24 and x == y)
Georg Brandlc6fdec62006-10-28 13:10:17 +000085 x = "does \"shrink\" doesn't it"
86 y = 'does "shrink" doesn\'t it'
Benjamin Peterson5c8da862009-06-30 22:57:08 +000087 self.assertTrue(len(x) == 24 and x == y)
Georg Brandlc6fdec62006-10-28 13:10:17 +000088 x = """
Guido van Rossumb6775db1994-08-01 11:34:53 +000089The "quick"
90brown fox
91jumps over
92the 'lazy' dog.
93"""
Georg Brandlc6fdec62006-10-28 13:10:17 +000094 y = '\nThe "quick"\nbrown fox\njumps over\nthe \'lazy\' dog.\n'
Florent Xiclunabc27c6a2010-03-19 18:34:55 +000095 self.assertEqual(x, y)
Georg Brandlc6fdec62006-10-28 13:10:17 +000096 y = '''
Guido van Rossumb6775db1994-08-01 11:34:53 +000097The "quick"
98brown fox
99jumps over
100the 'lazy' dog.
Georg Brandlc6fdec62006-10-28 13:10:17 +0000101'''
Florent Xiclunabc27c6a2010-03-19 18:34:55 +0000102 self.assertEqual(x, y)
Georg Brandlc6fdec62006-10-28 13:10:17 +0000103 y = "\n\
Guido van Rossumb6775db1994-08-01 11:34:53 +0000104The \"quick\"\n\
105brown fox\n\
106jumps over\n\
107the 'lazy' dog.\n\
Georg Brandlc6fdec62006-10-28 13:10:17 +0000108"
Florent Xiclunabc27c6a2010-03-19 18:34:55 +0000109 self.assertEqual(x, y)
Georg Brandlc6fdec62006-10-28 13:10:17 +0000110 y = '\n\
Guido van Rossumb6775db1994-08-01 11:34:53 +0000111The \"quick\"\n\
112brown fox\n\
113jumps over\n\
114the \'lazy\' dog.\n\
Georg Brandlc6fdec62006-10-28 13:10:17 +0000115'
Florent Xiclunabc27c6a2010-03-19 18:34:55 +0000116 self.assertEqual(x, y)
Guido van Rossumb31c7f71993-11-11 10:31:23 +0000117
118
Georg Brandlc6fdec62006-10-28 13:10:17 +0000119class GrammarTests(unittest.TestCase):
Guido van Rossum3bead091992-01-27 17:00:37 +0000120
Georg Brandlc6fdec62006-10-28 13:10:17 +0000121 # single_input: NEWLINE | simple_stmt | compound_stmt NEWLINE
122 # XXX can't test in a script -- this rule is only used when interactive
Tim Petersabd8a332006-11-03 02:32:46 +0000123
Georg Brandlc6fdec62006-10-28 13:10:17 +0000124 # file_input: (NEWLINE | stmt)* ENDMARKER
125 # Being tested as this very moment this very module
Tim Petersabd8a332006-11-03 02:32:46 +0000126
Georg Brandlc6fdec62006-10-28 13:10:17 +0000127 # expr_input: testlist NEWLINE
128 # XXX Hard to test -- used only in calls to input()
Guido van Rossum3bead091992-01-27 17:00:37 +0000129
Georg Brandlc6fdec62006-10-28 13:10:17 +0000130 def testEvalInput(self):
131 # testlist ENDMARKER
132 x = eval('1, 0 or 1')
Guido van Rossum3bead091992-01-27 17:00:37 +0000133
Georg Brandlc6fdec62006-10-28 13:10:17 +0000134 def testFuncdef(self):
135 ### 'def' NAME parameters ':' suite
136 ### parameters: '(' [varargslist] ')'
137 ### varargslist: (fpdef ['=' test] ',')* ('*' NAME [',' ('**'|'*' '*') NAME]
138 ### | ('**'|'*' '*') NAME)
139 ### | fpdef ['=' test] (',' fpdef ['=' test])* [',']
140 ### fpdef: NAME | '(' fplist ')'
141 ### fplist: fpdef (',' fpdef)* [',']
142 ### arglist: (argument ',')* (argument | *' test [',' '**' test] | '**' test)
143 ### argument: [test '='] test # Really [keyword '='] test
144 def f1(): pass
145 f1()
146 f1(*())
147 f1(*(), **{})
148 def f2(one_argument): pass
149 def f3(two, arguments): pass
Ezio Melottid80b4bf2010-03-17 13:52:48 +0000150 # Silence Py3k warning
151 exec('def f4(two, (compound, (argument, list))): pass')
152 exec('def f5((compound, first), two): pass')
Florent Xiclunabc27c6a2010-03-19 18:34:55 +0000153 self.assertEqual(f2.func_code.co_varnames, ('one_argument',))
154 self.assertEqual(f3.func_code.co_varnames, ('two', 'arguments'))
Georg Brandlc6fdec62006-10-28 13:10:17 +0000155 if sys.platform.startswith('java'):
Florent Xiclunabc27c6a2010-03-19 18:34:55 +0000156 self.assertEqual(f4.func_code.co_varnames,
Georg Brandlc6fdec62006-10-28 13:10:17 +0000157 ('two', '(compound, (argument, list))', 'compound', 'argument',
158 'list',))
Florent Xiclunabc27c6a2010-03-19 18:34:55 +0000159 self.assertEqual(f5.func_code.co_varnames,
Georg Brandlc6fdec62006-10-28 13:10:17 +0000160 ('(compound, first)', 'two', 'compound', 'first'))
161 else:
Florent Xiclunabc27c6a2010-03-19 18:34:55 +0000162 self.assertEqual(f4.func_code.co_varnames,
Georg Brandlc6fdec62006-10-28 13:10:17 +0000163 ('two', '.1', 'compound', 'argument', 'list'))
Florent Xiclunabc27c6a2010-03-19 18:34:55 +0000164 self.assertEqual(f5.func_code.co_varnames,
Georg Brandlc6fdec62006-10-28 13:10:17 +0000165 ('.0', 'two', 'compound', 'first'))
166 def a1(one_arg,): pass
167 def a2(two, args,): pass
168 def v0(*rest): pass
169 def v1(a, *rest): pass
170 def v2(a, b, *rest): pass
Ezio Melottid80b4bf2010-03-17 13:52:48 +0000171 # Silence Py3k warning
172 exec('def v3(a, (b, c), *rest): return a, b, c, rest')
Guido van Rossum3bead091992-01-27 17:00:37 +0000173
Georg Brandlc6fdec62006-10-28 13:10:17 +0000174 f1()
175 f2(1)
176 f2(1,)
177 f3(1, 2)
178 f3(1, 2,)
179 f4(1, (2, (3, 4)))
180 v0()
181 v0(1)
182 v0(1,)
183 v0(1,2)
184 v0(1,2,3,4,5,6,7,8,9,0)
185 v1(1)
186 v1(1,)
187 v1(1,2)
188 v1(1,2,3)
189 v1(1,2,3,4,5,6,7,8,9,0)
190 v2(1,2)
191 v2(1,2,3)
192 v2(1,2,3,4)
193 v2(1,2,3,4,5,6,7,8,9,0)
194 v3(1,(2,3))
195 v3(1,(2,3),4)
196 v3(1,(2,3),4,5,6,7,8,9,0)
Guido van Rossum3bead091992-01-27 17:00:37 +0000197
Georg Brandlc6fdec62006-10-28 13:10:17 +0000198 # ceval unpacks the formal arguments into the first argcount names;
199 # thus, the names nested inside tuples must appear after these names.
200 if sys.platform.startswith('java'):
Florent Xiclunabc27c6a2010-03-19 18:34:55 +0000201 self.assertEqual(v3.func_code.co_varnames, ('a', '(b, c)', 'rest', 'b', 'c'))
Georg Brandlc6fdec62006-10-28 13:10:17 +0000202 else:
Florent Xiclunabc27c6a2010-03-19 18:34:55 +0000203 self.assertEqual(v3.func_code.co_varnames, ('a', '.1', 'rest', 'b', 'c'))
204 self.assertEqual(v3(1, (2, 3), 4), (1, 2, 3, (4,)))
Georg Brandlc6fdec62006-10-28 13:10:17 +0000205 def d01(a=1): pass
206 d01()
207 d01(1)
208 d01(*(1,))
209 d01(**{'a':2})
210 def d11(a, b=1): pass
211 d11(1)
212 d11(1, 2)
213 d11(1, **{'b':2})
214 def d21(a, b, c=1): pass
215 d21(1, 2)
216 d21(1, 2, 3)
217 d21(*(1, 2, 3))
218 d21(1, *(2, 3))
219 d21(1, 2, *(3,))
220 d21(1, 2, **{'c':3})
221 def d02(a=1, b=2): pass
222 d02()
223 d02(1)
224 d02(1, 2)
225 d02(*(1, 2))
226 d02(1, *(2,))
227 d02(1, **{'b':2})
228 d02(**{'a': 1, 'b': 2})
229 def d12(a, b=1, c=2): pass
230 d12(1)
231 d12(1, 2)
232 d12(1, 2, 3)
233 def d22(a, b, c=1, d=2): pass
234 d22(1, 2)
235 d22(1, 2, 3)
236 d22(1, 2, 3, 4)
237 def d01v(a=1, *rest): pass
238 d01v()
239 d01v(1)
240 d01v(1, 2)
241 d01v(*(1, 2, 3, 4))
242 d01v(*(1,))
243 d01v(**{'a':2})
244 def d11v(a, b=1, *rest): pass
245 d11v(1)
246 d11v(1, 2)
247 d11v(1, 2, 3)
248 def d21v(a, b, c=1, *rest): pass
249 d21v(1, 2)
250 d21v(1, 2, 3)
251 d21v(1, 2, 3, 4)
252 d21v(*(1, 2, 3, 4))
253 d21v(1, 2, **{'c': 3})
254 def d02v(a=1, b=2, *rest): pass
255 d02v()
256 d02v(1)
257 d02v(1, 2)
258 d02v(1, 2, 3)
259 d02v(1, *(2, 3, 4))
260 d02v(**{'a': 1, 'b': 2})
261 def d12v(a, b=1, c=2, *rest): pass
262 d12v(1)
263 d12v(1, 2)
264 d12v(1, 2, 3)
265 d12v(1, 2, 3, 4)
266 d12v(*(1, 2, 3, 4))
267 d12v(1, 2, *(3, 4, 5))
268 d12v(1, *(2,), **{'c': 3})
269 def d22v(a, b, c=1, d=2, *rest): pass
270 d22v(1, 2)
271 d22v(1, 2, 3)
272 d22v(1, 2, 3, 4)
273 d22v(1, 2, 3, 4, 5)
274 d22v(*(1, 2, 3, 4))
275 d22v(1, 2, *(3, 4, 5))
276 d22v(1, *(2, 3), **{'d': 4})
Ezio Melottid80b4bf2010-03-17 13:52:48 +0000277 # Silence Py3k warning
278 exec('def d31v((x)): pass')
279 exec('def d32v((x,)): pass')
Georg Brandlc6fdec62006-10-28 13:10:17 +0000280 d31v(1)
Georg Brandlc6fdec62006-10-28 13:10:17 +0000281 d32v((1,))
Guido van Rossum3bead091992-01-27 17:00:37 +0000282
Benjamin Peterson80f0ed52008-08-19 19:52:46 +0000283 # keyword arguments after *arglist
284 def f(*args, **kwargs):
285 return args, kwargs
Florent Xiclunabc27c6a2010-03-19 18:34:55 +0000286 self.assertEqual(f(1, x=2, *[3, 4], y=5), ((1, 3, 4),
Benjamin Peterson80f0ed52008-08-19 19:52:46 +0000287 {'x':2, 'y':5}))
288 self.assertRaises(SyntaxError, eval, "f(1, *(2,3), 4)")
289 self.assertRaises(SyntaxError, eval, "f(1, x=2, *(3,4), x=5)")
290
Amaury Forgeot d'Arcd21fb4c2008-03-05 01:50:33 +0000291 # Check ast errors in *args and *kwargs
292 check_syntax_error(self, "f(*g(1=2))")
293 check_syntax_error(self, "f(**g(1=2))")
294
Georg Brandlc6fdec62006-10-28 13:10:17 +0000295 def testLambdef(self):
296 ### lambdef: 'lambda' [varargslist] ':' test
297 l1 = lambda : 0
Florent Xiclunabc27c6a2010-03-19 18:34:55 +0000298 self.assertEqual(l1(), 0)
Georg Brandlc6fdec62006-10-28 13:10:17 +0000299 l2 = lambda : a[d] # XXX just testing the expression
300 l3 = lambda : [2 < x for x in [-1, 3, 0L]]
Florent Xiclunabc27c6a2010-03-19 18:34:55 +0000301 self.assertEqual(l3(), [0, 1, 0])
Georg Brandlc6fdec62006-10-28 13:10:17 +0000302 l4 = lambda x = lambda y = lambda z=1 : z : y() : x()
Florent Xiclunabc27c6a2010-03-19 18:34:55 +0000303 self.assertEqual(l4(), 1)
Georg Brandlc6fdec62006-10-28 13:10:17 +0000304 l5 = lambda x, y, z=2: x + y + z
Florent Xiclunabc27c6a2010-03-19 18:34:55 +0000305 self.assertEqual(l5(1, 2), 5)
306 self.assertEqual(l5(1, 2, 3), 6)
Georg Brandlc6fdec62006-10-28 13:10:17 +0000307 check_syntax_error(self, "lambda x: x = 2")
Georg Brandla161f602008-06-15 19:54:36 +0000308 check_syntax_error(self, "lambda (None,): None")
Jeremy Hylton619eea62001-01-25 20:12:27 +0000309
Georg Brandlc6fdec62006-10-28 13:10:17 +0000310 ### stmt: simple_stmt | compound_stmt
311 # Tested below
Guido van Rossum3bead091992-01-27 17:00:37 +0000312
Georg Brandlc6fdec62006-10-28 13:10:17 +0000313 def testSimpleStmt(self):
314 ### simple_stmt: small_stmt (';' small_stmt)* [';']
315 x = 1; pass; del x
316 def foo():
Ezio Melottic2077b02011-03-16 12:34:31 +0200317 # verify statements that end with semi-colons
Georg Brandlc6fdec62006-10-28 13:10:17 +0000318 x = 1; pass; del x;
319 foo()
Guido van Rossum3bead091992-01-27 17:00:37 +0000320
Georg Brandlc6fdec62006-10-28 13:10:17 +0000321 ### small_stmt: expr_stmt | print_stmt | pass_stmt | del_stmt | flow_stmt | import_stmt | global_stmt | access_stmt | exec_stmt
322 # Tested below
Guido van Rossum3bead091992-01-27 17:00:37 +0000323
Georg Brandlc6fdec62006-10-28 13:10:17 +0000324 def testExprStmt(self):
325 # (exprlist '=')* exprlist
326 1
327 1, 2, 3
328 x = 1
329 x = 1, 2, 3
330 x = y = z = 1, 2, 3
331 x, y, z = 1, 2, 3
332 abc = a, b, c = x, y, z = xyz = 1, 2, (3, 4)
Guido van Rossum3bead091992-01-27 17:00:37 +0000333
Georg Brandlc6fdec62006-10-28 13:10:17 +0000334 check_syntax_error(self, "x + 1 = 1")
335 check_syntax_error(self, "a + 1 = b + 2")
Jeremy Hylton47793992001-02-19 15:35:26 +0000336
Georg Brandlc6fdec62006-10-28 13:10:17 +0000337 def testPrintStmt(self):
338 # 'print' (test ',')* [test]
339 import StringIO
Guido van Rossum3bead091992-01-27 17:00:37 +0000340
Georg Brandlc6fdec62006-10-28 13:10:17 +0000341 # Can't test printing to real stdout without comparing output
342 # which is not available in unittest.
343 save_stdout = sys.stdout
344 sys.stdout = StringIO.StringIO()
Tim Petersabd8a332006-11-03 02:32:46 +0000345
Georg Brandlc6fdec62006-10-28 13:10:17 +0000346 print 1, 2, 3
347 print 1, 2, 3,
348 print
349 print 0 or 1, 0 or 1,
350 print 0 or 1
Barry Warsawefc92ee2000-08-21 15:46:50 +0000351
Georg Brandlc6fdec62006-10-28 13:10:17 +0000352 # 'print' '>>' test ','
353 print >> sys.stdout, 1, 2, 3
354 print >> sys.stdout, 1, 2, 3,
355 print >> sys.stdout
356 print >> sys.stdout, 0 or 1, 0 or 1,
357 print >> sys.stdout, 0 or 1
Barry Warsaw9182b452000-08-29 04:57:10 +0000358
Georg Brandlc6fdec62006-10-28 13:10:17 +0000359 # test printing to an instance
360 class Gulp:
361 def write(self, msg): pass
Barry Warsaw7e3e1c12000-10-11 21:26:03 +0000362
Georg Brandlc6fdec62006-10-28 13:10:17 +0000363 gulp = Gulp()
364 print >> gulp, 1, 2, 3
365 print >> gulp, 1, 2, 3,
366 print >> gulp
367 print >> gulp, 0 or 1, 0 or 1,
368 print >> gulp, 0 or 1
Barry Warsaw9182b452000-08-29 04:57:10 +0000369
Georg Brandlc6fdec62006-10-28 13:10:17 +0000370 # test print >> None
371 def driver():
372 oldstdout = sys.stdout
373 sys.stdout = Gulp()
374 try:
375 tellme(Gulp())
376 tellme()
377 finally:
378 sys.stdout = oldstdout
Barry Warsaw9182b452000-08-29 04:57:10 +0000379
Georg Brandlc6fdec62006-10-28 13:10:17 +0000380 # we should see this once
381 def tellme(file=sys.stdout):
382 print >> file, 'hello world'
Barry Warsaw9182b452000-08-29 04:57:10 +0000383
Georg Brandlc6fdec62006-10-28 13:10:17 +0000384 driver()
Barry Warsaw9182b452000-08-29 04:57:10 +0000385
Georg Brandlc6fdec62006-10-28 13:10:17 +0000386 # we should not see this at all
387 def tellme(file=None):
388 print >> file, 'goodbye universe'
Barry Warsaw9182b452000-08-29 04:57:10 +0000389
Georg Brandlc6fdec62006-10-28 13:10:17 +0000390 driver()
Barry Warsawefc92ee2000-08-21 15:46:50 +0000391
Georg Brandlc6fdec62006-10-28 13:10:17 +0000392 self.assertEqual(sys.stdout.getvalue(), '''\
3931 2 3
3941 2 3
3951 1 1
3961 2 3
3971 2 3
3981 1 1
399hello world
400''')
401 sys.stdout = save_stdout
Guido van Rossum3bead091992-01-27 17:00:37 +0000402
Georg Brandlc6fdec62006-10-28 13:10:17 +0000403 # syntax errors
404 check_syntax_error(self, 'print ,')
405 check_syntax_error(self, 'print >> x,')
Guido van Rossum3bead091992-01-27 17:00:37 +0000406
Georg Brandlc6fdec62006-10-28 13:10:17 +0000407 def testDelStmt(self):
408 # 'del' exprlist
409 abc = [1,2,3]
410 x, y, z = abc
411 xyz = x, y, z
Guido van Rossum3bead091992-01-27 17:00:37 +0000412
Georg Brandlc6fdec62006-10-28 13:10:17 +0000413 del abc
414 del x, y, (z, xyz)
Guido van Rossum3bead091992-01-27 17:00:37 +0000415
Georg Brandlc6fdec62006-10-28 13:10:17 +0000416 def testPassStmt(self):
417 # 'pass'
418 pass
Guido van Rossum3bead091992-01-27 17:00:37 +0000419
Georg Brandlc6fdec62006-10-28 13:10:17 +0000420 # flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt
421 # Tested below
Jeremy Hylton3faa52e2001-02-01 22:48:12 +0000422
Georg Brandlc6fdec62006-10-28 13:10:17 +0000423 def testBreakStmt(self):
424 # 'break'
425 while 1: break
Tim Peters10fb3862001-02-09 20:17:14 +0000426
Georg Brandlc6fdec62006-10-28 13:10:17 +0000427 def testContinueStmt(self):
428 # 'continue'
429 i = 1
430 while i: i = 0; continue
Thomas Wouters80d373c2001-09-26 12:43:39 +0000431
Georg Brandlc6fdec62006-10-28 13:10:17 +0000432 msg = ""
433 while not msg:
434 msg = "ok"
435 try:
436 continue
437 msg = "continue failed to continue inside try"
438 except:
439 msg = "continue inside try called except block"
440 if msg != "ok":
441 self.fail(msg)
Thomas Wouters80d373c2001-09-26 12:43:39 +0000442
Georg Brandlc6fdec62006-10-28 13:10:17 +0000443 msg = ""
444 while not msg:
445 msg = "finally block not called"
446 try:
447 continue
448 finally:
449 msg = "ok"
450 if msg != "ok":
451 self.fail(msg)
452
453 def test_break_continue_loop(self):
454 # This test warrants an explanation. It is a test specifically for SF bugs
455 # #463359 and #462937. The bug is that a 'break' statement executed or
456 # exception raised inside a try/except inside a loop, *after* a continue
457 # statement has been executed in that loop, will cause the wrong number of
458 # arguments to be popped off the stack and the instruction pointer reset to
459 # a very small number (usually 0.) Because of this, the following test
460 # *must* written as a function, and the tracking vars *must* be function
461 # arguments with default values. Otherwise, the test will loop and loop.
462
463 def test_inner(extra_burning_oil = 1, count=0):
464 big_hippo = 2
465 while big_hippo:
466 count += 1
467 try:
468 if extra_burning_oil and big_hippo == 1:
469 extra_burning_oil -= 1
470 break
471 big_hippo -= 1
472 continue
473 except:
474 raise
Ezio Melottid80b4bf2010-03-17 13:52:48 +0000475 if count > 2 or big_hippo != 1:
Georg Brandlc6fdec62006-10-28 13:10:17 +0000476 self.fail("continue then break in try/except in loop broken!")
477 test_inner()
478
479 def testReturn(self):
480 # 'return' [testlist]
481 def g1(): return
482 def g2(): return 1
483 g1()
484 x = g2()
485 check_syntax_error(self, "class foo:return 1")
486
487 def testYield(self):
488 check_syntax_error(self, "class foo:yield 1")
489
490 def testRaise(self):
491 # 'raise' test [',' test]
492 try: raise RuntimeError, 'just testing'
493 except RuntimeError: pass
494 try: raise KeyboardInterrupt
495 except KeyboardInterrupt: pass
496
497 def testImport(self):
498 # 'import' dotted_as_names
499 import sys
500 import time, sys
501 # 'from' dotted_name 'import' ('*' | '(' import_as_names ')' | import_as_names)
502 from time import time
503 from time import (time)
504 # not testable inside a function, but already done at top of the module
505 # from sys import *
506 from sys import path, argv
507 from sys import (path, argv)
508 from sys import (path, argv,)
509
510 def testGlobal(self):
511 # 'global' NAME (',' NAME)*
512 global a
513 global a, b
514 global one, two, three, four, five, six, seven, eight, nine, ten
515
516 def testExec(self):
517 # 'exec' expr ['in' expr [',' expr]]
518 z = None
519 del z
520 exec 'z=1+1\n'
521 if z != 2: self.fail('exec \'z=1+1\'\\n')
522 del z
523 exec 'z=1+1'
524 if z != 2: self.fail('exec \'z=1+1\'')
525 z = None
526 del z
527 import types
528 if hasattr(types, "UnicodeType"):
529 exec r"""if 1:
530 exec u'z=1+1\n'
531 if z != 2: self.fail('exec u\'z=1+1\'\\n')
532 del z
533 exec u'z=1+1'
534 if z != 2: self.fail('exec u\'z=1+1\'')"""
535 g = {}
536 exec 'z = 1' in g
Ezio Melottid80b4bf2010-03-17 13:52:48 +0000537 if '__builtins__' in g: del g['__builtins__']
Georg Brandlc6fdec62006-10-28 13:10:17 +0000538 if g != {'z': 1}: self.fail('exec \'z = 1\' in g')
539 g = {}
540 l = {}
541
Georg Brandlc6fdec62006-10-28 13:10:17 +0000542 exec 'global a; a = 1; b = 2' in g, l
Ezio Melottid80b4bf2010-03-17 13:52:48 +0000543 if '__builtins__' in g: del g['__builtins__']
544 if '__builtins__' in l: del l['__builtins__']
Georg Brandlc6fdec62006-10-28 13:10:17 +0000545 if (g, l) != ({'a':1}, {'b':2}):
546 self.fail('exec ... in g (%s), l (%s)' %(g,l))
547
548 def testAssert(self):
Benjamin Peterson5c8da862009-06-30 22:57:08 +0000549 # assertTruestmt: 'assert' test [',' test]
Georg Brandlc6fdec62006-10-28 13:10:17 +0000550 assert 1
551 assert 1, 1
552 assert lambda x:x
553 assert 1, lambda x:x+1
Ezio Melottiab731a32011-12-02 18:17:30 +0200554
555 try:
556 assert True
557 except AssertionError as e:
558 self.fail("'assert True' should not have raised an AssertionError")
559
560 try:
561 assert True, 'this should always pass'
562 except AssertionError as e:
563 self.fail("'assert True, msg' should not have "
564 "raised an AssertionError")
565
566 # these tests fail if python is run with -O, so check __debug__
567 @unittest.skipUnless(__debug__, "Won't work if __debug__ is False")
568 def testAssert2(self):
Thomas Wouters80d373c2001-09-26 12:43:39 +0000569 try:
Georg Brandlc6fdec62006-10-28 13:10:17 +0000570 assert 0, "msg"
571 except AssertionError, e:
Florent Xiclunabc27c6a2010-03-19 18:34:55 +0000572 self.assertEqual(e.args[0], "msg")
Georg Brandlfacd2732006-10-29 09:18:00 +0000573 else:
Ezio Melottiab731a32011-12-02 18:17:30 +0200574 self.fail("AssertionError not raised by assert 0")
575
576 try:
577 assert False
578 except AssertionError as e:
579 self.assertEqual(len(e.args), 0)
580 else:
581 self.fail("AssertionError not raised by 'assert False'")
582
Thomas Wouters80d373c2001-09-26 12:43:39 +0000583
Georg Brandlc6fdec62006-10-28 13:10:17 +0000584 ### compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef
585 # Tested below
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000586
Georg Brandlc6fdec62006-10-28 13:10:17 +0000587 def testIf(self):
588 # 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite]
589 if 1: pass
590 if 1: pass
591 else: pass
592 if 0: pass
593 elif 0: pass
594 if 0: pass
595 elif 0: pass
596 elif 0: pass
597 elif 0: pass
598 else: pass
Tim Petersabd8a332006-11-03 02:32:46 +0000599
Georg Brandlc6fdec62006-10-28 13:10:17 +0000600 def testWhile(self):
601 # 'while' test ':' suite ['else' ':' suite]
602 while 0: pass
603 while 0: pass
604 else: pass
Guido van Rossum3bead091992-01-27 17:00:37 +0000605
Amaury Forgeot d'Arc16570f52008-01-24 22:51:18 +0000606 # Issue1920: "while 0" is optimized away,
607 # ensure that the "else" clause is still present.
608 x = 0
609 while 0:
610 x = 1
611 else:
612 x = 2
Florent Xiclunabc27c6a2010-03-19 18:34:55 +0000613 self.assertEqual(x, 2)
Amaury Forgeot d'Arc16570f52008-01-24 22:51:18 +0000614
Georg Brandlc6fdec62006-10-28 13:10:17 +0000615 def testFor(self):
616 # 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite]
617 for i in 1, 2, 3: pass
618 for i, j, k in (): pass
619 else: pass
620 class Squares:
621 def __init__(self, max):
622 self.max = max
623 self.sofar = []
624 def __len__(self): return len(self.sofar)
625 def __getitem__(self, i):
626 if not 0 <= i < self.max: raise IndexError
627 n = len(self.sofar)
628 while n <= i:
629 self.sofar.append(n*n)
630 n = n+1
631 return self.sofar[i]
632 n = 0
633 for x in Squares(10): n = n+x
634 if n != 285:
635 self.fail('for over growing sequence')
Guido van Rossum3bead091992-01-27 17:00:37 +0000636
Georg Brandlc6fdec62006-10-28 13:10:17 +0000637 result = []
638 for x, in [(1,), (2,), (3,)]:
639 result.append(x)
640 self.assertEqual(result, [1, 2, 3])
Guido van Rossum3bead091992-01-27 17:00:37 +0000641
Georg Brandlc6fdec62006-10-28 13:10:17 +0000642 def testTry(self):
643 ### try_stmt: 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite]
644 ### | 'try' ':' suite 'finally' ':' suite
Collin Winter62903052007-05-18 23:11:24 +0000645 ### except_clause: 'except' [expr [('as' | ',') expr]]
Georg Brandlc6fdec62006-10-28 13:10:17 +0000646 try:
647 1/0
648 except ZeroDivisionError:
649 pass
650 else:
651 pass
652 try: 1/0
653 except EOFError: pass
Collin Winter62903052007-05-18 23:11:24 +0000654 except TypeError as msg: pass
Georg Brandlc6fdec62006-10-28 13:10:17 +0000655 except RuntimeError, msg: pass
656 except: pass
657 else: pass
658 try: 1/0
659 except (EOFError, TypeError, ZeroDivisionError): pass
660 try: 1/0
661 except (EOFError, TypeError, ZeroDivisionError), msg: pass
662 try: pass
663 finally: pass
Guido van Rossum3bead091992-01-27 17:00:37 +0000664
Georg Brandlc6fdec62006-10-28 13:10:17 +0000665 def testSuite(self):
666 # simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT
667 if 1: pass
668 if 1:
669 pass
670 if 1:
671 #
672 #
673 #
674 pass
675 pass
676 #
677 pass
678 #
Jeremy Hylton2922ea82001-02-28 23:49:19 +0000679
Georg Brandlc6fdec62006-10-28 13:10:17 +0000680 def testTest(self):
681 ### and_test ('or' and_test)*
682 ### and_test: not_test ('and' not_test)*
683 ### not_test: 'not' not_test | comparison
684 if not 1: pass
685 if 1 and 1: pass
686 if 1 or 1: pass
687 if not not not 1: pass
688 if not 1 and 1 and 1: pass
689 if 1 and 1 or 1 and 1 and 1 or not 1 and 1: pass
690
691 def testComparison(self):
692 ### comparison: expr (comp_op expr)*
693 ### comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not'
694 if 1: pass
695 x = (1 == 1)
696 if 1 == 1: pass
697 if 1 != 1: pass
Georg Brandlc6fdec62006-10-28 13:10:17 +0000698 if 1 < 1: pass
699 if 1 > 1: pass
700 if 1 <= 1: pass
701 if 1 >= 1: pass
702 if 1 is 1: pass
703 if 1 is not 1: pass
704 if 1 in (): pass
705 if 1 not in (): pass
Ezio Melottid80b4bf2010-03-17 13:52:48 +0000706 if 1 < 1 > 1 == 1 >= 1 <= 1 != 1 in 1 not in 1 is 1 is not 1: pass
707 # Silence Py3k warning
708 if eval('1 <> 1'): pass
709 if eval('1 < 1 > 1 == 1 >= 1 <= 1 <> 1 != 1 in 1 not in 1 is 1 is not 1'): pass
Georg Brandlc6fdec62006-10-28 13:10:17 +0000710
711 def testBinaryMaskOps(self):
712 x = 1 & 1
713 x = 1 ^ 1
714 x = 1 | 1
715
716 def testShiftOps(self):
717 x = 1 << 1
718 x = 1 >> 1
719 x = 1 << 1 >> 1
720
721 def testAdditiveOps(self):
722 x = 1
723 x = 1 + 1
724 x = 1 - 1 - 1
725 x = 1 - 1 + 1 - 1 + 1
726
727 def testMultiplicativeOps(self):
728 x = 1 * 1
729 x = 1 / 1
730 x = 1 % 1
731 x = 1 / 1 * 1 % 1
732
733 def testUnaryOps(self):
734 x = +1
735 x = -1
736 x = ~1
737 x = ~1 ^ 1 & 1 | 1 & 1 ^ -1
738 x = -1*1/1 + 1*1 - ---1*1
739
740 def testSelectors(self):
741 ### trailer: '(' [testlist] ')' | '[' subscript ']' | '.' NAME
742 ### subscript: expr | [expr] ':' [expr]
Tim Petersabd8a332006-11-03 02:32:46 +0000743
Georg Brandlc6fdec62006-10-28 13:10:17 +0000744 import sys, time
745 c = sys.path[0]
746 x = time.time()
747 x = sys.modules['time'].time()
748 a = '01234'
749 c = a[0]
750 c = a[-1]
751 s = a[0:5]
752 s = a[:5]
753 s = a[0:]
754 s = a[:]
755 s = a[-5:]
756 s = a[:-1]
757 s = a[-4:-3]
758 # A rough test of SF bug 1333982. http://python.org/sf/1333982
759 # The testing here is fairly incomplete.
760 # Test cases should include: commas with 1 and 2 colons
761 d = {}
762 d[1] = 1
763 d[1,] = 2
764 d[1,2] = 3
765 d[1,2,3] = 4
766 L = list(d)
767 L.sort()
Florent Xiclunabc27c6a2010-03-19 18:34:55 +0000768 self.assertEqual(str(L), '[1, (1,), (1, 2), (1, 2, 3)]')
Georg Brandlc6fdec62006-10-28 13:10:17 +0000769
770 def testAtoms(self):
771 ### atom: '(' [testlist] ')' | '[' [testlist] ']' | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING
Alexandre Vassalottiee936a22010-01-09 23:35:54 +0000772 ### dictorsetmaker: (test ':' test (',' test ':' test)* [',']) | (test (',' test)* [','])
Georg Brandlc6fdec62006-10-28 13:10:17 +0000773
774 x = (1)
775 x = (1 or 2 or 3)
776 x = (1 or 2 or 3, 2, 3)
777
778 x = []
779 x = [1]
780 x = [1 or 2 or 3]
781 x = [1 or 2 or 3, 2, 3]
782 x = []
783
784 x = {}
785 x = {'one': 1}
786 x = {'one': 1,}
787 x = {'one' or 'two': 1 or 2}
788 x = {'one': 1, 'two': 2}
789 x = {'one': 1, 'two': 2,}
790 x = {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5, 'six': 6}
791
Alexandre Vassalottiee936a22010-01-09 23:35:54 +0000792 x = {'one'}
793 x = {'one', 1,}
794 x = {'one', 'two', 'three'}
795 x = {2, 3, 4,}
796
Ezio Melottid80b4bf2010-03-17 13:52:48 +0000797 # Silence Py3k warning
798 x = eval('`x`')
799 x = eval('`1 or 2 or 3`')
800 self.assertEqual(eval('`1,2`'), '(1, 2)')
Neal Norwitz85dbec62006-11-04 19:25:22 +0000801
Georg Brandlc6fdec62006-10-28 13:10:17 +0000802 x = x
803 x = 'x'
804 x = 123
805
806 ### exprlist: expr (',' expr)* [',']
807 ### testlist: test (',' test)* [',']
808 # These have been exercised enough above
809
810 def testClassdef(self):
811 # 'class' NAME ['(' [testlist] ')'] ':' suite
812 class B: pass
813 class B2(): pass
814 class C1(B): pass
815 class C2(B): pass
816 class D(C1, C2, B): pass
817 class C:
818 def meth1(self): pass
819 def meth2(self, arg): pass
820 def meth3(self, a1, a2): pass
Christian Heimes5224d282008-02-23 15:01:05 +0000821 # decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE
822 # decorators: decorator+
823 # decorated: decorators (classdef | funcdef)
824 def class_decorator(x):
825 x.decorated = True
826 return x
827 @class_decorator
828 class G:
829 pass
830 self.assertEqual(G.decorated, True)
Georg Brandlc6fdec62006-10-28 13:10:17 +0000831
Alexandre Vassalottib6465472010-01-11 22:36:12 +0000832 def testDictcomps(self):
833 # dictorsetmaker: ( (test ':' test (comp_for |
834 # (',' test ':' test)* [','])) |
835 # (test (comp_for | (',' test)* [','])) )
836 nums = [1, 2, 3]
837 self.assertEqual({i:i+1 for i in nums}, {1: 2, 2: 3, 3: 4})
838
Georg Brandlc6fdec62006-10-28 13:10:17 +0000839 def testListcomps(self):
840 # list comprehension tests
841 nums = [1, 2, 3, 4, 5]
842 strs = ["Apple", "Banana", "Coconut"]
843 spcs = [" Apple", " Banana ", "Coco nut "]
844
845 self.assertEqual([s.strip() for s in spcs], ['Apple', 'Banana', 'Coco nut'])
846 self.assertEqual([3 * x for x in nums], [3, 6, 9, 12, 15])
847 self.assertEqual([x for x in nums if x > 2], [3, 4, 5])
848 self.assertEqual([(i, s) for i in nums for s in strs],
849 [(1, 'Apple'), (1, 'Banana'), (1, 'Coconut'),
850 (2, 'Apple'), (2, 'Banana'), (2, 'Coconut'),
851 (3, 'Apple'), (3, 'Banana'), (3, 'Coconut'),
852 (4, 'Apple'), (4, 'Banana'), (4, 'Coconut'),
853 (5, 'Apple'), (5, 'Banana'), (5, 'Coconut')])
854 self.assertEqual([(i, s) for i in nums for s in [f for f in strs if "n" in f]],
855 [(1, 'Banana'), (1, 'Coconut'), (2, 'Banana'), (2, 'Coconut'),
856 (3, 'Banana'), (3, 'Coconut'), (4, 'Banana'), (4, 'Coconut'),
857 (5, 'Banana'), (5, 'Coconut')])
858 self.assertEqual([(lambda a:[a**i for i in range(a+1)])(j) for j in range(5)],
859 [[1], [1, 1], [1, 2, 4], [1, 3, 9, 27], [1, 4, 16, 64, 256]])
860
861 def test_in_func(l):
862 return [None < x < 3 for x in l if x > 2]
863
864 self.assertEqual(test_in_func(nums), [False, False, False])
865
866 def test_nested_front():
867 self.assertEqual([[y for y in [x, x + 1]] for x in [1,3,5]],
868 [[1, 2], [3, 4], [5, 6]])
869
870 test_nested_front()
871
872 check_syntax_error(self, "[i, s for i in nums for s in strs]")
873 check_syntax_error(self, "[x if y]")
874
875 suppliers = [
876 (1, "Boeing"),
877 (2, "Ford"),
878 (3, "Macdonalds")
879 ]
880
881 parts = [
882 (10, "Airliner"),
883 (20, "Engine"),
884 (30, "Cheeseburger")
885 ]
886
887 suppart = [
888 (1, 10), (1, 20), (2, 20), (3, 30)
889 ]
890
891 x = [
892 (sname, pname)
893 for (sno, sname) in suppliers
894 for (pno, pname) in parts
895 for (sp_sno, sp_pno) in suppart
896 if sno == sp_sno and pno == sp_pno
897 ]
898
899 self.assertEqual(x, [('Boeing', 'Airliner'), ('Boeing', 'Engine'), ('Ford', 'Engine'),
900 ('Macdonalds', 'Cheeseburger')])
901
902 def testGenexps(self):
903 # generator expression tests
904 g = ([x for x in range(10)] for x in range(1))
905 self.assertEqual(g.next(), [x for x in range(10)])
906 try:
907 g.next()
908 self.fail('should produce StopIteration exception')
909 except StopIteration:
910 pass
911
912 a = 1
913 try:
914 g = (a for d in a)
915 g.next()
916 self.fail('should produce TypeError')
917 except TypeError:
918 pass
919
920 self.assertEqual(list((x, y) for x in 'abcd' for y in 'abcd'), [(x, y) for x in 'abcd' for y in 'abcd'])
921 self.assertEqual(list((x, y) for x in 'ab' for y in 'xy'), [(x, y) for x in 'ab' for y in 'xy'])
922
923 a = [x for x in range(10)]
924 b = (x for x in (y for y in a))
925 self.assertEqual(sum(b), sum([x for x in range(10)]))
926
927 self.assertEqual(sum(x**2 for x in range(10)), sum([x**2 for x in range(10)]))
928 self.assertEqual(sum(x*x for x in range(10) if x%2), sum([x*x for x in range(10) if x%2]))
929 self.assertEqual(sum(x for x in (y for y in range(10))), sum([x for x in range(10)]))
930 self.assertEqual(sum(x for x in (y for y in (z for z in range(10)))), sum([x for x in range(10)]))
931 self.assertEqual(sum(x for x in [y for y in (z for z in range(10))]), sum([x for x in range(10)]))
932 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)]))
933 self.assertEqual(sum(x for x in (y for y in (z for z in range(10) if True) if False) if True), 0)
934 check_syntax_error(self, "foo(x for x in range(10), 100)")
935 check_syntax_error(self, "foo(100, x for x in range(10))")
936
937 def testComprehensionSpecials(self):
938 # test for outmost iterable precomputation
939 x = 10; g = (i for i in range(x)); x = 5
940 self.assertEqual(len(list(g)), 10)
941
942 # This should hold, since we're only precomputing outmost iterable.
943 x = 10; t = False; g = ((i,j) for i in range(x) if t for j in range(x))
944 x = 5; t = True;
945 self.assertEqual([(i,j) for i in range(10) for j in range(5)], list(g))
946
947 # Grammar allows multiple adjacent 'if's in listcomps and genexps,
948 # even though it's silly. Make sure it works (ifelse broke this.)
949 self.assertEqual([ x for x in range(10) if x % 2 if x % 3 ], [1, 5, 7])
950 self.assertEqual(list(x for x in range(10) if x % 2 if x % 3), [1, 5, 7])
951
952 # verify unpacking single element tuples in listcomp/genexp.
953 self.assertEqual([x for x, in [(4,), (5,), (6,)]], [4, 5, 6])
954 self.assertEqual(list(x for x, in [(7,), (8,), (9,)]), [7, 8, 9])
955
Benjamin Peterson46508922009-05-29 21:48:19 +0000956 def test_with_statement(self):
957 class manager(object):
958 def __enter__(self):
959 return (1, 2)
960 def __exit__(self, *args):
961 pass
962
963 with manager():
964 pass
965 with manager() as x:
966 pass
967 with manager() as (x, y):
968 pass
969 with manager(), manager():
970 pass
971 with manager() as x, manager() as y:
972 pass
973 with manager() as x, manager():
974 pass
975
Georg Brandlc6fdec62006-10-28 13:10:17 +0000976 def testIfElseExpr(self):
977 # Test ifelse expressions in various cases
978 def _checkeval(msg, ret):
979 "helper to check that evaluation of expressions is done correctly"
980 print x
981 return ret
982
983 self.assertEqual([ x() for x in lambda: True, lambda: False if x() ], [True])
984 self.assertEqual([ x() for x in (lambda: True, lambda: False) if x() ], [True])
985 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])
986 self.assertEqual((5 if 1 else _checkeval("check 1", 0)), 5)
987 self.assertEqual((_checkeval("check 2", 0) if 0 else 5), 5)
988 self.assertEqual((5 and 6 if 0 else 1), 1)
989 self.assertEqual(((5 and 6) if 0 else 1), 1)
990 self.assertEqual((5 and (6 if 1 else 1)), 6)
991 self.assertEqual((0 or _checkeval("check 3", 2) if 0 else 3), 3)
992 self.assertEqual((1 or _checkeval("check 4", 2) if 1 else _checkeval("check 5", 3)), 1)
993 self.assertEqual((0 or 5 if 1 else _checkeval("check 6", 3)), 5)
994 self.assertEqual((not 5 if 1 else 1), False)
995 self.assertEqual((not 5 if 0 else 1), 1)
996 self.assertEqual((6 + 1 if 1 else 2), 7)
997 self.assertEqual((6 - 1 if 1 else 2), 5)
998 self.assertEqual((6 * 2 if 1 else 4), 12)
999 self.assertEqual((6 / 2 if 1 else 3), 3)
1000 self.assertEqual((6 < 4 if 0 else 2), 2)
Guido van Rossumb3b09c91993-10-22 14:24:22 +00001001
Benjamin Petersonb2e31a12009-10-31 03:56:15 +00001002 def test_paren_evaluation(self):
1003 self.assertEqual(16 // (4 // 2), 8)
1004 self.assertEqual((16 // 4) // 2, 2)
1005 self.assertEqual(16 // 4 // 2, 2)
1006 self.assertTrue(False is (2 is 3))
1007 self.assertFalse((False is 2) is 3)
1008 self.assertFalse(False is 2 is 3)
1009
Guido van Rossumb3b09c91993-10-22 14:24:22 +00001010
Georg Brandlc6fdec62006-10-28 13:10:17 +00001011def test_main():
Ezio Melottid80b4bf2010-03-17 13:52:48 +00001012 with check_py3k_warnings(
1013 ("backquote not supported", SyntaxWarning),
1014 ("tuple parameter unpacking has been removed", SyntaxWarning),
1015 ("parenthesized argument names are invalid", SyntaxWarning),
1016 ("classic int division", DeprecationWarning),
1017 (".+ not supported in 3.x", DeprecationWarning)):
1018 run_unittest(TokenTests, GrammarTests)
Jeremy Hyltonf828e2d2001-02-19 15:54:52 +00001019
Georg Brandlc6fdec62006-10-28 13:10:17 +00001020if __name__ == '__main__':
1021 test_main()