|
11 | 11 | import dill
|
12 | 12 | from dill import _dill
|
13 | 13 | from dill.session import (
|
14 |
| - EXCLUDE, INCLUDE, FilterRules, RuleType, ipython_filter, size_filter, settings |
| 14 | + EXCLUDE, INCLUDE, FilterRules, FilterSet, RuleType, ipython_filter, size_filter, settings |
15 | 15 | )
|
16 | 16 |
|
| 17 | +def test_filterset(): |
| 18 | + import re |
| 19 | + |
| 20 | + name = 'test' |
| 21 | + regex1 = re.compile(r'\w+\d+') |
| 22 | + regex2 = r'_\w+' |
| 23 | + id_ = id(FilterSet) |
| 24 | + type1 = FilterSet |
| 25 | + type2 = 'type:List' |
| 26 | + func = lambda obj: obj.name == 'Arthur' |
| 27 | + |
| 28 | + empty_filters = FilterSet() |
| 29 | + assert bool(empty_filters) is False |
| 30 | + assert len(empty_filters) == 0 |
| 31 | + assert len([*empty_filters]) == 0 |
| 32 | + |
| 33 | + # also tests add() and __ior__() for non-FilterSet other |
| 34 | + filters = FilterSet._from_iterable([name, regex1, regex2, id_, type1, type2, func]) |
| 35 | + assert filters.names == {name} |
| 36 | + assert filters.regexes == {regex1, re.compile(regex2)} |
| 37 | + assert filters.ids == {id_} |
| 38 | + assert filters.types == {type1, list} |
| 39 | + assert filters.funcs == {func} |
| 40 | + |
| 41 | + assert bool(filters) is True |
| 42 | + assert len(filters) == 7 |
| 43 | + assert all(x in filters for x in [name, regex1, id_, type1, func]) |
| 44 | + |
| 45 | + try: |
| 46 | + filters.add(re.compile(b'an 8-bit string regex')) |
| 47 | + except ValueError: |
| 48 | + pass |
| 49 | + else: |
| 50 | + raise AssertionError("adding invalid filter should raise error") |
| 51 | + |
| 52 | + filters_copy = filters.copy() |
| 53 | + for field in FilterSet._fields: |
| 54 | + original, copy = getattr(filters, field), getattr(filters_copy, field) |
| 55 | + assert copy is not original |
| 56 | + assert copy == original |
| 57 | + |
| 58 | + filters.remove(re.compile(regex2)) |
| 59 | + assert filters.regexes == {regex1} |
| 60 | + filters.discard(list) |
| 61 | + filters.discard(list) # should not raise error |
| 62 | + assert filters.types == {type1} |
| 63 | + assert [*filters] == [name, regex1, id_, type1, func] |
| 64 | + |
| 65 | + # also tests __ior__() for FilterSet other |
| 66 | + filters.update(filters_copy) |
| 67 | + assert filters.types == {type1, list} |
| 68 | + |
| 69 | + filters.clear() |
| 70 | + assert len(filters) == 0 |
| 71 | + |
17 | 72 | NS = {
|
18 | 73 | 'a': 1,
|
19 | 74 | 'aa': 2,
|
@@ -184,6 +239,7 @@ def test_size_filter():
|
184 | 239 | assert did_exclude(NS_copy, filter_size, excluded_subset={'large'})
|
185 | 240 |
|
186 | 241 | if __name__ == '__main__':
|
| 242 | + test_filterset() |
187 | 243 | test_basic_filtering()
|
188 | 244 | test_exclude_include()
|
189 | 245 | test_add_type()
|
|
0 commit comments