ยปCore Development>Code coverage>Lib/test/test_future.py

Python code coverage for Lib/test/test_future.py

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