Progmasoft Git

catch3

Progmasoft Catch3: an independently maintained C++17 test framework with a parallel runner, property checks, snapshots and structured results.

Clonegit clone https://git.progmasoft.com/catch3

Add explicit test for shortcircuiting behaviour of combined matchers

Commit
1806b215457ff90cfdc7d0eaaacbdf21f7a02d3c
Author
Martin Hořeňovský ·
Parents
288416f50108

Files · Download .tar.gz

Changes

 .../SelfTest/Baselines/compact.sw.approved.txt     |  6 +++
 .../SelfTest/Baselines/console.std.approved.txt    |  4 +-
 .../SelfTest/Baselines/console.sw.approved.txt     | 50 +++++++++++++++++-
 projects/SelfTest/Baselines/junit.sw.approved.txt  |  4 +-
 .../SelfTest/Baselines/sonarqube.sw.approved.txt   |  2 +
 projects/SelfTest/Baselines/xml.sw.approved.txt    | 61 +++++++++++++++++++++-
 projects/SelfTest/UsageTests/Matchers.tests.cpp    | 39 ++++++++++++++
 7 files changed, 159 insertions(+), 7 deletions(-)

diff --git a/projects/SelfTest/Baselines/compact.sw.approved.txt b/projects/SelfTest/Baselines/compact.sw.approved.txt
index 48bc91d1..cdff6666 100644
--- a/projects/SelfTest/Baselines/compact.sw.approved.txt
+++ b/projects/SelfTest/Baselines/compact.sw.approved.txt
@@ -335,6 +335,12 @@ Condition.tests.cpp:<line number>: passed: 6 == uc for: 6 == 6
 Condition.tests.cpp:<line number>: passed: (std::numeric_limits<uint32_t>::max)() > ul for: 4294967295 (0x<hex digits>) > 4
 Matchers.tests.cpp:<line number>: passed: testStringForMatching2(), !composed1 for: "some completely different text that contains one common word" not ( contains: "string" or contains: "random" )
 Matchers.tests.cpp:<line number>: passed: testStringForMatching2(), composed2 for: "some completely different text that contains one common word" ( contains: "string" or contains: "random" or contains: "different" )
+Matchers.tests.cpp:<line number>: passed: 1, !(first && second) for: 1 not ( CheckedTestingMatcher set to fail and CheckedTestingMatcher set to fail )
+Matchers.tests.cpp:<line number>: passed: first.matchCalled for: true
+Matchers.tests.cpp:<line number>: passed: !second.matchCalled for: true
+Matchers.tests.cpp:<line number>: passed: 1, first || second for: 1 ( CheckedTestingMatcher set to succeed or CheckedTestingMatcher set to fail )
+Matchers.tests.cpp:<line number>: passed: first.matchCalled for: true
+Matchers.tests.cpp:<line number>: passed: !second.matchCalled for: true
 Matchers.tests.cpp:<line number>: failed: testStringForMatching(), Contains("not there", Catch::CaseSensitive::No) for: "this string contains 'abc' as a substring" contains: "not there" (case insensitive)
 Matchers.tests.cpp:<line number>: failed: testStringForMatching(), Contains("STRING") for: "this string contains 'abc' as a substring" contains: "STRING"
 Generators.tests.cpp:<line number>: passed: elem % 2 == 1 for: 1 == 1
diff --git a/projects/SelfTest/Baselines/console.std.approved.txt b/projects/SelfTest/Baselines/console.std.approved.txt
index ff7f34b1..479fdafc 100644
--- a/projects/SelfTest/Baselines/console.std.approved.txt
+++ b/projects/SelfTest/Baselines/console.std.approved.txt
@@ -1380,6 +1380,6 @@ due to unexpected exception with message:
   Why would you throw a std::string?
 
 ===============================================================================
-test cases:  315 |  241 passed |  70 failed |  4 failed as expected
-assertions: 1710 | 1558 passed | 131 failed | 21 failed as expected
+test cases:  316 |  242 passed |  70 failed |  4 failed as expected
+assertions: 1716 | 1564 passed | 131 failed | 21 failed as expected
 
diff --git a/projects/SelfTest/Baselines/console.sw.approved.txt b/projects/SelfTest/Baselines/console.sw.approved.txt
index 19e3947f..521de74b 100644
--- a/projects/SelfTest/Baselines/console.sw.approved.txt
+++ b/projects/SelfTest/Baselines/console.sw.approved.txt
@@ -2584,6 +2584,52 @@ with expansion:
   "some completely different text that contains one common word" ( contains:
   "string" or contains: "random" or contains: "different" )
 
+-------------------------------------------------------------------------------
+Composed matchers shortcircuit
+  &&
+-------------------------------------------------------------------------------
+Matchers.tests.cpp:<line number>
+...............................................................................
+
+Matchers.tests.cpp:<line number>: PASSED:
+  CHECK_THAT( 1, !(first && second) )
+with expansion:
+  1 not ( CheckedTestingMatcher set to fail and CheckedTestingMatcher set to
+  fail )
+
+Matchers.tests.cpp:<line number>: PASSED:
+  REQUIRE( first.matchCalled )
+with expansion:
+  true
+
+Matchers.tests.cpp:<line number>: PASSED:
+  REQUIRE( !second.matchCalled )
+with expansion:
+  true
+
+-------------------------------------------------------------------------------
+Composed matchers shortcircuit
+  ||
+-------------------------------------------------------------------------------
+Matchers.tests.cpp:<line number>
+...............................................................................
+
+Matchers.tests.cpp:<line number>: PASSED:
+  CHECK_THAT( 1, first || second )
+with expansion:
+  1 ( CheckedTestingMatcher set to succeed or CheckedTestingMatcher set to fail
+  )
+
+Matchers.tests.cpp:<line number>: PASSED:
+  REQUIRE( first.matchCalled )
+with expansion:
+  true
+
+Matchers.tests.cpp:<line number>: PASSED:
+  REQUIRE( !second.matchCalled )
+with expansion:
+  true
+
 -------------------------------------------------------------------------------
 Contains string matcher
 -------------------------------------------------------------------------------
@@ -13663,6 +13709,6 @@ Misc.tests.cpp:<line number>
 Misc.tests.cpp:<line number>: PASSED:
 
 ===============================================================================
-test cases:  315 |  225 passed |  86 failed |  4 failed as expected
-assertions: 1727 | 1558 passed | 148 failed | 21 failed as expected
+test cases:  316 |  226 passed |  86 failed |  4 failed as expected
+assertions: 1733 | 1564 passed | 148 failed | 21 failed as expected
 
diff --git a/projects/SelfTest/Baselines/junit.sw.approved.txt b/projects/SelfTest/Baselines/junit.sw.approved.txt
index 19d9af0d..0a5ccecd 100644
--- a/projects/SelfTest/Baselines/junit.sw.approved.txt
+++ b/projects/SelfTest/Baselines/junit.sw.approved.txt
@@ -1,7 +1,7 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <testsuitesloose text artifact
 >
-  <testsuite name="<exe-name>" errors="17" failures="132" tests="1728" hostname="tbd" time="{duration}" timestamp="{iso8601-timestamp}">
+  <testsuite name="<exe-name>" errors="17" failures="132" tests="1734" hostname="tbd" time="{duration}" timestamp="{iso8601-timestamp}">
     <properties>
       <property name="filters" value="~[!nonportable]~[!benchmark]~[approvals]"/>
       <property name="random-seed" value="1"/>
@@ -366,6 +366,8 @@ Exception.tests.cpp:<line number>
     <testcase classname="<exe-name>.global" name="Comparisons between unsigned ints and negative signed ints match c++ standard behaviour" time="{duration}" status="run"/>
     <testcase classname="<exe-name>.global" name="Comparisons with int literals don't warn when mixing signed/ unsigned" time="{duration}" status="run"/>
     <testcase classname="<exe-name>.global" name="Composed matchers are distinct" time="{duration}" status="run"/>
+    <testcase classname="<exe-name>.global" name="Composed matchers shortcircuit/&amp;&amp;" time="{duration}" status="run"/>
+    <testcase classname="<exe-name>.global" name="Composed matchers shortcircuit/||" time="{duration}" status="run"/>
     <testcase classname="<exe-name>.global" name="Contains string matcher" time="{duration}" status="run">
       <failure message="testStringForMatching(), Contains(&quot;not there&quot;, Catch::CaseSensitive::No)" type="CHECK_THAT">
 FAILED:
diff --git a/projects/SelfTest/Baselines/sonarqube.sw.approved.txt b/projects/SelfTest/Baselines/sonarqube.sw.approved.txt
index 47d3b39f..9b161560 100644
--- a/projects/SelfTest/Baselines/sonarqube.sw.approved.txt
+++ b/projects/SelfTest/Baselines/sonarqube.sw.approved.txt
@@ -902,6 +902,8 @@ Exception.tests.cpp:<line number>
     <testCase name="Arbitrary predicate matcher/Function pointer" duration="{duration}"/>
     <testCase name="Arbitrary predicate matcher/Lambdas + different type" duration="{duration}"/>
     <testCase name="Composed matchers are distinct" duration="{duration}"/>
+    <testCase name="Composed matchers shortcircuit/&amp;&amp;" duration="{duration}"/>
+    <testCase name="Composed matchers shortcircuit/||" duration="{duration}"/>
     <testCase name="Contains string matcher" duration="{duration}">
       <failure message="CHECK_THAT(testStringForMatching(), Contains(&quot;not there&quot;, Catch::CaseSensitive::No))">
 FAILED:
diff --git a/projects/SelfTest/Baselines/xml.sw.approved.txt b/projects/SelfTest/Baselines/xml.sw.approved.txt
index 3cd5becd..784413d0 100644
--- a/projects/SelfTest/Baselines/xml.sw.approved.txt
+++ b/projects/SelfTest/Baselines/xml.sw.approved.txt
@@ -3025,6 +3025,63 @@ Nor would this
       </Expression>
       <OverallResult success="true"/>
     </TestCase>
+    <TestCase name="Composed matchers shortcircuit" tags="[composed][matchers]" filename="projects/<exe-name>/UsageTests/Matchers.tests.cpp" >
+      <Section name="&amp;&amp;" filename="projects/<exe-name>/UsageTests/Matchers.tests.cpp" >
+        <Expression success="true" type="CHECK_THAT" filename="projects/<exe-name>/UsageTests/Matchers.tests.cpp" >
+          <Original>
+            1, !(first &amp;&amp; second)
+          </Original>
+          <Expanded>
+            1 not ( CheckedTestingMatcher set to fail and CheckedTestingMatcher set to fail )
+          </Expanded>
+        </Expression>
+        <Expression success="true" type="REQUIRE" filename="projects/<exe-name>/UsageTests/Matchers.tests.cpp" >
+          <Original>
+            first.matchCalled
+          </Original>
+          <Expanded>
+            true
+          </Expanded>
+        </Expression>
+        <Expression success="true" type="REQUIRE" filename="projects/<exe-name>/UsageTests/Matchers.tests.cpp" >
+          <Original>
+            !second.matchCalled
+          </Original>
+          <Expanded>
+            true
+          </Expanded>
+        </Expression>
+        <OverallResults successes="3" failures="0" expectedFailures="0"/>
+      </Section>
+      <Section name="||" filename="projects/<exe-name>/UsageTests/Matchers.tests.cpp" >
+        <Expression success="true" type="CHECK_THAT" filename="projects/<exe-name>/UsageTests/Matchers.tests.cpp" >
+          <Original>
+            1, first || second
+          </Original>
+          <Expanded>
+            1 ( CheckedTestingMatcher set to succeed or CheckedTestingMatcher set to fail )
+          </Expanded>
+        </Expression>
+        <Expression success="true" type="REQUIRE" filename="projects/<exe-name>/UsageTests/Matchers.tests.cpp" >
+          <Original>
+            first.matchCalled
+          </Original>
+          <Expanded>
+            true
+          </Expanded>
+        </Expression>
+        <Expression success="true" type="REQUIRE" filename="projects/<exe-name>/UsageTests/Matchers.tests.cpp" >
+          <Original>
+            !second.matchCalled
+          </Original>
+          <Expanded>
+            true
+          </Expanded>
+        </Expression>
+        <OverallResults successes="3" failures="0" expectedFailures="0"/>
+      </Section>
+      <OverallResult success="true"/>
+    </TestCase>
     <TestCase name="Contains string matcher" tags="[!hide][.][failing][matchers]" filename="projects/<exe-name>/UsageTests/Matchers.tests.cpp" >
       <Expression success="false" type="CHECK_THAT" filename="projects/<exe-name>/UsageTests/Matchers.tests.cpp" >
         <Original>
@@ -16318,7 +16375,7 @@ loose text artifact
       </Section>
       <OverallResult success="true"/>
     </TestCase>
-    <OverallResults successes="1558" failures="149" expectedFailures="21"/>
+    <OverallResults successes="1564" failures="149" expectedFailures="21"/>
   </Group>
-  <OverallResults successes="1558" failures="148" expectedFailures="21"/>
+  <OverallResults successes="1564" failures="148" expectedFailures="21"/>
 </Catch>
diff --git a/projects/SelfTest/UsageTests/Matchers.tests.cpp b/projects/SelfTest/UsageTests/Matchers.tests.cpp
index c4a8ebf9..1797d4a1 100644
--- a/projects/SelfTest/UsageTests/Matchers.tests.cpp
+++ b/projects/SelfTest/UsageTests/Matchers.tests.cpp
@@ -628,6 +628,45 @@ namespace { namespace MatchersTests {
             REQUIRE_THAT(testStringForMatching2(), composed2);
         }
 
+        struct CheckedTestingMatcher : Catch::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);
+            }
+        }
+
 } } // namespace MatchersTests
 
 #endif // CATCH_CONFIG_DISABLE_MATCHERS