Debian Codegraph @

testing/test_mark_expression.py

raw
1 from __future__ import annotations
2
3 from _pytest.mark import MarkMatcher
4 from _pytest.mark.expression import Expression
5 from _pytest.mark.expression import ExpressionMatcher
6 import pytest
7
8
9 def evaluate(input: str, matcher: ExpressionMatcher) -> bool:
10 return Expression.compile(input).evaluate(matcher)
11
12
13 def test_empty_is_false() -> None:
14 assert not evaluate("", lambda ident, /, **kwargs: False)
15 assert not evaluate("", lambda ident, /, **kwargs: True)
16 assert not evaluate(" ", lambda ident, /, **kwargs: False)
17 assert not evaluate("\t", lambda ident, /, **kwargs: False)
18
19
20 @pytest.mark.parametrize(
21 ("expr", "expected"),
22 (
23 ("true", True),
24 ("false", False),
25 ("not true", False),
26 ("not false", True),
27 ("not not true", True),
28 ("not not false", False),
29 ("true and true", True),
30 ("true and false", False),
31 ("false and true", False),
32 ("true and true and true", True),
33 ("true and true and false", False),
34 ("true and true and not true", False),
35 ("false or false", False),
36 ("false or true", True),
37 ("true or true", True),
38 ("true or true or false", True),
39 ("true and true or false", True),
40 ("not true or true", True),
41 ("(not true) or true", True),
42 ("not (true or true)", False),
43 ("true and true or false and false", True),
44 ("true and (true or false) and false", False),
45 ("true and (true or (not (not false))) and false", False),
46 ),
47 )
48 def test_basic(expr: str, expected: bool) -> None:
49 def matcher(name: str, /, **kwargs: str | int | bool | None) -> bool:
50 return {"true": True, "false": False}[name]
51
52 assert evaluate(expr, matcher) is expected
53
54
55 @pytest.mark.parametrize(
56 ("expr", "expected"),
57 (
58 (" true ", True),
59 (" ((((((true)))))) ", True),
60 (" ( ((\t (((true))))) \t \t)", True),
61 ("( true and (((false))))", False),
62 ("not not not not true", True),
63 ("not not not not not true", False),
64 ),
65 )
66 def test_syntax_oddities(expr: str, expected: bool) -> None:
67 def matcher(name: str, /, **kwargs: str | int | bool | None) -> bool:
68 return {"true": True, "false": False}[name]
69
70 assert evaluate(expr, matcher) is expected
71
72
73 def test_backslash_not_treated_specially() -> None:
74 r"""When generating nodeids, if the source name contains special characters
75 like a newline, they are escaped into two characters like \n. Therefore, a
76 user will never need to insert a literal newline, only \n (two chars). So
77 mark expressions themselves do not support escaping, instead they treat
78 backslashes as regular identifier characters."""
79
80 def matcher(name: str, /, **kwargs: str | int | bool | None) -> bool:
81 return {r"\nfoo\n"}.__contains__(name)
82
83 assert evaluate(r"\nfoo\n", matcher)
84 assert not evaluate(r"foo", matcher)
85 with pytest.raises(SyntaxError):
86 evaluate("\nfoo\n", matcher)
87
88
89 def test_backslash_in_identifier_with_string_literal() -> None:
90 r"""Backslashes in identifiers should not cause false rejections when the
91 expression also contains string literals. Regression test for a bug where
92 the scanner searched the entire input for backslashes instead of only the
93 current string literal value."""
94
95 def matcher(name: str, /, **kwargs: str | int | bool | None) -> bool:
96 return {r"\nfoo\n", r"test\case", "mark"}.__contains__(name)
97
98 assert evaluate(r'\nfoo\n and mark(x="y")', matcher)
99 assert evaluate(r'mark(x="y") and \nfoo\n', matcher)
100 assert evaluate(r'test\case and mark(x="y")', matcher)
101
102
103 @pytest.mark.parametrize(
104 ("expr", "column", "message"),
105 (
106 ("(", 2, "expected not OR left parenthesis OR identifier; got end of input"),
107 (
108 " (",
109 3,
110 "expected not OR left parenthesis OR identifier; got end of input",
111 ),
112 (
113 ")",
114 1,
115 "expected not OR left parenthesis OR identifier; got right parenthesis",
116 ),
117 (
118 ") ",
119 1,
120 "expected not OR left parenthesis OR identifier; got right parenthesis",
121 ),
122 (
123 "not",
124 4,
125 "expected not OR left parenthesis OR identifier; got end of input",
126 ),
127 (
128 "not not",
129 8,
130 "expected not OR left parenthesis OR identifier; got end of input",
131 ),
132 (
133 "(not)",
134 5,
135 "expected not OR left parenthesis OR identifier; got right parenthesis",
136 ),
137 ("and", 1, "expected not OR left parenthesis OR identifier; got and"),
138 (
139 "ident and",
140 10,
141 "expected not OR left parenthesis OR identifier; got end of input",
142 ),
143 (
144 "ident and or",
145 11,
146 "expected not OR left parenthesis OR identifier; got or",
147 ),
148 ("ident ident", 7, "expected end of input; got identifier"),
149 ),
150 )
151 def test_syntax_errors(expr: str, column: int, message: str) -> None:
152 with pytest.raises(SyntaxError) as excinfo:
153 evaluate(expr, lambda ident, /, **kwargs: True)
154 assert excinfo.value.offset == column
155 assert excinfo.value.msg == message
156
157
158 @pytest.mark.parametrize(
159 "ident",
160 (
161 ".",
162 "...",
163 ":::",
164 "a:::c",
165 "a+-b",
166 r"\nhe\\l\lo\n\t\rbye",
167 "a/b",
168 "אבגד",
169 "aaאבגדcc",
170 "a[bcd]",
171 "1234",
172 "1234abcd",
173 "1234and",
174 "1234or",
175 "1234not",
176 "notandor",
177 "not_and_or",
178 "not[and]or",
179 "1234+5678",
180 "123.232",
181 "True",
182 "False",
183 "None",
184 "if",
185 "else",
186 "while",
187 ),
188 )
189 def test_valid_idents(ident: str) -> None:
190 def matcher(name: str, /, **kwargs: str | int | bool | None) -> bool:
191 return name == ident
192
193 assert evaluate(ident, matcher)
194
195
196 @pytest.mark.parametrize(
197 "ident",
198 (
199 "^",
200 "*",
201 "=",
202 "&",
203 "%",
204 "$",
205 "#",
206 "@",
207 "!",
208 "~",
209 "{",
210 "}",
211 '"',
212 "'",
213 "|",
214 ";",
215 "←",
216 ),
217 )
218 def test_invalid_idents(ident: str) -> None:
219 with pytest.raises(SyntaxError):
220 evaluate(ident, lambda ident, /, **kwargs: True)
221
222
223 @pytest.mark.parametrize(
224 "expr, expected_error_msg",
225 (
226 ("mark()", "expected identifier; got right parenthesis"),
227 ("mark(True=False)", "unexpected reserved python keyword `True`"),
228 ("mark(def=False)", "unexpected reserved python keyword `def`"),
229 ("mark(class=False)", "unexpected reserved python keyword `class`"),
230 ("mark(if=False)", "unexpected reserved python keyword `if`"),
231 ("mark(else=False)", "unexpected reserved python keyword `else`"),
232 ("mark(valid=False, def=1)", "unexpected reserved python keyword `def`"),
233 ("mark(1)", "not a valid python identifier 1"),
234 ("mark(var:=False", "not a valid python identifier var:"),
235 ("mark(1=2)", "not a valid python identifier 1"),
236 ("mark(/=2)", "not a valid python identifier /"),
237 ("mark(var==", "expected identifier; got ="),
238 ("mark(var)", "expected =; got right parenthesis"),
239 ("mark(var=none)", 'unexpected character/s "none"'),
240 ("mark(var=1.1)", 'unexpected character/s "1.1"'),
241 ("mark(var=')", """closing quote "'" is missing"""),
242 ('mark(var=")', 'closing quote """ is missing'),
243 ("""mark(var="')""", 'closing quote """ is missing'),
244 ("""mark(var='")""", """closing quote "'" is missing"""),
245 (
246 r"mark(var='\hugo')",
247 r'escaping with "\\" not supported in marker expression',
248 ),
249 ("mark(empty_list=[])", r'unexpected character/s "\[\]"'),
250 ("'str'", "expected not OR left parenthesis OR identifier; got string literal"),
251 ),
252 )
253 def test_invalid_kwarg_name_or_value(
254 expr: str, expected_error_msg: str, mark_matcher: MarkMatcher
255 ) -> None:
256 with pytest.raises(SyntaxError, match=expected_error_msg):
257 assert evaluate(expr, mark_matcher)
258
259
260 @pytest.fixture(scope="session")
261 def mark_matcher() -> MarkMatcher:
262 markers = [
263 pytest.mark.number_mark(a=1, b=2, c=3, d=999_999).mark,
264 pytest.mark.builtin_matchers_mark(x=True, y=False, z=None).mark,
265 pytest.mark.str_mark( # pylint: disable-next=non-ascii-name
266 m="M", space="with space", empty="", aaאבגדcc="aaאבגדcc", אבגד="אבגד"
267 ).mark,
268 ]
269
270 return MarkMatcher.from_markers(markers)
271
272
273 @pytest.mark.parametrize(
274 "expr, expected",
275 (
276 # happy cases
277 ("number_mark(a=1)", True),
278 ("number_mark(b=2)", True),
279 ("number_mark(a=1,b=2)", True),
280 ("number_mark(a=1, b=2)", True),
281 ("number_mark(d=999999)", True),
282 ("number_mark(a = 1,b= 2, c = 3)", True),
283 # sad cases
284 ("number_mark(a=6)", False),
285 ("number_mark(b=6)", False),
286 ("number_mark(a=1,b=6)", False),
287 ("number_mark(a=6,b=2)", False),
288 ("number_mark(a = 1,b= 2, c = 6)", False),
289 ("number_mark(a='1')", False),
290 ),
291 )
292 def test_keyword_expressions_with_numbers(
293 expr: str, expected: bool, mark_matcher: MarkMatcher
294 ) -> None:
295 assert evaluate(expr, mark_matcher) is expected
296
297
298 @pytest.mark.parametrize(
299 "expr, expected",
300 (
301 ("builtin_matchers_mark(x=True)", True),
302 ("builtin_matchers_mark(x=False)", False),
303 ("builtin_matchers_mark(y=True)", False),
304 ("builtin_matchers_mark(y=False)", True),
305 ("builtin_matchers_mark(z=None)", True),
306 ("builtin_matchers_mark(z=False)", False),
307 ("builtin_matchers_mark(z=True)", False),
308 ("builtin_matchers_mark(z=0)", False),
309 ("builtin_matchers_mark(z=1)", False),
310 ),
311 )
312 def test_builtin_matchers_keyword_expressions(
313 expr: str, expected: bool, mark_matcher: MarkMatcher
314 ) -> None:
315 assert evaluate(expr, mark_matcher) is expected
316
317
318 @pytest.mark.parametrize(
319 "expr, expected",
320 (
321 ("str_mark(m='M')", True),
322 ('str_mark(m="M")', True),
323 ("str_mark(aaאבגדcc='aaאבגדcc')", True),
324 ("str_mark(אבגד='אבגד')", True),
325 ("str_mark(space='with space')", True),
326 ("str_mark(empty='')", True),
327 ('str_mark(empty="")', True),
328 ("str_mark(m='wrong')", False),
329 ("str_mark(aaאבגדcc='wrong')", False),
330 ("str_mark(אבגד='wrong')", False),
331 ("str_mark(m='')", False),
332 ('str_mark(m="")', False),
333 ),
334 )
335 def test_str_keyword_expressions(
336 expr: str, expected: bool, mark_matcher: MarkMatcher
337 ) -> None:
338 assert evaluate(expr, mark_matcher) is expected