Added &&, || and ! operator overloads for matchers
(syntactic sugar for AllOf, AnyOf and Not compositional matchers, respectively)
diff --git a/include/internal/catch_matchers.hpp b/include/internal/catch_matchers.hpp
index fd9dfd2..14249eb 100644
--- a/include/internal/catch_matchers.hpp
+++ b/include/internal/catch_matchers.hpp
@@ -12,6 +12,12 @@
namespace Matchers {
namespace Impl {
+ namespace Generic {
+ template<typename ExpressionT> class AllOf;
+ template<typename ExpressionT> class AnyOf;
+ template<typename ExpressionT> class Not;
+ }
+
template<typename ExpressionT>
struct Matcher : SharedImpl<IShared>
{
@@ -21,6 +27,10 @@
virtual Ptr<Matcher> clone() const = 0;
virtual bool match( ExpressionT const& expr ) const = 0;
virtual std::string toString() const = 0;
+
+ Generic::AllOf<ExpressionT> operator && ( Matcher<ExpressionT> const& other ) const;
+ Generic::AnyOf<ExpressionT> operator || ( Matcher<ExpressionT> const& other ) const;
+ Generic::Not<ExpressionT> operator ! () const;
};
template<typename DerivedT, typename ExpressionT>
@@ -34,7 +44,7 @@
namespace Generic {
template<typename ExpressionT>
struct Not : public MatcherImpl<Not<ExpressionT>, ExpressionT> {
- Not( Matcher<ExpressionT> const& matcher ) : m_matcher(matcher.clone()) {}
+ explicit Not( Matcher<ExpressionT> const& matcher ) : m_matcher(matcher.clone()) {}
Not( Not const& other ) : m_matcher( other.m_matcher ) {}
virtual bool match( ExpressionT const& expr ) const CATCH_OVERRIDE {
@@ -78,6 +88,12 @@
return oss.str();
}
+ AllOf operator && ( Matcher<ExpressionT> const& other ) const {
+ AllOf allOfExpr( *this );
+ allOfExpr.add( other );
+ return allOfExpr;
+ }
+
private:
std::vector<Ptr<Matcher<ExpressionT> > > m_matchers;
};
@@ -112,11 +128,40 @@
return oss.str();
}
+ AnyOf operator || ( Matcher<ExpressionT> const& other ) const {
+ AnyOf anyOfExpr( *this );
+ anyOfExpr.add( other );
+ return anyOfExpr;
+ }
+
private:
std::vector<Ptr<Matcher<ExpressionT> > > m_matchers;
};
+
+ } // namespace Generic
+
+ template<typename ExpressionT>
+ Generic::AllOf<ExpressionT> Matcher<ExpressionT>::operator && ( Matcher<ExpressionT> const& other ) const {
+ Generic::AllOf<ExpressionT> allOfExpr;
+ allOfExpr.add( *this );
+ allOfExpr.add( other );
+ return allOfExpr;
}
+ template<typename ExpressionT>
+ Generic::AnyOf<ExpressionT> Matcher<ExpressionT>::operator || ( Matcher<ExpressionT> const& other ) const {
+ Generic::AnyOf<ExpressionT> anyOfExpr;
+ anyOfExpr.add( *this );
+ anyOfExpr.add( other );
+ return anyOfExpr;
+ }
+
+ template<typename ExpressionT>
+ Generic::Not<ExpressionT> Matcher<ExpressionT>::operator ! () const {
+ return Generic::Not<ExpressionT>( *this );
+ }
+
+
namespace StdString {
inline std::string makeString( std::string const& str ) { return str; }
diff --git a/projects/SelfTest/Baselines/console.std.approved.txt b/projects/SelfTest/Baselines/console.std.approved.txt
index c222e30..73cef33 100644
--- a/projects/SelfTest/Baselines/console.std.approved.txt
+++ b/projects/SelfTest/Baselines/console.std.approved.txt
@@ -708,6 +708,29 @@
"this string contains 'abc' as a substring" equals: "something else"
-------------------------------------------------------------------------------
+Matchers can be composed with both + and | - failing
+-------------------------------------------------------------------------------
+MiscTests.cpp:<line number>
+...............................................................................
+
+MiscTests.cpp:<line number>: FAILED:
+ CHECK_THAT( testStringForMatching() ( Contains( "string" ) || Contains( "different" ) ) && Contains( "random" ) )
+with expansion:
+ "this string contains 'abc' as a substring" ( ( contains: "string" or
+ contains: "different" ) and contains: "random" )
+
+-------------------------------------------------------------------------------
+Matchers can be negated (Not) with the ! operator - failing
+-------------------------------------------------------------------------------
+MiscTests.cpp:<line number>
+...............................................................................
+
+MiscTests.cpp:<line number>: FAILED:
+ CHECK_THAT( testStringForMatching() !Contains( "substring" ) )
+with expansion:
+ "this string contains 'abc' as a substring" not contains: "substring"
+
+-------------------------------------------------------------------------------
Nice descriptive name
-------------------------------------------------------------------------------
MiscTests.cpp:<line number>
@@ -797,6 +820,6 @@
"first" == "second"
===============================================================================
-test cases: 159 | 119 passed | 39 failed | 1 failed as expected
-assertions: 905 | 812 passed | 80 failed | 13 failed as expected
+test cases: 165 | 123 passed | 41 failed | 1 failed as expected
+assertions: 912 | 817 passed | 82 failed | 13 failed as expected
diff --git a/projects/SelfTest/Baselines/console.sw.approved.txt b/projects/SelfTest/Baselines/console.sw.approved.txt
index 981fce1..082fa32 100644
--- a/projects/SelfTest/Baselines/console.sw.approved.txt
+++ b/projects/SelfTest/Baselines/console.sw.approved.txt
@@ -3419,6 +3419,87 @@
'abc' as a substring"
-------------------------------------------------------------------------------
+Matchers can be (AllOf) composed with the + operator
+-------------------------------------------------------------------------------
+MiscTests.cpp:<line number>
+...............................................................................
+
+MiscTests.cpp:<line number>:
+PASSED:
+ CHECK_THAT( testStringForMatching() Contains( "string" ) && Contains( "abc" ) && Contains( "substring" ) && Contains( "contains" ) )
+with expansion:
+ "this string contains 'abc' as a substring" ( contains: "string" and
+ contains: "abc" and contains: "substring" and contains: "contains" )
+
+-------------------------------------------------------------------------------
+Matchers can be (AnyOf) composed with the | operator
+-------------------------------------------------------------------------------
+MiscTests.cpp:<line number>
+...............................................................................
+
+MiscTests.cpp:<line number>:
+PASSED:
+ CHECK_THAT( testStringForMatching() Contains( "string" ) || Contains( "different" ) || Contains( "random" ) )
+with expansion:
+ "this string contains 'abc' as a substring" ( contains: "string" or contains:
+ "different" or contains: "random" )
+
+MiscTests.cpp:<line number>:
+PASSED:
+ CHECK_THAT( testStringForMatching2() Contains( "string" ) || Contains( "different" ) || Contains( "random" ) )
+with expansion:
+ "some completely different text that contains one common word" ( contains:
+ "string" or contains: "different" or contains: "random" )
+
+-------------------------------------------------------------------------------
+Matchers can be composed with both + and |
+-------------------------------------------------------------------------------
+MiscTests.cpp:<line number>
+...............................................................................
+
+MiscTests.cpp:<line number>:
+PASSED:
+ CHECK_THAT( testStringForMatching() ( Contains( "string" ) || Contains( "different" ) ) && Contains( "substring" ) )
+with expansion:
+ "this string contains 'abc' as a substring" ( ( contains: "string" or
+ contains: "different" ) and contains: "substring" )
+
+-------------------------------------------------------------------------------
+Matchers can be composed with both + and | - failing
+-------------------------------------------------------------------------------
+MiscTests.cpp:<line number>
+...............................................................................
+
+MiscTests.cpp:<line number>: FAILED:
+ CHECK_THAT( testStringForMatching() ( Contains( "string" ) || Contains( "different" ) ) && Contains( "random" ) )
+with expansion:
+ "this string contains 'abc' as a substring" ( ( contains: "string" or
+ contains: "different" ) and contains: "random" )
+
+-------------------------------------------------------------------------------
+Matchers can be negated (Not) with the ! operator
+-------------------------------------------------------------------------------
+MiscTests.cpp:<line number>
+...............................................................................
+
+MiscTests.cpp:<line number>:
+PASSED:
+ CHECK_THAT( testStringForMatching() !Contains( "different" ) )
+with expansion:
+ "this string contains 'abc' as a substring" not contains: "different"
+
+-------------------------------------------------------------------------------
+Matchers can be negated (Not) with the ! operator - failing
+-------------------------------------------------------------------------------
+MiscTests.cpp:<line number>
+...............................................................................
+
+MiscTests.cpp:<line number>: FAILED:
+ CHECK_THAT( testStringForMatching() !Contains( "substring" ) )
+with expansion:
+ "this string contains 'abc' as a substring" not contains: "substring"
+
+-------------------------------------------------------------------------------
Factorials are computed
-------------------------------------------------------------------------------
MiscTests.cpp:<line number>
@@ -8943,6 +9024,6 @@
1 > 0
===============================================================================
-test cases: 159 | 118 passed | 40 failed | 1 failed as expected
-assertions: 907 | 812 passed | 82 failed | 13 failed as expected
+test cases: 165 | 122 passed | 42 failed | 1 failed as expected
+assertions: 914 | 817 passed | 84 failed | 13 failed as expected
diff --git a/projects/SelfTest/Baselines/junit.sw.approved.txt b/projects/SelfTest/Baselines/junit.sw.approved.txt
index 0766b19..039aeaa 100644
--- a/projects/SelfTest/Baselines/junit.sw.approved.txt
+++ b/projects/SelfTest/Baselines/junit.sw.approved.txt
@@ -1,5 +1,5 @@
<testsuites>
- <testsuite name="CatchSelfTest" errors="12" failures="70" tests="907" hostname="tbd" time="{duration}" timestamp="tbd">
+ <testsuite name="CatchSelfTest" errors="12" failures="72" tests="914" hostname="tbd" time="{duration}" timestamp="tbd">
<testcase classname="global" name="toString(enum)" time="{duration}"/>
<testcase classname="global" name="toString(enum w/operator<<)" time="{duration}"/>
<testcase classname="global" name="toString(enum class)" time="{duration}"/>
@@ -438,6 +438,20 @@
<testcase classname="global" name="AllOf matcher" time="{duration}"/>
<testcase classname="global" name="AnyOf matcher" time="{duration}"/>
<testcase classname="global" name="Equals" time="{duration}"/>
+ <testcase classname="global" name="Matchers can be (AllOf) composed with the + operator" time="{duration}"/>
+ <testcase classname="global" name="Matchers can be (AnyOf) composed with the | operator" time="{duration}"/>
+ <testcase classname="global" name="Matchers can be composed with both + and |" time="{duration}"/>
+ <testcase classname="global" name="Matchers can be composed with both + and | - failing" time="{duration}">
+ <failure message=""this string contains 'abc' as a substring" ( ( contains: "string" or contains: "different" ) and contains: "random" )" type="CHECK_THAT">
+MiscTests.cpp:<line number>
+ </failure>
+ </testcase>
+ <testcase classname="global" name="Matchers can be negated (Not) with the ! operator" time="{duration}"/>
+ <testcase classname="global" name="Matchers can be negated (Not) with the ! operator - failing" time="{duration}">
+ <failure message=""this string contains 'abc' as a substring" not contains: "substring"" type="CHECK_THAT">
+MiscTests.cpp:<line number>
+ </failure>
+ </testcase>
<testcase classname="global" name="Factorials are computed" time="{duration}"/>
<testcase classname="global" name="Nice descriptive name" time="{duration}"/>
<testcase classname="vectors can be sized and resized" name="root" time="{duration}"/>
diff --git a/projects/SelfTest/Baselines/xml.sw.approved.txt b/projects/SelfTest/Baselines/xml.sw.approved.txt
index 1bb662f..ed4ce49 100644
--- a/projects/SelfTest/Baselines/xml.sw.approved.txt
+++ b/projects/SelfTest/Baselines/xml.sw.approved.txt
@@ -3590,6 +3590,80 @@
</Expression>
<OverallResult success="true"/>
</TestCase>
+ <TestCase name="Matchers can be (AllOf) composed with the + operator">
+ <Expression success="true" type="CHECK_THAT" filename="projects/SelfTest/MiscTests.cpp" >
+ <Original>
+ testStringForMatching() Contains( "string" ) && Contains( "abc" ) && Contains( "substring" ) && Contains( "contains" )
+ </Original>
+ <Expanded>
+ "this string contains 'abc' as a substring" ( contains: "string" and contains: "abc" and contains: "substring" and contains: "contains" )
+ </Expanded>
+ </Expression>
+ <OverallResult success="true"/>
+ </TestCase>
+ <TestCase name="Matchers can be (AnyOf) composed with the | operator">
+ <Expression success="true" type="CHECK_THAT" filename="projects/SelfTest/MiscTests.cpp" >
+ <Original>
+ testStringForMatching() Contains( "string" ) || Contains( "different" ) || Contains( "random" )
+ </Original>
+ <Expanded>
+ "this string contains 'abc' as a substring" ( contains: "string" or contains: "different" or contains: "random" )
+ </Expanded>
+ </Expression>
+ <Expression success="true" type="CHECK_THAT" filename="projects/SelfTest/MiscTests.cpp" >
+ <Original>
+ testStringForMatching2() Contains( "string" ) || Contains( "different" ) || Contains( "random" )
+ </Original>
+ <Expanded>
+ "some completely different text that contains one common word" ( contains: "string" or contains: "different" or contains: "random" )
+ </Expanded>
+ </Expression>
+ <OverallResult success="true"/>
+ </TestCase>
+ <TestCase name="Matchers can be composed with both + and |">
+ <Expression success="true" type="CHECK_THAT" filename="projects/SelfTest/MiscTests.cpp" >
+ <Original>
+ testStringForMatching() ( Contains( "string" ) || Contains( "different" ) ) && Contains( "substring" )
+ </Original>
+ <Expanded>
+ "this string contains 'abc' as a substring" ( ( contains: "string" or contains: "different" ) and contains: "substring" )
+ </Expanded>
+ </Expression>
+ <OverallResult success="true"/>
+ </TestCase>
+ <TestCase name="Matchers can be composed with both + and | - failing">
+ <Expression success="false" type="CHECK_THAT" filename="projects/SelfTest/MiscTests.cpp" >
+ <Original>
+ testStringForMatching() ( Contains( "string" ) || Contains( "different" ) ) && Contains( "random" )
+ </Original>
+ <Expanded>
+ "this string contains 'abc' as a substring" ( ( contains: "string" or contains: "different" ) and contains: "random" )
+ </Expanded>
+ </Expression>
+ <OverallResult success="false"/>
+ </TestCase>
+ <TestCase name="Matchers can be negated (Not) with the ! operator">
+ <Expression success="true" type="CHECK_THAT" filename="projects/SelfTest/MiscTests.cpp" >
+ <Original>
+ testStringForMatching() !Contains( "different" )
+ </Original>
+ <Expanded>
+ "this string contains 'abc' as a substring" not contains: "different"
+ </Expanded>
+ </Expression>
+ <OverallResult success="true"/>
+ </TestCase>
+ <TestCase name="Matchers can be negated (Not) with the ! operator - failing">
+ <Expression success="false" type="CHECK_THAT" filename="projects/SelfTest/MiscTests.cpp" >
+ <Original>
+ testStringForMatching() !Contains( "substring" )
+ </Original>
+ <Expanded>
+ "this string contains 'abc' as a substring" not contains: "substring"
+ </Expanded>
+ </Expression>
+ <OverallResult success="false"/>
+ </TestCase>
<TestCase name="Factorials are computed">
<Expression success="true" type="REQUIRE" filename="projects/SelfTest/MiscTests.cpp" >
<Original>
@@ -9422,7 +9496,7 @@
</Section>
<OverallResult success="true"/>
</TestCase>
- <OverallResults successes="812" failures="82" expectedFailures="13"/>
+ <OverallResults successes="817" failures="84" expectedFailures="13"/>
</Group>
- <OverallResults successes="812" failures="82" expectedFailures="13"/>
+ <OverallResults successes="817" failures="84" expectedFailures="13"/>
</Catch>
diff --git a/projects/SelfTest/MiscTests.cpp b/projects/SelfTest/MiscTests.cpp
index 8bd271c..c2a7784 100644
--- a/projects/SelfTest/MiscTests.cpp
+++ b/projects/SelfTest/MiscTests.cpp
@@ -208,6 +208,11 @@
{
return "this string contains 'abc' as a substring";
}
+inline const char* testStringForMatching2()
+{
+ return "some completely different text that contains one common word";
+}
+
using namespace Catch::Matchers;
TEST_CASE("String matchers", "[matchers]" )
@@ -257,6 +262,42 @@
CHECK_THAT( testStringForMatching(), Equals( "this string contains 'abc' as a substring" ) );
}
+TEST_CASE("Matchers can be (AllOf) composed with the + operator", "[matchers][operators][operator+]")
+{
+ CHECK_THAT( testStringForMatching(),
+ Contains( "string" ) &&
+ Contains( "abc" ) &&
+ Contains( "substring" ) &&
+ Contains( "contains" ) );
+}
+
+TEST_CASE("Matchers can be (AnyOf) composed with the | operator", "[matchers][operators][operator|]")
+{
+ CHECK_THAT( testStringForMatching(), Contains( "string" ) || Contains( "different" ) || Contains( "random" ) );
+ CHECK_THAT( testStringForMatching2(), Contains( "string" ) || Contains( "different" ) || Contains( "random" ) );
+}
+
+TEST_CASE("Matchers can be composed with both + and |", "[matchers][operators][operator|][operator+]")
+{
+ CHECK_THAT( testStringForMatching(), ( Contains( "string" ) || Contains( "different" ) ) && Contains( "substring" ) );
+}
+
+TEST_CASE("Matchers can be composed with both + and | - failing", "[matchers][operators][operator|][operator+][.failing]")
+{
+ CHECK_THAT( testStringForMatching(), ( Contains( "string" ) || Contains( "different" ) ) && Contains( "random" ) );
+}
+
+TEST_CASE("Matchers can be negated (Not) with the ! operator", "[matchers][operators][not]")
+{
+ CHECK_THAT( testStringForMatching(), !Contains( "different" ) );
+}
+
+TEST_CASE("Matchers can be negated (Not) with the ! operator - failing", "[matchers][operators][not][.failing]")
+{
+ CHECK_THAT( testStringForMatching(), !Contains( "substring" ) );
+}
+
+
inline unsigned int Factorial( unsigned int number )
{
// return number <= 1 ? number : Factorial(number-1)*number;