Jeremy Hylton | 62e2c7e | 2001-02-28 17:48:06 +0000 | [diff] [blame] | 1 | # Test various flavors of legal and illegal future statements |
| 2 | |
Neal Norwitz | 328f338 | 2003-12-13 22:43:34 +0000 | [diff] [blame] | 3 | import unittest |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 4 | from test import support |
Jeremy Hylton | 62e2c7e | 2001-02-28 17:48:06 +0000 | [diff] [blame] | 5 | import re |
| 6 | |
| 7 | rx = re.compile('\((\S+).py, line (\d+)') |
| 8 | |
Neal Norwitz | 328f338 | 2003-12-13 22:43:34 +0000 | [diff] [blame] | 9 | def get_error_location(msg): |
| 10 | mo = rx.search(str(msg)) |
| 11 | return mo.group(1, 2) |
Jeremy Hylton | 62e2c7e | 2001-02-28 17:48:06 +0000 | [diff] [blame] | 12 | |
Neal Norwitz | 328f338 | 2003-12-13 22:43:34 +0000 | [diff] [blame] | 13 | class FutureTest(unittest.TestCase): |
Jeremy Hylton | 62e2c7e | 2001-02-28 17:48:06 +0000 | [diff] [blame] | 14 | |
Neal Norwitz | 328f338 | 2003-12-13 22:43:34 +0000 | [diff] [blame] | 15 | def test_future1(self): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 16 | support.unload('test_future1') |
Neal Norwitz | 328f338 | 2003-12-13 22:43:34 +0000 | [diff] [blame] | 17 | from test import test_future1 |
| 18 | self.assertEqual(test_future1.result, 6) |
Jeremy Hylton | 62e2c7e | 2001-02-28 17:48:06 +0000 | [diff] [blame] | 19 | |
Neal Norwitz | 328f338 | 2003-12-13 22:43:34 +0000 | [diff] [blame] | 20 | def test_future2(self): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 21 | support.unload('test_future2') |
Neal Norwitz | 328f338 | 2003-12-13 22:43:34 +0000 | [diff] [blame] | 22 | from test import test_future2 |
| 23 | self.assertEqual(test_future2.result, 6) |
Jeremy Hylton | 62e2c7e | 2001-02-28 17:48:06 +0000 | [diff] [blame] | 24 | |
Neal Norwitz | 328f338 | 2003-12-13 22:43:34 +0000 | [diff] [blame] | 25 | def test_future3(self): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 26 | support.unload('test_future3') |
Neal Norwitz | 328f338 | 2003-12-13 22:43:34 +0000 | [diff] [blame] | 27 | from test import test_future3 |
Jeremy Hylton | 8471a35 | 2001-08-20 20:33:42 +0000 | [diff] [blame] | 28 | |
Neal Norwitz | 328f338 | 2003-12-13 22:43:34 +0000 | [diff] [blame] | 29 | def test_badfuture3(self): |
| 30 | try: |
| 31 | from test import badsyntax_future3 |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 32 | except SyntaxError as msg: |
Neal Norwitz | 328f338 | 2003-12-13 22:43:34 +0000 | [diff] [blame] | 33 | self.assertEqual(get_error_location(msg), ("badsyntax_future3", '3')) |
| 34 | else: |
| 35 | self.fail("expected exception didn't occur") |
Jeremy Hylton | 62e2c7e | 2001-02-28 17:48:06 +0000 | [diff] [blame] | 36 | |
Neal Norwitz | 328f338 | 2003-12-13 22:43:34 +0000 | [diff] [blame] | 37 | def test_badfuture4(self): |
| 38 | try: |
| 39 | from test import badsyntax_future4 |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 40 | except SyntaxError as msg: |
Neal Norwitz | 328f338 | 2003-12-13 22:43:34 +0000 | [diff] [blame] | 41 | self.assertEqual(get_error_location(msg), ("badsyntax_future4", '3')) |
| 42 | else: |
| 43 | self.fail("expected exception didn't occur") |
Jeremy Hylton | 62e2c7e | 2001-02-28 17:48:06 +0000 | [diff] [blame] | 44 | |
Neal Norwitz | 328f338 | 2003-12-13 22:43:34 +0000 | [diff] [blame] | 45 | def test_badfuture5(self): |
| 46 | try: |
| 47 | from test import badsyntax_future5 |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 48 | except SyntaxError as msg: |
Neal Norwitz | 328f338 | 2003-12-13 22:43:34 +0000 | [diff] [blame] | 49 | self.assertEqual(get_error_location(msg), ("badsyntax_future5", '4')) |
| 50 | else: |
| 51 | self.fail("expected exception didn't occur") |
Jeremy Hylton | 62e2c7e | 2001-02-28 17:48:06 +0000 | [diff] [blame] | 52 | |
Neal Norwitz | 328f338 | 2003-12-13 22:43:34 +0000 | [diff] [blame] | 53 | def test_badfuture6(self): |
| 54 | try: |
| 55 | from test import badsyntax_future6 |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 56 | except SyntaxError as msg: |
Neal Norwitz | 328f338 | 2003-12-13 22:43:34 +0000 | [diff] [blame] | 57 | self.assertEqual(get_error_location(msg), ("badsyntax_future6", '3')) |
| 58 | else: |
| 59 | self.fail("expected exception didn't occur") |
Jeremy Hylton | 62e2c7e | 2001-02-28 17:48:06 +0000 | [diff] [blame] | 60 | |
Neal Norwitz | 328f338 | 2003-12-13 22:43:34 +0000 | [diff] [blame] | 61 | def test_badfuture7(self): |
| 62 | try: |
| 63 | from test import badsyntax_future7 |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 64 | except SyntaxError as msg: |
Neal Norwitz | 328f338 | 2003-12-13 22:43:34 +0000 | [diff] [blame] | 65 | self.assertEqual(get_error_location(msg), ("badsyntax_future7", '3')) |
| 66 | else: |
| 67 | self.fail("expected exception didn't occur") |
| 68 | |
| 69 | def test_badfuture8(self): |
| 70 | try: |
| 71 | from test import badsyntax_future8 |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 72 | except SyntaxError as msg: |
Neal Norwitz | 328f338 | 2003-12-13 22:43:34 +0000 | [diff] [blame] | 73 | self.assertEqual(get_error_location(msg), ("badsyntax_future8", '3')) |
| 74 | else: |
| 75 | self.fail("expected exception didn't occur") |
| 76 | |
| 77 | def test_badfuture9(self): |
| 78 | try: |
| 79 | from test import badsyntax_future9 |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 80 | except SyntaxError as msg: |
Neal Norwitz | 328f338 | 2003-12-13 22:43:34 +0000 | [diff] [blame] | 81 | self.assertEqual(get_error_location(msg), ("badsyntax_future9", '3')) |
| 82 | else: |
| 83 | self.fail("expected exception didn't occur") |
| 84 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 85 | def test_parserhack(self): |
| 86 | # test that the parser.c::future_hack function works as expected |
| 87 | # Note: although this test must pass, it's not testing the original |
| 88 | # bug as of 2.6 since the with statement is not optional and |
| 89 | # the parser hack disabled. If a new keyword is introduced in |
| 90 | # 2.6, change this to refer to the new future import. |
| 91 | try: |
| 92 | exec("from __future__ import division, with_statement; with = 0") |
| 93 | except SyntaxError: |
| 94 | pass |
| 95 | else: |
| 96 | self.fail("syntax error didn't occur") |
| 97 | |
| 98 | try: |
| 99 | exec("from __future__ import (with_statement, division); with = 0") |
| 100 | except SyntaxError: |
| 101 | pass |
| 102 | else: |
| 103 | self.fail("syntax error didn't occur") |
| 104 | |
| 105 | |
Neal Norwitz | 328f338 | 2003-12-13 22:43:34 +0000 | [diff] [blame] | 106 | def test_main(): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 107 | support.run_unittest(FutureTest) |
Neal Norwitz | 328f338 | 2003-12-13 22:43:34 +0000 | [diff] [blame] | 108 | |
| 109 | if __name__ == "__main__": |
| 110 | test_main() |