blob: db1fc43bad24243ec913c975e1cc3c747c0f32c1 [file] [log] [blame]
Fred Drake3a28ca82001-08-13 20:26:19 +00001'''
2 Test cases for pyclbr.py
3 Nick Mathewson
4'''
Barry Warsaw04f357c2002-07-23 19:04:11 +00005from test.test_support import run_unittest
Fred Drake3a28ca82001-08-13 20:26:19 +00006import unittest, sys
7from types import ClassType, FunctionType, MethodType
8import pyclbr
Guido van Rossum0ed7aa12002-12-02 14:54:20 +00009from unittest import TestCase
Fred Drake3a28ca82001-08-13 20:26:19 +000010
11# This next line triggers an error on old versions of pyclbr.
12
Tim Peters04601062001-08-13 22:25:24 +000013from commands import getstatus
Fred Drake3a28ca82001-08-13 20:26:19 +000014
Tim Peters04601062001-08-13 22:25:24 +000015# Here we test the python class browser code.
Fred Drake3a28ca82001-08-13 20:26:19 +000016#
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 Rossum0ed7aa12002-12-02 14:54:20 +000022class PyclbrTest(TestCase):
Fred Drake3a28ca82001-08-13 20:26:19 +000023
24 def assertListEq(self, l1, l2, ignore):
25 ''' succeed iff {l1} - {ignore} == {l2} - {ignore} '''
Raymond Hettingera690a992003-11-16 16:17:49 +000026 missing = (set(l1) ^ set(l2)) - set(ignore)
Raymond Hettinger91bbd9a2003-05-02 09:06:28 +000027 if missing:
Guido van Rossum7f6a4392002-12-03 08:16:50 +000028 print >>sys.stderr, "l1=%r\nl2=%r\nignore=%r" % (l1, l2, ignore)
Raymond Hettinger91bbd9a2003-05-02 09:06:28 +000029 self.fail("%r missing" % missing.pop())
Tim Peters04601062001-08-13 22:25:24 +000030
Fred Drake3a28ca82001-08-13 20:26:19 +000031 def assertHasattr(self, obj, attr, ignore):
32 ''' succeed iff hasattr(obj,attr) or attr in ignore. '''
33 if attr in ignore: return
Tim Peters7402f792001-10-02 03:53:41 +000034 if not hasattr(obj, attr): print "???", attr
Tim Peters5e5ca562002-07-10 02:37:21 +000035 self.failUnless(hasattr(obj, attr),
36 'expected hasattr(%r, %r)' % (obj, attr))
Fred Drake3a28ca82001-08-13 20:26:19 +000037
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 Rossum0ed7aa12002-12-02 14:54:20 +000042 if not obj.has_key(key):
43 print >>sys.stderr, "***",key
Fred Drake3a28ca82001-08-13 20:26:19 +000044 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 Rossum0ed7aa12002-12-02 14:54:20 +000059 # Import it.
60 # ('<silly>' is to work around an API silliness in __import__)
61 module = __import__(moduleName, globals(), {}, ['<silly>'])
Fred Drake3a28ca82001-08-13 20:26:19 +000062
63 dict = pyclbr.readmodule_ex(moduleName)
64
Guido van Rossum7f6a4392002-12-03 08:16:50 +000065 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 Drake3a28ca82001-08-13 20:26:19 +000075 # Make sure the toplevel functions and classes are the same.
76 for name, value in dict.items():
Tim Peters04601062001-08-13 22:25:24 +000077 if name in ignore:
Fred Drake3a28ca82001-08-13 20:26:19 +000078 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 Peters04601062001-08-13 22:25:24 +000086 pyclbr_bases = [ getattr(base, 'name', base)
Fred Drake3a28ca82001-08-13 20:26:19 +000087 for base in value.super ]
Tim Peters04601062001-08-13 22:25:24 +000088
Guido van Rossum0ed7aa12002-12-02 14:54:20 +000089 try:
90 self.assertListEq(real_bases, pyclbr_bases, ignore)
91 except:
92 print >>sys.stderr, "class=%s" % py_item
93 raise
Fred Drake3a28ca82001-08-13 20:26:19 +000094
95 actualMethods = []
Tim Peters37a309d2001-09-04 01:20:04 +000096 for m in py_item.__dict__.keys():
Guido van Rossum7f6a4392002-12-03 08:16:50 +000097 if ismethod(getattr(py_item, m), m):
Fred Drake3a28ca82001-08-13 20:26:19 +000098 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 Rossum7f6a4392002-12-03 08:16:50 +0000106 try:
107 self.assertListEq(foundMethods, actualMethods, ignore)
108 self.assertEquals(py_item.__module__, value.module)
Fred Drake3a28ca82001-08-13 20:26:19 +0000109
Guido van Rossum7f6a4392002-12-03 08:16:50 +0000110 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 Drake3a28ca82001-08-13 20:26:19 +0000115
116 # Now check for missing stuff.
Guido van Rossum0ed7aa12002-12-02 14:54:20 +0000117 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 Drake3a28ca82001-08-13 20:26:19 +0000123 for name in dir(module):
124 item = getattr(module, name)
Guido van Rossum0ed7aa12002-12-02 14:54:20 +0000125 if isinstance(item, (ClassType, FunctionType)):
126 if defined_in(item, module):
127 self.assertHaskey(dict, name, ignore)
Fred Drake3a28ca82001-08-13 20:26:19 +0000128
129 def test_easy(self):
130 self.checkModule('pyclbr')
Guido van Rossum7f6a4392002-12-03 08:16:50 +0000131 self.checkModule('doctest')
132 self.checkModule('rfc822')
Fred Drake3a28ca82001-08-13 20:26:19 +0000133 self.checkModule('difflib')
134
135 def test_others(self):
136 cm = self.checkModule
137
Guido van Rossum7f6a4392002-12-03 08:16:50 +0000138 # These were once about the 10 longest modules
Raymond Hettingere401b6f2002-12-30 07:21:32 +0000139 cm('random', ignore=('Random',)) # from _random import Random as CoreGenerator
Guido van Rossum7f6a4392002-12-03 08:16:50 +0000140 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 Drake3a28ca82001-08-13 20:26:19 +0000150
Guido van Rossum0ed7aa12002-12-02 14:54:20 +0000151 # Tests for modules inside packages
152 cm('email.Parser')
Guido van Rossum7f6a4392002-12-03 08:16:50 +0000153 cm('test.test_pyclbr')
Fred Drake3a28ca82001-08-13 20:26:19 +0000154
Fred Drake2e2be372001-09-20 21:33:42 +0000155
156def test_main():
157 run_unittest(PyclbrTest)
158
159
160if __name__ == "__main__":
161 test_main()