2 Test cases for pyclbr.py
5 from test
.test_support
import run_unittest
, import_module
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 # Silence Py3k warning
15 import_module('commands', deprecated
=True)
17 # This next line triggers an error on old versions of pyclbr.
18 from commands
import getstatus
20 # Here we test the python class browser code.
22 # The main function in this suite, 'testModule', compares the output
23 # of pyclbr with the introspected members of a module. Because pyclbr
24 # is imperfect (as designed), testModule is called with a set of
27 class PyclbrTest(TestCase
):
29 def assertListEq(self
, l1
, l2
, ignore
):
30 ''' succeed iff {l1} - {ignore} == {l2} - {ignore} '''
31 missing
= (set(l1
) ^
set(l2
)) - set(ignore
)
33 print >>sys
.stderr
, "l1=%r\nl2=%r\nignore=%r" % (l1
, l2
, ignore
)
34 self
.fail("%r missing" % missing
.pop())
36 def assertHasattr(self
, obj
, attr
, ignore
):
37 ''' succeed iff hasattr(obj,attr) or attr in ignore. '''
38 if attr
in ignore
: return
39 if not hasattr(obj
, attr
): print "???", attr
40 self
.assertTrue(hasattr(obj
, attr
),
41 'expected hasattr(%r, %r)' % (obj
, attr
))
44 def assertHaskey(self
, obj
, key
, ignore
):
45 ''' succeed iff key in obj or key in ignore. '''
46 if key
in ignore
: return
48 print >>sys
.stderr
, "***", key
49 self
.assertIn(key
, obj
)
51 def assertEqualsOrIgnored(self
, a
, b
, ignore
):
52 ''' succeed iff a == b or a in ignore or b in ignore '''
53 if a
not in ignore
and b
not in ignore
:
54 self
.assertEqual(a
, b
)
56 def checkModule(self
, moduleName
, module
=None, ignore
=()):
57 ''' succeed iff pyclbr.readmodule_ex(modulename) corresponds
58 to the actual module object, module. Any identifiers in
59 ignore are ignored. If no module is provided, the appropriate
60 module is loaded with __import__.'''
64 # ('<silly>' is to work around an API silliness in __import__)
65 module
= __import__(moduleName
, globals(), {}, ['<silly>'])
67 dict = pyclbr
.readmodule_ex(moduleName
)
69 def ismethod(oclass
, obj
, name
):
70 classdict
= oclass
.__dict
__
71 if isinstance(obj
, FunctionType
):
72 if not isinstance(classdict
[name
], StaticMethodType
):
75 if not isinstance(obj
, MethodType
):
77 if obj
.im_self
is not None:
78 if (not isinstance(classdict
[name
], ClassMethodType
) or
79 obj
.im_self
is not oclass
):
82 if not isinstance(classdict
[name
], FunctionType
):
85 objname
= obj
.__name
__
86 if objname
.startswith("__") and not objname
.endswith("__"):
87 objname
= "_%s%s" % (obj
.im_class
.__name
__, objname
)
88 return objname
== name
90 # Make sure the toplevel functions and classes are the same.
91 for name
, value
in dict.items():
94 self
.assertHasattr(module
, name
, ignore
)
95 py_item
= getattr(module
, name
)
96 if isinstance(value
, pyclbr
.Function
):
97 self
.assertIsInstance(py_item
, (FunctionType
, BuiltinFunctionType
))
98 if py_item
.__module
__ != moduleName
:
99 continue # skip functions that came from somewhere else
100 self
.assertEquals(py_item
.__module
__, value
.module
)
102 self
.assertIsInstance(py_item
, (ClassType
, type))
103 if py_item
.__module
__ != moduleName
:
104 continue # skip classes that came from somewhere else
106 real_bases
= [base
.__name
__ for base
in py_item
.__bases
__]
107 pyclbr_bases
= [ getattr(base
, 'name', base
)
108 for base
in value
.super ]
111 self
.assertListEq(real_bases
, pyclbr_bases
, ignore
)
113 print >>sys
.stderr
, "class=%s" % py_item
117 for m
in py_item
.__dict
__.keys():
118 if ismethod(py_item
, getattr(py_item
, m
), m
):
119 actualMethods
.append(m
)
121 for m
in value
.methods
.keys():
122 if m
[:2] == '__' and m
[-2:] != '__':
123 foundMethods
.append('_'+name
+m
)
125 foundMethods
.append(m
)
128 self
.assertListEq(foundMethods
, actualMethods
, ignore
)
129 self
.assertEquals(py_item
.__module
__, value
.module
)
131 self
.assertEqualsOrIgnored(py_item
.__name
__, value
.name
,
133 # can't check file or lineno
135 print >>sys
.stderr
, "class=%s" % py_item
138 # Now check for missing stuff.
139 def defined_in(item
, module
):
140 if isinstance(item
, ClassType
):
141 return item
.__module
__ == module
.__name
__
142 if isinstance(item
, FunctionType
):
143 return item
.func_globals
is module
.__dict
__
145 for name
in dir(module
):
146 item
= getattr(module
, name
)
147 if isinstance(item
, (ClassType
, FunctionType
)):
148 if defined_in(item
, module
):
149 self
.assertHaskey(dict, name
, ignore
)
152 self
.checkModule('pyclbr')
153 self
.checkModule('doctest', ignore
=("DocTestCase",))
154 # Silence Py3k warning
155 rfc822
= import_module('rfc822', deprecated
=True)
156 self
.checkModule('rfc822', rfc822
)
157 self
.checkModule('difflib')
159 def test_decorators(self
):
160 # XXX: See comment in pyclbr_input.py for a test that would fail
161 # if it were not commented out.
163 self
.checkModule('test.pyclbr_input')
165 def test_others(self
):
166 cm
= self
.checkModule
168 # These were once about the 10 longest modules
169 cm('random', ignore
=('Random',)) # from _random import Random as CoreGenerator
170 cm('cgi', ignore
=('log',)) # set with = in module
171 cm('urllib', ignore
=('_CFNumberToInt32',
172 '_CStringFromCFString',
174 'getproxies_registry',
175 'proxy_bypass_registry',
176 'proxy_bypass_macosx_sysconf',
178 'getproxies_macosx_sysconf',
179 'getproxies_internetconfig',)) # not on all platforms
181 cm('aifc', ignore
=('openfp',)) # set with = in module
183 cm('sre_parse', ignore
=('dump',)) # from sre_constants import *
187 # Tests for modules inside packages
189 cm('test.test_pyclbr')
193 run_unittest(PyclbrTest
)
196 if __name__
== "__main__":