home *** CD-ROM | disk | FTP | other *** search
/ Freelog 33 / Freelog033.iso / Progr / Python-2.2.1.exe / TEST_PYCLBR.PY < prev    next >
Encoding:
Python Source  |  2002-03-14  |  6.0 KB  |  168 lines

  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.  
  12. from commands import getstatus
  13.  
  14. # Here we test the python class browser code.
  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)
  30.  
  31.  
  32.     def assertHasattr(self, obj, attr, ignore):
  33.         ''' succeed iff hasattr(obj,attr) or attr in ignore. '''
  34.         if attr in ignore: return
  35.         if not hasattr(obj, attr): print "???", attr
  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:
  58.             module = __import__(moduleName, globals(), {}, [])
  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():
  64.             if name in ignore:
  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__]
  73.                 pyclbr_bases = [ getattr(base, 'name', base)
  74.                                  for base in value.super ]
  75.  
  76.                 self.assertListEq(real_bases, pyclbr_bases, ignore)
  77.  
  78.                 actualMethods = []
  79.                 for m in py_item.__dict__.keys():
  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')
  103.         self.checkModule('doctest',
  104.                          ignore=['_isclass',
  105.                                  '_isfunction',
  106.                                  '_ismodule',
  107.                                  '_classify_class_attrs'])
  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 =
  126.                              'getproxies_registry',    # set with =
  127.                              'open_https'))            # not on all platforms
  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.  
  137.  
  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',
  144.                               'HTTP11'))   # not on all platforms
  145.  
  146.         cm('Cookie', ignore=('__str__', 'Cookie')) # set with =
  147.  
  148.         cm('sre_parse', ignore=('literal', # nested def
  149.                                 'makedict', 'dump' # from sre_constants
  150.                                 ))
  151.  
  152.         cm('test.test_pyclbr',
  153.            module=sys.modules[__name__])
  154.  
  155.         # pydoc doesn't work because of string issues
  156.         # cm('pydoc', pydoc)
  157.  
  158.         # pdb plays too many dynamic games
  159.         # cm('pdb', pdb)
  160.  
  161.  
  162. def test_main():
  163.     run_unittest(PyclbrTest)
  164.  
  165.  
  166. if __name__ == "__main__":
  167.     test_main()
  168.