Added &&, || and ! operator overloads for matchers

(syntactic sugar for AllOf, AnyOf and Not compositional matchers, respectively)
This commit is contained in:
Phil Nash 2015-11-05 18:46:00 +00:00
parent f3e7722cc6
commit 054e3c5b43
6 changed files with 286 additions and 8 deletions

View File

@ -12,6 +12,12 @@ namespace Catch {
namespace Matchers { namespace Matchers {
namespace Impl { namespace Impl {
namespace Generic {
template<typename ExpressionT> class AllOf;
template<typename ExpressionT> class AnyOf;
template<typename ExpressionT> class Not;
}
template<typename ExpressionT> template<typename ExpressionT>
struct Matcher : SharedImpl<IShared> struct Matcher : SharedImpl<IShared>
{ {
@ -21,6 +27,10 @@ namespace Matchers {
virtual Ptr<Matcher> clone() const = 0; virtual Ptr<Matcher> clone() const = 0;
virtual bool match( ExpressionT const& expr ) const = 0; virtual bool match( ExpressionT const& expr ) const = 0;
virtual std::string toString() 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> template<typename DerivedT, typename ExpressionT>
@ -34,7 +44,7 @@ namespace Matchers {
namespace Generic { namespace Generic {
template<typename ExpressionT> template<typename ExpressionT>
struct Not : public MatcherImpl<Not<ExpressionT>, 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 ) {} Not( Not const& other ) : m_matcher( other.m_matcher ) {}
virtual bool match( ExpressionT const& expr ) const CATCH_OVERRIDE { virtual bool match( ExpressionT const& expr ) const CATCH_OVERRIDE {
@ -78,6 +88,12 @@ namespace Matchers {
return oss.str(); return oss.str();
} }
AllOf operator && ( Matcher<ExpressionT> const& other ) const {
AllOf allOfExpr( *this );
allOfExpr.add( other );
return allOfExpr;
}
private: private:
std::vector<Ptr<Matcher<ExpressionT> > > m_matchers; std::vector<Ptr<Matcher<ExpressionT> > > m_matchers;
}; };
@ -112,11 +128,40 @@ namespace Matchers {
return oss.str(); return oss.str();
} }
AnyOf operator || ( Matcher<ExpressionT> const& other ) const {
AnyOf anyOfExpr( *this );
anyOfExpr.add( other );
return anyOfExpr;
}
private: private:
std::vector<Ptr<Matcher<ExpressionT> > > m_matchers; 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 { namespace StdString {
inline std::string makeString( std::string const& str ) { return str; } inline std::string makeString( std::string const& str ) { return str; }

View File

@ -707,6 +707,29 @@ MiscTests.cpp:<line number>: FAILED:
with expansion: with expansion:
"this string contains 'abc' as a substring" equals: "something else" "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 Nice descriptive name
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
@ -797,6 +820,6 @@ with expansion:
"first" == "second" "first" == "second"
=============================================================================== ===============================================================================
test cases: 159 | 119 passed | 39 failed | 1 failed as expected test cases: 165 | 123 passed | 41 failed | 1 failed as expected
assertions: 905 | 812 passed | 80 failed | 13 failed as expected assertions: 912 | 817 passed | 82 failed | 13 failed as expected

View File

@ -3418,6 +3418,87 @@ with expansion:
"this string contains 'abc' as a substring" equals: "this string contains "this string contains 'abc' as a substring" equals: "this string contains
'abc' as a substring" '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 Factorials are computed
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
@ -8943,6 +9024,6 @@ with expansion:
1 > 0 1 > 0
=============================================================================== ===============================================================================
test cases: 159 | 118 passed | 40 failed | 1 failed as expected test cases: 165 | 122 passed | 42 failed | 1 failed as expected
assertions: 907 | 812 passed | 82 failed | 13 failed as expected assertions: 914 | 817 passed | 84 failed | 13 failed as expected

View File

@ -1,5 +1,5 @@
<testsuites> <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)" time="{duration}"/>
<testcase classname="global" name="toString(enum w/operator&lt;&lt;)" time="{duration}"/> <testcase classname="global" name="toString(enum w/operator&lt;&lt;)" time="{duration}"/>
<testcase classname="global" name="toString(enum class)" time="{duration}"/> <testcase classname="global" name="toString(enum class)" time="{duration}"/>
@ -438,6 +438,20 @@ MiscTests.cpp:<line number>
<testcase classname="global" name="AllOf matcher" time="{duration}"/> <testcase classname="global" name="AllOf matcher" time="{duration}"/>
<testcase classname="global" name="AnyOf matcher" time="{duration}"/> <testcase classname="global" name="AnyOf matcher" time="{duration}"/>
<testcase classname="global" name="Equals" 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="&quot;this string contains 'abc' as a substring&quot; ( ( contains: &quot;string&quot; or contains: &quot;different&quot; ) and contains: &quot;random&quot; )" 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="&quot;this string contains 'abc' as a substring&quot; not contains: &quot;substring&quot;" type="CHECK_THAT">
MiscTests.cpp:<line number>
</failure>
</testcase>
<testcase classname="global" name="Factorials are computed" time="{duration}"/> <testcase classname="global" name="Factorials are computed" time="{duration}"/>
<testcase classname="global" name="Nice descriptive name" time="{duration}"/> <testcase classname="global" name="Nice descriptive name" time="{duration}"/>
<testcase classname="vectors can be sized and resized" name="root" time="{duration}"/> <testcase classname="vectors can be sized and resized" name="root" time="{duration}"/>

View File

@ -3590,6 +3590,80 @@
</Expression> </Expression>
<OverallResult success="true"/> <OverallResult success="true"/>
</TestCase> </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" ) &amp;&amp; Contains( "abc" ) &amp;&amp; Contains( "substring" ) &amp;&amp; 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" ) ) &amp;&amp; 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" ) ) &amp;&amp; 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"> <TestCase name="Factorials are computed">
<Expression success="true" type="REQUIRE" filename="projects/SelfTest/MiscTests.cpp" > <Expression success="true" type="REQUIRE" filename="projects/SelfTest/MiscTests.cpp" >
<Original> <Original>
@ -9422,7 +9496,7 @@ there"
</Section> </Section>
<OverallResult success="true"/> <OverallResult success="true"/>
</TestCase> </TestCase>
<OverallResults successes="812" failures="82" expectedFailures="13"/> <OverallResults successes="817" failures="84" expectedFailures="13"/>
</Group> </Group>
<OverallResults successes="812" failures="82" expectedFailures="13"/> <OverallResults successes="817" failures="84" expectedFailures="13"/>
</Catch> </Catch>

View File

@ -208,6 +208,11 @@ inline const char* testStringForMatching()
{ {
return "this string contains 'abc' as a substring"; 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; using namespace Catch::Matchers;
TEST_CASE("String matchers", "[matchers]" ) TEST_CASE("String matchers", "[matchers]" )
@ -257,6 +262,42 @@ TEST_CASE("Equals", "[matchers]")
CHECK_THAT( testStringForMatching(), Equals( "this string contains 'abc' as a substring" ) ); 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 ) inline unsigned int Factorial( unsigned int number )
{ {
// return number <= 1 ? number : Factorial(number-1)*number; // return number <= 1 ? number : Factorial(number-1)*number;