shithub: jbig2

Download patch

ref: 4cec0516180e0a8a79e88de5fbb12a28b3f64439
parent: 94071092c25507e1dd00451f89c0e82d45f65503
author: giles <giles@ded80894-8fb9-0310-811b-c03f3676ab4d>
date: Mon Feb 5 17:59:10 EST 2007

Rewrite the test script to use a custom test class. 

Previously we used the python unittest module, but we don't use most of 
its features, and it's quite weak for this kind of file-based external 
testing. So we lose on using a familiar api, but this doesn't add much 
code, and we can now add long-desired features like 'xfail' results.

This grew out of work revising the Ghostscript test code for 
parallelization.


git-svn-id: http://svn.ghostscript.com/jbig2dec/trunk@452 ded80894-8fb9-0310-811b-c03f3676ab4d

--- a/test_jbig2dec.py
+++ b/test_jbig2dec.py
@@ -4,10 +4,59 @@
 # $Id$
 
 import os, re
-import unittest
+import sys, time
 
-class KnownFileHash(unittest.TestCase):
+class SelfTest:
+  'generic class for self tests'
+  def __init__(self):
+    self.result = 'unrun'
+    self.msg = ''
+  def shortDescription(self):
+    'returns a short name for the test'
+    return "generic self test"
+  def runTest(self):
+    'call this to execute the test'
+    pass
+  def fail(self, msg=None):
+    self.result = 'FAIL'
+    self.msg = msg
+  def failIf(self, check, msg=None):
+    if check: self.fail(msg)
+  def assertEqual(self, a, b, msg=None):
+    if a != b: self.fail(msg)
 
+class SelfTestSuite:
+  'generic class for running a collection of SelfTest instances'
+  def __init__(self, stream=sys.stderr):
+    self.stream = stream
+    self.tests = []
+    self.fails = []
+    self.xfails = []
+    self.errors = []
+  def addTest(self, test):
+    self.tests.append(test)
+  def run(self):
+    starttime = time.time()
+    for test in self.tests:
+      self.stream.write("%s ... " % test.shortDescription())
+      test.result = 'ok'
+      test.runTest()
+      if test.result != 'ok':
+        self.fails.append(test)
+      self.stream.write("%s\n" % test.result)
+    stoptime = time.time()
+    self.stream.write('-'*72 + '\n')
+    self.stream.write('ran %d tests in %.3f seconds\n\n' % 
+	(len(self.tests), stoptime - starttime))
+    if len(self.fails):
+      self.stream.write('FAILED %d of %d tests\n' % 
+	(len(self.fails),len(self.tests)))
+    else:
+      self.stream.write('PASSED all %d tests\n' % len(self.tests))
+
+class KnownFileHash(SelfTest):
+  'self test to check for correct decode of known test files'
+
   # hashes of known test inputs
   known_042_DECODED = "ebfdf6e2fc5ff3ee2271c2fa19de0e52712046e8"
   # we do not have correct hashes for these
@@ -101,10 +150,10 @@
                         "ff373f070f5f405b732c53ffffff087eff22ff5b") )
 
   def __init__(self, file, file_hash, decode_hash):
+    SelfTest.__init__(self)
     self.file = file
     self.file_hash = file_hash
     self.decode_hash = decode_hash
-    unittest.TestCase.__init__(self)
 
   def shortDescription(self):
     return "Checking '%s' for correct decoded document hash" % self.file
@@ -126,7 +175,7 @@
         return
     self.fail('document hash was not found in the output')
 
-suite = unittest.TestSuite()
+suite = SelfTestSuite()
 for filename, file_hash, decode_hash in KnownFileHash.known_hashes:
   # only add tests for files we can find
   if not os.access(filename, os.R_OK): continue
@@ -135,8 +184,5 @@
 
 # run the defined tests if we're called as a script
 if __name__ == "__main__":
-    import sys
-    verbosity = 2
-    runner = unittest.TextTestRunner(verbosity=verbosity)
-    result = runner.run(suite)
-    sys.exit(not result.wasSuccessful())
+    result = suite.run()
+    sys.exit(not result)