/
catch_reporter_listening.cpp
188 lines (157 loc) · 6.5 KB
/
catch_reporter_listening.cpp
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
// Copyright Catch2 Authors
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// https://www.boost.org/LICENSE_1_0.txt)
// SPDX-License-Identifier: BSL-1.0
#include <catch2/reporters/catch_reporter_listening.hpp>
#include <catch2/catch_config.hpp>
#include <catch2/internal/catch_move_and_forward.hpp>
#include <catch2/internal/catch_stream.hpp>
#include <cassert>
namespace Catch {
void ListeningReporter::updatePreferences(IStreamingReporter const& reporterish) {
m_preferences.shouldRedirectStdOut |=
reporterish.getPreferences().shouldRedirectStdOut;
m_preferences.shouldReportAllAssertions |=
reporterish.getPreferences().shouldReportAllAssertions;
}
void ListeningReporter::addListener( IStreamingReporterPtr&& listener ) {
updatePreferences(*listener);
m_listeners.insert(m_listeners.begin() + m_insertedListeners, CATCH_MOVE(listener) );
++m_insertedListeners;
}
void ListeningReporter::addReporter( IStreamingReporterPtr&& reporter ) {
updatePreferences(*reporter);
// We will need to output the captured stdout if there are reporters
// that do not want it captured.
// TODO: Should this also apply to listeners?
m_haveNoncapturingListeners |= !reporter->getPreferences().shouldRedirectStdOut;
// Reporters can always be placed to the back without breaking the
// reporting order
m_listeners.push_back( CATCH_MOVE( reporter ) );
}
void ListeningReporter::noMatchingTestCases( StringRef unmatchedSpec ) {
for ( auto& listener : m_listeners ) {
listener->noMatchingTestCases( unmatchedSpec );
}
}
void ListeningReporter::fatalErrorEncountered( StringRef error ) {
for ( auto& listener : m_listeners ) {
listener->fatalErrorEncountered( error );
}
}
void ListeningReporter::reportInvalidArguments( StringRef arg ) {
for ( auto& listener : m_listeners ) {
listener->reportInvalidArguments( arg );
}
}
void ListeningReporter::benchmarkPreparing( StringRef name ) {
for (auto& listener : m_listeners) {
listener->benchmarkPreparing(name);
}
}
void ListeningReporter::benchmarkStarting( BenchmarkInfo const& benchmarkInfo ) {
for ( auto& listener : m_listeners ) {
listener->benchmarkStarting( benchmarkInfo );
}
}
void ListeningReporter::benchmarkEnded( BenchmarkStats<> const& benchmarkStats ) {
for ( auto& listener : m_listeners ) {
listener->benchmarkEnded( benchmarkStats );
}
}
void ListeningReporter::benchmarkFailed( StringRef error ) {
for (auto& listener : m_listeners) {
listener->benchmarkFailed(error);
}
}
void ListeningReporter::testRunStarting( TestRunInfo const& testRunInfo ) {
for ( auto& listener : m_listeners ) {
listener->testRunStarting( testRunInfo );
}
}
void ListeningReporter::testCaseStarting( TestCaseInfo const& testInfo ) {
for ( auto& listener : m_listeners ) {
listener->testCaseStarting( testInfo );
}
}
void
ListeningReporter::testCasePartialStarting( TestCaseInfo const& testInfo,
uint64_t partNumber ) {
for ( auto& listener : m_listeners ) {
listener->testCasePartialStarting( testInfo, partNumber );
}
}
void ListeningReporter::sectionStarting( SectionInfo const& sectionInfo ) {
for ( auto& listener : m_listeners ) {
listener->sectionStarting( sectionInfo );
}
}
void ListeningReporter::assertionStarting( AssertionInfo const& assertionInfo ) {
for ( auto& listener : m_listeners ) {
listener->assertionStarting( assertionInfo );
}
}
// The return value indicates if the messages buffer should be cleared:
void ListeningReporter::assertionEnded( AssertionStats const& assertionStats ) {
const bool alwaysReport =
assertionStats.assertionResult.getResultType() != ResultWas::Ok ||
m_config->includeSuccessfulResults();
for ( auto & listener : m_listeners ) {
if ( alwaysReport ||
listener->getPreferences().shouldReportAllAssertions ) {
listener->assertionEnded( assertionStats );
}
}
}
void ListeningReporter::sectionEnded( SectionStats const& sectionStats ) {
for ( auto& listener : m_listeners ) {
listener->sectionEnded( sectionStats );
}
}
void ListeningReporter::testCasePartialEnded( TestCaseStats const& testStats,
uint64_t partNumber ) {
// TODO: Fix handling of stderr/stdout?
for ( auto& listener : m_listeners ) {
listener->testCasePartialEnded( testStats, partNumber );
}
}
void ListeningReporter::testCaseEnded( TestCaseStats const& testCaseStats ) {
if ( m_preferences.shouldRedirectStdOut && m_haveNoncapturingListeners ) {
if ( !testCaseStats.stdOut.empty() ) {
Catch::cout() << testCaseStats.stdOut << std::flush;
}
if ( !testCaseStats.stdErr.empty() ) {
Catch::cerr() << testCaseStats.stdErr << std::flush;
}
}
for ( auto& listener : m_listeners ) {
listener->testCaseEnded( testCaseStats );
}
}
void ListeningReporter::testRunEnded( TestRunStats const& testRunStats ) {
for ( auto& listener : m_listeners ) {
listener->testRunEnded( testRunStats );
}
}
void ListeningReporter::skipTest( TestCaseInfo const& testInfo ) {
for ( auto& listener : m_listeners ) {
listener->skipTest( testInfo );
}
}
void ListeningReporter::listReporters(std::vector<ReporterDescription> const& descriptions) {
for (auto& listener : m_listeners) {
listener->listReporters(descriptions);
}
}
void ListeningReporter::listTests(std::vector<TestCaseHandle> const& tests) {
for (auto& listener : m_listeners) {
listener->listTests(tests);
}
}
void ListeningReporter::listTags(std::vector<TagInfo> const& tags) {
for (auto& listener : m_listeners) {
listener->listTags(tags);
}
}
} // end namespace Catch