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} ''' |
Guido van Rossum | 0ed7aa1 | 2002-12-02 14:54:20 +0000 | [diff] [blame^] | 26 | try: |
| 27 | for p1, p2 in (l1, l2), (l2, l1): |
| 28 | for item in p1: |
| 29 | ok = (item in p2) or (item in ignore) |
| 30 | if not ok: |
| 31 | self.fail("%r missing" % item) |
| 32 | except: |
| 33 | print >>sys.stderr, "l1=%r, l2=%r, ignore=%r" % (l1, l2, ignore) |
| 34 | raise |
Tim Peters | 0460106 | 2001-08-13 22:25:24 +0000 | [diff] [blame] | 35 | |
Fred Drake | 3a28ca8 | 2001-08-13 20:26:19 +0000 | [diff] [blame] | 36 | def assertHasattr(self, obj, attr, ignore): |
| 37 | ''' succeed iff hasattr(obj,attr) or attr in ignore. ''' |
| 38 | if attr in ignore: return |
Tim Peters | 7402f79 | 2001-10-02 03:53:41 +0000 | [diff] [blame] | 39 | if not hasattr(obj, attr): print "???", attr |
Tim Peters | 5e5ca56 | 2002-07-10 02:37:21 +0000 | [diff] [blame] | 40 | self.failUnless(hasattr(obj, attr), |
| 41 | 'expected hasattr(%r, %r)' % (obj, attr)) |
Fred Drake | 3a28ca8 | 2001-08-13 20:26:19 +0000 | [diff] [blame] | 42 | |
| 43 | |
| 44 | def assertHaskey(self, obj, key, ignore): |
| 45 | ''' succeed iff obj.has_key(key) or key in ignore. ''' |
| 46 | if key in ignore: return |
Guido van Rossum | 0ed7aa1 | 2002-12-02 14:54:20 +0000 | [diff] [blame^] | 47 | if not obj.has_key(key): |
| 48 | print >>sys.stderr, "***",key |
Fred Drake | 3a28ca8 | 2001-08-13 20:26:19 +0000 | [diff] [blame] | 49 | self.failUnless(obj.has_key(key)) |
| 50 | |
| 51 | def assertEquals(self, a, b, ignore=None): |
| 52 | ''' succeed iff a == b or a in ignore or b in ignore ''' |
| 53 | if (ignore == None) or (a in ignore) or (b in ignore): return |
| 54 | |
| 55 | unittest.TestCase.assertEquals(self, a, b) |
| 56 | |
| 57 | def checkModule(self, moduleName, module=None, ignore=()): |
| 58 | ''' succeed iff pyclbr.readmodule_ex(modulename) corresponds |
| 59 | to the actual module object, module. Any identifiers in |
| 60 | ignore are ignored. If no module is provided, the appropriate |
| 61 | module is loaded with __import__.''' |
| 62 | |
| 63 | if module == None: |
Guido van Rossum | 0ed7aa1 | 2002-12-02 14:54:20 +0000 | [diff] [blame^] | 64 | # Import it. |
| 65 | # ('<silly>' is to work around an API silliness in __import__) |
| 66 | module = __import__(moduleName, globals(), {}, ['<silly>']) |
Fred Drake | 3a28ca8 | 2001-08-13 20:26:19 +0000 | [diff] [blame] | 67 | |
| 68 | dict = pyclbr.readmodule_ex(moduleName) |
| 69 | |
| 70 | # Make sure the toplevel functions and classes are the same. |
| 71 | for name, value in dict.items(): |
Tim Peters | 0460106 | 2001-08-13 22:25:24 +0000 | [diff] [blame] | 72 | if name in ignore: |
Fred Drake | 3a28ca8 | 2001-08-13 20:26:19 +0000 | [diff] [blame] | 73 | continue |
| 74 | self.assertHasattr(module, name, ignore) |
| 75 | py_item = getattr(module, name) |
| 76 | if isinstance(value, pyclbr.Function): |
| 77 | self.assertEquals(type(py_item), FunctionType) |
| 78 | else: |
| 79 | self.assertEquals(type(py_item), ClassType) |
| 80 | real_bases = [base.__name__ for base in py_item.__bases__] |
Tim Peters | 0460106 | 2001-08-13 22:25:24 +0000 | [diff] [blame] | 81 | pyclbr_bases = [ getattr(base, 'name', base) |
Fred Drake | 3a28ca8 | 2001-08-13 20:26:19 +0000 | [diff] [blame] | 82 | for base in value.super ] |
Tim Peters | 0460106 | 2001-08-13 22:25:24 +0000 | [diff] [blame] | 83 | |
Guido van Rossum | 0ed7aa1 | 2002-12-02 14:54:20 +0000 | [diff] [blame^] | 84 | try: |
| 85 | self.assertListEq(real_bases, pyclbr_bases, ignore) |
| 86 | except: |
| 87 | print >>sys.stderr, "class=%s" % py_item |
| 88 | raise |
Fred Drake | 3a28ca8 | 2001-08-13 20:26:19 +0000 | [diff] [blame] | 89 | |
| 90 | actualMethods = [] |
Tim Peters | 37a309d | 2001-09-04 01:20:04 +0000 | [diff] [blame] | 91 | for m in py_item.__dict__.keys(): |
Fred Drake | 3a28ca8 | 2001-08-13 20:26:19 +0000 | [diff] [blame] | 92 | if type(getattr(py_item, m)) == MethodType: |
| 93 | actualMethods.append(m) |
| 94 | foundMethods = [] |
| 95 | for m in value.methods.keys(): |
| 96 | if m[:2] == '__' and m[-2:] != '__': |
| 97 | foundMethods.append('_'+name+m) |
| 98 | else: |
| 99 | foundMethods.append(m) |
| 100 | |
| 101 | self.assertListEq(foundMethods, actualMethods, ignore) |
| 102 | self.assertEquals(py_item.__module__, value.module) |
| 103 | |
| 104 | self.assertEquals(py_item.__name__, value.name, ignore) |
| 105 | # can't check file or lineno |
| 106 | |
| 107 | # Now check for missing stuff. |
Guido van Rossum | 0ed7aa1 | 2002-12-02 14:54:20 +0000 | [diff] [blame^] | 108 | def defined_in(item, module): |
| 109 | if isinstance(item, ClassType): |
| 110 | return item.__module__ == module.__name__ |
| 111 | if isinstance(item, FunctionType): |
| 112 | return item.func_globals is module.__dict__ |
| 113 | return False |
Fred Drake | 3a28ca8 | 2001-08-13 20:26:19 +0000 | [diff] [blame] | 114 | for name in dir(module): |
| 115 | item = getattr(module, name) |
Guido van Rossum | 0ed7aa1 | 2002-12-02 14:54:20 +0000 | [diff] [blame^] | 116 | if isinstance(item, (ClassType, FunctionType)): |
| 117 | if defined_in(item, module): |
| 118 | self.assertHaskey(dict, name, ignore) |
Fred Drake | 3a28ca8 | 2001-08-13 20:26:19 +0000 | [diff] [blame] | 119 | |
| 120 | def test_easy(self): |
| 121 | self.checkModule('pyclbr') |
Tim Peters | 7402f79 | 2001-10-02 03:53:41 +0000 | [diff] [blame] | 122 | self.checkModule('doctest', |
Tim Peters | 17111f3 | 2001-10-03 04:08:26 +0000 | [diff] [blame] | 123 | ignore=['_isclass', |
| 124 | '_isfunction', |
| 125 | '_ismodule', |
| 126 | '_classify_class_attrs']) |
Guido van Rossum | d842e07 | 2002-06-05 19:07:39 +0000 | [diff] [blame] | 127 | self.checkModule('rfc822', ignore=["get"]) |
Fred Drake | 3a28ca8 | 2001-08-13 20:26:19 +0000 | [diff] [blame] | 128 | self.checkModule('difflib') |
| 129 | |
| 130 | def test_others(self): |
| 131 | cm = self.checkModule |
| 132 | |
| 133 | # these are about the 20 longest modules. |
| 134 | |
| 135 | cm('random', ignore=('_verify',)) # deleted |
| 136 | |
| 137 | cm('cgi', ignore=('f', 'g', # nested declarations |
| 138 | 'log')) # set with =, not def |
| 139 | |
| 140 | cm('mhlib', ignore=('do', # nested declaration |
| 141 | 'bisect')) # imported method, set with = |
| 142 | |
| 143 | cm('urllib', ignore=('getproxies_environment', # set with = |
Tim Peters | 0460106 | 2001-08-13 22:25:24 +0000 | [diff] [blame] | 144 | 'getproxies_registry', # set with = |
| 145 | 'open_https')) # not on all platforms |
Fred Drake | 3a28ca8 | 2001-08-13 20:26:19 +0000 | [diff] [blame] | 146 | |
Tim Peters | 0460106 | 2001-08-13 22:25:24 +0000 | [diff] [blame] | 147 | cm('pickle', ignore=('g',)) # deleted declaration |
| 148 | |
| 149 | cm('aifc', ignore=('openfp',)) # set with = |
| 150 | |
Fred Drake | 3a28ca8 | 2001-08-13 20:26:19 +0000 | [diff] [blame] | 151 | cm('Cookie', ignore=('__str__', 'Cookie')) # set with = |
Tim Peters | 0460106 | 2001-08-13 22:25:24 +0000 | [diff] [blame] | 152 | |
Fred Drake | 3a28ca8 | 2001-08-13 20:26:19 +0000 | [diff] [blame] | 153 | cm('sre_parse', ignore=('literal', # nested def |
| 154 | 'makedict', 'dump' # from sre_constants |
| 155 | )) |
| 156 | |
Guido van Rossum | 0ed7aa1 | 2002-12-02 14:54:20 +0000 | [diff] [blame^] | 157 | # Tests for modules inside packages |
| 158 | cm('email.Parser') |
| 159 | |
| 160 | cm('test.test_pyclbr', ignore=('defined_in',)) |
Fred Drake | 3a28ca8 | 2001-08-13 20:26:19 +0000 | [diff] [blame] | 161 | |
| 162 | # pydoc doesn't work because of string issues |
| 163 | # cm('pydoc', pydoc) |
Tim Peters | 0460106 | 2001-08-13 22:25:24 +0000 | [diff] [blame] | 164 | |
Fred Drake | 3a28ca8 | 2001-08-13 20:26:19 +0000 | [diff] [blame] | 165 | # pdb plays too many dynamic games |
Tim Peters | 0460106 | 2001-08-13 22:25:24 +0000 | [diff] [blame] | 166 | # cm('pdb', pdb) |
Fred Drake | 3a28ca8 | 2001-08-13 20:26:19 +0000 | [diff] [blame] | 167 | |
Fred Drake | 2e2be37 | 2001-09-20 21:33:42 +0000 | [diff] [blame] | 168 | |
| 169 | def test_main(): |
| 170 | run_unittest(PyclbrTest) |
| 171 | |
| 172 | |
| 173 | if __name__ == "__main__": |
| 174 | test_main() |