2012-05-11 20:15:54 +02:00
|
|
|
/*
|
|
|
|
* Created by Phil on 11/5/2012.
|
|
|
|
* Copyright 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)
|
|
|
|
*/
|
|
|
|
#ifndef TWOBLUECUBES_CATCH_EXPRESSION_BUILDER_HPP_INCLUDED
|
|
|
|
#define TWOBLUECUBES_CATCH_EXPRESSION_BUILDER_HPP_INCLUDED
|
|
|
|
|
|
|
|
#include "catch_expression.hpp"
|
2012-08-08 09:58:28 +02:00
|
|
|
#include "catch_resultinfo_builder.h"
|
2012-05-11 20:15:54 +02:00
|
|
|
#include "catch_tostring.hpp"
|
2012-08-08 09:50:38 +02:00
|
|
|
#include "catch_resultinfo.h"
|
2012-05-11 20:15:54 +02:00
|
|
|
#include "catch_result_type.h"
|
|
|
|
#include "catch_context.h"
|
|
|
|
#include "catch_common.h"
|
|
|
|
#include <sstream>
|
|
|
|
|
2012-05-11 20:22:28 +02:00
|
|
|
namespace Catch {
|
2012-05-11 20:15:54 +02:00
|
|
|
|
2012-05-11 20:22:28 +02:00
|
|
|
class ExpressionBuilder {
|
2012-05-11 20:15:54 +02:00
|
|
|
public:
|
|
|
|
|
2012-05-11 20:22:28 +02:00
|
|
|
ExpressionBuilder( const SourceLineInfo& lineInfo,
|
|
|
|
const char* macroName,
|
|
|
|
const char* expr = "",
|
2012-10-09 12:59:11 +02:00
|
|
|
bool isFalse = false )
|
2012-10-05 19:35:01 +02:00
|
|
|
: m_messageStream()
|
|
|
|
{
|
|
|
|
m_result
|
|
|
|
.setCapturedExpression( expr )
|
2012-10-09 12:59:11 +02:00
|
|
|
.setIsFalse( isFalse )
|
2012-10-05 19:35:01 +02:00
|
|
|
.setLineInfo( lineInfo )
|
|
|
|
.setMacroName( macroName );
|
|
|
|
}
|
2012-05-11 20:15:54 +02:00
|
|
|
|
|
|
|
template<typename T>
|
2012-05-11 20:22:28 +02:00
|
|
|
Expression<const T&> operator->* ( const T & operand ) {
|
|
|
|
Expression<const T&> expr( m_result, operand );
|
2012-05-11 20:15:54 +02:00
|
|
|
return expr;
|
|
|
|
}
|
|
|
|
|
2012-05-11 20:22:28 +02:00
|
|
|
Expression<bool> operator->* ( bool value ) {
|
2012-05-11 20:15:54 +02:00
|
|
|
Expression<bool> expr( m_result, value );
|
|
|
|
return expr;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2012-05-11 20:22:28 +02:00
|
|
|
ExpressionBuilder& operator << ( const T & value ) {
|
2012-05-11 20:15:54 +02:00
|
|
|
m_messageStream << Catch::toString( value );
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename MatcherT, typename ArgT>
|
2012-05-11 20:22:28 +02:00
|
|
|
ExpressionBuilder& acceptMatcher( const MatcherT& matcher,
|
|
|
|
const ArgT& arg,
|
|
|
|
const std::string& matcherCallAsString ) {
|
2012-10-12 08:58:17 +02:00
|
|
|
std::string matcherAsString = matcher.toString();
|
2012-05-11 20:15:54 +02:00
|
|
|
if( matcherAsString == "{?}" )
|
|
|
|
matcherAsString = matcherCallAsString;
|
2012-10-05 19:35:01 +02:00
|
|
|
m_result
|
|
|
|
.setLhs( Catch::toString( arg ) )
|
|
|
|
.setRhs( matcherAsString )
|
|
|
|
.setOp( "matches" )
|
2012-10-12 08:58:17 +02:00
|
|
|
.setResultType( matcher.match( arg ) ? ResultWas::Ok : ResultWas::ExpressionFailed );
|
2012-05-11 20:15:54 +02:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename MatcherT, typename ArgT>
|
2012-05-11 20:22:28 +02:00
|
|
|
ExpressionBuilder& acceptMatcher( const MatcherT& matcher,
|
|
|
|
ArgT* arg,
|
|
|
|
const std::string& matcherCallAsString ) {
|
2012-10-12 08:58:17 +02:00
|
|
|
std::string matcherAsString = matcher.toString();
|
2012-05-11 20:15:54 +02:00
|
|
|
if( matcherAsString == "{?}" )
|
|
|
|
matcherAsString = matcherCallAsString;
|
2012-10-05 19:35:01 +02:00
|
|
|
m_result
|
|
|
|
.setLhs( Catch::toString( arg ) )
|
|
|
|
.setRhs( matcherAsString )
|
|
|
|
.setOp( "matches" )
|
2012-10-12 08:58:17 +02:00
|
|
|
.setResultType( matcher.match( arg ) ? ResultWas::Ok : ResultWas::ExpressionFailed );
|
2012-05-11 20:15:54 +02:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2012-05-11 20:22:28 +02:00
|
|
|
ExpressionBuilder& setResultType( ResultWas::OfType resultType ) {
|
2012-05-11 20:15:54 +02:00
|
|
|
m_result.setResultType( resultType );
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2012-05-11 20:22:28 +02:00
|
|
|
operator ResultInfoBuilder&() {
|
2012-05-11 20:15:54 +02:00
|
|
|
m_result.setMessage( m_messageStream.str() );
|
|
|
|
return m_result;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
ResultInfoBuilder m_result;
|
|
|
|
std::ostringstream m_messageStream;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // end namespace Catch
|
|
|
|
|
|
|
|
#endif // TWOBLUECUBES_CATCH_EXPRESSION_BUILDER_HPP_INCLUDED
|