2015-01-29 11:20:17 +00:00
|
|
|
"""
|
|
|
|
Tests for `attr.validators`.
|
|
|
|
"""
|
|
|
|
|
|
|
|
from __future__ import absolute_import, division, print_function
|
|
|
|
|
|
|
|
import pytest
|
2015-01-29 18:04:23 +00:00
|
|
|
import zope.interface
|
2015-01-29 11:20:17 +00:00
|
|
|
|
Support optional values through a new validator.
Unfortunately, I'm unable to run tests locally b/c of severe dependency
hell. I am unable to get tox, detox, python setup.py test, and other
means of running tests to work at all. So, since I'm blocked on a
project by the lack of optional validator support, I am going to close
my eyes, look away, and pull the trigger, and hope I hit the target.
If someone who has a working Python configuration can please be kind
enough to let me know if my changes fail any tests, I'd be very
appreciative.
Even better, I'd love to know why a stock Python distribution with
dependencies installed is incapable of running the tests for attrs.
However, this isn't the right forum to answer that question.
(This PR has the 17+ debugging commits cleaned up.)
2015-07-07 22:27:03 +00:00
|
|
|
from attr.validators import instance_of, provides, optional
|
2015-01-29 15:24:49 +00:00
|
|
|
from attr._compat import TYPE
|
2016-08-15 13:27:13 +00:00
|
|
|
|
|
|
|
from .utils import simple_attr
|
2015-01-29 11:20:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TestInstanceOf(object):
|
|
|
|
"""
|
|
|
|
Tests for `instance_of`.
|
|
|
|
"""
|
|
|
|
def test_success(self):
|
|
|
|
"""
|
|
|
|
Nothing happens if types match.
|
|
|
|
"""
|
|
|
|
v = instance_of(int)
|
2015-02-09 12:16:56 +00:00
|
|
|
v(None, simple_attr("test"), 42)
|
2015-01-29 11:20:17 +00:00
|
|
|
|
|
|
|
def test_subclass(self):
|
|
|
|
"""
|
|
|
|
Subclasses are accepted too.
|
|
|
|
"""
|
|
|
|
v = instance_of(int)
|
2015-02-09 12:16:56 +00:00
|
|
|
# yep, bools are a subclass of int :(
|
|
|
|
v(None, simple_attr("test"), True)
|
2015-01-29 11:20:17 +00:00
|
|
|
|
|
|
|
def test_fail(self):
|
|
|
|
"""
|
|
|
|
Raises `TypeError` on wrong types.
|
|
|
|
"""
|
|
|
|
v = instance_of(int)
|
|
|
|
a = simple_attr("test")
|
|
|
|
with pytest.raises(TypeError) as e:
|
2015-02-09 12:16:56 +00:00
|
|
|
v(None, a, "42")
|
2015-01-29 11:20:17 +00:00
|
|
|
assert (
|
|
|
|
"'test' must be <{type} 'int'> (got '42' that is a <{type} "
|
|
|
|
"'str'>).".format(type=TYPE),
|
|
|
|
a, int, "42",
|
|
|
|
|
|
|
|
) == e.value.args
|
|
|
|
|
|
|
|
def test_repr(self):
|
|
|
|
"""
|
|
|
|
Returned validator has a useful `__repr__`.
|
|
|
|
"""
|
|
|
|
v = instance_of(int)
|
|
|
|
assert (
|
|
|
|
"<instance_of validator for type <{type} 'int'>>"
|
|
|
|
.format(type=TYPE)
|
|
|
|
) == repr(v)
|
2015-01-29 18:04:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
class IFoo(zope.interface.Interface):
|
|
|
|
"""
|
|
|
|
An interface.
|
|
|
|
"""
|
|
|
|
def f():
|
|
|
|
"""
|
|
|
|
A function called f.
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
class TestProvides(object):
|
|
|
|
"""
|
|
|
|
Tests for `provides`.
|
|
|
|
"""
|
|
|
|
def test_success(self):
|
|
|
|
"""
|
|
|
|
Nothing happens if value provides requested interface.
|
|
|
|
"""
|
|
|
|
@zope.interface.implementer(IFoo)
|
|
|
|
class C(object):
|
|
|
|
def f(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
v = provides(IFoo)
|
2015-02-09 12:16:56 +00:00
|
|
|
v(None, simple_attr("x"), C())
|
2015-01-29 18:04:23 +00:00
|
|
|
|
|
|
|
def test_fail(self):
|
|
|
|
"""
|
|
|
|
Raises `TypeError` if interfaces isn't provided by value.
|
|
|
|
"""
|
|
|
|
value = object()
|
|
|
|
a = simple_attr("x")
|
|
|
|
|
|
|
|
v = provides(IFoo)
|
|
|
|
with pytest.raises(TypeError) as e:
|
2015-02-09 12:16:56 +00:00
|
|
|
v(None, a, value)
|
2015-01-29 18:04:23 +00:00
|
|
|
assert (
|
|
|
|
"'x' must provide {interface!r} which {value!r} doesn't."
|
|
|
|
.format(interface=IFoo, value=value),
|
|
|
|
a, IFoo, value,
|
|
|
|
) == e.value.args
|
2015-01-29 18:41:58 +00:00
|
|
|
|
|
|
|
def test_repr(self):
|
|
|
|
"""
|
|
|
|
Returned validator has a useful `__repr__`.
|
|
|
|
"""
|
|
|
|
v = provides(IFoo)
|
|
|
|
assert (
|
|
|
|
"<provides validator for interface {interface!r}>"
|
|
|
|
.format(interface=IFoo)
|
|
|
|
) == repr(v)
|
Support optional values through a new validator.
Unfortunately, I'm unable to run tests locally b/c of severe dependency
hell. I am unable to get tox, detox, python setup.py test, and other
means of running tests to work at all. So, since I'm blocked on a
project by the lack of optional validator support, I am going to close
my eyes, look away, and pull the trigger, and hope I hit the target.
If someone who has a working Python configuration can please be kind
enough to let me know if my changes fail any tests, I'd be very
appreciative.
Even better, I'd love to know why a stock Python distribution with
dependencies installed is incapable of running the tests for attrs.
However, this isn't the right forum to answer that question.
(This PR has the 17+ debugging commits cleaned up.)
2015-07-07 22:27:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TestOptional(object):
|
|
|
|
"""
|
|
|
|
Tests for `optional`.
|
|
|
|
"""
|
|
|
|
def test_success_with_type(self):
|
|
|
|
"""
|
|
|
|
Nothing happens if types match.
|
|
|
|
"""
|
|
|
|
v = optional(instance_of(int))
|
|
|
|
v(None, simple_attr("test"), 42)
|
|
|
|
|
|
|
|
def test_success_with_none(self):
|
|
|
|
"""
|
|
|
|
Nothing happens if None.
|
|
|
|
"""
|
|
|
|
v = optional(instance_of(int))
|
|
|
|
v(None, simple_attr("test"), None)
|
|
|
|
|
|
|
|
def test_fail(self):
|
|
|
|
"""
|
|
|
|
Raises `TypeError` on wrong types.
|
|
|
|
"""
|
|
|
|
v = optional(instance_of(int))
|
|
|
|
a = simple_attr("test")
|
|
|
|
with pytest.raises(TypeError) as e:
|
|
|
|
v(None, a, "42")
|
|
|
|
assert (
|
|
|
|
"'test' must be <{type} 'int'> (got '42' that is a <{type} "
|
|
|
|
"'str'>).".format(type=TYPE),
|
|
|
|
a, int, "42",
|
|
|
|
|
|
|
|
) == e.value.args
|
|
|
|
|
|
|
|
def test_repr(self):
|
|
|
|
"""
|
|
|
|
Returned validator has a useful `__repr__`.
|
|
|
|
"""
|
|
|
|
v = optional(instance_of(int))
|
|
|
|
assert (
|
|
|
|
("<optional validator for <instance_of validator for type "
|
|
|
|
"<{type} 'int'>> or None>")
|
|
|
|
.format(type=TYPE)
|
|
|
|
) == repr(v)
|