blob: beac993e4d543f7a29ce2a2e826c2e6bfd02bec1 [file] [log] [blame]
Jeremy Hylton62e2c7e2001-02-28 17:48:06 +00001# Test various flavors of legal and illegal future statements
2
Neal Norwitz328f3382003-12-13 22:43:34 +00003import unittest
Benjamin Petersonee8712c2008-05-20 21:35:26 +00004from test import support
Jeremy Hylton62e2c7e2001-02-28 17:48:06 +00005import re
6
7rx = re.compile('\((\S+).py, line (\d+)')
8
Neal Norwitz328f3382003-12-13 22:43:34 +00009def get_error_location(msg):
10 mo = rx.search(str(msg))
11 return mo.group(1, 2)
Jeremy Hylton62e2c7e2001-02-28 17:48:06 +000012
Neal Norwitz328f3382003-12-13 22:43:34 +000013class FutureTest(unittest.TestCase):
Jeremy Hylton62e2c7e2001-02-28 17:48:06 +000014
Neal Norwitz328f3382003-12-13 22:43:34 +000015 def test_future1(self):
Ezio Melotti1ed6be32013-02-27 10:00:03 +020016 with support.CleanImport('future_test1'):
17 from test import future_test1
18 self.assertEqual(future_test1.result, 6)
Jeremy Hylton62e2c7e2001-02-28 17:48:06 +000019
Neal Norwitz328f3382003-12-13 22:43:34 +000020 def test_future2(self):
Ezio Melotti1ed6be32013-02-27 10:00:03 +020021 with support.CleanImport('future_test2'):
22 from test import future_test2
23 self.assertEqual(future_test2.result, 6)
Jeremy Hylton62e2c7e2001-02-28 17:48:06 +000024
Neal Norwitz328f3382003-12-13 22:43:34 +000025 def test_future3(self):
Ezio Melotti1ed6be32013-02-27 10:00:03 +020026 with support.CleanImport('test_future3'):
27 from test import test_future3
Jeremy Hylton8471a352001-08-20 20:33:42 +000028
Neal Norwitz328f3382003-12-13 22:43:34 +000029 def test_badfuture3(self):
30 try:
31 from test import badsyntax_future3
Guido van Rossumb940e112007-01-10 16:19:56 +000032 except SyntaxError as msg:
Neal Norwitz328f3382003-12-13 22:43:34 +000033 self.assertEqual(get_error_location(msg), ("badsyntax_future3", '3'))
34 else:
35 self.fail("expected exception didn't occur")
Jeremy Hylton62e2c7e2001-02-28 17:48:06 +000036
Neal Norwitz328f3382003-12-13 22:43:34 +000037 def test_badfuture4(self):
38 try:
39 from test import badsyntax_future4
Guido van Rossumb940e112007-01-10 16:19:56 +000040 except SyntaxError as msg:
Neal Norwitz328f3382003-12-13 22:43:34 +000041 self.assertEqual(get_error_location(msg), ("badsyntax_future4", '3'))
42 else:
43 self.fail("expected exception didn't occur")
Jeremy Hylton62e2c7e2001-02-28 17:48:06 +000044
Neal Norwitz328f3382003-12-13 22:43:34 +000045 def test_badfuture5(self):
46 try:
47 from test import badsyntax_future5
Guido van Rossumb940e112007-01-10 16:19:56 +000048 except SyntaxError as msg:
Neal Norwitz328f3382003-12-13 22:43:34 +000049 self.assertEqual(get_error_location(msg), ("badsyntax_future5", '4'))
50 else:
51 self.fail("expected exception didn't occur")
Jeremy Hylton62e2c7e2001-02-28 17:48:06 +000052
Neal Norwitz328f3382003-12-13 22:43:34 +000053 def test_badfuture6(self):
54 try:
55 from test import badsyntax_future6
Guido van Rossumb940e112007-01-10 16:19:56 +000056 except SyntaxError as msg:
Neal Norwitz328f3382003-12-13 22:43:34 +000057 self.assertEqual(get_error_location(msg), ("badsyntax_future6", '3'))
58 else:
59 self.fail("expected exception didn't occur")
Jeremy Hylton62e2c7e2001-02-28 17:48:06 +000060
Neal Norwitz328f3382003-12-13 22:43:34 +000061 def test_badfuture7(self):
62 try:
63 from test import badsyntax_future7
Guido van Rossumb940e112007-01-10 16:19:56 +000064 except SyntaxError as msg:
Neal Norwitz328f3382003-12-13 22:43:34 +000065 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 Rossumb940e112007-01-10 16:19:56 +000072 except SyntaxError as msg:
Neal Norwitz328f3382003-12-13 22:43:34 +000073 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 Rossumb940e112007-01-10 16:19:56 +000080 except SyntaxError as msg:
Neal Norwitz328f3382003-12-13 22:43:34 +000081 self.assertEqual(get_error_location(msg), ("badsyntax_future9", '3'))
82 else:
83 self.fail("expected exception didn't occur")
84
Benjamin Peterson2d6acd22013-03-16 09:15:47 -070085 def test_badfuture10(self):
86 try:
87 from test import badsyntax_future10
88 except SyntaxError as msg:
89 self.assertEqual(get_error_location(msg), ("badsyntax_future10", '3'))
90 else:
91 self.fail("expected exception didn't occur")
92
Thomas Wouters89f507f2006-12-13 04:49:30 +000093 def test_parserhack(self):
94 # test that the parser.c::future_hack function works as expected
95 # Note: although this test must pass, it's not testing the original
96 # bug as of 2.6 since the with statement is not optional and
97 # the parser hack disabled. If a new keyword is introduced in
98 # 2.6, change this to refer to the new future import.
99 try:
Benjamin Peterson9aebc612008-10-26 20:58:53 +0000100 exec("from __future__ import print_function; print 0")
Thomas Wouters89f507f2006-12-13 04:49:30 +0000101 except SyntaxError:
102 pass
103 else:
104 self.fail("syntax error didn't occur")
105
106 try:
Benjamin Peterson9aebc612008-10-26 20:58:53 +0000107 exec("from __future__ import (print_function); print 0")
Thomas Wouters89f507f2006-12-13 04:49:30 +0000108 except SyntaxError:
109 pass
110 else:
111 self.fail("syntax error didn't occur")
112
Benjamin Peterson9aebc612008-10-26 20:58:53 +0000113 def test_multiple_features(self):
Ezio Melotti1ed6be32013-02-27 10:00:03 +0200114 with support.CleanImport("test.test_future5"):
115 from test import test_future5
Benjamin Peterson9aebc612008-10-26 20:58:53 +0000116
Benjamin Petersonf216c942008-10-31 02:28:05 +0000117 def test_unicode_literals_exec(self):
118 scope = {}
119 exec("from __future__ import unicode_literals; x = ''", {}, scope)
Ezio Melottie9615932010-01-24 19:26:24 +0000120 self.assertIsInstance(scope["x"], str)
Benjamin Petersonf216c942008-10-31 02:28:05 +0000121
Thomas Wouters89f507f2006-12-13 04:49:30 +0000122
Neal Norwitz328f3382003-12-13 22:43:34 +0000123
124if __name__ == "__main__":
Ezio Melotti1ed6be32013-02-27 10:00:03 +0200125 unittest.main()