Source code for pyspec.expectations

from .matchers import *


[docs]class ExpectationNotMetError(AssertionError): """Represents a failed expectation.""" def __init__(self, message): self.message = message def __str__(self): return self.message
[docs]class PositiveHandler(object): """Used to resolve match of *actual* against *matcher* and propogate a failure if it does not.""" def __init__(self, matcher, actual, *args, **kwargs): self.actual = actual self.matcher = matcher self.args = args self.kwargs = kwargs
[docs] def resolve(self): """Raises an ``ExpectationNotMetError`` error with *matcher*'s ``failure_message`` if *matcher* does not match *actual*.""" if not self.matcher.matches(self.actual, *self.args, **self.kwargs): self.handle_failure()
def handle_failure(self): raise ExpectationNotMetError(self.matcher.failure_message)
[docs]class NegativeHandler(object): """Used to resolve match of actual against a matcher and propogate a failure if it does.""" def __init__(self, matcher, actual, *args, **kwargs): self.actual = actual self.matcher = matcher self.args = args self.kwargs = kwargs
[docs] def resolve(self): """Raises an ``ExpectationNotMetError`` error with *matcher*'s ``failure_message_when_negated`` if *matcher* matches *actual*.""" if self.matcher.matches(self.actual, *self.args, **self.kwargs): self.handle_failure()
def handle_failure(self): raise ExpectationNotMetError(self.matcher.failure_message_when_negated)
[docs]class Target(object): """Represents a value against which expectations may be tested.""" def __init__(self, target, *args, **kwargs): self.target = target self.args = args self.kwargs = kwargs
[docs] def to(self, matcher): """Checks the positive case of an expectation being met.""" PositiveHandler( matcher, self.target, *self.args, **self.kwargs).resolve()
[docs] def not_to(self, matcher): """Checks the negative case of an expectation being met.""" NegativeHandler( matcher, self.target, *self.args, **self.kwargs).resolve()
[docs]def expect(target, *args, **kwargs): """Returns a :class:`Target` to test expectations against.""" return Target(target, *args, **kwargs)
[docs]def eq(expected): """Tests equality of expected and actual.""" return EqualityMatcher(expected)
[docs]def be(expected): """Tests identify of expected and actual.""" return IdentityMatcher(expected)
[docs]def be_gt(expected): """Tests that *actual* is greater than *expected*""" return GreaterThanMatcher(expected)
[docs]def be_lt(expected): """Tests that *actual* is less than *expected*""" return LessThanMatcher(expected)
[docs]def be_gte(expected): """Tests that *actual* is greater than or equal to *expected*""" return GreaterThanOrEqualMatcher(expected)
def be_lte(expected): return LessThanOrEqualMatcher(expected) """Tests that *actual* is less than or equal to *expected*"""
[docs]def be_within(delta): """Tests that *actual* is within *delta* of *expected""" return WithinDeltaMatcher(delta)
[docs]def match(pattern): """Tests that *actual* matches *pattern*""" return RegexMatcher(pattern)
[docs]def be_an_instance_of(class_): """Tests that *actual* is an instance of *expected* or a descendant thereof""" return InstanceOfMatcher(class_)
[docs]def be_of_type(type_): """Tests that *actual* is specifically an instance of *expected*""" return OfTypeMatcher(type_)
[docs]def include(*expecteds): """Tests that *actual* is in *expected*""" return InclusionMatcher(expecteds)
[docs]def be_truthy(): """Tests that *actual* is truthy""" return TruthyMatcher(None)
[docs]def be_falsy(): """Tests that *actual* is falsy""" return FalsyMatcher(None)
[docs]def raise_error(*expecteds): """Tests that calling *actual* raises error""" return RaiseErrorMatcher(expecteds)