Some RunContext clean-up

This commit is contained in:
Phil Nash 2015-11-19 07:35:35 +00:00
parent 2ebe11660c
commit b77b45a390
9 changed files with 61 additions and 57 deletions

View File

@ -87,7 +87,9 @@ namespace Catch {
std::srand( config.rngSeed() ); std::srand( config.rngSeed() );
} }
unsigned int rngSeed() { unsigned int rngSeed() {
return getCurrentContext().getConfig()->rngSeed(); return getCurrentConfig()
? getCurrentConfig()->rngSeed()
: 0;
} }
std::ostream& operator << ( std::ostream& os, SourceLineInfo const& info ) { std::ostream& operator << ( std::ostream& os, SourceLineInfo const& info ) {

View File

@ -145,7 +145,7 @@ namespace {
}; };
IColourImpl* platformColourInstance() { IColourImpl* platformColourInstance() {
Ptr<IConfig const> config = getCurrentContext().getConfig(); IConfig const* config = getCurrentConfig();
return (config && config->forceColour()) || isatty(STDOUT_FILENO) return (config && config->forceColour()) || isatty(STDOUT_FILENO)
? PosixColourImpl::instance() ? PosixColourImpl::instance()
: NoColourImpl::instance(); : NoColourImpl::instance();

View File

@ -28,7 +28,7 @@ namespace Catch {
virtual IResultCapture* getResultCapture() = 0; virtual IResultCapture* getResultCapture() = 0;
virtual IRunner* getRunner() = 0; virtual IRunner* getRunner() = 0;
virtual Ptr<IConfig const> getConfig() const = 0; virtual IConfig const* getConfig() const = 0;
}; };
struct IMutableContext : IContext struct IMutableContext : IContext
@ -42,8 +42,7 @@ namespace Catch {
IContext& getCurrentContext(); IContext& getCurrentContext();
IMutableContext& getCurrentMutableContext(); IMutableContext& getCurrentMutableContext();
void cleanUpContext(); void cleanUpContext();
Stream createStream( std::string const& streamName ); IConfig const* getCurrentConfig();
} }
#endif // TWOBLUECUBES_CATCH_CONTEXT_H_INCLUDED #endif // TWOBLUECUBES_CATCH_CONTEXT_H_INCLUDED

View File

@ -28,8 +28,8 @@ namespace Catch {
virtual IRunner* getRunner() { virtual IRunner* getRunner() {
return m_runner; return m_runner;
} }
virtual Ptr<IConfig const> getConfig() const { virtual IConfig const* getConfig() const {
return m_config; return m_config.get();
} }
public: // IMutableContext public: // IMutableContext
@ -67,6 +67,13 @@ namespace Catch {
delete currentContext; delete currentContext;
currentContext = CATCH_NULL; currentContext = CATCH_NULL;
} }
IConfig const* getCurrentConfig() {
return currentContext
? currentContext->getConfig()
: CATCH_NULL;
}
} }
#endif // TWOBLUECUBES_CATCH_CONTEXT_IMPL_HPP_INCLUDED #endif // TWOBLUECUBES_CATCH_CONTEXT_IMPL_HPP_INCLUDED

View File

@ -101,7 +101,7 @@ namespace Catch {
getResultCapture().assertionEnded( result ); getResultCapture().assertionEnded( result );
if( !result.isOk() ) { if( !result.isOk() ) {
if( getCurrentContext().getConfig()->shouldDebugBreak() ) if( getCurrentConfig()->shouldDebugBreak() )
m_shouldDebugBreak = true; m_shouldDebugBreak = true;
if( getCurrentContext().getRunner()->aborting() || (m_assertionInfo.resultDisposition & ResultDisposition::Normal) ) if( getCurrentContext().getRunner()->aborting() || (m_assertionInfo.resultDisposition & ResultDisposition::Normal) )
m_shouldThrow = true; m_shouldThrow = true;
@ -113,7 +113,7 @@ namespace Catch {
} }
bool ResultBuilder::shouldDebugBreak() const { return m_shouldDebugBreak; } bool ResultBuilder::shouldDebugBreak() const { return m_shouldDebugBreak; }
bool ResultBuilder::allowThrows() const { return getCurrentContext().getConfig()->allowThrows(); } bool ResultBuilder::allowThrows() const { return getCurrentConfig()->allowThrows(); }
AssertionResult ResultBuilder::build() const AssertionResult ResultBuilder::build() const
{ {

View File

@ -62,9 +62,9 @@ namespace Catch {
explicit RunContext( Ptr<IConfig const> const& _config, Ptr<IStreamingReporter> const& reporter ) explicit RunContext( Ptr<IConfig const> const& _config, Ptr<IStreamingReporter> const& reporter )
: m_runInfo( _config->name() ), : m_runInfo( _config->name() ),
m_context( getCurrentMutableContext() ), m_context( getCurrentMutableContext() ),
m_activeTestCase( CATCH_NULL ),
m_config( _config ), m_config( _config ),
m_reporter( reporter ) m_reporter( reporter ),
m_activeTestCaseInfo( CATCH_NULL )
{ {
m_context.setRunner( this ); m_context.setRunner( this );
m_context.setConfig( m_config ); m_context.setConfig( m_config );
@ -84,36 +84,33 @@ namespace Catch {
} }
Totals runTest( TestCase const& testCase ) { Totals runTest( TestCase const& testCase ) {
m_activeTestCaseInfo = &testCase;
Totals prevTotals = m_totals; Totals prevTotals = m_totals;
std::string redirectedCout, redirectedCerr;
std::string redirectedCout; m_reporter->testCaseStarting( testCase );
std::string redirectedCerr;
TestCaseInfo testInfo = testCase.getTestCaseInfo();
m_reporter->testCaseStarting( testInfo );
m_activeTestCase = &testCase;
ITracker* m_testCaseTracker;
m_trackerContext.startRun(); m_trackerContext.startRun();
do { do {
m_trackerContext.startCycle(); m_trackerContext.startCycle();
m_testCaseTracker = &SectionTracker::acquire( m_trackerContext, testInfo.name ); m_testCaseTracker = &SectionTracker::acquire( m_trackerContext, testCase.name );
runCurrentTest( redirectedCout, redirectedCerr ); runTest( testCase, redirectedCout, redirectedCerr );
} }
while( !m_testCaseTracker->isSuccessfullyCompleted() && !aborting() ); while( !m_testCaseTracker->isSuccessfullyCompleted() && !aborting() );
Totals deltaTotals = m_totals.delta( prevTotals ); Totals deltaTotals = m_totals.delta( prevTotals );
m_totals.testCases += deltaTotals.testCases; m_totals.testCases += deltaTotals.testCases;
m_reporter->testCaseEnded( TestCaseStats( testInfo, m_reporter->testCaseEnded( TestCaseStats( testCase,
deltaTotals, deltaTotals,
redirectedCout, redirectedCout,
redirectedCerr, redirectedCerr,
aborting() ) ); aborting() ) );
m_activeTestCase = CATCH_NULL; m_activeTestCaseInfo = CATCH_NULL;
m_testCaseTracker = CATCH_NULL;
return deltaTotals; return deltaTotals;
} }
@ -126,12 +123,10 @@ namespace Catch {
virtual void assertionEnded( AssertionResult const& result ) { virtual void assertionEnded( AssertionResult const& result ) {
if( result.getResultType() == ResultWas::Ok ) { if( result.getResultType() == ResultWas::Ok )
m_totals.assertions.passed++; m_totals.assertions.passed++;
} else if( !result.isOk() )
else if( !result.isOk() ) {
m_totals.assertions.failed++; m_totals.assertions.failed++;
}
if( m_reporter->assertionEnded( AssertionStats( result, m_messages, m_totals ) ) ) if( m_reporter->assertionEnded( AssertionStats( result, m_messages, m_totals ) ) )
m_messages.clear(); m_messages.clear();
@ -206,8 +201,8 @@ namespace Catch {
} }
virtual std::string getCurrentTestName() const { virtual std::string getCurrentTestName() const {
return m_activeTestCase return m_activeTestCaseInfo
? m_activeTestCase->getTestCaseInfo().name ? m_activeTestCaseInfo->name
: ""; : "";
} }
@ -224,19 +219,19 @@ namespace Catch {
handleUnfinishedSections(); handleUnfinishedSections();
// Recreate section for test case (as we will lose the one that was in scope) // Recreate section for test case (as we will lose the one that was in scope)
TestCaseInfo const& testCaseInfo = m_activeTestCase->getTestCaseInfo(); SectionInfo testCaseSection
SectionInfo testCaseSection( testCaseInfo.lineInfo, testCaseInfo.name, testCaseInfo.description ); ( m_activeTestCaseInfo->lineInfo,
m_activeTestCaseInfo->name,
m_activeTestCaseInfo->description );
Counts assertions; Counts assertions;
assertions.failed = 1; assertions.failed = 1;
SectionStats testCaseSectionStats( testCaseSection, assertions, 0, false ); SectionStats testCaseSectionStats( testCaseSection, assertions, 0, false );
m_reporter->sectionEnded( testCaseSectionStats ); m_reporter->sectionEnded( testCaseSectionStats );
TestCaseInfo testInfo = m_activeTestCase->getTestCaseInfo();
Totals deltaTotals; Totals deltaTotals;
deltaTotals.testCases.failed = 1; deltaTotals.testCases.failed = 1;
m_reporter->testCaseEnded( TestCaseStats( testInfo, m_reporter->testCaseEnded( TestCaseStats( *m_activeTestCaseInfo,
deltaTotals, deltaTotals,
"", "",
"", "",
@ -254,14 +249,13 @@ namespace Catch {
private: private:
void runCurrentTest( std::string& redirectedCout, std::string& redirectedCerr ) { void runTest( TestCase const& testCase, std::string& redirectedCout, std::string& redirectedCerr ) {
TestCaseInfo const& testCaseInfo = m_activeTestCase->getTestCaseInfo(); SectionInfo testCaseSection( testCase.lineInfo, testCase.name, testCase.description );
SectionInfo testCaseSection( testCaseInfo.lineInfo, testCaseInfo.name, testCaseInfo.description );
m_reporter->sectionStarting( testCaseSection ); m_reporter->sectionStarting( testCaseSection );
Counts prevAssertions = m_totals.assertions; Counts prevAssertions = m_totals.assertions;
double duration = 0; double duration = 0;
try { try {
m_lastAssertionInfo = AssertionInfo( "TEST_CASE", testCaseInfo.lineInfo, "", ResultDisposition::Normal ); m_lastAssertionInfo = AssertionInfo( "TEST_CASE", testCase.lineInfo, "", ResultDisposition::Normal );
seedRng( *m_config ); seedRng( *m_config );
@ -270,10 +264,10 @@ namespace Catch {
if( m_reporter->getPreferences().shouldRedirectStdOut ) { if( m_reporter->getPreferences().shouldRedirectStdOut ) {
StreamRedirect coutRedir( Catch::cout(), redirectedCout ); StreamRedirect coutRedir( Catch::cout(), redirectedCout );
StreamRedirect cerrRedir( Catch::cerr(), redirectedCerr ); StreamRedirect cerrRedir( Catch::cerr(), redirectedCerr );
invokeActiveTestCase(); invokeTestCase( testCase );
} }
else { else {
invokeActiveTestCase(); invokeTestCase( testCase );
} }
duration = timer.getElapsedSeconds(); duration = timer.getElapsedSeconds();
} }
@ -283,14 +277,15 @@ namespace Catch {
catch(...) { catch(...) {
makeUnexpectedResultBuilder().useActiveException(); makeUnexpectedResultBuilder().useActiveException();
} }
m_testCaseTracker->close(); m_trackerContext.currentTracker().close();
handleUnfinishedSections(); handleUnfinishedSections();
m_messages.clear(); m_messages.clear();
Counts assertions = m_totals.assertions - prevAssertions; Counts assertions = m_totals.assertions - prevAssertions;
bool missingAssertions = testForMissingAssertions( assertions ); bool missingAssertions = testForMissingAssertions( assertions );
if( testCaseInfo.okToFail() ) { if( testCase.okToFail() ) {
std::swap( assertions.failedButOk, assertions.failed ); std::swap( assertions.failedButOk, assertions.failed );
m_totals.assertions.failed -= assertions.failedButOk; m_totals.assertions.failed -= assertions.failedButOk;
m_totals.assertions.failedButOk += assertions.failedButOk; m_totals.assertions.failedButOk += assertions.failedButOk;
@ -300,10 +295,9 @@ namespace Catch {
m_reporter->sectionEnded( testCaseSectionStats ); m_reporter->sectionEnded( testCaseSectionStats );
} }
void invokeActiveTestCase() { static void invokeTestCase( TestCase const& testCase ) {
FatalConditionHandler fatalConditionHandler; // Handle signals FatalConditionHandler fatalConditionHandler; // Handle signals
m_activeTestCase->invoke(); testCase.invoke();
fatalConditionHandler.reset();
} }
private: private:
@ -328,19 +322,19 @@ namespace Catch {
TestRunInfo m_runInfo; TestRunInfo m_runInfo;
IMutableContext& m_context; IMutableContext& m_context;
TestCase const* m_activeTestCase;
ITracker* m_testCaseTracker;
ITracker* m_currentSectionTracker;
AssertionResult m_lastResult;
Ptr<IConfig const> m_config; Ptr<IConfig const> m_config;
Totals m_totals;
Ptr<IStreamingReporter> m_reporter; Ptr<IStreamingReporter> m_reporter;
std::vector<MessageInfo> m_messages; TrackerContext m_trackerContext;
Totals m_totals;
// Transient state
TestCaseInfo const* m_activeTestCaseInfo;
AssertionResult m_lastResult;
AssertionInfo m_lastAssertionInfo; AssertionInfo m_lastAssertionInfo;
std::vector<SectionEndInfo> m_unfinishedSections; std::vector<SectionEndInfo> m_unfinishedSections;
std::vector<ITracker*> m_activeSections; std::vector<ITracker*> m_activeSections;
TrackerContext m_trackerContext; std::vector<MessageInfo> m_messages;
}; };
IResultCapture& getResultCapture() { IResultCapture& getResultCapture() {

View File

@ -55,7 +55,7 @@ namespace Detail {
std::string toString( std::string const& value ) { std::string toString( std::string const& value ) {
std::string s = value; std::string s = value;
if( getCurrentContext().getConfig()->showInvisibles() ) { if( getCurrentConfig() && getCurrentConfig()->showInvisibles() ) {
for(size_t i = 0; i < s.size(); ++i ) { for(size_t i = 0; i < s.size(); ++i ) {
std::string subs; std::string subs;
switch( s[i] ) { switch( s[i] ) {

View File

@ -1,2 +1,2 @@
// The old generators have been removed // The old generators have been removed
// A new generator implementation is coming // A new generator implementation is coming

View File

@ -475,6 +475,8 @@ TEST_CASE( "long long" ) {
//TEST_CASE( "Divide by Zero signal handler", "[.][sig]" ) { //TEST_CASE( "Divide by Zero signal handler", "[.][sig]" ) {
// int i = 0; // int i = 0;
// int x = 10/i; // This should cause the signal to fire // SECTION( "s" ) {
// CHECK( x == 0 ); // int x = 10/i; // This should cause the signal to fire
// CHECK( x == 0 );
// }
//} //}