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): |
Guido van Rossum | e2b70bc | 2006-08-18 22:13:04 +0000 | [diff] [blame] | 43 | ''' succeed iff key in obj or key in ignore. ''' |
Fred Drake | 3a28ca8 | 2001-08-13 20:26:19 +0000 | [diff] [blame] | 44 | if key in ignore: return |
Guido van Rossum | e2b70bc | 2006-08-18 22:13:04 +0000 | [diff] [blame] | 45 | if key not in obj: |
Guido van Rossum | 0ed7aa1 | 2002-12-02 14:54:20 +0000 | [diff] [blame] | 46 | print >>sys.stderr, "***",key |
Thomas Wouters | 08f0046 | 2007-02-03 21:49:06 +0000 | [diff] [blame^] | 47 | self.failUnless(key in obj) |
Fred Drake | 3a28ca8 | 2001-08-13 20:26:19 +0000 | [diff] [blame] | 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 | |
Guido van Rossum | d858f70 | 2006-04-21 09:17:15 +0000 | [diff] [blame] | 60 | ignore = set(ignore) | set(['object']) |
| 61 | |
Fred Drake | 3a28ca8 | 2001-08-13 20:26:19 +0000 | [diff] [blame] | 62 | if module == None: |
Guido van Rossum | 0ed7aa1 | 2002-12-02 14:54:20 +0000 | [diff] [blame] | 63 | # Import it. |
| 64 | # ('<silly>' is to work around an API silliness in __import__) |
| 65 | module = __import__(moduleName, globals(), {}, ['<silly>']) |
Fred Drake | 3a28ca8 | 2001-08-13 20:26:19 +0000 | [diff] [blame] | 66 | |
| 67 | dict = pyclbr.readmodule_ex(moduleName) |
| 68 | |
Anthony Baxter | c2a5a63 | 2004-08-02 06:10:11 +0000 | [diff] [blame] | 69 | def ismethod(oclass, obj, name): |
| 70 | classdict = oclass.__dict__ |
| 71 | if isinstance(obj, FunctionType): |
| 72 | if not isinstance(classdict[name], StaticMethodType): |
| 73 | return False |
| 74 | else: |
| 75 | if not isinstance(obj, MethodType): |
| 76 | return False |
| 77 | if obj.im_self is not None: |
| 78 | if (not isinstance(classdict[name], ClassMethodType) or |
| 79 | obj.im_self is not oclass): |
| 80 | return False |
| 81 | else: |
| 82 | if not isinstance(classdict[name], FunctionType): |
| 83 | return False |
| 84 | |
Guido van Rossum | 7f6a439 | 2002-12-03 08:16:50 +0000 | [diff] [blame] | 85 | objname = obj.__name__ |
| 86 | if objname.startswith("__") and not objname.endswith("__"): |
| 87 | objname = "_%s%s" % (obj.im_class.__name__, objname) |
| 88 | return objname == name |
| 89 | |
Fred Drake | 3a28ca8 | 2001-08-13 20:26:19 +0000 | [diff] [blame] | 90 | # Make sure the toplevel functions and classes are the same. |
| 91 | for name, value in dict.items(): |
Tim Peters | 0460106 | 2001-08-13 22:25:24 +0000 | [diff] [blame] | 92 | if name in ignore: |
Fred Drake | 3a28ca8 | 2001-08-13 20:26:19 +0000 | [diff] [blame] | 93 | continue |
| 94 | self.assertHasattr(module, name, ignore) |
| 95 | py_item = getattr(module, name) |
| 96 | if isinstance(value, pyclbr.Function): |
Raymond Hettinger | c4536a1 | 2004-09-04 23:53:20 +0000 | [diff] [blame] | 97 | self.assert_(isinstance(py_item, (FunctionType, BuiltinFunctionType))) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 98 | if py_item.__module__ != moduleName: |
| 99 | continue # skip functions that came from somewhere else |
| 100 | self.assertEquals(py_item.__module__, value.module) |
Fred Drake | 3a28ca8 | 2001-08-13 20:26:19 +0000 | [diff] [blame] | 101 | else: |
Anthony Baxter | c2a5a63 | 2004-08-02 06:10:11 +0000 | [diff] [blame] | 102 | self.failUnless(isinstance(py_item, (ClassType, type))) |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 103 | if py_item.__module__ != moduleName: |
| 104 | continue # skip classes that came from somewhere else |
| 105 | |
Fred Drake | 3a28ca8 | 2001-08-13 20:26:19 +0000 | [diff] [blame] | 106 | real_bases = [base.__name__ for base in py_item.__bases__] |
Tim Peters | 0460106 | 2001-08-13 22:25:24 +0000 | [diff] [blame] | 107 | pyclbr_bases = [ getattr(base, 'name', base) |
Fred Drake | 3a28ca8 | 2001-08-13 20:26:19 +0000 | [diff] [blame] | 108 | for base in value.super ] |
Tim Peters | 0460106 | 2001-08-13 22:25:24 +0000 | [diff] [blame] | 109 | |
Guido van Rossum | 0ed7aa1 | 2002-12-02 14:54:20 +0000 | [diff] [blame] | 110 | try: |
| 111 | self.assertListEq(real_bases, pyclbr_bases, ignore) |
| 112 | except: |
| 113 | print >>sys.stderr, "class=%s" % py_item |
| 114 | raise |
Fred Drake | 3a28ca8 | 2001-08-13 20:26:19 +0000 | [diff] [blame] | 115 | |
| 116 | actualMethods = [] |
Tim Peters | 37a309d | 2001-09-04 01:20:04 +0000 | [diff] [blame] | 117 | for m in py_item.__dict__.keys(): |
Anthony Baxter | c2a5a63 | 2004-08-02 06:10:11 +0000 | [diff] [blame] | 118 | if ismethod(py_item, getattr(py_item, m), m): |
Fred Drake | 3a28ca8 | 2001-08-13 20:26:19 +0000 | [diff] [blame] | 119 | actualMethods.append(m) |
| 120 | foundMethods = [] |
| 121 | for m in value.methods.keys(): |
| 122 | if m[:2] == '__' and m[-2:] != '__': |
| 123 | foundMethods.append('_'+name+m) |
| 124 | else: |
| 125 | foundMethods.append(m) |
| 126 | |
Guido van Rossum | 7f6a439 | 2002-12-03 08:16:50 +0000 | [diff] [blame] | 127 | try: |
| 128 | self.assertListEq(foundMethods, actualMethods, ignore) |
| 129 | self.assertEquals(py_item.__module__, value.module) |
Fred Drake | 3a28ca8 | 2001-08-13 20:26:19 +0000 | [diff] [blame] | 130 | |
Anthony Baxter | c2a5a63 | 2004-08-02 06:10:11 +0000 | [diff] [blame] | 131 | self.assertEqualsOrIgnored(py_item.__name__, value.name, |
| 132 | ignore) |
Guido van Rossum | 7f6a439 | 2002-12-03 08:16:50 +0000 | [diff] [blame] | 133 | # can't check file or lineno |
| 134 | except: |
| 135 | print >>sys.stderr, "class=%s" % py_item |
| 136 | raise |
Fred Drake | 3a28ca8 | 2001-08-13 20:26:19 +0000 | [diff] [blame] | 137 | |
| 138 | # Now check for missing stuff. |
Guido van Rossum | 0ed7aa1 | 2002-12-02 14:54:20 +0000 | [diff] [blame] | 139 | def defined_in(item, module): |
| 140 | if isinstance(item, ClassType): |
| 141 | return item.__module__ == module.__name__ |
| 142 | if isinstance(item, FunctionType): |
| 143 | return item.func_globals is module.__dict__ |
| 144 | return False |
Fred Drake | 3a28ca8 | 2001-08-13 20:26:19 +0000 | [diff] [blame] | 145 | for name in dir(module): |
| 146 | item = getattr(module, name) |
Guido van Rossum | 0ed7aa1 | 2002-12-02 14:54:20 +0000 | [diff] [blame] | 147 | if isinstance(item, (ClassType, FunctionType)): |
| 148 | if defined_in(item, module): |
| 149 | self.assertHaskey(dict, name, ignore) |
Fred Drake | 3a28ca8 | 2001-08-13 20:26:19 +0000 | [diff] [blame] | 150 | |
| 151 | def test_easy(self): |
| 152 | self.checkModule('pyclbr') |
Guido van Rossum | 7f6a439 | 2002-12-03 08:16:50 +0000 | [diff] [blame] | 153 | self.checkModule('doctest') |
| 154 | self.checkModule('rfc822') |
Fred Drake | 3a28ca8 | 2001-08-13 20:26:19 +0000 | [diff] [blame] | 155 | self.checkModule('difflib') |
| 156 | |
Anthony Baxter | c2a5a63 | 2004-08-02 06:10:11 +0000 | [diff] [blame] | 157 | def test_decorators(self): |
| 158 | # XXX: See comment in pyclbr_input.py for a test that would fail |
| 159 | # if it were not commented out. |
| 160 | # |
| 161 | self.checkModule('test.pyclbr_input') |
Tim Peters | 6db15d7 | 2004-08-04 02:36:18 +0000 | [diff] [blame] | 162 | |
Fred Drake | 3a28ca8 | 2001-08-13 20:26:19 +0000 | [diff] [blame] | 163 | def test_others(self): |
| 164 | cm = self.checkModule |
| 165 | |
Guido van Rossum | 7f6a439 | 2002-12-03 08:16:50 +0000 | [diff] [blame] | 166 | # These were once about the 10 longest modules |
Raymond Hettinger | e401b6f | 2002-12-30 07:21:32 +0000 | [diff] [blame] | 167 | cm('random', ignore=('Random',)) # from _random import Random as CoreGenerator |
Guido van Rossum | 7f6a439 | 2002-12-03 08:16:50 +0000 | [diff] [blame] | 168 | cm('cgi', ignore=('log',)) # set with = in module |
| 169 | cm('mhlib') |
| 170 | cm('urllib', ignore=('getproxies_registry', |
Tim Peters | fa7809d | 2004-07-18 00:00:03 +0000 | [diff] [blame] | 171 | 'open_https', |
| 172 | 'getproxies_internetconfig',)) # not on all platforms |
Tim Peters | 264c659 | 2004-07-18 00:08:11 +0000 | [diff] [blame] | 173 | cm('pickle') |
Guido van Rossum | 7f6a439 | 2002-12-03 08:16:50 +0000 | [diff] [blame] | 174 | cm('aifc', ignore=('openfp',)) # set with = in module |
Guido van Rossum | d858f70 | 2006-04-21 09:17:15 +0000 | [diff] [blame] | 175 | cm('Cookie', ignore=('Cookie',)) # Cookie is an alias for SmartCookie |
Guido van Rossum | 7f6a439 | 2002-12-03 08:16:50 +0000 | [diff] [blame] | 176 | cm('sre_parse', ignore=('dump',)) # from sre_constants import * |
| 177 | cm('pdb') |
| 178 | cm('pydoc') |
Fred Drake | 3a28ca8 | 2001-08-13 20:26:19 +0000 | [diff] [blame] | 179 | |
Guido van Rossum | 0ed7aa1 | 2002-12-02 14:54:20 +0000 | [diff] [blame] | 180 | # Tests for modules inside packages |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 181 | cm('email.parser') |
Guido van Rossum | 7f6a439 | 2002-12-03 08:16:50 +0000 | [diff] [blame] | 182 | cm('test.test_pyclbr') |
Fred Drake | 3a28ca8 | 2001-08-13 20:26:19 +0000 | [diff] [blame] | 183 | |
Fred Drake | 2e2be37 | 2001-09-20 21:33:42 +0000 | [diff] [blame] | 184 | |
| 185 | def test_main(): |
| 186 | run_unittest(PyclbrTest) |
| 187 | |
| 188 | |
| 189 | if __name__ == "__main__": |
| 190 | test_main() |