def starts_with_matches_when_actual_string_starts_with_value_passed_to_matcher():
    matcher = starts_with("ab")
    assert_equal(matched(), matcher.match("ab"))
    assert_equal(matched(), matcher.match("abc"))
    assert_equal(matched(), matcher.match("abcd"))
    assert_equal(unmatched("was 'a'"), matcher.match("a"))
    assert_equal(unmatched("was 'cab'"), matcher.match("cab"))
Example #2
0
def close_to_matches_when_actual_is_close_to_value_plus_delta():
    matcher = close_to(42, 1)
    assert_equal(matched(), matcher.match(43))
    assert_equal(matched(), matcher.match(42.5))
    assert_equal(matched(), matcher.match(42))
    assert_equal(matched(), matcher.match(41.5))
    assert_equal(matched(), matcher.match(41))
    assert_equal(unmatched("was 40 (2 away from 42)"), matcher.match(40))
def contains_string_matches_when_actual_string_contains_value_passed_to_matcher():
    matcher = contains_string("ab")
    assert_equal(matched(), matcher.match("ab"))
    assert_equal(matched(), matcher.match("abc"))
    assert_equal(matched(), matcher.match("abcd"))
    assert_equal(matched(), matcher.match("cabd"))
    assert_equal(matched(), matcher.match("cdab"))
    assert_equal(unmatched("was 'a'"), matcher.match("a"))
def close_to_matches_when_actual_is_close_to_value_plus_delta():
    matcher = close_to(42, 1)
    assert_equal(matched(), matcher.match(43))
    assert_equal(matched(), matcher.match(42.5))
    assert_equal(matched(), matcher.match(42))
    assert_equal(matched(), matcher.match(41.5))
    assert_equal(matched(), matcher.match(41))
    assert_equal(unmatched("was 40 (2 away from 42)"), matcher.match(40))
Example #5
0
def contains_string_matches_when_actual_string_contains_value_passed_to_matcher(
):
    matcher = contains_string("ab")
    assert_equal(matched(), matcher.match("ab"))
    assert_equal(matched(), matcher.match("abc"))
    assert_equal(matched(), matcher.match("abcd"))
    assert_equal(matched(), matcher.match("cabd"))
    assert_equal(matched(), matcher.match("cdab"))
    assert_equal(unmatched("was 'a'"), matcher.match("a"))
def close_to_matches_datetime_values():
    matcher = close_to(datetime(2018, 1, 17), timedelta(days=1))
    assert_equal(matched(), matcher.match(datetime(2018, 1, 18)))
    assert_equal(matched(), matcher.match(datetime(2018, 1, 17)))
    assert_equal(matched(), matcher.match(datetime(2018, 1, 16)))
    assert_equal(unmatched(
        "was datetime.datetime(2018, 1, 15, 0, 0) (datetime.timedelta(2) away from datetime.datetime(2018, 1, 17, 0, 0))"
    ),
         matcher.match(datetime(2018, 1, 15))
    )
Example #7
0
def close_to_matches_any_types_supporting_comparison_and_addition_and_subtraction(
):
    class Instant(object):
        def __init__(self, seconds_since_epoch):
            self.seconds_since_epoch = seconds_since_epoch

        def __sub__(self, other):
            if isinstance(other, Instant):
                return Interval(self.seconds_since_epoch -
                                other.seconds_since_epoch)
            else:
                return NotImplemented

        def __repr__(self):
            return "Instant({})".format(self.seconds_since_epoch)

    @functools.total_ordering
    class Interval(object):
        def __init__(self, seconds):
            self.seconds = seconds

        def __abs__(self):
            return Interval(abs(self.seconds))

        def __eq__(self, other):
            if isinstance(other, Interval):
                return self.seconds == other.seconds
            else:
                return NotImplemented

        def __lt__(self, other):
            if isinstance(other, Interval):
                return self.seconds < other.seconds
            else:
                return NotImplemented

        def __repr__(self):
            return "Interval({})".format(self.seconds)

    matcher = close_to(Instant(42), Interval(1))
    assert_equal(matched(), matcher.match(Instant(43)))
    assert_equal(matched(), matcher.match(Instant(42.5)))
    assert_equal(matched(), matcher.match(Instant(42)))
    assert_equal(matched(), matcher.match(Instant(41.5)))
    assert_equal(matched(), matcher.match(Instant(41)))
    assert_equal(
        unmatched("was Instant(40) (Interval(2) away from Instant(42))"),
        matcher.match(Instant(40)))
Example #8
0
def when_empty_iterable_is_expected_then_empty_iterable_matches():
    matcher = contains_exactly()

    assert_equal(
        matched(),
        matcher.match([])
    )
def when_empty_iterable_is_expected_then_empty_iterable_matches():
    matcher = is_sequence()

    assert_equal(
        matched(),
        matcher.match([])
    )
def matches_when_submatchers_all_match():
    matcher = any_of(
        has_attr("username", equal_to("bob")),
        has_attr("email_address", equal_to("*****@*****.**")),
    )
    
    assert_equal(matched(), matcher.match(User("bob", "*****@*****.**")))
def matches_when_properties_all_match():
    matcher = has_attrs(
        username=equal_to("bob"),
        email_address=equal_to("*****@*****.**"),
    )
    
    assert_equal(matched(), matcher.match(User("bob", "*****@*****.**")))
Example #12
0
def matches_when_properties_all_match():
    matcher = has_attrs(
        username=equal_to("bob"),
        email_address=equal_to("*****@*****.**"),
    )

    assert_equal(matched(), matcher.match(User("bob", "*****@*****.**")))
def when_no_elements_are_expected_then_empty_iterable_matches():
    matcher = includes()

    assert_equal(
        matched(),
        matcher.match([])
    )
def matches_when_any_submatchers_match():
    matcher = any_of(
        equal_to("bob"),
        equal_to("jim"),
    )
    
    assert_equal(
        matched(),
        matcher.match("bob"),
    )
Example #15
0
 def match(self, actual):
     if self._name not in actual:
         return unmatched("was missing value '{0}'".format(self._name))
     else:
         actual_value = actual.get(self._name)
         property_result = self._matcher.match(actual_value)
         if property_result.is_match:
             return matched()
         else:
             return unmatched("value '{0}' {1}".format(
                 self._name, property_result.explanation))
def matches_when_property_has_correct_value():
    assert_equal(matched(),
                 has_attr("username", equal_to("bob")).match(User("bob")))
Example #17
0
 def match(self, actual):
     for matcher in self._matchers:
         result = matcher.match(actual)
         if not result.is_match:
             return result
     return matched()
def matches_when_all_submatchers_match_one_item_with_no_items_leftover():
    matcher = includes(equal_to("apple"), equal_to("banana"))

    assert_equal(matched(), matcher.match(["apple", "banana"]))
    assert_equal(matched(), matcher.match(["apple", "banana", "coconut"]))
def matches_when_there_are_extra_items():
    matcher = includes(equal_to("apple"))

    assert_equal(matched(), matcher.match(["coconut", "apple"]))
def matches_when_expected_exception_is_raised():
    def raise_key_error():
        raise KeyError()

    matcher = raises(is_instance(KeyError))
    assert_equal(matched(), matcher.match(raise_key_error))
def greater_than_matches_when_actual_is_greater_than_value():
    matcher = greater_than(42)
    assert_equal(matched(), matcher.match(43))
    assert_equal(unmatched("was 42"), matcher.match(42))
    assert_equal(unmatched("was 41"), matcher.match(41))
def less_than_or_equal_to_matches_when_actual_is_less_than_or_equal_to_value():
    matcher = less_than_or_equal_to(42)
    assert_equal(matched(), matcher.match(41))
    assert_equal(matched(), matcher.match(42))
    assert_equal(unmatched("was 43"), matcher.match(43))
def matches_when_all_submatchers_match_one_item_with_no_items_leftover():
    matcher = contains_exactly(equal_to("apple"), equal_to("banana"))
    
    assert_equal(matched(), matcher.match(["banana", "apple"]))
def values_are_coerced_to_matchers():
    matcher = is_mapping({"a": 1, "b": 2})
    assert_equal(matched(), matcher.match({"a": 1, "b": 2}))
def matches_when_iterable_is_empty():
    matcher = all_elements(equal_to("apple"))

    assert_equal(matched(), matcher.match([]))
def when_empty_iterable_is_expected_then_empty_iterable_matches():
    matcher = is_sequence()

    assert_equal(matched(), matcher.match([]))
def matches_when_property_has_correct_value():
    assert_equal(matched(), has_attr("username", equal_to("bob")).match(User("bob")))
Example #28
0
def matches_when_feature_has_correct_value():
    matcher = has_feature("name", lambda user: user.username, equal_to("bob"))
    assert_equal(matched(), matcher.match(User("bob")))
Example #29
0
def matches_when_there_are_extra_keys():
    matcher = mapping_includes({"a": equal_to(1)})
    assert_equal(matched(), matcher.match({"a": 1, "b": 1, "c": 1}))
def close_to_can_be_used_in_composite_matcher():
    matcher = is_sequence("a", "b", close_to(42, 1))
    assert_equal(matched(), matcher.match(("a", "b", 42)))
def matches_when_all_items_in_iterable_match():
    matcher = all_elements(equal_to("apple"))

    assert_equal(matched(), matcher.match(["apple", "apple"]))
Example #32
0
def matches_when_all_submatchers_match_one_item_with_no_items_leftover():
    matcher = contains_exactly(equal_to("apple"), equal_to("banana"))

    assert_equal(matched(), matcher.match(["banana", "apple"]))
Example #33
0
def matches_when_expected_exception_is_raised():
    def raise_key_error():
        raise KeyError()

    matcher = raises(is_instance(KeyError))
    assert_equal(matched(), matcher.match(raise_key_error))
def matches_when_keys_and_values_match():
    matcher = is_mapping({"a": equal_to(1), "b": equal_to(2)})
    assert_equal(matched(), matcher.match({"a": 1, "b": 2}))
def matches_when_value_is_instance_of_class():
    assert_equal(matched(), is_instance(int).match(1))
Example #36
0
def close_to_can_be_used_in_composite_matcher():
    matcher = is_sequence("a", "b", close_to(42, 1))
    assert_equal(matched(), matcher.match(("a", "b", 42)))
def matches_anything():
    assert_equal(matched(), anything.match(4))
    assert_equal(matched(), anything.match(None))
    assert_equal(matched(), anything.match("Hello"))
Example #38
0
def matches_when_negated_matcher_does_not_match():
    assert_equal(matched(), not_(equal_to(1)).match(2))
Example #39
0
def matches_when_values_are_equal():
    assert_equal(matched(), equal_to(1).match(1))