| 1 |
""" |
| 2 |
Pygments tests for using() |
| 3 |
~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 4 |
|
| 5 |
:copyright: Copyright 2006-present by the Pygments team, see AUTHORS. |
| 6 |
:license: BSD, see LICENSE for details. |
| 7 |
""" |
| 8 |
|
| 9 |
from pytest import raises |
| 10 |
|
| 11 |
from pygments.lexer import using, bygroups, this, RegexLexer |
| 12 |
from pygments.token import String, Text, Keyword |
| 13 |
|
| 14 |
|
| 15 |
class MyLexer(RegexLexer): |
| 16 |
tokens = { |
| 17 |
'root': [ |
| 18 |
(r'#.*', |
| 19 |
using(this, state='invalid')), |
| 20 |
(r'(")(.+?)(")', |
| 21 |
bygroups(String, using(this, state='string'), String)), |
| 22 |
(r'[^"]+', Text), |
| 23 |
], |
| 24 |
'string': [ |
| 25 |
(r'.+', Keyword), |
| 26 |
], |
| 27 |
} |
| 28 |
|
| 29 |
|
| 30 |
def test_basic(): |
| 31 |
expected = [(Text, 'a'), (String, '"'), (Keyword, 'bcd'), |
| 32 |
(String, '"'), (Text, 'e\n')] |
| 33 |
assert list(MyLexer().get_tokens('a"bcd"e')) == expected |
| 34 |
|
| 35 |
|
| 36 |
def test_error(): |
| 37 |
def gen(): |
| 38 |
return list(MyLexer().get_tokens('#a')) |
| 39 |
assert raises(KeyError, gen) |