Georg Brandl avatar Georg Brandl committed 5109baf

Allow storing and comparing output between test runs.

Comments (0)

Files changed (2)

 dist/*
 Pygments.egg-info/*
 .ropeproject
+tests/examplefiles/output

tests/test_examplefiles.py

 """
 
 import os
+import pprint
+import difflib
+import cPickle as pickle
 
 from pygments.lexers import get_lexer_for_filename, get_lexer_by_name
 from pygments.token import Error
 from pygments.util import ClassNotFound, b
 
+STORE_OUTPUT = False
 
 # generate methods
 def test_example_files():
     testdir = os.path.dirname(__file__)
+    outdir = os.path.join(testdir, 'examplefiles', 'output')
+    if STORE_OUTPUT and not os.path.isdir(outdir):
+        os.makedirs(outdir)
     for fn in os.listdir(os.path.join(testdir, 'examplefiles')):
         absfn = os.path.join(testdir, 'examplefiles', fn)
         if not os.path.isfile(absfn):
             continue
+        outfn = os.path.join(outdir, fn)
 
         try:
             lx = get_lexer_for_filename(absfn)
                 lx = get_lexer_by_name(name)
             except ClassNotFound:
                 raise AssertionError('no lexer found for file %r' % fn)
-        yield check_lexer, lx, absfn
+        yield check_lexer, lx, absfn, outfn
 
-def check_lexer(lx, absfn):
+def check_lexer(lx, absfn, outfn):
     text = open(absfn, 'rb').read()
     text = text.replace(b('\r\n'), b('\n'))
     text = text.strip(b('\n')) + b('\n')
     except UnicodeError:
         text = text.decode('latin1')
     ntext = []
+    tokens = []
     for type, val in lx.get_tokens(text):
         ntext.append(val)
         assert type != Error, 'lexer %s generated error token for %s' % \
                 (lx, absfn)
+        tokens.append((type, val))
     if u''.join(ntext) != text:
         raise AssertionError('round trip failed for ' + absfn)
+
+    # check output against previous run if enabled
+    if STORE_OUTPUT:
+        # no previous output -- store it
+        if not os.path.isfile(outfn):
+            fp = open(outfn, 'wb')
+            try:
+                pickle.dump(tokens, fp)
+            finally:
+                fp.close()
+            return
+        # otherwise load it and compare
+        fp = open(outfn, 'rb')
+        try:
+            stored_tokens = pickle.load(fp)
+        finally:
+            fp.close()
+        if stored_tokens != tokens:
+            f1 = pprint.pformat(stored_tokens)
+            f2 = pprint.pformat(tokens)
+            print '\n'.join(difflib.unified_diff(f1.splitlines(),
+                                                 f2.splitlines()))
+            assert False, absfn
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.