Read-only mirrors. Development, issues and pull requests are on GitHub.

summaryrefslogtreecommitdiffhomepage
path: root/tests/SelfTest/UsageTests/Matchers.tests.cpp
diff options
context:
space:
mode:
authorMartin Hořeňovský2020-06-14 21:48:08 +0200
committerMartin Hořeňovský2020-07-22 21:36:14 +0200
commit0e77adee059f2d43ef6278f872f4c5a6198fc957 (patch)
tree1d83841820789387d5fd826b75d192f0a0147b4a /tests/SelfTest/UsageTests/Matchers.tests.cpp
parentb74996a29ca9c0a0d98d4866663d0c2943a60617 (diff)
downloadcatch3-0e77adee059f2d43ef6278f872f4c5a6198fc957.tar.gz
Add explicit test for shortcircuiting behaviour of combined matchers
Diffstat (limited to 'tests/SelfTest/UsageTests/Matchers.tests.cpp')
-rw-r--r--tests/SelfTest/UsageTests/Matchers.tests.cpp41
1 files changed, 41 insertions, 0 deletions
diff --git a/tests/SelfTest/UsageTests/Matchers.tests.cpp b/tests/SelfTest/UsageTests/Matchers.tests.cpp
index f8c9ed1d..c9c61285 100644
--- a/tests/SelfTest/UsageTests/Matchers.tests.cpp
+++ b/tests/SelfTest/UsageTests/Matchers.tests.cpp
@@ -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 {