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

Python code coverage for Lib/test/test_global.py

#countcontent
1n/a"""Verify that warnings are issued for global statements following use."""
2n/a
3n/afrom test.support import run_unittest, check_syntax_error, check_warnings
4n/aimport unittest
5n/aimport warnings
6n/a
7n/a
8n/aclass GlobalTests(unittest.TestCase):
9n/a
10n/a def setUp(self):
11n/a self._warnings_manager = check_warnings()
12n/a self._warnings_manager.__enter__()
13n/a warnings.filterwarnings("error", module="<test string>")
14n/a
15n/a def tearDown(self):
16n/a self._warnings_manager.__exit__(None, None, None)
17n/a
18n/a
19n/a def test1(self):
20n/a prog_text_1 = """\
21n/adef wrong1():
22n/a a = 1
23n/a b = 2
24n/a global a
25n/a global b
26n/a"""
27n/a check_syntax_error(self, prog_text_1, lineno=4, offset=4)
28n/a
29n/a def test2(self):
30n/a prog_text_2 = """\
31n/adef wrong2():
32n/a print(x)
33n/a global x
34n/a"""
35n/a check_syntax_error(self, prog_text_2, lineno=3, offset=4)
36n/a
37n/a def test3(self):
38n/a prog_text_3 = """\
39n/adef wrong3():
40n/a print(x)
41n/a x = 2
42n/a global x
43n/a"""
44n/a check_syntax_error(self, prog_text_3, lineno=4, offset=4)
45n/a
46n/a def test4(self):
47n/a prog_text_4 = """\
48n/aglobal x
49n/ax = 2
50n/a"""
51n/a # this should work
52n/a compile(prog_text_4, "<test string>", "exec")
53n/a
54n/a
55n/adef test_main():
56n/a with warnings.catch_warnings():
57n/a warnings.filterwarnings("error", module="<test string>")
58n/a run_unittest(GlobalTests)
59n/a
60n/aif __name__ == "__main__":
61n/a test_main()