Fred Drake | 3a28ca8 | 2001-08-13 20:26:19 +0000 | [diff] [blame] | 1 | ''' |
| 2 | Test cases for pyclbr.py |
| 3 | Nick Mathewson |
| 4 | ''' |
| 5 | from test_support import run_unittest |
| 6 | import unittest, sys |
| 7 | from types import ClassType, FunctionType, MethodType |
| 8 | import pyclbr |
| 9 | |
| 10 | # This next line triggers an error on old versions of pyclbr. |
| 11 | |
Tim Peters | 0460106 | 2001-08-13 22:25:24 +0000 | [diff] [blame] | 12 | from commands import getstatus |
Fred Drake | 3a28ca8 | 2001-08-13 20:26:19 +0000 | [diff] [blame] | 13 | |
Tim Peters | 0460106 | 2001-08-13 22:25:24 +0000 | [diff] [blame] | 14 | # Here we test the python class browser code. |
Fred Drake | 3a28ca8 | 2001-08-13 20:26:19 +0000 | [diff] [blame] | 15 | # |
| 16 | # The main function in this suite, 'testModule', compares the output |
| 17 | # of pyclbr with the introspected members of a module. Because pyclbr |
| 18 | # is imperfect (as designed), testModule is called with a set of |
| 19 | # members to ignore. |
| 20 | |
| 21 | class PyclbrTest(unittest.TestCase): |
| 22 | |
| 23 | def assertListEq(self, l1, l2, ignore): |
| 24 | ''' succeed iff {l1} - {ignore} == {l2} - {ignore} ''' |
| 25 | for p1, p2 in (l1, l2), (l2, l1): |
| 26 | for item in p1: |
| 27 | ok = (item in p2) or (item in ignore) |
| 28 | if not ok: |
| 29 | self.fail("%r missing" % item) |
Tim Peters | 0460106 | 2001-08-13 22:25:24 +0000 | [diff] [blame] | 30 | |
| 31 | |
Fred Drake | 3a28ca8 | 2001-08-13 20:26:19 +0000 | [diff] [blame] | 32 | def assertHasattr(self, obj, attr, ignore): |
| 33 | ''' succeed iff hasattr(obj,attr) or attr in ignore. ''' |
| 34 | if attr in ignore: return |
Tim Peters | 7402f79 | 2001-10-02 03:53:41 +0000 | [diff] [blame] | 35 | if not hasattr(obj, attr): print "???", attr |
Fred Drake | 3a28ca8 | 2001-08-13 20:26:19 +0000 | [diff] [blame] | 36 | self.failUnless(hasattr(obj, attr)) |
| 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 |
| 42 | if not obj.has_key(key): print "***",key |
| 43 | self.failUnless(obj.has_key(key)) |
| 44 | |
| 45 | def assertEquals(self, a, b, ignore=None): |
| 46 | ''' succeed iff a == b or a in ignore or b in ignore ''' |
| 47 | if (ignore == None) or (a in ignore) or (b in ignore): return |
| 48 | |
| 49 | unittest.TestCase.assertEquals(self, a, b) |
| 50 | |
| 51 | def checkModule(self, moduleName, module=None, ignore=()): |
| 52 | ''' succeed iff pyclbr.readmodule_ex(modulename) corresponds |
| 53 | to the actual module object, module. Any identifiers in |
| 54 | ignore are ignored. If no module is provided, the appropriate |
| 55 | module is loaded with __import__.''' |
| 56 | |
| 57 | if module == None: |
Tim Peters | 0460106 | 2001-08-13 22:25:24 +0000 | [diff] [blame] | 58 | module = __import__(moduleName, globals(), {}, []) |
Fred Drake | 3a28ca8 | 2001-08-13 20:26:19 +0000 | [diff] [blame] | 59 | |
| 60 | dict = pyclbr.readmodule_ex(moduleName) |
| 61 | |
| 62 | # Make sure the toplevel functions and classes are the same. |
| 63 | for name, value in dict.items(): |
Tim Peters | 0460106 | 2001-08-13 22:25:24 +0000 | [diff] [blame] | 64 | if name in ignore: |
Fred Drake | 3a28ca8 | 2001-08-13 20:26:19 +0000 | [diff] [blame] | 65 | continue |
| 66 | self.assertHasattr(module, name, ignore) |
| 67 | py_item = getattr(module, name) |
| 68 | if isinstance(value, pyclbr.Function): |
| 69 | self.assertEquals(type(py_item), FunctionType) |
| 70 | else: |
| 71 | self.assertEquals(type(py_item), ClassType) |
| 72 | real_bases = [base.__name__ for base in py_item.__bases__] |
Tim Peters | 0460106 | 2001-08-13 22:25:24 +0000 | [diff] [blame] | 73 | pyclbr_bases = [ getattr(base, 'name', base) |
Fred Drake | 3a28ca8 | 2001-08-13 20:26:19 +0000 | [diff] [blame] | 74 | for base in value.super ] |
Tim Peters | 0460106 | 2001-08-13 22:25:24 +0000 | [diff] [blame] | 75 | |
Fred Drake | 3a28ca8 | 2001-08-13 20:26:19 +0000 | [diff] [blame] | 76 | self.assertListEq(real_bases, pyclbr_bases, ignore) |
| 77 | |
| 78 | actualMethods = [] |
Tim Peters | 37a309d | 2001-09-04 01:20:04 +0000 | [diff] [blame] | 79 | for m in py_item.__dict__.keys(): |
Fred Drake | 3a28ca8 | 2001-08-13 20:26:19 +0000 | [diff] [blame] | 80 | if type(getattr(py_item, m)) == MethodType: |
| 81 | actualMethods.append(m) |
| 82 | foundMethods = [] |
| 83 | for m in value.methods.keys(): |
| 84 | if m[:2] == '__' and m[-2:] != '__': |
| 85 | foundMethods.append('_'+name+m) |
| 86 | else: |
| 87 | foundMethods.append(m) |
| 88 | |
| 89 | self.assertListEq(foundMethods, actualMethods, ignore) |
| 90 | self.assertEquals(py_item.__module__, value.module) |
| 91 | |
| 92 | self.assertEquals(py_item.__name__, value.name, ignore) |
| 93 | # can't check file or lineno |
| 94 | |
| 95 | # Now check for missing stuff. |
| 96 | for name in dir(module): |
| 97 | item = getattr(module, name) |
| 98 | if type(item) in (ClassType, FunctionType): |
| 99 | self.assertHaskey(dict, name, ignore) |
| 100 | |
| 101 | def test_easy(self): |
| 102 | self.checkModule('pyclbr') |
Tim Peters | 7402f79 | 2001-10-02 03:53:41 +0000 | [diff] [blame] | 103 | self.checkModule('doctest', |
Tim Peters | 17111f3 | 2001-10-03 04:08:26 +0000 | [diff] [blame] | 104 | ignore=['_isclass', |
| 105 | '_isfunction', |
| 106 | '_ismodule', |
| 107 | '_classify_class_attrs']) |
Fred Drake | 3a28ca8 | 2001-08-13 20:26:19 +0000 | [diff] [blame] | 108 | self.checkModule('rfc822') |
| 109 | self.checkModule('xmllib') |
| 110 | self.checkModule('difflib') |
| 111 | |
| 112 | def test_others(self): |
| 113 | cm = self.checkModule |
| 114 | |
| 115 | # these are about the 20 longest modules. |
| 116 | |
| 117 | cm('random', ignore=('_verify',)) # deleted |
| 118 | |
| 119 | cm('cgi', ignore=('f', 'g', # nested declarations |
| 120 | 'log')) # set with =, not def |
| 121 | |
| 122 | cm('mhlib', ignore=('do', # nested declaration |
| 123 | 'bisect')) # imported method, set with = |
| 124 | |
| 125 | cm('urllib', ignore=('getproxies_environment', # set with = |
Tim Peters | 0460106 | 2001-08-13 22:25:24 +0000 | [diff] [blame] | 126 | 'getproxies_registry', # set with = |
| 127 | 'open_https')) # not on all platforms |
Fred Drake | 3a28ca8 | 2001-08-13 20:26:19 +0000 | [diff] [blame] | 128 | |
| 129 | #XXXX bad example |
| 130 | #cm('urllib2', ignore=('at_cnri', # defined inside __main__ |
| 131 | # '__super_init', # set with =. |
| 132 | # '_HTTPError__super_init', # set with =. |
| 133 | # 'http_error_301', # set with =. |
| 134 | # )) |
| 135 | |
| 136 | |
Fred Drake | 3a28ca8 | 2001-08-13 20:26:19 +0000 | [diff] [blame] | 137 | |
Tim Peters | 0460106 | 2001-08-13 22:25:24 +0000 | [diff] [blame] | 138 | cm('pickle', ignore=('g',)) # deleted declaration |
| 139 | |
| 140 | cm('aifc', ignore=('openfp',)) # set with = |
| 141 | |
| 142 | cm('httplib', ignore=('error', # set with = |
| 143 | 'HTTPS')) # not on all platforms |
Fred Drake | 3a28ca8 | 2001-08-13 20:26:19 +0000 | [diff] [blame] | 144 | |
| 145 | cm('Cookie', ignore=('__str__', 'Cookie')) # set with = |
Tim Peters | 0460106 | 2001-08-13 22:25:24 +0000 | [diff] [blame] | 146 | |
Fred Drake | 3a28ca8 | 2001-08-13 20:26:19 +0000 | [diff] [blame] | 147 | cm('sre_parse', ignore=('literal', # nested def |
| 148 | 'makedict', 'dump' # from sre_constants |
| 149 | )) |
| 150 | |
Tim Peters | 0460106 | 2001-08-13 22:25:24 +0000 | [diff] [blame] | 151 | cm('test.test_pyclbr', |
Fred Drake | 3a28ca8 | 2001-08-13 20:26:19 +0000 | [diff] [blame] | 152 | module=sys.modules[__name__]) |
| 153 | |
| 154 | # pydoc doesn't work because of string issues |
| 155 | # cm('pydoc', pydoc) |
Tim Peters | 0460106 | 2001-08-13 22:25:24 +0000 | [diff] [blame] | 156 | |
Fred Drake | 3a28ca8 | 2001-08-13 20:26:19 +0000 | [diff] [blame] | 157 | # pdb plays too many dynamic games |
Tim Peters | 0460106 | 2001-08-13 22:25:24 +0000 | [diff] [blame] | 158 | # cm('pdb', pdb) |
Fred Drake | 3a28ca8 | 2001-08-13 20:26:19 +0000 | [diff] [blame] | 159 | |
Fred Drake | 2e2be37 | 2001-09-20 21:33:42 +0000 | [diff] [blame] | 160 | |
| 161 | def test_main(): |
| 162 | run_unittest(PyclbrTest) |
| 163 | |
| 164 | |
| 165 | if __name__ == "__main__": |
| 166 | test_main() |