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