2021-12-27 08:29:09 +00:00
|
|
|
# SPDX-License-Identifier: MIT
|
|
|
|
|
2015-02-20 15:34:21 +00:00
|
|
|
"""
|
|
|
|
Tests for `attr.filters`.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
2017-10-02 17:32:10 +00:00
|
|
|
import attr
|
|
|
|
|
|
|
|
from attr import fields
|
2017-11-26 21:18:07 +00:00
|
|
|
from attr.filters import _split_what, exclude, include
|
2015-02-20 15:34:21 +00:00
|
|
|
|
|
|
|
|
2017-10-02 17:32:10 +00:00
|
|
|
@attr.s
|
2022-03-21 07:47:47 +00:00
|
|
|
class C:
|
2017-10-02 17:32:10 +00:00
|
|
|
a = attr.ib()
|
|
|
|
b = attr.ib()
|
2015-02-20 15:34:21 +00:00
|
|
|
|
|
|
|
|
2022-03-21 07:47:47 +00:00
|
|
|
class TestSplitWhat:
|
2015-02-20 15:34:21 +00:00
|
|
|
"""
|
|
|
|
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)),
|
2023-04-14 08:54:31 +00:00
|
|
|
frozenset(("abcd", "123")),
|
2017-10-02 10:14:37 +00:00
|
|
|
frozenset((fields(C).a,)),
|
2023-04-14 08:54:31 +00:00
|
|
|
) == _split_what((str, "123", fields(C).a, int, "abcd"))
|
2015-02-20 15:34:21 +00:00
|
|
|
|
|
|
|
|
2022-03-21 07:47:47 +00:00
|
|
|
class TestInclude:
|
2015-02-20 15:34:21 +00:00
|
|
|
"""
|
|
|
|
Tests for `include`.
|
|
|
|
"""
|
2018-06-10 17:40:07 +00:00
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
2023-08-20 10:03:53 +00:00
|
|
|
("incl", "value"),
|
2018-06-10 17:40:07 +00:00
|
|
|
[
|
|
|
|
((int,), 42),
|
|
|
|
((str,), "hello"),
|
|
|
|
((str, fields(C).a), 42),
|
|
|
|
((str, fields(C).b), "hello"),
|
2023-04-14 08:54:31 +00:00
|
|
|
(("a",), 42),
|
|
|
|
(("a",), "hello"),
|
|
|
|
(("a", str), 42),
|
|
|
|
(("a", fields(C).b), "hello"),
|
2018-06-10 17:40:07 +00:00
|
|
|
],
|
|
|
|
)
|
2015-02-20 15:34:21 +00:00
|
|
|
def test_allow(self, incl, value):
|
|
|
|
"""
|
2021-11-29 19:35:59 +00:00
|
|
|
Return True if a class or attribute is included.
|
2015-02-20 15:34:21 +00:00
|
|
|
"""
|
|
|
|
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
|
|
|
|
2018-06-10 17:40:07 +00:00
|
|
|
@pytest.mark.parametrize(
|
2023-08-20 10:03:53 +00:00
|
|
|
("incl", "value"),
|
2018-06-10 17:40:07 +00:00
|
|
|
[
|
|
|
|
((str,), 42),
|
|
|
|
((int,), "hello"),
|
|
|
|
((str, fields(C).b), 42),
|
|
|
|
((int, fields(C).b), "hello"),
|
2023-04-14 08:54:31 +00:00
|
|
|
(("b",), 42),
|
|
|
|
(("b",), "hello"),
|
|
|
|
(("b", str), 42),
|
|
|
|
(("b", fields(C).b), "hello"),
|
2018-06-10 17:40:07 +00:00
|
|
|
],
|
|
|
|
)
|
2015-02-20 15:34:21 +00:00
|
|
|
def test_drop_class(self, incl, value):
|
|
|
|
"""
|
2021-11-29 19:35:59 +00:00
|
|
|
Return False on non-included classes and attributes.
|
2015-02-20 15:34:21 +00:00
|
|
|
"""
|
|
|
|
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
|
|
|
|
|
|
|
|
2022-03-21 07:47:47 +00:00
|
|
|
class TestExclude:
|
2015-02-20 15:34:21 +00:00
|
|
|
"""
|
|
|
|
Tests for `exclude`.
|
|
|
|
"""
|
2018-06-10 17:40:07 +00:00
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
2023-08-20 10:03:53 +00:00
|
|
|
("excl", "value"),
|
2018-06-10 17:40:07 +00:00
|
|
|
[
|
|
|
|
((str,), 42),
|
|
|
|
((int,), "hello"),
|
|
|
|
((str, fields(C).b), 42),
|
|
|
|
((int, fields(C).b), "hello"),
|
2023-04-14 08:54:31 +00:00
|
|
|
(("b",), 42),
|
|
|
|
(("b",), "hello"),
|
|
|
|
(("b", str), 42),
|
|
|
|
(("b", fields(C).b), "hello"),
|
2018-06-10 17:40:07 +00:00
|
|
|
],
|
|
|
|
)
|
2015-02-20 15:34:21 +00:00
|
|
|
def test_allow(self, excl, value):
|
|
|
|
"""
|
2021-11-29 19:35:59 +00:00
|
|
|
Return True if class or attribute is not excluded.
|
2015-02-20 15:34:21 +00:00
|
|
|
"""
|
|
|
|
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
|
|
|
|
2018-06-10 17:40:07 +00:00
|
|
|
@pytest.mark.parametrize(
|
2023-08-20 10:03:53 +00:00
|
|
|
("excl", "value"),
|
2018-06-10 17:40:07 +00:00
|
|
|
[
|
|
|
|
((int,), 42),
|
|
|
|
((str,), "hello"),
|
|
|
|
((str, fields(C).a), 42),
|
|
|
|
((str, fields(C).b), "hello"),
|
2023-04-14 08:54:31 +00:00
|
|
|
(("a",), 42),
|
|
|
|
(("a",), "hello"),
|
|
|
|
(("a", str), 42),
|
|
|
|
(("a", fields(C).b), "hello"),
|
2018-06-10 17:40:07 +00:00
|
|
|
],
|
|
|
|
)
|
2015-02-20 15:34:21 +00:00
|
|
|
def test_drop_class(self, excl, value):
|
|
|
|
"""
|
2021-11-29 19:35:59 +00:00
|
|
|
Return True on non-excluded classes and attributes.
|
2015-02-20 15:34:21 +00:00
|
|
|
"""
|
|
|
|
e = exclude(*excl)
|
2016-09-10 17:14:34 +00:00
|
|
|
assert e(fields(C).a, value) is False
|