blob: 3bfee8d24341c7ae6c4d235465a9c217e684c8cf [file] [log] [blame]
Fred Drake3a28ca82001-08-13 20:26:19 +00001'''
2 Test cases for pyclbr.py
3 Nick Mathewson
4'''
Ezio Melottia2d46532010-01-30 07:22:54 +00005from test.test_support import run_unittest, import_module
Christian Heimesc5f05e42008-02-23 17:40:11 +00006import sys
Raymond Hettingerc4536a12004-09-04 23:53:20 +00007from types import ClassType, 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
Ezio Melottia2d46532010-01-30 07:22:54 +000014# Silence Py3k warning
15import_module('commands', deprecated=True)
Fred Drake3a28ca82001-08-13 20:26:19 +000016
Ezio Melottia2d46532010-01-30 07:22:54 +000017# This next line triggers an error on old versions of pyclbr.
Tim Peters04601062001-08-13 22:25:24 +000018from commands import getstatus
Fred Drake3a28ca82001-08-13 20:26:19 +000019
Tim Peters04601062001-08-13 22:25:24 +000020# Here we test the python class browser code.
Fred Drake3a28ca82001-08-13 20:26:19 +000021#
22# The main function in this suite, 'testModule', compares the output
23# of pyclbr with the introspected members of a module. Because pyclbr
24# is imperfect (as designed), testModule is called with a set of
25# members to ignore.
26
Guido van Rossum0ed7aa12002-12-02 14:54:20 +000027class PyclbrTest(TestCase):
Fred Drake3a28ca82001-08-13 20:26:19 +000028
29 def assertListEq(self, l1, l2, ignore):
30 ''' succeed iff {l1} - {ignore} == {l2} - {ignore} '''
Raymond Hettingera690a992003-11-16 16:17:49 +000031 missing = (set(l1) ^ set(l2)) - set(ignore)
Raymond Hettinger91bbd9a2003-05-02 09:06:28 +000032 if missing:
Guido van Rossum7f6a4392002-12-03 08:16:50 +000033 print >>sys.stderr, "l1=%r\nl2=%r\nignore=%r" % (l1, l2, ignore)
Raymond Hettinger91bbd9a2003-05-02 09:06:28 +000034 self.fail("%r missing" % missing.pop())
Tim Peters04601062001-08-13 22:25:24 +000035
Fred Drake3a28ca82001-08-13 20:26:19 +000036 def assertHasattr(self, obj, attr, ignore):
37 ''' succeed iff hasattr(obj,attr) or attr in ignore. '''
38 if attr in ignore: return
Tim Peters7402f792001-10-02 03:53:41 +000039 if not hasattr(obj, attr): print "???", attr
Benjamin Peterson5c8da862009-06-30 22:57:08 +000040 self.assertTrue(hasattr(obj, attr),
Tim Peters5e5ca562002-07-10 02:37:21 +000041 'expected hasattr(%r, %r)' % (obj, attr))
Fred Drake3a28ca82001-08-13 20:26:19 +000042
43
44 def assertHaskey(self, obj, key, ignore):
Ezio Melottia2d46532010-01-30 07:22:54 +000045 ''' succeed iff key in obj or key in ignore. '''
Fred Drake3a28ca82001-08-13 20:26:19 +000046 if key in ignore: return
Ezio Melottia2d46532010-01-30 07:22:54 +000047 if key not in obj:
48 print >>sys.stderr, "***", key
49 self.assertIn(key, obj)
Fred Drake3a28ca82001-08-13 20:26:19 +000050
Anthony Baxterc2a5a632004-08-02 06:10:11 +000051 def assertEqualsOrIgnored(self, a, b, ignore):
Fred Drake3a28ca82001-08-13 20:26:19 +000052 ''' succeed iff a == b or a in ignore or b in ignore '''
Anthony Baxterc2a5a632004-08-02 06:10:11 +000053 if a not in ignore and b not in ignore:
Ezio Melottia2d46532010-01-30 07:22:54 +000054 self.assertEqual(a, b)
Fred Drake3a28ca82001-08-13 20:26:19 +000055
56 def checkModule(self, moduleName, module=None, ignore=()):
57 ''' succeed iff pyclbr.readmodule_ex(modulename) corresponds
58 to the actual module object, module. Any identifiers in
59 ignore are ignored. If no module is provided, the appropriate
60 module is loaded with __import__.'''
61
Benjamin Peterson5b63acd2008-03-29 15:24:25 +000062 if module is None:
Guido van Rossum0ed7aa12002-12-02 14:54:20 +000063 # Import it.
64 # ('<silly>' is to work around an API silliness in __import__)
65 module = __import__(moduleName, globals(), {}, ['<silly>'])
Fred Drake3a28ca82001-08-13 20:26:19 +000066
67 dict = pyclbr.readmodule_ex(moduleName)
68
Anthony Baxterc2a5a632004-08-02 06:10:11 +000069 def ismethod(oclass, obj, name):
70 classdict = oclass.__dict__
71 if isinstance(obj, FunctionType):
72 if not isinstance(classdict[name], StaticMethodType):
73 return False
74 else:
75 if not isinstance(obj, MethodType):
76 return False
77 if obj.im_self is not None:
78 if (not isinstance(classdict[name], ClassMethodType) or
79 obj.im_self is not oclass):
80 return False
81 else:
82 if not isinstance(classdict[name], FunctionType):
83 return False
84
Guido van Rossum7f6a4392002-12-03 08:16:50 +000085 objname = obj.__name__
86 if objname.startswith("__") and not objname.endswith("__"):
87 objname = "_%s%s" % (obj.im_class.__name__, objname)
88 return objname == name
89
Fred Drake3a28ca82001-08-13 20:26:19 +000090 # Make sure the toplevel functions and classes are the same.
91 for name, value in dict.items():
Tim Peters04601062001-08-13 22:25:24 +000092 if name in ignore:
Fred Drake3a28ca82001-08-13 20:26:19 +000093 continue
94 self.assertHasattr(module, name, ignore)
95 py_item = getattr(module, name)
96 if isinstance(value, pyclbr.Function):
Ezio Melottib0f5adc2010-01-24 16:58:36 +000097 self.assertIsInstance(py_item, (FunctionType, BuiltinFunctionType))
Georg Brandl154324a2006-09-30 11:06:47 +000098 if py_item.__module__ != moduleName:
99 continue # skip functions that came from somewhere else
Ezio Melotti2623a372010-11-21 13:34:58 +0000100 self.assertEqual(py_item.__module__, value.module)
Fred Drake3a28ca82001-08-13 20:26:19 +0000101 else:
Ezio Melottib0f5adc2010-01-24 16:58:36 +0000102 self.assertIsInstance(py_item, (ClassType, type))
Neal Norwitz041669f2006-04-18 04:53:28 +0000103 if py_item.__module__ != moduleName:
Phillip J. Eby742cd242006-04-18 01:39:25 +0000104 continue # skip classes that came from somewhere else
105
Fred Drake3a28ca82001-08-13 20:26:19 +0000106 real_bases = [base.__name__ for base in py_item.__bases__]
Tim Peters04601062001-08-13 22:25:24 +0000107 pyclbr_bases = [ getattr(base, 'name', base)
Fred Drake3a28ca82001-08-13 20:26:19 +0000108 for base in value.super ]
Tim Peters04601062001-08-13 22:25:24 +0000109
Guido van Rossum0ed7aa12002-12-02 14:54:20 +0000110 try:
111 self.assertListEq(real_bases, pyclbr_bases, ignore)
112 except:
113 print >>sys.stderr, "class=%s" % py_item
114 raise
Fred Drake3a28ca82001-08-13 20:26:19 +0000115
116 actualMethods = []
Tim Peters37a309d2001-09-04 01:20:04 +0000117 for m in py_item.__dict__.keys():
Anthony Baxterc2a5a632004-08-02 06:10:11 +0000118 if ismethod(py_item, getattr(py_item, m), m):
Fred Drake3a28ca82001-08-13 20:26:19 +0000119 actualMethods.append(m)
120 foundMethods = []
121 for m in value.methods.keys():
122 if m[:2] == '__' and m[-2:] != '__':
123 foundMethods.append('_'+name+m)
124 else:
125 foundMethods.append(m)
126
Guido van Rossum7f6a4392002-12-03 08:16:50 +0000127 try:
128 self.assertListEq(foundMethods, actualMethods, ignore)
Ezio Melotti2623a372010-11-21 13:34:58 +0000129 self.assertEqual(py_item.__module__, value.module)
Fred Drake3a28ca82001-08-13 20:26:19 +0000130
Anthony Baxterc2a5a632004-08-02 06:10:11 +0000131 self.assertEqualsOrIgnored(py_item.__name__, value.name,
132 ignore)
Guido van Rossum7f6a4392002-12-03 08:16:50 +0000133 # can't check file or lineno
134 except:
135 print >>sys.stderr, "class=%s" % py_item
136 raise
Fred Drake3a28ca82001-08-13 20:26:19 +0000137
138 # Now check for missing stuff.
Guido van Rossum0ed7aa12002-12-02 14:54:20 +0000139 def defined_in(item, module):
140 if isinstance(item, ClassType):
141 return item.__module__ == module.__name__
142 if isinstance(item, FunctionType):
143 return item.func_globals is module.__dict__
144 return False
Fred Drake3a28ca82001-08-13 20:26:19 +0000145 for name in dir(module):
146 item = getattr(module, name)
Guido van Rossum0ed7aa12002-12-02 14:54:20 +0000147 if isinstance(item, (ClassType, FunctionType)):
148 if defined_in(item, module):
149 self.assertHaskey(dict, name, ignore)
Fred Drake3a28ca82001-08-13 20:26:19 +0000150
151 def test_easy(self):
152 self.checkModule('pyclbr')
Benjamin Petersond7b0eeb2009-07-19 20:18:21 +0000153 self.checkModule('doctest', ignore=("DocTestCase",))
Ezio Melottia2d46532010-01-30 07:22:54 +0000154 # Silence Py3k warning
155 rfc822 = import_module('rfc822', deprecated=True)
156 self.checkModule('rfc822', rfc822)
Fred Drake3a28ca82001-08-13 20:26:19 +0000157 self.checkModule('difflib')
158
Anthony Baxterc2a5a632004-08-02 06:10:11 +0000159 def test_decorators(self):
160 # XXX: See comment in pyclbr_input.py for a test that would fail
161 # if it were not commented out.
162 #
163 self.checkModule('test.pyclbr_input')
Tim Peters6db15d72004-08-04 02:36:18 +0000164
Fred Drake3a28ca82001-08-13 20:26:19 +0000165 def test_others(self):
166 cm = self.checkModule
167
Guido van Rossum7f6a4392002-12-03 08:16:50 +0000168 # These were once about the 10 longest modules
Raymond Hettingere401b6f2002-12-30 07:21:32 +0000169 cm('random', ignore=('Random',)) # from _random import Random as CoreGenerator
Guido van Rossum7f6a4392002-12-03 08:16:50 +0000170 cm('cgi', ignore=('log',)) # set with = in module
Amaury Forgeot d'Arce1b93f22008-05-12 22:21:39 +0000171 cm('urllib', ignore=('_CFNumberToInt32',
172 '_CStringFromCFString',
Georg Brandl6e7e0792008-05-18 21:10:19 +0000173 '_CFSetup',
Amaury Forgeot d'Arce1b93f22008-05-12 22:21:39 +0000174 'getproxies_registry',
Georg Brandl85849322008-01-20 14:20:02 +0000175 'proxy_bypass_registry',
Amaury Forgeot d'Arce1b93f22008-05-12 22:21:39 +0000176 'proxy_bypass_macosx_sysconf',
Tim Petersfa7809d2004-07-18 00:00:03 +0000177 'open_https',
Amaury Forgeot d'Arce1b93f22008-05-12 22:21:39 +0000178 'getproxies_macosx_sysconf',
Tim Petersfa7809d2004-07-18 00:00:03 +0000179 'getproxies_internetconfig',)) # not on all platforms
Tim Peters264c6592004-07-18 00:08:11 +0000180 cm('pickle')
Guido van Rossum7f6a4392002-12-03 08:16:50 +0000181 cm('aifc', ignore=('openfp',)) # set with = in module
182 cm('Cookie')
Serhiy Storchakac4d27602014-11-07 22:31:54 +0200183 cm('sre_parse', ignore=('dump', 'groups')) # from sre_constants import *; property
Guido van Rossum7f6a4392002-12-03 08:16:50 +0000184 cm('pdb')
185 cm('pydoc')
Fred Drake3a28ca82001-08-13 20:26:19 +0000186
Guido van Rossum0ed7aa12002-12-02 14:54:20 +0000187 # Tests for modules inside packages
Barry Warsaw40ef0062006-03-18 15:41:53 +0000188 cm('email.parser')
Guido van Rossum7f6a4392002-12-03 08:16:50 +0000189 cm('test.test_pyclbr')
Fred Drake3a28ca82001-08-13 20:26:19 +0000190
Petri Lehtinen280e9f72012-05-18 21:51:11 +0300191 def test_issue_14798(self):
192 # test ImportError is raised when the first part of a dotted name is
193 # not a package
194 self.assertRaises(ImportError, pyclbr.readmodule_ex, 'asyncore.foo')
195
Fred Drake2e2be372001-09-20 21:33:42 +0000196
197def test_main():
198 run_unittest(PyclbrTest)
199
200
201if __name__ == "__main__":
202 test_main()