blob: e83989e2d8e3f75252c9959ddf4c5afd8e6b00c3 [file] [log] [blame]
Fred Drake3a28ca82001-08-13 20:26:19 +00001'''
2 Test cases for pyclbr.py
3 Nick Mathewson
4'''
Benjamin Petersonee8712c2008-05-20 21:35:26 +00005from test.support import run_unittest
Christian Heimes05e8be12008-02-23 18:30:17 +00006import sys
Guido van Rossum13257902007-06-07 23:15:56 +00007from types import FunctionType, MethodType, BuiltinFunctionType
Fred Drake3a28ca82001-08-13 20:26:19 +00008import pyclbr
Guido van Rossum0ed7aa12002-12-02 14:54:20 +00009from unittest import TestCase
Fred Drake3a28ca82001-08-13 20:26:19 +000010
Anthony Baxterc2a5a632004-08-02 06:10:11 +000011StaticMethodType = type(staticmethod(lambda: None))
12ClassMethodType = type(classmethod(lambda c: None))
13
Tim Peters04601062001-08-13 22:25:24 +000014# Here we test the python class browser code.
Fred Drake3a28ca82001-08-13 20:26:19 +000015#
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
Guido van Rossum0ed7aa12002-12-02 14:54:20 +000021class PyclbrTest(TestCase):
Fred Drake3a28ca82001-08-13 20:26:19 +000022
23 def assertListEq(self, l1, l2, ignore):
24 ''' succeed iff {l1} - {ignore} == {l2} - {ignore} '''
Raymond Hettingera690a992003-11-16 16:17:49 +000025 missing = (set(l1) ^ set(l2)) - set(ignore)
Raymond Hettinger91bbd9a2003-05-02 09:06:28 +000026 if missing:
Guido van Rossumbe19ed72007-02-09 05:37:30 +000027 print("l1=%r\nl2=%r\nignore=%r" % (l1, l2, ignore), file=sys.stderr)
Raymond Hettinger91bbd9a2003-05-02 09:06:28 +000028 self.fail("%r missing" % missing.pop())
Tim Peters04601062001-08-13 22:25:24 +000029
Fred Drake3a28ca82001-08-13 20:26:19 +000030 def assertHasattr(self, obj, attr, ignore):
31 ''' succeed iff hasattr(obj,attr) or attr in ignore. '''
32 if attr in ignore: return
Guido van Rossumbe19ed72007-02-09 05:37:30 +000033 if not hasattr(obj, attr): print("???", attr)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000034 self.assertTrue(hasattr(obj, attr),
Tim Peters5e5ca562002-07-10 02:37:21 +000035 'expected hasattr(%r, %r)' % (obj, attr))
Fred Drake3a28ca82001-08-13 20:26:19 +000036
37
38 def assertHaskey(self, obj, key, ignore):
Guido van Rossume2b70bc2006-08-18 22:13:04 +000039 ''' succeed iff key in obj or key in ignore. '''
Fred Drake3a28ca82001-08-13 20:26:19 +000040 if key in ignore: return
Guido van Rossume2b70bc2006-08-18 22:13:04 +000041 if key not in obj:
Guido van Rossumbe19ed72007-02-09 05:37:30 +000042 print("***",key, file=sys.stderr)
Benjamin Peterson577473f2010-01-19 00:09:57 +000043 self.assertIn(key, obj)
Fred Drake3a28ca82001-08-13 20:26:19 +000044
Anthony Baxterc2a5a632004-08-02 06:10:11 +000045 def assertEqualsOrIgnored(self, a, b, ignore):
Fred Drake3a28ca82001-08-13 20:26:19 +000046 ''' succeed iff a == b or a in ignore or b in ignore '''
Anthony Baxterc2a5a632004-08-02 06:10:11 +000047 if a not in ignore and b not in ignore:
Ezio Melottib3aedd42010-11-20 19:04:17 +000048 self.assertEqual(a, b)
Fred Drake3a28ca82001-08-13 20:26:19 +000049
50 def checkModule(self, moduleName, module=None, ignore=()):
51 ''' succeed iff pyclbr.readmodule_ex(modulename) corresponds
52 to the actual module object, module. Any identifiers in
53 ignore are ignored. If no module is provided, the appropriate
54 module is loaded with __import__.'''
55
Guido van Rossumd858f702006-04-21 09:17:15 +000056 ignore = set(ignore) | set(['object'])
57
Benjamin Peterson2a691a82008-03-31 01:51:45 +000058 if module is 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
Anthony Baxterc2a5a632004-08-02 06:10:11 +000065 def ismethod(oclass, obj, name):
66 classdict = oclass.__dict__
Christian Heimes4a22b5d2007-11-25 09:39:14 +000067 if isinstance(obj, MethodType):
68 # could be a classmethod
69 if (not isinstance(classdict[name], ClassMethodType) or
Christian Heimesff737952007-11-27 10:40:20 +000070 obj.__self__ is not oclass):
Anthony Baxterc2a5a632004-08-02 06:10:11 +000071 return False
Christian Heimes4a22b5d2007-11-25 09:39:14 +000072 elif not isinstance(obj, FunctionType):
73 return False
Anthony Baxterc2a5a632004-08-02 06:10:11 +000074
Guido van Rossum7f6a4392002-12-03 08:16:50 +000075 objname = obj.__name__
76 if objname.startswith("__") and not objname.endswith("__"):
Christian Heimes4a22b5d2007-11-25 09:39:14 +000077 objname = "_%s%s" % (oclass.__name__, objname)
Guido van Rossum7f6a4392002-12-03 08:16:50 +000078 return objname == name
79
Fred Drake3a28ca82001-08-13 20:26:19 +000080 # Make sure the toplevel functions and classes are the same.
81 for name, value in dict.items():
Tim Peters04601062001-08-13 22:25:24 +000082 if name in ignore:
Fred Drake3a28ca82001-08-13 20:26:19 +000083 continue
84 self.assertHasattr(module, name, ignore)
85 py_item = getattr(module, name)
86 if isinstance(value, pyclbr.Function):
Ezio Melottie9615932010-01-24 19:26:24 +000087 self.assertIsInstance(py_item, (FunctionType, BuiltinFunctionType))
Thomas Wouters89f507f2006-12-13 04:49:30 +000088 if py_item.__module__ != moduleName:
89 continue # skip functions that came from somewhere else
Ezio Melottib3aedd42010-11-20 19:04:17 +000090 self.assertEqual(py_item.__module__, value.module)
Fred Drake3a28ca82001-08-13 20:26:19 +000091 else:
Ezio Melottie9615932010-01-24 19:26:24 +000092 self.assertIsInstance(py_item, type)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000093 if py_item.__module__ != moduleName:
94 continue # skip classes that came from somewhere else
95
Fred Drake3a28ca82001-08-13 20:26:19 +000096 real_bases = [base.__name__ for base in py_item.__bases__]
Tim Peters04601062001-08-13 22:25:24 +000097 pyclbr_bases = [ getattr(base, 'name', base)
Fred Drake3a28ca82001-08-13 20:26:19 +000098 for base in value.super ]
Tim Peters04601062001-08-13 22:25:24 +000099
Guido van Rossum0ed7aa12002-12-02 14:54:20 +0000100 try:
101 self.assertListEq(real_bases, pyclbr_bases, ignore)
102 except:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000103 print("class=%s" % py_item, file=sys.stderr)
Guido van Rossum0ed7aa12002-12-02 14:54:20 +0000104 raise
Fred Drake3a28ca82001-08-13 20:26:19 +0000105
106 actualMethods = []
Tim Peters37a309d2001-09-04 01:20:04 +0000107 for m in py_item.__dict__.keys():
Anthony Baxterc2a5a632004-08-02 06:10:11 +0000108 if ismethod(py_item, getattr(py_item, m), m):
Fred Drake3a28ca82001-08-13 20:26:19 +0000109 actualMethods.append(m)
110 foundMethods = []
111 for m in value.methods.keys():
112 if m[:2] == '__' and m[-2:] != '__':
113 foundMethods.append('_'+name+m)
114 else:
115 foundMethods.append(m)
116
Guido van Rossum7f6a4392002-12-03 08:16:50 +0000117 try:
118 self.assertListEq(foundMethods, actualMethods, ignore)
Ezio Melottib3aedd42010-11-20 19:04:17 +0000119 self.assertEqual(py_item.__module__, value.module)
Fred Drake3a28ca82001-08-13 20:26:19 +0000120
Anthony Baxterc2a5a632004-08-02 06:10:11 +0000121 self.assertEqualsOrIgnored(py_item.__name__, value.name,
122 ignore)
Guido van Rossum7f6a4392002-12-03 08:16:50 +0000123 # can't check file or lineno
124 except:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000125 print("class=%s" % py_item, file=sys.stderr)
Guido van Rossum7f6a4392002-12-03 08:16:50 +0000126 raise
Fred Drake3a28ca82001-08-13 20:26:19 +0000127
128 # Now check for missing stuff.
Guido van Rossum0ed7aa12002-12-02 14:54:20 +0000129 def defined_in(item, module):
Guido van Rossum13257902007-06-07 23:15:56 +0000130 if isinstance(item, type):
Guido van Rossum0ed7aa12002-12-02 14:54:20 +0000131 return item.__module__ == module.__name__
132 if isinstance(item, FunctionType):
Neal Norwitz221085d2007-02-25 20:55:47 +0000133 return item.__globals__ is module.__dict__
Guido van Rossum0ed7aa12002-12-02 14:54:20 +0000134 return False
Fred Drake3a28ca82001-08-13 20:26:19 +0000135 for name in dir(module):
136 item = getattr(module, name)
Guido van Rossum13257902007-06-07 23:15:56 +0000137 if isinstance(item, (type, FunctionType)):
Guido van Rossum0ed7aa12002-12-02 14:54:20 +0000138 if defined_in(item, module):
139 self.assertHaskey(dict, name, ignore)
Fred Drake3a28ca82001-08-13 20:26:19 +0000140
141 def test_easy(self):
142 self.checkModule('pyclbr')
Georg Brandl9f0f9602008-06-12 22:23:59 +0000143 self.checkModule('ast')
Benjamin Petersonbed7d042009-07-19 21:01:52 +0000144 self.checkModule('doctest', ignore=("TestResults", "_SpoofOut",
145 "DocTestCase"))
Christian Heimes25bb7832008-01-11 16:17:00 +0000146 self.checkModule('difflib', ignore=("Match",))
Fred Drake3a28ca82001-08-13 20:26:19 +0000147
Anthony Baxterc2a5a632004-08-02 06:10:11 +0000148 def test_decorators(self):
149 # XXX: See comment in pyclbr_input.py for a test that would fail
150 # if it were not commented out.
151 #
Christian Heimes4a22b5d2007-11-25 09:39:14 +0000152 self.checkModule('test.pyclbr_input', ignore=['om'])
Tim Peters6db15d72004-08-04 02:36:18 +0000153
Fred Drake3a28ca82001-08-13 20:26:19 +0000154 def test_others(self):
155 cm = self.checkModule
156
Guido van Rossum7f6a4392002-12-03 08:16:50 +0000157 # These were once about the 10 longest modules
Raymond Hettingere401b6f2002-12-30 07:21:32 +0000158 cm('random', ignore=('Random',)) # from _random import Random as CoreGenerator
Guido van Rossum7f6a4392002-12-03 08:16:50 +0000159 cm('cgi', ignore=('log',)) # set with = in module
Tim Peters264c6592004-07-18 00:08:11 +0000160 cm('pickle')
Guido van Rossum7f6a4392002-12-03 08:16:50 +0000161 cm('aifc', ignore=('openfp',)) # set with = in module
Guido van Rossum7f6a4392002-12-03 08:16:50 +0000162 cm('sre_parse', ignore=('dump',)) # from sre_constants import *
163 cm('pdb')
164 cm('pydoc')
Fred Drake3a28ca82001-08-13 20:26:19 +0000165
Guido van Rossum0ed7aa12002-12-02 14:54:20 +0000166 # Tests for modules inside packages
Neal Norwitz315d8452007-08-30 03:06:59 +0000167 cm('email.parser')
Guido van Rossum7f6a4392002-12-03 08:16:50 +0000168 cm('test.test_pyclbr')
Fred Drake3a28ca82001-08-13 20:26:19 +0000169
Petri Lehtinen8d886042012-05-18 21:51:11 +0300170 def test_issue_14798(self):
171 # test ImportError is raised when the first part of a dotted name is
172 # not a package
173 self.assertRaises(ImportError, pyclbr.readmodule_ex, 'asyncore.foo')
174
Fred Drake2e2be372001-09-20 21:33:42 +0000175
176def test_main():
177 run_unittest(PyclbrTest)
178
179
180if __name__ == "__main__":
181 test_main()