2012-05-09 20:37:14 +02:00
|
|
|
/*
|
|
|
|
* Created by Phil Nash on 04/03/2012.
|
|
|
|
* Copyright (c) 2012 Two Blue Cubes Ltd. All rights reserved.
|
|
|
|
*
|
|
|
|
* Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
* file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
*/
|
2012-03-04 21:10:36 +01:00
|
|
|
#ifndef TWOBLUECUBES_CATCH_MATCHERS_HPP_INCLUDED
|
|
|
|
#define TWOBLUECUBES_CATCH_MATCHERS_HPP_INCLUDED
|
|
|
|
|
2012-05-15 08:42:26 +02:00
|
|
|
namespace Catch {
|
|
|
|
namespace Matchers {
|
|
|
|
namespace Impl {
|
|
|
|
|
2015-11-05 19:46:00 +01:00
|
|
|
namespace Generic {
|
|
|
|
template<typename ExpressionT> class AllOf;
|
|
|
|
template<typename ExpressionT> class AnyOf;
|
|
|
|
template<typename ExpressionT> class Not;
|
|
|
|
}
|
|
|
|
|
2012-10-12 08:58:17 +02:00
|
|
|
template<typename ExpressionT>
|
|
|
|
struct Matcher : SharedImpl<IShared>
|
|
|
|
{
|
2012-12-06 09:41:38 +01:00
|
|
|
typedef ExpressionT ExpressionType;
|
|
|
|
|
2012-10-12 08:58:17 +02:00
|
|
|
virtual ~Matcher() {}
|
|
|
|
virtual Ptr<Matcher> clone() const = 0;
|
2012-12-06 09:41:38 +01:00
|
|
|
virtual bool match( ExpressionT const& expr ) const = 0;
|
2012-10-12 08:58:17 +02:00
|
|
|
virtual std::string toString() const = 0;
|
2015-11-05 19:46:00 +01:00
|
|
|
|
|
|
|
Generic::AllOf<ExpressionT> operator && ( Matcher<ExpressionT> const& other ) const;
|
|
|
|
Generic::AnyOf<ExpressionT> operator || ( Matcher<ExpressionT> const& other ) const;
|
|
|
|
Generic::Not<ExpressionT> operator ! () const;
|
2012-10-12 08:58:17 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
template<typename DerivedT, typename ExpressionT>
|
|
|
|
struct MatcherImpl : Matcher<ExpressionT> {
|
|
|
|
|
|
|
|
virtual Ptr<Matcher<ExpressionT> > clone() const {
|
2013-04-23 19:58:56 +02:00
|
|
|
return Ptr<Matcher<ExpressionT> >( new DerivedT( static_cast<DerivedT const&>( *this ) ) );
|
2012-10-12 08:58:17 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
namespace Generic {
|
2015-08-12 11:05:16 +02:00
|
|
|
template<typename ExpressionT>
|
2015-11-06 19:05:35 +01:00
|
|
|
class Not : public MatcherImpl<Not<ExpressionT>, ExpressionT> {
|
|
|
|
public:
|
2015-11-05 19:46:00 +01:00
|
|
|
explicit Not( Matcher<ExpressionT> const& matcher ) : m_matcher(matcher.clone()) {}
|
2015-08-12 11:05:16 +02:00
|
|
|
Not( Not const& other ) : m_matcher( other.m_matcher ) {}
|
|
|
|
|
2015-08-24 07:28:25 +02:00
|
|
|
virtual bool match( ExpressionT const& expr ) const CATCH_OVERRIDE {
|
2015-08-12 11:05:16 +02:00
|
|
|
return !m_matcher->match( expr );
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual std::string toString() const CATCH_OVERRIDE {
|
|
|
|
return "not " + m_matcher->toString();
|
|
|
|
}
|
2015-11-06 19:05:35 +01:00
|
|
|
private:
|
2015-08-12 11:05:16 +02:00
|
|
|
Ptr< Matcher<ExpressionT> > m_matcher;
|
|
|
|
};
|
2012-10-12 08:58:17 +02:00
|
|
|
|
|
|
|
template<typename ExpressionT>
|
|
|
|
class AllOf : public MatcherImpl<AllOf<ExpressionT>, ExpressionT> {
|
|
|
|
public:
|
|
|
|
|
|
|
|
AllOf() {}
|
2013-04-23 19:58:56 +02:00
|
|
|
AllOf( AllOf const& other ) : m_matchers( other.m_matchers ) {}
|
2012-10-12 08:58:17 +02:00
|
|
|
|
2013-04-23 19:58:56 +02:00
|
|
|
AllOf& add( Matcher<ExpressionT> const& matcher ) {
|
2012-10-12 08:58:17 +02:00
|
|
|
m_matchers.push_back( matcher.clone() );
|
|
|
|
return *this;
|
|
|
|
}
|
2013-04-23 19:58:56 +02:00
|
|
|
virtual bool match( ExpressionT const& expr ) const
|
2012-05-24 09:27:50 +02:00
|
|
|
{
|
2012-10-12 08:58:17 +02:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2015-11-05 19:46:00 +01:00
|
|
|
AllOf operator && ( Matcher<ExpressionT> const& other ) const {
|
|
|
|
AllOf allOfExpr( *this );
|
|
|
|
allOfExpr.add( other );
|
|
|
|
return allOfExpr;
|
|
|
|
}
|
|
|
|
|
2012-10-12 08:58:17 +02:00
|
|
|
private:
|
|
|
|
std::vector<Ptr<Matcher<ExpressionT> > > m_matchers;
|
|
|
|
};
|
2013-07-03 20:14:59 +02:00
|
|
|
|
2012-10-12 08:58:17 +02:00
|
|
|
template<typename ExpressionT>
|
|
|
|
class AnyOf : public MatcherImpl<AnyOf<ExpressionT>, ExpressionT> {
|
|
|
|
public:
|
|
|
|
|
|
|
|
AnyOf() {}
|
2013-04-23 19:58:56 +02:00
|
|
|
AnyOf( AnyOf const& other ) : m_matchers( other.m_matchers ) {}
|
2012-10-12 08:58:17 +02:00
|
|
|
|
2013-04-23 19:58:56 +02:00
|
|
|
AnyOf& add( Matcher<ExpressionT> const& matcher ) {
|
2012-10-12 08:58:17 +02:00
|
|
|
m_matchers.push_back( matcher.clone() );
|
|
|
|
return *this;
|
2012-05-24 09:27:50 +02:00
|
|
|
}
|
2013-04-23 19:58:56 +02:00
|
|
|
virtual bool match( ExpressionT const& expr ) const
|
2012-05-24 09:27:50 +02:00
|
|
|
{
|
2012-10-12 08:58:17 +02:00
|
|
|
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();
|
2012-05-24 09:27:50 +02:00
|
|
|
}
|
2012-10-12 08:58:17 +02:00
|
|
|
|
2015-11-05 19:46:00 +01:00
|
|
|
AnyOf operator || ( Matcher<ExpressionT> const& other ) const {
|
|
|
|
AnyOf anyOfExpr( *this );
|
|
|
|
anyOfExpr.add( other );
|
|
|
|
return anyOfExpr;
|
|
|
|
}
|
|
|
|
|
2012-10-12 08:58:17 +02:00
|
|
|
private:
|
|
|
|
std::vector<Ptr<Matcher<ExpressionT> > > m_matchers;
|
|
|
|
};
|
2015-11-05 19:46:00 +01:00
|
|
|
|
|
|
|
} // 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 );
|
2012-10-12 08:58:17 +02:00
|
|
|
}
|
2015-11-05 19:46:00 +01:00
|
|
|
|
2012-10-12 08:58:17 +02:00
|
|
|
|
|
|
|
namespace StdString {
|
|
|
|
|
2013-04-23 19:58:56 +02:00
|
|
|
inline std::string makeString( std::string const& str ) { return str; }
|
2013-04-23 08:59:56 +02:00
|
|
|
inline std::string makeString( const char* str ) { return str ? std::string( str ) : std::string(); }
|
2013-07-03 20:14:59 +02:00
|
|
|
|
2015-07-16 00:02:25 +02:00
|
|
|
struct CasedString
|
|
|
|
{
|
|
|
|
CasedString( std::string const& str, CaseSensitive::Choice caseSensitivity )
|
|
|
|
: m_caseSensitivity( caseSensitivity ),
|
|
|
|
m_str( adjustString( str ) )
|
|
|
|
{}
|
|
|
|
std::string adjustString( std::string const& str ) const {
|
|
|
|
return m_caseSensitivity == CaseSensitive::No
|
|
|
|
? toLower( str )
|
|
|
|
: str;
|
2015-11-04 19:01:28 +01:00
|
|
|
|
2015-07-16 00:02:25 +02:00
|
|
|
}
|
|
|
|
std::string toStringSuffix() const
|
|
|
|
{
|
|
|
|
return m_caseSensitivity == CaseSensitive::No
|
|
|
|
? " (case insensitive)"
|
|
|
|
: "";
|
|
|
|
}
|
|
|
|
CaseSensitive::Choice m_caseSensitivity;
|
|
|
|
std::string m_str;
|
|
|
|
};
|
2015-11-04 19:01:28 +01:00
|
|
|
|
2012-10-12 08:58:17 +02:00
|
|
|
struct Equals : MatcherImpl<Equals, std::string> {
|
2015-07-16 00:02:25 +02:00
|
|
|
Equals( std::string const& str, CaseSensitive::Choice caseSensitivity = CaseSensitive::Yes )
|
|
|
|
: m_data( str, caseSensitivity )
|
|
|
|
{}
|
|
|
|
Equals( Equals const& other ) : m_data( other.m_data ){}
|
2012-10-12 08:58:17 +02:00
|
|
|
|
|
|
|
virtual ~Equals();
|
|
|
|
|
2013-04-23 19:58:56 +02:00
|
|
|
virtual bool match( std::string const& expr ) const {
|
2015-07-16 00:02:25 +02:00
|
|
|
return m_data.m_str == m_data.adjustString( expr );;
|
2012-10-12 08:58:17 +02:00
|
|
|
}
|
|
|
|
virtual std::string toString() const {
|
2015-07-16 00:02:25 +02:00
|
|
|
return "equals: \"" + m_data.m_str + "\"" + m_data.toStringSuffix();
|
2012-10-12 08:58:17 +02:00
|
|
|
}
|
|
|
|
|
2015-07-16 00:02:25 +02:00
|
|
|
CasedString m_data;
|
2012-05-24 09:27:50 +02:00
|
|
|
};
|
2013-07-03 20:14:59 +02:00
|
|
|
|
2012-10-12 08:58:17 +02:00
|
|
|
struct Contains : MatcherImpl<Contains, std::string> {
|
2015-07-16 00:02:25 +02:00
|
|
|
Contains( std::string const& substr, CaseSensitive::Choice caseSensitivity = CaseSensitive::Yes )
|
|
|
|
: m_data( substr, caseSensitivity ){}
|
|
|
|
Contains( Contains const& other ) : m_data( other.m_data ){}
|
2012-10-12 08:58:17 +02:00
|
|
|
|
|
|
|
virtual ~Contains();
|
|
|
|
|
2013-04-23 19:58:56 +02:00
|
|
|
virtual bool match( std::string const& expr ) const {
|
2015-07-16 00:02:25 +02:00
|
|
|
return m_data.adjustString( expr ).find( m_data.m_str ) != std::string::npos;
|
2012-03-04 21:10:36 +01:00
|
|
|
}
|
2012-10-12 08:58:17 +02:00
|
|
|
virtual std::string toString() const {
|
2015-07-16 00:02:25 +02:00
|
|
|
return "contains: \"" + m_data.m_str + "\"" + m_data.toStringSuffix();
|
2012-03-04 21:10:36 +01:00
|
|
|
}
|
2012-10-12 08:58:17 +02:00
|
|
|
|
2015-07-16 00:02:25 +02:00
|
|
|
CasedString m_data;
|
2012-03-04 21:10:36 +01:00
|
|
|
};
|
2013-07-03 20:14:59 +02:00
|
|
|
|
2012-10-12 08:58:17 +02:00
|
|
|
struct StartsWith : MatcherImpl<StartsWith, std::string> {
|
2015-07-16 00:02:25 +02:00
|
|
|
StartsWith( std::string const& substr, CaseSensitive::Choice caseSensitivity = CaseSensitive::Yes )
|
|
|
|
: m_data( substr, caseSensitivity ){}
|
2015-11-04 19:01:28 +01:00
|
|
|
|
2015-07-16 00:02:25 +02:00
|
|
|
StartsWith( StartsWith const& other ) : m_data( other.m_data ){}
|
2012-10-12 08:58:17 +02:00
|
|
|
|
|
|
|
virtual ~StartsWith();
|
|
|
|
|
2013-04-23 19:58:56 +02:00
|
|
|
virtual bool match( std::string const& expr ) const {
|
2015-07-16 00:02:25 +02:00
|
|
|
return m_data.adjustString( expr ).find( m_data.m_str ) == 0;
|
2012-03-04 21:10:36 +01:00
|
|
|
}
|
2012-10-12 08:58:17 +02:00
|
|
|
virtual std::string toString() const {
|
2015-07-16 00:02:25 +02:00
|
|
|
return "starts with: \"" + m_data.m_str + "\"" + m_data.toStringSuffix();
|
2012-03-04 21:10:36 +01:00
|
|
|
}
|
2012-10-12 08:58:17 +02:00
|
|
|
|
2015-07-16 00:02:25 +02:00
|
|
|
CasedString m_data;
|
2012-03-04 21:10:36 +01:00
|
|
|
};
|
2013-07-03 20:14:59 +02:00
|
|
|
|
2012-10-12 08:58:17 +02:00
|
|
|
struct EndsWith : MatcherImpl<EndsWith, std::string> {
|
2015-07-16 00:02:25 +02:00
|
|
|
EndsWith( std::string const& substr, CaseSensitive::Choice caseSensitivity = CaseSensitive::Yes )
|
|
|
|
: m_data( substr, caseSensitivity ){}
|
|
|
|
EndsWith( EndsWith const& other ) : m_data( other.m_data ){}
|
2012-10-12 08:58:17 +02:00
|
|
|
|
|
|
|
virtual ~EndsWith();
|
|
|
|
|
2013-04-23 19:58:56 +02:00
|
|
|
virtual bool match( std::string const& expr ) const {
|
2015-07-16 00:02:25 +02:00
|
|
|
return m_data.adjustString( expr ).find( m_data.m_str ) == expr.size() - m_data.m_str.size();
|
2012-03-04 21:10:36 +01:00
|
|
|
}
|
2012-10-12 08:58:17 +02:00
|
|
|
virtual std::string toString() const {
|
2015-07-16 00:02:25 +02:00
|
|
|
return "ends with: \"" + m_data.m_str + "\"" + m_data.toStringSuffix();
|
2012-03-04 21:10:36 +01:00
|
|
|
}
|
2012-10-12 08:58:17 +02:00
|
|
|
|
2015-07-16 00:02:25 +02:00
|
|
|
CasedString m_data;
|
2012-03-04 21:10:36 +01:00
|
|
|
};
|
|
|
|
} // namespace StdString
|
|
|
|
} // namespace Impl
|
2012-10-12 08:58:17 +02:00
|
|
|
|
|
|
|
// The following functions create the actual matcher objects.
|
|
|
|
// This allows the types to be inferred
|
2015-08-12 11:05:16 +02:00
|
|
|
template<typename ExpressionT>
|
|
|
|
inline Impl::Generic::Not<ExpressionT> Not( Impl::Matcher<ExpressionT> const& m ) {
|
|
|
|
return Impl::Generic::Not<ExpressionT>( m );
|
|
|
|
}
|
|
|
|
|
2012-10-12 08:58:17 +02:00
|
|
|
template<typename ExpressionT>
|
2013-04-23 19:58:56 +02:00
|
|
|
inline Impl::Generic::AllOf<ExpressionT> AllOf( Impl::Matcher<ExpressionT> const& m1,
|
|
|
|
Impl::Matcher<ExpressionT> const& m2 ) {
|
2012-10-12 08:58:17 +02:00
|
|
|
return Impl::Generic::AllOf<ExpressionT>().add( m1 ).add( m2 );
|
|
|
|
}
|
|
|
|
template<typename ExpressionT>
|
2013-04-23 19:58:56 +02:00
|
|
|
inline Impl::Generic::AllOf<ExpressionT> AllOf( Impl::Matcher<ExpressionT> const& m1,
|
|
|
|
Impl::Matcher<ExpressionT> const& m2,
|
|
|
|
Impl::Matcher<ExpressionT> const& m3 ) {
|
2012-10-12 08:58:17 +02:00
|
|
|
return Impl::Generic::AllOf<ExpressionT>().add( m1 ).add( m2 ).add( m3 );
|
|
|
|
}
|
|
|
|
template<typename ExpressionT>
|
2013-04-23 19:58:56 +02:00
|
|
|
inline Impl::Generic::AnyOf<ExpressionT> AnyOf( Impl::Matcher<ExpressionT> const& m1,
|
|
|
|
Impl::Matcher<ExpressionT> const& m2 ) {
|
2012-10-12 08:58:17 +02:00
|
|
|
return Impl::Generic::AnyOf<ExpressionT>().add( m1 ).add( m2 );
|
|
|
|
}
|
|
|
|
template<typename ExpressionT>
|
2013-04-23 19:58:56 +02:00
|
|
|
inline Impl::Generic::AnyOf<ExpressionT> AnyOf( Impl::Matcher<ExpressionT> const& m1,
|
|
|
|
Impl::Matcher<ExpressionT> const& m2,
|
|
|
|
Impl::Matcher<ExpressionT> const& m3 ) {
|
2012-10-12 08:58:17 +02:00
|
|
|
return Impl::Generic::AnyOf<ExpressionT>().add( m1 ).add( m2 ).add( m3 );
|
|
|
|
}
|
|
|
|
|
2015-07-16 00:02:25 +02:00
|
|
|
inline Impl::StdString::Equals Equals( std::string const& str, CaseSensitive::Choice caseSensitivity = CaseSensitive::Yes ) {
|
|
|
|
return Impl::StdString::Equals( str, caseSensitivity );
|
2013-04-23 08:59:56 +02:00
|
|
|
}
|
2015-07-16 00:02:25 +02:00
|
|
|
inline Impl::StdString::Equals Equals( const char* str, CaseSensitive::Choice caseSensitivity = CaseSensitive::Yes ) {
|
|
|
|
return Impl::StdString::Equals( Impl::StdString::makeString( str ), caseSensitivity );
|
2013-04-23 08:59:56 +02:00
|
|
|
}
|
2015-07-16 00:02:25 +02:00
|
|
|
inline Impl::StdString::Contains Contains( std::string const& substr, CaseSensitive::Choice caseSensitivity = CaseSensitive::Yes ) {
|
|
|
|
return Impl::StdString::Contains( substr, caseSensitivity );
|
2013-04-23 08:59:56 +02:00
|
|
|
}
|
2015-07-16 00:02:25 +02:00
|
|
|
inline Impl::StdString::Contains Contains( const char* substr, CaseSensitive::Choice caseSensitivity = CaseSensitive::Yes ) {
|
|
|
|
return Impl::StdString::Contains( Impl::StdString::makeString( substr ), caseSensitivity );
|
2013-04-23 08:59:56 +02:00
|
|
|
}
|
2013-04-23 19:58:56 +02:00
|
|
|
inline Impl::StdString::StartsWith StartsWith( std::string const& substr ) {
|
2013-04-23 08:59:56 +02:00
|
|
|
return Impl::StdString::StartsWith( substr );
|
|
|
|
}
|
|
|
|
inline Impl::StdString::StartsWith StartsWith( const char* substr ) {
|
|
|
|
return Impl::StdString::StartsWith( Impl::StdString::makeString( substr ) );
|
|
|
|
}
|
2013-04-23 19:58:56 +02:00
|
|
|
inline Impl::StdString::EndsWith EndsWith( std::string const& substr ) {
|
2013-04-23 08:59:56 +02:00
|
|
|
return Impl::StdString::EndsWith( substr );
|
|
|
|
}
|
|
|
|
inline Impl::StdString::EndsWith EndsWith( const char* substr ) {
|
|
|
|
return Impl::StdString::EndsWith( Impl::StdString::makeString( substr ) );
|
|
|
|
}
|
2013-07-03 20:14:59 +02:00
|
|
|
|
2012-03-04 21:10:36 +01:00
|
|
|
} // namespace Matchers
|
2013-07-03 20:14:59 +02:00
|
|
|
|
2012-03-04 21:10:36 +01:00
|
|
|
using namespace Matchers;
|
2013-07-03 20:14:59 +02:00
|
|
|
|
2012-03-04 21:10:36 +01:00
|
|
|
} // namespace Catch
|
|
|
|
|
|
|
|
#endif // TWOBLUECUBES_CATCH_MATCHERS_HPP_INCLUDED
|