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