Benjamin Peterson | 55e00f2 | 2008-08-17 18:02:44 +0000 | [diff] [blame] | 1 | """ |
| 2 | Test the API of the symtable module. |
| 3 | """ |
Neal Norwitz | 89886ab | 2005-11-25 03:15:49 +0000 | [diff] [blame] | 4 | import symtable |
Neal Norwitz | cd3e219 | 2006-01-23 07:49:36 +0000 | [diff] [blame] | 5 | import unittest |
Jeremy Hylton | 5e7cb24 | 2001-02-02 18:24:26 +0000 | [diff] [blame] | 6 | |
Benjamin Peterson | 55e00f2 | 2008-08-17 18:02:44 +0000 | [diff] [blame] | 7 | from test import support |
Jeremy Hylton | 5e7cb24 | 2001-02-02 18:24:26 +0000 | [diff] [blame] | 8 | |
Jeremy Hylton | 42d9016 | 2003-07-15 20:24:27 +0000 | [diff] [blame] | 9 | |
Benjamin Peterson | 55e00f2 | 2008-08-17 18:02:44 +0000 | [diff] [blame] | 10 | TEST_CODE = """ |
| 11 | import sys |
Jeremy Hylton | bc2a62f | 2005-10-20 14:27:21 +0000 | [diff] [blame] | 12 | |
Benjamin Peterson | 55e00f2 | 2008-08-17 18:02:44 +0000 | [diff] [blame] | 13 | glob = 42 |
| 14 | |
| 15 | class Mine: |
| 16 | instance_var = 24 |
| 17 | def a_method(p1, p2): |
| 18 | pass |
| 19 | |
| 20 | def 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 | |
| 29 | def foo(): |
| 30 | pass |
| 31 | |
| 32 | def namespace_test(): pass |
| 33 | def namespace_test(): pass |
| 34 | """ |
| 35 | |
| 36 | |
| 37 | def find_block(block, name): |
| 38 | for ch in block.get_children(): |
| 39 | if ch.get_name() == name: |
| 40 | return ch |
| 41 | |
Neal Norwitz | cd3e219 | 2006-01-23 07:49:36 +0000 | [diff] [blame] | 42 | |
| 43 | class SymtableTest(unittest.TestCase): |
Benjamin Peterson | 55e00f2 | 2008-08-17 18:02:44 +0000 | [diff] [blame] | 44 | |
| 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 Peterson | b462da8 | 2012-01-03 16:23:11 -0600 | [diff] [blame] | 83 | self.assertEqual(sorted(func.get_parameters()), ["a", "b", "kw", "var"]) |
Benjamin Peterson | a8d7037 | 2012-01-03 16:24:39 -0600 | [diff] [blame] | 84 | expected = ["a", "b", "internal", "kw", "var", "x"] |
Benjamin Peterson | b462da8 | 2012-01-03 16:23:11 -0600 | [diff] [blame] | 85 | self.assertEqual(sorted(func.get_locals()), expected) |
| 86 | self.assertEqual(sorted(func.get_globals()), ["bar", "glob"]) |
Benjamin Peterson | b71caf1 | 2008-08-20 12:55:31 +0000 | [diff] [blame] | 87 | self.assertEqual(self.internal.get_frees(), ("x",)) |
Benjamin Peterson | 55e00f2 | 2008-08-17 18:02:44 +0000 | [diff] [blame] | 88 | |
| 89 | def test_globals(self): |
| 90 | self.assertTrue(self.spam.lookup("glob").is_global()) |
Jeremy Hylton | f37708e | 2009-03-31 15:26:37 +0000 | [diff] [blame] | 91 | self.assertFalse(self.spam.lookup("glob").is_declared_global()) |
Benjamin Peterson | 55e00f2 | 2008-08-17 18:02:44 +0000 | [diff] [blame] | 92 | self.assertTrue(self.spam.lookup("bar").is_global()) |
Jeremy Hylton | f37708e | 2009-03-31 15:26:37 +0000 | [diff] [blame] | 93 | self.assertTrue(self.spam.lookup("bar").is_declared_global()) |
Benjamin Peterson | 55e00f2 | 2008-08-17 18:02:44 +0000 | [diff] [blame] | 94 | 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 Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 125 | self.assertTrue(self.top.lookup("spam").get_namespace() is self.spam) |
Benjamin Peterson | 55e00f2 | 2008-08-17 18:02:44 +0000 | [diff] [blame] | 126 | 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 Peterson | b71caf1 | 2008-08-20 12:55:31 +0000 | [diff] [blame] | 147 | self.assertEqual(self.Mine.get_methods(), ('a_method',)) |
Benjamin Peterson | 55e00f2 | 2008-08-17 18:02:44 +0000 | [diff] [blame] | 148 | |
| 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 Norwitz | cd3e219 | 2006-01-23 07:49:36 +0000 | [diff] [blame] | 161 | |
| 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 | |
| 172 | def test_main(): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 173 | support.run_unittest(SymtableTest) |
Neal Norwitz | cd3e219 | 2006-01-23 07:49:36 +0000 | [diff] [blame] | 174 | |
| 175 | if __name__ == '__main__': |
| 176 | test_main() |