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