diff options
author | Georg Brandl <georg@python.org> | 2019-05-06 08:17:53 +0200 |
---|---|---|
committer | Georg Brandl <georg@python.org> | 2019-11-10 10:15:13 +0100 |
commit | a41f232a0aa9f2b435e54a7e247c858a8f2d7fa8 (patch) | |
tree | 8245310d57e48e3e966a46f40e120d6ea3ce28f7 /tests/test_basic_api.py | |
parent | 7b418bd18e01b06ccaa1c7bd03d11abf303f2fbd (diff) | |
download | pygments-git-a41f232a0aa9f2b435e54a7e247c858a8f2d7fa8.tar.gz |
Initial port to py.test
Unittest classes are kept - for now - since py.test is ok with them.
Generator tests had to be switched to pytest parametrized tests.
Diffstat (limited to 'tests/test_basic_api.py')
-rw-r--r-- | tests/test_basic_api.py | 252 |
1 files changed, 120 insertions, 132 deletions
diff --git a/tests/test_basic_api.py b/tests/test_basic_api.py index b1b69267..07b2f09c 100644 --- a/tests/test_basic_api.py +++ b/tests/test_basic_api.py @@ -11,6 +11,9 @@ from __future__ import print_function import random import unittest +from os import path + +import pytest from pygments import lexers, formatters, lex, format from pygments.token import _TokenType, Text @@ -18,100 +21,92 @@ from pygments.lexer import RegexLexer from pygments.formatters.img import FontNotFound from pygments.util import text_type, StringIO, BytesIO, xrange, ClassNotFound -import support - -TESTFILE, TESTDIR = support.location(__file__) +TESTDIR = path.dirname(path.abspath(__file__)) +TESTFILE = path.join(TESTDIR, 'test_basic_api.py') test_content = [chr(i) for i in xrange(33, 128)] * 5 random.shuffle(test_content) test_content = ''.join(test_content) + '\n' -def test_lexer_instantiate_all(): +@pytest.mark.parametrize('name', lexers.LEXERS) +def test_lexer_instantiate_all(name): # instantiate every lexer, to see if the token type defs are correct - def verify(name): - getattr(lexers, name) - for x in lexers.LEXERS: - yield verify, x + getattr(lexers, name) -def test_lexer_classes(): +@pytest.mark.parametrize('cls', lexers._iter_lexerclasses(plugins=False)) +def test_lexer_classes(cls): # test that every lexer class has the correct public API - def verify(cls): - assert type(cls.name) is str - for attr in 'aliases', 'filenames', 'alias_filenames', 'mimetypes': - assert hasattr(cls, attr) - assert type(getattr(cls, attr)) is list, \ - "%s: %s attribute wrong" % (cls, attr) - result = cls.analyse_text("abc") - assert isinstance(result, float) and 0.0 <= result <= 1.0 - result = cls.analyse_text(".abc") - assert isinstance(result, float) and 0.0 <= result <= 1.0 - - assert all(al.lower() == al for al in cls.aliases) - - inst = cls(opt1="val1", opt2="val2") - if issubclass(cls, RegexLexer): - if not hasattr(cls, '_tokens'): - # if there's no "_tokens", the lexer has to be one with - # multiple tokendef variants - assert cls.token_variants - for variant in cls.tokens: - assert 'root' in cls.tokens[variant] - else: - assert 'root' in cls._tokens, \ - '%s has no root state' % cls - - if cls.name in ['XQuery', 'Opa']: # XXX temporary - return - - try: - tokens = list(inst.get_tokens(test_content)) - except KeyboardInterrupt: - raise KeyboardInterrupt( - 'interrupted %s.get_tokens(): test_content=%r' % - (cls.__name__, test_content)) - txt = "" - for token in tokens: - assert isinstance(token, tuple) - assert isinstance(token[0], _TokenType) - assert isinstance(token[1], text_type) - txt += token[1] - assert txt == test_content, "%s lexer roundtrip failed: %r != %r" % \ - (cls.name, test_content, txt) - - for lexer in lexers._iter_lexerclasses(plugins=False): - yield verify, lexer - - -def test_lexer_options(): + assert type(cls.name) is str + for attr in 'aliases', 'filenames', 'alias_filenames', 'mimetypes': + assert hasattr(cls, attr) + assert type(getattr(cls, attr)) is list, \ + "%s: %s attribute wrong" % (cls, attr) + result = cls.analyse_text("abc") + assert isinstance(result, float) and 0.0 <= result <= 1.0 + result = cls.analyse_text(".abc") + assert isinstance(result, float) and 0.0 <= result <= 1.0 + + assert all(al.lower() == al for al in cls.aliases) + + inst = cls(opt1="val1", opt2="val2") + if issubclass(cls, RegexLexer): + if not hasattr(cls, '_tokens'): + # if there's no "_tokens", the lexer has to be one with + # multiple tokendef variants + assert cls.token_variants + for variant in cls.tokens: + assert 'root' in cls.tokens[variant] + else: + assert 'root' in cls._tokens, \ + '%s has no root state' % cls + + if cls.name in ['XQuery', 'Opa']: # XXX temporary + return + + try: + tokens = list(inst.get_tokens(test_content)) + except KeyboardInterrupt: + raise KeyboardInterrupt( + 'interrupted %s.get_tokens(): test_content=%r' % + (cls.__name__, test_content)) + txt = "" + for token in tokens: + assert isinstance(token, tuple) + assert isinstance(token[0], _TokenType) + assert isinstance(token[1], text_type) + txt += token[1] + assert txt == test_content, "%s lexer roundtrip failed: %r != %r" % \ + (cls.name, test_content, txt) + + +@pytest.mark.parametrize('cls', lexers._iter_lexerclasses(plugins=False)) +def test_lexer_options(cls): + if cls.__name__ == 'RawTokenLexer': + # this one is special + return + # test that the basic options work def ensure(tokens, output): concatenated = ''.join(token[1] for token in tokens) assert concatenated == output, \ '%s: %r != %r' % (lexer, concatenated, output) - def verify(cls): - inst = cls(stripnl=False) - ensure(inst.get_tokens('a\nb'), 'a\nb\n') - ensure(inst.get_tokens('\n\n\n'), '\n\n\n') - inst = cls(stripall=True) - ensure(inst.get_tokens(' \n b\n\n\n'), 'b\n') - # some lexers require full lines in input - if ('ConsoleLexer' not in cls.__name__ and - 'SessionLexer' not in cls.__name__ and - not cls.__name__.startswith('Literate') and - cls.__name__ not in ('ErlangShellLexer', 'RobotFrameworkLexer')): - inst = cls(ensurenl=False) - ensure(inst.get_tokens('a\nb'), 'a\nb') - inst = cls(ensurenl=False, stripall=True) - ensure(inst.get_tokens('a\nb\n\n'), 'a\nb') - - for lexer in lexers._iter_lexerclasses(plugins=False): - if lexer.__name__ == 'RawTokenLexer': - # this one is special - continue - yield verify, lexer + inst = cls(stripnl=False) + ensure(inst.get_tokens('a\nb'), 'a\nb\n') + ensure(inst.get_tokens('\n\n\n'), '\n\n\n') + inst = cls(stripall=True) + ensure(inst.get_tokens(' \n b\n\n\n'), 'b\n') + # some lexers require full lines in input + if ('ConsoleLexer' not in cls.__name__ and + 'SessionLexer' not in cls.__name__ and + not cls.__name__.startswith('Literate') and + cls.__name__ not in ('ErlangShellLexer', 'RobotFrameworkLexer')): + inst = cls(ensurenl=False) + ensure(inst.get_tokens('a\nb'), 'a\nb') + inst = cls(ensurenl=False, stripall=True) + ensure(inst.get_tokens('a\nb\n\n'), 'a\nb') def test_get_lexers(): @@ -127,7 +122,7 @@ def test_get_lexers(): (lexers.guess_lexer, ("#!/usr/bin/python -O\nprint",)), (lexers.guess_lexer_for_filename, ("a.py", "<%= @foo %>")) ]: - yield verify, func, args + verify(func, args) for cls, (_, lname, aliases, _, mimetypes) in lexers.LEXERS.items(): assert cls == lexers.find_lexer_class(lname).__name__ @@ -146,38 +141,35 @@ def test_get_lexers(): raise Exception -def test_formatter_public_api(): +@pytest.mark.parametrize('cls', [getattr(formatters, name) + for name in formatters.FORMATTERS]) +def test_formatter_public_api(cls): # test that every formatter class has the correct public API ts = list(lexers.PythonLexer().get_tokens("def f(): pass")) string_out = StringIO() bytes_out = BytesIO() - def verify(formatter): - info = formatters.FORMATTERS[formatter.__name__] - assert len(info) == 5 - assert info[1], "missing formatter name" - assert info[2], "missing formatter aliases" - assert info[4], "missing formatter docstring" - - try: - inst = formatter(opt1="val1") - except (ImportError, FontNotFound) as e: - raise support.SkipTest(e) - - try: - inst.get_style_defs() - except NotImplementedError: - # may be raised by formatters for which it doesn't make sense - pass - - if formatter.unicodeoutput: - inst.format(ts, string_out) - else: - inst.format(ts, bytes_out) + info = formatters.FORMATTERS[cls.__name__] + assert len(info) == 5 + assert info[1], "missing formatter name" + assert info[2], "missing formatter aliases" + assert info[4], "missing formatter docstring" - for name in formatters.FORMATTERS: - formatter = getattr(formatters, name) - yield verify, formatter + try: + inst = cls(opt1="val1") + except (ImportError, FontNotFound) as e: + pytest.skip(str(e)) + + try: + inst.get_style_defs() + except NotImplementedError: + # may be raised by formatters for which it doesn't make sense + pass + + if cls.unicodeoutput: + inst.format(ts, string_out) + else: + inst.format(ts, bytes_out) def test_formatter_encodings(): @@ -201,37 +193,33 @@ def test_formatter_encodings(): assert u"ä".encode("utf8") in format(tokens, fmt) -def test_formatter_unicode_handling(): +@pytest.mark.parametrize('cls', [getattr(formatters, name) + for name in formatters.FORMATTERS]) +def test_formatter_unicode_handling(cls): # test that the formatter supports encoding and Unicode tokens = list(lexers.PythonLexer(encoding='utf-8'). get_tokens("def f(): 'ä'")) - def verify(formatter): - try: - inst = formatter(encoding=None) - except (ImportError, FontNotFound) as e: - # some dependency or font not installed - raise support.SkipTest(e) - - if formatter.name != 'Raw tokens': - out = format(tokens, inst) - if formatter.unicodeoutput: - assert type(out) is text_type, '%s: %r' % (formatter, out) - - inst = formatter(encoding='utf-8') - out = format(tokens, inst) - 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) - assert type(out) is bytes, '%s: %r' % (formatter, out) - - for formatter, info in formatters.FORMATTERS.items(): - # this tests the automatic importing as well - fmter = getattr(formatters, formatter) - yield verify, fmter + try: + inst = cls(encoding=None) + except (ImportError, FontNotFound) as e: + # some dependency or font not installed + pytest.skip(str(e)) + + if cls.name != 'Raw tokens': + out = format(tokens, inst) + if cls.unicodeoutput: + assert type(out) is text_type, '%s: %r' % (cls, out) + + inst = cls(encoding='utf-8') + out = format(tokens, inst) + assert type(out) is bytes, '%s: %r' % (cls, out) + # Cannot test for encoding, since formatters may have to escape + # non-ASCII characters. + else: + inst = cls() + out = format(tokens, inst) + assert type(out) is bytes, '%s: %r' % (cls, out) def test_get_formatters(): |