diff options
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/run.py | 13 | ||||
| -rw-r--r-- | tests/test_basic_api.py | 35 | ||||
| -rw-r--r-- | tests/test_cmdline.py | 11 | ||||
| -rw-r--r-- | tests/test_examplefiles.py | 6 | ||||
| -rw-r--r-- | tests/test_latex_formatter.py | 12 |
5 files changed, 51 insertions, 26 deletions
diff --git a/tests/run.py b/tests/run.py index 71bbe01f..0b5fd457 100644 --- a/tests/run.py +++ b/tests/run.py @@ -12,12 +12,21 @@ :license: BSD, see LICENSE for details. """ -import sys +import sys, os + +if sys.version_info >= (3,): + # copy test suite over to "build/lib" and convert it + print ('Copying and converting sources to build/lib/test...') + from distutils.util import copydir_run_2to3 + testroot = os.path.dirname(__file__) + newroot = os.path.join(testroot, '..', 'build/lib/test') + copydir_run_2to3(testroot, newroot) + os.chdir(os.path.join(newroot, '..')) try: import nose except ImportError: - print >> sys.stderr, "nose is required to run the test suites" + print ("nose is required to run the test suites") sys.exit(1) nose.main() diff --git a/tests/test_basic_api.py b/tests/test_basic_api.py index ab6658fd..e106b19c 100644 --- a/tests/test_basic_api.py +++ b/tests/test_basic_api.py @@ -9,13 +9,13 @@ import os import unittest -import StringIO import random from pygments import lexers, formatters, filters, format from pygments.token import _TokenType, Text from pygments.lexer import RegexLexer from pygments.formatters.img import FontNotFound +from pygments.util import BytesIO, StringIO, bytes, b import support @@ -144,7 +144,7 @@ class FormattersTest(unittest.TestCase): a = self.assert_ ae = self.assertEquals ts = list(lexers.PythonLexer().get_tokens("def f(): pass")) - out = StringIO.StringIO() + out = StringIO() # test that every formatter class has the correct public API for formatter, info in formatters.FORMATTERS.iteritems(): a(len(info) == 4) @@ -152,6 +152,10 @@ class FormattersTest(unittest.TestCase): a(info[1], "missing formatter aliases") # aliases a(info[3], "missing formatter docstring") # doc + if formatter.name == 'Raw tokens': + # will not work with Unicode output file + continue + try: inst = formatter(opt1="val1") except (ImportError, FontNotFound): @@ -189,22 +193,29 @@ class FormattersTest(unittest.TestCase): def test_unicode_handling(self): # test that the formatter supports encoding and Unicode - tokens = list(lexers.PythonLexer(encoding='utf-8').get_tokens("def f(): 'ä'")) + tokens = list(lexers.PythonLexer(encoding='utf-8'). + get_tokens("def f(): 'ä'")) for formatter, info in formatters.FORMATTERS.iteritems(): try: inst = formatter(encoding=None) except (ImportError, FontNotFound): # some dependency or font not installed continue - out = format(tokens, inst) - if formatter.unicodeoutput: - self.assert_(type(out) is unicode) - - inst = formatter(encoding='utf-8') - out = format(tokens, inst) - self.assert_(type(out) is str) - # Cannot test for encoding, since formatters may have to escape - # non-ASCII characters. + + if formatter.name != 'Raw tokens': + out = format(tokens, inst) + if formatter.unicodeoutput: + self.assert_(type(out) is unicode) + + inst = formatter(encoding='utf-8') + out = format(tokens, inst) + self.assert_(type(out) is bytes, '%s: %r' % (formatter, out)) + # Cannot test for encoding, since formatters may have to escape + # non-ASCII characters. + else: + inst = formatter() + out = format(tokens, inst) + self.assert_(type(out) is bytes, '%s: %r' % (formatter, out)) def test_get_formatters(self): a = self.assert_ diff --git a/tests/test_cmdline.py b/tests/test_cmdline.py index c64a213f..324a8c85 100644 --- a/tests/test_cmdline.py +++ b/tests/test_cmdline.py @@ -49,20 +49,23 @@ class CmdLineTest(unittest.TestCase): def test_O_opt(self): filename = TESTFILE - c, o, e = run_cmdline("-Ofull=1,linenos=true,foo=bar", "-fhtml", filename) + 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 = TESTFILE - c, o, e = run_cmdline("-Pfull", "-Ptitle=foo, bar=baz=,", "-fhtml", filename) + 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 = TESTFILE - c, o, e = run_cmdline("-Fhighlight:tokentype=Name.Blubb,names=TESTFILE filename", + 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) @@ -87,7 +90,7 @@ class CmdLineTest(unittest.TestCase): from pygments.lexers import PythonLexer from pygments.formatters import HtmlFormatter filename = TESTFILE - code = open(filename).read() + code = open(filename, 'rb').read() output = highlight(code, PythonLexer(), HtmlFormatter()) diff --git a/tests/test_examplefiles.py b/tests/test_examplefiles.py index c6867b9e..d64756ff 100644 --- a/tests/test_examplefiles.py +++ b/tests/test_examplefiles.py @@ -13,7 +13,7 @@ import unittest from pygments import highlight from pygments.lexers import get_lexer_for_filename, get_lexer_by_name from pygments.token import Error -from pygments.util import ClassNotFound +from pygments.util import ClassNotFound, b # generate methods @@ -40,8 +40,8 @@ def test_example_files(): yield check_lexer, lx, absfn def check_lexer(lx, absfn): - text = open(absfn, 'U').read() - text = text.strip('\n') + '\n' + text = open(absfn, 'rb').read() + text = text.strip(b('\n')) + b('\n') try: text = text.decode('utf-8') except UnicodeError: diff --git a/tests/test_latex_formatter.py b/tests/test_latex_formatter.py index 93d4578d..54c43811 100644 --- a/tests/test_latex_formatter.py +++ b/tests/test_latex_formatter.py @@ -22,24 +22,26 @@ TESTFILE, TESTDIR = support.location(__file__) class LatexFormatterTest(unittest.TestCase): def test_valid_output(self): - tokensource = list(PythonLexer().get_tokens(file(TESTFILE).read())) - fmt = LatexFormatter(full=True) + tokensource = list(PythonLexer().get_tokens(open(TESTFILE).read())) + fmt = LatexFormatter(full=True, encoding='latin1') handle, pathname = tempfile.mkstemp('.tex') # place all output files in /tmp too old_wd = os.getcwd() os.chdir(os.path.dirname(pathname)) - tfile = os.fdopen(handle, 'w+b') + tfile = os.fdopen(handle, 'wb') fmt.format(tokensource, tfile) tfile.close() try: try: import subprocess - ret = subprocess.Popen(['latex', '-interaction=nonstopmode', pathname], + ret = subprocess.Popen(['latex', '-interaction=nonstopmode', + pathname], stdout=subprocess.PIPE).wait() except ImportError: # Python 2.3 - no subprocess module - ret = os.popen('latex -interaction=nonstopmode "%s"' % pathname).close() + ret = os.popen('latex -interaction=nonstopmode "%s"' + % pathname).close() if ret == 32512: raise OSError # not found except OSError: # latex not available |
