attrs/tests/test_filters.py

110 lines
2.3 KiB
Python
Raw Normal View History

2015-02-20 15:34:21 +00:00
"""
Tests for `attr.filters`.
"""
from __future__ import absolute_import, division, print_function
import pytest
import attr
from attr import fields
from attr.filters import _split_what, exclude, include
2015-02-20 15:34:21 +00:00
@attr.s
2015-02-20 15:34:21 +00:00
class C(object):
a = attr.ib()
b = attr.ib()
2015-02-20 15:34:21 +00:00
class TestSplitWhat(object):
"""
Tests for `_split_what`.
"""
2018-06-10 17:40:07 +00:00
2015-02-20 15:34:21 +00:00
def test_splits(self):
"""
Splits correctly.
"""
assert (
frozenset((int, str)),
frozenset((fields(C).a,)),
2018-06-10 17:40:07 +00:00
) == _split_what((str, fields(C).a, int))
2015-02-20 15:34:21 +00:00
class TestInclude(object):
"""
Tests for `include`.
"""
2018-06-10 17:40:07 +00:00
@pytest.mark.parametrize(
"incl,value",
[
((int,), 42),
((str,), "hello"),
((str, fields(C).a), 42),
((str, fields(C).b), "hello"),
],
)
2015-02-20 15:34:21 +00:00
def test_allow(self, incl, value):
"""
Return True if a class or attribute is included.
2015-02-20 15:34:21 +00:00
"""
i = include(*incl)
assert i(fields(C).a, value) is True
2015-02-20 15:34:21 +00:00
2018-06-10 17:40:07 +00:00
@pytest.mark.parametrize(
"incl,value",
[
((str,), 42),
((int,), "hello"),
((str, fields(C).b), 42),
((int, fields(C).b), "hello"),
],
)
2015-02-20 15:34:21 +00:00
def test_drop_class(self, incl, value):
"""
Return False on non-included classes and attributes.
2015-02-20 15:34:21 +00:00
"""
i = include(*incl)
assert i(fields(C).a, value) is False
2015-02-20 15:34:21 +00:00
class TestExclude(object):
"""
Tests for `exclude`.
"""
2018-06-10 17:40:07 +00:00
@pytest.mark.parametrize(
"excl,value",
[
((str,), 42),
((int,), "hello"),
((str, fields(C).b), 42),
((int, fields(C).b), "hello"),
],
)
2015-02-20 15:34:21 +00:00
def test_allow(self, excl, value):
"""
Return True if class or attribute is not excluded.
2015-02-20 15:34:21 +00:00
"""
e = exclude(*excl)
assert e(fields(C).a, value) is True
2015-02-20 15:34:21 +00:00
2018-06-10 17:40:07 +00:00
@pytest.mark.parametrize(
"excl,value",
[
((int,), 42),
((str,), "hello"),
((str, fields(C).a), 42),
((str, fields(C).b), "hello"),
],
)
2015-02-20 15:34:21 +00:00
def test_drop_class(self, excl, value):
"""
Return True on non-excluded classes and attributes.
2015-02-20 15:34:21 +00:00
"""
e = exclude(*excl)
assert e(fields(C).a, value) is False