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 | |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 4 | from test.support import run_unittest, check_syntax_error |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5 | import unittest |
Jeremy Hylton | 7d3dff2 | 2001-10-10 01:45:02 +0000 | [diff] [blame] | 6 | import sys |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 7 | # testing import * |
| 8 | from sys import * |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 9 | |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 10 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 11 | class TokenTests(unittest.TestCase): |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 12 | |
Benjamin Peterson | c8507bf | 2011-05-30 10:52:48 -0500 | [diff] [blame] | 13 | def test_backslash(self): |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 14 | # Backslash means line continuation: |
| 15 | x = 1 \ |
| 16 | + 1 |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 17 | self.assertEqual(x, 2, 'backslash for line continuation') |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 18 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 19 | # Backslash does not means continuation in comments :\ |
| 20 | x = 0 |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 21 | self.assertEqual(x, 0, 'backslash ending comment') |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 22 | |
Benjamin Peterson | c8507bf | 2011-05-30 10:52:48 -0500 | [diff] [blame] | 23 | def test_plain_integers(self): |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 24 | self.assertEqual(type(000), type(0)) |
| 25 | self.assertEqual(0xff, 255) |
| 26 | self.assertEqual(0o377, 255) |
| 27 | self.assertEqual(2147483647, 0o17777777777) |
| 28 | self.assertEqual(0b1001, 9) |
Georg Brandl | fceab5a | 2008-01-19 20:08:23 +0000 | [diff] [blame] | 29 | # "0x" is not a valid literal |
| 30 | self.assertRaises(SyntaxError, eval, "0x") |
Christian Heimes | a37d4c6 | 2007-12-04 23:02:19 +0000 | [diff] [blame] | 31 | from sys import maxsize |
| 32 | if maxsize == 2147483647: |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 33 | self.assertEqual(-2147483647-1, -0o20000000000) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 34 | # XXX -2147483648 |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 35 | self.assertTrue(0o37777777777 > 0) |
| 36 | self.assertTrue(0xffffffff > 0) |
| 37 | self.assertTrue(0b1111111111111111111111111111111 > 0) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 38 | for s in ('2147483648', '0o40000000000', '0x100000000', |
| 39 | '0b10000000000000000000000000000000'): |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 40 | try: |
| 41 | x = eval(s) |
| 42 | except OverflowError: |
| 43 | self.fail("OverflowError on huge integer literal %r" % s) |
Christian Heimes | a37d4c6 | 2007-12-04 23:02:19 +0000 | [diff] [blame] | 44 | elif maxsize == 9223372036854775807: |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 45 | self.assertEqual(-9223372036854775807-1, -0o1000000000000000000000) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 46 | self.assertTrue(0o1777777777777777777777 > 0) |
| 47 | self.assertTrue(0xffffffffffffffff > 0) |
| 48 | self.assertTrue(0b11111111111111111111111111111111111111111111111111111111111111 > 0) |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 49 | for s in '9223372036854775808', '0o2000000000000000000000', \ |
| 50 | '0x10000000000000000', \ |
| 51 | '0b100000000000000000000000000000000000000000000000000000000000000': |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 52 | try: |
| 53 | x = eval(s) |
| 54 | except OverflowError: |
| 55 | self.fail("OverflowError on huge integer literal %r" % s) |
| 56 | else: |
Christian Heimes | a37d4c6 | 2007-12-04 23:02:19 +0000 | [diff] [blame] | 57 | self.fail('Weird maxsize value %r' % maxsize) |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 58 | |
Benjamin Peterson | c8507bf | 2011-05-30 10:52:48 -0500 | [diff] [blame] | 59 | def test_long_integers(self): |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 60 | x = 0 |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 61 | x = 0xffffffffffffffff |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 62 | x = 0Xffffffffffffffff |
| 63 | x = 0o77777777777777777 |
| 64 | x = 0O77777777777777777 |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 65 | x = 123456789012345678901234567890 |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 66 | x = 0b100000000000000000000000000000000000000000000000000000000000000000000 |
| 67 | x = 0B111111111111111111111111111111111111111111111111111111111111111111111 |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 68 | |
Benjamin Peterson | c8507bf | 2011-05-30 10:52:48 -0500 | [diff] [blame] | 69 | def test_floats(self): |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 70 | x = 3.14 |
| 71 | x = 314. |
| 72 | x = 0.314 |
| 73 | # XXX x = 000.314 |
| 74 | x = .314 |
| 75 | x = 3e14 |
| 76 | x = 3E14 |
| 77 | x = 3e-14 |
| 78 | x = 3e+14 |
| 79 | x = 3.e14 |
| 80 | x = .3e14 |
| 81 | x = 3.1e4 |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 82 | |
Benjamin Peterson | c416162 | 2014-06-07 12:36:39 -0700 | [diff] [blame] | 83 | def test_float_exponent_tokenization(self): |
| 84 | # See issue 21642. |
| 85 | self.assertEqual(1 if 1else 0, 1) |
| 86 | self.assertEqual(1 if 0else 0, 0) |
| 87 | self.assertRaises(SyntaxError, eval, "0 if 1Else 0") |
| 88 | |
Benjamin Peterson | c8507bf | 2011-05-30 10:52:48 -0500 | [diff] [blame] | 89 | def test_string_literals(self): |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 90 | x = ''; y = ""; self.assertTrue(len(x) == 0 and x == y) |
| 91 | x = '\''; y = "'"; self.assertTrue(len(x) == 1 and x == y and ord(x) == 39) |
| 92 | x = '"'; y = "\""; self.assertTrue(len(x) == 1 and x == y and ord(x) == 34) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 93 | x = "doesn't \"shrink\" does it" |
| 94 | y = 'doesn\'t "shrink" does it' |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 95 | self.assertTrue(len(x) == 24 and x == y) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 96 | x = "does \"shrink\" doesn't it" |
| 97 | y = 'does "shrink" doesn\'t it' |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 98 | self.assertTrue(len(x) == 24 and x == y) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 99 | x = """ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 100 | The "quick" |
| 101 | brown fox |
| 102 | jumps over |
| 103 | the 'lazy' dog. |
| 104 | """ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 105 | y = '\nThe "quick"\nbrown fox\njumps over\nthe \'lazy\' dog.\n' |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 106 | self.assertEqual(x, y) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 107 | y = ''' |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 108 | The "quick" |
| 109 | brown fox |
| 110 | jumps over |
| 111 | the 'lazy' dog. |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 112 | ''' |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 113 | self.assertEqual(x, y) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 114 | y = "\n\ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 115 | The \"quick\"\n\ |
| 116 | brown fox\n\ |
| 117 | jumps over\n\ |
| 118 | the 'lazy' dog.\n\ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 119 | " |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 120 | self.assertEqual(x, y) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 121 | y = '\n\ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 122 | The \"quick\"\n\ |
| 123 | brown fox\n\ |
| 124 | jumps over\n\ |
| 125 | the \'lazy\' dog.\n\ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 126 | ' |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 127 | self.assertEqual(x, y) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 128 | |
Benjamin Peterson | c8507bf | 2011-05-30 10:52:48 -0500 | [diff] [blame] | 129 | def test_ellipsis(self): |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 130 | x = ... |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 131 | self.assertTrue(x is Ellipsis) |
Georg Brandl | dde0028 | 2007-03-18 19:01:53 +0000 | [diff] [blame] | 132 | self.assertRaises(SyntaxError, eval, ".. .") |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 133 | |
Benjamin Peterson | 758888d | 2011-05-30 11:12:38 -0500 | [diff] [blame] | 134 | def test_eof_error(self): |
| 135 | samples = ("def foo(", "\ndef foo(", "def foo(\n") |
| 136 | for s in samples: |
| 137 | with self.assertRaises(SyntaxError) as cm: |
| 138 | compile(s, "<test>", "exec") |
| 139 | self.assertIn("unexpected EOF", str(cm.exception)) |
| 140 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 141 | class GrammarTests(unittest.TestCase): |
| 142 | |
| 143 | # single_input: NEWLINE | simple_stmt | compound_stmt NEWLINE |
| 144 | # XXX can't test in a script -- this rule is only used when interactive |
| 145 | |
| 146 | # file_input: (NEWLINE | stmt)* ENDMARKER |
| 147 | # Being tested as this very moment this very module |
| 148 | |
| 149 | # expr_input: testlist NEWLINE |
| 150 | # XXX Hard to test -- used only in calls to input() |
| 151 | |
Benjamin Peterson | c8507bf | 2011-05-30 10:52:48 -0500 | [diff] [blame] | 152 | def test_eval_input(self): |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 153 | # testlist ENDMARKER |
| 154 | x = eval('1, 0 or 1') |
| 155 | |
Benjamin Peterson | c8507bf | 2011-05-30 10:52:48 -0500 | [diff] [blame] | 156 | def test_funcdef(self): |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 157 | ### [decorators] 'def' NAME parameters ['->' test] ':' suite |
| 158 | ### decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE |
| 159 | ### decorators: decorator+ |
| 160 | ### parameters: '(' [typedargslist] ')' |
| 161 | ### typedargslist: ((tfpdef ['=' test] ',')* |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 162 | ### ('*' [tfpdef] (',' tfpdef ['=' test])* [',' '**' tfpdef] | '**' tfpdef) |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 163 | ### | tfpdef ['=' test] (',' tfpdef ['=' test])* [',']) |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 164 | ### tfpdef: NAME [':' test] |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 165 | ### varargslist: ((vfpdef ['=' test] ',')* |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 166 | ### ('*' [vfpdef] (',' vfpdef ['=' test])* [',' '**' vfpdef] | '**' vfpdef) |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 167 | ### | vfpdef ['=' test] (',' vfpdef ['=' test])* [',']) |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 168 | ### vfpdef: NAME |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 169 | def f1(): pass |
| 170 | f1() |
| 171 | f1(*()) |
| 172 | f1(*(), **{}) |
| 173 | def f2(one_argument): pass |
| 174 | def f3(two, arguments): pass |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 175 | self.assertEqual(f2.__code__.co_varnames, ('one_argument',)) |
| 176 | self.assertEqual(f3.__code__.co_varnames, ('two', 'arguments')) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 177 | def a1(one_arg,): pass |
| 178 | def a2(two, args,): pass |
| 179 | def v0(*rest): pass |
| 180 | def v1(a, *rest): pass |
| 181 | def v2(a, b, *rest): pass |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 182 | |
| 183 | f1() |
| 184 | f2(1) |
| 185 | f2(1,) |
| 186 | f3(1, 2) |
| 187 | f3(1, 2,) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 188 | v0() |
| 189 | v0(1) |
| 190 | v0(1,) |
| 191 | v0(1,2) |
| 192 | v0(1,2,3,4,5,6,7,8,9,0) |
| 193 | v1(1) |
| 194 | v1(1,) |
| 195 | v1(1,2) |
| 196 | v1(1,2,3) |
| 197 | v1(1,2,3,4,5,6,7,8,9,0) |
| 198 | v2(1,2) |
| 199 | v2(1,2,3) |
| 200 | v2(1,2,3,4) |
| 201 | v2(1,2,3,4,5,6,7,8,9,0) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 202 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 203 | def d01(a=1): pass |
| 204 | d01() |
| 205 | d01(1) |
| 206 | d01(*(1,)) |
| 207 | d01(**{'a':2}) |
| 208 | def d11(a, b=1): pass |
| 209 | d11(1) |
| 210 | d11(1, 2) |
| 211 | d11(1, **{'b':2}) |
| 212 | def d21(a, b, c=1): pass |
| 213 | d21(1, 2) |
| 214 | d21(1, 2, 3) |
| 215 | d21(*(1, 2, 3)) |
| 216 | d21(1, *(2, 3)) |
| 217 | d21(1, 2, *(3,)) |
| 218 | d21(1, 2, **{'c':3}) |
| 219 | def d02(a=1, b=2): pass |
| 220 | d02() |
| 221 | d02(1) |
| 222 | d02(1, 2) |
| 223 | d02(*(1, 2)) |
| 224 | d02(1, *(2,)) |
| 225 | d02(1, **{'b':2}) |
| 226 | d02(**{'a': 1, 'b': 2}) |
| 227 | def d12(a, b=1, c=2): pass |
| 228 | d12(1) |
| 229 | d12(1, 2) |
| 230 | d12(1, 2, 3) |
| 231 | def d22(a, b, c=1, d=2): pass |
| 232 | d22(1, 2) |
| 233 | d22(1, 2, 3) |
| 234 | d22(1, 2, 3, 4) |
| 235 | def d01v(a=1, *rest): pass |
| 236 | d01v() |
| 237 | d01v(1) |
| 238 | d01v(1, 2) |
| 239 | d01v(*(1, 2, 3, 4)) |
| 240 | d01v(*(1,)) |
| 241 | d01v(**{'a':2}) |
| 242 | def d11v(a, b=1, *rest): pass |
| 243 | d11v(1) |
| 244 | d11v(1, 2) |
| 245 | d11v(1, 2, 3) |
| 246 | def d21v(a, b, c=1, *rest): pass |
| 247 | d21v(1, 2) |
| 248 | d21v(1, 2, 3) |
| 249 | d21v(1, 2, 3, 4) |
| 250 | d21v(*(1, 2, 3, 4)) |
| 251 | d21v(1, 2, **{'c': 3}) |
| 252 | def d02v(a=1, b=2, *rest): pass |
| 253 | d02v() |
| 254 | d02v(1) |
| 255 | d02v(1, 2) |
| 256 | d02v(1, 2, 3) |
| 257 | d02v(1, *(2, 3, 4)) |
| 258 | d02v(**{'a': 1, 'b': 2}) |
| 259 | def d12v(a, b=1, c=2, *rest): pass |
| 260 | d12v(1) |
| 261 | d12v(1, 2) |
| 262 | d12v(1, 2, 3) |
| 263 | d12v(1, 2, 3, 4) |
| 264 | d12v(*(1, 2, 3, 4)) |
| 265 | d12v(1, 2, *(3, 4, 5)) |
| 266 | d12v(1, *(2,), **{'c': 3}) |
| 267 | def d22v(a, b, c=1, d=2, *rest): pass |
| 268 | d22v(1, 2) |
| 269 | d22v(1, 2, 3) |
| 270 | d22v(1, 2, 3, 4) |
| 271 | d22v(1, 2, 3, 4, 5) |
| 272 | d22v(*(1, 2, 3, 4)) |
| 273 | d22v(1, 2, *(3, 4, 5)) |
| 274 | d22v(1, *(2, 3), **{'d': 4}) |
Georg Brandl | d8b690f | 2008-05-16 17:28:50 +0000 | [diff] [blame] | 275 | |
| 276 | # keyword argument type tests |
| 277 | try: |
| 278 | str('x', **{b'foo':1 }) |
| 279 | except TypeError: |
| 280 | pass |
| 281 | else: |
| 282 | self.fail('Bytes should not work as keyword argument names') |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 283 | # keyword only argument tests |
| 284 | def pos0key1(*, key): return key |
| 285 | pos0key1(key=100) |
| 286 | def pos2key2(p1, p2, *, k1, k2=100): return p1,p2,k1,k2 |
| 287 | pos2key2(1, 2, k1=100) |
| 288 | pos2key2(1, 2, k1=100, k2=200) |
| 289 | pos2key2(1, 2, k2=100, k1=200) |
| 290 | def pos2key2dict(p1, p2, *, k1=100, k2, **kwarg): return p1,p2,k1,k2,kwarg |
| 291 | pos2key2dict(1,2,k2=100,tokwarg1=100,tokwarg2=200) |
| 292 | pos2key2dict(1,2,tokwarg1=100,tokwarg2=200, k2=100) |
| 293 | |
Benjamin Peterson | 2d735bc | 2008-08-19 20:57:10 +0000 | [diff] [blame] | 294 | # keyword arguments after *arglist |
| 295 | def f(*args, **kwargs): |
| 296 | return args, kwargs |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 297 | self.assertEqual(f(1, x=2, *[3, 4], y=5), ((1, 3, 4), |
Benjamin Peterson | 2d735bc | 2008-08-19 20:57:10 +0000 | [diff] [blame] | 298 | {'x':2, 'y':5})) |
| 299 | self.assertRaises(SyntaxError, eval, "f(1, *(2,3), 4)") |
| 300 | self.assertRaises(SyntaxError, eval, "f(1, x=2, *(3,4), x=5)") |
| 301 | |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 302 | # argument annotation tests |
| 303 | def f(x) -> list: pass |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 304 | self.assertEqual(f.__annotations__, {'return': list}) |
Zachary Ware | ce17f76 | 2015-08-01 21:55:36 -0500 | [diff] [blame^] | 305 | def f(x: int): pass |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 306 | self.assertEqual(f.__annotations__, {'x': int}) |
Zachary Ware | ce17f76 | 2015-08-01 21:55:36 -0500 | [diff] [blame^] | 307 | def f(*x: str): pass |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 308 | self.assertEqual(f.__annotations__, {'x': str}) |
Zachary Ware | ce17f76 | 2015-08-01 21:55:36 -0500 | [diff] [blame^] | 309 | def f(**x: float): pass |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 310 | self.assertEqual(f.__annotations__, {'x': float}) |
Zachary Ware | ce17f76 | 2015-08-01 21:55:36 -0500 | [diff] [blame^] | 311 | def f(x, y: 1+2): pass |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 312 | self.assertEqual(f.__annotations__, {'y': 3}) |
Zachary Ware | ce17f76 | 2015-08-01 21:55:36 -0500 | [diff] [blame^] | 313 | def f(a, b: 1, c: 2, d): pass |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 314 | self.assertEqual(f.__annotations__, {'b': 1, 'c': 2}) |
Zachary Ware | ce17f76 | 2015-08-01 21:55:36 -0500 | [diff] [blame^] | 315 | def f(a, b: 1, c: 2, d, e: 3 = 4, f=5, *g: 6): pass |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 316 | self.assertEqual(f.__annotations__, |
Zachary Ware | ce17f76 | 2015-08-01 21:55:36 -0500 | [diff] [blame^] | 317 | {'b': 1, 'c': 2, 'e': 3, 'g': 6}) |
| 318 | def f(a, b: 1, c: 2, d, e: 3 = 4, f=5, *g: 6, h: 7, i=8, j: 9 = 10, |
| 319 | **k: 11) -> 12: pass |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 320 | self.assertEqual(f.__annotations__, |
Zachary Ware | ce17f76 | 2015-08-01 21:55:36 -0500 | [diff] [blame^] | 321 | {'b': 1, 'c': 2, 'e': 3, 'g': 6, 'h': 7, 'j': 9, |
| 322 | 'k': 11, 'return': 12}) |
Yury Selivanov | 34ce99f | 2014-02-18 12:49:41 -0500 | [diff] [blame] | 323 | # Check for issue #20625 -- annotations mangling |
| 324 | class Spam: |
Zachary Ware | ce17f76 | 2015-08-01 21:55:36 -0500 | [diff] [blame^] | 325 | def f(self, *, __kw: 1): |
Yury Selivanov | 34ce99f | 2014-02-18 12:49:41 -0500 | [diff] [blame] | 326 | pass |
| 327 | class Ham(Spam): pass |
Benjamin Peterson | bcfcfc5 | 2014-03-09 20:59:24 -0500 | [diff] [blame] | 328 | self.assertEqual(Spam.f.__annotations__, {'_Spam__kw': 1}) |
| 329 | self.assertEqual(Ham.f.__annotations__, {'_Spam__kw': 1}) |
Nick Coghlan | 71011e2 | 2007-04-23 11:05:01 +0000 | [diff] [blame] | 330 | # Check for SF Bug #1697248 - mixing decorators and a return annotation |
| 331 | def null(x): return x |
| 332 | @null |
| 333 | def f(x) -> list: pass |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 334 | self.assertEqual(f.__annotations__, {'return': list}) |
Nick Coghlan | 71011e2 | 2007-04-23 11:05:01 +0000 | [diff] [blame] | 335 | |
Guido van Rossum | 0240b92 | 2007-02-26 21:23:50 +0000 | [diff] [blame] | 336 | # test MAKE_CLOSURE with a variety of oparg's |
| 337 | closure = 1 |
| 338 | def f(): return closure |
| 339 | def f(x=1): return closure |
| 340 | def f(*, k=1): return closure |
| 341 | def f() -> int: return closure |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 342 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 343 | # Check ast errors in *args and *kwargs |
| 344 | check_syntax_error(self, "f(*g(1=2))") |
| 345 | check_syntax_error(self, "f(**g(1=2))") |
| 346 | |
Benjamin Peterson | c8507bf | 2011-05-30 10:52:48 -0500 | [diff] [blame] | 347 | def test_lambdef(self): |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 348 | ### lambdef: 'lambda' [varargslist] ':' test |
| 349 | l1 = lambda : 0 |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 350 | self.assertEqual(l1(), 0) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 351 | l2 = lambda : a[d] # XXX just testing the expression |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 352 | l3 = lambda : [2 < x for x in [-1, 3, 0]] |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 353 | self.assertEqual(l3(), [0, 1, 0]) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 354 | l4 = lambda x = lambda y = lambda z=1 : z : y() : x() |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 355 | self.assertEqual(l4(), 1) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 356 | l5 = lambda x, y, z=2: x + y + z |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 357 | self.assertEqual(l5(1, 2), 5) |
| 358 | self.assertEqual(l5(1, 2, 3), 6) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 359 | check_syntax_error(self, "lambda x: x = 2") |
Amaury Forgeot d'Arc | 35c8658 | 2008-06-17 21:11:29 +0000 | [diff] [blame] | 360 | check_syntax_error(self, "lambda (None,): None") |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 361 | l6 = lambda x, y, *, k=20: x+y+k |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 362 | self.assertEqual(l6(1,2), 1+2+20) |
| 363 | self.assertEqual(l6(1,2,k=10), 1+2+10) |
Guido van Rossum | b31c7f7 | 1993-11-11 10:31:23 +0000 | [diff] [blame] | 364 | |
| 365 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 366 | ### stmt: simple_stmt | compound_stmt |
| 367 | # Tested below |
Georg Brandl | 52318d6 | 2006-09-06 07:06:08 +0000 | [diff] [blame] | 368 | |
Benjamin Peterson | c8507bf | 2011-05-30 10:52:48 -0500 | [diff] [blame] | 369 | def test_simple_stmt(self): |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 370 | ### simple_stmt: small_stmt (';' small_stmt)* [';'] |
| 371 | x = 1; pass; del x |
| 372 | def foo(): |
Ezio Melotti | 1392500 | 2011-03-16 11:05:33 +0200 | [diff] [blame] | 373 | # verify statements that end with semi-colons |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 374 | x = 1; pass; del x; |
| 375 | foo() |
Georg Brandl | 52318d6 | 2006-09-06 07:06:08 +0000 | [diff] [blame] | 376 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 377 | ### small_stmt: expr_stmt | pass_stmt | del_stmt | flow_stmt | import_stmt | global_stmt | access_stmt |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 378 | # Tested below |
Georg Brandl | 52318d6 | 2006-09-06 07:06:08 +0000 | [diff] [blame] | 379 | |
Benjamin Peterson | c8507bf | 2011-05-30 10:52:48 -0500 | [diff] [blame] | 380 | def test_expr_stmt(self): |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 381 | # (exprlist '=')* exprlist |
| 382 | 1 |
| 383 | 1, 2, 3 |
| 384 | x = 1 |
| 385 | x = 1, 2, 3 |
| 386 | x = y = z = 1, 2, 3 |
| 387 | x, y, z = 1, 2, 3 |
| 388 | 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] | 389 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 390 | check_syntax_error(self, "x + 1 = 1") |
| 391 | check_syntax_error(self, "a + 1 = b + 2") |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 392 | |
Nick Coghlan | 5b1fdc1 | 2014-06-16 19:48:02 +1000 | [diff] [blame] | 393 | # Check the heuristic for print & exec covers significant cases |
| 394 | # As well as placing some limits on false positives |
| 395 | def test_former_statements_refer_to_builtins(self): |
| 396 | keywords = "print", "exec" |
| 397 | # Cases where we want the custom error |
| 398 | cases = [ |
| 399 | "{} foo", |
| 400 | "{} {{1:foo}}", |
| 401 | "if 1: {} foo", |
| 402 | "if 1: {} {{1:foo}}", |
| 403 | "if 1:\n {} foo", |
| 404 | "if 1:\n {} {{1:foo}}", |
| 405 | ] |
| 406 | for keyword in keywords: |
| 407 | custom_msg = "call to '{}'".format(keyword) |
| 408 | for case in cases: |
| 409 | source = case.format(keyword) |
| 410 | with self.subTest(source=source): |
| 411 | with self.assertRaisesRegex(SyntaxError, custom_msg): |
| 412 | exec(source) |
| 413 | source = source.replace("foo", "(foo.)") |
| 414 | with self.subTest(source=source): |
| 415 | with self.assertRaisesRegex(SyntaxError, "invalid syntax"): |
| 416 | exec(source) |
| 417 | |
Benjamin Peterson | c8507bf | 2011-05-30 10:52:48 -0500 | [diff] [blame] | 418 | def test_del_stmt(self): |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 419 | # 'del' exprlist |
| 420 | abc = [1,2,3] |
| 421 | x, y, z = abc |
| 422 | xyz = x, y, z |
Barry Warsaw | 7e3e1c1 | 2000-10-11 21:26:03 +0000 | [diff] [blame] | 423 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 424 | del abc |
| 425 | del x, y, (z, xyz) |
Barry Warsaw | 9182b45 | 2000-08-29 04:57:10 +0000 | [diff] [blame] | 426 | |
Benjamin Peterson | c8507bf | 2011-05-30 10:52:48 -0500 | [diff] [blame] | 427 | def test_pass_stmt(self): |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 428 | # 'pass' |
| 429 | pass |
Barry Warsaw | 9182b45 | 2000-08-29 04:57:10 +0000 | [diff] [blame] | 430 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 431 | # flow_stmt: break_stmt | continue_stmt | return_stmt | raise_stmt |
| 432 | # Tested below |
Barry Warsaw | 9182b45 | 2000-08-29 04:57:10 +0000 | [diff] [blame] | 433 | |
Benjamin Peterson | c8507bf | 2011-05-30 10:52:48 -0500 | [diff] [blame] | 434 | def test_break_stmt(self): |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 435 | # 'break' |
| 436 | while 1: break |
Barry Warsaw | 9182b45 | 2000-08-29 04:57:10 +0000 | [diff] [blame] | 437 | |
Benjamin Peterson | c8507bf | 2011-05-30 10:52:48 -0500 | [diff] [blame] | 438 | def test_continue_stmt(self): |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 439 | # 'continue' |
| 440 | i = 1 |
| 441 | while i: i = 0; continue |
Barry Warsaw | 9182b45 | 2000-08-29 04:57:10 +0000 | [diff] [blame] | 442 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 443 | msg = "" |
| 444 | while not msg: |
| 445 | msg = "ok" |
| 446 | try: |
| 447 | continue |
| 448 | msg = "continue failed to continue inside try" |
| 449 | except: |
| 450 | msg = "continue inside try called except block" |
| 451 | if msg != "ok": |
| 452 | self.fail(msg) |
Barry Warsaw | efc92ee | 2000-08-21 15:46:50 +0000 | [diff] [blame] | 453 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 454 | msg = "" |
| 455 | while not msg: |
| 456 | msg = "finally block not called" |
| 457 | try: |
| 458 | continue |
| 459 | finally: |
| 460 | msg = "ok" |
| 461 | if msg != "ok": |
| 462 | self.fail(msg) |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 463 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 464 | def test_break_continue_loop(self): |
| 465 | # This test warrants an explanation. It is a test specifically for SF bugs |
| 466 | # #463359 and #462937. The bug is that a 'break' statement executed or |
| 467 | # exception raised inside a try/except inside a loop, *after* a continue |
| 468 | # statement has been executed in that loop, will cause the wrong number of |
| 469 | # arguments to be popped off the stack and the instruction pointer reset to |
| 470 | # a very small number (usually 0.) Because of this, the following test |
| 471 | # *must* written as a function, and the tracking vars *must* be function |
| 472 | # arguments with default values. Otherwise, the test will loop and loop. |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 473 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 474 | def test_inner(extra_burning_oil = 1, count=0): |
| 475 | big_hippo = 2 |
| 476 | while big_hippo: |
| 477 | count += 1 |
| 478 | try: |
| 479 | if extra_burning_oil and big_hippo == 1: |
| 480 | extra_burning_oil -= 1 |
| 481 | break |
| 482 | big_hippo -= 1 |
| 483 | continue |
| 484 | except: |
| 485 | raise |
| 486 | if count > 2 or big_hippo != 1: |
| 487 | self.fail("continue then break in try/except in loop broken!") |
| 488 | test_inner() |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 489 | |
Benjamin Peterson | c8507bf | 2011-05-30 10:52:48 -0500 | [diff] [blame] | 490 | def test_return(self): |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 491 | # 'return' [testlist] |
| 492 | def g1(): return |
| 493 | def g2(): return 1 |
| 494 | g1() |
| 495 | x = g2() |
| 496 | check_syntax_error(self, "class foo:return 1") |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 497 | |
Benjamin Peterson | c8507bf | 2011-05-30 10:52:48 -0500 | [diff] [blame] | 498 | def test_yield(self): |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 499 | # Allowed as standalone statement |
| 500 | def g(): yield 1 |
| 501 | def g(): yield from () |
| 502 | # Allowed as RHS of assignment |
| 503 | def g(): x = yield 1 |
| 504 | def g(): x = yield from () |
| 505 | # Ordinary yield accepts implicit tuples |
| 506 | def g(): yield 1, 1 |
| 507 | def g(): x = yield 1, 1 |
| 508 | # 'yield from' does not |
| 509 | check_syntax_error(self, "def g(): yield from (), 1") |
| 510 | check_syntax_error(self, "def g(): x = yield from (), 1") |
| 511 | # Requires parentheses as subexpression |
| 512 | def g(): 1, (yield 1) |
| 513 | def g(): 1, (yield from ()) |
| 514 | check_syntax_error(self, "def g(): 1, yield 1") |
| 515 | check_syntax_error(self, "def g(): 1, yield from ()") |
| 516 | # Requires parentheses as call argument |
| 517 | def g(): f((yield 1)) |
| 518 | def g(): f((yield 1), 1) |
| 519 | def g(): f((yield from ())) |
| 520 | def g(): f((yield from ()), 1) |
| 521 | check_syntax_error(self, "def g(): f(yield 1)") |
| 522 | check_syntax_error(self, "def g(): f(yield 1, 1)") |
| 523 | check_syntax_error(self, "def g(): f(yield from ())") |
| 524 | check_syntax_error(self, "def g(): f(yield from (), 1)") |
| 525 | # Not allowed at top level |
| 526 | check_syntax_error(self, "yield") |
| 527 | check_syntax_error(self, "yield from") |
| 528 | # Not allowed at class scope |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 529 | check_syntax_error(self, "class foo:yield 1") |
Nick Coghlan | 1f7ce62 | 2012-01-13 21:43:40 +1000 | [diff] [blame] | 530 | check_syntax_error(self, "class foo:yield from ()") |
| 531 | |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 532 | |
Benjamin Peterson | c8507bf | 2011-05-30 10:52:48 -0500 | [diff] [blame] | 533 | def test_raise(self): |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 534 | # 'raise' test [',' test] |
Collin Winter | 828f04a | 2007-08-31 00:04:24 +0000 | [diff] [blame] | 535 | try: raise RuntimeError('just testing') |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 536 | except RuntimeError: pass |
| 537 | try: raise KeyboardInterrupt |
| 538 | except KeyboardInterrupt: pass |
Jeremy Hylton | 3faa52e | 2001-02-01 22:48:12 +0000 | [diff] [blame] | 539 | |
Benjamin Peterson | c8507bf | 2011-05-30 10:52:48 -0500 | [diff] [blame] | 540 | def test_import(self): |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 541 | # 'import' dotted_as_names |
| 542 | import sys |
| 543 | import time, sys |
| 544 | # 'from' dotted_name 'import' ('*' | '(' import_as_names ')' | import_as_names) |
| 545 | from time import time |
| 546 | from time import (time) |
| 547 | # not testable inside a function, but already done at top of the module |
| 548 | # from sys import * |
| 549 | from sys import path, argv |
| 550 | from sys import (path, argv) |
| 551 | from sys import (path, argv,) |
Tim Peters | 10fb386 | 2001-02-09 20:17:14 +0000 | [diff] [blame] | 552 | |
Benjamin Peterson | c8507bf | 2011-05-30 10:52:48 -0500 | [diff] [blame] | 553 | def test_global(self): |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 554 | # 'global' NAME (',' NAME)* |
| 555 | global a |
| 556 | global a, b |
| 557 | global one, two, three, four, five, six, seven, eight, nine, ten |
Thomas Wouters | 80d373c | 2001-09-26 12:43:39 +0000 | [diff] [blame] | 558 | |
Benjamin Peterson | c8507bf | 2011-05-30 10:52:48 -0500 | [diff] [blame] | 559 | def test_nonlocal(self): |
Benjamin Peterson | a933e52 | 2008-10-24 22:16:39 +0000 | [diff] [blame] | 560 | # 'nonlocal' NAME (',' NAME)* |
| 561 | x = 0 |
| 562 | y = 0 |
| 563 | def f(): |
| 564 | nonlocal x |
| 565 | nonlocal x, y |
| 566 | |
Benjamin Peterson | c8507bf | 2011-05-30 10:52:48 -0500 | [diff] [blame] | 567 | def test_assert(self): |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 568 | # assertTruestmt: 'assert' test [',' test] |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 569 | assert 1 |
| 570 | assert 1, 1 |
| 571 | assert lambda x:x |
| 572 | assert 1, lambda x:x+1 |
Ezio Melotti | 6cc5bf7 | 2011-12-02 18:22:52 +0200 | [diff] [blame] | 573 | |
| 574 | try: |
| 575 | assert True |
| 576 | except AssertionError as e: |
| 577 | self.fail("'assert True' should not have raised an AssertionError") |
| 578 | |
| 579 | try: |
| 580 | assert True, 'this should always pass' |
| 581 | except AssertionError as e: |
| 582 | self.fail("'assert True, msg' should not have " |
| 583 | "raised an AssertionError") |
| 584 | |
| 585 | # these tests fail if python is run with -O, so check __debug__ |
| 586 | @unittest.skipUnless(__debug__, "Won't work if __debug__ is False") |
| 587 | def testAssert2(self): |
Thomas Wouters | 80d373c | 2001-09-26 12:43:39 +0000 | [diff] [blame] | 588 | try: |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 589 | assert 0, "msg" |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 590 | except AssertionError as e: |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 591 | self.assertEqual(e.args[0], "msg") |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 592 | else: |
Ezio Melotti | 6cc5bf7 | 2011-12-02 18:22:52 +0200 | [diff] [blame] | 593 | self.fail("AssertionError not raised by assert 0") |
| 594 | |
| 595 | try: |
| 596 | assert False |
| 597 | except AssertionError as e: |
| 598 | self.assertEqual(len(e.args), 0) |
| 599 | else: |
| 600 | self.fail("AssertionError not raised by 'assert False'") |
| 601 | |
Thomas Wouters | 80d373c | 2001-09-26 12:43:39 +0000 | [diff] [blame] | 602 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 603 | ### compound_stmt: if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef |
| 604 | # Tested below |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 605 | |
Benjamin Peterson | c8507bf | 2011-05-30 10:52:48 -0500 | [diff] [blame] | 606 | def test_if(self): |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 607 | # 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite] |
| 608 | if 1: pass |
| 609 | if 1: pass |
| 610 | else: pass |
| 611 | if 0: pass |
| 612 | elif 0: pass |
| 613 | if 0: pass |
| 614 | elif 0: pass |
| 615 | elif 0: pass |
| 616 | elif 0: pass |
| 617 | else: pass |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 618 | |
Benjamin Peterson | c8507bf | 2011-05-30 10:52:48 -0500 | [diff] [blame] | 619 | def test_while(self): |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 620 | # 'while' test ':' suite ['else' ':' suite] |
| 621 | while 0: pass |
| 622 | while 0: pass |
| 623 | else: pass |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 624 | |
Christian Heimes | 969fe57 | 2008-01-25 11:23:10 +0000 | [diff] [blame] | 625 | # Issue1920: "while 0" is optimized away, |
| 626 | # ensure that the "else" clause is still present. |
| 627 | x = 0 |
| 628 | while 0: |
| 629 | x = 1 |
| 630 | else: |
| 631 | x = 2 |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 632 | self.assertEqual(x, 2) |
Christian Heimes | 969fe57 | 2008-01-25 11:23:10 +0000 | [diff] [blame] | 633 | |
Benjamin Peterson | c8507bf | 2011-05-30 10:52:48 -0500 | [diff] [blame] | 634 | def test_for(self): |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 635 | # 'for' exprlist 'in' exprlist ':' suite ['else' ':' suite] |
| 636 | for i in 1, 2, 3: pass |
| 637 | for i, j, k in (): pass |
| 638 | else: pass |
| 639 | class Squares: |
| 640 | def __init__(self, max): |
| 641 | self.max = max |
| 642 | self.sofar = [] |
| 643 | def __len__(self): return len(self.sofar) |
| 644 | def __getitem__(self, i): |
| 645 | if not 0 <= i < self.max: raise IndexError |
| 646 | n = len(self.sofar) |
| 647 | while n <= i: |
| 648 | self.sofar.append(n*n) |
| 649 | n = n+1 |
| 650 | return self.sofar[i] |
| 651 | n = 0 |
| 652 | for x in Squares(10): n = n+x |
| 653 | if n != 285: |
| 654 | self.fail('for over growing sequence') |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 655 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 656 | result = [] |
| 657 | for x, in [(1,), (2,), (3,)]: |
| 658 | result.append(x) |
| 659 | self.assertEqual(result, [1, 2, 3]) |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 660 | |
Benjamin Peterson | c8507bf | 2011-05-30 10:52:48 -0500 | [diff] [blame] | 661 | def test_try(self): |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 662 | ### try_stmt: 'try' ':' suite (except_clause ':' suite)+ ['else' ':' suite] |
| 663 | ### | 'try' ':' suite 'finally' ':' suite |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 664 | ### except_clause: 'except' [expr ['as' expr]] |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 665 | try: |
| 666 | 1/0 |
| 667 | except ZeroDivisionError: |
| 668 | pass |
| 669 | else: |
| 670 | pass |
| 671 | try: 1/0 |
| 672 | except EOFError: pass |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 673 | except TypeError as msg: pass |
| 674 | except RuntimeError as msg: pass |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 675 | except: pass |
| 676 | else: pass |
| 677 | try: 1/0 |
| 678 | except (EOFError, TypeError, ZeroDivisionError): pass |
| 679 | try: 1/0 |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 680 | except (EOFError, TypeError, ZeroDivisionError) as msg: pass |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 681 | try: pass |
| 682 | finally: pass |
Jeremy Hylton | f828e2d | 2001-02-19 15:54:52 +0000 | [diff] [blame] | 683 | |
Benjamin Peterson | c8507bf | 2011-05-30 10:52:48 -0500 | [diff] [blame] | 684 | def test_suite(self): |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 685 | # simple_stmt | NEWLINE INDENT NEWLINE* (stmt NEWLINE*)+ DEDENT |
| 686 | if 1: pass |
| 687 | if 1: |
| 688 | pass |
| 689 | if 1: |
| 690 | # |
| 691 | # |
| 692 | # |
| 693 | pass |
| 694 | pass |
| 695 | # |
| 696 | pass |
| 697 | # |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 698 | |
Benjamin Peterson | c8507bf | 2011-05-30 10:52:48 -0500 | [diff] [blame] | 699 | def test_test(self): |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 700 | ### and_test ('or' and_test)* |
| 701 | ### and_test: not_test ('and' not_test)* |
| 702 | ### not_test: 'not' not_test | comparison |
| 703 | if not 1: pass |
| 704 | if 1 and 1: pass |
| 705 | if 1 or 1: pass |
| 706 | if not not not 1: pass |
| 707 | if not 1 and 1 and 1: pass |
| 708 | if 1 and 1 or 1 and 1 and 1 or not 1 and 1: pass |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 709 | |
Benjamin Peterson | c8507bf | 2011-05-30 10:52:48 -0500 | [diff] [blame] | 710 | def test_comparison(self): |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 711 | ### comparison: expr (comp_op expr)* |
| 712 | ### comp_op: '<'|'>'|'=='|'>='|'<='|'!='|'in'|'not' 'in'|'is'|'is' 'not' |
| 713 | if 1: pass |
| 714 | x = (1 == 1) |
| 715 | if 1 == 1: pass |
| 716 | if 1 != 1: pass |
| 717 | if 1 < 1: pass |
| 718 | if 1 > 1: pass |
| 719 | if 1 <= 1: pass |
| 720 | if 1 >= 1: pass |
| 721 | if 1 is 1: pass |
| 722 | if 1 is not 1: pass |
| 723 | if 1 in (): pass |
| 724 | if 1 not in (): pass |
| 725 | if 1 < 1 > 1 == 1 >= 1 <= 1 != 1 in 1 not in 1 is 1 is not 1: pass |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 726 | |
Benjamin Peterson | c8507bf | 2011-05-30 10:52:48 -0500 | [diff] [blame] | 727 | def test_binary_mask_ops(self): |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 728 | x = 1 & 1 |
| 729 | x = 1 ^ 1 |
| 730 | x = 1 | 1 |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 731 | |
Benjamin Peterson | c8507bf | 2011-05-30 10:52:48 -0500 | [diff] [blame] | 732 | def test_shift_ops(self): |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 733 | x = 1 << 1 |
| 734 | x = 1 >> 1 |
| 735 | x = 1 << 1 >> 1 |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 736 | |
Benjamin Peterson | c8507bf | 2011-05-30 10:52:48 -0500 | [diff] [blame] | 737 | def test_additive_ops(self): |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 738 | x = 1 |
| 739 | x = 1 + 1 |
| 740 | x = 1 - 1 - 1 |
| 741 | x = 1 - 1 + 1 - 1 + 1 |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 742 | |
Benjamin Peterson | c8507bf | 2011-05-30 10:52:48 -0500 | [diff] [blame] | 743 | def test_multiplicative_ops(self): |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 744 | x = 1 * 1 |
| 745 | x = 1 / 1 |
| 746 | x = 1 % 1 |
| 747 | x = 1 / 1 * 1 % 1 |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 748 | |
Benjamin Peterson | c8507bf | 2011-05-30 10:52:48 -0500 | [diff] [blame] | 749 | def test_unary_ops(self): |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 750 | x = +1 |
| 751 | x = -1 |
| 752 | x = ~1 |
| 753 | x = ~1 ^ 1 & 1 | 1 & 1 ^ -1 |
| 754 | x = -1*1/1 + 1*1 - ---1*1 |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 755 | |
Benjamin Peterson | c8507bf | 2011-05-30 10:52:48 -0500 | [diff] [blame] | 756 | def test_selectors(self): |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 757 | ### trailer: '(' [testlist] ')' | '[' subscript ']' | '.' NAME |
| 758 | ### subscript: expr | [expr] ':' [expr] |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 759 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 760 | import sys, time |
| 761 | c = sys.path[0] |
| 762 | x = time.time() |
| 763 | x = sys.modules['time'].time() |
| 764 | a = '01234' |
| 765 | c = a[0] |
| 766 | c = a[-1] |
| 767 | s = a[0:5] |
| 768 | s = a[:5] |
| 769 | s = a[0:] |
| 770 | s = a[:] |
| 771 | s = a[-5:] |
| 772 | s = a[:-1] |
| 773 | s = a[-4:-3] |
| 774 | # A rough test of SF bug 1333982. http://python.org/sf/1333982 |
| 775 | # The testing here is fairly incomplete. |
| 776 | # Test cases should include: commas with 1 and 2 colons |
| 777 | d = {} |
| 778 | d[1] = 1 |
| 779 | d[1,] = 2 |
| 780 | d[1,2] = 3 |
| 781 | d[1,2,3] = 4 |
| 782 | L = list(d) |
| 783 | L.sort(key=lambda x: x if isinstance(x, tuple) else ()) |
Florent Xicluna | 9b86b9a | 2010-03-19 19:00:44 +0000 | [diff] [blame] | 784 | self.assertEqual(str(L), '[1, (1,), (1, 2), (1, 2, 3)]') |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 785 | |
Benjamin Peterson | c8507bf | 2011-05-30 10:52:48 -0500 | [diff] [blame] | 786 | def test_atoms(self): |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 787 | ### atom: '(' [testlist] ')' | '[' [testlist] ']' | '{' [dictsetmaker] '}' | NAME | NUMBER | STRING |
| 788 | ### dictsetmaker: (test ':' test (',' test ':' test)* [',']) | (test (',' test)* [',']) |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 789 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 790 | x = (1) |
| 791 | x = (1 or 2 or 3) |
| 792 | x = (1 or 2 or 3, 2, 3) |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 793 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 794 | x = [] |
| 795 | x = [1] |
| 796 | x = [1 or 2 or 3] |
| 797 | x = [1 or 2 or 3, 2, 3] |
| 798 | x = [] |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 799 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 800 | x = {} |
| 801 | x = {'one': 1} |
| 802 | x = {'one': 1,} |
| 803 | x = {'one' or 'two': 1 or 2} |
| 804 | x = {'one': 1, 'two': 2} |
| 805 | x = {'one': 1, 'two': 2,} |
| 806 | x = {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5, 'six': 6} |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 807 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 808 | x = {'one'} |
| 809 | x = {'one', 1,} |
| 810 | x = {'one', 'two', 'three'} |
| 811 | x = {2, 3, 4,} |
| 812 | |
| 813 | x = x |
| 814 | x = 'x' |
| 815 | x = 123 |
| 816 | |
| 817 | ### exprlist: expr (',' expr)* [','] |
| 818 | ### testlist: test (',' test)* [','] |
| 819 | # These have been exercised enough above |
| 820 | |
Benjamin Peterson | c8507bf | 2011-05-30 10:52:48 -0500 | [diff] [blame] | 821 | def test_classdef(self): |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 822 | # 'class' NAME ['(' [testlist] ')'] ':' suite |
| 823 | class B: pass |
| 824 | class B2(): pass |
| 825 | class C1(B): pass |
| 826 | class C2(B): pass |
| 827 | class D(C1, C2, B): pass |
| 828 | class C: |
| 829 | def meth1(self): pass |
| 830 | def meth2(self, arg): pass |
| 831 | def meth3(self, a1, a2): pass |
| 832 | |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 833 | # decorator: '@' dotted_name [ '(' [arglist] ')' ] NEWLINE |
| 834 | # decorators: decorator+ |
| 835 | # decorated: decorators (classdef | funcdef) |
| 836 | def class_decorator(x): return x |
| 837 | @class_decorator |
| 838 | class G: pass |
| 839 | |
Benjamin Peterson | c8507bf | 2011-05-30 10:52:48 -0500 | [diff] [blame] | 840 | def test_dictcomps(self): |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 841 | # dictorsetmaker: ( (test ':' test (comp_for | |
| 842 | # (',' test ':' test)* [','])) | |
| 843 | # (test (comp_for | (',' test)* [','])) ) |
| 844 | nums = [1, 2, 3] |
| 845 | self.assertEqual({i:i+1 for i in nums}, {1: 2, 2: 3, 3: 4}) |
| 846 | |
Benjamin Peterson | c8507bf | 2011-05-30 10:52:48 -0500 | [diff] [blame] | 847 | def test_listcomps(self): |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 848 | # list comprehension tests |
| 849 | nums = [1, 2, 3, 4, 5] |
| 850 | strs = ["Apple", "Banana", "Coconut"] |
| 851 | spcs = [" Apple", " Banana ", "Coco nut "] |
| 852 | |
| 853 | self.assertEqual([s.strip() for s in spcs], ['Apple', 'Banana', 'Coco nut']) |
| 854 | self.assertEqual([3 * x for x in nums], [3, 6, 9, 12, 15]) |
| 855 | self.assertEqual([x for x in nums if x > 2], [3, 4, 5]) |
| 856 | self.assertEqual([(i, s) for i in nums for s in strs], |
| 857 | [(1, 'Apple'), (1, 'Banana'), (1, 'Coconut'), |
| 858 | (2, 'Apple'), (2, 'Banana'), (2, 'Coconut'), |
| 859 | (3, 'Apple'), (3, 'Banana'), (3, 'Coconut'), |
| 860 | (4, 'Apple'), (4, 'Banana'), (4, 'Coconut'), |
| 861 | (5, 'Apple'), (5, 'Banana'), (5, 'Coconut')]) |
| 862 | self.assertEqual([(i, s) for i in nums for s in [f for f in strs if "n" in f]], |
| 863 | [(1, 'Banana'), (1, 'Coconut'), (2, 'Banana'), (2, 'Coconut'), |
| 864 | (3, 'Banana'), (3, 'Coconut'), (4, 'Banana'), (4, 'Coconut'), |
| 865 | (5, 'Banana'), (5, 'Coconut')]) |
| 866 | self.assertEqual([(lambda a:[a**i for i in range(a+1)])(j) for j in range(5)], |
| 867 | [[1], [1, 1], [1, 2, 4], [1, 3, 9, 27], [1, 4, 16, 64, 256]]) |
| 868 | |
| 869 | def test_in_func(l): |
| 870 | return [0 < x < 3 for x in l if x > 2] |
| 871 | |
| 872 | self.assertEqual(test_in_func(nums), [False, False, False]) |
| 873 | |
| 874 | def test_nested_front(): |
| 875 | self.assertEqual([[y for y in [x, x + 1]] for x in [1,3,5]], |
| 876 | [[1, 2], [3, 4], [5, 6]]) |
| 877 | |
| 878 | test_nested_front() |
| 879 | |
| 880 | check_syntax_error(self, "[i, s for i in nums for s in strs]") |
| 881 | check_syntax_error(self, "[x if y]") |
| 882 | |
| 883 | suppliers = [ |
| 884 | (1, "Boeing"), |
| 885 | (2, "Ford"), |
| 886 | (3, "Macdonalds") |
| 887 | ] |
| 888 | |
| 889 | parts = [ |
| 890 | (10, "Airliner"), |
| 891 | (20, "Engine"), |
| 892 | (30, "Cheeseburger") |
| 893 | ] |
| 894 | |
| 895 | suppart = [ |
| 896 | (1, 10), (1, 20), (2, 20), (3, 30) |
| 897 | ] |
| 898 | |
| 899 | x = [ |
| 900 | (sname, pname) |
| 901 | for (sno, sname) in suppliers |
| 902 | for (pno, pname) in parts |
| 903 | for (sp_sno, sp_pno) in suppart |
| 904 | if sno == sp_sno and pno == sp_pno |
| 905 | ] |
| 906 | |
| 907 | self.assertEqual(x, [('Boeing', 'Airliner'), ('Boeing', 'Engine'), ('Ford', 'Engine'), |
| 908 | ('Macdonalds', 'Cheeseburger')]) |
| 909 | |
Benjamin Peterson | c8507bf | 2011-05-30 10:52:48 -0500 | [diff] [blame] | 910 | def test_genexps(self): |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 911 | # generator expression tests |
| 912 | g = ([x for x in range(10)] for x in range(1)) |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 913 | self.assertEqual(next(g), [x for x in range(10)]) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 914 | try: |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 915 | next(g) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 916 | self.fail('should produce StopIteration exception') |
| 917 | except StopIteration: |
| 918 | pass |
| 919 | |
| 920 | a = 1 |
| 921 | try: |
| 922 | g = (a for d in a) |
Georg Brandl | a18af4e | 2007-04-21 15:47:16 +0000 | [diff] [blame] | 923 | next(g) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 924 | self.fail('should produce TypeError') |
| 925 | except TypeError: |
| 926 | pass |
| 927 | |
| 928 | self.assertEqual(list((x, y) for x in 'abcd' for y in 'abcd'), [(x, y) for x in 'abcd' for y in 'abcd']) |
| 929 | self.assertEqual(list((x, y) for x in 'ab' for y in 'xy'), [(x, y) for x in 'ab' for y in 'xy']) |
| 930 | |
| 931 | a = [x for x in range(10)] |
| 932 | b = (x for x in (y for y in a)) |
| 933 | self.assertEqual(sum(b), sum([x for x in range(10)])) |
| 934 | |
| 935 | self.assertEqual(sum(x**2 for x in range(10)), sum([x**2 for x in range(10)])) |
| 936 | self.assertEqual(sum(x*x for x in range(10) if x%2), sum([x*x for x in range(10) if x%2])) |
| 937 | self.assertEqual(sum(x for x in (y for y in range(10))), sum([x for x in range(10)])) |
| 938 | self.assertEqual(sum(x for x in (y for y in (z for z in range(10)))), sum([x for x in range(10)])) |
| 939 | self.assertEqual(sum(x for x in [y for y in (z for z in range(10))]), sum([x for x in range(10)])) |
| 940 | 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)])) |
| 941 | self.assertEqual(sum(x for x in (y for y in (z for z in range(10) if True) if False) if True), 0) |
| 942 | check_syntax_error(self, "foo(x for x in range(10), 100)") |
| 943 | check_syntax_error(self, "foo(100, x for x in range(10))") |
| 944 | |
Benjamin Peterson | c8507bf | 2011-05-30 10:52:48 -0500 | [diff] [blame] | 945 | def test_comprehension_specials(self): |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 946 | # test for outmost iterable precomputation |
| 947 | x = 10; g = (i for i in range(x)); x = 5 |
| 948 | self.assertEqual(len(list(g)), 10) |
| 949 | |
| 950 | # This should hold, since we're only precomputing outmost iterable. |
| 951 | x = 10; t = False; g = ((i,j) for i in range(x) if t for j in range(x)) |
| 952 | x = 5; t = True; |
| 953 | self.assertEqual([(i,j) for i in range(10) for j in range(5)], list(g)) |
| 954 | |
| 955 | # Grammar allows multiple adjacent 'if's in listcomps and genexps, |
| 956 | # even though it's silly. Make sure it works (ifelse broke this.) |
| 957 | self.assertEqual([ x for x in range(10) if x % 2 if x % 3 ], [1, 5, 7]) |
| 958 | self.assertEqual(list(x for x in range(10) if x % 2 if x % 3), [1, 5, 7]) |
| 959 | |
| 960 | # verify unpacking single element tuples in listcomp/genexp. |
| 961 | self.assertEqual([x for x, in [(4,), (5,), (6,)]], [4, 5, 6]) |
| 962 | self.assertEqual(list(x for x, in [(7,), (8,), (9,)]), [7, 8, 9]) |
| 963 | |
Benjamin Peterson | f17ab89 | 2009-05-29 21:55:57 +0000 | [diff] [blame] | 964 | def test_with_statement(self): |
| 965 | class manager(object): |
| 966 | def __enter__(self): |
| 967 | return (1, 2) |
| 968 | def __exit__(self, *args): |
| 969 | pass |
| 970 | |
| 971 | with manager(): |
| 972 | pass |
| 973 | with manager() as x: |
| 974 | pass |
| 975 | with manager() as (x, y): |
| 976 | pass |
| 977 | with manager(), manager(): |
| 978 | pass |
| 979 | with manager() as x, manager() as y: |
| 980 | pass |
| 981 | with manager() as x, manager(): |
| 982 | pass |
| 983 | |
Benjamin Peterson | c8507bf | 2011-05-30 10:52:48 -0500 | [diff] [blame] | 984 | def test_if_else_expr(self): |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 985 | # Test ifelse expressions in various cases |
| 986 | def _checkeval(msg, ret): |
| 987 | "helper to check that evaluation of expressions is done correctly" |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 988 | print(x) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 989 | return ret |
| 990 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 991 | # the next line is not allowed anymore |
| 992 | #self.assertEqual([ x() for x in lambda: True, lambda: False if x() ], [True]) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 993 | self.assertEqual([ x() for x in (lambda: True, lambda: False) if x() ], [True]) |
| 994 | 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]) |
| 995 | self.assertEqual((5 if 1 else _checkeval("check 1", 0)), 5) |
| 996 | self.assertEqual((_checkeval("check 2", 0) if 0 else 5), 5) |
| 997 | self.assertEqual((5 and 6 if 0 else 1), 1) |
| 998 | self.assertEqual(((5 and 6) if 0 else 1), 1) |
| 999 | self.assertEqual((5 and (6 if 1 else 1)), 6) |
| 1000 | self.assertEqual((0 or _checkeval("check 3", 2) if 0 else 3), 3) |
| 1001 | self.assertEqual((1 or _checkeval("check 4", 2) if 1 else _checkeval("check 5", 3)), 1) |
| 1002 | self.assertEqual((0 or 5 if 1 else _checkeval("check 6", 3)), 5) |
| 1003 | self.assertEqual((not 5 if 1 else 1), False) |
| 1004 | self.assertEqual((not 5 if 0 else 1), 1) |
| 1005 | self.assertEqual((6 + 1 if 1 else 2), 7) |
| 1006 | self.assertEqual((6 - 1 if 1 else 2), 5) |
| 1007 | self.assertEqual((6 * 2 if 1 else 4), 12) |
| 1008 | self.assertEqual((6 / 2 if 1 else 3), 3) |
| 1009 | self.assertEqual((6 < 4 if 0 else 2), 2) |
Jeremy Hylton | 7b03bad | 2006-02-28 17:46:23 +0000 | [diff] [blame] | 1010 | |
Benjamin Peterson | f6489f9 | 2009-11-25 17:46:26 +0000 | [diff] [blame] | 1011 | def test_paren_evaluation(self): |
| 1012 | self.assertEqual(16 // (4 // 2), 8) |
| 1013 | self.assertEqual((16 // 4) // 2, 2) |
| 1014 | self.assertEqual(16 // 4 // 2, 2) |
| 1015 | self.assertTrue(False is (2 is 3)) |
| 1016 | self.assertFalse((False is 2) is 3) |
| 1017 | self.assertFalse(False is 2 is 3) |
| 1018 | |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 1019 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1020 | def test_main(): |
| 1021 | run_unittest(TokenTests, GrammarTests) |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 1022 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 1023 | if __name__ == '__main__': |
| 1024 | test_main() |