2 Test cases for pyclbr.py
5 from test_support
import run_unittest
7 from types
import ClassType
, FunctionType
, MethodType
10 # This next line triggers an error on old versions of pyclbr.
12 from commands
import getstatus
14 # Here we test the python class browser code.
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
21 class PyclbrTest(unittest
.TestCase
):
23 def assertListEq(self
, l1
, l2
, ignore
):
24 ''' succeed iff {l1} - {ignore} == {l2} - {ignore} '''
25 for p1
, p2
in (l1
, l2
), (l2
, l1
):
27 ok
= (item
in p2
) or (item
in ignore
)
29 self
.fail("%r missing" % item
)
32 def assertHasattr(self
, obj
, attr
, ignore
):
33 ''' succeed iff hasattr(obj,attr) or attr in ignore. '''
34 if attr
in ignore
: return
35 if not hasattr(obj
, attr
): print "???", attr
36 self
.failUnless(hasattr(obj
, attr
))
39 def assertHaskey(self
, obj
, key
, ignore
):
40 ''' succeed iff obj.has_key(key) or key in ignore. '''
41 if key
in ignore
: return
42 if not obj
.has_key(key
): print "***",key
43 self
.failUnless(obj
.has_key(key
))
45 def assertEquals(self
, a
, b
, ignore
=None):
46 ''' succeed iff a == b or a in ignore or b in ignore '''
47 if (ignore
== None) or (a
in ignore
) or (b
in ignore
): return
49 unittest
.TestCase
.assertEquals(self
, a
, b
)
51 def checkModule(self
, moduleName
, module
=None, ignore
=()):
52 ''' succeed iff pyclbr.readmodule_ex(modulename) corresponds
53 to the actual module object, module. Any identifiers in
54 ignore are ignored. If no module is provided, the appropriate
55 module is loaded with __import__.'''
58 module
= __import__(moduleName
, globals(), {}, [])
60 dict = pyclbr
.readmodule_ex(moduleName
)
62 # Make sure the toplevel functions and classes are the same.
63 for name
, value
in dict.items():
66 self
.assertHasattr(module
, name
, ignore
)
67 py_item
= getattr(module
, name
)
68 if isinstance(value
, pyclbr
.Function
):
69 self
.assertEquals(type(py_item
), FunctionType
)
71 self
.assertEquals(type(py_item
), ClassType
)
72 real_bases
= [base
.__name
__ for base
in py_item
.__bases
__]
73 pyclbr_bases
= [ getattr(base
, 'name', base
)
74 for base
in value
.super ]
76 self
.assertListEq(real_bases
, pyclbr_bases
, ignore
)
79 for m
in py_item
.__dict
__.keys():
80 if type(getattr(py_item
, m
)) == MethodType
:
81 actualMethods
.append(m
)
83 for m
in value
.methods
.keys():
84 if m
[:2] == '__' and m
[-2:] != '__':
85 foundMethods
.append('_'+name
+m
)
87 foundMethods
.append(m
)
89 self
.assertListEq(foundMethods
, actualMethods
, ignore
)
90 self
.assertEquals(py_item
.__module
__, value
.module
)
92 self
.assertEquals(py_item
.__name
__, value
.name
, ignore
)
93 # can't check file or lineno
95 # Now check for missing stuff.
96 for name
in dir(module
):
97 item
= getattr(module
, name
)
98 if type(item
) in (ClassType
, FunctionType
):
99 self
.assertHaskey(dict, name
, ignore
)
102 self
.checkModule('pyclbr')
103 self
.checkModule('doctest',
107 '_classify_class_attrs'])
108 self
.checkModule('rfc822')
109 self
.checkModule('xmllib')
110 self
.checkModule('difflib')
112 def test_others(self
):
113 cm
= self
.checkModule
115 # these are about the 20 longest modules.
117 cm('random', ignore
=('_verify',)) # deleted
119 cm('cgi', ignore
=('f', 'g', # nested declarations
120 'log')) # set with =, not def
122 cm('mhlib', ignore
=('do', # nested declaration
123 'bisect')) # imported method, set with =
125 cm('urllib', ignore
=('getproxies_environment', # set with =
126 'getproxies_registry', # set with =
127 'open_https')) # not on all platforms
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 =.
138 cm('pickle', ignore
=('g',)) # deleted declaration
140 cm('aifc', ignore
=('openfp',)) # set with =
142 cm('httplib', ignore
=('error', # set with =
144 'HTTP11')) # not on all platforms
146 cm('Cookie', ignore
=('__str__', 'Cookie')) # set with =
148 cm('sre_parse', ignore
=('literal', # nested def
149 'makedict', 'dump' # from sre_constants
152 cm('test.test_pyclbr',
153 module
=sys
.modules
[__name__
])
155 # pydoc doesn't work because of string issues
158 # pdb plays too many dynamic games
163 run_unittest(PyclbrTest
)
166 if __name__
== "__main__":