pygments-main / tests /

# -*- coding: utf-8 -*-
    Pygments unit tests


        python [testfile ...]

    :copyright: 2006 by Georg Brandl.
    :license: GNU GPL, see LICENSE for more details.

import sys, os, new
import unittest
import __builtin__

from os.path import dirname, basename, join, abspath

testdir = abspath(dirname(__file__))

# useful for all tests
__builtin__.testdir = testdir

failed = []
total_test_count = 0
error_test_count = 0

def err(file, what, exc):
    print >>sys.stderr, file, 'failed %s:' % what,
    print >>sys.stderr, exc

class QuietTestRunner(object):
    """Customized test runner for relatively quiet output"""

    def __init__(self, testname, stream=sys.stderr):
        self.testname = testname = unittest._WritelnDecorator(stream)

    def run(self, test):
        global total_test_count
        global error_test_count
        result = unittest._TextTestResult(, True, 1)
        if not result.wasSuccessful():
  ' FAIL:')
  ' ok\n')
        total_test_count += result.testsRun
        error_test_count += len(result.errors) + len(result.failures)
        return result

def run_tests():
    # needed to avoid confusion involving atexit handlers
    import logging

    orig_modules = sys.modules.keys()

    if sys.argv[1:]:
        # test only files given on cmdline
        files = [entry + '.py' for entry in sys.argv[1:] if entry.startswith('test_')]
        files = [entry for entry in os.listdir(testdir)
                 if (entry.startswith('test_') and entry.endswith('.py'))]

    print >>sys.stderr, '    Pygments Test Suite running, stand by...   '
    print >>sys.stderr, '             (using Python %s)' % sys.version.split()[0]
    print >>sys.stderr, '==============================================='

    for testfile in files:
        globs = {}
            __builtin__.testfile = testfile
            execfile(join(testdir, testfile), globs)
        except Exception, exc:
            err(testfile, 'execfile', exc)
        sys.stderr.write(testfile[:-3] + ': ')
            runner = QuietTestRunner(testfile[:-3])
            # make a test suite of all TestCases in the file
            tests = []
            for name, thing in globs.iteritems():
                if name.endswith('Test'):
                    tests.append((name, unittest.makeSuite(thing)))
            # PY24: use key keyword arg
            tests.sort(lambda x, y: cmp(x[0], y[0]))
            suite = unittest.TestSuite()
            suite.addTests([x[1] for x in tests])
        except Exception, exc:
            err(testfile, 'running test', exc)

    print >>sys.stderr, '==============================================='
    if failed:
        print >>sys.stderr, '%d of %d tests failed.' % \
              (error_test_count, total_test_count)
        print >>sys.stderr, 'Tests failed in:', ', '.join(failed)
        return 1
        if total_test_count == 1:
            print >>sys.stderr, '1 test happy.'
            print >>sys.stderr, 'All %d tests happy.' % total_test_count
        return 0

if __name__ == '__main__':
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
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.