Fredrik Lundh | f785042 | 2001-01-17 21:51:36 +0000 | [diff] [blame] | 1 | from test_support import verbose, TestFailed |
Peter Schneider-Kamp | fdee0f0 | 2000-07-25 22:15:45 +0000 | [diff] [blame] | 2 | |
| 3 | if verbose: |
Jeremy Hylton | 047e2c9 | 2001-01-19 03:25:56 +0000 | [diff] [blame] | 4 | print 'Running tests on argument handling' |
Peter Schneider-Kamp | fdee0f0 | 2000-07-25 22:15:45 +0000 | [diff] [blame] | 5 | |
| 6 | try: |
Jeremy Hylton | 121b6eb | 2001-02-19 23:53:42 +0000 | [diff] [blame] | 7 | exec 'def f(a, a): pass' |
Peter Schneider-Kamp | fdee0f0 | 2000-07-25 22:15:45 +0000 | [diff] [blame] | 8 | raise TestFailed, "duplicate arguments" |
| 9 | except SyntaxError: |
| 10 | pass |
| 11 | |
| 12 | try: |
Jeremy Hylton | 121b6eb | 2001-02-19 23:53:42 +0000 | [diff] [blame] | 13 | exec 'def f(a = 0, a = 1): pass' |
Peter Schneider-Kamp | fdee0f0 | 2000-07-25 22:15:45 +0000 | [diff] [blame] | 14 | raise TestFailed, "duplicate keyword arguments" |
| 15 | except SyntaxError: |
| 16 | pass |
Jeremy Hylton | 047e2c9 | 2001-01-19 03:25:56 +0000 | [diff] [blame] | 17 | |
| 18 | try: |
Jeremy Hylton | 121b6eb | 2001-02-19 23:53:42 +0000 | [diff] [blame] | 19 | exec 'def f(a): global a; a = 1' |
Jeremy Hylton | 047e2c9 | 2001-01-19 03:25:56 +0000 | [diff] [blame] | 20 | raise TestFailed, "variable is global and local" |
| 21 | except SyntaxError: |
| 22 | pass |
Jeremy Hylton | 121b6eb | 2001-02-19 23:53:42 +0000 | [diff] [blame] | 23 | |
| 24 | print "testing complex args" |
| 25 | |
| 26 | def comp_args((a, b)): |
Tim Peters | 0009c4e | 2001-02-21 07:29:48 +0000 | [diff] [blame] | 27 | print a,b |
Jeremy Hylton | 121b6eb | 2001-02-19 23:53:42 +0000 | [diff] [blame] | 28 | |
| 29 | comp_args((1, 2)) |
| 30 | |
| 31 | def comp_args((a, b)=(3, 4)): |
| 32 | print a, b |
| 33 | |
| 34 | comp_args((1, 2)) |
| 35 | comp_args() |
| 36 | |
| 37 | def comp_args(a, (b, c)): |
| 38 | print a, b, c |
| 39 | |
| 40 | comp_args(1, (2, 3)) |
| 41 | |
| 42 | def comp_args(a=2, (b, c)=(3, 4)): |
| 43 | print a, b, c |
| 44 | |
| 45 | comp_args(1, (2, 3)) |
| 46 | comp_args() |
| 47 | |
| 48 | try: |
| 49 | exec 'def f(a=1, (b, c)): pass' |
| 50 | raise TestFailed, "non-default args after default" |
| 51 | except SyntaxError: |
| 52 | pass |
Tim Peters | 9aa70d9 | 2001-08-27 19:19:28 +0000 | [diff] [blame] | 53 | |
| 54 | if verbose: |
| 55 | print "testing bad float literals" |
| 56 | |
| 57 | def expect_error(s): |
| 58 | try: |
| 59 | eval(s) |
| 60 | raise TestFailed("%r accepted" % s) |
| 61 | except SyntaxError: |
| 62 | pass |
| 63 | |
| 64 | expect_error("2e") |
| 65 | expect_error("2.0e+") |
| 66 | expect_error("1e-") |
| 67 | expect_error("3-4e/21") |
Tim Peters | d507dab | 2001-08-30 20:51:59 +0000 | [diff] [blame] | 68 | |
| 69 | |
| 70 | if verbose: |
| 71 | print "testing literals with leading zeroes" |
| 72 | |
| 73 | def expect_same(test_source, expected): |
| 74 | got = eval(test_source) |
| 75 | if got != expected: |
| 76 | raise TestFailed("eval(%r) gave %r, but expected %r" % |
| 77 | (test_source, got, expected)) |
| 78 | |
| 79 | expect_error("077787") |
| 80 | expect_error("0xj") |
| 81 | expect_error("0x.") |
| 82 | expect_error("0e") |
| 83 | expect_same("0777", 511) |
| 84 | expect_same("0777L", 511) |
| 85 | expect_same("000777", 511) |
| 86 | expect_same("0xff", 255) |
| 87 | expect_same("0xffL", 255) |
| 88 | expect_same("0XfF", 255) |
| 89 | expect_same("0777.", 777) |
| 90 | expect_same("0777.0", 777) |
| 91 | expect_same("000000000000000000000000000000000000000000000000000777e0", 777) |
| 92 | expect_same("0777e1", 7770) |
| 93 | expect_same("0e0", 0) |
| 94 | expect_same("0000E-012", 0) |
| 95 | expect_same("09.5", 9.5) |
| 96 | expect_same("0777j", 777j) |
| 97 | expect_same("00j", 0j) |
| 98 | expect_same("00.0", 0) |
| 99 | expect_same("0e3", 0) |
| 100 | expect_same("090000000000000.", 90000000000000.) |
| 101 | expect_same("090000000000000.0000000000000000000000", 90000000000000.) |
| 102 | expect_same("090000000000000e0", 90000000000000.) |
| 103 | expect_same("090000000000000e-0", 90000000000000.) |
| 104 | expect_same("090000000000000j", 90000000000000j) |
| 105 | expect_error("090000000000000") # plain octal literal w/ decimal digit |
| 106 | expect_error("080000000000000") # plain octal literal w/ decimal digit |
| 107 | expect_error("000000000000009") # plain octal literal w/ decimal digit |
| 108 | expect_error("000000000000008") # plain octal literal w/ decimal digit |
| 109 | expect_same("000000000000007", 7) |
| 110 | expect_same("000000000000008.", 8.) |
| 111 | expect_same("000000000000009.", 9.) |