Eliminate some work when results won't be reported.

This commit is contained in:
Neal Coombes 2017-06-26 14:30:23 -05:00 committed by Martin Hořeňovský
parent 28741467d5
commit 2212cdfe26
6 changed files with 60 additions and 20 deletions

View File

@ -83,12 +83,12 @@
///////////////////////////////////////////////////////////////////////////////
#define INTERNAL_CATCH_IF( macroName, resultDisposition, ... ) \
INTERNAL_CATCH_TEST( macroName, resultDisposition, __VA_ARGS__ ); \
if( Catch::getResultCapture().getLastResult()->succeeded() )
if( Catch::getResultCapture().lastAssertionPassed() )
///////////////////////////////////////////////////////////////////////////////
#define INTERNAL_CATCH_ELSE( macroName, resultDisposition, ... ) \
INTERNAL_CATCH_TEST( macroName, resultDisposition, __VA_ARGS__ ); \
if( !Catch::getResultCapture().getLastResult()->succeeded() )
if( !Catch::getResultCapture().lastAssertionPassed() )
///////////////////////////////////////////////////////////////////////////////
#define INTERNAL_CATCH_NO_THROW( macroName, resultDisposition, ... ) \

View File

@ -41,6 +41,10 @@ namespace Catch {
virtual void exceptionEarlyReported() = 0;
virtual void handleFatalErrorCondition( std::string const& message ) = 0;
virtual bool lastAssertionPassed() = 0;
virtual void assertionPassed() = 0;
virtual void assertionRun() = 0;
};
IResultCapture& getResultCapture();

View File

@ -34,14 +34,12 @@ namespace Catch {
char const* capturedExpression,
ResultDisposition::Flags resultDisposition )
: m_assertionInfo( macroName, lineInfo, capturedExpression, resultDisposition)
{
m_stream().oss.str("");
}
{}
ResultBuilder::~ResultBuilder() {
#if defined(CATCH_CONFIG_FAST_COMPILE)
if ( m_guardException ) {
m_stream().oss << "Exception translation was disabled by CATCH_CONFIG_FAST_COMPILE";
stream().oss << "Exception translation was disabled by CATCH_CONFIG_FAST_COMPILE";
captureResult( ResultWas::ThrewException );
getCurrentContext().getResultCapture()->exceptionEarlyReported();
}
@ -58,13 +56,25 @@ namespace Catch {
}
void ResultBuilder::endExpression( DecomposedExpression const& expr ) {
AssertionResult result = build( expr );
handleResult( result );
// Flip bool results if FalseTest flag is set
if( isFalseTest( m_assertionInfo.resultDisposition ) ) {
m_data.negate( expr.isBinaryExpression() );
}
getResultCapture().assertionRun();
if(getCurrentContext().getConfig()->includeSuccessfulResults()
|| m_data.resultType != ResultWas::Ok) {
AssertionResult result = build( expr );
handleResult( result );
}
else
getResultCapture().assertionPassed();
}
void ResultBuilder::useActiveException( ResultDisposition::Flags resultDisposition ) {
m_assertionInfo.resultDisposition = resultDisposition;
m_stream().oss << Catch::translateActiveException();
stream().oss << Catch::translateActiveException();
captureResult( ResultWas::ThrewException );
}
@ -142,17 +152,12 @@ namespace Catch {
// It should immediately be passed to handleResult; if the expression
// needs to be reported, its string expansion must be composed before
// the temporaries are destroyed.
AssertionResult ResultBuilder::build( DecomposedExpression const& expr ) const
{
AssertionResult ResultBuilder::build( DecomposedExpression const& expr ) const {
assert( m_data.resultType != ResultWas::Unknown );
AssertionResultData data = m_data;
// Flip bool results if FalseTest flag is set
if( isFalseTest( m_assertionInfo.resultDisposition ) ) {
data.negate( expr.isBinaryExpression() );
}
data.message = m_stream().oss.str();
if(m_usedStream)
data.message = m_stream().oss.str();
data.decomposedExpression = &expr; // for lazy reconstruction
return AssertionResult( m_assertionInfo, data );
}
@ -168,7 +173,15 @@ namespace Catch {
m_guardException = false;
}
CopyableStream& ResultBuilder::m_stream() {
CopyableStream& ResultBuilder::stream() {
if( !m_usedStream ) {
m_usedStream = true;
s_stream().oss.str("");
}
return s_stream();
}
CopyableStream& ResultBuilder::s_stream() {
static CopyableStream s;
return s;
}

View File

@ -43,7 +43,7 @@ namespace Catch {
template<typename T>
ResultBuilder& operator << ( T const& value ) {
m_stream().oss << value;
stream().oss << value;
return *this;
}
@ -79,11 +79,13 @@ namespace Catch {
AssertionInfo m_assertionInfo;
AssertionResultData m_data;
static CopyableStream& m_stream();
CopyableStream& stream();
static CopyableStream& s_stream();
bool m_shouldDebugBreak = false;
bool m_shouldThrow = false;
bool m_guardException = false;
bool m_usedStream = false;
};
} // namespace Catch

View File

@ -213,6 +213,20 @@ namespace Catch {
m_reporter->testRunEnded(TestRunStats(m_runInfo, m_totals, false));
}
bool RunContext::lastAssertionPassed() {
return m_totals.assertions.passed == (m_prevPassed + 1);
}
void RunContext::assertionPassed() {
++m_totals.assertions.passed;
m_lastAssertionInfo.capturedExpression = "{Unknown expression after the reported line}";
m_lastAssertionInfo.macroName = "";
}
void RunContext::assertionRun() {
m_prevPassed = m_totals.assertions.passed;
}
bool RunContext::aborting() const {
return m_totals.assertions.failed == static_cast<std::size_t>(m_config->abortAfter());
}

View File

@ -84,6 +84,12 @@ namespace Catch {
virtual void handleFatalErrorCondition(std::string const& message) override;
virtual bool lastAssertionPassed() override;
virtual void assertionPassed();
virtual void assertionRun();
public:
// !TBD We need to do this another way!
bool aborting() const override;
@ -113,6 +119,7 @@ namespace Catch {
std::vector<SectionEndInfo> m_unfinishedSections;
std::vector<ITracker*> m_activeSections;
TrackerContext m_trackerContext;
size_t m_prevPassed = 0;
bool m_shouldReportUnexpected = true;
};