2012-05-08 19:29:51 +01:00
/*
2012-08-08 08:58:28 +01:00
* Created by Phil on 8/8/2012.
2012-05-08 19:29:51 +01:00
* 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)
*/
2012-10-26 08:45:23 +01:00
#ifndef TWOBLUECUBES_CATCH_EXPRESSIONRESULT_BUILDER_HPP_INCLUDED
#define TWOBLUECUBES_CATCH_EXPRESSIONRESULT_BUILDER_HPP_INCLUDED
2012-05-08 19:29:51 +01:00
2012-10-26 08:45:23 +01:00
#include "catch_expressionresult_builder.h"
2012-05-08 19:29:51 +01:00
2012-10-19 08:01:34 +01:00
#include <assert.h>
2012-05-15 07:42:26 +01:00
namespace Catch {
2012-05-08 19:29:51 +01:00
2012-10-26 08:45:23 +01:00
ExpressionResultBuilder :: ExpressionResultBuilder ( ResultWas :: OfType resultType ) {
2012-10-19 08:01:34 +01:00
m_data . resultType = resultType ;
}
2013-04-23 18:58:56 +01:00
ExpressionResultBuilder :: ExpressionResultBuilder ( ExpressionResultBuilder const & other )
2012-10-18 08:39:44 +01:00
: m_data ( other . m_data ),
m_exprComponents ( other . m_exprComponents )
{
m_stream << other . m_stream . str ();
}
2013-04-23 18:58:56 +01:00
ExpressionResultBuilder & ExpressionResultBuilder :: operator = ( ExpressionResultBuilder const & other ) {
2012-10-18 08:39:44 +01:00
m_data = other . m_data ;
m_exprComponents = other . m_exprComponents ;
2012-10-28 10:27:44 +00:00
m_stream . str ( "" );
2012-10-18 08:39:44 +01:00
m_stream << other . m_stream . str ();
return * this ;
}
2012-10-26 08:45:23 +01:00
ExpressionResultBuilder & ExpressionResultBuilder :: setResultType ( ResultWas :: OfType result ) {
2012-10-19 08:01:34 +01:00
m_data . resultType = result ;
2012-10-05 18:35:01 +01:00
return * this ;
}
2012-10-26 08:45:23 +01:00
ExpressionResultBuilder & ExpressionResultBuilder :: setResultType ( bool result ) {
2012-10-24 21:59:47 +01:00
m_data . resultType = result ? ResultWas :: Ok : ResultWas :: ExpressionFailed ;
2012-10-05 18:35:01 +01:00
return * this ;
}
2012-11-10 18:43:23 +00:00
ExpressionResultBuilder & ExpressionResultBuilder :: endExpression ( ResultDisposition :: Flags resultDisposition ) {
2012-11-13 09:44:52 +00:00
m_exprComponents . shouldNegate = shouldNegate ( resultDisposition );
2012-10-04 08:09:09 +01:00
return * this ;
2012-05-08 19:29:51 +01:00
}
2013-04-23 18:58:56 +01:00
ExpressionResultBuilder & ExpressionResultBuilder :: setLhs ( std :: string const & lhs ) {
2012-10-18 08:39:44 +01:00
m_exprComponents . lhs = lhs ;
2012-10-04 08:09:09 +01:00
return * this ;
2012-05-08 19:29:51 +01:00
}
2013-04-23 18:58:56 +01:00
ExpressionResultBuilder & ExpressionResultBuilder :: setRhs ( std :: string const & rhs ) {
2012-10-18 08:39:44 +01:00
m_exprComponents . rhs = rhs ;
2012-10-04 08:09:09 +01:00
return * this ;
2012-05-08 19:29:51 +01:00
}
2013-04-23 18:58:56 +01:00
ExpressionResultBuilder & ExpressionResultBuilder :: setOp ( std :: string const & op ) {
2012-10-18 08:39:44 +01:00
m_exprComponents . op = op ;
2012-10-04 08:09:09 +01:00
return * this ;
}
2013-04-23 18:58:56 +01:00
AssertionResult ExpressionResultBuilder :: buildResult ( AssertionInfo const & info ) const
2012-10-05 18:35:01 +01:00
{
2012-10-19 08:01:34 +01:00
assert ( m_data . resultType != ResultWas :: Unknown );
2012-10-16 08:33:13 +01:00
AssertionResultData data = m_data ;
2013-07-03 19:14:59 +01:00
2012-10-24 21:59:47 +01:00
// Flip bool results if shouldNegate is set
if ( m_exprComponents . shouldNegate && data . resultType == ResultWas :: Ok )
2012-10-19 08:01:34 +01:00
data . resultType = ResultWas :: ExpressionFailed ;
2012-10-24 21:59:47 +01:00
else if ( m_exprComponents . shouldNegate && data . resultType == ResultWas :: ExpressionFailed )
2012-10-19 08:01:34 +01:00
data . resultType = ResultWas :: Ok ;
2012-10-18 08:39:44 +01:00
data . message = m_stream . str ();
2012-10-24 21:59:47 +01:00
data . reconstructedExpression = reconstructExpression ( info );
if ( m_exprComponents . shouldNegate ) {
if ( m_exprComponents . op == "" )
2012-10-09 11:48:55 +01:00
data . reconstructedExpression = "!" + data . reconstructedExpression ;
2012-10-24 21:59:47 +01:00
else
2012-10-09 11:48:55 +01:00
data . reconstructedExpression = "!(" + data . reconstructedExpression + ")" ;
}
2012-10-26 09:05:36 +01:00
return AssertionResult ( info , data );
2012-05-08 19:29:51 +01:00
}
2013-04-23 18:58:56 +01:00
std :: string ExpressionResultBuilder :: reconstructExpression ( AssertionInfo const & info ) const {
2012-10-18 08:39:44 +01:00
if ( m_exprComponents . op == "" )
2012-10-24 21:59:47 +01:00
return m_exprComponents . lhs . empty () ? info . capturedExpression : m_exprComponents . op + m_exprComponents . lhs ;
2012-10-18 08:39:44 +01:00
else if ( m_exprComponents . op == "matches" )
return m_exprComponents . lhs + " " + m_exprComponents . rhs ;
else if ( m_exprComponents . op != "!" ) {
2013-03-25 09:24:13 +00:00
if ( m_exprComponents . lhs . size () + m_exprComponents . rhs . size () < 40 &&
m_exprComponents . lhs . find ( " \n " ) == std :: string :: npos &&
m_exprComponents . rhs . find ( " \n " ) == std :: string :: npos )
2012-10-18 08:39:44 +01:00
return m_exprComponents . lhs + " " + m_exprComponents . op + " " + m_exprComponents . rhs ;
2012-10-05 18:35:01 +01:00
else
2013-03-04 12:19:15 +01:00
return m_exprComponents . lhs + " \n " + m_exprComponents . op + " \n " + m_exprComponents . rhs ;
2012-10-05 18:35:01 +01:00
}
else
2012-10-24 21:59:47 +01:00
return "{can't expand - use " + info . macroName + "_FALSE( " + info . capturedExpression . substr ( 1 ) + " ) instead of " + info . macroName + "( " + info . capturedExpression + " ) for better diagnostics}" ;
2012-05-08 19:29:51 +01:00
}
2012-10-05 18:35:01 +01:00
2012-05-08 19:29:51 +01:00
} // end namespace Catch
2012-10-26 08:45:23 +01:00
#endif // TWOBLUECUBES_CATCH_EXPRESSIONRESULT_BUILDER_HPP_INCLUDED