| 1 |
""" |
| 2 |
Pygments regex lexer tests |
| 3 |
~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 4 |
|
| 5 |
:copyright: Copyright 2006-present by the Pygments team, see AUTHORS. |
| 6 |
:license: BSD, see LICENSE for details. |
| 7 |
""" |
| 8 |
|
| 9 |
import time |
| 10 |
|
| 11 |
import pytest |
| 12 |
|
| 13 |
from pygments.token import Keyword, Name, String, Text |
| 14 |
from pygments.lexers.perl import PerlLexer |
| 15 |
|
| 16 |
|
| 17 |
@pytest.fixture(scope='module') |
| 18 |
def lexer(): |
| 19 |
yield PerlLexer() |
| 20 |
|
| 21 |
|
| 22 |
# Test runaway regexes. |
| 23 |
# A previous version of the Perl lexer would spend a great deal of |
| 24 |
# time backtracking when given particular strings. These tests show that |
| 25 |
# the runaway backtracking doesn't happen any more (at least for the given |
| 26 |
# cases). |
| 27 |
|
| 28 |
|
| 29 |
# Test helpers. |
| 30 |
|
| 31 |
def assert_single_token(lexer, s, token): |
| 32 |
"""Show that a given string generates only one token.""" |
| 33 |
tokens = list(lexer.get_tokens_unprocessed(s)) |
| 34 |
assert len(tokens) == 1 |
| 35 |
assert s == tokens[0][2] |
| 36 |
assert token == tokens[0][1] |
| 37 |
|
| 38 |
|
| 39 |
def assert_tokens(lexer, strings, expected_tokens): |
| 40 |
"""Show that a given string generates the expected tokens.""" |
| 41 |
tokens = list(lexer.get_tokens_unprocessed(''.join(strings))) |
| 42 |
parsed_strings = [t[2] for t in tokens] |
| 43 |
assert parsed_strings == strings |
| 44 |
parsed_tokens = [t[1] for t in tokens] |
| 45 |
assert parsed_tokens == expected_tokens |
| 46 |
|
| 47 |
|
| 48 |
def assert_fast_tokenization(lexer, s): |
| 49 |
"""Show that a given string is tokenized quickly.""" |
| 50 |
start = time.time() |
| 51 |
tokens = list(lexer.get_tokens_unprocessed(s)) |
| 52 |
end = time.time() |
| 53 |
# Isn't 10 seconds kind of a long time? Yes, but we don't want false |
| 54 |
# positives when the tests are starved for CPU time. |
| 55 |
if end-start > 10: |
| 56 |
pytest.fail('tokenization took too long') |
| 57 |
return tokens |
| 58 |
|
| 59 |
|
| 60 |
# Strings. |
| 61 |
|
| 62 |
def test_single_quote_strings(lexer): |
| 63 |
assert_single_token(lexer, r"'foo\tbar\\\'baz'", String) |
| 64 |
assert_fast_tokenization(lexer, "'" + '\\'*999) |
| 65 |
|
| 66 |
|
| 67 |
def test_double_quote_strings(lexer): |
| 68 |
assert_single_token(lexer, r'"foo\tbar\\\"baz"', String) |
| 69 |
assert_fast_tokenization(lexer, '"' + '\\'*999) |
| 70 |
|
| 71 |
|
| 72 |
def test_backtick_strings(lexer): |
| 73 |
assert_single_token(lexer, r'`foo\tbar\\\`baz`', String.Backtick) |
| 74 |
assert_fast_tokenization(lexer, '`' + '\\'*999) |
| 75 |
|
| 76 |
|
| 77 |
# Regex matches with various delimiters. |
| 78 |
|
| 79 |
def test_match(lexer): |
| 80 |
assert_single_token(lexer, r'/aa\tbb/', String.Regex) |
| 81 |
assert_fast_tokenization(lexer, '/' + '\\'*999) |
| 82 |
|
| 83 |
|
| 84 |
def test_match_with_slash(lexer): |
| 85 |
assert_tokens(lexer, ['m', '/\n\\t\\\\/'], [String.Regex, String.Regex]) |
| 86 |
assert_fast_tokenization(lexer, 'm/xxx\n' + '\\'*999) |
| 87 |
|
| 88 |
|
| 89 |
def test_match_with_bang(lexer): |
| 90 |
assert_tokens(lexer, ['m', r'!aa\t\!bb!'], [String.Regex, String.Regex]) |
| 91 |
assert_fast_tokenization(lexer, 'm!' + '\\'*999) |
| 92 |
|
| 93 |
|
| 94 |
def test_match_with_brace(lexer): |
| 95 |
assert_tokens(lexer, ['m', r'{aa\t\}bb}'], [String.Regex, String.Regex]) |
| 96 |
assert_fast_tokenization(lexer, 'm{' + '\\'*999) |
| 97 |
|
| 98 |
|
| 99 |
def test_match_with_angle_brackets(lexer): |
| 100 |
assert_tokens(lexer, ['m', r'<aa\t\>bb>'], [String.Regex, String.Regex]) |
| 101 |
assert_fast_tokenization(lexer, 'm<' + '\\'*999) |
| 102 |
|
| 103 |
|
| 104 |
def test_match_with_parenthesis(lexer): |
| 105 |
assert_tokens(lexer, ['m', r'(aa\t\)bb)'], [String.Regex, String.Regex]) |
| 106 |
assert_fast_tokenization(lexer, 'm(' + '\\'*999) |
| 107 |
|
| 108 |
|
| 109 |
def test_match_with_at_sign(lexer): |
| 110 |
assert_tokens(lexer, ['m', r'@aa\t\@bb@'], [String.Regex, String.Regex]) |
| 111 |
assert_fast_tokenization(lexer, 'm@' + '\\'*999) |
| 112 |
|
| 113 |
|
| 114 |
def test_match_with_percent_sign(lexer): |
| 115 |
assert_tokens(lexer, ['m', r'%aa\t\%bb%'], [String.Regex, String.Regex]) |
| 116 |
assert_fast_tokenization(lexer, 'm%' + '\\'*999) |
| 117 |
|
| 118 |
|
| 119 |
def test_match_with_dollar_sign(lexer): |
| 120 |
assert_tokens(lexer, ['m', r'$aa\t\$bb$'], [String.Regex, String.Regex]) |
| 121 |
assert_fast_tokenization(lexer, 'm$' + '\\'*999) |
| 122 |
|
| 123 |
|
| 124 |
# Regex substitutions with various delimeters. |
| 125 |
|
| 126 |
def test_substitution_with_slash(lexer): |
| 127 |
assert_single_token(lexer, 's/aaa/bbb/g', String.Regex) |
| 128 |
assert_fast_tokenization(lexer, 's/foo/' + '\\'*999) |
| 129 |
|
| 130 |
|
| 131 |
def test_substitution_with_at_sign(lexer): |
| 132 |
assert_single_token(lexer, r's@aaa@bbb@g', String.Regex) |
| 133 |
assert_fast_tokenization(lexer, 's@foo@' + '\\'*999) |
| 134 |
|
| 135 |
|
| 136 |
def test_substitution_with_percent_sign(lexer): |
| 137 |
assert_single_token(lexer, r's%aaa%bbb%g', String.Regex) |
| 138 |
assert_fast_tokenization(lexer, 's%foo%' + '\\'*999) |
| 139 |
|
| 140 |
|
| 141 |
def test_substitution_with_brace(lexer): |
| 142 |
assert_single_token(lexer, r's{aaa}', String.Regex) |
| 143 |
assert_fast_tokenization(lexer, 's{' + '\\'*999) |
| 144 |
|
| 145 |
|
| 146 |
def test_substitution_with_angle_bracket(lexer): |
| 147 |
assert_single_token(lexer, r's<aaa>', String.Regex) |
| 148 |
assert_fast_tokenization(lexer, 's<' + '\\'*999) |
| 149 |
|
| 150 |
|
| 151 |
def test_substitution_with_square_bracket(lexer): |
| 152 |
assert_single_token(lexer, r's[aaa]', String.Regex) |
| 153 |
assert_fast_tokenization(lexer, 's[' + '\\'*999) |
| 154 |
|
| 155 |
|
| 156 |
def test_substitution_with_parenthesis(lexer): |
| 157 |
assert_single_token(lexer, r's(aaa)', String.Regex) |
| 158 |
assert_fast_tokenization(lexer, 's(' + '\\'*999) |
| 159 |
|
| 160 |
|
| 161 |
# Namespaces/modules |
| 162 |
|
| 163 |
def test_package_statement(lexer): |
| 164 |
assert_tokens(lexer, ['package', ' ', 'Foo'], |
| 165 |
[Keyword, Text.Whitespace, Name.Namespace]) |
| 166 |
assert_tokens(lexer, ['package', ' ', 'Foo::Bar'], |
| 167 |
[Keyword, Text.Whitespace, Name.Namespace]) |
| 168 |
|
| 169 |
|
| 170 |
def test_use_statement(lexer): |
| 171 |
assert_tokens(lexer, ['use', ' ', 'Foo'], |
| 172 |
[Keyword, Text.Whitespace, Name.Namespace]) |
| 173 |
assert_tokens(lexer, ['use', ' ', 'Foo::Bar'], |
| 174 |
[Keyword, Text.Whitespace, Name.Namespace]) |
| 175 |
|
| 176 |
|
| 177 |
def test_no_statement(lexer): |
| 178 |
assert_tokens(lexer, ['no', ' ', 'Foo'], |
| 179 |
[Keyword, Text.Whitespace, Name.Namespace]) |
| 180 |
assert_tokens(lexer, ['no', ' ', 'Foo::Bar'], |
| 181 |
[Keyword, Text.Whitespace, Name.Namespace]) |
| 182 |
|
| 183 |
|
| 184 |
def test_require_statement(lexer): |
| 185 |
assert_tokens(lexer, ['require', ' ', 'Foo'], |
| 186 |
[Keyword, Text.Whitespace, Name.Namespace]) |
| 187 |
assert_tokens(lexer, ['require', ' ', 'Foo::Bar'], |
| 188 |
[Keyword, Text.Whitespace, Name.Namespace]) |
| 189 |
assert_tokens(lexer, ['require', ' ', '"Foo/Bar.pm"'], |
| 190 |
[Keyword, Text.Whitespace, String]) |