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
0e77adee059f2d43ef6278f872f4c5a6198fc957
Author
Martin Hořeňovský ·
Parents
b74996a29ca9

Files · Download .tar.gz

Changes

 tests/SelfTest/Baselines/automake.sw.approved.txt  |  1 +
 tests/SelfTest/Baselines/compact.sw.approved.txt   |  6 +++
 tests/SelfTest/Baselines/console.std.approved.txt  |  4 +-
 tests/SelfTest/Baselines/console.sw.approved.txt   | 50 +++++++++++++++++-
 tests/SelfTest/Baselines/junit.sw.approved.txt     |  4 +-
 tests/SelfTest/Baselines/sonarqube.sw.approved.txt |  2 +
 tests/SelfTest/Baselines/tap.sw.approved.txt       | 14 ++++-
 tests/SelfTest/Baselines/teamcity.sw.approved.txt  |  2 +
 tests/SelfTest/Baselines/xml.sw.approved.txt       | 61 +++++++++++++++++++++-
 tests/SelfTest/UsageTests/Matchers.tests.cpp       | 41 +++++++++++++++
 10 files changed, 177 insertions(+), 8 deletions(-)

diff --git a/tests/SelfTest/Baselines/automake.sw.approved.txt b/tests/SelfTest/Baselines/automake.sw.approved.txt
index cc531e1d..d6cf5cc6 100644
--- a/tests/SelfTest/Baselines/automake.sw.approved.txt
+++ b/tests/SelfTest/Baselines/automake.sw.approved.txt
@@ -108,6 +108,7 @@ Nor would this
 :test-result: PASS Comparisons between ints where one side is computed
 :test-result: PASS Comparisons between unsigned ints and negative signed ints match c++ standard behaviour
 :test-result: PASS Comparisons with int literals don't warn when mixing signed/ unsigned
+:test-result: PASS Composed matchers shortcircuit
 :test-result: FAIL Contains string matcher
 :test-result: PASS Copy and then generate a range
 :test-result: FAIL Custom exceptions can be translated when testing for nothrow
diff --git a/tests/SelfTest/Baselines/compact.sw.approved.txt b/tests/SelfTest/Baselines/compact.sw.approved.txt
index aed08153..fc8ce18b 100644
--- a/tests/SelfTest/Baselines/compact.sw.approved.txt
+++ b/tests/SelfTest/Baselines/compact.sw.approved.txt
@@ -387,6 +387,12 @@ Condition.tests.cpp:<line number>: passed: 4 == ul for: 4 == 4
 Condition.tests.cpp:<line number>: passed: 5 == c for: 5 == 5
 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: 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/tests/SelfTest/Baselines/console.std.approved.txt b/tests/SelfTest/Baselines/console.std.approved.txt
index 1b423428..75813901 100644
--- a/tests/SelfTest/Baselines/console.std.approved.txt
+++ b/tests/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:  342 |  268 passed |  70 failed |  4 failed as expected
-assertions: 1935 | 1783 passed | 131 failed | 21 failed as expected
+test cases:  343 |  269 passed |  70 failed |  4 failed as expected
+assertions: 1941 | 1789 passed | 131 failed | 21 failed as expected
 
diff --git a/tests/SelfTest/Baselines/console.sw.approved.txt b/tests/SelfTest/Baselines/console.sw.approved.txt
index 4b8d12f6..be013f5c 100644
--- a/tests/SelfTest/Baselines/console.sw.approved.txt
+++ b/tests/SelfTest/Baselines/console.sw.approved.txt
@@ -2955,6 +2955,52 @@ Condition.tests.cpp:<line number>: PASSED:
 with expansion:
   4294967295 (0x<hex digits>) > 4
 
+-------------------------------------------------------------------------------
+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
 -------------------------------------------------------------------------------
@@ -15148,6 +15194,6 @@ Misc.tests.cpp:<line number>
 Misc.tests.cpp:<line number>: PASSED:
 
 ===============================================================================
-test cases:  342 |  252 passed |  86 failed |  4 failed as expected
-assertions: 1952 | 1783 passed | 148 failed | 21 failed as expected
+test cases:  343 |  253 passed |  86 failed |  4 failed as expected
+assertions: 1958 | 1789 passed | 148 failed | 21 failed as expected
 
diff --git a/tests/SelfTest/Baselines/junit.sw.approved.txt b/tests/SelfTest/Baselines/junit.sw.approved.txt
index 162fb04b..9b42e725 100644
--- a/tests/SelfTest/Baselines/junit.sw.approved.txt
+++ b/tests/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="1953" hostname="tbd" time="{duration}" timestamp="{iso8601-timestamp}">
+  <testsuite name="<exe-name>" errors="17" failures="132" tests="1959" hostname="tbd" time="{duration}" timestamp="{iso8601-timestamp}">
     <properties>
       <property name="filters" value="~[!nonportable]~[!benchmark]~[approvals] *"/>
       <property name="random-seed" value="1"/>
@@ -381,6 +381,8 @@ Exception.tests.cpp:<line number>
     <testcase classname="<exe-name>.global" name="Comparisons between ints where one side is computed" time="{duration}" status="run"/>
     <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 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/tests/SelfTest/Baselines/sonarqube.sw.approved.txt b/tests/SelfTest/Baselines/sonarqube.sw.approved.txt
index cdbed24a..7c9b5f18 100644
--- a/tests/SelfTest/Baselines/sonarqube.sw.approved.txt
+++ b/tests/SelfTest/Baselines/sonarqube.sw.approved.txt
@@ -948,6 +948,8 @@ Exception.tests.cpp:<line number>
     <testCase name="Combining only templated matchers" duration="{duration}"/>
     <testCase name="Combining templated and concrete matchers" duration="{duration}"/>
     <testCase name="Combining templated matchers" 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/tests/SelfTest/Baselines/tap.sw.approved.txt b/tests/SelfTest/Baselines/tap.sw.approved.txt
index 63d56bce..7d9b2ac8 100644
--- a/tests/SelfTest/Baselines/tap.sw.approved.txt
+++ b/tests/SelfTest/Baselines/tap.sw.approved.txt
@@ -772,6 +772,18 @@ ok {test-number} - 5 == c for: 5 == 5
 ok {test-number} - 6 == uc for: 6 == 6
 # Comparisons with int literals don't warn when mixing signed/ unsigned
 ok {test-number} - (std::numeric_limits<uint32_t>::max)() > ul for: 4294967295 (0x<hex digits>) > 4
+# Composed matchers shortcircuit
+ok {test-number} - 1, !(first && second) for: 1 not ( CheckedTestingMatcher set to fail and CheckedTestingMatcher set to fail )
+# Composed matchers shortcircuit
+ok {test-number} - first.matchCalled for: true
+# Composed matchers shortcircuit
+ok {test-number} - !second.matchCalled for: true
+# Composed matchers shortcircuit
+ok {test-number} - 1, first || second for: 1 ( CheckedTestingMatcher set to succeed or CheckedTestingMatcher set to fail )
+# Composed matchers shortcircuit
+ok {test-number} - first.matchCalled for: true
+# Composed matchers shortcircuit
+ok {test-number} - !second.matchCalled for: true
 # Contains string matcher
 not ok {test-number} - testStringForMatching(), Contains("not there", Catch::CaseSensitive::No) for: "this string contains 'abc' as a substring" contains: "not there" (case insensitive)
 # Contains string matcher
@@ -3896,5 +3908,5 @@ ok {test-number} - q3 == 23. for: 23.0 == 23.0
 ok {test-number} -
 # xmlentitycheck
 ok {test-number} -
-1..1944
+1..1950
 
diff --git a/tests/SelfTest/Baselines/teamcity.sw.approved.txt b/tests/SelfTest/Baselines/teamcity.sw.approved.txt
index 40264a41..703935f8 100644
--- a/tests/SelfTest/Baselines/teamcity.sw.approved.txt
+++ b/tests/SelfTest/Baselines/teamcity.sw.approved.txt
@@ -249,6 +249,8 @@ Exception.tests.cpp:<line number>|nunexpected exception with message:|n  "unexpe
 ##teamcity[testFinished name='Comparisons between unsigned ints and negative signed ints match c++ standard behaviour' duration="{duration}"]
 ##teamcity[testStarted name='Comparisons with int literals don|'t warn when mixing signed/ unsigned']
 ##teamcity[testFinished name='Comparisons with int literals don|'t warn when mixing signed/ unsigned' duration="{duration}"]
+##teamcity[testStarted name='Composed matchers shortcircuit']
+##teamcity[testFinished name='Composed matchers shortcircuit' duration="{duration}"]
 ##teamcity[testStarted name='Contains string matcher']
 Matchers.tests.cpp:<line number>|nexpression failed|n  CHECK_THAT( testStringForMatching(), Contains("not there", Catch::CaseSensitive::No) )|nwith expansion:|n  "this string contains |'abc|' as a substring" contains: "not there" (case insensitive)|n']
 Matchers.tests.cpp:<line number>|nexpression failed|n  CHECK_THAT( testStringForMatching(), Contains("STRING") )|nwith expansion:|n  "this string contains |'abc|' as a substring" contains: "STRING"|n']
diff --git a/tests/SelfTest/Baselines/xml.sw.approved.txt b/tests/SelfTest/Baselines/xml.sw.approved.txt
index a90439f7..b5ac74ab 100644
--- a/tests/SelfTest/Baselines/xml.sw.approved.txt
+++ b/tests/SelfTest/Baselines/xml.sw.approved.txt
@@ -3375,6 +3375,63 @@ Nor would this
       </Expression>
       <OverallResult success="true"/>
     </TestCase>
+    <TestCase name="Composed matchers shortcircuit" tags="[composed][matchers]" filename="tests/<exe-name>/UsageTests/Matchers.tests.cpp" >
+      <Section name="&amp;&amp;" filename="tests/<exe-name>/UsageTests/Matchers.tests.cpp" >
+        <Expression success="true" type="CHECK_THAT" filename="tests/<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="tests/<exe-name>/UsageTests/Matchers.tests.cpp" >
+          <Original>
+            first.matchCalled
+          </Original>
+          <Expanded>
+            true
+          </Expanded>
+        </Expression>
+        <Expression success="true" type="REQUIRE" filename="tests/<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="tests/<exe-name>/UsageTests/Matchers.tests.cpp" >
+        <Expression success="true" type="CHECK_THAT" filename="tests/<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="tests/<exe-name>/UsageTests/Matchers.tests.cpp" >
+          <Original>
+            first.matchCalled
+          </Original>
+          <Expanded>
+            true
+          </Expanded>
+        </Expression>
+        <Expression success="true" type="REQUIRE" filename="tests/<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="[.][failing][matchers]" filename="tests/<exe-name>/UsageTests/Matchers.tests.cpp" >
       <Expression success="false" type="CHECK_THAT" filename="tests/<exe-name>/UsageTests/Matchers.tests.cpp" >
         <Original>
@@ -18118,7 +18175,7 @@ loose text artifact
       </Section>
       <OverallResult success="true"/>
     </TestCase>
-    <OverallResults successes="1783" failures="149" expectedFailures="21"/>
+    <OverallResults successes="1789" failures="149" expectedFailures="21"/>
   </Group>
-  <OverallResults successes="1783" failures="148" expectedFailures="21"/>
+  <OverallResults successes="1789" failures="148" expectedFailures="21"/>
 </Catch>
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 {