attrs/tests/test_dark_magic.py

81 lines
1.8 KiB
Python
Raw Normal View History

2015-01-27 16:53:17 +00:00
# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, print_function
2015-01-29 11:20:17 +00:00
import pytest
2015-01-27 16:53:17 +00:00
import attr
from attr._compat import TYPE
2015-01-30 07:57:33 +00:00
from attr._make import Attribute, NOTHING
2015-01-27 16:53:17 +00:00
@attr.s
class C1(object):
2015-01-29 11:20:17 +00:00
x = attr.ib(validator=attr.validators.instance_of(int))
2015-01-27 22:03:42 +00:00
y = attr.ib()
2015-01-27 16:53:17 +00:00
foo = None
@attr.s()
class C2(object):
2015-01-29 21:32:41 +00:00
x = attr.ib(default=foo)
y = attr.ib(default=attr.Factory(list))
2015-01-27 16:53:17 +00:00
class TestDarkMagic(object):
"""
Integration tests.
"""
2015-01-29 20:55:25 +00:00
def test_fields(self):
2015-01-27 16:53:17 +00:00
"""
2015-01-29 20:55:25 +00:00
`attr.fields` works.
2015-01-27 16:53:17 +00:00
"""
assert [
Attribute(name="x", default=foo, validator=None),
Attribute(name="y", default=attr.Factory(list), validator=None),
2015-01-29 20:55:25 +00:00
] == attr.fields(C2)
2015-01-27 16:53:17 +00:00
def test_asdict(self):
2015-01-27 16:53:17 +00:00
"""
`attr.asdict` works.
2015-01-27 16:53:17 +00:00
"""
assert {
"x": 1,
"y": 2,
} == attr.asdict(C1(x=1, y=2))
2015-01-29 11:20:17 +00:00
def test_validator(self):
"""
`instance_of` raises `TypeError` on type mismatch.
"""
with pytest.raises(TypeError) as e:
C1("1", 2)
assert (
"'x' must be <{type} 'int'> (got '1' that is a <{type} "
"'str'>).".format(type=TYPE),
C1.x, int, "1",
) == e.value.args
def test_renaming(self):
"""
Private members are renamed but only in `__init__`.
"""
@attr.s
class C3(object):
_x = attr.ib()
assert "C3(_x=1)" == repr(C3(x=1))
2015-01-30 07:57:33 +00:00
def test_programmatic(self):
"""
`attr.make_class` works.
"""
PC = attr.make_class("PC", ["a", "b"])
assert [
Attribute(name="a", default=NOTHING, validator=None),
Attribute(name="b", default=NOTHING, validator=None),
] == attr.fields(PC)