| 
									
										
										
										
											2001-08-13 20:26:19 +00:00
										 |  |  | '''
 | 
					
						
							|  |  |  |    Test cases for pyclbr.py | 
					
						
							|  |  |  |    Nick Mathewson | 
					
						
							|  |  |  | '''
 | 
					
						
							| 
									
										
										
										
											2002-07-23 19:04:11 +00:00
										 |  |  | from test.test_support import run_unittest | 
					
						
							| 
									
										
										
										
											2001-08-13 20:26:19 +00:00
										 |  |  | import unittest, sys | 
					
						
							| 
									
										
										
										
											2004-09-04 23:53:20 +00:00
										 |  |  | from types import ClassType, FunctionType, MethodType, BuiltinFunctionType | 
					
						
							| 
									
										
										
										
											2001-08-13 20:26:19 +00:00
										 |  |  | import pyclbr | 
					
						
							| 
									
										
										
										
											2002-12-02 14:54:20 +00:00
										 |  |  | from unittest import TestCase | 
					
						
							| 
									
										
										
										
											2001-08-13 20:26:19 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-08-02 06:10:11 +00:00
										 |  |  | StaticMethodType = type(staticmethod(lambda: None)) | 
					
						
							|  |  |  | ClassMethodType = type(classmethod(lambda c: None)) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-08-13 20:26:19 +00:00
										 |  |  | # This next line triggers an error on old versions of pyclbr. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-08-13 22:25:24 +00:00
										 |  |  | from commands import getstatus | 
					
						
							| 
									
										
										
										
											2001-08-13 20:26:19 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-08-13 22:25:24 +00:00
										 |  |  | # Here we test the python class browser code. | 
					
						
							| 
									
										
										
										
											2001-08-13 20:26:19 +00:00
										 |  |  | # | 
					
						
							|  |  |  | # The main function in this suite, 'testModule', compares the output | 
					
						
							|  |  |  | # of pyclbr with the introspected members of a module.  Because pyclbr | 
					
						
							|  |  |  | # is imperfect (as designed), testModule is called with a set of | 
					
						
							|  |  |  | # members to ignore. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-12-02 14:54:20 +00:00
										 |  |  | class PyclbrTest(TestCase): | 
					
						
							| 
									
										
										
										
											2001-08-13 20:26:19 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def assertListEq(self, l1, l2, ignore): | 
					
						
							|  |  |  |         ''' succeed iff {l1} - {ignore} == {l2} - {ignore} ''' | 
					
						
							| 
									
										
										
										
											2003-11-16 16:17:49 +00:00
										 |  |  |         missing = (set(l1) ^ set(l2)) - set(ignore) | 
					
						
							| 
									
										
										
										
											2003-05-02 09:06:28 +00:00
										 |  |  |         if missing: | 
					
						
							| 
									
										
										
										
											2002-12-03 08:16:50 +00:00
										 |  |  |             print >>sys.stderr, "l1=%r\nl2=%r\nignore=%r" % (l1, l2, ignore) | 
					
						
							| 
									
										
										
										
											2003-05-02 09:06:28 +00:00
										 |  |  |             self.fail("%r missing" % missing.pop()) | 
					
						
							| 
									
										
										
										
											2001-08-13 22:25:24 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-08-13 20:26:19 +00:00
										 |  |  |     def assertHasattr(self, obj, attr, ignore): | 
					
						
							|  |  |  |         ''' succeed iff hasattr(obj,attr) or attr in ignore. ''' | 
					
						
							|  |  |  |         if attr in ignore: return | 
					
						
							| 
									
										
										
										
											2001-10-02 03:53:41 +00:00
										 |  |  |         if not hasattr(obj, attr): print "???", attr | 
					
						
							| 
									
										
										
										
											2002-07-10 02:37:21 +00:00
										 |  |  |         self.failUnless(hasattr(obj, attr), | 
					
						
							|  |  |  |                         'expected hasattr(%r, %r)' % (obj, attr)) | 
					
						
							| 
									
										
										
										
											2001-08-13 20:26:19 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def assertHaskey(self, obj, key, ignore): | 
					
						
							|  |  |  |         ''' succeed iff obj.has_key(key) or key in ignore. ''' | 
					
						
							|  |  |  |         if key in ignore: return | 
					
						
							| 
									
										
										
										
											2002-12-02 14:54:20 +00:00
										 |  |  |         if not obj.has_key(key): | 
					
						
							|  |  |  |             print >>sys.stderr, "***",key | 
					
						
							| 
									
										
										
										
											2001-08-13 20:26:19 +00:00
										 |  |  |         self.failUnless(obj.has_key(key)) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-08-02 06:10:11 +00:00
										 |  |  |     def assertEqualsOrIgnored(self, a, b, ignore): | 
					
						
							| 
									
										
										
										
											2001-08-13 20:26:19 +00:00
										 |  |  |         ''' succeed iff a == b or a in ignore or b in ignore ''' | 
					
						
							| 
									
										
										
										
											2004-08-02 06:10:11 +00:00
										 |  |  |         if a not in ignore and b not in ignore: | 
					
						
							|  |  |  |             self.assertEquals(a, b) | 
					
						
							| 
									
										
										
										
											2001-08-13 20:26:19 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def checkModule(self, moduleName, module=None, ignore=()): | 
					
						
							|  |  |  |         ''' succeed iff pyclbr.readmodule_ex(modulename) corresponds
 | 
					
						
							|  |  |  |             to the actual module object, module.  Any identifiers in | 
					
						
							|  |  |  |             ignore are ignored.   If no module is provided, the appropriate | 
					
						
							|  |  |  |             module is loaded with __import__.'''
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if module == None: | 
					
						
							| 
									
										
										
										
											2002-12-02 14:54:20 +00:00
										 |  |  |             # Import it. | 
					
						
							|  |  |  |             # ('<silly>' is to work around an API silliness in __import__) | 
					
						
							|  |  |  |             module = __import__(moduleName, globals(), {}, ['<silly>']) | 
					
						
							| 
									
										
										
										
											2001-08-13 20:26:19 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         dict = pyclbr.readmodule_ex(moduleName) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-08-02 06:10:11 +00:00
										 |  |  |         def ismethod(oclass, obj, name): | 
					
						
							|  |  |  |             classdict = oclass.__dict__ | 
					
						
							|  |  |  |             if isinstance(obj, FunctionType): | 
					
						
							|  |  |  |                 if not isinstance(classdict[name], StaticMethodType): | 
					
						
							|  |  |  |                     return False | 
					
						
							|  |  |  |             else: | 
					
						
							|  |  |  |                 if not  isinstance(obj, MethodType): | 
					
						
							|  |  |  |                     return False | 
					
						
							|  |  |  |                 if obj.im_self is not None: | 
					
						
							|  |  |  |                     if (not isinstance(classdict[name], ClassMethodType) or | 
					
						
							|  |  |  |                         obj.im_self is not oclass): | 
					
						
							|  |  |  |                         return False | 
					
						
							|  |  |  |                 else: | 
					
						
							|  |  |  |                     if not isinstance(classdict[name], FunctionType): | 
					
						
							|  |  |  |                         return False | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-12-03 08:16:50 +00:00
										 |  |  |             objname = obj.__name__ | 
					
						
							|  |  |  |             if objname.startswith("__") and not objname.endswith("__"): | 
					
						
							|  |  |  |                 objname = "_%s%s" % (obj.im_class.__name__, objname) | 
					
						
							|  |  |  |             return objname == name | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-08-13 20:26:19 +00:00
										 |  |  |         # Make sure the toplevel functions and classes are the same. | 
					
						
							|  |  |  |         for name, value in dict.items(): | 
					
						
							| 
									
										
										
										
											2001-08-13 22:25:24 +00:00
										 |  |  |             if name in ignore: | 
					
						
							| 
									
										
										
										
											2001-08-13 20:26:19 +00:00
										 |  |  |                 continue | 
					
						
							|  |  |  |             self.assertHasattr(module, name, ignore) | 
					
						
							|  |  |  |             py_item = getattr(module, name) | 
					
						
							|  |  |  |             if isinstance(value, pyclbr.Function): | 
					
						
							| 
									
										
										
										
											2004-09-04 23:53:20 +00:00
										 |  |  |                 self.assert_(isinstance(py_item, (FunctionType, BuiltinFunctionType))) | 
					
						
							| 
									
										
										
										
											2006-09-30 11:06:47 +00:00
										 |  |  |                 if py_item.__module__ != moduleName: | 
					
						
							|  |  |  |                     continue   # skip functions that came from somewhere else | 
					
						
							|  |  |  |                 self.assertEquals(py_item.__module__, value.module) | 
					
						
							| 
									
										
										
										
											2001-08-13 20:26:19 +00:00
										 |  |  |             else: | 
					
						
							| 
									
										
										
										
											2004-08-02 06:10:11 +00:00
										 |  |  |                 self.failUnless(isinstance(py_item, (ClassType, type))) | 
					
						
							| 
									
										
										
										
											2006-04-18 04:53:28 +00:00
										 |  |  |                 if py_item.__module__ != moduleName: | 
					
						
							| 
									
										
										
										
											2006-04-18 01:39:25 +00:00
										 |  |  |                     continue   # skip classes that came from somewhere else | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-08-13 20:26:19 +00:00
										 |  |  |                 real_bases = [base.__name__ for base in py_item.__bases__] | 
					
						
							| 
									
										
										
										
											2001-08-13 22:25:24 +00:00
										 |  |  |                 pyclbr_bases = [ getattr(base, 'name', base) | 
					
						
							| 
									
										
										
										
											2001-08-13 20:26:19 +00:00
										 |  |  |                                  for base in value.super ] | 
					
						
							| 
									
										
										
										
											2001-08-13 22:25:24 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-12-02 14:54:20 +00:00
										 |  |  |                 try: | 
					
						
							|  |  |  |                     self.assertListEq(real_bases, pyclbr_bases, ignore) | 
					
						
							|  |  |  |                 except: | 
					
						
							|  |  |  |                     print >>sys.stderr, "class=%s" % py_item | 
					
						
							|  |  |  |                     raise | 
					
						
							| 
									
										
										
										
											2001-08-13 20:26:19 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |                 actualMethods = [] | 
					
						
							| 
									
										
										
										
											2001-09-04 01:20:04 +00:00
										 |  |  |                 for m in py_item.__dict__.keys(): | 
					
						
							| 
									
										
										
										
											2004-08-02 06:10:11 +00:00
										 |  |  |                     if ismethod(py_item, getattr(py_item, m), m): | 
					
						
							| 
									
										
										
										
											2001-08-13 20:26:19 +00:00
										 |  |  |                         actualMethods.append(m) | 
					
						
							|  |  |  |                 foundMethods = [] | 
					
						
							|  |  |  |                 for m in value.methods.keys(): | 
					
						
							|  |  |  |                     if m[:2] == '__' and m[-2:] != '__': | 
					
						
							|  |  |  |                         foundMethods.append('_'+name+m) | 
					
						
							|  |  |  |                     else: | 
					
						
							|  |  |  |                         foundMethods.append(m) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-12-03 08:16:50 +00:00
										 |  |  |                 try: | 
					
						
							|  |  |  |                     self.assertListEq(foundMethods, actualMethods, ignore) | 
					
						
							|  |  |  |                     self.assertEquals(py_item.__module__, value.module) | 
					
						
							| 
									
										
										
										
											2001-08-13 20:26:19 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-08-02 06:10:11 +00:00
										 |  |  |                     self.assertEqualsOrIgnored(py_item.__name__, value.name, | 
					
						
							|  |  |  |                                                ignore) | 
					
						
							| 
									
										
										
										
											2002-12-03 08:16:50 +00:00
										 |  |  |                     # can't check file or lineno | 
					
						
							|  |  |  |                 except: | 
					
						
							|  |  |  |                     print >>sys.stderr, "class=%s" % py_item | 
					
						
							|  |  |  |                     raise | 
					
						
							| 
									
										
										
										
											2001-08-13 20:26:19 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         # Now check for missing stuff. | 
					
						
							| 
									
										
										
										
											2002-12-02 14:54:20 +00:00
										 |  |  |         def defined_in(item, module): | 
					
						
							|  |  |  |             if isinstance(item, ClassType): | 
					
						
							|  |  |  |                 return item.__module__ == module.__name__ | 
					
						
							|  |  |  |             if isinstance(item, FunctionType): | 
					
						
							|  |  |  |                 return item.func_globals is module.__dict__ | 
					
						
							|  |  |  |             return False | 
					
						
							| 
									
										
										
										
											2001-08-13 20:26:19 +00:00
										 |  |  |         for name in dir(module): | 
					
						
							|  |  |  |             item = getattr(module, name) | 
					
						
							| 
									
										
										
										
											2002-12-02 14:54:20 +00:00
										 |  |  |             if isinstance(item,  (ClassType, FunctionType)): | 
					
						
							|  |  |  |                 if defined_in(item, module): | 
					
						
							|  |  |  |                     self.assertHaskey(dict, name, ignore) | 
					
						
							| 
									
										
										
										
											2001-08-13 20:26:19 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_easy(self): | 
					
						
							|  |  |  |         self.checkModule('pyclbr') | 
					
						
							| 
									
										
										
										
											2002-12-03 08:16:50 +00:00
										 |  |  |         self.checkModule('doctest') | 
					
						
							|  |  |  |         self.checkModule('rfc822') | 
					
						
							| 
									
										
										
										
											2001-08-13 20:26:19 +00:00
										 |  |  |         self.checkModule('difflib') | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2004-08-02 06:10:11 +00:00
										 |  |  |     def test_decorators(self): | 
					
						
							|  |  |  |         # XXX: See comment in pyclbr_input.py for a test that would fail | 
					
						
							|  |  |  |         #      if it were not commented out. | 
					
						
							|  |  |  |         # | 
					
						
							|  |  |  |         self.checkModule('test.pyclbr_input') | 
					
						
							| 
									
										
										
										
											2004-08-04 02:36:18 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-08-13 20:26:19 +00:00
										 |  |  |     def test_others(self): | 
					
						
							|  |  |  |         cm = self.checkModule | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-12-03 08:16:50 +00:00
										 |  |  |         # These were once about the 10 longest modules | 
					
						
							| 
									
										
										
										
											2002-12-30 07:21:32 +00:00
										 |  |  |         cm('random', ignore=('Random',))  # from _random import Random as CoreGenerator | 
					
						
							| 
									
										
										
										
											2002-12-03 08:16:50 +00:00
										 |  |  |         cm('cgi', ignore=('log',))      # set with = in module | 
					
						
							|  |  |  |         cm('mhlib') | 
					
						
							|  |  |  |         cm('urllib', ignore=('getproxies_registry', | 
					
						
							| 
									
										
										
										
											2004-07-18 00:00:03 +00:00
										 |  |  |                              'open_https', | 
					
						
							|  |  |  |                              'getproxies_internetconfig',)) # not on all platforms | 
					
						
							| 
									
										
										
										
											2004-07-18 00:08:11 +00:00
										 |  |  |         cm('pickle') | 
					
						
							| 
									
										
										
										
											2002-12-03 08:16:50 +00:00
										 |  |  |         cm('aifc', ignore=('openfp',))  # set with = in module | 
					
						
							|  |  |  |         cm('Cookie') | 
					
						
							|  |  |  |         cm('sre_parse', ignore=('dump',)) # from sre_constants import * | 
					
						
							|  |  |  |         cm('pdb') | 
					
						
							|  |  |  |         cm('pydoc') | 
					
						
							| 
									
										
										
										
											2001-08-13 20:26:19 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-12-02 14:54:20 +00:00
										 |  |  |         # Tests for modules inside packages | 
					
						
							| 
									
										
										
										
											2006-03-18 15:41:53 +00:00
										 |  |  |         cm('email.parser') | 
					
						
							| 
									
										
										
										
											2002-12-03 08:16:50 +00:00
										 |  |  |         cm('test.test_pyclbr') | 
					
						
							| 
									
										
										
										
											2001-08-13 20:26:19 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-09-20 21:33:42 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | def test_main(): | 
					
						
							|  |  |  |     run_unittest(PyclbrTest) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | if __name__ == "__main__": | 
					
						
							|  |  |  |     test_main() |