Read-only mirrors. Development, issues and pull requests are on GitHub.

summaryrefslogtreecommitdiffhomepage
path: root/src/Progmasoft/XmlWriter.cpp
blob: b3a5f9b14e152aaebb34bcb7edf2537761c44f86 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
// Copyright (c) 2026 Progmasoft.
// SPDX-License-Identifier: MPL-2.0 WITH AdditionRef-Progmasoft-Exception-1.1
#include <Progmasoft/Catch3/Results.hpp>
#include <Progmasoft/Catch3/XmlWriter.hpp>

#include <cmath>
#include <iomanip>
#include <locale>
#include <ostream>
#include <sstream>
#include <stdexcept>
#include <string_view>

namespace Progmasoft::Catch3 {

    namespace {

        struct DecodedCharacter final {
            char32_t Value = 0;
            std::size_t Length = 0;
        };

        DecodedCharacter DecodeUtf8( std::string_view text,
                                     std::size_t offset ) noexcept {
            const auto first = static_cast<unsigned char>( text[offset] );
            if ( first < 0x80 ) { return { first, 1 }; }

            std::size_t length = 0;
            char32_t value = 0;
            char32_t minimum = 0;
            if ( first >= 0xC2 && first <= 0xDF ) {
                length = 2;
                value = first & 0x1F;
                minimum = 0x80;
            } else if ( first >= 0xE0 && first <= 0xEF ) {
                length = 3;
                value = first & 0x0F;
                minimum = 0x800;
            } else if ( first >= 0xF0 && first <= 0xF4 ) {
                length = 4;
                value = first & 0x07;
                minimum = 0x10000;
            } else {
                return { 0xFFFD, 0 };
            }

            if ( offset + length > text.size() ) { return { 0xFFFD, 0 }; }
            for ( std::size_t index = 1; index < length; ++index ) {
                const auto continuation =
                    static_cast<unsigned char>( text[offset + index] );
                if ( ( continuation & 0xC0 ) != 0x80 ) { return { 0xFFFD, 0 }; }
                value = static_cast<char32_t>( ( value << 6 ) |
                                               ( continuation & 0x3F ) );
            }
            if ( value < minimum || value > 0x10FFFF ||
                 ( value >= 0xD800 && value <= 0xDFFF ) ) {
                return { 0xFFFD, 0 };
            }
            return { value, length };
        }

        bool IsXmlCharacter( char32_t value ) noexcept {
            return value == 0x09 || value == 0x0A || value == 0x0D ||
                   ( value >= 0x20 && value <= 0xD7FF ) ||
                   ( value >= 0xE000 && value <= 0xFFFD ) ||
                   ( value >= 0x10000 && value <= 0x10FFFF );
        }

        void WriteEscaped( std::string_view value,
                           std::ostream& output,
                           bool attribute ) {
            for ( std::size_t index = 0; index < value.size(); ) {
                const DecodedCharacter character = DecodeUtf8( value, index );
                if ( character.Length == 0 ||
                     !IsXmlCharacter( character.Value ) ) {
                    // Preserve parseability without silently dropping input.
                    output << "&#xFFFD;";
                    ++index;
                    continue;
                }

                if ( character.Value < 0x80 ) {
                    switch ( static_cast<char>( character.Value ) ) {
                    case '&':
                        output << "&amp;";
                        break;
                    case '<':
                        output << "&lt;";
                        break;
                    case '>':
                        output << "&gt;";
                        break;
                    case '"':
                        if ( attribute ) {
                            output << "&quot;";
                        } else {
                            output.put( '"' );
                        }
                        break;
                    case '\'':
                        if ( attribute ) {
                            output << "&apos;";
                        } else {
                            output.put( '\'' );
                        }
                        break;
                    default:
                        output.put( static_cast<char>( character.Value ) );
                    }
                } else {
                    output.write(
                        value.data() + index,
                        static_cast<std::streamsize>( character.Length ) );
                }
                index += character.Length;
            }
        }

        void WriteAttribute( std::ostream& output,
                             std::string_view name,
                             std::string_view value ) {
            output.put( ' ' );
            output.write( name.data(),
                          static_cast<std::streamsize>( name.size() ) );
            output << "=\"";
            WriteEscaped( value, output, true );
            output.put( '"' );
        }

        std::string FormatSeconds( double seconds ) {
            std::ostringstream formatted;
            formatted.imbue( std::locale::classic() );
            formatted << std::fixed << std::setprecision( 6 ) << seconds;
            return formatted.str();
        }

        void WriteIndent( std::ostream& output, unsigned int level ) {
            for ( unsigned int index = 0; index < level; ++index ) {
                output << "  ";
            }
        }

        void WriteTextElement( std::ostream& output,
                               unsigned int indent,
                               std::string_view name,
                               std::string_view value ) {
            WriteIndent( output, indent );
            output.put( '<' );
            output.write( name.data(),
                          static_cast<std::streamsize>( name.size() ) );
            output.put( '>' );
            WriteEscaped( value, output, false );
            output << "</";
            output.write( name.data(),
                          static_cast<std::streamsize>( name.size() ) );
            output << ">\n";
        }

        TestRunSummary SummarizeSuite( const TestSuiteResult& suite ) {
            return SummarizeTestSuite( suite );
        }

        void WriteTestCase( const TestCaseResult& testCase,
                            std::string_view suiteName,
                            std::ostream& output ) {
            const bool hasBody = testCase.Status != TestCaseStatus::kPassed ||
                                 !testCase.StandardOutput.empty() ||
                                 !testCase.StandardError.empty();
            WriteIndent( output, 2 );
            output << "<testcase";
            WriteAttribute( output,
                            "classname",
                            testCase.ClassName.empty() ? suiteName
                                                       : testCase.ClassName );
            WriteAttribute( output, "name", testCase.Name );
            WriteAttribute(
                output, "time", FormatSeconds( testCase.DurationSeconds ) );
            if ( !testCase.SourceFile.empty() ) {
                WriteAttribute( output, "file", testCase.SourceFile );
            }
            if ( !hasBody ) {
                output << "/>\n";
                return;
            }

            output << ">\n";
            if ( testCase.Status == TestCaseStatus::kFailed ||
                 testCase.Status == TestCaseStatus::kErrored ) {
                const bool isError =
                    testCase.Status == TestCaseStatus::kErrored;
                WriteIndent( output, 3 );
                output << ( isError ? "<error" : "<failure" );
                WriteAttribute( output, "message", testCase.Message );
                WriteAttribute( output,
                                "type",
                                isError ? "TestError" : "AssertionFailure" );
                output.put( '>' );
                WriteEscaped( testCase.Details.empty() ? testCase.Message
                                                       : testCase.Details,
                              output,
                              false );
                output << ( isError ? "</error>\n" : "</failure>\n" );
            } else if ( testCase.Status == TestCaseStatus::kSkipped ) {
                WriteIndent( output, 3 );
                output << "<skipped";
                WriteAttribute( output, "message", testCase.Message );
                output << "/>\n";
            }
            if ( !testCase.StandardOutput.empty() ) {
                WriteTextElement(
                    output, 3, "system-out", testCase.StandardOutput );
            }
            if ( !testCase.StandardError.empty() ) {
                WriteTextElement(
                    output, 3, "system-err", testCase.StandardError );
            }
            WriteIndent( output, 2 );
            output << "</testcase>\n";
        }

    } // namespace

    void JUnitXmlWriter::Write( const TestRunResult& result,
                                std::ostream& output ) {
        const TestRunSummary runSummary = SummarizeTestRun( result );
        output << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<testsuites";
        WriteAttribute( output, "name", result.Name );
        WriteAttribute( output, "tests", std::to_string( runSummary.Tests ) );
        WriteAttribute(
            output, "failures", std::to_string( runSummary.Failures ) );
        WriteAttribute( output, "errors", std::to_string( runSummary.Errors ) );
        WriteAttribute(
            output, "skipped", std::to_string( runSummary.Skipped ) );
        WriteAttribute(
            output, "time", FormatSeconds( runSummary.DurationSeconds ) );
        output << ">\n";

        for ( const TestSuiteResult& suite : result.TestSuites ) {
            const TestRunSummary suiteSummary = SummarizeSuite( suite );
            WriteIndent( output, 1 );
            output << "<testsuite";
            WriteAttribute( output, "name", suite.Name );
            WriteAttribute(
                output, "tests", std::to_string( suiteSummary.Tests ) );
            WriteAttribute(
                output, "failures", std::to_string( suiteSummary.Failures ) );
            WriteAttribute(
                output, "errors", std::to_string( suiteSummary.Errors ) );
            WriteAttribute(
                output, "skipped", std::to_string( suiteSummary.Skipped ) );
            WriteAttribute(
                output, "time", FormatSeconds( suiteSummary.DurationSeconds ) );
            output << ">\n";

            for ( const TestCaseResult& testCase : suite.TestCases ) {
                WriteTestCase( testCase, suite.Name, output );
            }
            WriteIndent( output, 1 );
            output << "</testsuite>\n";
        }
        output << "</testsuites>\n";

        if ( !output ) {
            throw std::ios_base::failure( "could not write JUnit XML results" );
        }
    }

    std::string JUnitXmlWriter::ToString( const TestRunResult& result ) {
        std::ostringstream output;
        Write( result, output );
        return output.str();
    }

} // namespace Progmasoft::Catch3