1# Python test set -- part 1, grammar. 2# This just tests whether the parser accepts them all. 3 4# NOTE: When you run this test as a script from the command line, you 5# get warnings about certain hex/oct constants. Since those are 6# issued by the parser, you can't suppress them by adding a 7# filterwarnings() call to this module. Therefore, to shut up the 8# regression test, the filterwarnings() call has been added to 9# regrtest.py. 10 11from test.test_support import run_unittest, check_syntax_error 12import unittest 13import sys 14# testing import * 15from sys import * 16 17class TokenTests(unittest.TestCase): 18 19 def testBackslash(self): 20 # Backslash means line continuation: 21 x = 1 \ 22 + 1 23 self.assertEquals(x, 2, 'backslash for line continuation') 24 25 # Backslash does not means continuation in comments :\ 26 x = 0 27 self.assertEquals(x, 0, 'backslash ending comment') 28 29 def testPlainIntegers(self): 30 self.assertEquals(0xff, 255) 31 self.assertEquals(0377, 255) 32 self.assertEquals(2147483647, 017777777777) 33 # "0x" is not a valid literal 34 self.assertRaises(SyntaxError, eval, "0x") 35 from sys import maxint 36 if maxint == 2147483647: 37 self.assertEquals(-2147483647-1, -020000000000) 38 # XXX -2147483648 39 self.assert_(037777777777 > 0) 40 self.assert_(0xffffffff > 0) 41 for s in '2147483648', '040000000000', '0x100000000': 42 try: 43 x = eval(s) 44 except OverflowError: 45 self.fail("OverflowError on huge integer literal %r" % s) 46 elif maxint == 9223372036854775807: 47 self.assertEquals(-9223372036854775807-1, -01000000000000000000000) 48 self.assert_(01777777777777777777777 > 0) 49 self.assert_(0xffffffffffffffff > 0) 50 for s in '9223372036854775808', '02000000000000000000000', \ 51 '0x10000000000000000': 52 try: 53 x = eval(s) 54 except OverflowError: 55 self.fail("OverflowError on huge integer literal %r" % s) 56 else: 57 self.fail('Weird maxint value %r' % maxint) 58 59 def testLongIntegers(self): 60 x = 0L 61 x = 0l 62 x = 0xffffffffffffffffL 63 x = 0xffffffffffffffffl 64 x = 077777777777777777L 65 x = 077777777777777777l 66 x = 123456789012345678901234567890L 67 x = 123456789012345678901234567890l 68 69 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 82 83 def testStringLiterals(self): 84 x = ''; y = ""; self.assert_(len(x) == 0 and x == y) 85 x = '\''; y = "'"; self.assert_(len(x) == 1 and x == y and ord(x) == 39) 86 x = '"'; y = "\""; self.assert_(len(x) == 1 and x == y and ord(x) == 34) 87 x = "doesn't \"shrink\" does it" 88 y = 'doesn\'t "shrink" does it' 89 self.assert_(len(x) == 24 and x == y) 90 x = "does \"shrink\" doesn't it" 91 y = 'does "shrink" doesn\'t it' 92 self.assert_(len(x) == 24 and x == y) 93 x = """ 94The "quick" 95brown fox 96jumps over 97the 'lazy' dog. 98""" 99 y = '\nThe "quick"\nbrown fox\njumps over\nthe \'lazy\' dog.\n' 100 self.assertEquals(x, y) 101 y = ''' 102The "quick" 103brown fox 104jumps over 105the 'lazy' dog. 106''' 107 self.assertEquals(x, y) 108 y = "\n\ 109The \"quick\"\n\ 110brown fox\n\ 111jumps over\n\ 112the 'lazy' dog.\n\ 113" 114 self.assertEquals(x, y) 115 y = '\n\ 116The \"quick\"\n\ 117brown fox\n\ 118jumps over\n\ 119the \'lazy\' dog.\n\ 120' 121 self.assertEquals(x, y) 122 123 124class GrammarTests(unittest.TestCase): 125 126 # single_input: NEWLINE | simple_stmt | compound_stmt NEWLINE 127 # XXX can't test in a script -- this rule is only used when interactive 128 129 # file_input: (NEWLINE | stmt)* ENDMARKER 130 # Being tested as this very moment this very module 131 132 # expr_input: testlist NEWLINE 133 # XXX Hard to test -- used only in calls to input() 134 135 def testEvalInput(self): 136 # testlist ENDMARKER 137 x = eval('1, 0 or 1') 138 139 def testFuncdef(self): 140 ### 'def' NAME parameters ':' suite 141 ### parameters: '(' [varargslist] ')' 142 ### varargslist: (fpdef ['=' test] ',')* ('*' NAME [',' ('**'|'*' '*') NAME] 143 ### | ('**'|'*' '*') NAME) 144 ### | fpdef ['=' test] (',' fpdef ['=' test])* [','] 145 ### fpdef: NAME | '(' fplist ')' 146 ### fplist: fpdef (',' fpdef)* [','] 147 ### arglist: (argument ',')* (argument | *' test [',' '**' test] | '**' test) 148 ### argument: [test '='] test # Really [keyword '='] test 149 def f1(): pass 150 f1() 151 f1(*()) 152 f1(*(), **{}) 153 def f2(one_argument): pass 154 def f3(two, arguments): pass 155 def f4(two, (compound, (argument, list))): pass 156 def f5((compound, first), two): pass 157 self.assertEquals(f2.func_code.co_varnames, ('one_argument',)) 158 self.assertEquals(f3.func_code.co_varnames, ('two', 'arguments')) 159 if sys.platform.startswith('java'): 160 self.assertEquals(f4.func_code.co_varnames, 161 ('two', '(compound, (argument, list))', 'compound', 'argument', 162 'list',)) 163 self.assertEquals(f5.func_code.co_varnames, 164 ('(compound, first)', 'two', 'compound', 'first')) 165 else: 166 self.assertEquals(f4.func_code.co_varnames, 167 ('two', '.1', 'compound', 'argument', 'list')) 168 self.assertEquals(f5.func_code.co_varnames, 169 ('.0', 'two', 'compound', 'first')) 170 def a1(one_arg,): pass 171 def a2(two, args,): pass 172 def v0(*rest): pass 173 def v1(a, *rest): pass 174 def v2(a, b, *rest): pass 175 def v3(a, (b, c), *rest): return a, b, c, rest 176 177 f1() 178 f2(1) 179 f2(1,) 180 f3(1, 2) 181 f3(1, 2,) 182 f4(1, (2, (3, 4))) 183 v0() 184 v0(1) 185 v0(1,) 186 v0(1,2) 187 v0(1,2,3,4,5,6,7,8,9,0) 188 v1(1) 189 v1(1,) 190 v1(1,2) 191 v1(1,2,3) 192 v1(1,2,3,4,5,6,7,8,9,0) 193 v2(1,2) 194 v2(1,2,3) 195 v2(1,2,3,4) 196 v2(1,2,3,4,5,6,7,8,9,0) 197 v3(1,(2,3)) 198 v3(1,(2,3),4) 199 v3(1,(2,3),4,5,6,7,8,9,0) 200 201 # ceval unpacks the formal arguments into the first argcount names; 202 # thus, the names nested inside tuples must appear after these names. 203 if sys.platform.startswith('java'): 204 self.assertEquals(v3.func_code.co_varnames, ('a', '(b, c)', 'rest', 'b', 'c')) 205 else: 206 self.assertEquals(v3.func_code.co_varnames, ('a', '.1', 'rest', 'b', 'c')) 207 self.assertEquals(v3(1, (2, 3), 4), (1, 2, 3, (4,))) 208 def d01(a=1): pass 209 d01() 210 d01(1) 211 d01(*(1,)) 212 d01(**{'a':2}) 213 def d11(a, b=1): pass 214 d11(1) 215 d11(1, 2) 216 d11(1, **{'b':2}) 217 def d21(a, b, c=1): pass 218 d21(1, 2) 219 d21(1, 2, 3) 220 d21(*(1, 2, 3)) 221 d21(1, *(2, 3)) 222 d21(1, 2, *(3,)) 223 d21(1, 2, **{'c':3}) 224 def d02(a=1, b=2): pass 225 d02() 226 d02(1) 227 d02(1, 2) 228 d02(*(1, 2)) 229 d02(1, *(2,)) 230 d02(1, **{'b':2}) 231 d02(**{'a': 1, 'b': 2}) 232 def d12(a, b=1, c=2): pass 233 d12(1) 234 d12(1, 2) 235 d12(1, 2, 3) 236 def d22(a, b, c=1, d=2): pass 237 d22(1, 2) 238 d22(1, 2, 3) 239 d22(1, 2, 3, 4) 240 def d01v(a=1, *rest): pass 241 d01v() 242 d01v(1) 243 d01v(1, 2) 244 d01v(*(1, 2, 3, 4)) 245 d01v(*(1,)) 246 d01v(**{'a':2}) 247 def d11v(a, b=1, *rest): pass 248 d11v(1) 249 d11v(1, 2) 250 d11v(1, 2, 3) 251 def d21v(a, b, c=1, *rest): pass 252 d21v(1, 2) 253 d21v(1, 2, 3) 254 d21v(1, 2, 3, 4) 255 d21v(*(1, 2, 3, 4)) 256 d21v(1, 2, **{'c': 3}) 257 def d02v(a=1, b=2, *rest): pass 258 d02v() 259 d02v(1) 260 d02v(1, 2) 261 d02v(1, 2, 3) 262 d02v(1, *(2, 3, 4)) 263 d02v(**{'a': 1, 'b': 2}) 264 def d12v(a, b=1, c=2, *rest): pass 265 d12v(1) 266 d12v(1, 2) 267 d12v(1, 2, 3) 268 d12v(1, 2, 3, 4) 269 d12v(*(1, 2, 3, 4)) 270 d12v(1, 2, *(3, 4, 5)) 271 d12v(1, *(2,), **{'c': 3}) 272 def d22v(a, b, c=1, d=2, *rest): pass 273 d22v(1, 2) 274 d22v(1, 2, 3) 275 d22v(1, 2, 3, 4) 276 d22v(1, 2, 3, 4, 5) 277 d22v(*(1, 2, 3, 4)) 278 d22v(1, 2, *(3, 4, 5)) 279 d22v(1, *(2, 3), **{'d': 4}) 280 def d31v((x)): pass 281 d31v(1) 282 def d32v((x,)): pass 283 d32v((1,)) 284 285 # keyword arguments after *arglist 286 def f(*args, **kwargs): 287 return args, kwargs 288 self.assertEquals(f(1, x=2, *[3, 4], y=5), ((1, 3, 4), 289 {'x':2, 'y':5})) 290 self.assertRaises(SyntaxError, eval, "f(1, *(2,3), 4)") 291 self.assertRaises(SyntaxError, eval, "f(1, x=2, *(3,4), x=5)") 292 293 # Check ast errors in *args and *kwargs 294 check_syntax_error(self, "f(*g(1=2))") 295 check_syntax_error(self, "f(**g(1=2))") 296 297 def testLambdef(self): 298 ### lambdef: 'lambda' [varargslist] ':' test 299 l1 = lambda : 0 300 self.assertEquals(l1(), 0) 301 l2 = lambda : a[d] # XXX just testing the expression 302 l3 = lambda : [2 < x for x in [-1, 3, 0L]] 303 self.assertEquals(l3(), [0, 1, 0]) 304 l4 = lambda x = lambda y = lambda z=1 : z : y() : x() 305 self.assertEquals(l4(), 1) 306 l5 = lambda x, y, z=2: x + y + z 307 self.assertEquals(l5(1, 2), 5) 308 self.assertEquals(l5(1, 2, 3), 6) 309 check_syntax_error(self, "lambda x: x = 2") 310 check_syntax_error(self, "lambda (None,): None") 311 312 ### stmt: simple_stmt | compound_stmt 313 # Tested below 314 315 def testSimpleStmt(self): 316 ### simple_stmt: small_stmt (';' small_stmt)* [';'] 317 x = 1; pass; del x 318 def foo(): 319 # verify statements that end with semi-colons 320 x = 1; pass; del x; 321 foo() 322 323 ### small_stmt: expr_stmt | print_stmt | pass_stmt | del_stmt | flow_stmt | import_stmt | global_stmt | access_stmt | exec_stmt 324 # Tested below 325 326 def testExprStmt(self): 327 # (exprlist '=')* exprlist 328 1 329 1, 2, 3 330 x = 1 331 x = 1, 2, 3 332 x = y = z = 1, 2, 3 333 x, y, z = 1, 2, 3 334 abc = a, b, c = x, y, z = xyz = 1, 2, (3, 4) 335 336 check_syntax_error(self, "x + 1 = 1") 337 check_syntax_error(self, "a + 1 = b + 2") 338 339 def testPrintStmt(self): 340 # 'print' (test ',')* [test] 341 import StringIO 342 343 # Can't test printing to real stdout without comparing output 344 # which is not available in unittest. 345 save_stdout = sys.stdout 346 sys.stdout = StringIO.StringIO() 347 348 print 1, 2, 3 349 print 1, 2, 3, 350 print 351 print 0 or 1, 0 or 1, 352 print 0 or 1 353 354 # 'print' '>>' test ',' 355 print >> sys.stdout, 1, 2, 3 356 print >> sys.stdout, 1, 2, 3, 357 print >> sys.stdout 358 print >> sys.stdout, 0 or 1, 0 or 1, 359 print >> sys.stdout, 0 or 1 360 361 # test printing to an instance 362 class Gulp: 363 def write(self, msg): pass 364 365 gulp = Gulp() 366 print >> gulp, 1, 2, 3 367 print >> gulp, 1, 2, 3, 368 print >> gulp 369 print >> gulp, 0 or 1, 0 or 1, 370 print >> gulp, 0 or 1 371 372 # test print >> None 373 def driver(): 374 oldstdout = sys.stdout 375 sys.stdout = Gulp() 376 try: 377 tellme(Gulp()) 378 tellme() 379 finally: 380 sys.stdout = oldstdout 381 382 # we should see this once 383 def tellme(file=sys.stdout): 384 print >> file, 'hello world' 385 386 driver() 387 388 # we should not see this at all 389 def tellme(file=None): 390 print >> file, 'goodbye universe' 391 392 driver() 393 394 self.assertEqual(sys.stdout.getvalue(), '''\ 3951 2 3 3961 2 3 3971 1 1 3981 2 3 3991 2 3 4001 1 1 401hello world 402''') 403 sys.stdout = save_stdout 404 405 # syntax errors 406 check_syntax_error(self, 'print ,') 407 check_syntax_error(self, 'print >> x,') 408 409 def testDelStmt(self): 410 # 'del' exprlist 411 abc = [1,2,3] 412 x, y, z = abc 413 xyz = x, y, z 414 415 del abc 416 del x, y, (z, xyz) 417 418 def testPassStmt(self): 419 # 'pass' 420 pass 421 422 # flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt 423 # Tested below 424 425 def testBreakStmt(self): 426 # 'break' 427 while 1: break 428 429 def testContinueStmt(self): 430 # 'continue' 431 i = 1 432 while i: i = 0; continue 433 434 msg = "" 435 while not msg: 436 msg = "ok" 437 try: 438 continue 439 msg = "continue failed to continue inside try" 440 except: 441 msg = "continue inside try called except block" 442 if msg != "ok": 443 self.fail(msg) 444 445 msg = "" 446 while not msg: 447 msg = "finally block not called" 448 try: 449 continue 450 finally: 451 msg = "ok" 452 if msg != "ok": 453 self.fail(msg) 454 455 def test_break_continue_loop(self): 456 # This test warrants an explanation. It is a test specifically for SF bugs 457 # #463359 and #462937. The bug is that a 'break' statement executed or 458 # exception raised inside a try/except inside a loop, *after* a continue 459 # statement has been executed in that loop, will cause the wrong number of 460 # arguments to be popped off the stack and the instruction pointer reset to 461 # a very small number (usually 0.) Because of this, the following test 462 # *must* written as a function, and the tracking vars *must* be function 463 # arguments with default values. Otherwise, the test will loop and loop. 464 465 def test_inner(extra_burning_oil = 1, count=0): 466 big_hippo = 2 467 while big_hippo: 468 count += 1 469 try: 470 if extra_burning_oil and big_hippo == 1: 471 extra_burning_oil -= 1 472 break 473 big_hippo -= 1 474 continue 475 except: 476 raise 477 if count > 2 or big_hippo <> 1: 478 self.fail("continue then break in try/except in loop broken!") 479 test_inner() 480 481 def testReturn(self): 482 # 'return' [testlist] 483 def g1(): return 484 def g2(): return 1 485 g1() 486 x = g2() 487 check_syntax_error(self, "class foo:return 1") 488 489 def testYield(self): 490 check_syntax_error(self, "class foo:yield 1") 491 492 def testRaise(self): 493 # 'raise' test [',' test] 494 try: raise RuntimeError, 'just testing' 495 except RuntimeError: pass 496 try: raise KeyboardInterrupt 497 except KeyboardInterrupt: pass 498 499 def testImport(self): 500 # 'import' dotted_as_names 501 import sys 502 import time, sys 503 # 'from' dotted_name 'import' ('*' | '(' import_as_names ')' | import_as_names) 504 from time import time 505 from time import (time) 506 # not testable inside a function, but already done at top of the module 507 # from sys import * 508 from sys import path, argv 509 from sys import (path, argv) 510 from sys import (path, argv,) 511 512 def testGlobal(self): 513 # 'global' NAME (',' NAME)* 514 global a 515 global a, b 516 global one, two, three, four, five, six, seven, eight, nine, ten 517 518 def testExec(self): 519 # 'exec' expr ['in' expr [',' expr]] 520 z = None 521 del z 522 exec 'z=1+1\n' 523 if z != 2: self.fail('exec \'z=1+1\'\\n') 524 del z 525 exec 'z=1+1' 526 if z != 2: self.fail('exec \'z=1+1\'') 527 z = None 528 del z 529 import types 530 if hasattr(types, "UnicodeType"): 531 exec r"""if 1: 532 exec u'z=1+1\n' 533 if z != 2: self.fail('exec u\'z=1+1\'\\n') 534 del z 535 exec u'z=1+1' 536 if z != 2: self.fail('exec u\'z=1+1\'')""" 537 g = {} 538 exec 'z = 1' in g 539 if g.has_key('__builtins__'): del g['__builtins__'] 540 if g != {'z': 1}: self.fail('exec \'z = 1\' in g') 541 g = {} 542 l = {} 543 544 import warnings 545 warnings.filterwarnings("ignore", "global statement", module="<string>") 546 exec 'global a; a = 1; b = 2' in g, l 547 if g.has_key('__builtins__'): del g['__builtins__'] 548 if l.has_key('__builtins__'): del l['__builtins__'] 549 if (g, l) != ({'a':1}, {'b':2}): 550 self.fail('exec ... in g (%s), l (%s)' %(g,l)) 551 552 def testAssert(self): 553 # assert_stmt: 'assert' test [',' test] 554 assert 1 555 assert 1, 1 556 assert lambda x:x 557 assert 1, lambda x:x+1 558 try: 559 assert 0, "msg" 560 except AssertionError, e: 561 self.assertEquals(e.args[0], "msg") 562 else: 563 if __debug__: 564 self.fail("AssertionError not raised by assert 0") 565 566 ### compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef 567 # Tested below 568 569 def testIf(self): 570 # 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite] 571 if 1: pass 572 if 1: pass 573 else: pass 574 if 0: pass 575 elif 0: pass 576 if 0: pass 577 elif 0: pass 578 elif 0: pass 579 elif 0: pass 580 else: pass 581 582 def testWhile(self): 583 # 'while' test ':' suite ['else' ':' suite] 584 while 0: pass 585 while 0: pass 586 else: pass 587 588 # Issue1920: "while 0" is optimized away, 589 # ensure that the "else" clause is still present. 590 x = 0 591 while 0: 592 x = 1 593 else: 594 x = 2 595 self.assertEquals(x, 2) 596 597 def testFor(self): 598 # 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite] 599 for i in 1, 2, 3: pass 600 for i, j, k in (): pass 601 else: pass 602 class Squares: 603 def __init__(self, max): 604 self.max = max 605 self.sofar = [] 606 def __len__(self): return len(self.sofar) 607 def __getitem__(self, i): 608 if not 0 <= i < self.max: raise IndexError 609 n = len(self.sofar) 610 while n <= i: 611 self.sofar.append(n*n) 612 n = n+1 613 return self.sofar[i] 614 n = 0 615 for x in Squares(10): n = n+x 616 if n != 285: 617 self.fail('for over growing sequence') 618 619 result = [] 620 for x, in [(1,), (2,), (3,)]: 621 result.append(x) 622 self.assertEqual(result, [1, 2, 3]) 623 624 def testTry(self): 625 ### try_stmt: 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite] 626 ### | 'try' ':' suite 'finally' ':' suite 627 ### except_clause: 'except' [expr [('as' | ',') expr]] 628 try: 629 1/0 630 except ZeroDivisionError: 631 pass 632 else: 633 pass 634 try: 1/0 635 except EOFError: pass 636 except TypeError as msg: pass 637 except RuntimeError, msg: pass 638 except: pass 639 else: pass 640 try: 1/0 641 except (EOFError, TypeError, ZeroDivisionError): pass 642 try: 1/0 643 except (EOFError, TypeError, ZeroDivisionError), msg: pass 644 try: pass 645 finally: pass 646 647 def testSuite(self): 648 # simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT 649 if 1: pass 650 if 1: 651 pass 652 if 1: 653 # 654 # 655 # 656 pass 657 pass 658 # 659 pass 660 # 661 662 def testTest(self): 663 ### and_test ('or' and_test)* 664 ### and_test: not_test ('and' not_test)* 665 ### not_test: 'not' not_test | comparison 666 if not 1: pass 667 if 1 and 1: pass 668 if 1 or 1: pass 669 if not not not 1: pass 670 if not 1 and 1 and 1: pass 671 if 1 and 1 or 1 and 1 and 1 or not 1 and 1: pass 672 673 def testComparison(self): 674 ### comparison: expr (comp_op expr)* 675 ### comp_op: '<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not' 676 if 1: pass 677 x = (1 == 1) 678 if 1 == 1: pass 679 if 1 != 1: pass 680 if 1 <> 1: pass 681 if 1 < 1: pass 682 if 1 > 1: pass 683 if 1 <= 1: pass 684 if 1 >= 1: pass 685 if 1 is 1: pass 686 if 1 is not 1: pass 687 if 1 in (): pass 688 if 1 not in (): pass 689 if 1 < 1 > 1 == 1 >= 1 <= 1 <> 1 != 1 in 1 not in 1 is 1 is not 1: pass 690 691 def testBinaryMaskOps(self): 692 x = 1 & 1 693 x = 1 ^ 1 694 x = 1 | 1 695 696 def testShiftOps(self): 697 x = 1 << 1 698 x = 1 >> 1 699 x = 1 << 1 >> 1 700 701 def testAdditiveOps(self): 702 x = 1 703 x = 1 + 1 704 x = 1 - 1 - 1 705 x = 1 - 1 + 1 - 1 + 1 706 707 def testMultiplicativeOps(self): 708 x = 1 * 1 709 x = 1 / 1 710 x = 1 % 1 711 x = 1 / 1 * 1 % 1 712 713 def testUnaryOps(self): 714 x = +1 715 x = -1 716 x = ~1 717 x = ~1 ^ 1 & 1 | 1 & 1 ^ -1 718 x = -1*1/1 + 1*1 - ---1*1 719 720 def testSelectors(self): 721 ### trailer: '(' [testlist] ')' | '[' subscript ']' | '.' NAME 722 ### subscript: expr | [expr] ':' [expr] 723 724 import sys, time 725 c = sys.path[0] 726 x = time.time() 727 x = sys.modules['time'].time() 728 a = '01234' 729 c = a[0] 730 c = a[-1] 731 s = a[0:5] 732 s = a[:5] 733 s = a[0:] 734 s = a[:] 735 s = a[-5:] 736 s = a[:-1] 737 s = a[-4:-3] 738 # A rough test of SF bug 1333982. http://python.org/sf/1333982 739 # The testing here is fairly incomplete. 740 # Test cases should include: commas with 1 and 2 colons 741 d = {} 742 d[1] = 1 743 d[1,] = 2 744 d[1,2] = 3 745 d[1,2,3] = 4 746 L = list(d) 747 L.sort() 748 self.assertEquals(str(L), '[1, (1,), (1, 2), (1, 2, 3)]') 749 750 def testAtoms(self): 751 ### atom: '(' [testlist] ')' | '[' [testlist] ']' | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING 752 ### dictmaker: test ':' test (',' test ':' test)* [','] 753 754 x = (1) 755 x = (1 or 2 or 3) 756 x = (1 or 2 or 3, 2, 3) 757 758 x = [] 759 x = [1] 760 x = [1 or 2 or 3] 761 x = [1 or 2 or 3, 2, 3] 762 x = [] 763 764 x = {} 765 x = {'one': 1} 766 x = {'one': 1,} 767 x = {'one' or 'two': 1 or 2} 768 x = {'one': 1, 'two': 2} 769 x = {'one': 1, 'two': 2,} 770 x = {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5, 'six': 6} 771 772 x = `x` 773 x = `1 or 2 or 3` 774 self.assertEqual(`1,2`, '(1, 2)') 775 776 x = x 777 x = 'x' 778 x = 123 779 780 ### exprlist: expr (',' expr)* [','] 781 ### testlist: test (',' test)* [','] 782 # These have been exercised enough above 783 784 def testClassdef(self): 785 # 'class' NAME ['(' [testlist] ')'] ':' suite 786 class B: pass 787 class B2(): pass 788 class C1(B): pass 789 class C2(B): pass 790 class D(C1, C2, B): pass 791 class C: 792 def meth1(self): pass 793 def meth2(self, arg): pass 794 def meth3(self, a1, a2): pass 795 # decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE 796 # decorators: decorator+ 797 # decorated: decorators (classdef | funcdef) 798 def class_decorator(x): 799 x.decorated = True 800 return x 801 @class_decorator 802 class G: 803 pass 804 self.assertEqual(G.decorated, True) 805 806 def testListcomps(self): 807 # list comprehension tests 808 nums = [1, 2, 3, 4, 5] 809 strs = ["Apple", "Banana", "Coconut"] 810 spcs = [" Apple", " Banana ", "Coco nut "] 811 812 self.assertEqual([s.strip() for s in spcs], ['Apple', 'Banana', 'Coco nut']) 813 self.assertEqual([3 * x for x in nums], [3, 6, 9, 12, 15]) 814 self.assertEqual([x for x in nums if x > 2], [3, 4, 5]) 815 self.assertEqual([(i, s) for i in nums for s in strs], 816 [(1, 'Apple'), (1, 'Banana'), (1, 'Coconut'), 817 (2, 'Apple'), (2, 'Banana'), (2, 'Coconut'), 818 (3, 'Apple'), (3, 'Banana'), (3, 'Coconut'), 819 (4, 'Apple'), (4, 'Banana'), (4, 'Coconut'), 820 (5, 'Apple'), (5, 'Banana'), (5, 'Coconut')]) 821 self.assertEqual([(i, s) for i in nums for s in [f for f in strs if "n" in f]], 822 [(1, 'Banana'), (1, 'Coconut'), (2, 'Banana'), (2, 'Coconut'), 823 (3, 'Banana'), (3, 'Coconut'), (4, 'Banana'), (4, 'Coconut'), 824 (5, 'Banana'), (5, 'Coconut')]) 825 self.assertEqual([(lambda a:[a**i for i in range(a+1)])(j) for j in range(5)], 826 [[1], [1, 1], [1, 2, 4], [1, 3, 9, 27], [1, 4, 16, 64, 256]]) 827 828 def test_in_func(l): 829 return [None < x < 3 for x in l if x > 2] 830 831 self.assertEqual(test_in_func(nums), [False, False, False]) 832 833 def test_nested_front(): 834 self.assertEqual([[y for y in [x, x + 1]] for x in [1,3,5]], 835 [[1, 2], [3, 4], [5, 6]]) 836 837 test_nested_front() 838 839 check_syntax_error(self, "[i, s for i in nums for s in strs]") 840 check_syntax_error(self, "[x if y]") 841 842 suppliers = [ 843 (1, "Boeing"), 844 (2, "Ford"), 845 (3, "Macdonalds") 846 ] 847 848 parts = [ 849 (10, "Airliner"), 850 (20, "Engine"), 851 (30, "Cheeseburger") 852 ] 853 854 suppart = [ 855 (1, 10), (1, 20), (2, 20), (3, 30) 856 ] 857 858 x = [ 859 (sname, pname) 860 for (sno, sname) in suppliers 861 for (pno, pname) in parts 862 for (sp_sno, sp_pno) in suppart 863 if sno == sp_sno and pno == sp_pno 864 ] 865 866 self.assertEqual(x, [('Boeing', 'Airliner'), ('Boeing', 'Engine'), ('Ford', 'Engine'), 867 ('Macdonalds', 'Cheeseburger')]) 868 869 def testGenexps(self): 870 # generator expression tests 871 g = ([x for x in range(10)] for x in range(1)) 872 self.assertEqual(g.next(), [x for x in range(10)]) 873 try: 874 g.next() 875 self.fail('should produce StopIteration exception') 876 except StopIteration: 877 pass 878 879 a = 1 880 try: 881 g = (a for d in a) 882 g.next() 883 self.fail('should produce TypeError') 884 except TypeError: 885 pass 886 887 self.assertEqual(list((x, y) for x in 'abcd' for y in 'abcd'), [(x, y) for x in 'abcd' for y in 'abcd']) 888 self.assertEqual(list((x, y) for x in 'ab' for y in 'xy'), [(x, y) for x in 'ab' for y in 'xy']) 889 890 a = [x for x in range(10)] 891 b = (x for x in (y for y in a)) 892 self.assertEqual(sum(b), sum([x for x in range(10)])) 893 894 self.assertEqual(sum(x**2 for x in range(10)), sum([x**2 for x in range(10)])) 895 self.assertEqual(sum(x*x for x in range(10) if x%2), sum([x*x for x in range(10) if x%2])) 896 self.assertEqual(sum(x for x in (y for y in range(10))), sum([x for x in range(10)])) 897 self.assertEqual(sum(x for x in (y for y in (z for z in range(10)))), sum([x for x in range(10)])) 898 self.assertEqual(sum(x for x in [y for y in (z for z in range(10))]), sum([x for x in range(10)])) 899 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)])) 900 self.assertEqual(sum(x for x in (y for y in (z for z in range(10) if True) if False) if True), 0) 901 check_syntax_error(self, "foo(x for x in range(10), 100)") 902 check_syntax_error(self, "foo(100, x for x in range(10))") 903 904 def testComprehensionSpecials(self): 905 # test for outmost iterable precomputation 906 x = 10; g = (i for i in range(x)); x = 5 907 self.assertEqual(len(list(g)), 10) 908 909 # This should hold, since we're only precomputing outmost iterable. 910 x = 10; t = False; g = ((i,j) for i in range(x) if t for j in range(x)) 911 x = 5; t = True; 912 self.assertEqual([(i,j) for i in range(10) for j in range(5)], list(g)) 913 914 # Grammar allows multiple adjacent 'if's in listcomps and genexps, 915 # even though it's silly. Make sure it works (ifelse broke this.) 916 self.assertEqual([ x for x in range(10) if x % 2 if x % 3 ], [1, 5, 7]) 917 self.assertEqual(list(x for x in range(10) if x % 2 if x % 3), [1, 5, 7]) 918 919 # verify unpacking single element tuples in listcomp/genexp. 920 self.assertEqual([x for x, in [(4,), (5,), (6,)]], [4, 5, 6]) 921 self.assertEqual(list(x for x, in [(7,), (8,), (9,)]), [7, 8, 9]) 922 923 def test_with_statement(self): 924 class manager(object): 925 def __enter__(self): 926 return (1, 2) 927 def __exit__(self, *args): 928 pass 929 930 with manager(): 931 pass 932 with manager() as x: 933 pass 934 with manager() as (x, y): 935 pass 936 with manager(), manager(): 937 pass 938 with manager() as x, manager() as y: 939 pass 940 with manager() as x, manager(): 941 pass 942 943 def testIfElseExpr(self): 944 # Test ifelse expressions in various cases 945 def _checkeval(msg, ret): 946 "helper to check that evaluation of expressions is done correctly" 947 print x 948 return ret 949 950 self.assertEqual([ x() for x in lambda: True, lambda: False if x() ], [True]) 951 self.assertEqual([ x() for x in (lambda: True, lambda: False) if x() ], [True]) 952 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]) 953 self.assertEqual((5 if 1 else _checkeval("check 1", 0)), 5) 954 self.assertEqual((_checkeval("check 2", 0) if 0 else 5), 5) 955 self.assertEqual((5 and 6 if 0 else 1), 1) 956 self.assertEqual(((5 and 6) if 0 else 1), 1) 957 self.assertEqual((5 and (6 if 1 else 1)), 6) 958 self.assertEqual((0 or _checkeval("check 3", 2) if 0 else 3), 3) 959 self.assertEqual((1 or _checkeval("check 4", 2) if 1 else _checkeval("check 5", 3)), 1) 960 self.assertEqual((0 or 5 if 1 else _checkeval("check 6", 3)), 5) 961 self.assertEqual((not 5 if 1 else 1), False) 962 self.assertEqual((not 5 if 0 else 1), 1) 963 self.assertEqual((6 + 1 if 1 else 2), 7) 964 self.assertEqual((6 - 1 if 1 else 2), 5) 965 self.assertEqual((6 * 2 if 1 else 4), 12) 966 self.assertEqual((6 / 2 if 1 else 3), 3) 967 self.assertEqual((6 < 4 if 0 else 2), 2) 968 969 970def test_main(): 971 run_unittest(TokenTests, GrammarTests) 972 973if __name__ == '__main__': 974 test_main() 975