Add explicit test for shortcircuiting behaviour of combined matchers

This commit is contained in:
Martin Hořeňovský
2020-06-14 21:48:08 +02:00
parent b74996a29c
commit 0e77adee05
10 changed files with 177 additions and 8 deletions

View File

@@ -622,6 +622,47 @@ namespace { namespace MatchersTests {
REQUIRE_THROWS_MATCHES(throwsSpecialException(2), SpecialException, Message("SpecialException::what"));
}
struct CheckedTestingMatcher : Catch::Matchers::MatcherBase<int> {
mutable bool matchCalled = false;
bool matchSucceeds = false;
bool match(int const&) const override {
matchCalled = true;
return matchSucceeds;
}
std::string describe() const override {
return "CheckedTestingMatcher set to " + (matchSucceeds ? std::string("succeed") : std::string("fail"));
}
};
TEST_CASE("Composed matchers shortcircuit", "[matchers][composed]") {
// Check that if first returns false, second is not touched
CheckedTestingMatcher first, second;
SECTION("&&") {
first.matchSucceeds = false;
// This assertion doesn't actually test anything, we just
// want the composed matcher's `match` being called.
CHECK_THAT(1, !(first && second));
// These two assertions are the important ones
REQUIRE(first.matchCalled);
REQUIRE(!second.matchCalled);
}
// Check that if first returns true, second is not touched
SECTION("||") {
first.matchSucceeds = true;
// This assertion doesn't actually test anything, we just
// want the composed matcher's `match` being called.
CHECK_THAT(1, first || second);
// These two assertions are the important ones
REQUIRE(first.matchCalled);
REQUIRE(!second.matchCalled);
}
}
template<typename Range>
struct EqualsRangeMatcher : Catch::Matchers::MatcherGenericBase {