catch2/include/internal/catch_expressionresult_builder.hpp

99 lines
4.3 KiB
C++
Raw Normal View History

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