attrs/tests/utils.py

80 lines
1.5 KiB
Python
Raw Normal View History

"""
Common helper functions for tests.
"""
from __future__ import absolute_import, division, print_function
from attr import Attribute
from attr._make import NOTHING, make_class
2018-06-10 17:40:07 +00:00
def simple_class(
cmp=False,
repr=False,
hash=False,
str=False,
slots=False,
frozen=False,
cache_hash=False,
2018-06-10 17:40:07 +00:00
):
"""
Return a new simple class.
"""
return make_class(
2018-06-10 17:40:07 +00:00
"C",
["a", "b"],
cmp=cmp,
repr=repr,
hash=hash,
init=True,
slots=slots,
str=str,
2017-02-19 08:51:43 +00:00
frozen=frozen,
cache_hash=cache_hash,
)
2018-06-10 17:40:07 +00:00
def simple_attr(
name,
default=NOTHING,
validator=None,
repr=True,
cmp=True,
hash=None,
init=True,
converter=None,
kw_only=False,
2018-06-10 17:40:07 +00:00
):
"""
Return an attribute with a name and no other bells and whistles.
"""
return Attribute(
2018-06-10 17:40:07 +00:00
name=name,
default=default,
validator=validator,
repr=repr,
cmp=cmp,
hash=hash,
init=init,
converter=converter,
kw_only=False,
)
class TestSimpleClass(object):
"""
Tests for the testing helper function `make_class`.
"""
2018-06-10 17:40:07 +00:00
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()