Skip to content

Commit

Permalink
test validate_satisfied_by() and use str() in error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
cosmicexplorer committed Feb 6, 2019
1 parent 9d3a319 commit bff5668
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 5 deletions.
6 changes: 3 additions & 3 deletions src/python/pants/util/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ def satisfied_by(self, obj):

def make_type_constraint_error(self, obj, constraint):
return TypeConstraintError(
"value {!r} (with type {!r}) must satisfy this type constraint: {!r}."
"value {!r} (with type {!r}) must satisfy this type constraint: {}."
.format(obj, type(obj).__name__, constraint))

# TODO: disallow overriding this method with some form of mixin/decorator along with datatype
Expand Down Expand Up @@ -447,7 +447,7 @@ def satisfied_by(self, obj):

def make_collection_type_constraint_error(self, base_obj, el):
base_error = self.make_type_constraint_error(el, self._constraint)
return TypeConstraintError("in wrapped constraint {!r} matching iterable object {!r}: {}"
return TypeConstraintError("in wrapped constraint {} matching iterable object {}: {}"
.format(self, base_obj, base_error))

def validate_satisfied_by(self, obj):
Expand All @@ -459,7 +459,7 @@ def validate_satisfied_by(self, obj):

base_iterable_error = self.make_type_constraint_error(obj, self._iterable_constraint)
raise TypeConstraintError(
"in wrapped constraint {!r}: {}".format(self, base_iterable_error))
"in wrapped constraint {}: {}".format(self, base_iterable_error))

def __hash__(self):
return hash((type(self), self._constraint))
Expand Down
46 changes: 44 additions & 2 deletions tests/python/pants_test/util/test_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,20 @@
from future.utils import PY2, PY3, text_type

from pants.util.objects import (Exactly, SubclassesOf, SuperclassesOf, TypeCheckError,
TypedCollection, TypedDatatypeInstanceConstructionError, datatype,
enum)
TypeConstraintError, TypedCollection,
TypedDatatypeInstanceConstructionError, datatype, enum)
from pants_test.test_base import TestBase


class TypeConstraintTestBase(TestBase):
class A(object):

def __repr__(self):
return '{}()'.format(type(self).__name__)

def __str__(self):
return '(str form): {}'.format(repr(self))

def __eq__(self, other):
return type(self) == type(other)

Expand Down Expand Up @@ -62,6 +68,14 @@ def test_multiple(self):
self.assertFalse(superclasses_of_a_or_b.satisfied_by(self.BPrime()))
self.assertFalse(superclasses_of_a_or_b.satisfied_by(self.C()))

def test_validate(self):
superclasses_of_a_or_b = SuperclassesOf(self.A, self.B)
self.assertEqual(self.A(), superclasses_of_a_or_b.validate_satisfied_by(self.A()))
self.assertEqual(self.B(), superclasses_of_a_or_b.validate_satisfied_by(self.B()))
with self.assertRaisesRegexp(TypeConstraintError,
re.escape("value C() (with type 'C') must satisfy this type constraint: SuperclassesOf(A or B).")):
superclasses_of_a_or_b.validate_satisfied_by(self.C())


class ExactlyTest(TypeConstraintTestBase):
def test_none(self):
Expand Down Expand Up @@ -99,6 +113,14 @@ def test_checking_via_bare_type(self):
self.assertTrue(Exactly(self.B).satisfied_by_type(self.B))
self.assertFalse(Exactly(self.B).satisfied_by_type(self.C))

def test_validate(self):
exactly_a_or_b = Exactly(self.A, self.B)
self.assertEqual(self.A(), exactly_a_or_b.validate_satisfied_by(self.A()))
self.assertEqual(self.B(), exactly_a_or_b.validate_satisfied_by(self.B()))
with self.assertRaisesRegexp(TypeConstraintError,
re.escape("value C() (with type 'C') must satisfy this type constraint: Exactly(A or B).")):
exactly_a_or_b.validate_satisfied_by(self.C())


class SubclassesOfTest(TypeConstraintTestBase):
def test_none(self):
Expand Down Expand Up @@ -128,6 +150,15 @@ def test_multiple(self):
self.assertFalse(subclasses_of_b_or_c.satisfied_by(self.BPrime()))
self.assertFalse(subclasses_of_b_or_c.satisfied_by(self.A()))

def test_validate(self):
subclasses_of_a_or_b = SubclassesOf(self.A, self.B)
self.assertEqual(self.A(), subclasses_of_a_or_b.validate_satisfied_by(self.A()))
self.assertEqual(self.B(), subclasses_of_a_or_b.validate_satisfied_by(self.B()))
self.assertEqual(self.C(), subclasses_of_a_or_b.validate_satisfied_by(self.C()))
with self.assertRaisesRegexp(TypeConstraintError,
re.escape("value 1 (with type 'int') must satisfy this type constraint: SubclassesOf(A or B).")):
subclasses_of_a_or_b.validate_satisfied_by(1)


class TypedCollectionTest(TypeConstraintTestBase):
def test_str_and_repr(self):
Expand Down Expand Up @@ -159,6 +190,17 @@ def test_no_complex_sub_constraint(self):
"constraint for collection must be a TypeOnlyConstraint! was: {}".format(sub_collection))):
TypedCollection(sub_collection)

def test_validate(self):
collection_exactly_a_or_b = TypedCollection(Exactly(self.A, self.B))
self.assertEqual([self.A()], collection_exactly_a_or_b.validate_satisfied_by([self.A()]))
self.assertEqual([self.B()], collection_exactly_a_or_b.validate_satisfied_by([self.B()]))
with self.assertRaisesRegexp(TypeConstraintError,
re.escape("in wrapped constraint TypedCollection(Exactly(A or B)): value A() (with type 'A') must satisfy this type constraint: SubclassesOf(Iterable).")):
collection_exactly_a_or_b.validate_satisfied_by(self.A())
with self.assertRaisesRegexp(TypeConstraintError,
re.escape("in wrapped constraint TypedCollection(Exactly(A or B)) matching iterable object [C()]: value C() (with type 'C') must satisfy this type constraint: Exactly(A or B).")):
collection_exactly_a_or_b.validate_satisfied_by([self.C()])


class ExportedDatatype(datatype(['val'])):
pass
Expand Down

0 comments on commit bff5668

Please sign in to comment.