attrs/tests/test_funcs.py

100 lines
1.9 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
import pytest
2015-01-28 10:15:40 +00:00
from attr._funcs import ls, has, to_dict
2015-01-27 16:53:17 +00:00
from attr._make import (
Attribute,
_make_attr,
s,
)
@s
class C(object):
x = _make_attr()
y = _make_attr()
class TestLs(object):
2015-01-28 10:15:40 +00:00
"""
Tests for `ls`.
"""
2015-01-27 16:53:17 +00:00
def test_instance(self):
"""
Works also on instances of classes.
"""
assert ls(C) == ls(C(1, 2))
def test_handler_non_attrs_class(self):
"""
Raises `TypeError` if passed a non-attrs instance.
"""
with pytest.raises(TypeError) as e:
ls(object)
assert (
"{o!r} is not an attrs-decorated class.".format(o=object)
) == e.value.args[0]
def test_ls(self):
"""
Returns a list of `Attribute`.
"""
assert all(isinstance(a, Attribute) for a in ls(C))
class TestToDict(object):
2015-01-28 10:15:40 +00:00
"""
Tests for `to_dict`.
"""
2015-01-27 16:53:17 +00:00
def test_shallow(self):
"""
Shallow to_dict returns correct dict.
"""
assert {
"x": 1,
"y": 2,
} == to_dict(C(x=1, y=2), False)
def test_recurse(self):
"""
Deep to_dict returns correct dict.
"""
assert {
"x": {"x": 1, "y": 2},
"y": {"x": 3, "y": 4},
} == to_dict(C(
C(1, 2),
C(3, 4),
))
2015-01-28 10:15:40 +00:00
class TestHas(object):
"""
Tests for `has`.
"""
def test_positive(self):
"""
Returns `True` on decorated classes.
"""
assert has(C)
def test_positive_empty(self):
"""
Returns `True` on decorated classes even if there are no attributes.
"""
@s
class D(object):
pass
assert has(D)
def test_negative(self):
"""
Returns `False` on non-decorated classes.
"""
assert not has(object)