PageRenderTime 345ms CodeModel.GetById 141ms app.highlight 62ms RepoModel.GetById 139ms app.codeStats 0ms

/Lib/test/test_symtable.py

http://unladen-swallow.googlecode.com/
Python | 202 lines | 196 code | 3 blank | 3 comment | 4 complexity | f9b8f956c529c0f8f3de27f46959a04d MD5 | raw file
  1"""
  2Test the API of the symtable module.
  3"""
  4import symtable
  5import unittest
  6import warnings
  7
  8from test import test_support
  9
 10
 11TEST_CODE = """
 12import sys
 13
 14glob = 42
 15
 16class Mine:
 17    instance_var = 24
 18    def a_method(p1, p2):
 19        pass
 20
 21def spam(a, b, *var, **kw):
 22    global bar
 23    bar = 47
 24    x = 23
 25    glob
 26    def internal():
 27        return x
 28    return internal
 29
 30def foo():
 31    exec 'm'
 32    from sys import *
 33
 34def namespace_test(): pass
 35def namespace_test(): pass
 36"""
 37
 38
 39def find_block(block, name):
 40    for ch in block.get_children():
 41        if ch.get_name() == name:
 42            return ch
 43
 44
 45class SymtableTest(unittest.TestCase):
 46
 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")
 57
 58    def test_noops(self):
 59        # Check methods that don't work. They should warn and return False.
 60        def check(w, msg):
 61            self.assertEqual(str(w.message), msg)
 62        sym = self.top.lookup("glob")
 63        with test_support.check_warnings() as w:
 64            warnings.simplefilter("always", DeprecationWarning)
 65            self.assertFalse(sym.is_vararg())
 66            check(w, "is_vararg() is obsolete and will be removed")
 67            w.reset()
 68            self.assertFalse(sym.is_keywordarg())
 69            check(w, "is_keywordarg() is obsolete and will be removed")
 70            w.reset()
 71            self.assertFalse(sym.is_in_tuple())
 72            check(w, "is_in_tuple() is obsolete and will be removed")
 73
 74    def test_type(self):
 75        self.assertEqual(self.top.get_type(), "module")
 76        self.assertEqual(self.Mine.get_type(), "class")
 77        self.assertEqual(self.a_method.get_type(), "function")
 78        self.assertEqual(self.spam.get_type(), "function")
 79        self.assertEqual(self.internal.get_type(), "function")
 80
 81    def test_optimized(self):
 82        self.assertFalse(self.top.is_optimized())
 83        self.assertFalse(self.top.has_exec())
 84        self.assertFalse(self.top.has_import_star())
 85
 86        self.assertTrue(self.spam.is_optimized())
 87
 88        self.assertFalse(self.foo.is_optimized())
 89        self.assertTrue(self.foo.has_exec())
 90        self.assertTrue(self.foo.has_import_star())
 91
 92    def test_nested(self):
 93        self.assertFalse(self.top.is_nested())
 94        self.assertFalse(self.Mine.is_nested())
 95        self.assertFalse(self.spam.is_nested())
 96        self.assertTrue(self.internal.is_nested())
 97
 98    def test_children(self):
 99        self.assertTrue(self.top.has_children())
100        self.assertTrue(self.Mine.has_children())
101        self.assertFalse(self.foo.has_children())
102
103    def test_lineno(self):
104        self.assertEqual(self.top.get_lineno(), 0)
105        self.assertEqual(self.spam.get_lineno(), 11)
106
107    def test_function_info(self):
108        func = self.spam
109        self.assertEqual(func.get_parameters(), ("a", "b", "kw", "var"))
110        self.assertEqual(func.get_locals(),
111                         ("a", "b", "bar", "internal", "kw", "var", "x"))
112        self.assertEqual(func.get_globals(), ("bar", "glob"))
113        self.assertEqual(self.internal.get_frees(), ("x",))
114
115    def test_globals(self):
116        self.assertTrue(self.spam.lookup("glob").is_global())
117        self.assertFalse(self.spam.lookup("glob").is_declared_global())
118        self.assertTrue(self.spam.lookup("bar").is_global())
119        self.assertTrue(self.spam.lookup("bar").is_declared_global())
120        self.assertFalse(self.internal.lookup("x").is_global())
121        self.assertFalse(self.Mine.lookup("instance_var").is_global())
122
123    def test_local(self):
124        self.assertTrue(self.spam.lookup("x").is_local())
125        self.assertFalse(self.internal.lookup("x").is_local())
126
127    def test_referenced(self):
128        self.assertTrue(self.internal.lookup("x").is_referenced())
129        self.assertTrue(self.spam.lookup("internal").is_referenced())
130        self.assertFalse(self.spam.lookup("x").is_referenced())
131
132    def test_parameters(self):
133        for sym in ("a", "var", "kw"):
134            self.assertTrue(self.spam.lookup(sym).is_parameter())
135        self.assertFalse(self.spam.lookup("x").is_parameter())
136
137    def test_symbol_lookup(self):
138        self.assertEqual(len(self.top.get_identifiers()),
139                         len(self.top.get_symbols()))
140
141        self.assertRaises(KeyError, self.top.lookup, "not_here")
142
143    def test_namespaces(self):
144        self.assertTrue(self.top.lookup("Mine").is_namespace())
145        self.assertTrue(self.Mine.lookup("a_method").is_namespace())
146        self.assertTrue(self.top.lookup("spam").is_namespace())
147        self.assertTrue(self.spam.lookup("internal").is_namespace())
148        self.assertTrue(self.top.lookup("namespace_test").is_namespace())
149        self.assertFalse(self.spam.lookup("x").is_namespace())
150
151        self.assert_(self.top.lookup("spam").get_namespace() is self.spam)
152        ns_test = self.top.lookup("namespace_test")
153        self.assertEqual(len(ns_test.get_namespaces()), 2)
154        self.assertRaises(ValueError, ns_test.get_namespace)
155
156    def test_assigned(self):
157        self.assertTrue(self.spam.lookup("x").is_assigned())
158        self.assertTrue(self.spam.lookup("bar").is_assigned())
159        self.assertTrue(self.top.lookup("spam").is_assigned())
160        self.assertTrue(self.Mine.lookup("a_method").is_assigned())
161        self.assertFalse(self.internal.lookup("x").is_assigned())
162
163    def test_imported(self):
164        self.assertTrue(self.top.lookup("sys").is_imported())
165
166    def test_name(self):
167        self.assertEqual(self.top.get_name(), "top")
168        self.assertEqual(self.spam.get_name(), "spam")
169        self.assertEqual(self.spam.lookup("x").get_name(), "x")
170        self.assertEqual(self.Mine.get_name(), "Mine")
171
172    def test_class_info(self):
173        self.assertEqual(self.Mine.get_methods(), ('a_method',))
174
175    def test_filename_correct(self):
176        ### Bug tickler: SyntaxError file name correct whether error raised
177        ### while parsing or building symbol table.
178        def checkfilename(brokencode):
179            try:
180                symtable.symtable(brokencode, "spam", "exec")
181            except SyntaxError as e:
182                self.assertEqual(e.filename, "spam")
183            else:
184                self.fail("no SyntaxError for %r" % (brokencode,))
185        checkfilename("def f(x): foo)(")  # parse-time
186        checkfilename("def f(x): global x")  # symtable-build-time
187
188    def test_eval(self):
189        symbols = symtable.symtable("42", "?", "eval")
190
191    def test_single(self):
192        symbols = symtable.symtable("42", "?", "single")
193
194    def test_exec(self):
195        symbols = symtable.symtable("def f(x): return x", "?", "exec")
196
197
198def test_main():
199    test_support.run_unittest(SymtableTest)
200
201if __name__ == '__main__':
202    test_main()