2 Test the API of the symtable module.
8 from test
import test_support
21 def spam(a, b, *var, **kw):
34 def namespace_test(): pass
35 def namespace_test(): pass
39 def find_block(block
, name
):
40 for ch
in block
.get_children():
41 if ch
.get_name() == name
:
45 class SymtableTest(unittest
.TestCase
):
47 with warnings
.catch_warnings():
48 # Ignore warnings about "from blank import *"
49 warnings
.simplefilter("ignore", SyntaxWarning)
50 top
= symtable
.symtable(TEST_CODE
, "?", "exec")
51 # These correspond to scopes in TEST_CODE
52 Mine
= find_block(top
, "Mine")
53 a_method
= find_block(Mine
, "a_method")
54 spam
= find_block(top
, "spam")
55 internal
= find_block(spam
, "internal")
56 foo
= find_block(top
, "foo")
59 self
.assertEqual(self
.top
.get_type(), "module")
60 self
.assertEqual(self
.Mine
.get_type(), "class")
61 self
.assertEqual(self
.a_method
.get_type(), "function")
62 self
.assertEqual(self
.spam
.get_type(), "function")
63 self
.assertEqual(self
.internal
.get_type(), "function")
65 def test_optimized(self
):
66 self
.assertFalse(self
.top
.is_optimized())
67 self
.assertFalse(self
.top
.has_exec())
68 self
.assertFalse(self
.top
.has_import_star())
70 self
.assertTrue(self
.spam
.is_optimized())
72 self
.assertFalse(self
.foo
.is_optimized())
73 self
.assertTrue(self
.foo
.has_exec())
74 self
.assertTrue(self
.foo
.has_import_star())
76 def test_nested(self
):
77 self
.assertFalse(self
.top
.is_nested())
78 self
.assertFalse(self
.Mine
.is_nested())
79 self
.assertFalse(self
.spam
.is_nested())
80 self
.assertTrue(self
.internal
.is_nested())
82 def test_children(self
):
83 self
.assertTrue(self
.top
.has_children())
84 self
.assertTrue(self
.Mine
.has_children())
85 self
.assertFalse(self
.foo
.has_children())
87 def test_lineno(self
):
88 self
.assertEqual(self
.top
.get_lineno(), 0)
89 self
.assertEqual(self
.spam
.get_lineno(), 11)
91 def test_function_info(self
):
93 self
.assertEqual(func
.get_parameters(), ("a", "b", "kw", "var"))
94 self
.assertEqual(func
.get_locals(),
95 ("a", "b", "internal", "kw", "var", "x"))
96 self
.assertEqual(func
.get_globals(), ("bar", "glob"))
97 self
.assertEqual(self
.internal
.get_frees(), ("x",))
99 def test_globals(self
):
100 self
.assertTrue(self
.spam
.lookup("glob").is_global())
101 self
.assertFalse(self
.spam
.lookup("glob").is_declared_global())
102 self
.assertTrue(self
.spam
.lookup("bar").is_global())
103 self
.assertTrue(self
.spam
.lookup("bar").is_declared_global())
104 self
.assertFalse(self
.internal
.lookup("x").is_global())
105 self
.assertFalse(self
.Mine
.lookup("instance_var").is_global())
107 def test_local(self
):
108 self
.assertTrue(self
.spam
.lookup("x").is_local())
109 self
.assertFalse(self
.internal
.lookup("x").is_local())
111 def test_referenced(self
):
112 self
.assertTrue(self
.internal
.lookup("x").is_referenced())
113 self
.assertTrue(self
.spam
.lookup("internal").is_referenced())
114 self
.assertFalse(self
.spam
.lookup("x").is_referenced())
116 def test_parameters(self
):
117 for sym
in ("a", "var", "kw"):
118 self
.assertTrue(self
.spam
.lookup(sym
).is_parameter())
119 self
.assertFalse(self
.spam
.lookup("x").is_parameter())
121 def test_symbol_lookup(self
):
122 self
.assertEqual(len(self
.top
.get_identifiers()),
123 len(self
.top
.get_symbols()))
125 self
.assertRaises(KeyError, self
.top
.lookup
, "not_here")
127 def test_namespaces(self
):
128 self
.assertTrue(self
.top
.lookup("Mine").is_namespace())
129 self
.assertTrue(self
.Mine
.lookup("a_method").is_namespace())
130 self
.assertTrue(self
.top
.lookup("spam").is_namespace())
131 self
.assertTrue(self
.spam
.lookup("internal").is_namespace())
132 self
.assertTrue(self
.top
.lookup("namespace_test").is_namespace())
133 self
.assertFalse(self
.spam
.lookup("x").is_namespace())
135 self
.assertTrue(self
.top
.lookup("spam").get_namespace() is self
.spam
)
136 ns_test
= self
.top
.lookup("namespace_test")
137 self
.assertEqual(len(ns_test
.get_namespaces()), 2)
138 self
.assertRaises(ValueError, ns_test
.get_namespace
)
140 def test_assigned(self
):
141 self
.assertTrue(self
.spam
.lookup("x").is_assigned())
142 self
.assertTrue(self
.spam
.lookup("bar").is_assigned())
143 self
.assertTrue(self
.top
.lookup("spam").is_assigned())
144 self
.assertTrue(self
.Mine
.lookup("a_method").is_assigned())
145 self
.assertFalse(self
.internal
.lookup("x").is_assigned())
147 def test_imported(self
):
148 self
.assertTrue(self
.top
.lookup("sys").is_imported())
151 self
.assertEqual(self
.top
.get_name(), "top")
152 self
.assertEqual(self
.spam
.get_name(), "spam")
153 self
.assertEqual(self
.spam
.lookup("x").get_name(), "x")
154 self
.assertEqual(self
.Mine
.get_name(), "Mine")
156 def test_class_info(self
):
157 self
.assertEqual(self
.Mine
.get_methods(), ('a_method',))
159 def test_filename_correct(self
):
160 ### Bug tickler: SyntaxError file name correct whether error raised
161 ### while parsing or building symbol table.
162 def checkfilename(brokencode
):
164 symtable
.symtable(brokencode
, "spam", "exec")
165 except SyntaxError as e
:
166 self
.assertEqual(e
.filename
, "spam")
168 self
.fail("no SyntaxError for %r" % (brokencode
,))
169 checkfilename("def f(x): foo)(") # parse-time
170 checkfilename("def f(x): global x") # symtable-build-time
173 symbols
= symtable
.symtable("42", "?", "eval")
175 def test_single(self
):
176 symbols
= symtable
.symtable("42", "?", "single")
179 symbols
= symtable
.symtable("def f(x): return x", "?", "exec")
183 test_support
.run_unittest(SymtableTest
)
185 if __name__
== '__main__':