mirror of
				https://github.com/catchorg/Catch2.git
				synced 2025-11-04 05:59:32 +01:00 
			
		
		
		
	AssertionResultBuilder can be constructed from result type
This commit is contained in:
		@@ -21,7 +21,7 @@ struct STATIC_ASSERT_Expression_Too_Complex_Please_Rewrite_As_Binary_Comparison;
 | 
			
		||||
class AssertionResultBuilder {
 | 
			
		||||
public:
 | 
			
		||||
    
 | 
			
		||||
    AssertionResultBuilder();
 | 
			
		||||
    AssertionResultBuilder( ResultWas::OfType resultType = ResultWas::Unknown );
 | 
			
		||||
    AssertionResultBuilder( const AssertionResultBuilder& other );
 | 
			
		||||
    AssertionResultBuilder& operator=(const AssertionResultBuilder& other );
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -10,16 +10,19 @@
 | 
			
		||||
 | 
			
		||||
#include "catch_assertionresult_builder.h"
 | 
			
		||||
 | 
			
		||||
#include <assert.h>
 | 
			
		||||
 | 
			
		||||
namespace Catch {
 | 
			
		||||
 | 
			
		||||
    AssertionResultBuilder::AssertionResultBuilder() {}
 | 
			
		||||
    AssertionResultBuilder::AssertionResultBuilder( ResultWas::OfType resultType ) {
 | 
			
		||||
        m_data.resultType = resultType;
 | 
			
		||||
    }
 | 
			
		||||
    AssertionResultBuilder::AssertionResultBuilder( const AssertionResultBuilder& other )
 | 
			
		||||
    :   m_data( other.m_data ),
 | 
			
		||||
        m_exprComponents( other.m_exprComponents )
 | 
			
		||||
    {
 | 
			
		||||
        m_stream << other.m_stream.str();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    AssertionResultBuilder& AssertionResultBuilder::operator=(const AssertionResultBuilder& other ) {
 | 
			
		||||
        m_data = other.m_data;
 | 
			
		||||
        m_exprComponents = other.m_exprComponents;
 | 
			
		||||
@@ -27,15 +30,8 @@ namespace Catch {
 | 
			
		||||
        m_stream << other.m_stream.str();
 | 
			
		||||
        return *this;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    AssertionResultBuilder& AssertionResultBuilder::setResultType( ResultWas::OfType result ) {
 | 
			
		||||
        // Flip bool results if isFalse is set
 | 
			
		||||
        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;
 | 
			
		||||
        m_data.resultType = result;
 | 
			
		||||
        return *this;
 | 
			
		||||
    }
 | 
			
		||||
    AssertionResultBuilder& AssertionResultBuilder::setCapturedExpression( const std::string& capturedExpression ) {
 | 
			
		||||
@@ -46,35 +42,38 @@ namespace Catch {
 | 
			
		||||
        m_exprComponents.isFalse = isFalse;
 | 
			
		||||
        return *this;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    AssertionResultBuilder& AssertionResultBuilder::setLineInfo( const SourceLineInfo& lineInfo ) {
 | 
			
		||||
        m_data.lineInfo = lineInfo;
 | 
			
		||||
        return *this;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    AssertionResultBuilder& AssertionResultBuilder::setMacroName( const std::string& macroName ) {
 | 
			
		||||
        m_data.macroName = macroName;
 | 
			
		||||
        return *this;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    AssertionResultBuilder& AssertionResultBuilder::setLhs( const std::string& lhs ) {
 | 
			
		||||
        m_exprComponents.lhs = lhs;
 | 
			
		||||
        return *this;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    AssertionResultBuilder& AssertionResultBuilder::setRhs( const std::string& rhs ) {
 | 
			
		||||
        m_exprComponents.rhs = rhs;
 | 
			
		||||
        return *this;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    AssertionResultBuilder& AssertionResultBuilder::setOp( const std::string& op ) {
 | 
			
		||||
        m_exprComponents.op = op;
 | 
			
		||||
        return *this;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    AssertionResult AssertionResultBuilder::build() const
 | 
			
		||||
    {
 | 
			
		||||
        assert( m_data.resultType != ResultWas::Unknown );
 | 
			
		||||
 | 
			
		||||
        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.reconstructedExpression = reconstructExpression();
 | 
			
		||||
        if( m_exprComponents.isFalse ) {
 | 
			
		||||
@@ -89,7 +88,6 @@ namespace Catch {
 | 
			
		||||
        }
 | 
			
		||||
        return AssertionResult( data );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    std::string AssertionResultBuilder::reconstructExpression() const {
 | 
			
		||||
        if( m_exprComponents.op == "" )
 | 
			
		||||
            return m_exprComponents.lhs.empty() ? m_data.capturedExpression : m_exprComponents.op + m_exprComponents.lhs;
 | 
			
		||||
 
 | 
			
		||||
@@ -101,11 +101,11 @@ inline bool isTrue( bool value ){ return value; }
 | 
			
		||||
#define INTERNAL_CATCH_TEST( expr, isFalse, stopOnFailure, macroName ) \
 | 
			
		||||
    do { try { \
 | 
			
		||||
        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& ) { \
 | 
			
		||||
        throw; \
 | 
			
		||||
    } 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; \
 | 
			
		||||
    } } while( Catch::isTrue( false ) )
 | 
			
		||||
 | 
			
		||||
@@ -124,10 +124,10 @@ inline bool isTrue( bool value ){ return value; }
 | 
			
		||||
    try { \
 | 
			
		||||
        Catch::getResultCapture().acceptAssertionInfo( Catch::AssertionInfo( macroName, CATCH_INTERNAL_LINEINFO, #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( ... ) { \
 | 
			
		||||
        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  ) ); \
 | 
			
		||||
        if( Catch::getCurrentContext().getConfig()->allowThrows() ) { \
 | 
			
		||||
            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& ) { \
 | 
			
		||||
        throw; \
 | 
			
		||||
    } \
 | 
			
		||||
    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 ) \
 | 
			
		||||
    INTERNAL_CATCH_THROWS( expr, exceptionType, stopOnFailure, macroName ) \
 | 
			
		||||
    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 ) \
 | 
			
		||||
    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 ) \
 | 
			
		||||
@@ -170,7 +170,7 @@ inline bool isTrue( bool value ){ return value; }
 | 
			
		||||
    } catch( Catch::TestFailureException& ) { \
 | 
			
		||||
        throw; \
 | 
			
		||||
    } 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; \
 | 
			
		||||
    }}while( Catch::isTrue( false ) )
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user