1 # Test various flavors of legal and illegal future statements
4 from test
import test_support
7 rx
= re
.compile('\((\S+).py, line (\d+)')
9 def get_error_location(msg
):
10 mo
= rx
.search(str(msg
))
13 class FutureTest(unittest
.TestCase
):
15 def test_future1(self
):
16 test_support
.unload('test_future1')
17 from test
import test_future1
18 self
.assertEqual(test_future1
.result
, 6)
20 def test_future2(self
):
21 test_support
.unload('test_future2')
22 from test
import test_future2
23 self
.assertEqual(test_future2
.result
, 6)
25 def test_future3(self
):
26 test_support
.unload('test_future3')
27 from test
import test_future3
29 def test_badfuture3(self
):
31 from test
import badsyntax_future3
32 except SyntaxError, msg
:
33 self
.assertEqual(get_error_location(msg
), ("badsyntax_future3", '3'))
35 self
.fail("expected exception didn't occur")
37 def test_badfuture4(self
):
39 from test
import badsyntax_future4
40 except SyntaxError, msg
:
41 self
.assertEqual(get_error_location(msg
), ("badsyntax_future4", '3'))
43 self
.fail("expected exception didn't occur")
45 def test_badfuture5(self
):
47 from test
import badsyntax_future5
48 except SyntaxError, msg
:
49 self
.assertEqual(get_error_location(msg
), ("badsyntax_future5", '4'))
51 self
.fail("expected exception didn't occur")
53 def test_badfuture6(self
):
55 from test
import badsyntax_future6
56 except SyntaxError, msg
:
57 self
.assertEqual(get_error_location(msg
), ("badsyntax_future6", '3'))
59 self
.fail("expected exception didn't occur")
61 def test_badfuture7(self
):
63 from test
import badsyntax_future7
64 except SyntaxError, msg
:
65 self
.assertEqual(get_error_location(msg
), ("badsyntax_future7", '3'))
67 self
.fail("expected exception didn't occur")
69 def test_badfuture8(self
):
71 from test
import badsyntax_future8
72 except SyntaxError, msg
:
73 self
.assertEqual(get_error_location(msg
), ("badsyntax_future8", '3'))
75 self
.fail("expected exception didn't occur")
77 def test_badfuture9(self
):
79 from test
import badsyntax_future9
80 except SyntaxError, msg
:
81 self
.assertEqual(get_error_location(msg
), ("badsyntax_future9", '3'))
83 self
.fail("expected exception didn't occur")
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.
92 exec "from __future__ import print_function; print 0"
96 self
.fail("syntax error didn't occur")
99 exec "from __future__ import (print_function); print 0"
103 self
.fail("syntax error didn't occur")
105 def test_multiple_features(self
):
106 test_support
.unload("test.test_future5")
107 from test
import test_future5
109 def test_unicode_literals_exec(self
):
111 exec "from __future__ import unicode_literals; x = ''" in scope
112 self
.assertIsInstance(scope
["x"], unicode)
116 test_support
.run_unittest(FutureTest
)
118 if __name__
== "__main__":