attrs/tests/__init__.py

61 lines
1.6 KiB
Python
Raw Normal View History

2015-01-29 11:20:17 +00:00
from __future__ import absolute_import, division, print_function
import string
from hypothesis import strategies as st
from attr import Attribute, ib
2015-01-30 16:48:34 +00:00
from attr._make import NOTHING, make_class
2015-01-29 11:20:17 +00:00
2015-01-30 16:48:34 +00:00
2016-03-14 02:02:13 +00:00
def simple_class(cmp=False, repr=False, hash=False, slots=False):
2015-01-30 16:48:34 +00:00
"""
Return a new simple class.
"""
return make_class(
"C", ["a", "b"],
2016-03-14 02:02:13 +00:00
cmp=cmp, repr=repr, hash=hash, init=True, slots=slots
2015-01-30 16:48:34 +00:00
)
2015-02-20 12:29:47 +00:00
def simple_attr(name, default=NOTHING, validator=None, repr=True,
cmp=True, hash=True, init=True):
2015-01-29 11:20:17 +00:00
"""
Return an attribute with a name and no other bells and whistles.
"""
2015-01-30 16:48:34 +00:00
return Attribute(
2015-02-20 12:29:47 +00:00
name=name, default=default, validator=validator, repr=repr,
cmp=cmp, hash=hash, init=init
2015-01-30 16:48:34 +00:00
)
class TestSimpleClass(object):
"""
Tests for the testing helper function `make_class`.
"""
def test_returns_class(self):
"""
Returns a class object.
"""
assert type is simple_class().__class__
def returns_distinct_classes(self):
"""
Each call returns a completely new class.
"""
assert simple_class() is not simple_class()
def create_class(attrs):
# What if we get more than len(string.ascii_lowercase) attributes?
return make_class('HypClass', dict(zip(string.ascii_lowercase, attrs)))
bare_attrs = st.just(ib(default=None))
int_attrs = st.integers().map(lambda i: ib(default=i))
str_attrs = st.text().map(lambda s: ib(default=s))
float_attrs = st.floats().map(lambda f: ib(default=f))
simple_attrs = st.one_of(bare_attrs, int_attrs, str_attrs, float_attrs)
simple_classes = st.lists(simple_attrs).map(create_class)