AssertionResultBuilder can be constructed from result type

This commit is contained in:
Phil Nash 2012-10-19 08:01:34 +01:00
parent f2d5f1b3e4
commit 1dd56d4d2b
3 changed files with 25 additions and 27 deletions

View File

@ -21,7 +21,7 @@ struct STATIC_ASSERT_Expression_Too_Complex_Please_Rewrite_As_Binary_Comparison;
class AssertionResultBuilder { class AssertionResultBuilder {
public: public:
AssertionResultBuilder(); AssertionResultBuilder( ResultWas::OfType resultType = ResultWas::Unknown );
AssertionResultBuilder( const AssertionResultBuilder& other ); AssertionResultBuilder( const AssertionResultBuilder& other );
AssertionResultBuilder& operator=(const AssertionResultBuilder& other ); AssertionResultBuilder& operator=(const AssertionResultBuilder& other );

View File

@ -10,16 +10,19 @@
#include "catch_assertionresult_builder.h" #include "catch_assertionresult_builder.h"
#include <assert.h>
namespace Catch { namespace Catch {
AssertionResultBuilder::AssertionResultBuilder() {} AssertionResultBuilder::AssertionResultBuilder( ResultWas::OfType resultType ) {
m_data.resultType = resultType;
}
AssertionResultBuilder::AssertionResultBuilder( const AssertionResultBuilder& other ) AssertionResultBuilder::AssertionResultBuilder( const AssertionResultBuilder& other )
: m_data( other.m_data ), : m_data( other.m_data ),
m_exprComponents( other.m_exprComponents ) m_exprComponents( other.m_exprComponents )
{ {
m_stream << other.m_stream.str(); m_stream << other.m_stream.str();
} }
AssertionResultBuilder& AssertionResultBuilder::operator=(const AssertionResultBuilder& other ) { AssertionResultBuilder& AssertionResultBuilder::operator=(const AssertionResultBuilder& other ) {
m_data = other.m_data; m_data = other.m_data;
m_exprComponents = other.m_exprComponents; m_exprComponents = other.m_exprComponents;
@ -27,15 +30,8 @@ namespace Catch {
m_stream << other.m_stream.str(); m_stream << other.m_stream.str();
return *this; return *this;
} }
AssertionResultBuilder& AssertionResultBuilder::setResultType( ResultWas::OfType result ) { AssertionResultBuilder& AssertionResultBuilder::setResultType( ResultWas::OfType result ) {
// Flip bool results if isFalse is set m_data.resultType = result;
if( m_exprComponents.isFalse && result == ResultWas::Ok )
m_data.resultType = ResultWas::ExpressionFailed;
else if( m_exprComponents.isFalse && result == ResultWas::ExpressionFailed )
m_data.resultType = ResultWas::Ok;
else
m_data.resultType = result;
return *this; return *this;
} }
AssertionResultBuilder& AssertionResultBuilder::setCapturedExpression( const std::string& capturedExpression ) { AssertionResultBuilder& AssertionResultBuilder::setCapturedExpression( const std::string& capturedExpression ) {
@ -46,35 +42,38 @@ namespace Catch {
m_exprComponents.isFalse = isFalse; m_exprComponents.isFalse = isFalse;
return *this; return *this;
} }
AssertionResultBuilder& AssertionResultBuilder::setLineInfo( const SourceLineInfo& lineInfo ) { AssertionResultBuilder& AssertionResultBuilder::setLineInfo( const SourceLineInfo& lineInfo ) {
m_data.lineInfo = lineInfo; m_data.lineInfo = lineInfo;
return *this; return *this;
} }
AssertionResultBuilder& AssertionResultBuilder::setMacroName( const std::string& macroName ) { AssertionResultBuilder& AssertionResultBuilder::setMacroName( const std::string& macroName ) {
m_data.macroName = macroName; m_data.macroName = macroName;
return *this; return *this;
} }
AssertionResultBuilder& AssertionResultBuilder::setLhs( const std::string& lhs ) { AssertionResultBuilder& AssertionResultBuilder::setLhs( const std::string& lhs ) {
m_exprComponents.lhs = lhs; m_exprComponents.lhs = lhs;
return *this; return *this;
} }
AssertionResultBuilder& AssertionResultBuilder::setRhs( const std::string& rhs ) { AssertionResultBuilder& AssertionResultBuilder::setRhs( const std::string& rhs ) {
m_exprComponents.rhs = rhs; m_exprComponents.rhs = rhs;
return *this; return *this;
} }
AssertionResultBuilder& AssertionResultBuilder::setOp( const std::string& op ) { AssertionResultBuilder& AssertionResultBuilder::setOp( const std::string& op ) {
m_exprComponents.op = op; m_exprComponents.op = op;
return *this; return *this;
} }
AssertionResult AssertionResultBuilder::build() const AssertionResult AssertionResultBuilder::build() const
{ {
assert( m_data.resultType != ResultWas::Unknown );
AssertionResultData data = m_data; AssertionResultData data = m_data;
// Flip bool results if isFalse is set
if( m_exprComponents.isFalse && data.resultType == ResultWas::Ok )
data.resultType = ResultWas::ExpressionFailed;
else if( m_exprComponents.isFalse && data.resultType == ResultWas::ExpressionFailed )
data.resultType = ResultWas::Ok;
data.message = m_stream.str(); data.message = m_stream.str();
data.reconstructedExpression = reconstructExpression(); data.reconstructedExpression = reconstructExpression();
if( m_exprComponents.isFalse ) { if( m_exprComponents.isFalse ) {
@ -89,7 +88,6 @@ namespace Catch {
} }
return AssertionResult( data ); return AssertionResult( data );
} }
std::string AssertionResultBuilder::reconstructExpression() const { std::string AssertionResultBuilder::reconstructExpression() const {
if( m_exprComponents.op == "" ) if( m_exprComponents.op == "" )
return m_exprComponents.lhs.empty() ? m_data.capturedExpression : m_exprComponents.op + m_exprComponents.lhs; return m_exprComponents.lhs.empty() ? m_data.capturedExpression : m_exprComponents.op + m_exprComponents.lhs;

View File

@ -101,11 +101,11 @@ inline bool isTrue( bool value ){ return value; }
#define INTERNAL_CATCH_TEST( expr, isFalse, stopOnFailure, macroName ) \ #define INTERNAL_CATCH_TEST( expr, isFalse, stopOnFailure, macroName ) \
do { try { \ do { try { \
Catch::getResultCapture().acceptAssertionInfo( Catch::AssertionInfo( macroName, CATCH_INTERNAL_LINEINFO, #expr ) ); \ Catch::getResultCapture().acceptAssertionInfo( Catch::AssertionInfo( macroName, CATCH_INTERNAL_LINEINFO, #expr ) ); \
INTERNAL_CATCH_ACCEPT_EXPR( ( Catch::ExpressionBuilder( isFalse )->*expr ), stopOnFailure, expr ); \ INTERNAL_CATCH_ACCEPT_EXPR( ( Catch::ExpressionBuilder()->*expr ).setIsFalse( isFalse ), stopOnFailure, expr ); \
} catch( Catch::TestFailureException& ) { \ } catch( Catch::TestFailureException& ) { \
throw; \ throw; \
} catch( ... ) { \ } catch( ... ) { \
INTERNAL_CATCH_ACCEPT_EXPR( Catch::AssertionResultBuilder().setResultType( Catch::ResultWas::ThrewException ) << Catch::translateActiveException(), false, expr ); \ INTERNAL_CATCH_ACCEPT_EXPR( Catch::AssertionResultBuilder( Catch::ResultWas::ThrewException ) << Catch::translateActiveException(), false, expr ); \
throw; \ throw; \
} } while( Catch::isTrue( false ) ) } } while( Catch::isTrue( false ) )
@ -124,10 +124,10 @@ inline bool isTrue( bool value ){ return value; }
try { \ try { \
Catch::getResultCapture().acceptAssertionInfo( Catch::AssertionInfo( macroName, CATCH_INTERNAL_LINEINFO, #expr ) ); \ Catch::getResultCapture().acceptAssertionInfo( Catch::AssertionInfo( macroName, CATCH_INTERNAL_LINEINFO, #expr ) ); \
expr; \ expr; \
INTERNAL_CATCH_ACCEPT_EXPR( Catch::AssertionResultBuilder().setResultType( Catch::ResultWas::Ok ), stopOnFailure, false ); \ INTERNAL_CATCH_ACCEPT_EXPR( Catch::AssertionResultBuilder( Catch::ResultWas::Ok ), stopOnFailure, false ); \
} \ } \
catch( ... ) { \ catch( ... ) { \
INTERNAL_CATCH_ACCEPT_EXPR( Catch::AssertionResultBuilder().setResultType( Catch::ResultWas::ThrewException ) << Catch::translateActiveException(), stopOnFailure, false ); \ INTERNAL_CATCH_ACCEPT_EXPR( Catch::AssertionResultBuilder( Catch::ResultWas::ThrewException ) << Catch::translateActiveException(), stopOnFailure, false ); \
} }
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
@ -136,26 +136,26 @@ inline bool isTrue( bool value ){ return value; }
Catch::getResultCapture().acceptAssertionInfo( Catch::AssertionInfo( macroName, CATCH_INTERNAL_LINEINFO, #expr ) ); \ Catch::getResultCapture().acceptAssertionInfo( Catch::AssertionInfo( macroName, CATCH_INTERNAL_LINEINFO, #expr ) ); \
if( Catch::getCurrentContext().getConfig()->allowThrows() ) { \ if( Catch::getCurrentContext().getConfig()->allowThrows() ) { \
expr; \ expr; \
INTERNAL_CATCH_ACCEPT_EXPR( Catch::AssertionResultBuilder().setResultType( Catch::ResultWas::DidntThrowException ), stopOnFailure, false ); \ INTERNAL_CATCH_ACCEPT_EXPR( Catch::AssertionResultBuilder( Catch::ResultWas::DidntThrowException ), stopOnFailure, false ); \
} \ } \
} \ } \
catch( Catch::TestFailureException& ) { \ catch( Catch::TestFailureException& ) { \
throw; \ throw; \
} \ } \
catch( exceptionType ) { \ catch( exceptionType ) { \
INTERNAL_CATCH_ACCEPT_EXPR( Catch::AssertionResultBuilder().setResultType( Catch::ResultWas::Ok ), stopOnFailure, false ); \ INTERNAL_CATCH_ACCEPT_EXPR( Catch::AssertionResultBuilder( Catch::ResultWas::Ok ), stopOnFailure, false ); \
} }
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
#define INTERNAL_CATCH_THROWS_AS( expr, exceptionType, stopOnFailure, macroName ) \ #define INTERNAL_CATCH_THROWS_AS( expr, exceptionType, stopOnFailure, macroName ) \
INTERNAL_CATCH_THROWS( expr, exceptionType, stopOnFailure, macroName ) \ INTERNAL_CATCH_THROWS( expr, exceptionType, stopOnFailure, macroName ) \
catch( ... ) { \ catch( ... ) { \
INTERNAL_CATCH_ACCEPT_EXPR( ( Catch::AssertionResultBuilder().setResultType( Catch::ResultWas::ThrewException ) << Catch::translateActiveException() ), stopOnFailure, false ); \ INTERNAL_CATCH_ACCEPT_EXPR( ( Catch::AssertionResultBuilder( Catch::ResultWas::ThrewException ) << Catch::translateActiveException() ), stopOnFailure, false ); \
} }
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
#define INTERNAL_CATCH_MSG( reason, resultType, stopOnFailure, macroName ) \ #define INTERNAL_CATCH_MSG( reason, resultType, stopOnFailure, macroName ) \
INTERNAL_CATCH_ACCEPT_EXPR( Catch::AssertionResultBuilder().setResultType( resultType ) << reason, stopOnFailure, true ); INTERNAL_CATCH_ACCEPT_EXPR( Catch::AssertionResultBuilder( resultType ) << reason, stopOnFailure, true );
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
#define INTERNAL_CATCH_SCOPED_INFO( log ) \ #define INTERNAL_CATCH_SCOPED_INFO( log ) \
@ -170,7 +170,7 @@ inline bool isTrue( bool value ){ return value; }
} catch( Catch::TestFailureException& ) { \ } catch( Catch::TestFailureException& ) { \
throw; \ throw; \
} catch( ... ) { \ } catch( ... ) { \
INTERNAL_CATCH_ACCEPT_EXPR( ( Catch::AssertionResultBuilder().setResultType( Catch::ResultWas::ThrewException ) << Catch::translateActiveException() ), false, false ); \ INTERNAL_CATCH_ACCEPT_EXPR( ( Catch::AssertionResultBuilder( Catch::ResultWas::ThrewException ) << Catch::translateActiveException() ), false, false ); \
throw; \ throw; \
}}while( Catch::isTrue( false ) ) }}while( Catch::isTrue( false ) )