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'>>" )
def test__assert_expected__when_the_matcher_takes_no_args__prints_the_failure_reason( ): class MatcherWithNoArgs(Matcher): def __init__(self): super().__init__("some_matcher", lambda subject, *params: False) expectation = Expectation("to_meet_some_condition", MatcherWithNoArgs()) expect_expectation_to_fail_with_message( lambda: expectation.assert_expected("some-subject"), "Expected <'some-subject'> to meet some condition")
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'>" )
def test__assert_expected__when_the_matcher_match_fails__treats_it_as_not_matching( ): class BadMatcher(Matcher): def matches(self, subject): raise Exception("some error when matching") expectation = Expectation("to_meet_some_condition", BadMatcher('bad_matcher', equal_to_comparator)) expect_expectation_to_fail_with_message( lambda: expectation.assert_expected("some-subject"), "Expected <'some-subject'> to meet some condition but matcher raised <Exception: some error when matching>" )
def test__assert_expected__when_the_matcher_has_a_failure_reason__prints_the_failure_reason( ): class MatcherWithFailureReason(Matcher): def __init__(self): super().__init__("some_matcher", lambda subject, *params: False) def reason(self): return "it was not like that, yo" expectation = Expectation("to_meet_some_condition", MatcherWithFailureReason()) expect_expectation_to_fail_with_message( lambda: expectation.assert_expected("some-subject"), "Expected <'some-subject'> to meet some condition" " but it was not like that, yo")
def to_be_about(self, number, tolerance=0.001): expectation = Expectation( "to_be_about", about(number, tolerance), message_format="Expected <{subject}> to be about <{0}>") expectation.assert_expected(self.subject, number, tolerance)
def to_match_list(self, expected_list): expectation = Expectation("to_match_list", matches_list(expected_list)) expectation.assert_expected(self.subject, expected_list)
def not_to_contain(self, item_or_text): expectation = Expectation("to_contain", contains(item_or_text)).get_inverse() expectation.assert_expected(self.subject, item_or_text)
def not_to_be_none(self): expectation = Expectation( "to_be_none", is_none(), message_format="Expected <{subject}> to be None").get_inverse() expectation.assert_expected(self.subject)
def to_be_a(self, class_): Expectation("to_be_a", instance_of(class_)).assert_expected(self.subject, class_)
def to_contain(self, item_or_text): Expectation("to_contain", contains(item_or_text)).assert_expected( self.subject, item_or_text)
def not_to_have_length(self, length): Expectation("to_have_length", has_length(length)).get_inverse().assert_expected( self.subject, length)
def to_have_length(self, length): Expectation("to_have_length", has_length(length)).assert_expected(self.subject, length)
def not_to_be(self, expected): Expectation("to_be", equal_to(expected)).get_inverse().assert_expected( self.subject, expected)
def to_be(self, expected): Expectation("to_be", equal_to(expected)).assert_expected( self.subject, expected)
def unmatcherify(self, params, subject): return Expectation.unmatcherify(self, params, Spy.get_spy(subject))