forked from catchorg/Catch2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcatch_reporter_basic.hpp
223 lines (199 loc) · 7.47 KB
/
catch_reporter_basic.hpp
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
/*
* catch_reporter_basic.hpp
* Catch
*
* Created by Phil on 28/10/2010.
* Copyright 2010 Two Blue Cubes Ltd. All rights reserved.
*
* Distributed under the Boost Software License, Version 1.0. (See accompanying
* file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
*
*/
#ifndef TWOBLUECUBES_CATCH_REPORTER_BASIC_HPP_INCLUDED
#define TWOBLUECUBES_CATCH_REPORTER_BASIC_HPP_INCLUDED
#include "internal/catch_capture.hpp"
#include "internal/catch_interfaces_reporter.h"
#include "internal/catch_reporter_registrars.hpp"
namespace Catch
{
class BasicReporter : public IReporter
{
public:
///////////////////////////////////////////////////////////////////////////
BasicReporter
(
const IReporterConfig& config
)
: m_config( config )
{
}
///////////////////////////////////////////////////////////////////////////
static std::string getDescription
()
{
return "Reports test results as lines of text";
}
private:
///////////////////////////////////////////////////////////////////////////
void ReportCounts
(
std::size_t succeeded,
std::size_t failed
)
{
if( failed + succeeded == 0 )
m_config.stream() << "No tests ran";
else if( failed == 0 )
m_config.stream() << "All " << succeeded << " test(s) succeeded";
else if( succeeded == 0 )
m_config.stream() << "All " << failed << " test(s) failed";
else
m_config.stream() << succeeded << " test(s) passed but " << failed << " test(s) failed";
}
private: // IReporter
///////////////////////////////////////////////////////////////////////////
virtual void StartTesting
()
{
m_config.stream() << "[Started testing]" << std::endl;
}
///////////////////////////////////////////////////////////////////////////
virtual void EndTesting
(
std::size_t succeeded,
std::size_t failed
)
{
m_config.stream() << "[Testing completed. ";
ReportCounts( succeeded, failed );
m_config.stream() << "]" << std::endl;
}
///////////////////////////////////////////////////////////////////////////
virtual void StartGroup
(
const std::string& groupName
)
{
if( !groupName.empty() )
m_config.stream() << "[Started group: '" << groupName << "']" << std::endl;
}
///////////////////////////////////////////////////////////////////////////
virtual void EndGroup
(
const std::string& groupName,
std::size_t succeeded,
std::size_t failed
)
{
if( !groupName.empty() )
{
m_config.stream() << "[End of group: '" << groupName << "'. ";
ReportCounts( succeeded, failed );
m_config.stream() << "]\n" << std::endl;
}
}
///////////////////////////////////////////////////////////////////////////
virtual void StartTestCase
(
const TestCaseInfo& testInfo
)
{
m_config.stream() << std::endl << "[Running: " << testInfo.getName() << "]" << std::endl;
m_firstSectionInTestCase = true;
}
///////////////////////////////////////////////////////////////////////////
virtual void StartSection
(
const std::string& sectionName,
const std::string /*description*/
)
{
if( m_firstSectionInTestCase )
{
m_config.stream() << "\n";
m_firstSectionInTestCase = false;
}
m_config.stream() << "[Started section: '" << sectionName << "']" << std::endl;
}
///////////////////////////////////////////////////////////////////////////
virtual void EndSection
(
const std::string& sectionName,
std::size_t succeeded,
std::size_t failed
)
{
m_config.stream() << "[End of section: '" << sectionName << "'. ";
ReportCounts( succeeded, failed );
m_config.stream() << "]\n" << std::endl;
}
///////////////////////////////////////////////////////////////////////////
virtual void Result
(
const ResultInfo& resultInfo
)
{
if( !m_config.includeSuccessfulResults() && resultInfo.getResultType() == ResultWas::Ok )
return;
if( !resultInfo.getFilename().empty() )
m_config.stream() << resultInfo.getFilename() << "(" << resultInfo.getLine() << "): ";
if( resultInfo.hasExpression() )
{
m_config.stream() << resultInfo.getExpression();
if( resultInfo.ok() )
m_config.stream() << " succeeded";
else
m_config.stream() << " failed";
}
switch( resultInfo.getResultType() )
{
case ResultWas::ThrewException:
if( resultInfo.hasExpression() )
m_config.stream() << " with unexpected";
else
m_config.stream() << "Unexpected";
m_config.stream() << " exception with message: '" << resultInfo.getMessage() << "'";
break;
case ResultWas::Info:
m_config.stream() << "info: '" << resultInfo.getMessage() << "'";
break;
case ResultWas::Warning:
m_config.stream() << "warning: '" << resultInfo.getMessage() << "'";
break;
case ResultWas::ExplicitFailure:
m_config.stream() << "failed with message: '" << resultInfo.getMessage() << "'";
break;
default:
break;
}
if( resultInfo.hasExpression() )
{
m_config.stream() << " for: " << resultInfo.getExpandedExpression();
}
m_config.stream() << std::endl;
}
///////////////////////////////////////////////////////////////////////////
virtual void EndTestCase
(
const TestCaseInfo& testInfo,
std::size_t succeeded,
std::size_t failed,
const std::string& stdOut,
const std::string& stdErr
)
{
if( !stdOut.empty() )
m_config.stream() << "[stdout: " << trim( stdOut ) << "]\n";
if( !stdErr.empty() )
m_config.stream() << "[stderr: " << trim( stdErr ) << "]\n";
m_config.stream() << "[Finished: " << testInfo.getName() << " ";
ReportCounts( succeeded, failed );
m_config.stream() << "]" << std::endl;
}
private:
const IReporterConfig& m_config;
bool m_firstSectionInTestCase;
};
INTERNAL_CATCH_REGISTER_REPORTER( "basic", BasicReporter );
} // end namespace Catch
#endif // TWOBLUECUBES_CATCH_REPORTER_BASIC_HPP_INCLUDED