Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
catch_reporter_xml.cpp
Go to the documentation of this file.
1/*
2 * Created by Phil on 28/10/2010.
3 * Copyright 2010 Two Blue Cubes Ltd. All rights reserved.
4 *
5 * Distributed under the Boost Software License, Version 1.0. (See accompanying
6 * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 */
8
10
13
14#if defined(_MSC_VER)
15#pragma warning(push)
16#pragma warning(disable:4061) // Not all labels are EXPLICITLY handled in switch
17 // Note that 4062 (not all labels are handled
18 // and default is missing) is enabled
19#endif
20
21namespace Catch {
23 : StreamingReporterBase( _config ),
24 m_xml(_config.stream())
25 {
28 }
29
30 XmlReporter::~XmlReporter() = default;
31
33 return "Reports test results as an XML document";
34 }
35
36 std::string XmlReporter::getStylesheetRef() const {
37 return std::string();
38 }
39
40 void XmlReporter::writeSourceInfo( SourceLineInfo const& sourceInfo ) {
41 m_xml
42 .writeAttribute( "filename", sourceInfo.file )
43 .writeAttribute( "line", sourceInfo.line );
44 }
45
49
50 void XmlReporter::testRunStarting( TestRunInfo const& testInfo ) {
52 std::string stylesheetRef = getStylesheetRef();
53 if( !stylesheetRef.empty() )
54 m_xml.writeStylesheetRef( stylesheetRef );
55 m_xml.startElement( "Catch" );
56 if( !m_config->name().empty() )
57 m_xml.writeAttribute( "name", m_config->name() );
58 if (m_config->testSpec().hasFilters())
59 m_xml.writeAttribute( "filters", serializeFilters( m_config->getTestsOrTags() ) );
60 if( m_config->rngSeed() != 0 )
61 m_xml.scopedElement( "Randomness" )
62 .writeAttribute( "seed", m_config->rngSeed() );
63 }
64
65 void XmlReporter::testGroupStarting( GroupInfo const& groupInfo ) {
67 m_xml.startElement( "Group" )
68 .writeAttribute( "name", groupInfo.name );
69 }
70
73 m_xml.startElement( "TestCase" )
74 .writeAttribute( "name", trim( testInfo.name ) )
75 .writeAttribute( "description", testInfo.description )
76 .writeAttribute( "tags", testInfo.tagsAsString() );
77
78 writeSourceInfo( testInfo.lineInfo );
79
80 if ( m_config->showDurations() == ShowDurations::Always )
81 m_testCaseTimer.start();
82 m_xml.ensureTagClosed();
83 }
84
85 void XmlReporter::sectionStarting( SectionInfo const& sectionInfo ) {
87 if( m_sectionDepth++ > 0 ) {
88 m_xml.startElement( "Section" )
89 .writeAttribute( "name", trim( sectionInfo.name ) );
90 writeSourceInfo( sectionInfo.lineInfo );
91 m_xml.ensureTagClosed();
92 }
93 }
94
96
97 bool XmlReporter::assertionEnded( AssertionStats const& assertionStats ) {
98
99 AssertionResult const& result = assertionStats.assertionResult;
100
101 bool includeResults = m_config->includeSuccessfulResults() || !result.isOk();
102
103 if( includeResults || result.getResultType() == ResultWas::Warning ) {
104 // Print any info messages in <Info> tags.
105 for( auto const& msg : assertionStats.infoMessages ) {
106 if( msg.type == ResultWas::Info && includeResults ) {
107 m_xml.scopedElement( "Info" )
108 .writeText( msg.message );
109 } else if ( msg.type == ResultWas::Warning ) {
110 m_xml.scopedElement( "Warning" )
111 .writeText( msg.message );
112 }
113 }
114 }
115
116 // Drop out if result was successful but we're not printing them.
117 if( !includeResults && result.getResultType() != ResultWas::Warning )
118 return true;
119
120
121 // Print the expression if there is one.
122 if( result.hasExpression() ) {
123 m_xml.startElement( "Expression" )
124 .writeAttribute( "success", result.succeeded() )
125 .writeAttribute( "type", result.getTestMacroName() );
126
127 writeSourceInfo( result.getSourceInfo() );
128
129 m_xml.scopedElement( "Original" )
130 .writeText( result.getExpression() );
131 m_xml.scopedElement( "Expanded" )
132 .writeText( result.getExpandedExpression() );
133 }
134
135 // And... Print a result applicable to each result type.
136 switch( result.getResultType() ) {
138 m_xml.startElement( "Exception" );
139 writeSourceInfo( result.getSourceInfo() );
140 m_xml.writeText( result.getMessage() );
141 m_xml.endElement();
142 break;
144 m_xml.startElement( "FatalErrorCondition" );
145 writeSourceInfo( result.getSourceInfo() );
146 m_xml.writeText( result.getMessage() );
147 m_xml.endElement();
148 break;
149 case ResultWas::Info:
150 m_xml.scopedElement( "Info" )
151 .writeText( result.getMessage() );
152 break;
154 // Warning will already have been written
155 break;
157 m_xml.startElement( "Failure" );
158 writeSourceInfo( result.getSourceInfo() );
159 m_xml.writeText( result.getMessage() );
160 m_xml.endElement();
161 break;
162 default:
163 break;
164 }
165
166 if( result.hasExpression() )
167 m_xml.endElement();
168
169 return true;
170 }
171
172 void XmlReporter::sectionEnded( SectionStats const& sectionStats ) {
174 if( --m_sectionDepth > 0 ) {
175 XmlWriter::ScopedElement e = m_xml.scopedElement( "OverallResults" );
176 e.writeAttribute( "successes", sectionStats.assertions.passed );
177 e.writeAttribute( "failures", sectionStats.assertions.failed );
178 e.writeAttribute( "expectedFailures", sectionStats.assertions.failedButOk );
179
180 if ( m_config->showDurations() == ShowDurations::Always )
181 e.writeAttribute( "durationInSeconds", sectionStats.durationInSeconds );
182
183 m_xml.endElement();
184 }
185 }
186
187 void XmlReporter::testCaseEnded( TestCaseStats const& testCaseStats ) {
189 XmlWriter::ScopedElement e = m_xml.scopedElement( "OverallResult" );
190 e.writeAttribute( "success", testCaseStats.totals.assertions.allOk() );
191
192 if ( m_config->showDurations() == ShowDurations::Always )
193 e.writeAttribute( "durationInSeconds", m_testCaseTimer.getElapsedSeconds() );
194
195 if( !testCaseStats.stdOut.empty() )
196 m_xml.scopedElement( "StdOut" ).writeText( trim( testCaseStats.stdOut ), false );
197 if( !testCaseStats.stdErr.empty() )
198 m_xml.scopedElement( "StdErr" ).writeText( trim( testCaseStats.stdErr ), false );
199
200 m_xml.endElement();
201 }
202
203 void XmlReporter::testGroupEnded( TestGroupStats const& testGroupStats ) {
205 // TODO: Check testGroupStats.aborting and act accordingly.
206 m_xml.scopedElement( "OverallResults" )
207 .writeAttribute( "successes", testGroupStats.totals.assertions.passed )
208 .writeAttribute( "failures", testGroupStats.totals.assertions.failed )
209 .writeAttribute( "expectedFailures", testGroupStats.totals.assertions.failedButOk );
210 m_xml.endElement();
211 }
212
213 void XmlReporter::testRunEnded( TestRunStats const& testRunStats ) {
215 m_xml.scopedElement( "OverallResults" )
216 .writeAttribute( "successes", testRunStats.totals.assertions.passed )
217 .writeAttribute( "failures", testRunStats.totals.assertions.failed )
218 .writeAttribute( "expectedFailures", testRunStats.totals.assertions.failedButOk );
219 m_xml.endElement();
220 }
221
223
224} // end namespace Catch
225
226#if defined(_MSC_VER)
227#pragma warning(pop)
228#endif
#define CATCH_REGISTER_REPORTER(name, reporterType)
auto getElapsedSeconds() const -> double
~XmlReporter() override
void sectionStarting(SectionInfo const &sectionInfo) override
bool assertionEnded(AssertionStats const &assertionStats) override
void testCaseStarting(TestCaseInfo const &testInfo) override
void writeSourceInfo(SourceLineInfo const &sourceInfo)
void testRunEnded(TestRunStats const &testRunStats) override
void assertionStarting(AssertionInfo const &) override
void noMatchingTestCases(std::string const &s) override
void sectionEnded(SectionStats const &sectionStats) override
void testGroupStarting(GroupInfo const &groupInfo) override
XmlReporter(ReporterConfig const &_config)
void testRunStarting(TestRunInfo const &testInfo) override
void testCaseEnded(TestCaseStats const &testCaseStats) override
static std::string getDescription()
void testGroupEnded(TestGroupStats const &testGroupStats) override
virtual std::string getStylesheetRef() const
ScopedElement & writeText(std::string const &text, bool indent=true)
ScopedElement & writeAttribute(std::string const &name, T const &attribute)
ScopedElement scopedElement(std::string const &name)
XmlWriter & writeText(std::string const &text, bool indent=true)
XmlWriter & startElement(std::string const &name)
void writeStylesheetRef(std::string const &url)
XmlWriter & endElement()
XmlWriter & writeAttribute(std::string const &name, std::string const &attribute)
std::string trim(std::string const &str)
std::string serializeFilters(std::vector< std::string > const &container)
std::vector< MessageInfo > infoMessages
std::size_t failed
bool allOk() const
std::size_t failedButOk
std::size_t passed
SourceLineInfo lineInfo
void sectionStarting(SectionInfo const &_sectionInfo) override
void noMatchingTestCases(std::string const &) override
void testRunStarting(TestRunInfo const &_testRunInfo) override
void testGroupStarting(GroupInfo const &_groupInfo) override
void testRunEnded(TestRunStats const &) override
void sectionEnded(SectionStats const &) override
void testCaseStarting(TestCaseInfo const &_testInfo) override
void testCaseEnded(TestCaseStats const &) override
void testGroupEnded(TestGroupStats const &) override
std::string tagsAsString() const
Counts assertions
char * s