Beispiel #1
0
def instance_of(clazz):
    if is_matcher(clazz):
        return Matcher(
            "instance_of",
            lambda subject, *params: clazz.matches(subject.__class__), clazz)
    else:
        return Matcher("instance_of",
                       lambda subject, *params: isinstance(subject, clazz),
                       clazz)
Beispiel #2
0
 def __init__(self, *params):
     self.expected_type = params[0]
     self.inner_matcher = Matcher(
         "of_type",
         lambda exception, clazz: instance_of(clazz).matches(exception),
         self.expected_type)
     super().__init__(self.inner_matcher)
Beispiel #3
0
def test__assert_expected__when_the_matcher_takes_multiple_args__prints_the_failure_reason(
):
    expectation = Expectation(
        "to_meet_some_conditions_of",
        Matcher("some_matcher", lambda subject, *params: False))

    expect_expectation_to_fail_with_message(
        lambda: expectation.assert_expected("some-subject", "first-arg",
                                            222222, "third-arg"),
        "Expected <'some-subject'> to meet some conditions of <<'first-arg'>, <222222>, <'third-arg'>>"
    )
Beispiel #4
0
 def __init__(self, error_matcher, exception_format=None):
     if exception_format is None:
         exception_format = lambda e: repr(e)
     matcher_name = error_matcher.name
     name = "to_raise_error_" + matcher_name
     self.actual_exception = None
     self.error_description = matcher_name.replace("_", " ")
     super().__init__(name, Matcher(matcher_name, self.check_error),
                      self.message_format)
     self.error_matcher = error_matcher
     self.exception_format = exception_format
Beispiel #5
0
def test__assert_expected__when_the_matcher_comparator_fails__treats_it_as_not_matching(
):
    def comparator_that_raises(subject, *params):
        return subject.do_something() is True

    expectation = Expectation("to_meet_some_condition",
                              Matcher("some_matcher", comparator_that_raises))

    expect_expectation_to_fail_with_message(
        lambda: expectation.assert_expected("some-subject"),
        "Expected <'some-subject'> to meet some condition but comparator raised <AttributeError: 'str' object has no attribute 'do_something'>"
    )
Beispiel #6
0
def contains(item):
    def contains_comparator(subject, *params):
        if is_matcher(item):
            for candidate in subject:
                if item.matches(candidate):
                    return True
            return False
        else:
            try:
                return item in subject
            except TypeError:
                return False

    return Matcher("contains", contains_comparator, item)
 def __init__(self, *params):
     expected_message = params[0]
     message_matcher = Matcher(
         "with_message", lambda exception, message: equal_to(message).
         matches(str(exception)), expected_message)
     super().__init__(message_matcher, exception_format=lambda e: str(e))
Beispiel #8
0
 def __init__(self, number):
     Matcher.__init__(self, "has_length", self.comparator, number)
Beispiel #9
0
def about(number, tolerance=0.001):
    return Matcher(
        "about",
        lambda subject, *params: -tolerance < number - subject < tolerance,
        number)
Beispiel #10
0
def same_instance_as(value):
    return Matcher("same_instance_as", lambda a, b: a is b, value)
Beispiel #11
0
def at_most(number):
    return Matcher("at_most", lambda subject, *params: subject <= number,
                   number)
Beispiel #12
0
def between(lower, upper):
    return Matcher("between", lambda subject, *params: lower < subject < upper,
                   lower, upper)
Beispiel #13
0
def is_none():
    return Matcher("is_none", lambda subject, *params: subject is None, None)
Beispiel #14
0
def contained_in(collection):
    return Matcher("contained_in",
                   lambda subject, *params: subject in params[0], collection)
Beispiel #15
0
def anything():
    return Matcher("anything", lambda subject, *params: True)
Beispiel #16
0
def contains_text(text):
    return Matcher(
        "contains_text", lambda subject, *params: isinstance(subject, str) and
        params[0] in subject, text)
Beispiel #17
0
def match(regular_expression):
    return Matcher("match",
                   lambda subject, *params: re.search(params[0], subject),
                   regular_expression)