tests/test_docstrings.py
branchpy33
changeset 3804 79c6b1df0eca
parent 3794 398ea04239b5
child 3910 9f97541a63fe
equal deleted inserted replaced
3803:e2bd2e9f0b3e 3804:79c6b1df0eca
     9 Currently, methods with leading and trailing double underscores
     9 Currently, methods with leading and trailing double underscores
    10 are skipped.
    10 are skipped.
    11 """
    11 """
    12 from reportlab.lib.testutils import setOutDir,SecureTestCase, GlobDirectoryWalker, outputfile, printLocation
    12 from reportlab.lib.testutils import setOutDir,SecureTestCase, GlobDirectoryWalker, outputfile, printLocation
    13 setOutDir(__name__)
    13 setOutDir(__name__)
    14 import os, sys, glob, re, unittest
    14 import os, sys, glob, re, unittest, inspect
    15 from types import ModuleType, ClassType, MethodType, FunctionType
       
    16 import reportlab
    15 import reportlab
       
    16 
       
    17 def typ2is(typ):
       
    18     return getattr(inspect,'is'+typ)
       
    19 
       
    20 def obj2typ(obj):
       
    21     for typ in ('function','module','class','method'):
       
    22         if typ2is(typ)(obj): return typ
       
    23     return None
    17 
    24 
    18 def getModuleObjects(folder, rootName, typ, pattern='*.py'):
    25 def getModuleObjects(folder, rootName, typ, pattern='*.py'):
    19     "Get a list of all objects defined *somewhere* in a package."
    26     "Get a list of all objects defined *somewhere* in a package."
    20     objects = []
    27     objects = []
    21     lookup = {}
    28     lookup = {}
    54 
    61 
    55             # Find the objects in the module's content.
    62             # Find the objects in the module's content.
    56             modContentNames = dir(module)
    63             modContentNames = dir(module)
    57 
    64 
    58             # Handle modules.
    65             # Handle modules.
    59             if typ == ModuleType:
    66             if typ=='module':
    60                 if module.__name__.find('reportlab') > -1:
    67                 if module.__name__.find('reportlab') > -1:
    61                     objects.append((mName, module))
    68                     objects.append((mName, module))
    62                     continue
    69                     continue
    63 
    70 
    64             for n in modContentNames:
    71             for n in modContentNames:
    65                 obj = eval(mName + '.' + n)
    72                 obj = eval(mName + '.' + n)
    66                 # Handle functions and classes.
    73                 # Handle functions and classes.
    67                 if typ in (FunctionType, ClassType):
    74                 if typ in ('function','module'):
    68                     if type(obj) == typ and obj not in lookup:
    75                     if obj2typ(obj) == typ and obj not in lookup:
    69                         if typ == ClassType:
    76                         if typ == 'class':
    70                             if obj.__module__.find(rootName) != 0:
    77                             if obj.__module__.find(rootName) != 0:
    71                                 continue
    78                                 continue
    72                         objects.append((mName, obj))
    79                         objects.append((mName, obj))
    73                         lookup[obj] = 1
    80                         lookup[obj] = 1
    74                 # Handle methods.
    81                 # Handle methods.
    75                 elif typ == MethodType:
    82                 elif typ == 'method':
    76                     if type(obj) == ClassType:
    83                     if obj2typ(obj) == 'class':
    77                         for m in dir(obj):
    84                         for m in dir(obj):
    78                             a = getattr(obj, m)
    85                             a = getattr(obj, m)
    79                             if type(a) == typ and a not in lookup:
    86                             if obj2typ(a) == typ and a not in lookup:
    80                                 if a.__self__.__class__.__module__.find(rootName) != 0:
    87                                 if a.__self__.__class__.__module__.find(rootName) != 0:
    81                                     continue
    88                                     continue
    82                                 cName = obj.__name__
    89                                 cName = obj.__name__
    83                                 objects.append((mName, a))
    90                                 objects.append((mName, a))
    84                                 lookup[a] = 1
    91                                 lookup[a] = 1
    95         "Write log file for different kind of documentable objects."
   102         "Write log file for different kind of documentable objects."
    96 
   103 
    97         cwd = os.getcwd()
   104         cwd = os.getcwd()
    98         from reportlab.lib.testutils import RL_HOME
   105         from reportlab.lib.testutils import RL_HOME
    99         objects = getModuleObjects(RL_HOME, 'reportlab', objType)
   106         objects = getModuleObjects(RL_HOME, 'reportlab', objType)
   100         objects.sort()
   107         if objType!='function':
       
   108             objects.sort()
   101         os.chdir(cwd)
   109         os.chdir(cwd)
   102 
   110 
   103         expl = {FunctionType:'functions',
   111         expl = {'function':'functions',
   104                 ClassType:'classes',
   112                 'class':'classes',
   105                 MethodType:'methods',
   113                 'method':'methods',
   106                 ModuleType:'modules'}[objType]
   114                 'module':'modules'}[objType]
   107 
   115 
   108         path = outputfile("test_docstrings-%s.log" % expl)
   116         path = outputfile("test_docstrings-%s.log" % expl)
   109         file = open(path, 'w')
   117         file = open(path, 'w')
   110         file.write('No doc strings found for the following %s below.\n\n' % expl)
   118         file.write('No doc strings found for the following %s below.\n\n' % expl)
   111         p = re.compile('__.+__')
   119         p = re.compile('__.+__')
   112 
   120 
   113         lines = []
   121         lines = []
   114         for name, obj in objects:
   122         for name, obj in objects:
   115             if objType == MethodType:
   123             if objType == 'method':
   116                 n = obj.__name__
   124                 n = obj.__name__
   117                 # Skip names with leading and trailing double underscores.
   125                 # Skip names with leading and trailing double underscores.
   118                 if p.match(n):
   126                 if p.match(n):
   119                     continue
   127                     continue
   120 
   128 
   121             if objType == FunctionType:
   129             if objType == 'function':
   122                 if not obj.__doc__ or len(obj.__doc__) == 0:
   130                 if not obj.__doc__ or len(obj.__doc__) == 0:
   123                     lines.append("%s.%s\n" % (name, obj.__name__))
   131                     lines.append("%s.%s\n" % (name, obj.__name__))
   124             else:
   132             else:
   125                 if not obj.__doc__ or len(obj.__doc__) == 0:
   133                 if not obj.__doc__ or len(obj.__doc__) == 0:
   126                     if objType == ClassType:
   134                     if objType == 'class':
   127                         lines.append("%s.%s\n" % (obj.__module__, obj.__name__))
   135                         lines.append("%s.%s\n" % (obj.__module__, obj.__name__))
   128                     elif objType == MethodType:
   136                     elif objType == 'method':
   129                         lines.append("%s.%s\n" % (obj.__self__.__class__, obj.__name__))
   137                         lines.append("%s.%s\n" % (obj.__self__.__class__, obj.__name__))
   130                     else:
   138                     else:
   131                         lines.append("%s\n" % (obj.__name__))
   139                         lines.append("%s\n" % (obj.__name__))
   132 
   140 
   133         lines.sort()
   141         lines.sort()
   136 
   144 
   137         file.close()
   145         file.close()
   138 
   146 
   139     def test0(self):
   147     def test0(self):
   140         "Test if functions have a doc string."
   148         "Test if functions have a doc string."
   141         self._writeLogFile(FunctionType)
   149         self._writeLogFile('function')
   142 
   150 
   143     def test1(self):
   151     def test1(self):
   144         "Test if classes have a doc string."
   152         "Test if classes have a doc string."
   145         self._writeLogFile(ClassType)
   153         self._writeLogFile('class')
   146 
   154 
   147     def test2(self):
   155     def test2(self):
   148         "Test if methods have a doc string."
   156         "Test if methods have a doc string."
   149         self._writeLogFile(MethodType)
   157         self._writeLogFile('method')
   150 
   158 
   151     def test3(self):
   159     def test3(self):
   152         "Test if modules have a doc string."
   160         "Test if modules have a doc string."
   153         self._writeLogFile(ModuleType)
   161         self._writeLogFile('module')
   154 
   162 
   155 def makeSuite():
   163 def makeSuite():
   156     suite = unittest.TestSuite()
   164     suite = unittest.TestSuite()
   157     loader = unittest.TestLoader()
   165     loader = unittest.TestLoader()
   158     if sys.platform[:4] != 'java': suite.addTest(loader.loadTestsFromTestCase(DocstringTestCase))
   166     if sys.platform[:4] != 'java': suite.addTest(loader.loadTestsFromTestCase(DocstringTestCase))