1"""
2Tests for `attr.filters`.
3"""
4
5from __future__ import absolute_import, division, print_function
6
7import pytest
8
9import attr
10
11from attr import fields
12from attr.filters import _split_what, exclude, include
13
14
15@attr.s
16class C(object):
17    a = attr.ib()
18    b = attr.ib()
19
20
21class TestSplitWhat(object):
22    """
23    Tests for `_split_what`.
24    """
25    def test_splits(self):
26        """
27        Splits correctly.
28        """
29        assert (
30            frozenset((int, str)),
31            frozenset((fields(C).a,)),
32        ) == _split_what((str, fields(C).a, int,))
33
34
35class TestInclude(object):
36    """
37    Tests for `include`.
38    """
39    @pytest.mark.parametrize("incl,value", [
40        ((int,), 42),
41        ((str,), "hello"),
42        ((str, fields(C).a), 42),
43        ((str, fields(C).b), "hello"),
44    ])
45    def test_allow(self, incl, value):
46        """
47        Return True if a class or attribute is whitelisted.
48        """
49        i = include(*incl)
50        assert i(fields(C).a, value) is True
51
52    @pytest.mark.parametrize("incl,value", [
53        ((str,), 42),
54        ((int,), "hello"),
55        ((str, fields(C).b), 42),
56        ((int, fields(C).b), "hello"),
57    ])
58    def test_drop_class(self, incl, value):
59        """
60        Return False on non-whitelisted classes and attributes.
61        """
62        i = include(*incl)
63        assert i(fields(C).a, value) is False
64
65
66class TestExclude(object):
67    """
68    Tests for `exclude`.
69    """
70    @pytest.mark.parametrize("excl,value", [
71        ((str,), 42),
72        ((int,), "hello"),
73        ((str, fields(C).b), 42),
74        ((int, fields(C).b), "hello"),
75    ])
76    def test_allow(self, excl, value):
77        """
78        Return True if class or attribute is not blacklisted.
79        """
80        e = exclude(*excl)
81        assert e(fields(C).a, value) is True
82
83    @pytest.mark.parametrize("excl,value", [
84        ((int,), 42),
85        ((str,), "hello"),
86        ((str, fields(C).a), 42),
87        ((str, fields(C).b), "hello"),
88    ])
89    def test_drop_class(self, excl, value):
90        """
91        Return True on non-blacklisted classes and attributes.
92        """
93        e = exclude(*excl)
94        assert e(fields(C).a, value) is False
95