Debian Codegraph @

tests/test_inherit.py

raw
1 """
2 Tests for inheritance in RegexLexer
3 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4
5 :copyright: Copyright 2006-present by the Pygments team, see AUTHORS.
6 :license: BSD, see LICENSE for details.
7 """
8
9 from pygments.lexer import RegexLexer, inherit
10 from pygments.token import Text
11
12
13 class One(RegexLexer):
14 tokens = {
15 'root': [
16 ('a', Text),
17 ('b', Text),
18 ],
19 }
20
21
22 class Two(One):
23 tokens = {
24 'root': [
25 ('x', Text),
26 inherit,
27 ('y', Text),
28 ],
29 }
30
31
32 class Three(Two):
33 tokens = {
34 'root': [
35 ('i', Text),
36 inherit,
37 ('j', Text),
38 ],
39 }
40
41
42 class Beginning(Two):
43 tokens = {
44 'root': [
45 inherit,
46 ('m', Text),
47 ],
48 }
49
50
51 class End(Two):
52 tokens = {
53 'root': [
54 ('m', Text),
55 inherit,
56 ],
57 }
58
59
60 class Empty(One):
61 tokens = {}
62
63
64 class Skipped(Empty):
65 tokens = {
66 'root': [
67 ('x', Text),
68 inherit,
69 ('y', Text),
70 ],
71 }
72
73
74 def test_single_inheritance_position():
75 t = Two()
76 pats = [x[0].__self__.pattern for x in t._tokens['root']]
77 assert ['x', 'a', 'b', 'y'] == pats
78
79
80 def test_multi_inheritance_beginning():
81 t = Beginning()
82 pats = [x[0].__self__.pattern for x in t._tokens['root']]
83 assert ['x', 'a', 'b', 'y', 'm'] == pats
84
85
86 def test_multi_inheritance_end():
87 t = End()
88 pats = [x[0].__self__.pattern for x in t._tokens['root']]
89 assert ['m', 'x', 'a', 'b', 'y'] == pats
90
91
92 def test_multi_inheritance_position():
93 t = Three()
94 pats = [x[0].__self__.pattern for x in t._tokens['root']]
95 assert ['i', 'x', 'a', 'b', 'y', 'j'] == pats
96
97
98 def test_single_inheritance_with_skip():
99 t = Skipped()
100 pats = [x[0].__self__.pattern for x in t._tokens['root']]
101 assert ['x', 'a', 'b', 'y'] == pats