| 1 | n/a | # Test various flavors of legal and illegal future statements |
|---|
| 2 | n/a | |
|---|
| 3 | n/a | import unittest |
|---|
| 4 | n/a | from test import support |
|---|
| 5 | n/a | import os |
|---|
| 6 | n/a | import re |
|---|
| 7 | n/a | |
|---|
| 8 | n/a | rx = re.compile(r'\((\S+).py, line (\d+)') |
|---|
| 9 | n/a | |
|---|
| 10 | n/a | def get_error_location(msg): |
|---|
| 11 | n/a | mo = rx.search(str(msg)) |
|---|
| 12 | n/a | return mo.group(1, 2) |
|---|
| 13 | n/a | |
|---|
| 14 | n/a | class FutureTest(unittest.TestCase): |
|---|
| 15 | n/a | |
|---|
| 16 | n/a | def check_syntax_error(self, err, basename, lineno, offset=0): |
|---|
| 17 | n/a | self.assertIn('%s.py, line %d' % (basename, lineno), str(err)) |
|---|
| 18 | n/a | self.assertEqual(os.path.basename(err.filename), basename + '.py') |
|---|
| 19 | n/a | self.assertEqual(err.lineno, lineno) |
|---|
| 20 | n/a | self.assertEqual(err.offset, offset) |
|---|
| 21 | n/a | |
|---|
| 22 | n/a | def test_future1(self): |
|---|
| 23 | n/a | with support.CleanImport('future_test1'): |
|---|
| 24 | n/a | from test import future_test1 |
|---|
| 25 | n/a | self.assertEqual(future_test1.result, 6) |
|---|
| 26 | n/a | |
|---|
| 27 | n/a | def test_future2(self): |
|---|
| 28 | n/a | with support.CleanImport('future_test2'): |
|---|
| 29 | n/a | from test import future_test2 |
|---|
| 30 | n/a | self.assertEqual(future_test2.result, 6) |
|---|
| 31 | n/a | |
|---|
| 32 | n/a | def test_future3(self): |
|---|
| 33 | n/a | with support.CleanImport('test_future3'): |
|---|
| 34 | n/a | from test import test_future3 |
|---|
| 35 | n/a | |
|---|
| 36 | n/a | def test_badfuture3(self): |
|---|
| 37 | n/a | with self.assertRaises(SyntaxError) as cm: |
|---|
| 38 | n/a | from test import badsyntax_future3 |
|---|
| 39 | n/a | self.check_syntax_error(cm.exception, "badsyntax_future3", 3) |
|---|
| 40 | n/a | |
|---|
| 41 | n/a | def test_badfuture4(self): |
|---|
| 42 | n/a | with self.assertRaises(SyntaxError) as cm: |
|---|
| 43 | n/a | from test import badsyntax_future4 |
|---|
| 44 | n/a | self.check_syntax_error(cm.exception, "badsyntax_future4", 3) |
|---|
| 45 | n/a | |
|---|
| 46 | n/a | def test_badfuture5(self): |
|---|
| 47 | n/a | with self.assertRaises(SyntaxError) as cm: |
|---|
| 48 | n/a | from test import badsyntax_future5 |
|---|
| 49 | n/a | self.check_syntax_error(cm.exception, "badsyntax_future5", 4) |
|---|
| 50 | n/a | |
|---|
| 51 | n/a | def test_badfuture6(self): |
|---|
| 52 | n/a | with self.assertRaises(SyntaxError) as cm: |
|---|
| 53 | n/a | from test import badsyntax_future6 |
|---|
| 54 | n/a | self.check_syntax_error(cm.exception, "badsyntax_future6", 3) |
|---|
| 55 | n/a | |
|---|
| 56 | n/a | def test_badfuture7(self): |
|---|
| 57 | n/a | with self.assertRaises(SyntaxError) as cm: |
|---|
| 58 | n/a | from test import badsyntax_future7 |
|---|
| 59 | n/a | self.check_syntax_error(cm.exception, "badsyntax_future7", 3, 53) |
|---|
| 60 | n/a | |
|---|
| 61 | n/a | def test_badfuture8(self): |
|---|
| 62 | n/a | with self.assertRaises(SyntaxError) as cm: |
|---|
| 63 | n/a | from test import badsyntax_future8 |
|---|
| 64 | n/a | self.check_syntax_error(cm.exception, "badsyntax_future8", 3) |
|---|
| 65 | n/a | |
|---|
| 66 | n/a | def test_badfuture9(self): |
|---|
| 67 | n/a | with self.assertRaises(SyntaxError) as cm: |
|---|
| 68 | n/a | from test import badsyntax_future9 |
|---|
| 69 | n/a | self.check_syntax_error(cm.exception, "badsyntax_future9", 3, 0) |
|---|
| 70 | n/a | |
|---|
| 71 | n/a | def test_badfuture10(self): |
|---|
| 72 | n/a | with self.assertRaises(SyntaxError) as cm: |
|---|
| 73 | n/a | from test import badsyntax_future10 |
|---|
| 74 | n/a | self.check_syntax_error(cm.exception, "badsyntax_future10", 3, 0) |
|---|
| 75 | n/a | |
|---|
| 76 | n/a | def test_parserhack(self): |
|---|
| 77 | n/a | # test that the parser.c::future_hack function works as expected |
|---|
| 78 | n/a | # Note: although this test must pass, it's not testing the original |
|---|
| 79 | n/a | # bug as of 2.6 since the with statement is not optional and |
|---|
| 80 | n/a | # the parser hack disabled. If a new keyword is introduced in |
|---|
| 81 | n/a | # 2.6, change this to refer to the new future import. |
|---|
| 82 | n/a | try: |
|---|
| 83 | n/a | exec("from __future__ import print_function; print 0") |
|---|
| 84 | n/a | except SyntaxError: |
|---|
| 85 | n/a | pass |
|---|
| 86 | n/a | else: |
|---|
| 87 | n/a | self.fail("syntax error didn't occur") |
|---|
| 88 | n/a | |
|---|
| 89 | n/a | try: |
|---|
| 90 | n/a | exec("from __future__ import (print_function); print 0") |
|---|
| 91 | n/a | except SyntaxError: |
|---|
| 92 | n/a | pass |
|---|
| 93 | n/a | else: |
|---|
| 94 | n/a | self.fail("syntax error didn't occur") |
|---|
| 95 | n/a | |
|---|
| 96 | n/a | def test_multiple_features(self): |
|---|
| 97 | n/a | with support.CleanImport("test.test_future5"): |
|---|
| 98 | n/a | from test import test_future5 |
|---|
| 99 | n/a | |
|---|
| 100 | n/a | def test_unicode_literals_exec(self): |
|---|
| 101 | n/a | scope = {} |
|---|
| 102 | n/a | exec("from __future__ import unicode_literals; x = ''", {}, scope) |
|---|
| 103 | n/a | self.assertIsInstance(scope["x"], str) |
|---|
| 104 | n/a | |
|---|
| 105 | n/a | |
|---|
| 106 | n/a | |
|---|
| 107 | n/a | if __name__ == "__main__": |
|---|
| 108 | n/a | unittest.main() |
|---|