mirror of
https://github.com/catchorg/Catch2.git
synced 2024-12-23 03:43:28 +01:00
Added &&, || and ! operator overloads for matchers
(syntactic sugar for AllOf, AnyOf and Not compositional matchers, respectively)
This commit is contained in:
parent
f3e7722cc6
commit
054e3c5b43
@ -12,6 +12,12 @@ namespace Catch {
|
||||
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 @@ namespace Matchers {
|
||||
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 Matchers {
|
||||
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 @@ namespace Matchers {
|
||||
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 @@ namespace Matchers {
|
||||
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; }
|
||||
|
@ -707,6 +707,29 @@ MiscTests.cpp:<line number>: FAILED:
|
||||
with expansion:
|
||||
"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
|
||||
-------------------------------------------------------------------------------
|
||||
@ -797,6 +820,6 @@ with expansion:
|
||||
"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
|
||||
|
||||
|
@ -3418,6 +3418,87 @@ with expansion:
|
||||
"this string contains 'abc' as a substring" equals: "this string contains
|
||||
'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
|
||||
-------------------------------------------------------------------------------
|
||||
@ -8943,6 +9024,6 @@ with expansion:
|
||||
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
|
||||
|
||||
|
@ -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 @@ MiscTests.cpp:<line number>
|
||||
<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}"/>
|
||||
|
@ -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 @@ there"
|
||||
</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>
|
||||
|
@ -208,6 +208,11 @@ inline const char* testStringForMatching()
|
||||
{
|
||||
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 @@ TEST_CASE("Equals", "[matchers]")
|
||||
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;
|
||||
|
Loading…
Reference in New Issue
Block a user