blob: ce352afc4616cab86e50a6e3aebb28e94b3bcc88 [file] [log] [blame]
Fred Drake3a28ca82001-08-13 20:26:19 +00001'''
2 Test cases for pyclbr.py
3 Nick Mathewson
4'''
5from test_support import run_unittest
6import unittest, sys
7from types import ClassType, FunctionType, MethodType
8import pyclbr
9
10# This next line triggers an error on old versions of pyclbr.
11
Tim Peters04601062001-08-13 22:25:24 +000012from commands import getstatus
Fred Drake3a28ca82001-08-13 20:26:19 +000013
Tim Peters04601062001-08-13 22:25:24 +000014# Here we test the python class browser code.
Fred Drake3a28ca82001-08-13 20:26:19 +000015#
16# The main function in this suite, 'testModule', compares the output
17# of pyclbr with the introspected members of a module. Because pyclbr
18# is imperfect (as designed), testModule is called with a set of
19# members to ignore.
20
21class PyclbrTest(unittest.TestCase):
22
23 def assertListEq(self, l1, l2, ignore):
24 ''' succeed iff {l1} - {ignore} == {l2} - {ignore} '''
25 for p1, p2 in (l1, l2), (l2, l1):
26 for item in p1:
27 ok = (item in p2) or (item in ignore)
28 if not ok:
29 self.fail("%r missing" % item)
Tim Peters04601062001-08-13 22:25:24 +000030
31
Fred Drake3a28ca82001-08-13 20:26:19 +000032 def assertHasattr(self, obj, attr, ignore):
33 ''' succeed iff hasattr(obj,attr) or attr in ignore. '''
34 if attr in ignore: return
Tim Peters7402f792001-10-02 03:53:41 +000035 if not hasattr(obj, attr): print "???", attr
Tim Peters5e5ca562002-07-10 02:37:21 +000036 self.failUnless(hasattr(obj, attr),
37 'expected hasattr(%r, %r)' % (obj, attr))
Fred Drake3a28ca82001-08-13 20:26:19 +000038
39
40 def assertHaskey(self, obj, key, ignore):
41 ''' succeed iff obj.has_key(key) or key in ignore. '''
42 if key in ignore: return
43 if not obj.has_key(key): print "***",key
44 self.failUnless(obj.has_key(key))
45
46 def assertEquals(self, a, b, ignore=None):
47 ''' succeed iff a == b or a in ignore or b in ignore '''
48 if (ignore == None) or (a in ignore) or (b in ignore): return
49
50 unittest.TestCase.assertEquals(self, a, b)
51
52 def checkModule(self, moduleName, module=None, ignore=()):
53 ''' succeed iff pyclbr.readmodule_ex(modulename) corresponds
54 to the actual module object, module. Any identifiers in
55 ignore are ignored. If no module is provided, the appropriate
56 module is loaded with __import__.'''
57
58 if module == None:
Tim Peters04601062001-08-13 22:25:24 +000059 module = __import__(moduleName, globals(), {}, [])
Fred Drake3a28ca82001-08-13 20:26:19 +000060
61 dict = pyclbr.readmodule_ex(moduleName)
62
63 # Make sure the toplevel functions and classes are the same.
64 for name, value in dict.items():
Tim Peters04601062001-08-13 22:25:24 +000065 if name in ignore:
Fred Drake3a28ca82001-08-13 20:26:19 +000066 continue
67 self.assertHasattr(module, name, ignore)
68 py_item = getattr(module, name)
69 if isinstance(value, pyclbr.Function):
70 self.assertEquals(type(py_item), FunctionType)
71 else:
72 self.assertEquals(type(py_item), ClassType)
73 real_bases = [base.__name__ for base in py_item.__bases__]
Tim Peters04601062001-08-13 22:25:24 +000074 pyclbr_bases = [ getattr(base, 'name', base)
Fred Drake3a28ca82001-08-13 20:26:19 +000075 for base in value.super ]
Tim Peters04601062001-08-13 22:25:24 +000076
Fred Drake3a28ca82001-08-13 20:26:19 +000077 self.assertListEq(real_bases, pyclbr_bases, ignore)
78
79 actualMethods = []
Tim Peters37a309d2001-09-04 01:20:04 +000080 for m in py_item.__dict__.keys():
Fred Drake3a28ca82001-08-13 20:26:19 +000081 if type(getattr(py_item, m)) == MethodType:
82 actualMethods.append(m)
83 foundMethods = []
84 for m in value.methods.keys():
85 if m[:2] == '__' and m[-2:] != '__':
86 foundMethods.append('_'+name+m)
87 else:
88 foundMethods.append(m)
89
90 self.assertListEq(foundMethods, actualMethods, ignore)
91 self.assertEquals(py_item.__module__, value.module)
92
93 self.assertEquals(py_item.__name__, value.name, ignore)
94 # can't check file or lineno
95
96 # Now check for missing stuff.
97 for name in dir(module):
98 item = getattr(module, name)
99 if type(item) in (ClassType, FunctionType):
100 self.assertHaskey(dict, name, ignore)
101
102 def test_easy(self):
103 self.checkModule('pyclbr')
Tim Peters7402f792001-10-02 03:53:41 +0000104 self.checkModule('doctest',
Tim Peters17111f32001-10-03 04:08:26 +0000105 ignore=['_isclass',
106 '_isfunction',
107 '_ismodule',
108 '_classify_class_attrs'])
Guido van Rossumd842e072002-06-05 19:07:39 +0000109 self.checkModule('rfc822', ignore=["get"])
Fred Drake3a28ca82001-08-13 20:26:19 +0000110 self.checkModule('difflib')
111
112 def test_others(self):
113 cm = self.checkModule
114
115 # these are about the 20 longest modules.
116
117 cm('random', ignore=('_verify',)) # deleted
118
119 cm('cgi', ignore=('f', 'g', # nested declarations
120 'log')) # set with =, not def
121
122 cm('mhlib', ignore=('do', # nested declaration
123 'bisect')) # imported method, set with =
124
125 cm('urllib', ignore=('getproxies_environment', # set with =
Tim Peters04601062001-08-13 22:25:24 +0000126 'getproxies_registry', # set with =
127 'open_https')) # not on all platforms
Fred Drake3a28ca82001-08-13 20:26:19 +0000128
129 #XXXX bad example
130 #cm('urllib2', ignore=('at_cnri', # defined inside __main__
131 # '__super_init', # set with =.
132 # '_HTTPError__super_init', # set with =.
133 # 'http_error_301', # set with =.
134 # ))
135
136
Fred Drake3a28ca82001-08-13 20:26:19 +0000137
Tim Peters04601062001-08-13 22:25:24 +0000138 cm('pickle', ignore=('g',)) # deleted declaration
139
140 cm('aifc', ignore=('openfp',)) # set with =
141
142 cm('httplib', ignore=('error', # set with =
Tim Peters5e5ca562002-07-10 02:37:21 +0000143 'sendall', # set with =
144 '_closedsocket', # it's a nested class
Jeremy Hylton547a3162002-03-08 21:31:59 +0000145 'HTTPS',
146 'HTTP11')) # not on all platforms
Fred Drake3a28ca82001-08-13 20:26:19 +0000147
148 cm('Cookie', ignore=('__str__', 'Cookie')) # set with =
Tim Peters04601062001-08-13 22:25:24 +0000149
Fred Drake3a28ca82001-08-13 20:26:19 +0000150 cm('sre_parse', ignore=('literal', # nested def
151 'makedict', 'dump' # from sre_constants
152 ))
153
Tim Peters04601062001-08-13 22:25:24 +0000154 cm('test.test_pyclbr',
Fred Drake3a28ca82001-08-13 20:26:19 +0000155 module=sys.modules[__name__])
156
157 # pydoc doesn't work because of string issues
158 # cm('pydoc', pydoc)
Tim Peters04601062001-08-13 22:25:24 +0000159
Fred Drake3a28ca82001-08-13 20:26:19 +0000160 # pdb plays too many dynamic games
Tim Peters04601062001-08-13 22:25:24 +0000161 # cm('pdb', pdb)
Fred Drake3a28ca82001-08-13 20:26:19 +0000162
Fred Drake2e2be372001-09-20 21:33:42 +0000163
164def test_main():
165 run_unittest(PyclbrTest)
166
167
168if __name__ == "__main__":
169 test_main()