from test
.test_support
import run_unittest
from types
import ClassType
, FunctionType
, MethodType
, BuiltinFunctionType
from unittest
import TestCase
StaticMethodType
= type(staticmethod(lambda: None))
ClassMethodType
= type(classmethod(lambda c
: None))
# This next line triggers an error on old versions of pyclbr.
from commands
import getstatus
# Here we test the python class browser code.
# The main function in this suite, 'testModule', compares the output
# of pyclbr with the introspected members of a module. Because pyclbr
# is imperfect (as designed), testModule is called with a set of
class PyclbrTest(TestCase
):
def assertListEq(self
, l1
, l2
, ignore
):
''' succeed iff {l1} - {ignore} == {l2} - {ignore} '''
missing
= (set(l1
) ^
set(l2
)) - set(ignore
)
print >>sys
.stderr
, "l1=%r\nl2=%r\nignore=%r" % (l1
, l2
, ignore
)
self
.fail("%r missing" % missing
.pop())
def assertHasattr(self
, obj
, attr
, ignore
):
''' succeed iff hasattr(obj,attr) or attr in ignore. '''
if attr
in ignore
: return
if not hasattr(obj
, attr
): print "???", attr
self
.failUnless(hasattr(obj
, attr
),
'expected hasattr(%r, %r)' % (obj
, attr
))
def assertHaskey(self
, obj
, key
, ignore
):
''' succeed iff obj.has_key(key) or key in ignore. '''
print >>sys
.stderr
, "***",key
self
.failUnless(obj
.has_key(key
))
def assertEqualsOrIgnored(self
, a
, b
, ignore
):
''' succeed iff a == b or a in ignore or b in ignore '''
if a
not in ignore
and b
not in ignore
:
def checkModule(self
, moduleName
, module
=None, ignore
=()):
''' succeed iff pyclbr.readmodule_ex(modulename) corresponds
to the actual module object, module. Any identifiers in
ignore are ignored. If no module is provided, the appropriate
module is loaded with __import__.'''
# ('<silly>' is to work around an API silliness in __import__)
module
= __import__(moduleName
, globals(), {}, ['<silly>'])
dict = pyclbr
.readmodule_ex(moduleName
)
def ismethod(oclass
, obj
, name
):
classdict
= oclass
.__dict
__
if isinstance(obj
, FunctionType
):
if not isinstance(classdict
[name
], StaticMethodType
):
if not isinstance(obj
, MethodType
):
if obj
.im_self
is not None:
if (not isinstance(classdict
[name
], ClassMethodType
) or
obj
.im_self
is not oclass
):
if not isinstance(classdict
[name
], FunctionType
):
if objname
.startswith("__") and not objname
.endswith("__"):
objname
= "_%s%s" % (obj
.im_class
.__name
__, objname
)
# Make sure the toplevel functions and classes are the same.
for name
, value
in dict.items():
self
.assertHasattr(module
, name
, ignore
)
py_item
= getattr(module
, name
)
if isinstance(value
, pyclbr
.Function
):
self
.assert_(isinstance(py_item
, (FunctionType
, BuiltinFunctionType
)))
self
.failUnless(isinstance(py_item
, (ClassType
, type)))
real_bases
= [base
.__name
__ for base
in py_item
.__bases
__]
pyclbr_bases
= [ getattr(base
, 'name', base
)
for base
in value
.super ]
self
.assertListEq(real_bases
, pyclbr_bases
, ignore
)
print >>sys
.stderr
, "class=%s" % py_item
for m
in py_item
.__dict
__.keys():
if ismethod(py_item
, getattr(py_item
, m
), m
):
for m
in value
.methods
.keys():
if m
[:2] == '__' and m
[-2:] != '__':
foundMethods
.append('_'+name
+m
)
self
.assertListEq(foundMethods
, actualMethods
, ignore
)
self
.assertEquals(py_item
.__module
__, value
.module
)
self
.assertEqualsOrIgnored(py_item
.__name
__, value
.name
,
# can't check file or lineno
print >>sys
.stderr
, "class=%s" % py_item
# Now check for missing stuff.
def defined_in(item
, module
):
if isinstance(item
, ClassType
):
return item
.__module
__ == module
.__name
__
if isinstance(item
, FunctionType
):
return item
.func_globals
is module
.__dict
__
item
= getattr(module
, name
)
if isinstance(item
, (ClassType
, FunctionType
)):
if defined_in(item
, module
):
self
.assertHaskey(dict, name
, ignore
)
self
.checkModule('pyclbr')
self
.checkModule('doctest')
self
.checkModule('rfc822')
self
.checkModule('difflib')
def test_decorators(self
):
# XXX: See comment in pyclbr_input.py for a test that would fail
# if it were not commented out.
self
.checkModule('test.pyclbr_input')
# These were once about the 10 longest modules
cm('random', ignore
=('Random',)) # from _random import Random as CoreGenerator
cm('cgi', ignore
=('log',)) # set with = in module
cm('urllib', ignore
=('getproxies_registry',
'getproxies_internetconfig',)) # not on all platforms
cm('aifc', ignore
=('openfp',)) # set with = in module
cm('sre_parse', ignore
=('dump',)) # from sre_constants import *
# Tests for modules inside packages
if __name__
== "__main__":