Changeset 391 for python/trunk/Lib/test/test_grammar.py
- Timestamp:
- Mar 19, 2014, 11:31:01 PM (11 years ago)
- Location:
- python/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
python/trunk
-
Property svn:mergeinfo
set to
/python/vendor/Python-2.7.6 merged eligible /python/vendor/current merged eligible
-
Property svn:mergeinfo
set to
-
python/trunk/Lib/test/test_grammar.py
r2 r391 2 2 # This just tests whether the parser accepts them all. 3 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 11 from test.test_support import run_unittest, check_syntax_error 4 from test.test_support import run_unittest, check_syntax_error, \ 5 check_py3k_warnings 12 6 import unittest 13 7 import sys … … 15 9 from sys import * 16 10 11 17 12 class TokenTests(unittest.TestCase): 18 13 … … 21 16 x = 1 \ 22 17 + 1 23 self.assertEqual s(x, 2, 'backslash for line continuation')18 self.assertEqual(x, 2, 'backslash for line continuation') 24 19 25 20 # Backslash does not means continuation in comments :\ 26 21 x = 0 27 self.assertEqual s(x, 0, 'backslash ending comment')22 self.assertEqual(x, 0, 'backslash ending comment') 28 23 29 24 def testPlainIntegers(self): 30 self.assertEqual s(0xff, 255)31 self.assertEqual s(0377, 255)32 self.assertEqual s(2147483647, 017777777777)25 self.assertEqual(0xff, 255) 26 self.assertEqual(0377, 255) 27 self.assertEqual(2147483647, 017777777777) 33 28 # "0x" is not a valid literal 34 29 self.assertRaises(SyntaxError, eval, "0x") 35 30 from sys import maxint 36 31 if maxint == 2147483647: 37 self.assertEqual s(-2147483647-1, -020000000000)32 self.assertEqual(-2147483647-1, -020000000000) 38 33 # XXX -2147483648 39 self.assert _(037777777777 > 0)40 self.assert _(0xffffffff > 0)34 self.assertTrue(037777777777 > 0) 35 self.assertTrue(0xffffffff > 0) 41 36 for s in '2147483648', '040000000000', '0x100000000': 42 37 try: … … 45 40 self.fail("OverflowError on huge integer literal %r" % s) 46 41 elif maxint == 9223372036854775807: 47 self.assertEqual s(-9223372036854775807-1, -01000000000000000000000)48 self.assert _(01777777777777777777777 > 0)49 self.assert _(0xffffffffffffffff > 0)42 self.assertEqual(-9223372036854775807-1, -01000000000000000000000) 43 self.assertTrue(01777777777777777777777 > 0) 44 self.assertTrue(0xffffffffffffffff > 0) 50 45 for s in '9223372036854775808', '02000000000000000000000', \ 51 46 '0x10000000000000000': … … 82 77 83 78 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)79 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) 87 82 x = "doesn't \"shrink\" does it" 88 83 y = 'doesn\'t "shrink" does it' 89 self.assert _(len(x) == 24 and x == y)84 self.assertTrue(len(x) == 24 and x == y) 90 85 x = "does \"shrink\" doesn't it" 91 86 y = 'does "shrink" doesn\'t it' 92 self.assert _(len(x) == 24 and x == y)87 self.assertTrue(len(x) == 24 and x == y) 93 88 x = """ 94 89 The "quick" … … 98 93 """ 99 94 y = '\nThe "quick"\nbrown fox\njumps over\nthe \'lazy\' dog.\n' 100 self.assertEqual s(x, y)95 self.assertEqual(x, y) 101 96 y = ''' 102 97 The "quick" … … 105 100 the 'lazy' dog. 106 101 ''' 107 self.assertEqual s(x, y)102 self.assertEqual(x, y) 108 103 y = "\n\ 109 104 The \"quick\"\n\ … … 112 107 the 'lazy' dog.\n\ 113 108 " 114 self.assertEqual s(x, y)109 self.assertEqual(x, y) 115 110 y = '\n\ 116 111 The \"quick\"\n\ … … 119 114 the \'lazy\' dog.\n\ 120 115 ' 121 self.assertEqual s(x, y)116 self.assertEqual(x, y) 122 117 123 118 … … 153 148 def f2(one_argument): pass 154 149 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')) 150 # Silence Py3k warning 151 exec('def f4(two, (compound, (argument, list))): pass') 152 exec('def f5((compound, first), two): pass') 153 self.assertEqual(f2.func_code.co_varnames, ('one_argument',)) 154 self.assertEqual(f3.func_code.co_varnames, ('two', 'arguments')) 159 155 if sys.platform.startswith('java'): 160 self.assertEqual s(f4.func_code.co_varnames,156 self.assertEqual(f4.func_code.co_varnames, 161 157 ('two', '(compound, (argument, list))', 'compound', 'argument', 162 158 'list',)) 163 self.assertEqual s(f5.func_code.co_varnames,159 self.assertEqual(f5.func_code.co_varnames, 164 160 ('(compound, first)', 'two', 'compound', 'first')) 165 161 else: 166 self.assertEqual s(f4.func_code.co_varnames,162 self.assertEqual(f4.func_code.co_varnames, 167 163 ('two', '.1', 'compound', 'argument', 'list')) 168 self.assertEqual s(f5.func_code.co_varnames,164 self.assertEqual(f5.func_code.co_varnames, 169 165 ('.0', 'two', 'compound', 'first')) 170 166 def a1(one_arg,): pass … … 173 169 def v1(a, *rest): pass 174 170 def v2(a, b, *rest): pass 175 def v3(a, (b, c), *rest): return a, b, c, rest 171 # Silence Py3k warning 172 exec('def v3(a, (b, c), *rest): return a, b, c, rest') 176 173 177 174 f1() … … 202 199 # thus, the names nested inside tuples must appear after these names. 203 200 if sys.platform.startswith('java'): 204 self.assertEqual s(v3.func_code.co_varnames, ('a', '(b, c)', 'rest', 'b', 'c'))201 self.assertEqual(v3.func_code.co_varnames, ('a', '(b, c)', 'rest', 'b', 'c')) 205 202 else: 206 self.assertEqual s(v3.func_code.co_varnames, ('a', '.1', 'rest', 'b', 'c'))207 self.assertEqual s(v3(1, (2, 3), 4), (1, 2, 3, (4,)))203 self.assertEqual(v3.func_code.co_varnames, ('a', '.1', 'rest', 'b', 'c')) 204 self.assertEqual(v3(1, (2, 3), 4), (1, 2, 3, (4,))) 208 205 def d01(a=1): pass 209 206 d01() … … 278 275 d22v(1, 2, *(3, 4, 5)) 279 276 d22v(1, *(2, 3), **{'d': 4}) 280 def d31v((x)): pass 277 # Silence Py3k warning 278 exec('def d31v((x)): pass') 279 exec('def d32v((x,)): pass') 281 280 d31v(1) 282 def d32v((x,)): pass283 281 d32v((1,)) 284 282 … … 286 284 def f(*args, **kwargs): 287 285 return args, kwargs 288 self.assertEqual s(f(1, x=2, *[3, 4], y=5), ((1, 3, 4),286 self.assertEqual(f(1, x=2, *[3, 4], y=5), ((1, 3, 4), 289 287 {'x':2, 'y':5})) 290 288 self.assertRaises(SyntaxError, eval, "f(1, *(2,3), 4)") … … 298 296 ### lambdef: 'lambda' [varargslist] ':' test 299 297 l1 = lambda : 0 300 self.assertEqual s(l1(), 0)298 self.assertEqual(l1(), 0) 301 299 l2 = lambda : a[d] # XXX just testing the expression 302 300 l3 = lambda : [2 < x for x in [-1, 3, 0L]] 303 self.assertEqual s(l3(), [0, 1, 0])301 self.assertEqual(l3(), [0, 1, 0]) 304 302 l4 = lambda x = lambda y = lambda z=1 : z : y() : x() 305 self.assertEqual s(l4(), 1)303 self.assertEqual(l4(), 1) 306 304 l5 = lambda x, y, z=2: x + y + z 307 self.assertEqual s(l5(1, 2), 5)308 self.assertEqual s(l5(1, 2, 3), 6)305 self.assertEqual(l5(1, 2), 5) 306 self.assertEqual(l5(1, 2, 3), 6) 309 307 check_syntax_error(self, "lambda x: x = 2") 310 308 check_syntax_error(self, "lambda (None,): None") … … 317 315 x = 1; pass; del x 318 316 def foo(): 319 # verify stat ments that end with semi-colons317 # verify statements that end with semi-colons 320 318 x = 1; pass; del x; 321 319 foo() … … 475 473 except: 476 474 raise 477 if count > 2 or big_hippo <>1:475 if count > 2 or big_hippo != 1: 478 476 self.fail("continue then break in try/except in loop broken!") 479 477 test_inner() … … 537 535 g = {} 538 536 exec 'z = 1' in g 539 if g.has_key('__builtins__'): del g['__builtins__']537 if '__builtins__' in g: del g['__builtins__'] 540 538 if g != {'z': 1}: self.fail('exec \'z = 1\' in g') 541 539 g = {} 542 540 l = {} 543 541 544 import warnings545 warnings.filterwarnings("ignore", "global statement", module="<string>")546 542 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__']543 if '__builtins__' in g: del g['__builtins__'] 544 if '__builtins__' in l: del l['__builtins__'] 549 545 if (g, l) != ({'a':1}, {'b':2}): 550 546 self.fail('exec ... in g (%s), l (%s)' %(g,l)) 551 547 552 548 def testAssert(self): 553 # assert _stmt: 'assert' test [',' test]549 # assertTruestmt: 'assert' test [',' test] 554 550 assert 1 555 551 assert 1, 1 556 552 assert lambda x:x 557 553 assert 1, lambda x:x+1 554 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): 558 569 try: 559 570 assert 0, "msg" 560 571 except AssertionError, e: 561 self.assertEqual s(e.args[0], "msg")572 self.assertEqual(e.args[0], "msg") 562 573 else: 563 if __debug__: 564 self.fail("AssertionError not raised by assert 0") 574 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 565 583 566 584 ### compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef … … 593 611 else: 594 612 x = 2 595 self.assertEqual s(x, 2)613 self.assertEqual(x, 2) 596 614 597 615 def testFor(self): … … 678 696 if 1 == 1: pass 679 697 if 1 != 1: pass 680 if 1 <> 1: pass681 698 if 1 < 1: pass 682 699 if 1 > 1: pass … … 687 704 if 1 in (): pass 688 705 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 706 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 690 710 691 711 def testBinaryMaskOps(self): … … 746 766 L = list(d) 747 767 L.sort() 748 self.assertEqual s(str(L), '[1, (1,), (1, 2), (1, 2, 3)]')768 self.assertEqual(str(L), '[1, (1,), (1, 2), (1, 2, 3)]') 749 769 750 770 def testAtoms(self): 751 771 ### atom: '(' [testlist] ')' | '[' [testlist] ']' | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING 752 ### dict maker: test ':' test (',' test ':' test)* [',']772 ### dictorsetmaker: (test ':' test (',' test ':' test)* [',']) | (test (',' test)* [',']) 753 773 754 774 x = (1) … … 770 790 x = {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5, 'six': 6} 771 791 772 x = `x` 773 x = `1 or 2 or 3` 774 self.assertEqual(`1,2`, '(1, 2)') 792 x = {'one'} 793 x = {'one', 1,} 794 x = {'one', 'two', 'three'} 795 x = {2, 3, 4,} 796 797 # Silence Py3k warning 798 x = eval('`x`') 799 x = eval('`1 or 2 or 3`') 800 self.assertEqual(eval('`1,2`'), '(1, 2)') 775 801 776 802 x = x … … 804 830 self.assertEqual(G.decorated, True) 805 831 832 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 806 839 def testListcomps(self): 807 840 # list comprehension tests … … 920 953 self.assertEqual([x for x, in [(4,), (5,), (6,)]], [4, 5, 6]) 921 954 self.assertEqual(list(x for x, in [(7,), (8,), (9,)]), [7, 8, 9]) 955 956 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 922 975 923 976 def testIfElseExpr(self): … … 947 1000 self.assertEqual((6 < 4 if 0 else 2), 2) 948 1001 1002 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 949 1010 950 1011 def test_main(): 951 run_unittest(TokenTests, GrammarTests) 1012 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) 952 1019 953 1020 if __name__ == '__main__':
Note:
See TracChangeset
for help on using the changeset viewer.