mirror of
https://github.com/catchorg/Catch2.git
synced 2024-12-23 03:43:28 +01:00
First step to refactoring ResultsCapture into pure calls onto IResultListener interface
This commit is contained in:
parent
245be004fe
commit
6ea8f298a4
@ -190,6 +190,16 @@ private:
|
||||
|
||||
};
|
||||
|
||||
struct ResultAction
|
||||
{
|
||||
enum Value
|
||||
{
|
||||
None,
|
||||
Failed = 1, // Failure - but no debug break if Debug bit not set
|
||||
DebugFailed = 3 // Indicates that the debugger should break, if possible
|
||||
};
|
||||
};
|
||||
|
||||
class TestCaseInfo;
|
||||
class ScopedInfo;
|
||||
|
||||
@ -202,18 +212,14 @@ struct IResultListener
|
||||
virtual void pushScopedInfo( ScopedInfo* scopedInfo ) = 0;
|
||||
virtual void popScopedInfo( ScopedInfo* scopedInfo ) = 0;
|
||||
virtual bool shouldDebugBreak() const = 0;
|
||||
|
||||
virtual ResultAction::Value acceptResult( bool result ) = 0;
|
||||
virtual ResultAction::Value acceptResult( ResultWas::OfType result ) = 0;
|
||||
virtual void acceptExpression( const MutableResultInfo& resultInfo ) = 0;
|
||||
virtual void acceptMessage( const std::string& msg ) = 0;
|
||||
|
||||
};
|
||||
|
||||
struct ResultAction
|
||||
{
|
||||
enum Value
|
||||
{
|
||||
None,
|
||||
Failed = 1, // Failure - but no debug break if Debug bit not set
|
||||
DebugFailed = 3 // Indicates that the debugger should break, if possible
|
||||
};
|
||||
|
||||
};
|
||||
|
||||
class ResultsCapture
|
||||
{
|
||||
@ -237,58 +243,9 @@ public:
|
||||
instance().m_listener = listener;
|
||||
return prevListener;
|
||||
}
|
||||
|
||||
static void acceptExpression( const MutableResultInfo& resultInfo )
|
||||
static IResultListener& getListener()
|
||||
{
|
||||
instance().currentResult = resultInfo;
|
||||
}
|
||||
|
||||
static ResultAction::Value acceptResult( bool result )
|
||||
{
|
||||
return acceptResult( result ? ResultWas::Ok : ResultWas::ExpressionFailed );
|
||||
}
|
||||
|
||||
static ResultAction::Value acceptResult( ResultWas::OfType result )
|
||||
{
|
||||
MutableResultInfo& currentResult = instance().currentResult;
|
||||
currentResult.setResultType( result );
|
||||
|
||||
if( instance().m_listener )
|
||||
{
|
||||
instance().m_listener->testEnded( currentResult );
|
||||
}
|
||||
bool ok = currentResult.ok();
|
||||
instance().currentResult = MutableResultInfo();
|
||||
if( ok )
|
||||
return ResultAction::None;
|
||||
else if( instance().m_listener->shouldDebugBreak() )
|
||||
return ResultAction::DebugFailed;
|
||||
else
|
||||
return ResultAction::Failed;
|
||||
}
|
||||
|
||||
static void acceptMessage( const std::string& msg )
|
||||
{
|
||||
instance().currentResult.setMessage( msg );
|
||||
}
|
||||
|
||||
static bool acceptSectionStart( const std::string& name, const std::string& description, std::size_t& successes, std::size_t& failures )
|
||||
{
|
||||
return instance().m_listener->sectionStarted( name, description, successes, failures );
|
||||
}
|
||||
|
||||
static void acceptSectionEnd( const std::string& name, std::size_t successes, std::size_t failures )
|
||||
{
|
||||
instance().m_listener->sectionEnded( name, successes, failures );
|
||||
}
|
||||
|
||||
static void pushScopedInfo( ScopedInfo* scopedInfo )
|
||||
{
|
||||
instance().m_listener->pushScopedInfo( scopedInfo );
|
||||
}
|
||||
static void popScopedInfo( ScopedInfo* scopedInfo )
|
||||
{
|
||||
instance().m_listener->popScopedInfo( scopedInfo );
|
||||
return *instance().m_listener;
|
||||
}
|
||||
|
||||
private:
|
||||
@ -302,12 +259,12 @@ class ScopedInfo
|
||||
public:
|
||||
ScopedInfo()
|
||||
{
|
||||
ResultsCapture::pushScopedInfo( this );
|
||||
ResultsCapture::getListener().pushScopedInfo( this );
|
||||
}
|
||||
|
||||
~ScopedInfo()
|
||||
{
|
||||
ResultsCapture::popScopedInfo( this );
|
||||
ResultsCapture::getListener().popScopedInfo( this );
|
||||
}
|
||||
|
||||
ScopedInfo& operator << ( const char* str )
|
||||
@ -378,7 +335,7 @@ inline bool isTrue( bool value )
|
||||
} // end namespace Catch
|
||||
|
||||
#define INTERNAL_CATCH_ACCEPT_RESULT( result, stopOnFailure ) \
|
||||
if( Catch::ResultAction::Value action = Catch::ResultsCapture::acceptResult( result ) ) \
|
||||
if( Catch::ResultAction::Value action = Catch::ResultsCapture::getListener().acceptResult( result ) ) \
|
||||
{ \
|
||||
if( action == Catch::ResultAction::DebugFailed ) DebugBreak(); \
|
||||
if( Catch::isTrue( stopOnFailure ) ) throw Catch::TestFailureException(); \
|
||||
@ -386,12 +343,12 @@ inline bool isTrue( bool value )
|
||||
|
||||
#define INTERNAL_CATCH_TEST( expr, isNot, stopOnFailure, macroName ) \
|
||||
{ \
|
||||
Catch::ResultsCapture::acceptExpression( Catch::ResultBuilder( #expr, isNot, __FILE__, __LINE__, macroName )->*expr ); \
|
||||
Catch::ResultsCapture::getListener().acceptExpression( Catch::ResultBuilder( #expr, isNot, __FILE__, __LINE__, macroName )->*expr ); \
|
||||
INTERNAL_CATCH_ACCEPT_RESULT( expr, stopOnFailure ) \
|
||||
}
|
||||
|
||||
#define INTERNAL_CATCH_THROWS( expr, exceptionType, nothrow, stopOnFailure, macroName ) \
|
||||
Catch::ResultsCapture::acceptExpression( Catch::ResultBuilder( #expr, false, __FILE__, __LINE__, macroName ) ); \
|
||||
Catch::ResultsCapture::getListener().acceptExpression( Catch::ResultBuilder( #expr, false, __FILE__, __LINE__, macroName ) ); \
|
||||
try \
|
||||
{ \
|
||||
expr; \
|
||||
@ -413,8 +370,8 @@ catch( ... ) \
|
||||
{ \
|
||||
std::ostringstream INTERNAL_CATCH_UNIQUE_NAME( strm ); \
|
||||
INTERNAL_CATCH_UNIQUE_NAME( strm ) << reason; \
|
||||
Catch::ResultsCapture::acceptExpression( Catch::MutableResultInfo( "", false, __FILE__, __LINE__, macroName ) ); \
|
||||
Catch::ResultsCapture::acceptMessage( INTERNAL_CATCH_UNIQUE_NAME( strm ).str() ); \
|
||||
Catch::ResultsCapture::getListener().acceptExpression( Catch::MutableResultInfo( "", false, __FILE__, __LINE__, macroName ) ); \
|
||||
Catch::ResultsCapture::getListener().acceptMessage( INTERNAL_CATCH_UNIQUE_NAME( strm ).str() ); \
|
||||
INTERNAL_CATCH_ACCEPT_RESULT( resultType, stopOnFailure ) \
|
||||
}
|
||||
|
||||
|
@ -136,13 +136,13 @@ namespace Catch
|
||||
}
|
||||
catch( std::exception& ex )
|
||||
{
|
||||
ResultsCapture::acceptMessage( ex.what() );
|
||||
ResultsCapture::acceptResult( ResultWas::ThrewException );
|
||||
acceptMessage( ex.what() );
|
||||
acceptResult( ResultWas::ThrewException );
|
||||
}
|
||||
catch(...)
|
||||
{
|
||||
ResultsCapture::acceptMessage( "unknown exception" );
|
||||
ResultsCapture::acceptResult( ResultWas::ThrewException );
|
||||
acceptMessage( "unknown exception" );
|
||||
acceptResult( ResultWas::ThrewException );
|
||||
}
|
||||
m_info.clear();
|
||||
m_reporter->EndTestCase( testInfo, redirectedCout, redirectedCerr );
|
||||
@ -160,6 +160,39 @@ namespace Catch
|
||||
|
||||
private: // IResultListener
|
||||
|
||||
virtual ResultAction::Value acceptResult( bool result )
|
||||
{
|
||||
return acceptResult( result ? ResultWas::Ok : ResultWas::ExpressionFailed );
|
||||
}
|
||||
|
||||
virtual ResultAction::Value acceptResult( ResultWas::OfType result )
|
||||
{
|
||||
m_currentResult.setResultType( result );
|
||||
testEnded( m_currentResult );
|
||||
|
||||
bool ok = m_currentResult.ok();
|
||||
m_currentResult = MutableResultInfo();
|
||||
if( ok )
|
||||
return ResultAction::None;
|
||||
else if( shouldDebugBreak() )
|
||||
return ResultAction::DebugFailed;
|
||||
else
|
||||
return ResultAction::Failed;
|
||||
|
||||
}
|
||||
|
||||
virtual void acceptExpression( const MutableResultInfo& resultInfo )
|
||||
{
|
||||
m_currentResult = resultInfo;
|
||||
}
|
||||
|
||||
virtual void acceptMessage( const std::string& msg )
|
||||
{
|
||||
m_currentResult.setMessage( msg );
|
||||
}
|
||||
|
||||
///
|
||||
|
||||
virtual void testEnded( const ResultInfo& result )
|
||||
{
|
||||
if( result.getResultType() == ResultWas::Ok )
|
||||
@ -213,6 +246,8 @@ namespace Catch
|
||||
}
|
||||
|
||||
private:
|
||||
MutableResultInfo m_currentResult;
|
||||
|
||||
const RunnerConfig& m_config;
|
||||
std::size_t m_successes;
|
||||
std::size_t m_failures;
|
||||
|
@ -24,13 +24,13 @@ namespace Catch
|
||||
public:
|
||||
Section( const std::string& name, const std::string& description )
|
||||
: m_name( name ),
|
||||
m_sectionIncluded( ResultsCapture::acceptSectionStart( name, description, m_successes, m_failures ) )
|
||||
m_sectionIncluded( ResultsCapture::getListener().sectionStarted( name, description, m_successes, m_failures ) )
|
||||
{
|
||||
}
|
||||
|
||||
~Section()
|
||||
{
|
||||
ResultsCapture::acceptSectionEnd( m_name, m_successes, m_failures );
|
||||
ResultsCapture::getListener().sectionEnded( m_name, m_successes, m_failures );
|
||||
}
|
||||
|
||||
// This indicates whether the section should be executed or not
|
||||
|
Loading…
Reference in New Issue
Block a user