-
-
Notifications
You must be signed in to change notification settings - Fork 469
Expand file tree
/
Copy pathtest_filters.py
More file actions
149 lines (126 loc) · 3.37 KB
/
Copy pathtest_filters.py
File metadata and controls
149 lines (126 loc) · 3.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# SPDX-License-Identifier: MIT
"""
Tests for `attr.filters`.
"""
import pytest
import attr
from attr import fields
from attr.filters import _split_what, exclude, include
@attr.s
class C:
a = attr.ib()
b = attr.ib()
@attr.s
class D:
a = attr.ib()
class TestSplitWhat:
"""
Tests for `_split_what`.
"""
def test_splits(self):
"""
Splits correctly.
"""
assert (
frozenset((int, str)),
frozenset(("abcd", "123")),
(fields(C).a,),
) == _split_what((str, "123", fields(C).a, int, "abcd"))
class TestInclude:
"""
Tests for `include`.
"""
@pytest.mark.parametrize(
("incl", "value"),
[
((int,), 42),
((str,), "hello"),
((str, fields(C).a), 42),
((str, fields(C).b), "hello"),
(("a",), 42),
(("a",), "hello"),
(("a", str), 42),
(("a", fields(C).b), "hello"),
],
)
def test_allow(self, incl, value):
"""
Return True if a class or attribute is included.
"""
i = include(*incl)
assert i(fields(C).a, value) is True
@pytest.mark.parametrize(
("incl", "value"),
[
((str,), 42),
((int,), "hello"),
((str, fields(C).b), 42),
((int, fields(C).b), "hello"),
(("b",), 42),
(("b",), "hello"),
(("b", str), 42),
(("b", fields(C).b), "hello"),
],
)
def test_drop_class(self, incl, value):
"""
Return False on non-included classes and attributes.
"""
i = include(*incl)
assert i(fields(C).a, value) is False
def test_allow_attributes_by_identity(self):
"""
Attributes with the same name on other classes are not included.
"""
i = include(fields(C).a)
assert i(fields(C).a, 42) is True
assert i(fields(D).a, 42) is False
class TestExclude:
"""
Tests for `exclude`.
"""
@pytest.mark.parametrize(
("excl", "value"),
[
((str,), 42),
((int,), "hello"),
((str, fields(C).b), 42),
((int, fields(C).b), "hello"),
(("b",), 42),
(("b",), "hello"),
(("b", str), 42),
(("b", fields(C).b), "hello"),
],
)
def test_allow(self, excl, value):
"""
Return True if class or attribute is not excluded.
"""
e = exclude(*excl)
assert e(fields(C).a, value) is True
@pytest.mark.parametrize(
("excl", "value"),
[
((int,), 42),
((str,), "hello"),
((str, fields(C).a), 42),
((str, fields(C).b), "hello"),
(("a",), 42),
(("a",), "hello"),
(("a", str), 42),
(("a", fields(C).b), "hello"),
],
)
def test_drop_class(self, excl, value):
"""
Return True on non-excluded classes and attributes.
"""
e = exclude(*excl)
assert e(fields(C).a, value) is False
def test_drop_attributes_by_identity(self):
"""
Attributes with the same name on other classes are not excluded.
"""
e = exclude(fields(C).a)
assert e(fields(C).a, 42) is False
assert e(fields(D).a, 42) is True