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

    Usage::

        python run.py [testfile ...]


    :copyright: 2006-2007 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
    failed.append(file[:-3])


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

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

    def run(self, test):
        global total_test_count
        global error_test_count
        result = unittest._TextTestResult(self.stream, True, 1)
        test(result)
        if not result.wasSuccessful():
            self.stream.write(' FAIL:')
            result.printErrors()
            failed.append(self.testname)
        else:
            self.stream.write(' 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_')]
    else:
        files = [entry for entry in os.listdir(testdir)
                 if (entry.startswith('test_') and entry.endswith('.py'))]
        files.sort()

    WIDTH = 70

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

    for testfile in files:
        globs = {}
        try:
            __builtin__.testfile = testfile
            execfile(join(testdir, testfile), globs)
        except Exception, exc:
            raise
            err(testfile, 'execfile', exc)
            continue
        sys.stderr.write(testfile[:-3] + ': ')
        try:
            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])
            runner.run(suite)
        except Exception, exc:
            err(testfile, 'running test', exc)

    print >>sys.stderr, '='*WIDTH
    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
    else:
        if total_test_count == 1:
            print >>sys.stderr, '1 test happy.'
        else:
            print >>sys.stderr, 'All %d tests happy.' % total_test_count
        return 0


if __name__ == '__main__':
    sys.exit(run_tests())

@@ -332,16 +332,82 @@
 bspath%0A%0A
+try:%0A    import coverage%0Aexcept ImportError:%0A    coverage = None%0A%0A
 testdir 
@@ -1451,16 +1451,35 @@
 n_tests(
+with_coverage=False
 ):%0A    #
@@ -1555,16 +1555,17 @@
 ng%0A%0A    
+#
 orig_mod
@@ -1945,16 +1945,17 @@
 stderr, 
+(
 'Pygment
@@ -1974,16 +1974,19 @@
  running
+ %25s
 , stand 
@@ -1991,16 +1991,95 @@
 d by...'
+ %25%0A                         (with_coverage and %22with coverage analysis%22 or %22%22))
 .center(
@@ -2205,16 +2205,89 @@
 *WIDTH%0A%0A
+    if with_coverage:%0A        coverage.erase()%0A        coverage.start()%0A%0A
     for 
@@ -3430,19 +3430,18 @@
      ret
-urn
+ =
  1%0A    e
@@ -3629,42 +3629,382 @@
  ret
-urn 0%0A%0A%0Aif __name__ == '__main__':
+ = 0%0A%0A    if with_coverage:%0A        coverage.stop()%0A        modules = %5Bmod for name, mod in sys.modules.iteritems()%0A                   if name.startswith('pygments.') and mod%5D%0A        coverage.report(modules)%0A%0A    return ret%0A%0A%0Aif __name__ == '__main__':%0A    with_coverage = False%0A    if sys.argv%5B1:2%5D == %5B'-C'%5D:%0A        with_coverage = bool(coverage)%0A        del sys.argv%5B1%5D
 %0A   
@@ -4023,11 +4023,24 @@
 n_tests(
+with_coverage
 ))%0A

Add support for code coverage analysis.