2 Test cases for pyclbr.py
5 from test
.test_support
import run_unittest
7 from types
import ClassType
, FunctionType
, MethodType
, BuiltinFunctionType
9 from unittest
import TestCase
11 StaticMethodType
= type(staticmethod(lambda: None))
12 ClassMethodType
= type(classmethod(lambda c
: None))
14 # This next line triggers an error on old versions of pyclbr.
16 from commands
import getstatus
18 # Here we test the python class browser code.
20 # The main function in this suite, 'testModule', compares the output
21 # of pyclbr with the introspected members of a module. Because pyclbr
22 # is imperfect (as designed), testModule is called with a set of
25 class PyclbrTest(TestCase
):
27 def assertListEq(self
, l1
, l2
, ignore
):
28 ''' succeed iff {l1} - {ignore} == {l2} - {ignore} '''
29 missing
= (set(l1
) ^
set(l2
)) - set(ignore
)
31 print >>sys
.stderr
, "l1=%r\nl2=%r\nignore=%r" % (l1
, l2
, ignore
)
32 self
.fail("%r missing" % missing
.pop())
34 def assertHasattr(self
, obj
, attr
, ignore
):
35 ''' succeed iff hasattr(obj,attr) or attr in ignore. '''
36 if attr
in ignore
: return
37 if not hasattr(obj
, attr
): print "???", attr
38 self
.assertTrue(hasattr(obj
, attr
),
39 'expected hasattr(%r, %r)' % (obj
, attr
))
42 def assertHaskey(self
, obj
, key
, ignore
):
43 ''' succeed iff obj.has_key(key) or key in ignore. '''
44 if key
in ignore
: return
45 if not obj
.has_key(key
):
46 print >>sys
.stderr
, "***",key
47 self
.assertTrue(obj
.has_key(key
))
49 def assertEqualsOrIgnored(self
, a
, b
, ignore
):
50 ''' succeed iff a == b or a in ignore or b in ignore '''
51 if a
not in ignore
and b
not in ignore
:
52 self
.assertEquals(a
, b
)
54 def checkModule(self
, moduleName
, module
=None, ignore
=()):
55 ''' succeed iff pyclbr.readmodule_ex(modulename) corresponds
56 to the actual module object, module. Any identifiers in
57 ignore are ignored. If no module is provided, the appropriate
58 module is loaded with __import__.'''
62 # ('<silly>' is to work around an API silliness in __import__)
63 module
= __import__(moduleName
, globals(), {}, ['<silly>'])
65 dict = pyclbr
.readmodule_ex(moduleName
)
67 def ismethod(oclass
, obj
, name
):
68 classdict
= oclass
.__dict
__
69 if isinstance(obj
, FunctionType
):
70 if not isinstance(classdict
[name
], StaticMethodType
):
73 if not isinstance(obj
, MethodType
):
75 if obj
.im_self
is not None:
76 if (not isinstance(classdict
[name
], ClassMethodType
) or
77 obj
.im_self
is not oclass
):
80 if not isinstance(classdict
[name
], FunctionType
):
83 objname
= obj
.__name
__
84 if objname
.startswith("__") and not objname
.endswith("__"):
85 objname
= "_%s%s" % (obj
.im_class
.__name
__, objname
)
86 return objname
== name
88 # Make sure the toplevel functions and classes are the same.
89 for name
, value
in dict.items():
92 self
.assertHasattr(module
, name
, ignore
)
93 py_item
= getattr(module
, name
)
94 if isinstance(value
, pyclbr
.Function
):
95 self
.assertTrue(isinstance(py_item
, (FunctionType
, BuiltinFunctionType
)))
96 if py_item
.__module
__ != moduleName
:
97 continue # skip functions that came from somewhere else
98 self
.assertEquals(py_item
.__module
__, value
.module
)
100 self
.assertTrue(isinstance(py_item
, (ClassType
, type)))
101 if py_item
.__module
__ != moduleName
:
102 continue # skip classes that came from somewhere else
104 real_bases
= [base
.__name
__ for base
in py_item
.__bases
__]
105 pyclbr_bases
= [ getattr(base
, 'name', base
)
106 for base
in value
.super ]
109 self
.assertListEq(real_bases
, pyclbr_bases
, ignore
)
111 print >>sys
.stderr
, "class=%s" % py_item
115 for m
in py_item
.__dict
__.keys():
116 if ismethod(py_item
, getattr(py_item
, m
), m
):
117 actualMethods
.append(m
)
119 for m
in value
.methods
.keys():
120 if m
[:2] == '__' and m
[-2:] != '__':
121 foundMethods
.append('_'+name
+m
)
123 foundMethods
.append(m
)
126 self
.assertListEq(foundMethods
, actualMethods
, ignore
)
127 self
.assertEquals(py_item
.__module
__, value
.module
)
129 self
.assertEqualsOrIgnored(py_item
.__name
__, value
.name
,
131 # can't check file or lineno
133 print >>sys
.stderr
, "class=%s" % py_item
136 # Now check for missing stuff.
137 def defined_in(item
, module
):
138 if isinstance(item
, ClassType
):
139 return item
.__module
__ == module
.__name
__
140 if isinstance(item
, FunctionType
):
141 return item
.func_globals
is module
.__dict
__
143 for name
in dir(module
):
144 item
= getattr(module
, name
)
145 if isinstance(item
, (ClassType
, FunctionType
)):
146 if defined_in(item
, module
):
147 self
.assertHaskey(dict, name
, ignore
)
150 self
.checkModule('pyclbr')
151 self
.checkModule('doctest', ignore
=("DocTestCase",))
152 self
.checkModule('rfc822')
153 self
.checkModule('difflib')
155 def test_decorators(self
):
156 # XXX: See comment in pyclbr_input.py for a test that would fail
157 # if it were not commented out.
159 self
.checkModule('test.pyclbr_input')
161 def test_others(self
):
162 cm
= self
.checkModule
164 # These were once about the 10 longest modules
165 cm('random', ignore
=('Random',)) # from _random import Random as CoreGenerator
166 cm('cgi', ignore
=('log',)) # set with = in module
167 cm('urllib', ignore
=('_CFNumberToInt32',
168 '_CStringFromCFString',
170 'getproxies_registry',
171 'proxy_bypass_registry',
172 'proxy_bypass_macosx_sysconf',
174 'getproxies_macosx_sysconf',
175 'getproxies_internetconfig',)) # not on all platforms
177 cm('aifc', ignore
=('openfp',)) # set with = in module
179 cm('sre_parse', ignore
=('dump',)) # from sre_constants import *
183 # Tests for modules inside packages
185 cm('test.test_pyclbr')
189 run_unittest(PyclbrTest
)
192 if __name__
== "__main__":