예제 #1
0
파일: matchers.py 프로젝트: Avvir/pyne
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)
예제 #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)
예제 #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'>>"
    )
예제 #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
예제 #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'>"
    )
예제 #6
0
파일: matchers.py 프로젝트: Avvir/pyne
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)
예제 #7
0
 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))
예제 #8
0
 def __init__(self, number):
     Matcher.__init__(self, "has_length", self.comparator, number)
예제 #9
0
파일: matchers.py 프로젝트: Avvir/pyne
def about(number, tolerance=0.001):
    return Matcher(
        "about",
        lambda subject, *params: -tolerance < number - subject < tolerance,
        number)
예제 #10
0
파일: matchers.py 프로젝트: Avvir/pyne
def same_instance_as(value):
    return Matcher("same_instance_as", lambda a, b: a is b, value)
예제 #11
0
파일: matchers.py 프로젝트: Avvir/pyne
def at_most(number):
    return Matcher("at_most", lambda subject, *params: subject <= number,
                   number)
예제 #12
0
파일: matchers.py 프로젝트: Avvir/pyne
def between(lower, upper):
    return Matcher("between", lambda subject, *params: lower < subject < upper,
                   lower, upper)
예제 #13
0
파일: matchers.py 프로젝트: Avvir/pyne
def is_none():
    return Matcher("is_none", lambda subject, *params: subject is None, None)
예제 #14
0
파일: matchers.py 프로젝트: Avvir/pyne
def contained_in(collection):
    return Matcher("contained_in",
                   lambda subject, *params: subject in params[0], collection)
예제 #15
0
파일: matchers.py 프로젝트: Avvir/pyne
def anything():
    return Matcher("anything", lambda subject, *params: True)
예제 #16
0
파일: matchers.py 프로젝트: Avvir/pyne
def contains_text(text):
    return Matcher(
        "contains_text", lambda subject, *params: isinstance(subject, str) and
        params[0] in subject, text)
예제 #17
0
파일: matchers.py 프로젝트: Avvir/pyne
def match(regular_expression):
    return Matcher("match",
                   lambda subject, *params: re.search(params[0], subject),
                   regular_expression)