2001-08-13 17:26:19 -03:00
|
|
|
'''
|
|
|
|
Test cases for pyclbr.py
|
|
|
|
Nick Mathewson
|
|
|
|
'''
|
2002-07-23 16:04:11 -03:00
|
|
|
from test.test_support import run_unittest
|
2001-08-13 17:26:19 -03:00
|
|
|
import unittest, sys
|
|
|
|
from types import ClassType, FunctionType, MethodType
|
|
|
|
import pyclbr
|
2002-12-02 10:54:20 -04:00
|
|
|
from unittest import TestCase
|
2001-08-13 17:26:19 -03:00
|
|
|
|
|
|
|
# This next line triggers an error on old versions of pyclbr.
|
|
|
|
|
2001-08-13 19:25:24 -03:00
|
|
|
from commands import getstatus
|
2001-08-13 17:26:19 -03:00
|
|
|
|
2001-08-13 19:25:24 -03:00
|
|
|
# Here we test the python class browser code.
|
2001-08-13 17:26:19 -03:00
|
|
|
#
|
|
|
|
# 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
|
|
|
|
# members to ignore.
|
|
|
|
|
2002-12-02 10:54:20 -04:00
|
|
|
class PyclbrTest(TestCase):
|
2001-08-13 17:26:19 -03:00
|
|
|
|
|
|
|
def assertListEq(self, l1, l2, ignore):
|
|
|
|
''' succeed iff {l1} - {ignore} == {l2} - {ignore} '''
|
2003-11-16 12:17:49 -04:00
|
|
|
missing = (set(l1) ^ set(l2)) - set(ignore)
|
2003-05-02 06:06:28 -03:00
|
|
|
if missing:
|
2002-12-03 04:16:50 -04:00
|
|
|
print >>sys.stderr, "l1=%r\nl2=%r\nignore=%r" % (l1, l2, ignore)
|
2003-05-02 06:06:28 -03:00
|
|
|
self.fail("%r missing" % missing.pop())
|
2001-08-13 19:25:24 -03:00
|
|
|
|
2001-08-13 17:26:19 -03:00
|
|
|
def assertHasattr(self, obj, attr, ignore):
|
|
|
|
''' succeed iff hasattr(obj,attr) or attr in ignore. '''
|
|
|
|
if attr in ignore: return
|
2001-10-02 00:53:41 -03:00
|
|
|
if not hasattr(obj, attr): print "???", attr
|
2002-07-09 23:37:21 -03:00
|
|
|
self.failUnless(hasattr(obj, attr),
|
|
|
|
'expected hasattr(%r, %r)' % (obj, attr))
|
2001-08-13 17:26:19 -03:00
|
|
|
|
|
|
|
|
|
|
|
def assertHaskey(self, obj, key, ignore):
|
|
|
|
''' succeed iff obj.has_key(key) or key in ignore. '''
|
|
|
|
if key in ignore: return
|
2002-12-02 10:54:20 -04:00
|
|
|
if not obj.has_key(key):
|
|
|
|
print >>sys.stderr, "***",key
|
2001-08-13 17:26:19 -03:00
|
|
|
self.failUnless(obj.has_key(key))
|
|
|
|
|
|
|
|
def assertEquals(self, a, b, ignore=None):
|
|
|
|
''' succeed iff a == b or a in ignore or b in ignore '''
|
|
|
|
if (ignore == None) or (a in ignore) or (b in ignore): return
|
|
|
|
|
|
|
|
unittest.TestCase.assertEquals(self, a, b)
|
|
|
|
|
|
|
|
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__.'''
|
|
|
|
|
|
|
|
if module == None:
|
2002-12-02 10:54:20 -04:00
|
|
|
# Import it.
|
|
|
|
# ('<silly>' is to work around an API silliness in __import__)
|
|
|
|
module = __import__(moduleName, globals(), {}, ['<silly>'])
|
2001-08-13 17:26:19 -03:00
|
|
|
|
|
|
|
dict = pyclbr.readmodule_ex(moduleName)
|
|
|
|
|
2002-12-03 04:16:50 -04:00
|
|
|
def ismethod(obj, name):
|
|
|
|
if not isinstance(obj, MethodType):
|
|
|
|
return False
|
|
|
|
if obj.im_self is not None:
|
|
|
|
return False
|
|
|
|
objname = obj.__name__
|
|
|
|
if objname.startswith("__") and not objname.endswith("__"):
|
|
|
|
objname = "_%s%s" % (obj.im_class.__name__, objname)
|
|
|
|
return objname == name
|
|
|
|
|
2001-08-13 17:26:19 -03:00
|
|
|
# Make sure the toplevel functions and classes are the same.
|
|
|
|
for name, value in dict.items():
|
2001-08-13 19:25:24 -03:00
|
|
|
if name in ignore:
|
2001-08-13 17:26:19 -03:00
|
|
|
continue
|
|
|
|
self.assertHasattr(module, name, ignore)
|
|
|
|
py_item = getattr(module, name)
|
|
|
|
if isinstance(value, pyclbr.Function):
|
|
|
|
self.assertEquals(type(py_item), FunctionType)
|
|
|
|
else:
|
|
|
|
self.assertEquals(type(py_item), ClassType)
|
|
|
|
real_bases = [base.__name__ for base in py_item.__bases__]
|
2001-08-13 19:25:24 -03:00
|
|
|
pyclbr_bases = [ getattr(base, 'name', base)
|
2001-08-13 17:26:19 -03:00
|
|
|
for base in value.super ]
|
2001-08-13 19:25:24 -03:00
|
|
|
|
2002-12-02 10:54:20 -04:00
|
|
|
try:
|
|
|
|
self.assertListEq(real_bases, pyclbr_bases, ignore)
|
|
|
|
except:
|
|
|
|
print >>sys.stderr, "class=%s" % py_item
|
|
|
|
raise
|
2001-08-13 17:26:19 -03:00
|
|
|
|
|
|
|
actualMethods = []
|
2001-09-03 22:20:04 -03:00
|
|
|
for m in py_item.__dict__.keys():
|
2002-12-03 04:16:50 -04:00
|
|
|
if ismethod(getattr(py_item, m), m):
|
2001-08-13 17:26:19 -03:00
|
|
|
actualMethods.append(m)
|
|
|
|
foundMethods = []
|
|
|
|
for m in value.methods.keys():
|
|
|
|
if m[:2] == '__' and m[-2:] != '__':
|
|
|
|
foundMethods.append('_'+name+m)
|
|
|
|
else:
|
|
|
|
foundMethods.append(m)
|
|
|
|
|
2002-12-03 04:16:50 -04:00
|
|
|
try:
|
|
|
|
self.assertListEq(foundMethods, actualMethods, ignore)
|
|
|
|
self.assertEquals(py_item.__module__, value.module)
|
2001-08-13 17:26:19 -03:00
|
|
|
|
2002-12-03 04:16:50 -04:00
|
|
|
self.assertEquals(py_item.__name__, value.name, ignore)
|
|
|
|
# can't check file or lineno
|
|
|
|
except:
|
|
|
|
print >>sys.stderr, "class=%s" % py_item
|
|
|
|
raise
|
2001-08-13 17:26:19 -03:00
|
|
|
|
|
|
|
# Now check for missing stuff.
|
2002-12-02 10:54:20 -04:00
|
|
|
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__
|
|
|
|
return False
|
2001-08-13 17:26:19 -03:00
|
|
|
for name in dir(module):
|
|
|
|
item = getattr(module, name)
|
2002-12-02 10:54:20 -04:00
|
|
|
if isinstance(item, (ClassType, FunctionType)):
|
|
|
|
if defined_in(item, module):
|
|
|
|
self.assertHaskey(dict, name, ignore)
|
2001-08-13 17:26:19 -03:00
|
|
|
|
|
|
|
def test_easy(self):
|
|
|
|
self.checkModule('pyclbr')
|
2002-12-03 04:16:50 -04:00
|
|
|
self.checkModule('doctest')
|
|
|
|
self.checkModule('rfc822')
|
2001-08-13 17:26:19 -03:00
|
|
|
self.checkModule('difflib')
|
|
|
|
|
|
|
|
def test_others(self):
|
|
|
|
cm = self.checkModule
|
|
|
|
|
2002-12-03 04:16:50 -04:00
|
|
|
# These were once about the 10 longest modules
|
2002-12-30 03:21:32 -04:00
|
|
|
cm('random', ignore=('Random',)) # from _random import Random as CoreGenerator
|
2002-12-03 04:16:50 -04:00
|
|
|
cm('cgi', ignore=('log',)) # set with = in module
|
|
|
|
cm('mhlib')
|
|
|
|
cm('urllib', ignore=('getproxies_registry',
|
|
|
|
'open_https')) # not on all platforms
|
|
|
|
cm('pickle', ignore=('g',)) # from types import *
|
|
|
|
cm('aifc', ignore=('openfp',)) # set with = in module
|
|
|
|
cm('Cookie')
|
|
|
|
cm('sre_parse', ignore=('dump',)) # from sre_constants import *
|
|
|
|
cm('pdb')
|
|
|
|
cm('pydoc')
|
2001-08-13 17:26:19 -03:00
|
|
|
|
2002-12-02 10:54:20 -04:00
|
|
|
# Tests for modules inside packages
|
|
|
|
cm('email.Parser')
|
2002-12-03 04:16:50 -04:00
|
|
|
cm('test.test_pyclbr')
|
2001-08-13 17:26:19 -03:00
|
|
|
|
2001-09-20 18:33:42 -03:00
|
|
|
|
|
|
|
def test_main():
|
|
|
|
run_unittest(PyclbrTest)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
test_main()
|