Coverage for src/pytest_cases/filters.py: 88%
37 statements
« prev ^ index » next coverage.py v7.2.7, created at 2024-09-26 21:52 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2024-09-26 21:52 +0000
1# Authors: Sylvain MARIE <sylvain.marie@se.com>
2# + All contributors to <https://github.com/smarie/python-pytest-cases>
3#
4# License: 3-clause BSD, <https://github.com/smarie/python-pytest-cases/blob/master/LICENSE>
5import re
7from .case_funcs import get_case_id, get_case_tags
10class CaseFilter(object):
11 """
12 This class represents a case filter. You can use it in order to filter cases to be used by `parametrize_by_cases`.
14 `CaseFilter` implements logical operations "and" (`&`) "or" (`|`) and "not" (`~`). You can use it to define a
15 composable filter from any callable receiving a single `case` argument and returning a boolean indicating if the
16 `case` is selected.
17 """
19 def __init__(self, filter_function):
20 self.filter_function = filter_function
22 def __call__(self, case):
23 return self.filter_function(case)
25 def __and__(self, other):
26 return CaseFilter(
27 lambda case: self(case) and other(case)
28 )
30 def __rand__(self, other):
31 return self & other
33 def __or__(self, other):
34 return CaseFilter(
35 lambda case: self(case) or other(case)
36 )
38 def __ror__(self, other):
39 return self | other
41 def __invert__(self):
42 return CaseFilter(
43 lambda case: not self(case)
44 )
47def has_tags(*tag_names # type: str
48 ):
49 """
50 Selects cases that have all tags in `tag_names`. See `@case(tags=...)` to add tags to a case.
52 :param tag_names:
53 :return:
54 """
56 def _filter(case):
57 return len(
58 set(tag_names) - set(get_case_tags(case))
59 ) == 0
61 return CaseFilter(_filter)
64def has_tag(tag_name # type: str
65 ):
66 """
67 Selects cases that have the tag `tag_name`. See `@case(tags=...)` to add tags to a case.
69 :param tag_name:
70 :return:
71 """
73 def _filter(case):
74 return tag_name in get_case_tags(case)
76 return CaseFilter(_filter)
79def id_has_prefix(prefix # type: str
80 ):
81 """
82 Selects cases that have a case id prefix `prefix`.
84 Note that this is not the prefix of the whole case function name, but the case id,
85 possibly overridden with `@case(id=)`
86 """
88 def _filter(case):
89 return get_case_id(case).startswith(prefix)
91 return CaseFilter(_filter)
94def id_has_suffix(suffix # type: str
95 ):
96 """
97 Selects cases that have a case id suffix `suffix`.
99 Note that this is not the suffix of the whole case function name, but the case id,
100 possibly overridden with `@case(id=)`
101 """
103 def _filter(case):
104 return get_case_id(case).endswith(suffix)
106 return CaseFilter(_filter)
109def id_match_regex(regex # type: str
110 ):
111 """
112 Select cases that have a case id matching `regex`.
114 Note that this is not a match of the whole case function name, but the case id,
115 possibly overridden with `@case(id=)`
116 """
118 def _filter(case):
119 return re.match(regex, get_case_id(case))
121 return CaseFilter(_filter)