catch2/include/internal/catch_matchers.hpp

211 lines
7.7 KiB
C++
Raw Normal View History

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 {
2012-10-12 08:58:17 +02:00
template<typename ExpressionT>
struct Matcher : SharedImpl<IShared>
{
typedef ExpressionT ExpressionType;
2012-10-12 08:58:17 +02:00
virtual ~Matcher() {}
virtual Ptr<Matcher> clone() const = 0;
virtual bool match( ExpressionT const& expr ) const = 0;
2012-10-12 08:58:17 +02:00
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
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();
}
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;
2012-05-24 09:27:50 +02:00
}
2012-10-12 08:58:17 +02:00
virtual bool match( const ExpressionT& 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
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 + "\"";
}
2012-05-24 09:27:50 +02:00
std::string m_str;
};
2012-10-12 08:58:17 +02:00
struct Contains : MatcherImpl<Contains, std::string> {
2012-03-04 21:10:36 +01:00
Contains( const std::string& substr ) : m_substr( substr ){}
2012-10-12 08:58:17 +02:00
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;
2012-03-04 21:10:36 +01:00
}
2012-10-12 08:58:17 +02:00
virtual std::string toString() const {
return "contains: \"" + m_substr + "\"";
2012-03-04 21:10:36 +01:00
}
2012-10-12 08:58:17 +02:00
2012-03-04 21:10:36 +01:00
std::string m_substr;
};
2012-10-12 08:58:17 +02:00
struct StartsWith : MatcherImpl<StartsWith, std::string> {
2012-03-04 21:10:36 +01:00
StartsWith( const std::string& substr ) : m_substr( substr ){}
2012-10-12 08:58:17 +02:00
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;
2012-03-04 21:10:36 +01:00
}
2012-10-12 08:58:17 +02:00
virtual std::string toString() const {
return "starts with: \"" + m_substr + "\"";
2012-03-04 21:10:36 +01:00
}
2012-10-12 08:58:17 +02:00
2012-03-04 21:10:36 +01:00
std::string m_substr;
};
2012-10-12 08:58:17 +02:00
struct EndsWith : MatcherImpl<EndsWith, std::string> {
2012-03-04 21:10:36 +01:00
EndsWith( const std::string& substr ) : m_substr( substr ){}
2012-10-12 08:58:17 +02:00
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();
2012-03-04 21:10:36 +01:00
}
2012-10-12 08:58:17 +02:00
virtual std::string toString() const {
return "ends with: \"" + m_substr + "\"";
2012-03-04 21:10:36 +01:00
}
2012-10-12 08:58:17 +02:00
2012-03-04 21:10:36 +01:00
std::string m_substr;
};
} // 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
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 );
}
2012-05-24 09:27:50 +02:00
inline Impl::StdString::Equals Equals( const std::string& str ){ return Impl::StdString::Equals( str ); }
2012-03-04 21:10:36 +01:00
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 ); }
inline Impl::StdString::EndsWith EndsWith( const std::string& substr ){ return Impl::StdString::EndsWith( substr ); }
} // namespace Matchers
using namespace Matchers;
} // namespace Catch
#endif // TWOBLUECUBES_CATCH_MATCHERS_HPP_INCLUDED