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

Python code coverage for Lib/test/test_symtable.py

#countcontent
1n/a"""
2n/aTest the API of the symtable module.
3n/a"""
4n/aimport symtable
5n/aimport unittest
6n/a
7n/a
8n/a
9n/aTEST_CODE = """
10n/aimport sys
11n/a
12n/aglob = 42
13n/a
14n/aclass Mine:
15n/a instance_var = 24
16n/a def a_method(p1, p2):
17n/a pass
18n/a
19n/adef spam(a, b, *var, **kw):
20n/a global bar
21n/a bar = 47
22n/a x = 23
23n/a glob
24n/a def internal():
25n/a return x
26n/a return internal
27n/a
28n/adef foo():
29n/a pass
30n/a
31n/adef namespace_test(): pass
32n/adef namespace_test(): pass
33n/a"""
34n/a
35n/a
36n/adef find_block(block, name):
37n/a for ch in block.get_children():
38n/a if ch.get_name() == name:
39n/a return ch
40n/a
41n/a
42n/aclass SymtableTest(unittest.TestCase):
43n/a
44n/a top = symtable.symtable(TEST_CODE, "?", "exec")
45n/a # These correspond to scopes in TEST_CODE
46n/a Mine = find_block(top, "Mine")
47n/a a_method = find_block(Mine, "a_method")
48n/a spam = find_block(top, "spam")
49n/a internal = find_block(spam, "internal")
50n/a foo = find_block(top, "foo")
51n/a
52n/a def test_type(self):
53n/a self.assertEqual(self.top.get_type(), "module")
54n/a self.assertEqual(self.Mine.get_type(), "class")
55n/a self.assertEqual(self.a_method.get_type(), "function")
56n/a self.assertEqual(self.spam.get_type(), "function")
57n/a self.assertEqual(self.internal.get_type(), "function")
58n/a
59n/a def test_optimized(self):
60n/a self.assertFalse(self.top.is_optimized())
61n/a self.assertFalse(self.top.has_exec())
62n/a
63n/a self.assertTrue(self.spam.is_optimized())
64n/a
65n/a def test_nested(self):
66n/a self.assertFalse(self.top.is_nested())
67n/a self.assertFalse(self.Mine.is_nested())
68n/a self.assertFalse(self.spam.is_nested())
69n/a self.assertTrue(self.internal.is_nested())
70n/a
71n/a def test_children(self):
72n/a self.assertTrue(self.top.has_children())
73n/a self.assertTrue(self.Mine.has_children())
74n/a self.assertFalse(self.foo.has_children())
75n/a
76n/a def test_lineno(self):
77n/a self.assertEqual(self.top.get_lineno(), 0)
78n/a self.assertEqual(self.spam.get_lineno(), 11)
79n/a
80n/a def test_function_info(self):
81n/a func = self.spam
82n/a self.assertEqual(sorted(func.get_parameters()), ["a", "b", "kw", "var"])
83n/a expected = ["a", "b", "internal", "kw", "var", "x"]
84n/a self.assertEqual(sorted(func.get_locals()), expected)
85n/a self.assertEqual(sorted(func.get_globals()), ["bar", "glob"])
86n/a self.assertEqual(self.internal.get_frees(), ("x",))
87n/a
88n/a def test_globals(self):
89n/a self.assertTrue(self.spam.lookup("glob").is_global())
90n/a self.assertFalse(self.spam.lookup("glob").is_declared_global())
91n/a self.assertTrue(self.spam.lookup("bar").is_global())
92n/a self.assertTrue(self.spam.lookup("bar").is_declared_global())
93n/a self.assertFalse(self.internal.lookup("x").is_global())
94n/a self.assertFalse(self.Mine.lookup("instance_var").is_global())
95n/a
96n/a def test_local(self):
97n/a self.assertTrue(self.spam.lookup("x").is_local())
98n/a self.assertFalse(self.internal.lookup("x").is_local())
99n/a
100n/a def test_referenced(self):
101n/a self.assertTrue(self.internal.lookup("x").is_referenced())
102n/a self.assertTrue(self.spam.lookup("internal").is_referenced())
103n/a self.assertFalse(self.spam.lookup("x").is_referenced())
104n/a
105n/a def test_parameters(self):
106n/a for sym in ("a", "var", "kw"):
107n/a self.assertTrue(self.spam.lookup(sym).is_parameter())
108n/a self.assertFalse(self.spam.lookup("x").is_parameter())
109n/a
110n/a def test_symbol_lookup(self):
111n/a self.assertEqual(len(self.top.get_identifiers()),
112n/a len(self.top.get_symbols()))
113n/a
114n/a self.assertRaises(KeyError, self.top.lookup, "not_here")
115n/a
116n/a def test_namespaces(self):
117n/a self.assertTrue(self.top.lookup("Mine").is_namespace())
118n/a self.assertTrue(self.Mine.lookup("a_method").is_namespace())
119n/a self.assertTrue(self.top.lookup("spam").is_namespace())
120n/a self.assertTrue(self.spam.lookup("internal").is_namespace())
121n/a self.assertTrue(self.top.lookup("namespace_test").is_namespace())
122n/a self.assertFalse(self.spam.lookup("x").is_namespace())
123n/a
124n/a self.assertTrue(self.top.lookup("spam").get_namespace() is self.spam)
125n/a ns_test = self.top.lookup("namespace_test")
126n/a self.assertEqual(len(ns_test.get_namespaces()), 2)
127n/a self.assertRaises(ValueError, ns_test.get_namespace)
128n/a
129n/a def test_assigned(self):
130n/a self.assertTrue(self.spam.lookup("x").is_assigned())
131n/a self.assertTrue(self.spam.lookup("bar").is_assigned())
132n/a self.assertTrue(self.top.lookup("spam").is_assigned())
133n/a self.assertTrue(self.Mine.lookup("a_method").is_assigned())
134n/a self.assertFalse(self.internal.lookup("x").is_assigned())
135n/a
136n/a def test_annotated(self):
137n/a st1 = symtable.symtable('def f():\n x: int\n', 'test', 'exec')
138n/a st2 = st1.get_children()[0]
139n/a self.assertTrue(st2.lookup('x').is_local())
140n/a self.assertTrue(st2.lookup('x').is_annotated())
141n/a self.assertFalse(st2.lookup('x').is_global())
142n/a st3 = symtable.symtable('def f():\n x = 1\n', 'test', 'exec')
143n/a st4 = st3.get_children()[0]
144n/a self.assertTrue(st4.lookup('x').is_local())
145n/a self.assertFalse(st4.lookup('x').is_annotated())
146n/a
147n/a def test_imported(self):
148n/a self.assertTrue(self.top.lookup("sys").is_imported())
149n/a
150n/a def test_name(self):
151n/a self.assertEqual(self.top.get_name(), "top")
152n/a self.assertEqual(self.spam.get_name(), "spam")
153n/a self.assertEqual(self.spam.lookup("x").get_name(), "x")
154n/a self.assertEqual(self.Mine.get_name(), "Mine")
155n/a
156n/a def test_class_info(self):
157n/a self.assertEqual(self.Mine.get_methods(), ('a_method',))
158n/a
159n/a def test_filename_correct(self):
160n/a ### Bug tickler: SyntaxError file name correct whether error raised
161n/a ### while parsing or building symbol table.
162n/a def checkfilename(brokencode, offset):
163n/a try:
164n/a symtable.symtable(brokencode, "spam", "exec")
165n/a except SyntaxError as e:
166n/a self.assertEqual(e.filename, "spam")
167n/a self.assertEqual(e.lineno, 1)
168n/a self.assertEqual(e.offset, offset)
169n/a else:
170n/a self.fail("no SyntaxError for %r" % (brokencode,))
171n/a checkfilename("def f(x): foo)(", 14) # parse-time
172n/a checkfilename("def f(x): global x", 10) # symtable-build-time
173n/a symtable.symtable("pass", b"spam", "exec")
174n/a with self.assertWarns(DeprecationWarning), \
175n/a self.assertRaises(TypeError):
176n/a symtable.symtable("pass", bytearray(b"spam"), "exec")
177n/a with self.assertWarns(DeprecationWarning):
178n/a symtable.symtable("pass", memoryview(b"spam"), "exec")
179n/a with self.assertRaises(TypeError):
180n/a symtable.symtable("pass", list(b"spam"), "exec")
181n/a
182n/a def test_eval(self):
183n/a symbols = symtable.symtable("42", "?", "eval")
184n/a
185n/a def test_single(self):
186n/a symbols = symtable.symtable("42", "?", "single")
187n/a
188n/a def test_exec(self):
189n/a symbols = symtable.symtable("def f(x): return x", "?", "exec")
190n/a
191n/a
192n/aif __name__ == '__main__':
193n/a unittest.main()