blob: 335b4dc15f2eb62ea9e4e95bef516ebaa0775251 [file] [log] [blame]
Benjamin Peterson55e00f22008-08-17 18:02:44 +00001"""
2Test the API of the symtable module.
3"""
Neal Norwitz89886ab2005-11-25 03:15:49 +00004import symtable
Neal Norwitzcd3e2192006-01-23 07:49:36 +00005import unittest
Jeremy Hylton5e7cb242001-02-02 18:24:26 +00006
Benjamin Peterson55e00f22008-08-17 18:02:44 +00007from test import support
Jeremy Hylton5e7cb242001-02-02 18:24:26 +00008
Jeremy Hylton42d90162003-07-15 20:24:27 +00009
Benjamin Peterson55e00f22008-08-17 18:02:44 +000010TEST_CODE = """
11import sys
Jeremy Hyltonbc2a62f2005-10-20 14:27:21 +000012
Benjamin Peterson55e00f22008-08-17 18:02:44 +000013glob = 42
14
15class Mine:
16 instance_var = 24
17 def a_method(p1, p2):
18 pass
19
20def spam(a, b, *var, **kw):
21 global bar
22 bar = 47
23 x = 23
24 glob
25 def internal():
26 return x
27 return internal
28
29def foo():
30 pass
31
32def namespace_test(): pass
33def namespace_test(): pass
34"""
35
36
37def find_block(block, name):
38 for ch in block.get_children():
39 if ch.get_name() == name:
40 return ch
41
Neal Norwitzcd3e2192006-01-23 07:49:36 +000042
43class SymtableTest(unittest.TestCase):
Benjamin Peterson55e00f22008-08-17 18:02:44 +000044
45 top = symtable.symtable(TEST_CODE, "?", "exec")
46 # These correspond to scopes in TEST_CODE
47 Mine = find_block(top, "Mine")
48 a_method = find_block(Mine, "a_method")
49 spam = find_block(top, "spam")
50 internal = find_block(spam, "internal")
51 foo = find_block(top, "foo")
52
53 def test_type(self):
54 self.assertEqual(self.top.get_type(), "module")
55 self.assertEqual(self.Mine.get_type(), "class")
56 self.assertEqual(self.a_method.get_type(), "function")
57 self.assertEqual(self.spam.get_type(), "function")
58 self.assertEqual(self.internal.get_type(), "function")
59
60 def test_optimized(self):
61 self.assertFalse(self.top.is_optimized())
62 self.assertFalse(self.top.has_exec())
63
64 self.assertTrue(self.spam.is_optimized())
65
66 def test_nested(self):
67 self.assertFalse(self.top.is_nested())
68 self.assertFalse(self.Mine.is_nested())
69 self.assertFalse(self.spam.is_nested())
70 self.assertTrue(self.internal.is_nested())
71
72 def test_children(self):
73 self.assertTrue(self.top.has_children())
74 self.assertTrue(self.Mine.has_children())
75 self.assertFalse(self.foo.has_children())
76
77 def test_lineno(self):
78 self.assertEqual(self.top.get_lineno(), 0)
79 self.assertEqual(self.spam.get_lineno(), 11)
80
81 def test_function_info(self):
82 func = self.spam
Benjamin Petersonb462da82012-01-03 16:23:11 -060083 self.assertEqual(sorted(func.get_parameters()), ["a", "b", "kw", "var"])
Benjamin Petersona8d70372012-01-03 16:24:39 -060084 expected = ["a", "b", "internal", "kw", "var", "x"]
Benjamin Petersonb462da82012-01-03 16:23:11 -060085 self.assertEqual(sorted(func.get_locals()), expected)
86 self.assertEqual(sorted(func.get_globals()), ["bar", "glob"])
Benjamin Petersonb71caf12008-08-20 12:55:31 +000087 self.assertEqual(self.internal.get_frees(), ("x",))
Benjamin Peterson55e00f22008-08-17 18:02:44 +000088
89 def test_globals(self):
90 self.assertTrue(self.spam.lookup("glob").is_global())
Jeremy Hyltonf37708e2009-03-31 15:26:37 +000091 self.assertFalse(self.spam.lookup("glob").is_declared_global())
Benjamin Peterson55e00f22008-08-17 18:02:44 +000092 self.assertTrue(self.spam.lookup("bar").is_global())
Jeremy Hyltonf37708e2009-03-31 15:26:37 +000093 self.assertTrue(self.spam.lookup("bar").is_declared_global())
Benjamin Peterson55e00f22008-08-17 18:02:44 +000094 self.assertFalse(self.internal.lookup("x").is_global())
95 self.assertFalse(self.Mine.lookup("instance_var").is_global())
96
97 def test_local(self):
98 self.assertTrue(self.spam.lookup("x").is_local())
99 self.assertFalse(self.internal.lookup("x").is_local())
100
101 def test_referenced(self):
102 self.assertTrue(self.internal.lookup("x").is_referenced())
103 self.assertTrue(self.spam.lookup("internal").is_referenced())
104 self.assertFalse(self.spam.lookup("x").is_referenced())
105
106 def test_parameters(self):
107 for sym in ("a", "var", "kw"):
108 self.assertTrue(self.spam.lookup(sym).is_parameter())
109 self.assertFalse(self.spam.lookup("x").is_parameter())
110
111 def test_symbol_lookup(self):
112 self.assertEqual(len(self.top.get_identifiers()),
113 len(self.top.get_symbols()))
114
115 self.assertRaises(KeyError, self.top.lookup, "not_here")
116
117 def test_namespaces(self):
118 self.assertTrue(self.top.lookup("Mine").is_namespace())
119 self.assertTrue(self.Mine.lookup("a_method").is_namespace())
120 self.assertTrue(self.top.lookup("spam").is_namespace())
121 self.assertTrue(self.spam.lookup("internal").is_namespace())
122 self.assertTrue(self.top.lookup("namespace_test").is_namespace())
123 self.assertFalse(self.spam.lookup("x").is_namespace())
124
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000125 self.assertTrue(self.top.lookup("spam").get_namespace() is self.spam)
Benjamin Peterson55e00f22008-08-17 18:02:44 +0000126 ns_test = self.top.lookup("namespace_test")
127 self.assertEqual(len(ns_test.get_namespaces()), 2)
128 self.assertRaises(ValueError, ns_test.get_namespace)
129
130 def test_assigned(self):
131 self.assertTrue(self.spam.lookup("x").is_assigned())
132 self.assertTrue(self.spam.lookup("bar").is_assigned())
133 self.assertTrue(self.top.lookup("spam").is_assigned())
134 self.assertTrue(self.Mine.lookup("a_method").is_assigned())
135 self.assertFalse(self.internal.lookup("x").is_assigned())
136
137 def test_imported(self):
138 self.assertTrue(self.top.lookup("sys").is_imported())
139
140 def test_name(self):
141 self.assertEqual(self.top.get_name(), "top")
142 self.assertEqual(self.spam.get_name(), "spam")
143 self.assertEqual(self.spam.lookup("x").get_name(), "x")
144 self.assertEqual(self.Mine.get_name(), "Mine")
145
146 def test_class_info(self):
Benjamin Petersonb71caf12008-08-20 12:55:31 +0000147 self.assertEqual(self.Mine.get_methods(), ('a_method',))
Benjamin Peterson55e00f22008-08-17 18:02:44 +0000148
149 def test_filename_correct(self):
150 ### Bug tickler: SyntaxError file name correct whether error raised
151 ### while parsing or building symbol table.
152 def checkfilename(brokencode):
153 try:
154 symtable.symtable(brokencode, "spam", "exec")
155 except SyntaxError as e:
156 self.assertEqual(e.filename, "spam")
157 else:
158 self.fail("no SyntaxError for %r" % (brokencode,))
159 checkfilename("def f(x): foo)(") # parse-time
160 checkfilename("def f(x): global x") # symtable-build-time
Neal Norwitzcd3e2192006-01-23 07:49:36 +0000161
162 def test_eval(self):
163 symbols = symtable.symtable("42", "?", "eval")
164
165 def test_single(self):
166 symbols = symtable.symtable("42", "?", "single")
167
168 def test_exec(self):
169 symbols = symtable.symtable("def f(x): return x", "?", "exec")
170
171
172def test_main():
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000173 support.run_unittest(SymtableTest)
Neal Norwitzcd3e2192006-01-23 07:49:36 +0000174
175if __name__ == '__main__':
176 test_main()