2015-02-20 15:34:21 +00:00
|
|
|
"""
|
|
|
|
Tests for `attr.filters`.
|
|
|
|
"""
|
|
|
|
|
|
|
|
from __future__ import absolute_import, division, print_function
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
2016-09-10 17:14:34 +00:00
|
|
|
from attr._make import attributes, attr, fields
|
2015-02-20 15:34:21 +00:00
|
|
|
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"),
|
2016-09-10 17:14:34 +00:00
|
|
|
((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 whitelisted.
|
|
|
|
"""
|
|
|
|
i = include(*incl)
|
2016-09-10 17:14:34 +00:00
|
|
|
assert i(fields(C).a, value) is True
|
2015-02-20 15:34:21 +00:00
|
|
|
|
|
|
|
@pytest.mark.parametrize("incl,value", [
|
|
|
|
((str,), 42),
|
|
|
|
((int,), "hello"),
|
2016-09-10 17:14:34 +00:00
|
|
|
((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-whitelisted classes and attributes.
|
|
|
|
"""
|
|
|
|
i = include(*incl)
|
2016-09-10 17:14:34 +00:00
|
|
|
assert i(fields(C).a, value) is False
|
2015-02-20 15:34:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TestExclude(object):
|
|
|
|
"""
|
|
|
|
Tests for `exclude`.
|
|
|
|
"""
|
|
|
|
@pytest.mark.parametrize("excl,value", [
|
|
|
|
((str,), 42),
|
|
|
|
((int,), "hello"),
|
2016-09-10 17:14:34 +00:00
|
|
|
((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 blacklisted.
|
|
|
|
"""
|
|
|
|
e = exclude(*excl)
|
2016-09-10 17:14:34 +00:00
|
|
|
assert e(fields(C).a, value) is True
|
2015-02-20 15:34:21 +00:00
|
|
|
|
|
|
|
@pytest.mark.parametrize("excl,value", [
|
|
|
|
((int,), 42),
|
|
|
|
((str,), "hello"),
|
2016-09-10 17:14:34 +00:00
|
|
|
((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-blacklisted classes and attributes.
|
|
|
|
"""
|
|
|
|
e = exclude(*excl)
|
2016-09-10 17:14:34 +00:00
|
|
|
assert e(fields(C).a, value) is False
|