Refactored printResultType and printMessage to work off a single switch

This commit is contained in:
Phil Nash 2013-01-14 19:28:28 +00:00
parent 3b970e20e9
commit 313481006f
1 changed files with 88 additions and 55 deletions

View File

@ -48,40 +48,98 @@ namespace Catch {
stream << result.getSourceInfo() << ":\n"; stream << result.getSourceInfo() << ":\n";
} }
ResultComponents components( result );
bool endsWithNewLine = false; bool endsWithNewLine = false;
if( _assertionStats.totals.assertions.total() > 0 ) { if( _assertionStats.totals.assertions.total() > 0 ) {
endsWithNewLine = printOriginalExpression( result ); printOriginalExpression( result );
endsWithNewLine = printResultType( result ); printResultType( components );
endsWithNewLine |= printReconstructedExpression( result ); endsWithNewLine = printReconstructedExpression( result );
} }
endsWithNewLine |= printMessage( result ); endsWithNewLine |= printMessage( components );
if( !endsWithNewLine ) if( !endsWithNewLine )
stream << "\n"; stream << "\n";
stream << std::endl; stream << std::endl;
} }
bool printResultType( AssertionResult const& _result ) { struct ResultComponents {
switch( _result.getResultType() ) { ResultComponents( AssertionResult const& _result )
case ResultWas::Info: : colour( TextColour::None )
case ResultWas::Warning: {
break; switch( _result.getResultType() ) {
case ResultWas::Ok: case ResultWas::Ok:
{ colour = TextColour::Success;
TextColour successColour( TextColour::Success ); passOrFail = "passed";
stream << "passed "; if( _result.hasMessage() ){
} messageLabel = "with message";
break; message = _result.getMessage();
default: }
if( _result.isOk() ) { break;
TextColour okAnywayColour( TextColour::Success ); case ResultWas::ExpressionFailed:
stream << "failed - but was ok "; if( _result.isOk() ) {
} colour = TextColour::Success;
else { passOrFail = "failed - but was ok";
TextColour errorColour( TextColour::Error ); }
stream << "failed "; else {
} colour = TextColour::Error;
passOrFail = "failed";
}
if( _result.hasMessage() ){
messageLabel = "with message";
message = _result.getMessage();
}
break;
case ResultWas::ThrewException:
colour = TextColour::Error;
passOrFail = "failed";
messageLabel = "due to unexpected exception with message";
message = _result.getMessage();
break;
case ResultWas::DidntThrowException:
colour = TextColour::Error;
passOrFail = "failed";
messageLabel = "because no exception was thrown where one was expected";
break;
case ResultWas::Info:
messageLabel = "info";
message = _result.getMessage();
break;
case ResultWas::Warning:
messageLabel = "warning";
message = _result.getMessage();
break;
case ResultWas::ExplicitFailure:
passOrFail = "failed";
colour = TextColour::Error;
messageLabel = "explicitly with message";
message = _result.getMessage();
break;
case ResultWas::Exception:
passOrFail = "failed";
colour = TextColour::Error;
if( _result.hasMessage() ){
messageLabel = "with message";
message = _result.getMessage();
}
break;
case ResultWas::Unknown: // These cases are here to prevent compiler warnings
case ResultWas::FailureBit:
passOrFail = "** internal error **";
colour = TextColour::Error;
break;
}
}
TextColour::Colours colour;
std::string passOrFail;
std::string messageLabel;
std::string message;
};
void printResultType( ResultComponents const& _components ) {
if( !_components.passOrFail.empty() ) {
TextColour colour( _components.colour );
stream << _components.passOrFail << " ";
} }
return false;
} }
bool printOriginalExpression( AssertionResult const& _result ) { bool printOriginalExpression( AssertionResult const& _result ) {
if( _result.hasExpression() ) { if( _result.hasExpression() ) {
@ -106,43 +164,18 @@ namespace Catch {
} }
return false; return false;
} }
bool printMessage( AssertionResult const& _result ) { bool printMessage( ResultComponents const& _components ) {
std::pair<std::string, std::string> message = getMessage( _result );
bool endsWithNewLine = false; bool endsWithNewLine = false;
if( !message.first.empty() ) { if( !_components.messageLabel.empty() ) {
stream << message.first << ":" << "\n"; stream << _components.messageLabel << ":" << "\n";
endsWithNewLine = true; endsWithNewLine = true;
} }
if( !message.second.empty() ) { if( !_components.message.empty() ) {
stream << wrapLongStrings( message.second ) << "\n"; stream << wrapLongStrings( _components.message ) << "\n";
endsWithNewLine = true; endsWithNewLine = true;
} }
return endsWithNewLine; return endsWithNewLine;
} }
std::pair<std::string, std::string> getMessage( AssertionResult const& _result ) {
switch( _result.getResultType() ) {
case ResultWas::ThrewException:
return std::make_pair( "due to unexpected exception with message", _result.getMessage() );
case ResultWas::DidntThrowException:
return std::make_pair( "because no exception was thrown where one was expected", "" );
case ResultWas::Info:
return std::make_pair( "info", _result.getMessage() );
case ResultWas::Warning:
return std::make_pair( "warning", _result.getMessage() );
case ResultWas::ExplicitFailure:
return std::make_pair( "explicitly with message", _result.getMessage() );
case ResultWas::Unknown: // These cases are here to prevent compiler warnings
case ResultWas::Ok:
case ResultWas::FailureBit:
case ResultWas::ExpressionFailed:
case ResultWas::Exception:
if( _result.hasMessage() )
return std::make_pair( "with message", _result.getMessage() );
else
return std::make_pair( "", "" );
}
}
virtual void sectionEnded( SectionStats const& _sectionStats ) { virtual void sectionEnded( SectionStats const& _sectionStats ) {
if( _sectionStats.missingAssertions ) { if( _sectionStats.missingAssertions ) {