Big assertion capture refactoring.

- moved as much logic out of the macros as possible
- moved most logic into new ResultBuilder class, which wraps ExpressionResultBuilder (may take it over next), subsumes ResultAction and also takes place of ExpressionDecomposer.

This introduces many SRP violations - but all in the name of minimising macro logic!
This commit is contained in:
Phil Nash
2014-05-28 18:53:01 +01:00
parent 14796814b8
commit 9438a03d5b
26 changed files with 312 additions and 260 deletions

View File

@@ -63,7 +63,7 @@
#define CATCH_REQUIRE( expr ) INTERNAL_CATCH_TEST( expr, Catch::ResultDisposition::Normal, "CATCH_REQUIRE" )
#define CATCH_REQUIRE_FALSE( expr ) INTERNAL_CATCH_TEST( expr, Catch::ResultDisposition::Normal | Catch::ResultDisposition::NegateResult, "CATCH_REQUIRE_FALSE" )
#define CATCH_REQUIRE_THROWS( expr ) INTERNAL_CATCH_THROWS( expr, ..., Catch::ResultDisposition::Normal, "CATCH_REQUIRE_THROWS" )
#define CATCH_REQUIRE_THROWS( expr ) INTERNAL_CATCH_THROWS( expr, Catch::ResultDisposition::Normal, "CATCH_REQUIRE_THROWS" )
#define CATCH_REQUIRE_THROWS_AS( expr, exceptionType ) INTERNAL_CATCH_THROWS_AS( expr, exceptionType, Catch::ResultDisposition::Normal, "CATCH_REQUIRE_THROWS_AS" )
#define CATCH_REQUIRE_NOTHROW( expr ) INTERNAL_CATCH_NO_THROW( expr, Catch::ResultDisposition::Normal, "CATCH_REQUIRE_NOTHROW" )
@@ -73,7 +73,7 @@
#define CATCH_CHECKED_ELSE( expr ) INTERNAL_CATCH_ELSE( expr, Catch::ResultDisposition::ContinueOnFailure, "CATCH_CHECKED_ELSE" )
#define CATCH_CHECK_NOFAIL( expr ) INTERNAL_CATCH_TEST( expr, Catch::ResultDisposition::ContinueOnFailure | Catch::ResultDisposition::SuppressFail, "CATCH_CHECK_NOFAIL" )
#define CATCH_CHECK_THROWS( expr ) INTERNAL_CATCH_THROWS( expr, ..., Catch::ResultDisposition::ContinueOnFailure, "CATCH_CHECK_THROWS" )
#define CATCH_CHECK_THROWS( expr ) INTERNAL_CATCH_THROWS( expr, Catch::ResultDisposition::ContinueOnFailure, "CATCH_CHECK_THROWS" )
#define CATCH_CHECK_THROWS_AS( expr, exceptionType ) INTERNAL_CATCH_THROWS_AS( expr, exceptionType, Catch::ResultDisposition::ContinueOnFailure, "CATCH_CHECK_THROWS_AS" )
#define CATCH_CHECK_NOTHROW( expr ) INTERNAL_CATCH_NO_THROW( expr, Catch::ResultDisposition::ContinueOnFailure, "CATCH_CHECK_NOTHROW" )
@@ -126,7 +126,7 @@
#define REQUIRE( expr ) INTERNAL_CATCH_TEST( expr, Catch::ResultDisposition::Normal, "REQUIRE" )
#define REQUIRE_FALSE( expr ) INTERNAL_CATCH_TEST( expr, Catch::ResultDisposition::Normal | Catch::ResultDisposition::NegateResult, "REQUIRE_FALSE" )
#define REQUIRE_THROWS( expr ) INTERNAL_CATCH_THROWS( expr, ..., Catch::ResultDisposition::Normal, "REQUIRE_THROWS" )
#define REQUIRE_THROWS( expr ) INTERNAL_CATCH_THROWS( expr, Catch::ResultDisposition::Normal, "REQUIRE_THROWS" )
#define REQUIRE_THROWS_AS( expr, exceptionType ) INTERNAL_CATCH_THROWS_AS( expr, exceptionType, Catch::ResultDisposition::Normal, "REQUIRE_THROWS_AS" )
#define REQUIRE_NOTHROW( expr ) INTERNAL_CATCH_NO_THROW( expr, Catch::ResultDisposition::Normal, "REQUIRE_NOTHROW" )
@@ -136,7 +136,7 @@
#define CHECKED_ELSE( expr ) INTERNAL_CATCH_ELSE( expr, Catch::ResultDisposition::ContinueOnFailure, "CHECKED_ELSE" )
#define CHECK_NOFAIL( expr ) INTERNAL_CATCH_TEST( expr, Catch::ResultDisposition::ContinueOnFailure | Catch::ResultDisposition::SuppressFail, "CHECK_NOFAIL" )
#define CHECK_THROWS( expr ) INTERNAL_CATCH_THROWS( expr, ..., Catch::ResultDisposition::ContinueOnFailure, "CHECK_THROWS" )
#define CHECK_THROWS( expr ) INTERNAL_CATCH_THROWS( expr, Catch::ResultDisposition::ContinueOnFailure, "CHECK_THROWS" )
#define CHECK_THROWS_AS( expr, exceptionType ) INTERNAL_CATCH_THROWS_AS( expr, exceptionType, Catch::ResultDisposition::ContinueOnFailure, "CHECK_THROWS_AS" )
#define CHECK_NOTHROW( expr ) INTERNAL_CATCH_NO_THROW( expr, Catch::ResultDisposition::ContinueOnFailure, "CHECK_NOTHROW" )

View File

@@ -8,85 +8,38 @@
#ifndef TWOBLUECUBES_CATCH_CAPTURE_HPP_INCLUDED
#define TWOBLUECUBES_CATCH_CAPTURE_HPP_INCLUDED
#include "catch_expression_decomposer.hpp"
#include "catch_expressionresult_builder.h"
#include "catch_result_builder.h"
#include "catch_message.h"
#include "catch_interfaces_capture.h"
#include "catch_debugger.h"
#include "catch_context.h"
#include "catch_common.h"
#include "catch_tostring.h"
#include "catch_interfaces_registry_hub.h"
#include "catch_interfaces_config.h"
#include "catch_interfaces_runner.h"
#include "internal/catch_compiler_capabilities.h"
#include <ostream>
namespace Catch {
inline IResultCapture& getResultCapture() {
if( IResultCapture* capture = getCurrentContext().getResultCapture() )
return *capture;
else
throw std::logic_error( "No result capture instance" );
}
template<typename MatcherT>
ExpressionResultBuilder expressionResultBuilderFromMatcher( MatcherT const& matcher,
std::string const& matcherCallAsString ) {
std::string matcherAsString = matcher.toString();
if( matcherAsString == "{?}" )
matcherAsString = matcherCallAsString;
return ExpressionResultBuilder()
.setRhs( matcherAsString )
.setOp( "matches" );
}
template<typename MatcherT, typename ArgT>
ExpressionResultBuilder expressionResultBuilderFromMatcher( MatcherT const& matcher,
ArgT const& arg,
std::string const& matcherCallAsString ) {
return expressionResultBuilderFromMatcher( matcher, matcherCallAsString )
.setLhs( Catch::toString( arg ) )
.setResultType( matcher.match( arg ) );
}
template<typename MatcherT, typename ArgT>
ExpressionResultBuilder expressionResultBuilderFromMatcher( MatcherT const& matcher,
ArgT* arg,
std::string const& matcherCallAsString ) {
return expressionResultBuilderFromMatcher( matcher, matcherCallAsString )
.setLhs( Catch::toString( arg ) )
.setResultType( matcher.match( arg ) );
}
struct TestFailureException{};
} // end namespace Catch
///////////////////////////////////////////////////////////////////////////////
#define INTERNAL_CATCH_ACCEPT_EXPR( evaluatedExpr, resultDisposition, originalExpr ) \
if( Catch::ResultAction::Value internal_catch_action = Catch::getResultCapture().acceptExpression( evaluatedExpr, __assertionInfo ) ) { \
if( internal_catch_action & Catch::ResultAction::Debug ) CATCH_BREAK_INTO_DEBUGGER(); \
if( internal_catch_action & Catch::ResultAction::Abort ) throw Catch::TestFailureException(); \
if( !Catch::shouldContinueOnFailure( resultDisposition ) ) throw Catch::TestFailureException(); \
Catch::isTrue( false && originalExpr ); \
}
// In the event of a failure works out if the debugger needs to be invoked
// and/or an exception thrown and takes appropriate action.
// This needs to be done as a macro so the debugger will stop in the user
// source code rather than in Catch library code
#define INTERNAL_CATCH_REACT( resultBuilder ) \
if( resultBuilder.shouldDebugBreak() ) CATCH_BREAK_INTO_DEBUGGER(); \
resultBuilder.react();
///////////////////////////////////////////////////////////////////////////////
#define INTERNAL_CATCH_TEST( expr, resultDisposition, macroName ) \
do { \
Catch::AssertionInfo __assertionInfo( macroName, CATCH_INTERNAL_LINEINFO, #expr, resultDisposition ); \
Catch::ResultBuilder __catchResult( macroName, CATCH_INTERNAL_LINEINFO, #expr, resultDisposition ); \
try { \
INTERNAL_CATCH_ACCEPT_EXPR( ( Catch::ExpressionDecomposer()->*expr ).endExpression( resultDisposition ), resultDisposition, expr ); \
} catch( Catch::TestFailureException& ) { \
throw; \
} catch( ... ) { \
INTERNAL_CATCH_ACCEPT_EXPR( Catch::ExpressionResultBuilder( Catch::ResultWas::ThrewException ) << Catch::translateActiveException(), \
Catch::ResultDisposition::Normal, expr ); \
( __catchResult->*expr ).endExpression(); \
} \
} while( Catch::isTrue( false ) )
catch( ... ) { \
__catchResult.useActiveException( Catch::ResultDisposition::Normal ); \
} \
INTERNAL_CATCH_REACT( __catchResult ) \
} while( Catch::alwaysFalse() && (expr) ) // expr here is never evaluated at runtime but it forces the compiler to give it a look
///////////////////////////////////////////////////////////////////////////////
#define INTERNAL_CATCH_IF( expr, resultDisposition, macroName ) \
@@ -101,82 +54,90 @@ struct TestFailureException{};
///////////////////////////////////////////////////////////////////////////////
#define INTERNAL_CATCH_NO_THROW( expr, resultDisposition, macroName ) \
do { \
Catch::AssertionInfo __assertionInfo( macroName, CATCH_INTERNAL_LINEINFO, #expr, resultDisposition ); \
Catch::ResultBuilder __catchResult( macroName, CATCH_INTERNAL_LINEINFO, #expr, resultDisposition ); \
try { \
expr; \
INTERNAL_CATCH_ACCEPT_EXPR( Catch::ExpressionResultBuilder( Catch::ResultWas::Ok ), resultDisposition, false ); \
__catchResult.captureResult( Catch::ResultWas::Ok ); \
} \
catch( ... ) { \
INTERNAL_CATCH_ACCEPT_EXPR( Catch::ExpressionResultBuilder( Catch::ResultWas::ThrewException ) << Catch::translateActiveException(), resultDisposition, false ); \
__catchResult.useActiveException( resultDisposition ); \
} \
} while( Catch::isTrue( false ) )
INTERNAL_CATCH_REACT( __catchResult ) \
} while( Catch::alwaysFalse() )
///////////////////////////////////////////////////////////////////////////////
#define INTERNAL_CATCH_THROWS_IMPL( expr, exceptionType, resultDisposition ) \
try { \
if( Catch::getCurrentContext().getConfig()->allowThrows() ) { \
expr; \
INTERNAL_CATCH_ACCEPT_EXPR( Catch::ExpressionResultBuilder( Catch::ResultWas::DidntThrowException ), resultDisposition, false ); \
} \
} \
catch( Catch::TestFailureException& ) { \
throw; \
} \
catch( exceptionType ) { \
INTERNAL_CATCH_ACCEPT_EXPR( Catch::ExpressionResultBuilder( Catch::ResultWas::Ok ), resultDisposition, false ); \
}
///////////////////////////////////////////////////////////////////////////////
#define INTERNAL_CATCH_THROWS( expr, exceptionType, resultDisposition, macroName ) \
#define INTERNAL_CATCH_THROWS( expr, resultDisposition, macroName ) \
do { \
Catch::AssertionInfo __assertionInfo( macroName, CATCH_INTERNAL_LINEINFO, #expr, resultDisposition ); \
INTERNAL_CATCH_THROWS_IMPL( expr, exceptionType, resultDisposition ) \
} while( Catch::isTrue( false ) )
Catch::ResultBuilder __catchResult( macroName, CATCH_INTERNAL_LINEINFO, #expr, resultDisposition ); \
try { \
if( __catchResult.allowThrows() ) \
expr; \
__catchResult.captureResult( Catch::ResultWas::DidntThrowException ); \
} \
catch( ... ) { \
__catchResult.captureResult( Catch::ResultWas::Ok ); \
} \
INTERNAL_CATCH_REACT( __catchResult ) \
} while( Catch::alwaysFalse() )
///////////////////////////////////////////////////////////////////////////////
#define INTERNAL_CATCH_THROWS_AS( expr, exceptionType, resultDisposition, macroName ) \
do { \
Catch::AssertionInfo __assertionInfo( macroName, CATCH_INTERNAL_LINEINFO, #expr, resultDisposition ); \
INTERNAL_CATCH_THROWS_IMPL( expr, exceptionType, resultDisposition ) \
catch( ... ) { \
INTERNAL_CATCH_ACCEPT_EXPR( ( Catch::ExpressionResultBuilder( Catch::ResultWas::ThrewException ) << Catch::translateActiveException() ), \
resultDisposition | Catch::ResultDisposition::ContinueOnFailure, false ); \
Catch::ResultBuilder __catchResult( macroName, CATCH_INTERNAL_LINEINFO, #expr, resultDisposition ); \
try { \
if( __catchResult.allowThrows() ) \
expr; \
__catchResult.captureResult( Catch::ResultWas::DidntThrowException ); \
} \
} while( Catch::isTrue( false ) )
catch( exceptionType ) { \
__catchResult.captureResult( Catch::ResultWas::Ok ); \
} \
catch( ... ) { \
__catchResult.useActiveException( resultDisposition ); \
} \
INTERNAL_CATCH_REACT( __catchResult ) \
} while( Catch::alwaysFalse() )
///////////////////////////////////////////////////////////////////////////////
#ifdef CATCH_CONFIG_VARIADIC_MACROS
#define INTERNAL_CATCH_MSG( messageType, resultDisposition, macroName, ... ) \
do { \
Catch::AssertionInfo __assertionInfo( macroName, CATCH_INTERNAL_LINEINFO, "", resultDisposition ); \
INTERNAL_CATCH_ACCEPT_EXPR( Catch::ExpressionResultBuilder( messageType ) << __VA_ARGS__ +::Catch::StreamEndStop(), resultDisposition, true ) \
} while( Catch::isTrue( false ) )
Catch::ResultBuilder __catchResult( macroName, CATCH_INTERNAL_LINEINFO, "", resultDisposition ); \
__catchResult.m_resultBuilder << __VA_ARGS__ + ::Catch::StreamEndStop(); \
__catchResult.captureResult( messageType ); \
INTERNAL_CATCH_REACT( __catchResult ) \
} while( Catch::alwaysFalse() )
#else
#define INTERNAL_CATCH_MSG( messageType, resultDisposition, macroName, log ) \
do { \
Catch::AssertionInfo __assertionInfo( macroName, CATCH_INTERNAL_LINEINFO, "", resultDisposition ); \
INTERNAL_CATCH_ACCEPT_EXPR( Catch::ExpressionResultBuilder( messageType ) << log, resultDisposition, true ) \
} while( Catch::isTrue( false ) )
Catch::ResultBuilder __catchResult( macroName, CATCH_INTERNAL_LINEINFO, "", resultDisposition ); \
__catchResult.m_resultBuilder << log + ::Catch::StreamEndStop(); \
__catchResult.captureResult( messageType ); \
INTERNAL_CATCH_REACT( __catchResult ) \
} while( Catch::alwaysFalse() )
#endif
///////////////////////////////////////////////////////////////////////////////
#define INTERNAL_CATCH_INFO( log, macroName ) \
Catch::ScopedMessage INTERNAL_CATCH_UNIQUE_NAME( scopedMessage ) = Catch::MessageBuilder( macroName, CATCH_INTERNAL_LINEINFO, Catch::ResultWas::Info ) << log;
///////////////////////////////////////////////////////////////////////////////
#define INTERNAL_CHECK_THAT( arg, matcher, resultDisposition, macroName ) \
do { \
Catch::AssertionInfo __assertionInfo( macroName, CATCH_INTERNAL_LINEINFO, #arg " " #matcher, resultDisposition ); \
Catch::ResultBuilder __catchResult( macroName, CATCH_INTERNAL_LINEINFO, #arg " " #matcher, resultDisposition ); \
try { \
INTERNAL_CATCH_ACCEPT_EXPR( ( Catch::expressionResultBuilderFromMatcher( ::Catch::Matchers::matcher, arg, #matcher ) ), resultDisposition, false ); \
} catch( Catch::TestFailureException& ) { \
throw; \
std::string matcherAsString = ::Catch::Matchers::matcher.toString(); \
__catchResult.m_resultBuilder \
.setLhs( Catch::toString( arg ) ) \
.setRhs( matcherAsString == "{?}" ? #matcher : matcherAsString ) \
.setOp( "matches" ) \
.setResultType( ::Catch::Matchers::matcher.match( arg ) ); \
__catchResult.captureExpression(); \
} catch( ... ) { \
INTERNAL_CATCH_ACCEPT_EXPR( ( Catch::ExpressionResultBuilder( Catch::ResultWas::ThrewException ) << Catch::translateActiveException() ), \
resultDisposition | Catch::ResultDisposition::ContinueOnFailure, false ); \
__catchResult.useActiveException( resultDisposition | Catch::ResultDisposition::ContinueOnFailure ); \
} \
} while( Catch::isTrue( false ) )
INTERNAL_CATCH_REACT( __catchResult ) \
} while( Catch::alwaysFalse() )
#endif // TWOBLUECUBES_CATCH_CAPTURE_HPP_INCLUDED

View File

@@ -94,6 +94,8 @@ namespace Catch {
// This is just here to avoid compiler warnings with macro constants and boolean literals
inline bool isTrue( bool value ){ return value; }
inline bool alwaysTrue() { return true; }
inline bool alwaysFalse() { return false; }
void throwLogicError( std::string const& message, SourceLineInfo const& locationInfo );

View File

@@ -77,7 +77,7 @@ namespace Catch {
void throwLogicError( std::string const& message, SourceLineInfo const& locationInfo ) {
std::ostringstream oss;
oss << locationInfo << ": Internal Catch error: '" << message << "'";
if( isTrue( true ))
if( alwaysTrue() )
throw std::logic_error( oss.str() );
}
}

View File

@@ -43,7 +43,7 @@ namespace Catch{
#endif
#ifndef CATCH_BREAK_INTO_DEBUGGER
#define CATCH_BREAK_INTO_DEBUGGER() Catch::isTrue( true );
#define CATCH_BREAK_INTO_DEBUGGER() Catch::alwaysTrue();
#endif
#endif // TWOBLUECUBES_CATCH_DEBUGGER_H_INCLUDED

View File

@@ -41,6 +41,9 @@ namespace Catch {
throw;
#endif
}
catch( TestFailureException& ) {
throw;
}
catch( std::exception& ex ) {
return ex.what();
}

View File

@@ -1,31 +0,0 @@
/*
* 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_DECOMPOSER_HPP_INCLUDED
#define TWOBLUECUBES_CATCH_EXPRESSION_DECOMPOSER_HPP_INCLUDED
#include "catch_expression_lhs.hpp"
namespace Catch {
// Captures the LHS of the expression and wraps it in an Expression Lhs object
class ExpressionDecomposer {
public:
template<typename T>
ExpressionLhs<T const&> operator->* ( T const& operand ) {
return ExpressionLhs<T const&>( operand );
}
ExpressionLhs<bool> operator->* ( bool value ) {
return ExpressionLhs<bool>( value );
}
};
} // end namespace Catch
#endif // TWOBLUECUBES_CATCH_EXPRESSION_DECOMPOSER_HPP_INCLUDED

View File

@@ -8,12 +8,19 @@
#ifndef TWOBLUECUBES_CATCH_EXPRESSION_LHS_HPP_INCLUDED
#define TWOBLUECUBES_CATCH_EXPRESSION_LHS_HPP_INCLUDED
#include "catch_result_builder.h"
#include "catch_expressionresult_builder.h"
#include "catch_evaluate.hpp"
#include "catch_tostring.h"
#include <assert.h>
namespace Catch {
struct ResultBuilder;
ExpressionResultBuilder& getResultBuilder( ResultBuilder* rb );
// Wraps the LHS of an expression and captures the operator and RHS (if any) -
// wrapping them all in an ExpressionResultBuilder object
template<typename T>
@@ -24,7 +31,7 @@ class ExpressionLhs {
# endif
public:
ExpressionLhs( T lhs ) : m_lhs( lhs ) {}
ExpressionLhs( ResultBuilder& rb, T lhs ) : m_rb( &rb ), m_lhs( lhs ) {}
# ifdef CATCH_CPP11_OR_GREATER
ExpressionLhs( ExpressionLhs const& ) = default;
ExpressionLhs( ExpressionLhs && ) = default;
@@ -68,12 +75,13 @@ public:
return captureExpression<Internal::IsNotEqualTo>( rhs );
}
ExpressionResultBuilder& endExpression( ResultDisposition::Flags resultDisposition ) {
void endExpression() {
assert( m_rb );
bool value = m_lhs ? true : false;
return m_result
getResultBuilder( m_rb )
.setLhs( Catch::toString( value ) )
.setResultType( value )
.endExpression( resultDisposition );
.endExpression();
}
// Only simple binary expressions are allowed on the LHS.
@@ -88,7 +96,8 @@ public:
private:
template<Internal::Operator Op, typename RhsT>
ExpressionResultBuilder& captureExpression( RhsT const& rhs ) {
return m_result
assert( m_rb );
return getResultBuilder( m_rb )
.setResultType( Internal::compare<Op>( m_lhs, rhs ) )
.setLhs( Catch::toString( m_lhs ) )
.setRhs( Catch::toString( rhs ) )
@@ -96,7 +105,7 @@ private:
}
private:
ExpressionResultBuilder m_result;
ResultBuilder* m_rb;
T m_lhs;
};

View File

@@ -17,15 +17,16 @@
namespace Catch {
struct STATIC_ASSERT_Expression_Too_Complex_Please_Rewrite_As_Binary_Comparison;
struct ResultBuilder;
// Wraps the (stringised versions of) the lhs, operator and rhs of an expression - as well as
// the result of evaluating it. This is used to build an AssertionResult object
class ExpressionResultBuilder {
public:
ExpressionResultBuilder( ResultWas::OfType resultType = ResultWas::Unknown );
ExpressionResultBuilder( ResultBuilder* rb, ResultWas::OfType resultType = ResultWas::Unknown );
ExpressionResultBuilder( ExpressionResultBuilder const& other );
ExpressionResultBuilder& operator=(ExpressionResultBuilder const& other );
ExpressionResultBuilder& operator=( ExpressionResultBuilder const& other );
ExpressionResultBuilder& setResultType( ResultWas::OfType result );
ExpressionResultBuilder& setResultType( bool result );
@@ -33,7 +34,7 @@ public:
ExpressionResultBuilder& setRhs( std::string const& rhs );
ExpressionResultBuilder& setOp( std::string const& op );
ExpressionResultBuilder& endExpression( ResultDisposition::Flags resultDisposition );
void endExpression();
template<typename T>
ExpressionResultBuilder& operator << ( T const& value ) {
@@ -49,6 +50,7 @@ public:
template<typename RhsT> STATIC_ASSERT_Expression_Too_Complex_Please_Rewrite_As_Binary_Comparison& operator || ( RhsT const& );
private:
ResultBuilder* m_rb;
AssertionResultData m_data;
struct ExprComponents {
ExprComponents() : shouldNegate( false ) {}

View File

@@ -9,21 +9,25 @@
#define TWOBLUECUBES_CATCH_EXPRESSIONRESULT_BUILDER_HPP_INCLUDED
#include "catch_expressionresult_builder.h"
#include "catch_result_builder.h"
#include <assert.h>
namespace Catch {
ExpressionResultBuilder::ExpressionResultBuilder( ResultWas::OfType resultType ) {
ExpressionResultBuilder::ExpressionResultBuilder( ResultBuilder* rb, ResultWas::OfType resultType )
: m_rb( rb ) {
m_data.resultType = resultType;
}
ExpressionResultBuilder::ExpressionResultBuilder( ExpressionResultBuilder const& other )
: m_data( other.m_data ),
: m_rb( other.m_rb ),
m_data( other.m_data ),
m_exprComponents( other.m_exprComponents )
{
m_stream << other.m_stream.str();
}
ExpressionResultBuilder& ExpressionResultBuilder::operator=(ExpressionResultBuilder const& other ) {
m_rb = other.m_rb;
m_data = other.m_data;
m_exprComponents = other.m_exprComponents;
m_stream.str("");
@@ -38,9 +42,9 @@ namespace Catch {
m_data.resultType = result ? ResultWas::Ok : ResultWas::ExpressionFailed;
return *this;
}
ExpressionResultBuilder& ExpressionResultBuilder::endExpression( ResultDisposition::Flags resultDisposition ) {
m_exprComponents.shouldNegate = shouldNegate( resultDisposition );
return *this;
void ExpressionResultBuilder::endExpression() {
m_exprComponents.shouldNegate = shouldNegate( m_rb->m_assertionInfo.resultDisposition );
m_rb->captureExpression();
}
ExpressionResultBuilder& ExpressionResultBuilder::setLhs( std::string const& lhs ) {
m_exprComponents.lhs = lhs;
@@ -54,6 +58,7 @@ namespace Catch {
m_exprComponents.op = op;
return *this;
}
AssertionResult ExpressionResultBuilder::buildResult( AssertionInfo const& info ) const
{
assert( m_data.resultType != ResultWas::Unknown );

View File

@@ -34,6 +34,7 @@
#include "catch_section.hpp"
#include "catch_debugger.hpp"
#include "catch_tostring.hpp"
#include "catch_result_builder.hpp"
#include "../reporters/catch_reporter_xml.hpp"
#include "../reporters/catch_reporter_junit.hpp"

View File

@@ -34,13 +34,11 @@ namespace Catch {
virtual void pushScopedMessage( MessageInfo const& message ) = 0;
virtual void popScopedMessage( MessageInfo const& message ) = 0;
virtual bool shouldDebugBreak() const = 0;
virtual ResultAction::Value acceptExpression( ExpressionResultBuilder const& assertionResult, AssertionInfo const& assertionInfo ) = 0;
virtual std::string getCurrentTestName() const = 0;
virtual const AssertionResult* getLastResult() const = 0;
};
IResultCapture& getResultCapture();
}
#endif // TWOBLUECUBES_CATCH_INTERFACES_CAPTURE_H_INCLUDED

View File

@@ -13,6 +13,7 @@ namespace Catch {
struct IRunner {
virtual ~IRunner();
virtual bool aborting() const = 0;
};
}

View File

@@ -46,7 +46,7 @@ namespace Catch
it != itEnd;
++it ) {
if( it->type == ResultWas::Info ) {
ExpressionResultBuilder expressionBuilder( it->type );
ExpressionResultBuilder expressionBuilder( NULL, it->type );
expressionBuilder << it->message;
AssertionInfo info( it->macroName, it->lineInfo, "", ResultDisposition::Normal );
AssertionResult result = expressionBuilder.buildResult( info );

View File

@@ -0,0 +1,49 @@
/*
* Created by Phil on 28/5/2014.
* Copyright 2014 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_RESULT_BUILDER_H_INCLUDED
#define TWOBLUECUBES_CATCH_RESULT_BUILDER_H_INCLUDED
#include "catch_expression_lhs.hpp"
#include "catch_expressionresult_builder.h"
#include "catch_common.h"
namespace Catch {
struct TestFailureException{};
struct ResultBuilder {
ResultBuilder( char const* macroName,
SourceLineInfo const& lineInfo,
char const* capturedExpression,
ResultDisposition::Flags resultDisposition );
template<typename T>
ExpressionLhs<T const&> operator->* ( T const& operand ) {
return ExpressionLhs<T const&>( *this, operand );
}
ExpressionLhs<bool> operator->* ( bool value ) {
return ExpressionLhs<bool>( *this, value );
}
void useActiveException( ResultDisposition::Flags resultDisposition = ResultDisposition::Normal );
void captureResult( ResultWas::OfType resultType );
void captureExpression();
void react();
bool shouldDebugBreak() const;
bool allowThrows() const;
AssertionInfo m_assertionInfo;
ExpressionResultBuilder m_resultBuilder;
bool m_shouldDebugBreak;
bool m_shouldThrow;
};
} // end namespace Catch
#endif // TWOBLUECUBES_CATCH_RESULT_BUILDER_H_INCLUDED

View File

@@ -0,0 +1,69 @@
/*
* Created by Phil on 28/5/2014.
* Copyright 2014 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_RESULT_BUILDER_HPP_INCLUDED
#define TWOBLUECUBES_CATCH_RESULT_BUILDER_HPP_INCLUDED
#include "catch_result_builder.h"
#include "catch_expressionresult_builder.h"
#include "catch_context.h"
#include "catch_common.h"
#include "catch_interfaces_config.h"
#include "catch_interfaces_runner.h"
#include "catch_interfaces_capture.h"
#include "catch_interfaces_registry_hub.h"
namespace Catch {
ExpressionResultBuilder& getResultBuilder( ResultBuilder* rb ) {
return rb->m_resultBuilder;
}
ResultBuilder::ResultBuilder( char const* macroName,
SourceLineInfo const& lineInfo,
char const* capturedExpression,
ResultDisposition::Flags resultDisposition )
: m_assertionInfo( macroName, lineInfo, capturedExpression, resultDisposition ),
m_resultBuilder( this ),
m_shouldDebugBreak( false ),
m_shouldThrow( false )
{}
void ResultBuilder::useActiveException( ResultDisposition::Flags resultDisposition ) {
m_assertionInfo.resultDisposition = resultDisposition;
m_resultBuilder << Catch::translateActiveException();
captureResult( ResultWas::ThrewException );
}
void ResultBuilder::captureResult( ResultWas::OfType resultType ) {
m_resultBuilder.setResultType( resultType );
captureExpression();
}
void ResultBuilder::captureExpression() {
AssertionResult result = m_resultBuilder.buildResult( m_assertionInfo );
getResultCapture().assertionEnded( result );
if( !result.isOk() ) {
if( getCurrentContext().getConfig()->shouldDebugBreak() )
m_shouldDebugBreak = true;
if( getCurrentContext().getRunner()->aborting() || m_assertionInfo.resultDisposition == ResultDisposition::Normal )
m_shouldThrow = true;
}
}
void ResultBuilder::react() {
if( m_shouldThrow )
throw Catch::TestFailureException();
}
bool ResultBuilder::shouldDebugBreak() const { return m_shouldDebugBreak; }
bool ResultBuilder::allowThrows() const { return getCurrentContext().getConfig()->allowThrows(); }
} // end namespace Catch
#endif // TWOBLUECUBES_CATCH_RESULT_BUILDER_HPP_INCLUDED

View File

@@ -36,13 +36,6 @@ namespace Catch {
return flags == ResultWas::Info;
}
// ResultAction::Value enum
struct ResultAction { enum Value {
None,
Failed = 1, // Failure - but no debug break if Debug bit not set
Debug = 2, // If this bit is set, invoke the debugger
Abort = 4 // Test run should abort
}; };
// ResultDisposition::Flags enum
struct ResultDisposition { enum Flags {
@@ -60,7 +53,7 @@ namespace Catch {
inline bool shouldContinueOnFailure( int flags ) { return ( flags & ResultDisposition::ContinueOnFailure ) != 0; }
inline bool shouldNegate( int flags ) { return ( flags & ResultDisposition::NegateResult ) != 0; }
inline bool shouldSuppressFailure( int flags ) { return ( flags & ResultDisposition::SuppressFail ) != 0; }
} // end namespace Catch
#endif // TWOBLUECUBES_CATCH_RESULT_TYPE_H_INCLUDED

View File

@@ -19,6 +19,7 @@
#include "catch_test_spec.hpp"
#include "catch_test_case_tracker.hpp"
#include "catch_timer.h"
#include "catch_result_builder.h"
#include <set>
#include <string>
@@ -129,10 +130,6 @@ namespace Catch {
private: // IResultCapture
virtual ResultAction::Value acceptExpression( ExpressionResultBuilder const& assertionResult, AssertionInfo const& assertionInfo ) {
m_lastAssertionInfo = assertionInfo;
return actOnCurrentResult( assertionResult.buildResult( assertionInfo ) );
}
virtual void assertionEnded( AssertionResult const& result ) {
if( result.getResultType() == ResultWas::Ok ) {
@@ -147,6 +144,7 @@ namespace Catch {
// Reset working state
m_lastAssertionInfo = AssertionInfo( "", m_lastAssertionInfo.lineInfo, "{Unknown expression after the reported line}" , m_lastAssertionInfo.resultDisposition );
m_lastResult = result;
}
virtual bool sectionStarted (
@@ -201,10 +199,6 @@ namespace Catch {
m_messages.erase( std::remove( m_messages.begin(), m_messages.end(), message ), m_messages.end() );
}
virtual bool shouldDebugBreak() const {
return m_config->shouldDebugBreak();
}
virtual std::string getCurrentTestName() const {
return m_activeTestCase
? m_activeTestCase->getTestCaseInfo().name
@@ -223,22 +217,6 @@ namespace Catch {
private:
ResultAction::Value actOnCurrentResult( AssertionResult const& result ) {
m_lastResult = result;
assertionEnded( m_lastResult );
ResultAction::Value action = ResultAction::None;
if( !m_lastResult.isOk() ) {
action = ResultAction::Failed;
if( shouldDebugBreak() )
action = (ResultAction::Value)( action | ResultAction::Debug );
if( aborting() )
action = (ResultAction::Value)( action | ResultAction::Abort );
}
return action;
}
void runCurrentTest( std::string& redirectedCout, std::string& redirectedCerr ) {
TestCaseInfo const& testCaseInfo = m_activeTestCase->getTestCaseInfo();
SectionInfo testCaseSection( testCaseInfo.name, testCaseInfo.description, testCaseInfo.lineInfo );
@@ -265,9 +243,11 @@ namespace Catch {
// This just means the test was aborted due to failure
}
catch(...) {
ExpressionResultBuilder exResult( ResultWas::ThrewException );
exResult << translateActiveException();
actOnCurrentResult( exResult.buildResult( m_lastAssertionInfo ) );
ResultBuilder exResult( m_lastAssertionInfo.macroName.c_str(),
m_lastAssertionInfo.lineInfo,
m_lastAssertionInfo.capturedExpression.c_str(),
m_lastAssertionInfo.resultDisposition );
exResult.useActiveException();
}
// If sections ended prematurely due to an exception we stored their
// infos here so we can tear them down outside the unwind process.
@@ -314,6 +294,13 @@ namespace Catch {
std::vector<UnfinishedSections> m_unfinishedSections;
};
IResultCapture& getResultCapture() {
if( IResultCapture* capture = getCurrentContext().getResultCapture() )
return *capture;
else
throw std::logic_error( "No result capture instance" );
}
} // end namespace Catch
#endif // TWOBLUECUBES_CATCH_RUNNER_IMPL_HPP_INCLUDED