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

summaryrefslogtreecommitdiffhomepage
path: root/src/Progmasoft/Results.cpp
blob: 0007377a7598f68324f9c0448935a38a387f1cd4 (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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
// Copyright (c) 2026 Progmasoft.
// SPDX-License-Identifier: MPL-2.0 WITH AdditionRef-Progmasoft-Exception-2.0
#include <Progmasoft/Catch3/Results.hpp>

#include <array>
#include <string_view>
#include <utility>

namespace Progmasoft::Catch3
{
    namespace
    {
        constexpr std::array<std::pair<Outcome, std::string_view>, 9>
            kOutcomeNames = {{
                {Outcome::kPassed, "passed"},
                {Outcome::kFailed, "failed"},
                {Outcome::kFailedAsExpected, "failed-as-expected"},
                {Outcome::kSkipped, "skipped"},
                {Outcome::kCrashed, "crashed"},
                {Outcome::kTimedOut, "timed-out"},
                {Outcome::kBlocked, "blocked"},
                {Outcome::kNotRun, "not-run"},
                {Outcome::kUpToDate, "up-to-date"},
            }};

        constexpr std::array<std::pair<FailureCategory, std::string_view>, 9>
            kCategoryNames = {{
                {FailureCategory::kNone, "none"},
                {FailureCategory::kAssertion, "assertion"},
                {FailureCategory::kUnexpectedException, "unexpected-exception"},
                {FailureCategory::kMissingException, "missing-exception"},
                {FailureCategory::kExplicitFailure, "explicit-failure"},
                {FailureCategory::kCrash, "crash"},
                {FailureCategory::kTimeout, "timeout"},
                {FailureCategory::kInfrastructure, "infrastructure"},
                {FailureCategory::kDependency, "dependency"},
            }};
    } // namespace

    std::int64_t
    TestResult::DurationMicroseconds() const noexcept
    {
        std::int64_t total = 0;
        for (const auto &attempt : attempts)
        {
            total += attempt.durationMicroseconds;
        }
        return total;
    }

    bool
    TestResult::FailsRun() const noexcept
    {
        if (quarantined)
        {
            return false;
        }
        switch (outcome)
        {
            case Outcome::kFailed:
            case Outcome::kCrashed:
            case Outcome::kTimedOut:
            case Outcome::kBlocked:
                return true;
            default:
                return false;
        }
    }

    RunSummary
    Summarize(const RunResult &run)
    {
        RunSummary summary;
        summary.tests = run.tests.size();
        summary.wallMicroseconds = run.wallMicroseconds;
        for (const auto &test : run.tests)
        {
            switch (test.outcome)
            {
                case Outcome::kPassed:
                    ++summary.passed;
                    break;
                case Outcome::kFailed:
                    ++summary.failed;
                    break;
                case Outcome::kFailedAsExpected:
                    ++summary.failedAsExpected;
                    break;
                case Outcome::kSkipped:
                    ++summary.skipped;
                    break;
                case Outcome::kCrashed:
                    ++summary.crashed;
                    break;
                case Outcome::kTimedOut:
                    ++summary.timedOut;
                    break;
                case Outcome::kBlocked:
                    ++summary.blocked;
                    break;
                case Outcome::kNotRun:
                    ++summary.notRun;
                    break;
                case Outcome::kUpToDate:
                    ++summary.upToDate;
                    break;
            }
            summary.flaky += test.flaky ? 1 : 0;
            summary.quarantined += test.quarantined ? 1 : 0;
            summary.attempts += test.attempts.size();
            for (const auto &attempt : test.attempts)
            {
                summary.assertionsPassed += attempt.assertionsPassed;
                summary.assertionsFailed += attempt.assertionsFailed;
                summary.testMicroseconds += attempt.durationMicroseconds;
            }
        }
        return summary;
    }

    const char *
    OutcomeName(Outcome outcome) noexcept
    {
        for (const auto &[value, name] : kOutcomeNames)
        {
            if (value == outcome)
            {
                return name.data();
            }
        }
        return "unknown";
    }

    std::optional<Outcome>
    OutcomeFromName(const std::string &name) noexcept
    {
        for (const auto &[value, text] : kOutcomeNames)
        {
            if (text == name)
            {
                return value;
            }
        }
        return std::nullopt;
    }

    const char *
    CategoryName(FailureCategory category) noexcept
    {
        for (const auto &[value, name] : kCategoryNames)
        {
            if (value == category)
            {
                return name.data();
            }
        }
        return "unknown";
    }

    std::optional<FailureCategory>
    CategoryFromName(const std::string &name) noexcept
    {
        for (const auto &[value, text] : kCategoryNames)
        {
            if (text == name)
            {
                return value;
            }
        }
        return std::nullopt;
    }

    const char *
    HistoryName(FailureHistory history) noexcept
    {
        switch (history)
        {
            case FailureHistory::kUnknown:
                return "unknown";
            case FailureHistory::kNew:
                return "new";
            case FailureHistory::kPersistent:
                return "persistent";
            case FailureHistory::kIntermittent:
                return "intermittent";
        }
        return "unknown";
    }

    namespace
    {
        void
        AppendIndented(std::string &text, const std::string &block)
        {
            std::size_t start = 0;
            while (start <= block.size())
            {
                const auto end = block.find('\n', start);
                text += "  ";
                text += block.substr(start,
                                     end == std::string::npos ? std::string::npos
                                                              : end - start);
                text += '\n';
                if (end == std::string::npos)
                {
                    break;
                }
                start = end + 1;
            }
        }

        std::string
        JoinSections(const std::vector<std::string> &sections)
        {
            std::string text;
            for (const auto &section : sections)
            {
                text += text.empty() ? "" : " / ";
                text += section;
            }
            return text;
        }

        std::string
        Seconds(std::int64_t microseconds)
        {
            const auto tenths = (microseconds + 50000) / 100000;
            return std::to_string(tenths / 10) + "." +
                   std::to_string(tenths % 10) + " s";
        }
    } // namespace

    std::string
    DescribeFailure(const Attempt &attempt)
    {
        std::string text;
        if (attempt.outcome == Outcome::kTimedOut)
        {
            text += "timed out after " + Seconds(attempt.durationMicroseconds);
            text += "\n";
        }
        else if (attempt.outcome == Outcome::kCrashed)
        {
            text += "crashed";
            if (!attempt.exitDescription.empty())
            {
                text += " with " + attempt.exitDescription;
            }
            text += "\n";
            if (!attempt.fatalMessage.empty())
            {
                AppendIndented(text, attempt.fatalMessage);
            }
        }
        if ((attempt.outcome == Outcome::kTimedOut ||
             attempt.outcome == Outcome::kCrashed) &&
            !attempt.lastLocation.empty())
        {
            text += "last assertion started at " + attempt.lastLocation + "\n";
        }
        if ((attempt.outcome == Outcome::kTimedOut ||
             attempt.outcome == Outcome::kCrashed))
        {
            // The innermost section that had not ended is where it stopped.
            std::vector<std::string> open;
            for (const auto &section : attempt.sections)
            {
                if (section.durationMicroseconds < 0)
                {
                    open.resize(section.depth);
                    open.push_back(section.name);
                }
            }
            if (!open.empty())
            {
                text += "in section " + JoinSections(open) + "\n";
            }
        }
        for (const auto &assertion : attempt.assertions)
        {
            if (assertion.passed)
            {
                continue;
            }
            text += assertion.file + ":" + std::to_string(assertion.line) +
                    ": " + (assertion.allowed ? "FAILED (allowed)" : "FAILED") +
                    ":\n";
            if (!assertion.expression.empty())
            {
                AppendIndented(text,
                               assertion.macro.empty()
                                   ? assertion.expression
                                   : assertion.macro + "( " +
                                         assertion.expression + " )");
            }
            if (!assertion.expansion.empty() &&
                assertion.expansion != assertion.expression)
            {
                text += "with expansion:\n";
                AppendIndented(text, assertion.expansion);
            }
            if (!assertion.message.empty())
            {
                text += assertion.category ==
                                FailureCategory::kUnexpectedException
                            ? "due to unexpected exception with message:\n"
                            : "with message:\n";
                AppendIndented(text, assertion.message);
            }
            for (const auto &context : assertion.context)
            {
                text += "with context:\n";
                AppendIndented(text, context);
            }
            if (!assertion.sections.empty())
            {
                text += "in section " + JoinSections(assertion.sections) + "\n";
            }
        }
        return text;
    }

    std::string
    SummarizeFailure(const Attempt &attempt)
    {
        const std::string text = DescribeFailure(attempt);
        return text.substr(0, text.find('\n'));
    }

    bool
    IsFailure(Outcome outcome) noexcept
    {
        return outcome == Outcome::kFailed || outcome == Outcome::kCrashed ||
               outcome == Outcome::kTimedOut;
    }
} // namespace Progmasoft::Catch3