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
|
"""
Tests for `attr.filters`.
"""
from __future__ import absolute_import, division, print_function
import pytest
from attr._make import attributes, attr, fields
from attr.filters import _split_what, include, exclude
@attributes
class C(object):
a = attr()
b = attr()
class TestSplitWhat(object):
"""
Tests for `_split_what`.
"""
def test_splits(self):
"""
Splits correctly.
"""
assert (
frozenset((int, str)),
frozenset((C.a,)),
) == _split_what((str, C.a, int,))
class TestInclude(object):
"""
Tests for `include`.
"""
@pytest.mark.parametrize("incl,value", [
((int,), 42),
((str,), "hello"),
((str, fields(C).a), 42),
((str, fields(C).b), "hello"),
])
def test_allow(self, incl, value):
"""
Return True if a class or attribute is whitelisted.
"""
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"),
])
def test_drop_class(self, incl, value):
"""
Return False on non-whitelisted classes and attributes.
"""
i = include(*incl)
assert i(fields(C).a, value) is False
class TestExclude(object):
"""
Tests for `exclude`.
"""
@pytest.mark.parametrize("excl,value", [
((str,), 42),
((int,), "hello"),
((str, fields(C).b), 42),
((int, fields(C).b), "hello"),
])
def test_allow(self, excl, value):
"""
Return True if class or attribute is not blacklisted.
"""
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"),
])
def test_drop_class(self, excl, value):
"""
Return True on non-blacklisted classes and attributes.
"""
e = exclude(*excl)
assert e(fields(C).a, value) is False
|