Ejemplo n.º 1
0
    def test_underscores(self):
        """
        The argument names in `__init__` are without leading and trailing
        underscores.
        """
        class C(object):
            __attrs_attrs__ = [simple_attr("_private")]

        C = _add_init(C, False)
        i = C(private=42)
        assert 42 == i._private
Ejemplo n.º 2
0
    def test_underscores(self):
        """
        The argument names in `__init__` are without leading and trailing
        underscores.
        """
        class C(object):
            __attrs_attrs__ = [simple_attr("_private")]

        C = _add_init(C)
        i = C(private=42)
        assert 42 == i._private
Ejemplo n.º 3
0
    def test_default(self):
        """
        If a default value is present, it's used as fallback.
        """
        class C(object):
            __attrs_attrs__ = [
                simple_attr(name="a", default=2),
                simple_attr(name="b", default="hallo"),
                simple_attr(name="c", default=None),
            ]

        C = _add_init(C, False)
        i = C()
        assert 2 == i.a
        assert "hallo" == i.b
        assert None is i.c
    def test_factory(self):
        """
        If a default factory is present, it's used as fallback.
        """
        class D(object):
            pass

        class C(object):
            __attrs_attrs__ = [
                simple_attr(name="a", default=Factory(list)),
                simple_attr(name="b", default=Factory(D)),
            ]
        C = _add_init(C, False)
        i = C()
        assert [] == i.a
        assert isinstance(i.b, D)
Ejemplo n.º 5
0
    def test_factory(self):
        """
        If a default factory is present, it's used as fallback.
        """
        class D(object):
            pass

        class C(object):
            __attrs_attrs__ = [
                simple_attr(name="a", default=Factory(list)),
                simple_attr(name="b", default=Factory(D)),
            ]
        C = _add_init(C)
        i = C()
        assert [] == i.a
        assert isinstance(i.b, D)
Ejemplo n.º 6
0
    def test_default(self):
        """
        If a default value is present, it's used as fallback.
        """
        class C(object):
            __attrs_attrs__ = [
                simple_attr(name="a", default=2),
                simple_attr(name="b", default="hallo"),
                simple_attr(name="c", default=None),
            ]

        C = _add_init(C)
        i = C()
        assert 2 == i.a
        assert "hallo" == i.b
        assert None is i.c
Ejemplo n.º 7
0
CmpC = simple_class(cmp=True)
CmpCSlots = simple_class(cmp=True, slots=True)
ReprC = simple_class(repr=True)
ReprCSlots = simple_class(repr=True, slots=True)

# HashC is hashable by explicit definition while HashCSlots is hashable
# implicitly.
HashC = simple_class(hash=True)
HashCSlots = simple_class(hash=None, cmp=True, frozen=True, slots=True)


class InitC(object):
    __attrs_attrs__ = [simple_attr("a"), simple_attr("b")]


InitC = _add_init(InitC, False)


class TestAddCmp(object):
    """
    Tests for `_add_cmp`.
    """
    @given(booleans())
    def test_cmp(self, slots):
        """
        If `cmp` is False, ignore that attribute.
        """
        C = make_class("C", {
            "a": attr.ib(cmp=False),
            "b": attr.ib()
        },
Ejemplo n.º 8
0
)
from attr.validators import instance_of

CmpC = simple_class(cmp=True)
CmpCSlots = simple_class(cmp=True, slots=True)
ReprC = simple_class(repr=True)
ReprCSlots = simple_class(repr=True, slots=True)
HashC = simple_class(hash=True)
HashCSlots = simple_class(hash=True, slots=True)


class InitC(object):
    __attrs_attrs__ = [simple_attr("a"), simple_attr("b")]


InitC = _add_init(InitC)


class TestAddCmp(object):
    """
    Tests for `_add_cmp`.
    """
    @given(booleans())
    def test_cmp(self, slots):
        """
        If `cmp` is False, ignore that attribute.
        """
        C = make_class("C", {"a": attr(cmp=False), "b": attr()}, slots=slots)

        assert C(1, 2) == C(2, 2)
Ejemplo n.º 9
0
    _add_repr,
    attr,
    make_class,
)
from attr.validators import instance_of


CmpC = simple_class(cmp=True)
ReprC = simple_class(repr=True)
HashC = simple_class(hash=True)


class InitC(object):
    __attrs_attrs__ = [simple_attr("a"), simple_attr("b")]

InitC = _add_init(InitC)


class TestAddCmp(object):
    """
    Tests for `_add_cmp`.
    """
    def test_cmp(self):
        """
        If `cmp` is False, ignore that attribute.
        """
        C = make_class("C", {"a": attr(cmp=False), "b": attr()})

        assert C(1, 2) == C(2, 2)

    def test_equal(self):
Ejemplo n.º 10
0
CmpC = simple_class(cmp=True)
CmpCSlots = simple_class(cmp=True, slots=True)
ReprC = simple_class(repr=True)
ReprCSlots = simple_class(repr=True, slots=True)

# HashC is hashable by explicit definition while HashCSlots is hashable
# implicitly.
HashC = simple_class(hash=True)
HashCSlots = simple_class(hash=None, cmp=True, frozen=True, slots=True)


class InitC(object):
    __attrs_attrs__ = [simple_attr("a"), simple_attr("b")]


InitC = _add_init(InitC, False)


class TestAddCmp(object):
    """
    Tests for `_add_cmp`.
    """
    @given(booleans())
    def test_cmp(self, slots):
        """
        If `cmp` is False, ignore that attribute.
        """
        C = make_class("C", {"a": attr(cmp=False), "b": attr()}, slots=slots)

        assert C(1, 2) == C(2, 2)