Read-only mirrors. Development, issues and pull requests are on GitHub.

summaryrefslogtreecommitdiffhomepage
path: root/include/internal
diff options
context:
space:
mode:
authorPhil Nash2012-10-12 07:58:17 +0100
committerPhil Nash2012-10-12 07:58:17 +0100
commit9444bbcb7bd0ba2eee6717903490ef7b8bc783a0 (patch)
tree584edcb96fb6fce3a7ae6def705d47fdbdfec9ce /include/internal
parent9902ac9f1afc86d83bc5107259494d7c4631adfd (diff)
downloadcatch3-9444bbcb7bd0ba2eee6717903490ef7b8bc783a0.tar.gz
Added AllOf and AnyOf matchers
Diffstat (limited to 'include/internal')
-rw-r--r--include/internal/catch_expression_builder.hpp8
-rw-r--r--include/internal/catch_impl.hpp5
-rw-r--r--include/internal/catch_matchers.hpp197
-rw-r--r--include/internal/catch_ptr.hpp2
4 files changed, 166 insertions, 46 deletions
diff --git a/include/internal/catch_expression_builder.hpp b/include/internal/catch_expression_builder.hpp
index 45a7664c..86f68ecb 100644
--- a/include/internal/catch_expression_builder.hpp
+++ b/include/internal/catch_expression_builder.hpp
@@ -56,14 +56,14 @@ public:
ExpressionBuilder& acceptMatcher( const MatcherT& matcher,
const ArgT& arg,
const std::string& matcherCallAsString ) {
- std::string matcherAsString = Catch::toString( matcher );
+ std::string matcherAsString = matcher.toString();
if( matcherAsString == "{?}" )
matcherAsString = matcherCallAsString;
m_result
.setLhs( Catch::toString( arg ) )
.setRhs( matcherAsString )
.setOp( "matches" )
- .setResultType( matcher( arg ) ? ResultWas::Ok : ResultWas::ExpressionFailed );
+ .setResultType( matcher.match( arg ) ? ResultWas::Ok : ResultWas::ExpressionFailed );
return *this;
}
@@ -71,14 +71,14 @@ public:
ExpressionBuilder& acceptMatcher( const MatcherT& matcher,
ArgT* arg,
const std::string& matcherCallAsString ) {
- std::string matcherAsString = Catch::toString( matcher );
+ std::string matcherAsString = matcher.toString();
if( matcherAsString == "{?}" )
matcherAsString = matcherCallAsString;
m_result
.setLhs( Catch::toString( arg ) )
.setRhs( matcherAsString )
.setOp( "matches" )
- .setResultType( matcher( arg ) ? ResultWas::Ok : ResultWas::ExpressionFailed );
+ .setResultType( matcher.match( arg ) ? ResultWas::Ok : ResultWas::ExpressionFailed );
return *this;
}
diff --git a/include/internal/catch_impl.hpp b/include/internal/catch_impl.hpp
index 93650761..b4c4662b 100644
--- a/include/internal/catch_impl.hpp
+++ b/include/internal/catch_impl.hpp
@@ -60,6 +60,11 @@ namespace Catch {
TagExtracter::~TagExtracter() {}
TagExpressionParser::~TagExpressionParser() {}
+ Matchers::Impl::StdString::Equals::~Equals() {}
+ Matchers::Impl::StdString::Contains::~Contains() {}
+ Matchers::Impl::StdString::StartsWith::~StartsWith() {}
+ Matchers::Impl::StdString::EndsWith::~EndsWith() {}
+
void Config::dummy() {}
INTERNAL_CATCH_REGISTER_REPORTER( "basic", BasicReporter )
diff --git a/include/internal/catch_matchers.hpp b/include/internal/catch_matchers.hpp
index 79f9a2af..b0a697e5 100644
--- a/include/internal/catch_matchers.hpp
+++ b/include/internal/catch_matchers.hpp
@@ -11,74 +11,189 @@
namespace Catch {
namespace Matchers {
namespace Impl {
- namespace StdString {
- struct Equals {
- Equals( const std::string& str ) : m_str( str ){}
-
- bool operator()( const std::string& str ) const
+ template<typename ExpressionT>
+ struct Matcher : SharedImpl<IShared>
+ {
+ virtual ~Matcher() {}
+ virtual Ptr<Matcher> clone() const = 0;
+ virtual bool match( const ExpressionT& expr ) const = 0;
+ virtual std::string toString() const = 0;
+ };
+
+ template<typename DerivedT, typename ExpressionT>
+ struct MatcherImpl : Matcher<ExpressionT> {
+
+ virtual Ptr<Matcher<ExpressionT> > clone() const {
+ return Ptr<Matcher<ExpressionT> >( new DerivedT( static_cast<const DerivedT&>( *this ) ) );
+ }
+ };
+
+ namespace Generic {
+
+ template<typename ExpressionT>
+ class AllOf : public MatcherImpl<AllOf<ExpressionT>, ExpressionT> {
+ public:
+
+ AllOf() {}
+ AllOf( const AllOf& other ) : m_matchers( other.m_matchers ) {}
+
+ AllOf& add( const Matcher<ExpressionT>& matcher ) {
+ m_matchers.push_back( matcher.clone() );
+ return *this;
+ }
+ virtual bool match( const ExpressionT& expr ) const
{
- return str == m_str;
+ for( std::size_t i = 0; i < m_matchers.size(); ++i )
+ if( !m_matchers[i]->match( expr ) )
+ return false;
+ return true;
+ }
+ virtual std::string toString() const {
+ std::ostringstream oss;
+ oss << "( ";
+ for( std::size_t i = 0; i < m_matchers.size(); ++i ) {
+ if( i != 0 )
+ oss << " and ";
+ oss << m_matchers[i]->toString();
+ }
+ oss << " )";
+ return oss.str();
+ }
+
+ private:
+ std::vector<Ptr<Matcher<ExpressionT> > > m_matchers;
+ };
+
+ template<typename ExpressionT>
+ class AnyOf : public MatcherImpl<AnyOf<ExpressionT>, ExpressionT> {
+ public:
+
+ AnyOf() {}
+ AnyOf( const AnyOf& other ) : m_matchers( other.m_matchers ) {}
+
+ AnyOf& add( const Matcher<ExpressionT>& matcher ) {
+ m_matchers.push_back( matcher.clone() );
+ return *this;
}
-
- friend std::ostream& operator<<( std::ostream& os, const Equals& matcher )
+ virtual bool match( const ExpressionT& expr ) const
{
- os << "equals: \"" << matcher.m_str << "\"";
- return os;
+ for( std::size_t i = 0; i < m_matchers.size(); ++i )
+ if( m_matchers[i]->match( expr ) )
+ return true;
+ return false;
+ }
+ virtual std::string toString() const {
+ std::ostringstream oss;
+ oss << "( ";
+ for( std::size_t i = 0; i < m_matchers.size(); ++i ) {
+ if( i != 0 )
+ oss << " or ";
+ oss << m_matchers[i]->toString();
+ }
+ oss << " )";
+ return oss.str();
}
+
+ private:
+ std::vector<Ptr<Matcher<ExpressionT> > > m_matchers;
+ };
+
+ }
+
+ namespace StdString {
+
+ struct Equals : MatcherImpl<Equals, std::string> {
+ Equals( const std::string& str ) : m_str( str ){}
+ Equals( const Equals& other ) : m_str( other.m_str ){}
+
+ virtual ~Equals();
+
+ virtual bool match( const std::string& expr ) const {
+ return m_str == expr;
+ }
+ virtual std::string toString() const {
+ return "equals: \"" + m_str + "\"";
+ }
+
std::string m_str;
};
- struct Contains {
+ struct Contains : MatcherImpl<Contains, std::string> {
Contains( const std::string& substr ) : m_substr( substr ){}
-
- bool operator()( const std::string& str ) const
- {
- return str.find( m_substr ) != std::string::npos;
+ Contains( const Contains& other ) : m_substr( other.m_substr ){}
+
+ virtual ~Contains();
+
+ virtual bool match( const std::string& expr ) const {
+ return expr.find( m_substr ) != std::string::npos;
}
-
- friend std::ostream& operator<<( std::ostream& os, const Contains& matcher )
- {
- os << "contains: \"" << matcher.m_substr << "\"";
- return os;
+ virtual std::string toString() const {
+ return "contains: \"" + m_substr + "\"";
}
+
std::string m_substr;
};
- struct StartsWith {
+ struct StartsWith : MatcherImpl<StartsWith, std::string> {
StartsWith( const std::string& substr ) : m_substr( substr ){}
-
- bool operator()( const std::string& str ) const
- {
- return str.find( m_substr ) == 0;
+ StartsWith( const StartsWith& other ) : m_substr( other.m_substr ){}
+
+ virtual ~StartsWith();
+
+ virtual bool match( const std::string& expr ) const {
+ return expr.find( m_substr ) == 0;
}
-
- friend std::ostream& operator<<( std::ostream& os, const StartsWith& matcher )
- {
- os << "starts with: \"" << matcher.m_substr << "\"";
- return os;
+ virtual std::string toString() const {
+ return "starts with: \"" + m_substr + "\"";
}
+
std::string m_substr;
};
- struct EndsWith {
+ struct EndsWith : MatcherImpl<EndsWith, std::string> {
EndsWith( const std::string& substr ) : m_substr( substr ){}
-
- bool operator()( const std::string& str ) const
- {
- return str.find( m_substr ) == str.size() - m_substr.size();
+ EndsWith( const EndsWith& other ) : m_substr( other.m_substr ){}
+
+ virtual ~EndsWith();
+
+ virtual bool match( const std::string& expr ) const {
+ return expr.find( m_substr ) == expr.size() - m_substr.size();
}
-
- friend std::ostream& operator<<( std::ostream& os, const EndsWith& matcher )
- {
- os << "ends with: \"" << matcher.m_substr << "\"";
- return os;
+ virtual std::string toString() const {
+ return "ends with: \"" + m_substr + "\"";
}
+
std::string m_substr;
};
} // namespace StdString
} // namespace Impl
-
+
+ // The following functions create the actual matcher objects.
+ // This allows the types to be inferred
+ template<typename ExpressionT>
+ inline Impl::Generic::AllOf<ExpressionT> AllOf( const Impl::Matcher<ExpressionT>& m1,
+ const Impl::Matcher<ExpressionT>& m2 ) {
+ return Impl::Generic::AllOf<ExpressionT>().add( m1 ).add( m2 );
+ }
+ template<typename ExpressionT>
+ inline Impl::Generic::AllOf<ExpressionT> AllOf( const Impl::Matcher<ExpressionT>& m1,
+ const Impl::Matcher<ExpressionT>& m2,
+ const Impl::Matcher<ExpressionT>& m3 ) {
+ return Impl::Generic::AllOf<ExpressionT>().add( m1 ).add( m2 ).add( m3 );
+ }
+ template<typename ExpressionT>
+ inline Impl::Generic::AnyOf<ExpressionT> AnyOf( const Impl::Matcher<ExpressionT>& m1,
+ const Impl::Matcher<ExpressionT>& m2 ) {
+ return Impl::Generic::AnyOf<ExpressionT>().add( m1 ).add( m2 );
+ }
+ template<typename ExpressionT>
+ inline Impl::Generic::AnyOf<ExpressionT> AnyOf( const Impl::Matcher<ExpressionT>& m1,
+ const Impl::Matcher<ExpressionT>& m2,
+ const Impl::Matcher<ExpressionT>& m3 ) {
+ return Impl::Generic::AnyOf<ExpressionT>().add( m1 ).add( m2 ).add( m3 );
+ }
+
inline Impl::StdString::Equals Equals( const std::string& str ){ return Impl::StdString::Equals( str ); }
inline Impl::StdString::Contains Contains( const std::string& substr ){ return Impl::StdString::Contains( substr ); }
inline Impl::StdString::StartsWith StartsWith( const std::string& substr ){ return Impl::StdString::StartsWith( substr ); }
diff --git a/include/internal/catch_ptr.hpp b/include/internal/catch_ptr.hpp
index a688bc4f..19e92a8d 100644
--- a/include/internal/catch_ptr.hpp
+++ b/include/internal/catch_ptr.hpp
@@ -78,7 +78,7 @@ namespace Catch {
struct SharedImpl : T {
SharedImpl() : m_rc( 0 ){}
-
+
virtual void addRef(){
++m_rc;
}