Added streaming to message macros and added scoped info

This commit is contained in:
Phil Nash 2010-12-27 11:09:34 +00:00
parent b3ef7fc0f2
commit 0025668584
5 changed files with 65 additions and 8 deletions

View File

@ -14,11 +14,11 @@
TEST_CASE( "succeeding/message", "INFO and WARN do not abort tests" )
{
INFO( "this is a message" ); // This should output the message but continue
WARN( "this is a warning" ); // This should output the message but continue
INFO( "this is a " << "message" ); // This should output the message but continue
WARN( "this is a " << "warning" ); // This should output the message but continue
}
TEST_CASE( "failing/message", "FAIL aborts the test" )
{
FAIL( "This is a failure" ); // This should output the message and abort
FAIL( "This is a " << "failure" ); // This should output the message and abort
}

View File

@ -49,6 +49,7 @@
#define INFO( reason ) INTERNAL_CATCH_MSG( reason, Catch::ResultWas::Info, false, "INFO" )
#define WARN( reason ) INTERNAL_CATCH_MSG( reason, Catch::ResultWas::Warning, false, "WARN" )
#define FAIL( reason ) INTERNAL_CATCH_MSG( reason, Catch::ResultWas::ExplicitFailure, true, "FAIL" )
#define SCOPED_INFO( log ) INTERNAL_CATCH_SCOPED_INFO( log )
#define SECTION( name, description ) CATCH_SECTION( name, description )

View File

@ -76,7 +76,7 @@ namespace Catch
if( runner.runMatching( *it ) == 0 )
{
// Use reporter?
std::cerr << "\n[Unable to match any test cases with: " << *it << "]" << std::endl;
// std::cerr << "\n[Unable to match any test cases with: " << *it << "]" << std::endl;
}
config.getReporter()->EndGroup( *it, runner.getSuccessCount()-prevSuccess, runner.getFailureCount()-prevFail );
}

View File

@ -190,6 +190,7 @@ private:
};
class TestCaseInfo;
class ScopedInfo;
struct IResultListener
{
@ -197,8 +198,10 @@ struct IResultListener
virtual void testEnded( const ResultInfo& result ) = 0;
virtual bool sectionStarted( const std::string& name, const std::string& description, std::size_t& successes, std::size_t& failures ) = 0;
virtual void sectionEnded( const std::string& name, std::size_t successes, std::size_t failures ) = 0;
virtual void pushScopedInfo( ScopedInfo* scopedInfo ) = 0;
virtual void popScopedInfo( ScopedInfo* scopedInfo ) = 0;
};
class ResultsCapture
{
private:
@ -274,11 +277,50 @@ public:
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 );
}
private:
MutableResultInfo currentResult;
IResultListener* m_listener;
};
class ScopedInfo
{
public:
ScopedInfo()
{
ResultsCapture::pushScopedInfo( this );
}
~ScopedInfo()
{
ResultsCapture::popScopedInfo( this );
}
ScopedInfo& operator << ( const char* str )
{
m_oss << str;
return *this;
}
std::string getInfo() const
{
return m_oss.str();
}
private:
std::ostringstream m_oss;
};
// !TBD Need to clean this all up
#define CATCH_absTol 1e-10
#define CATCH_relTol 1e-10
@ -349,9 +391,12 @@ catch( ... ) \
}
#define INTERNAL_CATCH_MSG( reason, resultType, stopOnFailure, macroName ) \
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( reason ); \
Catch::ResultsCapture::acceptMessage( INTERNAL_CATCH_UNIQUE_NAME( strm ).str() ); \
Catch::ResultsCapture::acceptResult( resultType, stopOnFailure );
#define INTERNAL_CATCH_SCOPED_INFO( log ) Catch::ScopedInfo INTERNAL_CATCH_UNIQUE_NAME( info ); INTERNAL_CATCH_UNIQUE_NAME( info ) << log
#endif // TWOBLUECUBES_CATCH_CAPTURE_HPP_INCLUDED

View File

@ -157,9 +157,9 @@ namespace Catch
virtual void testEnded( const ResultInfo& result )
{
if( result.ok() )
if( result.getResultType() == ResultWas::Ok )
m_successes++;
else
else if( !result.ok() )
m_failures++;
m_reporter->Result( result );
@ -179,11 +179,22 @@ namespace Catch
{
m_reporter->EndSection( name, m_successes - prevSuccesses, m_failures - prevFailures );
}
virtual void pushScopedInfo( ScopedInfo* scopedInfo )
{
m_scopedInfos.push_back( scopedInfo );
}
virtual void popScopedInfo( ScopedInfo* scopedInfo )
{
if( m_scopedInfos.back() == scopedInfo )
m_scopedInfos.pop_back();
}
private:
std::size_t m_successes;
std::size_t m_failures;
ITestReporter* m_reporter;
std::vector<ScopedInfo*> m_scopedInfos;
};
}