Fred Drake | 3a28ca8 | 2001-08-13 20:26:19 +0000 | [diff] [blame] | 1 | ''' |
| 2 | Test cases for pyclbr.py |
| 3 | Nick Mathewson |
| 4 | ''' |
Barry Warsaw | 04f357c | 2002-07-23 19:04:11 +0000 | [diff] [blame] | 5 | from test.test_support import run_unittest |
Fred Drake | 3a28ca8 | 2001-08-13 20:26:19 +0000 | [diff] [blame] | 6 | import unittest, sys |
Raymond Hettinger | c4536a1 | 2004-09-04 23:53:20 +0000 | [diff] [blame] | 7 | from types import ClassType, FunctionType, MethodType, BuiltinFunctionType |
Fred Drake | 3a28ca8 | 2001-08-13 20:26:19 +0000 | [diff] [blame] | 8 | import pyclbr |
Guido van Rossum | 0ed7aa1 | 2002-12-02 14:54:20 +0000 | [diff] [blame] | 9 | from unittest import TestCase |
Fred Drake | 3a28ca8 | 2001-08-13 20:26:19 +0000 | [diff] [blame] | 10 | |
Anthony Baxter | c2a5a63 | 2004-08-02 06:10:11 +0000 | [diff] [blame] | 11 | StaticMethodType = type(staticmethod(lambda: None)) |
| 12 | ClassMethodType = type(classmethod(lambda c: None)) |
| 13 | |
Fred Drake | 3a28ca8 | 2001-08-13 20:26:19 +0000 | [diff] [blame] | 14 | # This next line triggers an error on old versions of pyclbr. |
| 15 | |
Tim Peters | 0460106 | 2001-08-13 22:25:24 +0000 | [diff] [blame] | 16 | from commands import getstatus |
Fred Drake | 3a28ca8 | 2001-08-13 20:26:19 +0000 | [diff] [blame] | 17 | |
Tim Peters | 0460106 | 2001-08-13 22:25:24 +0000 | [diff] [blame] | 18 | # Here we test the python class browser code. |
Fred Drake | 3a28ca8 | 2001-08-13 20:26:19 +0000 | [diff] [blame] | 19 | # |
| 20 | # The main function in this suite, 'testModule', compares the output |
| 21 | # of pyclbr with the introspected members of a module. Because pyclbr |
| 22 | # is imperfect (as designed), testModule is called with a set of |
| 23 | # members to ignore. |
| 24 | |
Guido van Rossum | 0ed7aa1 | 2002-12-02 14:54:20 +0000 | [diff] [blame] | 25 | class PyclbrTest(TestCase): |
Fred Drake | 3a28ca8 | 2001-08-13 20:26:19 +0000 | [diff] [blame] | 26 | |
| 27 | def assertListEq(self, l1, l2, ignore): |
| 28 | ''' succeed iff {l1} - {ignore} == {l2} - {ignore} ''' |
Raymond Hettinger | a690a99 | 2003-11-16 16:17:49 +0000 | [diff] [blame] | 29 | missing = (set(l1) ^ set(l2)) - set(ignore) |
Raymond Hettinger | 91bbd9a | 2003-05-02 09:06:28 +0000 | [diff] [blame] | 30 | if missing: |
Guido van Rossum | 7f6a439 | 2002-12-03 08:16:50 +0000 | [diff] [blame] | 31 | print >>sys.stderr, "l1=%r\nl2=%r\nignore=%r" % (l1, l2, ignore) |
Raymond Hettinger | 91bbd9a | 2003-05-02 09:06:28 +0000 | [diff] [blame] | 32 | self.fail("%r missing" % missing.pop()) |
Tim Peters | 0460106 | 2001-08-13 22:25:24 +0000 | [diff] [blame] | 33 | |
Fred Drake | 3a28ca8 | 2001-08-13 20:26:19 +0000 | [diff] [blame] | 34 | def assertHasattr(self, obj, attr, ignore): |
| 35 | ''' succeed iff hasattr(obj,attr) or attr in ignore. ''' |
| 36 | if attr in ignore: return |
Tim Peters | 7402f79 | 2001-10-02 03:53:41 +0000 | [diff] [blame] | 37 | if not hasattr(obj, attr): print "???", attr |
Tim Peters | 5e5ca56 | 2002-07-10 02:37:21 +0000 | [diff] [blame] | 38 | self.failUnless(hasattr(obj, attr), |
| 39 | 'expected hasattr(%r, %r)' % (obj, attr)) |
Fred Drake | 3a28ca8 | 2001-08-13 20:26:19 +0000 | [diff] [blame] | 40 | |
| 41 | |
| 42 | def assertHaskey(self, obj, key, ignore): |
| 43 | ''' succeed iff obj.has_key(key) or key in ignore. ''' |
| 44 | if key in ignore: return |
Guido van Rossum | 0ed7aa1 | 2002-12-02 14:54:20 +0000 | [diff] [blame] | 45 | if not obj.has_key(key): |
| 46 | print >>sys.stderr, "***",key |
Fred Drake | 3a28ca8 | 2001-08-13 20:26:19 +0000 | [diff] [blame] | 47 | self.failUnless(obj.has_key(key)) |
| 48 | |
Anthony Baxter | c2a5a63 | 2004-08-02 06:10:11 +0000 | [diff] [blame] | 49 | def assertEqualsOrIgnored(self, a, b, ignore): |
Fred Drake | 3a28ca8 | 2001-08-13 20:26:19 +0000 | [diff] [blame] | 50 | ''' succeed iff a == b or a in ignore or b in ignore ''' |
Anthony Baxter | c2a5a63 | 2004-08-02 06:10:11 +0000 | [diff] [blame] | 51 | if a not in ignore and b not in ignore: |
| 52 | self.assertEquals(a, b) |
Fred Drake | 3a28ca8 | 2001-08-13 20:26:19 +0000 | [diff] [blame] | 53 | |
| 54 | def checkModule(self, moduleName, module=None, ignore=()): |
| 55 | ''' succeed iff pyclbr.readmodule_ex(modulename) corresponds |
| 56 | to the actual module object, module. Any identifiers in |
| 57 | ignore are ignored. If no module is provided, the appropriate |
| 58 | module is loaded with __import__.''' |
| 59 | |
| 60 | if module == None: |
Guido van Rossum | 0ed7aa1 | 2002-12-02 14:54:20 +0000 | [diff] [blame] | 61 | # Import it. |
| 62 | # ('<silly>' is to work around an API silliness in __import__) |
| 63 | module = __import__(moduleName, globals(), {}, ['<silly>']) |
Fred Drake | 3a28ca8 | 2001-08-13 20:26:19 +0000 | [diff] [blame] | 64 | |
| 65 | dict = pyclbr.readmodule_ex(moduleName) |
| 66 | |
Anthony Baxter | c2a5a63 | 2004-08-02 06:10:11 +0000 | [diff] [blame] | 67 | def ismethod(oclass, obj, name): |
| 68 | classdict = oclass.__dict__ |
| 69 | if isinstance(obj, FunctionType): |
| 70 | if not isinstance(classdict[name], StaticMethodType): |
| 71 | return False |
| 72 | else: |
| 73 | if not isinstance(obj, MethodType): |
| 74 | return False |
| 75 | if obj.im_self is not None: |
| 76 | if (not isinstance(classdict[name], ClassMethodType) or |
| 77 | obj.im_self is not oclass): |
| 78 | return False |
| 79 | else: |
| 80 | if not isinstance(classdict[name], FunctionType): |
| 81 | return False |
| 82 | |
Guido van Rossum | 7f6a439 | 2002-12-03 08:16:50 +0000 | [diff] [blame] | 83 | objname = obj.__name__ |
| 84 | if objname.startswith("__") and not objname.endswith("__"): |
| 85 | objname = "_%s%s" % (obj.im_class.__name__, objname) |
| 86 | return objname == name |
| 87 | |
Fred Drake | 3a28ca8 | 2001-08-13 20:26:19 +0000 | [diff] [blame] | 88 | # Make sure the toplevel functions and classes are the same. |
| 89 | for name, value in dict.items(): |
Tim Peters | 0460106 | 2001-08-13 22:25:24 +0000 | [diff] [blame] | 90 | if name in ignore: |
Fred Drake | 3a28ca8 | 2001-08-13 20:26:19 +0000 | [diff] [blame] | 91 | continue |
| 92 | self.assertHasattr(module, name, ignore) |
| 93 | py_item = getattr(module, name) |
| 94 | if isinstance(value, pyclbr.Function): |
Raymond Hettinger | c4536a1 | 2004-09-04 23:53:20 +0000 | [diff] [blame] | 95 | self.assert_(isinstance(py_item, (FunctionType, BuiltinFunctionType))) |
Fred Drake | 3a28ca8 | 2001-08-13 20:26:19 +0000 | [diff] [blame] | 96 | else: |
Anthony Baxter | c2a5a63 | 2004-08-02 06:10:11 +0000 | [diff] [blame] | 97 | self.failUnless(isinstance(py_item, (ClassType, type))) |
Neal Norwitz | 041669f | 2006-04-18 04:53:28 +0000 | [diff] [blame] | 98 | if py_item.__module__ != moduleName: |
Phillip J. Eby | 742cd24 | 2006-04-18 01:39:25 +0000 | [diff] [blame] | 99 | continue # skip classes that came from somewhere else |
| 100 | |
Fred Drake | 3a28ca8 | 2001-08-13 20:26:19 +0000 | [diff] [blame] | 101 | real_bases = [base.__name__ for base in py_item.__bases__] |
Tim Peters | 0460106 | 2001-08-13 22:25:24 +0000 | [diff] [blame] | 102 | pyclbr_bases = [ getattr(base, 'name', base) |
Fred Drake | 3a28ca8 | 2001-08-13 20:26:19 +0000 | [diff] [blame] | 103 | for base in value.super ] |
Tim Peters | 0460106 | 2001-08-13 22:25:24 +0000 | [diff] [blame] | 104 | |
Guido van Rossum | 0ed7aa1 | 2002-12-02 14:54:20 +0000 | [diff] [blame] | 105 | try: |
| 106 | self.assertListEq(real_bases, pyclbr_bases, ignore) |
| 107 | except: |
| 108 | print >>sys.stderr, "class=%s" % py_item |
| 109 | raise |
Fred Drake | 3a28ca8 | 2001-08-13 20:26:19 +0000 | [diff] [blame] | 110 | |
| 111 | actualMethods = [] |
Tim Peters | 37a309d | 2001-09-04 01:20:04 +0000 | [diff] [blame] | 112 | for m in py_item.__dict__.keys(): |
Anthony Baxter | c2a5a63 | 2004-08-02 06:10:11 +0000 | [diff] [blame] | 113 | if ismethod(py_item, getattr(py_item, m), m): |
Fred Drake | 3a28ca8 | 2001-08-13 20:26:19 +0000 | [diff] [blame] | 114 | actualMethods.append(m) |
| 115 | foundMethods = [] |
| 116 | for m in value.methods.keys(): |
| 117 | if m[:2] == '__' and m[-2:] != '__': |
| 118 | foundMethods.append('_'+name+m) |
| 119 | else: |
| 120 | foundMethods.append(m) |
| 121 | |
Guido van Rossum | 7f6a439 | 2002-12-03 08:16:50 +0000 | [diff] [blame] | 122 | try: |
| 123 | self.assertListEq(foundMethods, actualMethods, ignore) |
| 124 | self.assertEquals(py_item.__module__, value.module) |
Fred Drake | 3a28ca8 | 2001-08-13 20:26:19 +0000 | [diff] [blame] | 125 | |
Anthony Baxter | c2a5a63 | 2004-08-02 06:10:11 +0000 | [diff] [blame] | 126 | self.assertEqualsOrIgnored(py_item.__name__, value.name, |
| 127 | ignore) |
Guido van Rossum | 7f6a439 | 2002-12-03 08:16:50 +0000 | [diff] [blame] | 128 | # can't check file or lineno |
| 129 | except: |
| 130 | print >>sys.stderr, "class=%s" % py_item |
| 131 | raise |
Fred Drake | 3a28ca8 | 2001-08-13 20:26:19 +0000 | [diff] [blame] | 132 | |
| 133 | # Now check for missing stuff. |
Guido van Rossum | 0ed7aa1 | 2002-12-02 14:54:20 +0000 | [diff] [blame] | 134 | def defined_in(item, module): |
| 135 | if isinstance(item, ClassType): |
| 136 | return item.__module__ == module.__name__ |
| 137 | if isinstance(item, FunctionType): |
| 138 | return item.func_globals is module.__dict__ |
| 139 | return False |
Fred Drake | 3a28ca8 | 2001-08-13 20:26:19 +0000 | [diff] [blame] | 140 | for name in dir(module): |
| 141 | item = getattr(module, name) |
Guido van Rossum | 0ed7aa1 | 2002-12-02 14:54:20 +0000 | [diff] [blame] | 142 | if isinstance(item, (ClassType, FunctionType)): |
| 143 | if defined_in(item, module): |
| 144 | self.assertHaskey(dict, name, ignore) |
Fred Drake | 3a28ca8 | 2001-08-13 20:26:19 +0000 | [diff] [blame] | 145 | |
| 146 | def test_easy(self): |
| 147 | self.checkModule('pyclbr') |
Guido van Rossum | 7f6a439 | 2002-12-03 08:16:50 +0000 | [diff] [blame] | 148 | self.checkModule('doctest') |
| 149 | self.checkModule('rfc822') |
Fred Drake | 3a28ca8 | 2001-08-13 20:26:19 +0000 | [diff] [blame] | 150 | self.checkModule('difflib') |
| 151 | |
Anthony Baxter | c2a5a63 | 2004-08-02 06:10:11 +0000 | [diff] [blame] | 152 | def test_decorators(self): |
| 153 | # XXX: See comment in pyclbr_input.py for a test that would fail |
| 154 | # if it were not commented out. |
| 155 | # |
| 156 | self.checkModule('test.pyclbr_input') |
Tim Peters | 6db15d7 | 2004-08-04 02:36:18 +0000 | [diff] [blame] | 157 | |
Fred Drake | 3a28ca8 | 2001-08-13 20:26:19 +0000 | [diff] [blame] | 158 | def test_others(self): |
| 159 | cm = self.checkModule |
| 160 | |
Guido van Rossum | 7f6a439 | 2002-12-03 08:16:50 +0000 | [diff] [blame] | 161 | # These were once about the 10 longest modules |
Raymond Hettinger | e401b6f | 2002-12-30 07:21:32 +0000 | [diff] [blame] | 162 | cm('random', ignore=('Random',)) # from _random import Random as CoreGenerator |
Guido van Rossum | 7f6a439 | 2002-12-03 08:16:50 +0000 | [diff] [blame] | 163 | cm('cgi', ignore=('log',)) # set with = in module |
| 164 | cm('mhlib') |
| 165 | cm('urllib', ignore=('getproxies_registry', |
Tim Peters | fa7809d | 2004-07-18 00:00:03 +0000 | [diff] [blame] | 166 | 'open_https', |
| 167 | 'getproxies_internetconfig',)) # not on all platforms |
Tim Peters | 264c659 | 2004-07-18 00:08:11 +0000 | [diff] [blame] | 168 | cm('pickle') |
Guido van Rossum | 7f6a439 | 2002-12-03 08:16:50 +0000 | [diff] [blame] | 169 | cm('aifc', ignore=('openfp',)) # set with = in module |
| 170 | cm('Cookie') |
| 171 | cm('sre_parse', ignore=('dump',)) # from sre_constants import * |
| 172 | cm('pdb') |
| 173 | cm('pydoc') |
Fred Drake | 3a28ca8 | 2001-08-13 20:26:19 +0000 | [diff] [blame] | 174 | |
Guido van Rossum | 0ed7aa1 | 2002-12-02 14:54:20 +0000 | [diff] [blame] | 175 | # Tests for modules inside packages |
Barry Warsaw | 40ef006 | 2006-03-18 15:41:53 +0000 | [diff] [blame] | 176 | cm('email.parser') |
Guido van Rossum | 7f6a439 | 2002-12-03 08:16:50 +0000 | [diff] [blame] | 177 | cm('test.test_pyclbr') |
Fred Drake | 3a28ca8 | 2001-08-13 20:26:19 +0000 | [diff] [blame] | 178 | |
Fred Drake | 2e2be37 | 2001-09-20 21:33:42 +0000 | [diff] [blame] | 179 | |
| 180 | def test_main(): |
| 181 | run_unittest(PyclbrTest) |
| 182 | |
| 183 | |
| 184 | if __name__ == "__main__": |
| 185 | test_main() |