OLD | NEW |
1 ''' | 1 ''' |
2 Test cases for pyclbr.py | 2 Test cases for pyclbr.py |
3 Nick Mathewson | 3 Nick Mathewson |
4 ''' | 4 ''' |
5 from test.support import run_unittest | 5 from test.support import run_unittest |
6 import sys | 6 import sys |
7 from types import FunctionType, MethodType, BuiltinFunctionType | 7 from types import FunctionType, MethodType, BuiltinFunctionType |
8 import pyclbr | 8 import pyclbr |
9 from unittest import TestCase | 9 from unittest import TestCase |
10 | 10 |
(...skipping 27 matching lines...) Expand all Loading... |
38 def assertHaskey(self, obj, key, ignore): | 38 def assertHaskey(self, obj, key, ignore): |
39 ''' succeed iff key in obj or key in ignore. ''' | 39 ''' succeed iff key in obj or key in ignore. ''' |
40 if key in ignore: return | 40 if key in ignore: return |
41 if key not in obj: | 41 if key not in obj: |
42 print("***",key, file=sys.stderr) | 42 print("***",key, file=sys.stderr) |
43 self.assertIn(key, obj) | 43 self.assertIn(key, obj) |
44 | 44 |
45 def assertEqualsOrIgnored(self, a, b, ignore): | 45 def assertEqualsOrIgnored(self, a, b, ignore): |
46 ''' succeed iff a == b or a in ignore or b in ignore ''' | 46 ''' succeed iff a == b or a in ignore or b in ignore ''' |
47 if a not in ignore and b not in ignore: | 47 if a not in ignore and b not in ignore: |
48 self.assertEquals(a, b) | 48 self.assertEqual(a, b) |
49 | 49 |
50 def checkModule(self, moduleName, module=None, ignore=()): | 50 def checkModule(self, moduleName, module=None, ignore=()): |
51 ''' succeed iff pyclbr.readmodule_ex(modulename) corresponds | 51 ''' succeed iff pyclbr.readmodule_ex(modulename) corresponds |
52 to the actual module object, module. Any identifiers in | 52 to the actual module object, module. Any identifiers in |
53 ignore are ignored. If no module is provided, the appropriate | 53 ignore are ignored. If no module is provided, the appropriate |
54 module is loaded with __import__.''' | 54 module is loaded with __import__.''' |
55 | 55 |
56 ignore = set(ignore) | set(['object']) | 56 ignore = set(ignore) | set(['object']) |
57 | 57 |
58 if module is None: | 58 if module is None: |
(...skipping 21 matching lines...) Expand all Loading... |
80 # Make sure the toplevel functions and classes are the same. | 80 # Make sure the toplevel functions and classes are the same. |
81 for name, value in dict.items(): | 81 for name, value in dict.items(): |
82 if name in ignore: | 82 if name in ignore: |
83 continue | 83 continue |
84 self.assertHasattr(module, name, ignore) | 84 self.assertHasattr(module, name, ignore) |
85 py_item = getattr(module, name) | 85 py_item = getattr(module, name) |
86 if isinstance(value, pyclbr.Function): | 86 if isinstance(value, pyclbr.Function): |
87 self.assertIsInstance(py_item, (FunctionType, BuiltinFunctionTyp
e)) | 87 self.assertIsInstance(py_item, (FunctionType, BuiltinFunctionTyp
e)) |
88 if py_item.__module__ != moduleName: | 88 if py_item.__module__ != moduleName: |
89 continue # skip functions that came from somewhere else | 89 continue # skip functions that came from somewhere else |
90 self.assertEquals(py_item.__module__, value.module) | 90 self.assertEqual(py_item.__module__, value.module) |
91 else: | 91 else: |
92 self.assertIsInstance(py_item, type) | 92 self.assertIsInstance(py_item, type) |
93 if py_item.__module__ != moduleName: | 93 if py_item.__module__ != moduleName: |
94 continue # skip classes that came from somewhere else | 94 continue # skip classes that came from somewhere else |
95 | 95 |
96 real_bases = [base.__name__ for base in py_item.__bases__] | 96 real_bases = [base.__name__ for base in py_item.__bases__] |
97 pyclbr_bases = [ getattr(base, 'name', base) | 97 pyclbr_bases = [ getattr(base, 'name', base) |
98 for base in value.super ] | 98 for base in value.super ] |
99 | 99 |
100 try: | 100 try: |
101 self.assertListEq(real_bases, pyclbr_bases, ignore) | 101 self.assertListEq(real_bases, pyclbr_bases, ignore) |
102 except: | 102 except: |
103 print("class=%s" % py_item, file=sys.stderr) | 103 print("class=%s" % py_item, file=sys.stderr) |
104 raise | 104 raise |
105 | 105 |
106 actualMethods = [] | 106 actualMethods = [] |
107 for m in py_item.__dict__.keys(): | 107 for m in py_item.__dict__.keys(): |
108 if ismethod(py_item, getattr(py_item, m), m): | 108 if ismethod(py_item, getattr(py_item, m), m): |
109 actualMethods.append(m) | 109 actualMethods.append(m) |
110 foundMethods = [] | 110 foundMethods = [] |
111 for m in value.methods.keys(): | 111 for m in value.methods.keys(): |
112 if m[:2] == '__' and m[-2:] != '__': | 112 if m[:2] == '__' and m[-2:] != '__': |
113 foundMethods.append('_'+name+m) | 113 foundMethods.append('_'+name+m) |
114 else: | 114 else: |
115 foundMethods.append(m) | 115 foundMethods.append(m) |
116 | 116 |
117 try: | 117 try: |
118 self.assertListEq(foundMethods, actualMethods, ignore) | 118 self.assertListEq(foundMethods, actualMethods, ignore) |
119 self.assertEquals(py_item.__module__, value.module) | 119 self.assertEqual(py_item.__module__, value.module) |
120 | 120 |
121 self.assertEqualsOrIgnored(py_item.__name__, value.name, | 121 self.assertEqualsOrIgnored(py_item.__name__, value.name, |
122 ignore) | 122 ignore) |
123 # can't check file or lineno | 123 # can't check file or lineno |
124 except: | 124 except: |
125 print("class=%s" % py_item, file=sys.stderr) | 125 print("class=%s" % py_item, file=sys.stderr) |
126 raise | 126 raise |
127 | 127 |
128 # Now check for missing stuff. | 128 # Now check for missing stuff. |
129 def defined_in(item, module): | 129 def defined_in(item, module): |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
167 cm('email.parser') | 167 cm('email.parser') |
168 cm('test.test_pyclbr') | 168 cm('test.test_pyclbr') |
169 | 169 |
170 | 170 |
171 def test_main(): | 171 def test_main(): |
172 run_unittest(PyclbrTest) | 172 run_unittest(PyclbrTest) |
173 | 173 |
174 | 174 |
175 if __name__ == "__main__": | 175 if __name__ == "__main__": |
176 test_main() | 176 test_main() |
OLD | NEW |