annotate Lib/test/test_grammar.py @ 54294:dba767bc3155 legacy-trunk

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