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