summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--pygments/cmdline.py4
-rwxr-xr-xpygments/formatters/_mapping.py4
-rw-r--r--pygments/lexers/_luabuiltins.py4
-rw-r--r--pygments/lexers/_mapping.py4
-rw-r--r--pygments/lexers/_phpbuiltins.py4
-rw-r--r--pygments/unistring.py4
-rw-r--r--tests/old_run.py138
-rw-r--r--tests/run.py130
-rw-r--r--tests/support.py15
-rw-r--r--tests/test_basic_api.py10
-rw-r--r--tests/test_cmdline.py14
-rw-r--r--tests/test_examplefiles.py53
-rw-r--r--tests/test_html_formatter.py19
-rw-r--r--tests/test_latex_formatter.py7
14 files changed, 226 insertions, 184 deletions
diff --git a/pygments/cmdline.py b/pygments/cmdline.py
index 4d485b6e..bbbbce25 100644
--- a/pygments/cmdline.py
+++ b/pygments/cmdline.py
@@ -315,7 +315,7 @@ def main(args=sys.argv):
print >>sys.stderr, 'Error:', err
return 1
try:
- outfile = file(outfn, 'wb')
+ outfile = open(outfn, 'wb')
except Exception, err:
print >>sys.stderr, 'Error: cannot open outfile:', err
return 1
@@ -340,7 +340,7 @@ def main(args=sys.argv):
infn = args[0]
try:
- code = file(infn).read()
+ code = open(infn).read()
except Exception, err:
print >>sys.stderr, 'Error: cannot read infile:', err
return 1
diff --git a/pygments/formatters/_mapping.py b/pygments/formatters/_mapping.py
index 4ad65f76..3060f95d 100755
--- a/pygments/formatters/_mapping.py
+++ b/pygments/formatters/_mapping.py
@@ -73,7 +73,7 @@ if __name__ == '__main__':
imports.sort()
# extract useful sourcecode from this file
- f = file(__file__)
+ f = open(__file__)
try:
content = f.read()
finally:
@@ -82,7 +82,7 @@ if __name__ == '__main__':
footer = content[content.find("if __name__ == '__main__':"):]
# write new file
- f = file(__file__, 'w')
+ f = open(__file__, 'w')
f.write(header)
f.write('# start\n')
f.write('\n'.join(['from %s import %s' % imp for imp in imports]))
diff --git a/pygments/lexers/_luabuiltins.py b/pygments/lexers/_luabuiltins.py
index 711c9195..3a993653 100644
--- a/pygments/lexers/_luabuiltins.py
+++ b/pygments/lexers/_luabuiltins.py
@@ -222,7 +222,7 @@ if __name__ == '__main__':
return 'basic'
def regenerate(filename, modules):
- f = file(filename)
+ f = open(filename)
try:
content = f.read()
finally:
@@ -232,7 +232,7 @@ if __name__ == '__main__':
footer = content[content.find("if __name__ == '__main__':"):]
- f = file(filename, 'w')
+ f = open(filename, 'w')
f.write(header)
f.write('MODULES = %s\n\n' % pprint.pformat(modules))
f.write(footer)
diff --git a/pygments/lexers/_mapping.py b/pygments/lexers/_mapping.py
index ba255dae..ba107b26 100644
--- a/pygments/lexers/_mapping.py
+++ b/pygments/lexers/_mapping.py
@@ -168,7 +168,7 @@ if __name__ == '__main__':
found_lexers.sort()
# extract useful sourcecode from this file
- f = file(__file__)
+ f = open(__file__)
try:
content = f.read()
finally:
@@ -177,7 +177,7 @@ if __name__ == '__main__':
footer = content[content.find("if __name__ == '__main__':"):]
# write new file
- f = file(__file__, 'w')
+ f = open(__file__, 'w')
f.write(header)
f.write('LEXERS = {\n %s\n}\n\n' % ',\n '.join(found_lexers))
f.write(footer)
diff --git a/pygments/lexers/_phpbuiltins.py b/pygments/lexers/_phpbuiltins.py
index 5173b20d..5f8fa228 100644
--- a/pygments/lexers/_phpbuiltins.py
+++ b/pygments/lexers/_phpbuiltins.py
@@ -3373,7 +3373,7 @@ if __name__ == '__main__':
idx += 1
# extract useful sourcecode from this file
- f = file(__file__)
+ f = open(__file__)
try:
content = f.read()
finally:
@@ -3382,7 +3382,7 @@ if __name__ == '__main__':
footer = content[content.find("if __name__ == '__main__':"):]
# write new file
- f = file(__file__, 'w')
+ f = open(__file__, 'w')
f.write(header)
f.write('MODULES = %s\n\n' % pprint.pformat(modules))
f.write(footer)
diff --git a/pygments/unistring.py b/pygments/unistring.py
index 382d7f76..d324d844 100644
--- a/pygments/unistring.py
+++ b/pygments/unistring.py
@@ -95,7 +95,7 @@ if __name__ == '__main__':
categories = {}
- f = file(__file__)
+ f = open(__file__)
try:
content = f.read()
finally:
@@ -109,7 +109,7 @@ if __name__ == '__main__':
cat = unicodedata.category(c)
categories.setdefault(cat, []).append(c)
- f = file(__file__, 'w')
+ f = open(__file__, 'w')
f.write(header)
for cat in sorted(categories):
diff --git a/tests/old_run.py b/tests/old_run.py
new file mode 100644
index 00000000..97873218
--- /dev/null
+++ b/tests/old_run.py
@@ -0,0 +1,138 @@
+# -*- 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
+
+from os.path import dirname, basename, join, abspath
+
+import pygments
+
+try:
+ import coverage
+except ImportError:
+ coverage = None
+
+testdir = abspath(dirname(__file__))
+
+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(with_coverage=False):
+ # needed to avoid confusion involving atexit handlers
+ import logging
+
+ 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 = 85
+
+ print >>sys.stderr, \
+ ('Pygments %s Test Suite running%s, stand by...' %
+ (pygments.__version__,
+ with_coverage and " with coverage analysis" or "")).center(WIDTH)
+ print >>sys.stderr, ('(using Python %s)' % sys.version.split()[0]).center(WIDTH)
+ print >>sys.stderr, '='*WIDTH
+
+ if with_coverage:
+ coverage.erase()
+ coverage.start()
+
+ for testfile in files:
+ globs = {'__file__': join(testdir, testfile)}
+ try:
+ 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)))
+ tests.sort()
+ 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)
+ ret = 1
+ else:
+ if total_test_count == 1:
+ print >>sys.stderr, '1 test happy.'
+ else:
+ print >>sys.stderr, 'All %d tests happy.' % total_test_count
+ ret = 0
+
+ if with_coverage:
+ coverage.stop()
+ modules = [mod for name, mod in sys.modules.iteritems()
+ if name.startswith('pygments.') and mod]
+ coverage.report(modules)
+
+ return ret
+
+
+if __name__ == '__main__':
+ with_coverage = False
+ if sys.argv[1:2] == ['-C']:
+ with_coverage = bool(coverage)
+ del sys.argv[1]
+ sys.exit(run_tests(with_coverage))
diff --git a/tests/run.py b/tests/run.py
index b0244d60..eee7d4d6 100644
--- a/tests/run.py
+++ b/tests/run.py
@@ -12,132 +12,12 @@
:license: GNU GPL, see LICENSE for more details.
"""
-import sys, os, new
-import unittest
-import __builtin__
-
-from os.path import dirname, basename, join, abspath
-
-import pygments
+import sys
try:
- import coverage
+ import nose
except ImportError:
- coverage = None
-
-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(with_coverage=False):
- # needed to avoid confusion involving atexit handlers
- import logging
-
- 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 = 85
-
- print >>sys.stderr, \
- ('Pygments %s Test Suite running%s, stand by...' %
- (pygments.__version__,
- with_coverage and " with coverage analysis" or "")).center(WIDTH)
- print >>sys.stderr, ('(using Python %s)' % sys.version.split()[0]).center(WIDTH)
- print >>sys.stderr, '='*WIDTH
-
- if with_coverage:
- coverage.erase()
- coverage.start()
-
- 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)))
- tests.sort()
- 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)
- ret = 1
- else:
- if total_test_count == 1:
- print >>sys.stderr, '1 test happy.'
- else:
- print >>sys.stderr, 'All %d tests happy.' % total_test_count
- ret = 0
-
- if with_coverage:
- coverage.stop()
- modules = [mod for name, mod in sys.modules.iteritems()
- if name.startswith('pygments.') and mod]
- coverage.report(modules)
-
- return ret
-
+ print >> sys.stderr, "nose is required to run the test suites"
+ sys.exit(1)
-if __name__ == '__main__':
- with_coverage = False
- if sys.argv[1:2] == ['-C']:
- with_coverage = bool(coverage)
- del sys.argv[1]
- sys.exit(run_tests(with_coverage))
+nose.main()
diff --git a/tests/support.py b/tests/support.py
new file mode 100644
index 00000000..505c17da
--- /dev/null
+++ b/tests/support.py
@@ -0,0 +1,15 @@
+# coding: utf-8
+"""
+Support for Pygments tests
+"""
+
+import os
+
+
+def location(mod_name):
+ """
+ Return the file and directory that the code for *mod_name* is in.
+ """
+ source = mod_name.endswith("pyc") and mod_name[:-1] or mod_name
+ source = os.path.abspath(source)
+ return source, os.path.dirname(source)
diff --git a/tests/test_basic_api.py b/tests/test_basic_api.py
index 760dda63..1168c7ad 100644
--- a/tests/test_basic_api.py
+++ b/tests/test_basic_api.py
@@ -17,6 +17,10 @@ from pygments.token import _TokenType, Text
from pygments.lexer import RegexLexer
from pygments.formatters.img import FontNotFound
+import support
+
+TESTFILE, TESTDIR = support.location(__file__)
+
test_content = [chr(i) for i in xrange(33, 128)] * 5
random.shuffle(test_content)
test_content = ''.join(test_content) + '\n'
@@ -90,7 +94,7 @@ class FiltersTest(unittest.TestCase):
for x in filters.FILTERS.keys():
lx = lexers.PythonLexer()
lx.add_filter(x, **filter_args.get(x, {}))
- text = file(os.path.join(testdir, testfile)).read().decode('utf-8')
+ text = file(TESTFILE).read().decode('utf-8')
tokens = list(lx.get_tokens(text))
roundtext = ''.join([t[1] for t in tokens])
if x not in ('whitespace', 'keywordcase'):
@@ -106,14 +110,14 @@ class FiltersTest(unittest.TestCase):
def test_whitespace(self):
lx = lexers.PythonLexer()
lx.add_filter('whitespace', spaces='%')
- text = file(os.path.join(testdir, testfile)).read().decode('utf-8')
+ text = file(TESTFILE).read().decode('utf-8')
lxtext = ''.join([t[1] for t in list(lx.get_tokens(text))])
self.failIf(' ' in lxtext)
def test_keywordcase(self):
lx = lexers.PythonLexer()
lx.add_filter('keywordcase', case='capitalize')
- text = file(os.path.join(testdir, testfile)).read().decode('utf-8')
+ text = file(TESTFILE).read().decode('utf-8')
lxtext = ''.join([t[1] for t in list(lx.get_tokens(text))])
self.assert_('Def' in lxtext and 'Class' in lxtext)
diff --git a/tests/test_cmdline.py b/tests/test_cmdline.py
index 46868cf1..eaf1cbfc 100644
--- a/tests/test_cmdline.py
+++ b/tests/test_cmdline.py
@@ -16,6 +16,10 @@ import StringIO
from pygments import highlight
from pygments.cmdline import main as cmdline_main
+import support
+
+TESTFILE, TESTDIR = support.location(__file__)
+
def run_cmdline(*args):
saved_stdout = sys.stdout
@@ -44,21 +48,21 @@ class CmdLineTest(unittest.TestCase):
self.assertEquals(c, 0)
def test_O_opt(self):
- filename = os.path.join(testdir, testfile)
+ filename = TESTFILE
c, o, e = run_cmdline("-Ofull=1,linenos=true,foo=bar", "-fhtml", filename)
self.assertEquals(c, 0)
self.assert_("<html" in o)
self.assert_('class="linenos"' in o)
def test_P_opt(self):
- filename = os.path.join(testdir, testfile)
+ filename = TESTFILE
c, o, e = run_cmdline("-Pfull", "-Ptitle=foo, bar=baz=,", "-fhtml", filename)
self.assertEquals(c, 0)
self.assert_("<title>foo, bar=baz=,</title>" in o)
def test_F_opt(self):
- filename = os.path.join(testdir, testfile)
- c, o, e = run_cmdline("-Fhighlight:tokentype=Name.Blubb,names=testfile testdir",
+ filename = TESTFILE
+ c, o, e = run_cmdline("-Fhighlight:tokentype=Name.Blubb,names=TESTFILE filename",
"-fhtml", filename)
self.assertEquals(c, 0)
self.assert_('<span class="n-Blubb' in o)
@@ -82,7 +86,7 @@ class CmdLineTest(unittest.TestCase):
# test that cmdline gives the same output as library api
from pygments.lexers import PythonLexer
from pygments.formatters import HtmlFormatter
- filename = os.path.join(testdir, testfile)
+ filename = TESTFILE
code = file(filename).read()
output = highlight(code, PythonLexer(), HtmlFormatter())
diff --git a/tests/test_examplefiles.py b/tests/test_examplefiles.py
index ee9af608..2ebb2101 100644
--- a/tests/test_examplefiles.py
+++ b/tests/test_examplefiles.py
@@ -16,36 +16,31 @@ from pygments.token import Error
from pygments.util import ClassNotFound
-class ExampleFileTest(unittest.TestCase):
- pass
-
-lfd = 0
-
# generate methods
-for fn in os.listdir(os.path.join(testdir, 'examplefiles')):
- absfn = os.path.join(testdir, 'examplefiles', fn)
- if not os.path.isfile(absfn):
- continue
+def test_example_files():
+ testdir = os.path.dirname(__file__)
+ for fn in os.listdir(os.path.join(testdir, 'examplefiles')):
+ absfn = os.path.join(testdir, 'examplefiles', fn)
+ if not os.path.isfile(absfn):
+ continue
- try:
- lx = get_lexer_for_filename(absfn)
- except ClassNotFound:
try:
- name, rest = fn.split("_", 1)
- lx = get_lexer_by_name(name)
+ lx = get_lexer_for_filename(absfn)
except ClassNotFound:
- raise AssertionError('no lexer found for file %r' % fn)
-
- def test(self, lx=lx, absfn=absfn):
- text = file(absfn, 'U').read()
- text = text.strip('\n') + '\n'
- text = text.decode('latin1')
- ntext = []
- for type, val in lx.get_tokens(text):
- ntext.append(val)
- self.failIf(type == Error, 'lexer generated error token for '+absfn)
- if u''.join(ntext) != text:
- self.fail('round trip failed for '+absfn)
-
- setattr(ExampleFileTest, 'test_%i' % lfd, test)
- lfd += 1
+ try:
+ name, rest = fn.split("_", 1)
+ lx = get_lexer_by_name(name)
+ except ClassNotFound:
+ raise AssertionError('no lexer found for file %r' % fn)
+ yield check_lexer, lx, absfn
+
+def check_lexer(lx, absfn):
+ text = file(absfn, 'U').read()
+ text = text.strip('\n') + '\n'
+ text = text.decode('latin1')
+ ntext = []
+ for type, val in lx.get_tokens(text):
+ ntext.append(val)
+ assert type != Error, 'lexer generated error token for ' + absfn
+ if u''.join(ntext) != text:
+ raise AssertionError('round trip failed for ' + absfn)
diff --git a/tests/test_html_formatter.py b/tests/test_html_formatter.py
index ed949a88..d4ac1bf2 100644
--- a/tests/test_html_formatter.py
+++ b/tests/test_html_formatter.py
@@ -12,14 +12,17 @@ import re
import unittest
import StringIO
import tempfile
-from os.path import join, dirname, isfile
+from os.path import join, dirname, isfile, abspath
from pygments.lexers import PythonLexer
from pygments.formatters import HtmlFormatter, NullFormatter
from pygments.formatters.html import escape_html
-tokensource = list(PythonLexer().get_tokens(file(
- os.path.join(testdir, testfile)).read()))
+import support
+
+TESTFILE, TESTDIR = support.location(__file__)
+
+tokensource = list(PythonLexer().get_tokens(file(TESTFILE).read()))
class HtmlFormatterTest(unittest.TestCase):
def test_correct_output(self):
@@ -39,13 +42,13 @@ class HtmlFormatterTest(unittest.TestCase):
# test correct behavior
# CSS should be in /tmp directory
fmt1 = HtmlFormatter(full=True, cssfile='fmt1.css')
- # CSS should be in testdir (testdir is absolute)
- fmt2 = HtmlFormatter(full=True, cssfile=join(testdir, 'fmt2.css'))
+ # CSS should be in TESTDIR (TESTDIR is absolute)
+ fmt2 = HtmlFormatter(full=True, cssfile=join(TESTDIR, 'fmt2.css'))
tfile = tempfile.NamedTemporaryFile(suffix='.html')
fmt1.format(tokensource, tfile)
try:
fmt2.format(tokensource, tfile)
- self.assert_(isfile(join(testdir, 'fmt2.css')))
+ self.assert_(isfile(join(TESTDIR, 'fmt2.css')))
except IOError:
# test directory not writable
pass
@@ -54,7 +57,7 @@ class HtmlFormatterTest(unittest.TestCase):
self.assert_(isfile(join(dirname(tfile.name), 'fmt1.css')))
os.unlink(join(dirname(tfile.name), 'fmt1.css'))
try:
- os.unlink(join(testdir, 'fmt2.css'))
+ os.unlink(join(TESTDIR, 'fmt2.css'))
except OSError:
pass
@@ -76,7 +79,7 @@ class HtmlFormatterTest(unittest.TestCase):
tfile = os.fdopen(handle, 'w+b')
fmt.format(tokensource, tfile)
tfile.close()
- catname = os.path.join(testdir, 'dtds', 'HTML4.soc')
+ catname = os.path.join(TESTDIR, 'dtds', 'HTML4.soc')
try:
try:
import subprocess
diff --git a/tests/test_latex_formatter.py b/tests/test_latex_formatter.py
index 45f48638..033f5a55 100644
--- a/tests/test_latex_formatter.py
+++ b/tests/test_latex_formatter.py
@@ -14,12 +14,15 @@ import tempfile
from pygments.formatters import LatexFormatter
from pygments.lexers import PythonLexer
+import support
+
+TESTFILE, TESTDIR = support.location(__file__)
+
class LatexFormatterTest(unittest.TestCase):
def test_valid_output(self):
- tokensource = list(PythonLexer().get_tokens(file(
- os.path.join(testdir, testfile)).read()))
+ tokensource = list(PythonLexer().get_tokens(file(TESTFILE).read()))
fmt = LatexFormatter(full=True)
handle, pathname = tempfile.mkstemp('.tex')