Tweaked Xml Reporter to follow same success/ info behaviour as Console reporter

This commit is contained in:
Phil Nash 2017-03-03 14:12:47 +00:00
parent be4f6ab8e1
commit 9bee606dd6
2 changed files with 107 additions and 25 deletions

View File

@ -92,73 +92,77 @@ namespace Catch {
virtual void assertionStarting( AssertionInfo const& ) CATCH_OVERRIDE { } virtual void assertionStarting( AssertionInfo const& ) CATCH_OVERRIDE { }
virtual bool assertionEnded( AssertionStats const& assertionStats ) CATCH_OVERRIDE { virtual bool assertionEnded( AssertionStats const& assertionStats ) CATCH_OVERRIDE {
const AssertionResult& assertionResult = assertionStats.assertionResult;
// Print any info messages in <Info> tags. AssertionResult const& result = assertionStats.assertionResult;
if( assertionStats.assertionResult.getResultType() != ResultWas::Ok ) {
bool includeResults = m_config->includeSuccessfulResults() || result.isOk();
if( includeResults ) {
// Print any info messages in <Info> tags.
for( std::vector<MessageInfo>::const_iterator it = assertionStats.infoMessages.begin(), itEnd = assertionStats.infoMessages.end(); for( std::vector<MessageInfo>::const_iterator it = assertionStats.infoMessages.begin(), itEnd = assertionStats.infoMessages.end();
it != itEnd; it != itEnd;
++it ) { ++it ) {
if( it->type == ResultWas::Info ) { if( it->type == ResultWas::Info ) {
m_xml.scopedElement( "Info" ) m_xml.scopedElement( "Info" )
.writeText( it->message ); .writeText( it->message );
} else if ( it->type == ResultWas::Warning ) { } else if ( it->type == ResultWas::Warning ) {
m_xml.scopedElement( "Warning" ) m_xml.scopedElement( "Warning" )
.writeText( it->message ); .writeText( it->message );
} }
} }
} }
// Drop out if result was successful but we're not printing them. // Drop out if result was successful but we're not printing them.
if( !m_config->includeSuccessfulResults() && isOk(assertionResult.getResultType()) ) if( !includeResults && result.getResultType() != ResultWas::Warning )
return true; return true;
// Print the expression if there is one.
if( assertionResult.hasExpression() ) {
m_xml.startElement( "Expression" )
.writeAttribute( "success", assertionResult.succeeded() )
.writeAttribute( "type", assertionResult.getTestMacroName() );
writeSourceInfo( assertionResult.getSourceInfo() ); // Print the expression if there is one.
if( result.hasExpression() ) {
m_xml.startElement( "Expression" )
.writeAttribute( "success", result.succeeded() )
.writeAttribute( "type", result.getTestMacroName() );
writeSourceInfo( result.getSourceInfo() );
m_xml.scopedElement( "Original" ) m_xml.scopedElement( "Original" )
.writeText( assertionResult.getExpression() ); .writeText( result.getExpression() );
m_xml.scopedElement( "Expanded" ) m_xml.scopedElement( "Expanded" )
.writeText( assertionResult.getExpandedExpression() ); .writeText( result.getExpandedExpression() );
} }
// And... Print a result applicable to each result type. // And... Print a result applicable to each result type.
switch( assertionResult.getResultType() ) { switch( result.getResultType() ) {
case ResultWas::ThrewException: case ResultWas::ThrewException:
m_xml.startElement( "Exception" ); m_xml.startElement( "Exception" );
writeSourceInfo( assertionResult.getSourceInfo() ); writeSourceInfo( result.getSourceInfo() );
m_xml.writeText( assertionResult.getMessage() ); m_xml.writeText( result.getMessage() );
m_xml.endElement(); m_xml.endElement();
break; break;
case ResultWas::FatalErrorCondition: case ResultWas::FatalErrorCondition:
m_xml.startElement( "FatalErrorCondition" ); m_xml.startElement( "FatalErrorCondition" );
writeSourceInfo( assertionResult.getSourceInfo() ); writeSourceInfo( result.getSourceInfo() );
m_xml.writeText( assertionResult.getMessage() ); m_xml.writeText( result.getMessage() );
m_xml.endElement(); m_xml.endElement();
break; break;
case ResultWas::Info: case ResultWas::Info:
m_xml.scopedElement( "Info" ) m_xml.scopedElement( "Info" )
.writeText( assertionResult.getMessage() ); .writeText( result.getMessage() );
break; break;
case ResultWas::Warning: case ResultWas::Warning:
// Warning will already have been written // Warning will already have been written
break; break;
case ResultWas::ExplicitFailure: case ResultWas::ExplicitFailure:
m_xml.startElement( "Failure" ); m_xml.startElement( "Failure" );
writeSourceInfo( assertionResult.getSourceInfo() ); writeSourceInfo( result.getSourceInfo() );
m_xml.writeText( assertionResult.getMessage() ); m_xml.writeText( result.getMessage() );
m_xml.endElement(); m_xml.endElement();
break; break;
default: default:
break; break;
} }
if( assertionResult.hasExpression() ) if( result.hasExpression() )
m_xml.endElement(); m_xml.endElement();
return true; return true;

View File

@ -645,6 +645,9 @@
</TestCase> </TestCase>
<TestCase name="Capture and info messages" filename="projects/<exe-name>/ToStringGeneralTests.cpp" > <TestCase name="Capture and info messages" filename="projects/<exe-name>/ToStringGeneralTests.cpp" >
<Section name="Capture should stringify like assertions" filename="projects/<exe-name>/ToStringGeneralTests.cpp" > <Section name="Capture should stringify like assertions" filename="projects/<exe-name>/ToStringGeneralTests.cpp" >
<Info>
i := 2
</Info>
<Expression success="true" type="REQUIRE" filename="projects/<exe-name>/ToStringGeneralTests.cpp" > <Expression success="true" type="REQUIRE" filename="projects/<exe-name>/ToStringGeneralTests.cpp" >
<Original> <Original>
true true
@ -656,6 +659,9 @@
<OverallResults successes="1" failures="0" expectedFailures="0"/> <OverallResults successes="1" failures="0" expectedFailures="0"/>
</Section> </Section>
<Section name="Info should NOT stringify the way assertions do" filename="projects/<exe-name>/ToStringGeneralTests.cpp" > <Section name="Info should NOT stringify the way assertions do" filename="projects/<exe-name>/ToStringGeneralTests.cpp" >
<Info>
3
</Info>
<Expression success="true" type="REQUIRE" filename="projects/<exe-name>/ToStringGeneralTests.cpp" > <Expression success="true" type="REQUIRE" filename="projects/<exe-name>/ToStringGeneralTests.cpp" >
<Original> <Original>
true true
@ -2632,6 +2638,9 @@
<OverallResult success="false"/> <OverallResult success="false"/>
</TestCase> </TestCase>
<TestCase name="INFO gets logged on failure, even if captured before successful assertions" tags="[.][failing][hide][messages]" filename="projects/<exe-name>/MessageTests.cpp" > <TestCase name="INFO gets logged on failure, even if captured before successful assertions" tags="[.][failing][hide][messages]" filename="projects/<exe-name>/MessageTests.cpp" >
<Info>
this message may be logged later
</Info>
<Expression success="true" type="CHECK" filename="projects/<exe-name>/MessageTests.cpp" > <Expression success="true" type="CHECK" filename="projects/<exe-name>/MessageTests.cpp" >
<Original> <Original>
a == 2 a == 2
@ -2662,6 +2671,9 @@
2 == 0 2 == 0
</Expanded> </Expanded>
</Expression> </Expression>
<Info>
but not this
</Info>
<Expression success="true" type="CHECK" filename="projects/<exe-name>/MessageTests.cpp" > <Expression success="true" type="CHECK" filename="projects/<exe-name>/MessageTests.cpp" >
<Original> <Original>
a == 2 a == 2
@ -6761,6 +6773,12 @@ re>"
<OverallResult success="true"/> <OverallResult success="true"/>
</TestCase> </TestCase>
<TestCase name="SCOPED_INFO is reset for each loop" tags="[.][failing][hide][messages]" filename="projects/<exe-name>/MessageTests.cpp" > <TestCase name="SCOPED_INFO is reset for each loop" tags="[.][failing][hide][messages]" filename="projects/<exe-name>/MessageTests.cpp" >
<Info>
current counter 0
</Info>
<Info>
i := 0
</Info>
<Expression success="true" type="REQUIRE" filename="projects/<exe-name>/MessageTests.cpp" > <Expression success="true" type="REQUIRE" filename="projects/<exe-name>/MessageTests.cpp" >
<Original> <Original>
i &lt; 10 i &lt; 10
@ -6769,6 +6787,12 @@ re>"
0 &lt; 10 0 &lt; 10
</Expanded> </Expanded>
</Expression> </Expression>
<Info>
current counter 1
</Info>
<Info>
i := 1
</Info>
<Expression success="true" type="REQUIRE" filename="projects/<exe-name>/MessageTests.cpp" > <Expression success="true" type="REQUIRE" filename="projects/<exe-name>/MessageTests.cpp" >
<Original> <Original>
i &lt; 10 i &lt; 10
@ -6777,6 +6801,12 @@ re>"
1 &lt; 10 1 &lt; 10
</Expanded> </Expanded>
</Expression> </Expression>
<Info>
current counter 2
</Info>
<Info>
i := 2
</Info>
<Expression success="true" type="REQUIRE" filename="projects/<exe-name>/MessageTests.cpp" > <Expression success="true" type="REQUIRE" filename="projects/<exe-name>/MessageTests.cpp" >
<Original> <Original>
i &lt; 10 i &lt; 10
@ -6785,6 +6815,12 @@ re>"
2 &lt; 10 2 &lt; 10
</Expanded> </Expanded>
</Expression> </Expression>
<Info>
current counter 3
</Info>
<Info>
i := 3
</Info>
<Expression success="true" type="REQUIRE" filename="projects/<exe-name>/MessageTests.cpp" > <Expression success="true" type="REQUIRE" filename="projects/<exe-name>/MessageTests.cpp" >
<Original> <Original>
i &lt; 10 i &lt; 10
@ -6793,6 +6829,12 @@ re>"
3 &lt; 10 3 &lt; 10
</Expanded> </Expanded>
</Expression> </Expression>
<Info>
current counter 4
</Info>
<Info>
i := 4
</Info>
<Expression success="true" type="REQUIRE" filename="projects/<exe-name>/MessageTests.cpp" > <Expression success="true" type="REQUIRE" filename="projects/<exe-name>/MessageTests.cpp" >
<Original> <Original>
i &lt; 10 i &lt; 10
@ -6801,6 +6843,12 @@ re>"
4 &lt; 10 4 &lt; 10
</Expanded> </Expanded>
</Expression> </Expression>
<Info>
current counter 5
</Info>
<Info>
i := 5
</Info>
<Expression success="true" type="REQUIRE" filename="projects/<exe-name>/MessageTests.cpp" > <Expression success="true" type="REQUIRE" filename="projects/<exe-name>/MessageTests.cpp" >
<Original> <Original>
i &lt; 10 i &lt; 10
@ -6809,6 +6857,12 @@ re>"
5 &lt; 10 5 &lt; 10
</Expanded> </Expanded>
</Expression> </Expression>
<Info>
current counter 6
</Info>
<Info>
i := 6
</Info>
<Expression success="true" type="REQUIRE" filename="projects/<exe-name>/MessageTests.cpp" > <Expression success="true" type="REQUIRE" filename="projects/<exe-name>/MessageTests.cpp" >
<Original> <Original>
i &lt; 10 i &lt; 10
@ -6817,6 +6871,12 @@ re>"
6 &lt; 10 6 &lt; 10
</Expanded> </Expanded>
</Expression> </Expression>
<Info>
current counter 7
</Info>
<Info>
i := 7
</Info>
<Expression success="true" type="REQUIRE" filename="projects/<exe-name>/MessageTests.cpp" > <Expression success="true" type="REQUIRE" filename="projects/<exe-name>/MessageTests.cpp" >
<Original> <Original>
i &lt; 10 i &lt; 10
@ -6825,6 +6885,12 @@ re>"
7 &lt; 10 7 &lt; 10
</Expanded> </Expanded>
</Expression> </Expression>
<Info>
current counter 8
</Info>
<Info>
i := 8
</Info>
<Expression success="true" type="REQUIRE" filename="projects/<exe-name>/MessageTests.cpp" > <Expression success="true" type="REQUIRE" filename="projects/<exe-name>/MessageTests.cpp" >
<Original> <Original>
i &lt; 10 i &lt; 10
@ -6833,6 +6899,12 @@ re>"
8 &lt; 10 8 &lt; 10
</Expanded> </Expanded>
</Expression> </Expression>
<Info>
current counter 9
</Info>
<Info>
i := 9
</Info>
<Expression success="true" type="REQUIRE" filename="projects/<exe-name>/MessageTests.cpp" > <Expression success="true" type="REQUIRE" filename="projects/<exe-name>/MessageTests.cpp" >
<Original> <Original>
i &lt; 10 i &lt; 10
@ -9111,6 +9183,9 @@ spanner <OverallResult success="true"/>
1 == 0 1 == 0
</Expanded> </Expanded>
</Expression> </Expression>
<Info>
Testing if fib[2] (2) is even
</Info>
<Expression success="true" type="CHECK" filename="projects/<exe-name>/MiscTests.cpp" > <Expression success="true" type="CHECK" filename="projects/<exe-name>/MiscTests.cpp" >
<Original> <Original>
( fib[i] % 2 ) == 0 ( fib[i] % 2 ) == 0
@ -9141,6 +9216,9 @@ spanner <OverallResult success="true"/>
1 == 0 1 == 0
</Expanded> </Expanded>
</Expression> </Expression>
<Info>
Testing if fib[5] (8) is even
</Info>
<Expression success="true" type="CHECK" filename="projects/<exe-name>/MiscTests.cpp" > <Expression success="true" type="CHECK" filename="projects/<exe-name>/MiscTests.cpp" >
<Original> <Original>
( fib[i] % 2 ) == 0 ( fib[i] % 2 ) == 0