mirror of
https://github.com/catchorg/Catch2.git
synced 2025-08-01 12:55:40 +02:00
Add explicit test for shortcircuiting behaviour of combined matchers
This commit is contained in:
@@ -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 {
|
||||
|
||||
|
Reference in New Issue
Block a user