"Fossies" - the Fresh Open Source Software Archive

Member "reportlab-3.5.32/tests/test_pyfiles.py" (1 Oct 2019, 4805 Bytes) of package /linux/privat/reportlab-3.5.32.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file.

    1 #Copyright ReportLab Europe Ltd. 2000-2017
    2 #see license.txt for license details
    3 __version__='3.3.0'
    4 """Tests performed on all Python source files of the ReportLab distribution.
    5 """
    6 from reportlab.lib.testutils import setOutDir,makeSuiteForClasses, SecureTestCase, GlobDirectoryWalker, outputfile, printLocation
    7 setOutDir(__name__)
    8 import os, sys, string, fnmatch, re
    9 import unittest
   10 from reportlab.lib.utils import open_and_read, open_and_readlines
   11 
   12 # Helper function and class.
   13 def unique(seq):
   14     "Remove elements from a list that occur more than once."
   15 
   16     # Return input if it has less than 2 elements.
   17     if len(seq) < 2:
   18         return seq
   19 
   20     # Make a sorted copy of the input sequence.
   21     cnvt = isinstance(seq,str)
   22     seq2 = seq[:]
   23     if cnvt: seq2 = list(seq2)
   24     seq2.sort()
   25 
   26     # Remove adjacent elements if they are identical.
   27     i = 0
   28     while i < len(seq2)-1:
   29         elem = seq2[i]
   30         try:
   31             while elem == seq2[i+1]:
   32                 del seq2[i+1]
   33         except IndexError:
   34             pass
   35         i += 1
   36 
   37     # Try to return something of the same type as the input.
   38     if cnvt:
   39         return seq[0:0].join(seq2)
   40     else:
   41         return seq2
   42 
   43 class SelfTestCase(unittest.TestCase):
   44     "Test unique() function."
   45 
   46     def testUnique(self):
   47         "Test unique() function."
   48 
   49         cases = [([], []),
   50                  ([0], [0]),
   51                  ([0, 1, 2], [0, 1, 2]),
   52                  ([2, 1, 0], [0, 1, 2]),
   53                  ([0, 0, 1, 1, 2, 2, 3, 3], [0, 1, 2, 3]),
   54                  ('abcabcabc', 'abc')
   55                  ]
   56 
   57         msg = "Failed: unique(%s) returns %s instead of %s."
   58         for sequence, expectedOutput in cases:
   59             output = unique(sequence)
   60             args = (sequence, output, expectedOutput)
   61             assert output == expectedOutput, msg % args
   62 
   63 
   64 class AsciiFileTestCase(unittest.TestCase):
   65     "Test if Python files are pure ASCII ones."
   66 
   67     def testAscii(self):
   68         "Test if Python files are pure ASCII ones."
   69         from reportlab.lib.testutils import RL_HOME
   70         allPyFiles = GlobDirectoryWalker(RL_HOME, '*.py')
   71 
   72         for path in allPyFiles:
   73             fileContent = open_and_read(path,'r')
   74             nonAscii = [c for c in fileContent if ord(c)>127]
   75             nonAscii = unique(nonAscii)
   76 
   77             truncPath = path[path.find('reportlab'):]
   78             args = (truncPath, repr(list(map(ord, nonAscii))))
   79             msg = "File %s contains characters: %s." % args
   80 ##            if nonAscii:
   81 ##                print msg
   82             assert not nonAscii, msg
   83 
   84 
   85 class FilenameTestCase(unittest.TestCase):
   86     "Test if Python files contain trailing digits."
   87 
   88     def testTrailingDigits(self):
   89         "Test if Python files contain trailing digits."
   90 
   91         from reportlab.lib.testutils import RL_HOME
   92         allPyFiles = GlobDirectoryWalker(RL_HOME, '*.py')
   93 
   94         for path in allPyFiles:
   95             #hack - exclude barcode extensions from this test
   96             if path.find('barcode'):
   97                 pass
   98             else:
   99                 basename = os.path.splitext(path)[0]
  100                 truncPath = path[path.find('reportlab'):]
  101                 msg = "Filename %s contains trailing digits." % truncPath
  102                 assert basename[-1] not in string.digits, msg
  103 
  104     ##            if basename[-1] in string.digits:
  105     ##                print truncPath
  106 
  107 
  108 class FirstLineTestCase(SecureTestCase):
  109     "Testing if objects in the ReportLab package have docstrings."
  110 
  111     def findSuspiciousModules(self, folder, rootName):
  112         "Get all modul paths with non-Unix-like first line."
  113 
  114         firstLinePat = re.compile('^#!.*python.*')
  115 
  116         paths = []
  117         for file in GlobDirectoryWalker(folder, '*.py'):
  118             if os.path.basename(file) == '__init__.py':
  119                 continue
  120             firstLine = open_and_readlines(file)[0]
  121             if not firstLinePat.match(firstLine):
  122                 paths.append(file)
  123 
  124         return paths
  125 
  126     def test1(self):
  127         "Test if all Python files have a Unix-like first line."
  128 
  129         path = outputfile("test_firstline.log")
  130         file = open(path, 'w')
  131         file.write('No Unix-like first line found in the files below.\n\n')
  132 
  133         from reportlab.lib.testutils import RL_HOME
  134         paths = self.findSuspiciousModules(RL_HOME, 'reportlab')
  135         paths.sort()
  136 
  137         for p in paths:
  138             file.write("%s\n" % p)
  139 
  140         file.close()
  141 
  142 def makeSuite():
  143     suite = makeSuiteForClasses(SelfTestCase, AsciiFileTestCase, FilenameTestCase)
  144     if sys.platform[:4] != 'java':
  145         loader = unittest.TestLoader()
  146         suite.addTest(loader.loadTestsFromTestCase(FirstLineTestCase))
  147     return suite
  148 
  149 #noruntests
  150 if __name__ == "__main__":
  151     unittest.TextTestRunner().run(makeSuite())
  152     printLocation()