Debian Codegraph @

tests/test_tnt.py

raw
1 """
2 Typograhic Number Theory 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 pytest
10
11 from pygments.lexers.tnt import TNTLexer
12 from pygments.token import Text, Operator, Keyword, Name, Number, \
13 Punctuation, Error
14
15
16 @pytest.fixture(autouse=True)
17 def lexer():
18 yield TNTLexer()
19
20
21 # whitespace
22
23 @pytest.mark.parametrize('text', (' a', ' \t0', '\n\n 3'))
24 def test_whitespace_positive_matches(lexer, text):
25 """Test fragments that should be tokenized as whitespace text."""
26 assert lexer.whitespace(0, text) == len(text) - 1
27 assert lexer.whitespace(0, text, True) == len(text) - 1
28 assert lexer.cur[-1] == (0, Text, text[:-1])
29
30
31 @pytest.mark.parametrize('text', ('0 a=b premise', 'b=a symmetry'))
32 def test_whitespace_negative_matches(lexer, text):
33 """Test statements that do not start with whitespace text."""
34 assert lexer.whitespace(0, text) == 0
35 with pytest.raises(AssertionError):
36 lexer.whitespace(0, text, True)
37 assert not lexer.cur
38
39
40 # terms that can go on either side of an = sign
41
42 @pytest.mark.parametrize('text', ('a ', "a' ", 'b ', "c' "))
43 def test_variable_positive_matches(lexer, text):
44 """Test fragments that should be tokenized as variables."""
45 assert lexer.variable(0, text) == len(text) - 1
46 assert lexer.cur[-1] == (0, Name.Variable, text[:-1])
47
48
49 @pytest.mark.parametrize('text', ("' ", 'f ', "f' "))
50 def test_variable_negative_matches(lexer, text):
51 """Test fragments that should **not** be tokenized as variables."""
52 with pytest.raises(AssertionError):
53 lexer.variable(0, text)
54 assert not lexer.cur
55
56
57 @pytest.mark.parametrize('text', ('0', 'S0', 'SSSSS0'))
58 def test_numeral_positive_matches(lexer, text):
59 """Test fragments that should be tokenized as (unary) numerals."""
60 assert lexer.term(0, text) == len(text)
61 assert lexer.cur[-1] == (len(text) - 1, Number.Integer, text[-1])
62 if text != '0':
63 assert lexer.cur[-2] == (0, Number.Integer, text[:-1])
64
65
66 @pytest.mark.parametrize('text', (
67 '(a+b)', '(b.a)', '(c+d)'
68 ))
69 def test_multiterm_positive_matches(lexer, text):
70 """Test fragments that should be tokenized as a compound term."""
71 assert lexer.term(0, text) == len(text)
72 assert [t[1] for t in lexer.cur] == [
73 Punctuation, Name.Variable, Operator,
74 Name.Variable, Punctuation
75 ]
76
77
78 @pytest.mark.parametrize('text', ('1', '=', 'A'))
79 def test_term_negative_matches(lexer, text):
80 """Test fragments that should not be tokenized as terms at all."""
81 with pytest.raises(AssertionError):
82 lexer.term(0, text)
83 assert not lexer.cur
84
85
86 # full statements, minus rule
87
88 @pytest.mark.parametrize('text', ('~a=b ', '~~~~a=b '))
89 def test_negator_positive_matches(lexer, text):
90 """Test statements that start with a negation."""
91 assert lexer.formula(0, text) == len(text) - 1
92 assert lexer.cur[0] == (0, Operator, text[:-4])
93
94
95 @pytest.mark.parametrize('text', ('Aa:a=b ', 'Eb:a=b '))
96 def test_quantifier_positive_matches(lexer, text):
97 """Test statements that start with a quantifier."""
98 assert lexer.formula(0, text) == len(text) - 1
99 assert lexer.cur[0][1] == Keyword.Declaration
100 assert lexer.cur[1][1] == Name.Variable
101 assert lexer.cur[2] == (2, Punctuation, ':')
102
103
104 @pytest.mark.parametrize('text', ('Aaa=b', 'Eba=b'))
105 def test_quantifier_negative_matches(lexer, text):
106 """Test quantifiers that are only partially valid."""
107 with pytest.raises(AssertionError):
108 lexer.formula(0, text)
109 # leftovers should still be valid
110 assert lexer.cur[0][1] == Keyword.Declaration
111 assert lexer.cur[1][1] == Name.Variable
112
113
114 @pytest.mark.parametrize('text', ('<a=b&b=a>', '<a=b|b=a>', '<a=b]b=a>'))
115 def test_compound_positive_matches(lexer, text):
116 """Test statements that consist of multiple formulas compounded."""
117 assert lexer.formula(0, text) == len(text)
118 assert lexer.cur[0] == (0, Punctuation, '<')
119 assert lexer.cur[4][1] == Operator
120 assert lexer.cur[-1] == (len(text)-1, Punctuation, '>')
121
122
123 @pytest.mark.parametrize('text', ('<a=b/b=a>', '<a=b&b=a '))
124 def test_compound_negative_matches(lexer, text):
125 """Test statements that look like compounds but are invalid."""
126 with pytest.raises(AssertionError):
127 lexer.formula(0, text)
128 assert lexer.cur[0] == (0, Punctuation, '<')
129
130
131 @pytest.mark.parametrize('text', ('a=b ', 'a=0 ', '0=b '))
132 def test_formula_postive_matches(lexer, text):
133 """Test the normal singular formula."""
134 assert lexer.formula(0, text) == len(text) - 1
135 assert lexer.cur[0][2] == text[0]
136 assert lexer.cur[1] == (1, Operator, '=')
137 assert lexer.cur[2][2] == text[2]
138
139
140 @pytest.mark.parametrize('text', ('a/b', '0+0 '))
141 def test_formula_negative_matches(lexer, text):
142 """Test anything but an equals sign."""
143 with pytest.raises(AssertionError):
144 lexer.formula(0, text)
145
146
147 # rules themselves
148
149 @pytest.mark.parametrize('text', (
150 'fantasy rule', 'carry over line 5', 'premise', 'joining',
151 'double-tilde', 'switcheroo', 'De Morgan', 'specification'
152 ))
153 def test_rule_positive_matches(lexer, text):
154 """Test some valid rules of TNT."""
155 assert lexer.rule(0, text) == len(text)
156 assert lexer.cur[0][:2] == (0, Keyword)
157 if text[-1].isdigit():
158 assert lexer.cur[1][1] == Number.Integer
159
160
161 @pytest.mark.parametrize('text', (
162 'fantasy', 'carry over', 'premse', 'unjoining',
163 'triple-tilde', 'switcheru', 'De-Morgan', 'despecification'
164 ))
165 def test_rule_negative_matches(lexer, text):
166 """Test some invalid rules of TNT."""
167 with pytest.raises(AssertionError):
168 lexer.rule(0, text)
169
170
171 # referrals
172
173 @pytest.mark.parametrize('text', ('(lines 1, 2, and 4)', '(line 3,5,6)',
174 '(lines 1, 6 and 0)'))
175 def test_lineno_positive_matches(lexer, text):
176 """Test line referrals."""
177 assert lexer.lineno(0, text) == len(text)
178 assert lexer.cur[0] == (0, Punctuation, '(')
179 assert lexer.cur[1][:2] == (1, Text)
180 assert lexer.cur[2][1] == Number.Integer
181 assert lexer.cur[3] == (len(text)-1, Punctuation, ')')
182
183
184 @pytest.mark.parametrize('text', (
185 '(lines one, two, and four)1 ', # to avoid IndexError
186 '(lines 1 2 and 3)', '(lines 1 2 3)'
187 ))
188 def test_lineno_negative_matches(lexer, text):
189 """Test invalid line referrals."""
190 with pytest.raises(AssertionError):
191 lexer.lineno(0, text)
192
193
194 # worst-case: error text
195
196 @pytest.mark.parametrize('text', ('asdf', 'fdsa\nasdf', 'asdf\n '))
197 def test_error_till_line_end(lexer, text):
198 try:
199 nl = text.index('\n')
200 except ValueError:
201 nl = len(text)
202 try:
203 end = text.find(text.split(None, 2)[1])
204 except IndexError: # split failed
205 end = len(text)
206 assert lexer.error_till_line_end(0, text) == end
207 assert lexer.cur[0] == (0, Error, text[:nl])
208
209
210 # full statement, including rule (because this can't be tested any other way)
211
212 @pytest.mark.parametrize('text', ('[ push', '] pop'))
213 def test_fantasy_positive_matches(lexer, text):
214 """Test statements that should be tokenized as push/pop statements."""
215 assert lexer.get_tokens_unprocessed(text)[0] == (0, Keyword, text[0])
216
217
218 # full text is already done by examplefiles, but here's some exceptions
219
220 @pytest.mark.parametrize('text', (
221 '0', 'a=b', 'premise',
222 '0 a=b premise', '1 b=a symmetry (line 0)'
223 ))
224 def test_no_crashing(lexer, text):
225 """Test incomplete text fragments that shouldn't crash the whole lexer."""
226 assert lexer.get_tokens(text)