Started adding support for isolated sections

This commit is contained in:
Phil Nash 2011-01-19 08:52:25 +00:00
parent 2b05dfaf05
commit ae307b000a
3 changed files with 94 additions and 29 deletions

View File

@ -10,9 +10,10 @@
* *
*/ */
#include "../internal/catch_self_test.hpp"
#include "../catch_with_main.hpp" #include "../catch_with_main.hpp"
#include "../internal/catch_self_test.hpp"
TEST_CASE( "selftest/main", "Runs all Catch self tests and checks their results" ) TEST_CASE( "selftest/main", "Runs all Catch self tests and checks their results" )
{ {
using namespace Catch; using namespace Catch;

View File

@ -48,6 +48,48 @@ namespace Catch
bool m_isWildcarded; bool m_isWildcarded;
}; };
class RunningTest
{
public:
explicit RunningTest( const TestCaseInfo* info = NULL )
: m_info( info ),
m_sectionSeen( false )
{
}
size_t sectionsSeenCount() const
{
return m_sectionsSeen.size();
}
bool wasSectionSeen() const
{
return m_sectionSeen;
}
void resetSectionSeen()
{
m_sectionSeen = false;
}
bool addSection( const std::string& name )
{
if( m_sectionsSeen.find( name ) != m_sectionsSeen.end() )
return false;
m_sectionsSeen.insert( name );
return m_sectionSeen = true;
}
const TestCaseInfo& getTestCaseInfo() const
{
return *m_info;
}
private:
const TestCaseInfo* m_info;
bool m_sectionSeen;
std::set<std::string> m_sectionsSeen;
};
class StreamRedirect class StreamRedirect
{ {
public: public:
@ -145,36 +187,16 @@ namespace Catch
const TestCaseInfo& testInfo const TestCaseInfo& testInfo
) )
{ {
std::size_t prevSuccessCount = m_successes; m_runningTest = RunningTest( &testInfo );
std::size_t prevFailureCount = m_failures;
m_reporter->StartTestCase( testInfo ); do
{
m_runningTest.resetSectionSeen();
runCurrentTest();
}
while( m_runningTest.wasSectionSeen() );
std::string redirectedCout; m_runningTest = RunningTest();
std::string redirectedCerr;
try
{
StreamRedirect coutRedir( std::cout, redirectedCout );
StreamRedirect cerrRedir( std::cerr, redirectedCerr );
testInfo.invoke();
}
catch( TestFailureException& )
{
// This just means the test was aborted due to failure
}
catch( std::exception& ex )
{
acceptMessage( ex.what() );
acceptResult( ResultWas::ThrewException );
}
catch(...)
{
acceptMessage( "unknown exception" );
acceptResult( ResultWas::ThrewException );
}
m_info.clear();
m_reporter->EndTestCase( testInfo, m_successes - prevSuccessCount, m_failures - prevFailureCount, redirectedCout, redirectedCerr );
} }
/////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////
@ -278,6 +300,9 @@ namespace Catch
std::size_t& failures std::size_t& failures
) )
{ {
if( m_runningTest.wasSectionSeen() || !m_runningTest.addSection( name ) )
return false;
m_reporter->StartSection( name, description ); m_reporter->StartSection( name, description );
successes = m_successes; successes = m_successes;
failures = m_failures; failures = m_failures;
@ -323,8 +348,46 @@ namespace Catch
{ {
return m_config.shouldDebugBreak(); return m_config.shouldDebugBreak();
} }
private:
///////////////////////////////////////////////////////////////////////////
void runCurrentTest
()
{
std::size_t prevSuccessCount = m_successes;
std::size_t prevFailureCount = m_failures;
m_reporter->StartTestCase( m_runningTest.getTestCaseInfo() );
std::string redirectedCout;
std::string redirectedCerr;
try
{
StreamRedirect coutRedir( std::cout, redirectedCout );
StreamRedirect cerrRedir( std::cerr, redirectedCerr );
m_runningTest.getTestCaseInfo().invoke();
}
catch( TestFailureException& )
{
// This just means the test was aborted due to failure
}
catch( std::exception& ex )
{
acceptMessage( ex.what() );
acceptResult( ResultWas::ThrewException );
}
catch(...)
{
acceptMessage( "unknown exception" );
acceptResult( ResultWas::ThrewException );
}
m_info.clear();
m_reporter->EndTestCase( m_runningTest.getTestCaseInfo(), m_successes - prevSuccessCount, m_failures - prevFailureCount, redirectedCout, redirectedCerr );
}
private: private:
RunningTest m_runningTest;
MutableResultInfo m_currentResult; MutableResultInfo m_currentResult;
const Config& m_config; const Config& m_config;

View File

@ -30,6 +30,7 @@ namespace Catch
~Section() ~Section()
{ {
if( m_sectionIncluded )
Hub::getResultCapture().sectionEnded( m_name, m_successes, m_failures ); Hub::getResultCapture().sectionEnded( m_name, m_successes, m_failures );
} }