2001-08-13 17:26:19 -03:00
|
|
|
'''
|
|
|
|
Test cases for pyclbr.py
|
|
|
|
Nick Mathewson
|
|
|
|
'''
|
2010-01-30 03:22:54 -04:00
|
|
|
from test.test_support import run_unittest, import_module
|
2008-02-23 13:40:11 -04:00
|
|
|
import sys
|
2004-09-04 20:53:20 -03:00
|
|
|
from types import ClassType, FunctionType, MethodType, BuiltinFunctionType
|
2001-08-13 17:26:19 -03:00
|
|
|
import pyclbr
|
2002-12-02 10:54:20 -04:00
|
|
|
from unittest import TestCase
|
2001-08-13 17:26:19 -03:00
|
|
|
|
2004-08-02 03:10:11 -03:00
|
|
|
StaticMethodType = type(staticmethod(lambda: None))
|
|
|
|
ClassMethodType = type(classmethod(lambda c: None))
|
|
|
|
|
2010-01-30 03:22:54 -04:00
|
|
|
# Silence Py3k warning
|
|
|
|
import_module('commands', deprecated=True)
|
2001-08-13 17:26:19 -03:00
|
|
|
|
2010-01-30 03:22:54 -04: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
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(hasattr(obj, attr),
|
2002-07-09 23:37:21 -03:00
|
|
|
'expected hasattr(%r, %r)' % (obj, attr))
|
2001-08-13 17:26:19 -03:00
|
|
|
|
|
|
|
|
|
|
|
def assertHaskey(self, obj, key, ignore):
|
2010-01-30 03:22:54 -04:00
|
|
|
''' succeed iff key in obj or key in ignore. '''
|
2001-08-13 17:26:19 -03:00
|
|
|
if key in ignore: return
|
2010-01-30 03:22:54 -04:00
|
|
|
if key not in obj:
|
|
|
|
print >>sys.stderr, "***", key
|
|
|
|
self.assertIn(key, obj)
|
2001-08-13 17:26:19 -03:00
|
|
|
|
2004-08-02 03:10:11 -03:00
|
|
|
def assertEqualsOrIgnored(self, a, b, ignore):
|
2001-08-13 17:26:19 -03:00
|
|
|
''' succeed iff a == b or a in ignore or b in ignore '''
|
2004-08-02 03:10:11 -03:00
|
|
|
if a not in ignore and b not in ignore:
|
2010-01-30 03:22:54 -04:00
|
|
|
self.assertEqual(a, b)
|
2001-08-13 17:26:19 -03:00
|
|
|
|
|
|
|
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__.'''
|
|
|
|
|
2008-03-29 12:24:25 -03:00
|
|
|
if module is 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)
|
|
|
|
|
2004-08-02 03:10:11 -03:00
|
|
|
def ismethod(oclass, obj, name):
|
|
|
|
classdict = oclass.__dict__
|
|
|
|
if isinstance(obj, FunctionType):
|
|
|
|
if not isinstance(classdict[name], StaticMethodType):
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
if not isinstance(obj, MethodType):
|
|
|
|
return False
|
|
|
|
if obj.im_self is not None:
|
|
|
|
if (not isinstance(classdict[name], ClassMethodType) or
|
|
|
|
obj.im_self is not oclass):
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
if not isinstance(classdict[name], FunctionType):
|
|
|
|
return False
|
|
|
|
|
2002-12-03 04:16:50 -04:00
|
|
|
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):
|
2010-01-24 12:58:36 -04:00
|
|
|
self.assertIsInstance(py_item, (FunctionType, BuiltinFunctionType))
|
2006-09-30 08:06:47 -03:00
|
|
|
if py_item.__module__ != moduleName:
|
|
|
|
continue # skip functions that came from somewhere else
|
2010-11-21 09:34:58 -04:00
|
|
|
self.assertEqual(py_item.__module__, value.module)
|
2001-08-13 17:26:19 -03:00
|
|
|
else:
|
2010-01-24 12:58:36 -04:00
|
|
|
self.assertIsInstance(py_item, (ClassType, type))
|
2006-04-18 01:53:28 -03:00
|
|
|
if py_item.__module__ != moduleName:
|
2006-04-17 22:39:25 -03:00
|
|
|
continue # skip classes that came from somewhere else
|
|
|
|
|
2001-08-13 17:26:19 -03:00
|
|
|
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():
|
2004-08-02 03:10:11 -03:00
|
|
|
if ismethod(py_item, 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)
|
2010-11-21 09:34:58 -04:00
|
|
|
self.assertEqual(py_item.__module__, value.module)
|
2001-08-13 17:26:19 -03:00
|
|
|
|
2004-08-02 03:10:11 -03:00
|
|
|
self.assertEqualsOrIgnored(py_item.__name__, value.name,
|
|
|
|
ignore)
|
2002-12-03 04:16:50 -04:00
|
|
|
# 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')
|
2009-07-19 17:18:21 -03:00
|
|
|
self.checkModule('doctest', ignore=("DocTestCase",))
|
2010-01-30 03:22:54 -04:00
|
|
|
# Silence Py3k warning
|
|
|
|
rfc822 = import_module('rfc822', deprecated=True)
|
|
|
|
self.checkModule('rfc822', rfc822)
|
2001-08-13 17:26:19 -03:00
|
|
|
self.checkModule('difflib')
|
|
|
|
|
2004-08-02 03:10:11 -03:00
|
|
|
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')
|
2004-08-03 23:36:18 -03:00
|
|
|
|
2001-08-13 17:26:19 -03:00
|
|
|
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
|
2008-05-12 19:21:39 -03:00
|
|
|
cm('urllib', ignore=('_CFNumberToInt32',
|
|
|
|
'_CStringFromCFString',
|
2008-05-18 18:10:19 -03:00
|
|
|
'_CFSetup',
|
2008-05-12 19:21:39 -03:00
|
|
|
'getproxies_registry',
|
2008-01-20 10:20:02 -04:00
|
|
|
'proxy_bypass_registry',
|
2008-05-12 19:21:39 -03:00
|
|
|
'proxy_bypass_macosx_sysconf',
|
2004-07-17 21:00:03 -03:00
|
|
|
'open_https',
|
2008-05-12 19:21:39 -03:00
|
|
|
'getproxies_macosx_sysconf',
|
2004-07-17 21:00:03 -03:00
|
|
|
'getproxies_internetconfig',)) # not on all platforms
|
2004-07-17 21:08:11 -03:00
|
|
|
cm('pickle')
|
2002-12-03 04:16:50 -04:00
|
|
|
cm('aifc', ignore=('openfp',)) # set with = in module
|
|
|
|
cm('Cookie')
|
2014-11-07 16:31:54 -04:00
|
|
|
cm('sre_parse', ignore=('dump', 'groups')) # from sre_constants import *; property
|
2002-12-03 04:16:50 -04:00
|
|
|
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
|
2006-03-18 11:41:53 -04:00
|
|
|
cm('email.parser')
|
2002-12-03 04:16:50 -04:00
|
|
|
cm('test.test_pyclbr')
|
2001-08-13 17:26:19 -03:00
|
|
|
|
2012-05-18 15:51:11 -03:00
|
|
|
def test_issue_14798(self):
|
|
|
|
# test ImportError is raised when the first part of a dotted name is
|
|
|
|
# not a package
|
|
|
|
self.assertRaises(ImportError, pyclbr.readmodule_ex, 'asyncore.foo')
|
|
|
|
|
2001-09-20 18:33:42 -03:00
|
|
|
|
|
|
|
def test_main():
|
|
|
|
run_unittest(PyclbrTest)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
test_main()
|