Debian Codegraph @

3rdparty/catch2/examples/210-Evt-EventListeners.cpp

raw symbols SCIP
1 // 210-Evt-EventListeners.cpp
2
3 // Contents:
4 // 1. Printing of listener data
5 // 2. My listener and registration
6 // 3. Test cases
7
8 #include <catch2/catch_test_macros.hpp>
9 #include <catch2/reporters/catch_reporter_event_listener.hpp>
10 #include <catch2/reporters/catch_reporter_registrars.hpp>
11 #include <catch2/catch_test_case_info.hpp>
12 #include <iostream>
13
14 // -----------------------------------------------------------------------
15 // 1. Printing of listener data:
16 //
17
18
19 namespace {
20 std::string ws(int const level) {
21 return std::string( 2 * level, ' ' );
22 }
23
24 std::ostream& operator<<(std::ostream& out, Catch::Tag t) {
25 return out << "original: " << t.original;
26 }
27
28 template< typename T >
29 std::ostream& operator<<( std::ostream& os, std::vector<T> const& v ) {
30 os << "{ ";
31 for ( const auto& x : v )
32 os << x << ", ";
33 return os << "}";
34 }
35 // struct SourceLineInfo {
36 // char const* file;
37 // std::size_t line;
38 // };
39
40 void print( std::ostream& os, int const level, std::string const& title, Catch::SourceLineInfo const& info ) {
41 os << ws(level ) << title << ":\n"
42 << ws(level+1) << "- file: " << info.file << "\n"
43 << ws(level+1) << "- line: " << info.line << "\n";
44 }
45
46 //struct MessageInfo {
47 // std::string macroName;
48 // std::string message;
49 // SourceLineInfo lineInfo;
50 // ResultWas::OfType type;
51 // unsigned int sequence;
52 //};
53
54 void print( std::ostream& os, int const level, Catch::MessageInfo const& info ) {
55 os << ws(level+1) << "- macroName: '" << info.macroName << "'\n"
56 << ws(level+1) << "- message '" << info.message << "'\n";
57 print( os,level+1 , "- lineInfo", info.lineInfo );
58 os << ws(level+1) << "- sequence " << info.sequence << "\n";
59 }
60
61 void print( std::ostream& os, int const level, std::string const& title, std::vector<Catch::MessageInfo> const& v ) {
62 os << ws(level ) << title << ":\n";
63 for ( const auto& x : v )
64 {
65 os << ws(level+1) << "{\n";
66 print( os, level+2, x );
67 os << ws(level+1) << "}\n";
68 }
69 // os << ws(level+1) << "\n";
70 }
71
72 // struct TestRunInfo {
73 // std::string name;
74 // };
75
76 void print( std::ostream& os, int const level, std::string const& title, Catch::TestRunInfo const& info ) {
77 os << ws(level ) << title << ":\n"
78 << ws(level+1) << "- name: " << info.name << "\n";
79 }
80
81 // struct Counts {
82 // std::size_t total() const;
83 // bool allPassed() const;
84 // bool allOk() const;
85 //
86 // std::size_t passed = 0;
87 // std::size_t failed = 0;
88 // std::size_t failedButOk = 0;
89 // };
90
91 void print( std::ostream& os, int const level, std::string const& title, Catch::Counts const& info ) {
92 os << ws(level ) << title << ":\n"
93 << ws(level+1) << "- total(): " << info.total() << "\n"
94 << ws(level+1) << "- allPassed(): " << info.allPassed() << "\n"
95 << ws(level+1) << "- allOk(): " << info.allOk() << "\n"
96 << ws(level+1) << "- passed: " << info.passed << "\n"
97 << ws(level+1) << "- failed: " << info.failed << "\n"
98 << ws(level+1) << "- failedButOk: " << info.failedButOk << "\n";
99 }
100
101 // struct Totals {
102 // Counts assertions;
103 // Counts testCases;
104 // };
105
106 void print( std::ostream& os, int const level, std::string const& title, Catch::Totals const& info ) {
107 os << ws(level) << title << ":\n";
108 print( os, level+1, "- assertions", info.assertions );
109 print( os, level+1, "- testCases" , info.testCases );
110 }
111
112 // struct TestRunStats {
113 // TestRunInfo runInfo;
114 // Totals totals;
115 // bool aborting;
116 // };
117
118 void print( std::ostream& os, int const level, std::string const& title, Catch::TestRunStats const& info ) {
119 os << ws(level) << title << ":\n";
120 print( os, level+1 , "- runInfo", info.runInfo );
121 print( os, level+1 , "- totals" , info.totals );
122 os << ws(level+1) << "- aborting: " << info.aborting << "\n";
123 }
124
125 // struct Tag {
126 // StringRef original, lowerCased;
127 // };
128 //
129 //
130 // enum class TestCaseProperties : uint8_t {
131 // None = 0,
132 // IsHidden = 1 << 1,
133 // ShouldFail = 1 << 2,
134 // MayFail = 1 << 3,
135 // Throws = 1 << 4,
136 // NonPortable = 1 << 5,
137 // Benchmark = 1 << 6
138 // };
139 //
140 //
141 // struct TestCaseInfo : NonCopyable {
142 //
143 // bool isHidden() const;
144 // bool throws() const;
145 // bool okToFail() const;
146 // bool expectedToFail() const;
147 //
148 //
149 // std::string name;
150 // std::string className;
151 // std::vector<Tag> tags;
152 // SourceLineInfo lineInfo;
153 // TestCaseProperties properties = TestCaseProperties::None;
154 // };
155
156 void print( std::ostream& os, int const level, std::string const& title, Catch::TestCaseInfo const& info ) {
157 os << ws(level ) << title << ":\n"
158 << ws(level+1) << "- isHidden(): " << info.isHidden() << "\n"
159 << ws(level+1) << "- throws(): " << info.throws() << "\n"
160 << ws(level+1) << "- okToFail(): " << info.okToFail() << "\n"
161 << ws(level+1) << "- expectedToFail(): " << info.expectedToFail() << "\n"
162 << ws(level+1) << "- tagsAsString(): '" << info.tagsAsString() << "'\n"
163 << ws(level+1) << "- name: '" << info.name << "'\n"
164 << ws(level+1) << "- className: '" << info.className << "'\n"
165 << ws(level+1) << "- tags: " << info.tags << "\n";
166 print( os, level+1 , "- lineInfo", info.lineInfo );
167 os << ws(level+1) << "- properties (flags): 0x" << std::hex << static_cast<uint32_t>(info.properties) << std::dec << "\n";
168 }
169
170 // struct TestCaseStats {
171 // TestCaseInfo testInfo;
172 // Totals totals;
173 // std::string stdOut;
174 // std::string stdErr;
175 // bool aborting;
176 // };
177
178 void print( std::ostream& os, int const level, std::string const& title, Catch::TestCaseStats const& info ) {
179 os << ws(level ) << title << ":\n";
180 print( os, level+1 , "- testInfo", *info.testInfo );
181 print( os, level+1 , "- totals" , info.totals );
182 os << ws(level+1) << "- stdOut: " << info.stdOut << "\n"
183 << ws(level+1) << "- stdErr: " << info.stdErr << "\n"
184 << ws(level+1) << "- aborting: " << info.aborting << "\n";
185 }
186
187 // struct SectionInfo {
188 // std::string name;
189 // std::string description;
190 // SourceLineInfo lineInfo;
191 // };
192
193 void print( std::ostream& os, int const level, std::string const& title, Catch::SectionInfo const& info ) {
194 os << ws(level ) << title << ":\n"
195 << ws(level+1) << "- name: " << info.name << "\n";
196 print( os, level+1 , "- lineInfo", info.lineInfo );
197 }
198
199 // struct SectionStats {
200 // SectionInfo sectionInfo;
201 // Counts assertions;
202 // double durationInSeconds;
203 // bool missingAssertions;
204 // };
205
206 void print( std::ostream& os, int const level, std::string const& title, Catch::SectionStats const& info ) {
207 os << ws(level ) << title << ":\n";
208 print( os, level+1 , "- sectionInfo", info.sectionInfo );
209 print( os, level+1 , "- assertions" , info.assertions );
210 os << ws(level+1) << "- durationInSeconds: " << info.durationInSeconds << "\n"
211 << ws(level+1) << "- missingAssertions: " << info.missingAssertions << "\n";
212 }
213
214 // struct AssertionInfo
215 // {
216 // StringRef macroName;
217 // SourceLineInfo lineInfo;
218 // StringRef capturedExpression;
219 // ResultDisposition::Flags resultDisposition;
220 // };
221
222 void print( std::ostream& os, int const level, std::string const& title, Catch::AssertionInfo const& info ) {
223 os << ws(level ) << title << ":\n"
224 << ws(level+1) << "- macroName: '" << info.macroName << "'\n";
225 print( os, level+1 , "- lineInfo" , info.lineInfo );
226 os << ws(level+1) << "- capturedExpression: '" << info.capturedExpression << "'\n"
227 << ws(level+1) << "- resultDisposition (flags): 0x" << std::hex << info.resultDisposition << std::dec << "\n";
228 }
229
230 //struct AssertionResultData
231 //{
232 // std::string reconstructExpression() const;
233 //
234 // std::string message;
235 // mutable std::string reconstructedExpression;
236 // LazyExpression lazyExpression;
237 // ResultWas::OfType resultType;
238 //};
239
240 void print( std::ostream& os, int const level, std::string const& title, Catch::AssertionResultData const& info ) {
241 os << ws(level ) << title << ":\n"
242 << ws(level+1) << "- reconstructExpression(): '" << info.reconstructExpression() << "'\n"
243 << ws(level+1) << "- message: '" << info.message << "'\n"
244 << ws(level+1) << "- lazyExpression: '" << "(info.lazyExpression)" << "'\n"
245 << ws(level+1) << "- resultType: '" << info.resultType << "'\n";
246 }
247
248 //class AssertionResult {
249 // bool isOk() const;
250 // bool succeeded() const;
251 // ResultWas::OfType getResultType() const;
252 // bool hasExpression() const;
253 // bool hasMessage() const;
254 // std::string getExpression() const;
255 // std::string getExpressionInMacro() const;
256 // bool hasExpandedExpression() const;
257 // std::string getExpandedExpression() const;
258 // std::string getMessage() const;
259 // SourceLineInfo getSourceInfo() const;
260 // std::string getTestMacroName() const;
261 //
262 // AssertionInfo m_info;
263 // AssertionResultData m_resultData;
264 //};
265
266 void print( std::ostream& os, int const level, std::string const& title, Catch::AssertionResult const& info ) {
267 os << ws(level ) << title << ":\n"
268 << ws(level+1) << "- isOk(): " << info.isOk() << "\n"
269 << ws(level+1) << "- succeeded(): " << info.succeeded() << "\n"
270 << ws(level+1) << "- getResultType(): " << info.getResultType() << "\n"
271 << ws(level+1) << "- hasExpression(): " << info.hasExpression() << "\n"
272 << ws(level+1) << "- hasMessage(): " << info.hasMessage() << "\n"
273 << ws(level+1) << "- getExpression(): '" << info.getExpression() << "'\n"
274 << ws(level+1) << "- getExpressionInMacro(): '" << info.getExpressionInMacro() << "'\n"
275 << ws(level+1) << "- hasExpandedExpression(): " << info.hasExpandedExpression() << "\n"
276 << ws(level+1) << "- getExpandedExpression(): " << info.getExpandedExpression() << "'\n"
277 << ws(level+1) << "- getMessage(): '" << info.getMessage() << "'\n";
278 print( os, level+1 , "- getSourceInfo(): ", info.getSourceInfo() );
279 os << ws(level+1) << "- getTestMacroName(): '" << info.getTestMacroName() << "'\n";
280
281 print( os, level+1 , "- *** m_info (AssertionInfo)", info.m_info );
282 print( os, level+1 , "- *** m_resultData (AssertionResultData)", info.m_resultData );
283 }
284
285 // struct AssertionStats {
286 // AssertionResult assertionResult;
287 // std::vector<MessageInfo> infoMessages;
288 // Totals totals;
289 // };
290
291 void print( std::ostream& os, int const level, std::string const& title, Catch::AssertionStats const& info ) {
292 os << ws(level ) << title << ":\n";
293 print( os, level+1 , "- assertionResult", info.assertionResult );
294 print( os, level+1 , "- infoMessages", info.infoMessages );
295 print( os, level+1 , "- totals", info.totals );
296 }
297
298 // -----------------------------------------------------------------------
299 // 2. My listener and registration:
300 //
301
302 char const * dashed_line =
303 "--------------------------------------------------------------------------";
304
305
306 struct MyListener : Catch::EventListenerBase {
307
308 using EventListenerBase::EventListenerBase; // inherit constructor
309
310 // Get rid of Wweak-tables
311 ~MyListener() override;
312
313 // The whole test run starting
314 void testRunStarting( Catch::TestRunInfo const& testRunInfo ) override {
315 std::cout
316 << std::boolalpha
317 << "\nEvent: testRunStarting:\n";
318 print( std::cout, 1, "- testRunInfo", testRunInfo );
319 }
320
321 // The whole test run ending
322 void testRunEnded( Catch::TestRunStats const& testRunStats ) override {
323 std::cout
324 << dashed_line
325 << "\nEvent: testRunEnded:\n";
326 print( std::cout, 1, "- testRunStats", testRunStats );
327 }
328
329 // A test is being skipped (because it is "hidden")
330 void skipTest( Catch::TestCaseInfo const& testInfo ) override {
331 std::cout
332 << dashed_line
333 << "\nEvent: skipTest:\n";
334 print( std::cout, 1, "- testInfo", testInfo );
335 }
336
337 // Test cases starting
338 void testCaseStarting( Catch::TestCaseInfo const& testInfo ) override {
339 std::cout
340 << dashed_line
341 << "\nEvent: testCaseStarting:\n";
342 print( std::cout, 1, "- testInfo", testInfo );
343 }
344
345 // Test cases ending
346 void testCaseEnded( Catch::TestCaseStats const& testCaseStats ) override {
347 std::cout << "\nEvent: testCaseEnded:\n";
348 print( std::cout, 1, "testCaseStats", testCaseStats );
349 }
350
351 // Sections starting
352 void sectionStarting( Catch::SectionInfo const& sectionInfo ) override {
353 std::cout << "\nEvent: sectionStarting:\n";
354 print( std::cout, 1, "- sectionInfo", sectionInfo );
355 }
356
357 // Sections ending
358 void sectionEnded( Catch::SectionStats const& sectionStats ) override {
359 std::cout << "\nEvent: sectionEnded:\n";
360 print( std::cout, 1, "- sectionStats", sectionStats );
361 }
362
363 // Assertions before/ after
364 void assertionStarting( Catch::AssertionInfo const& assertionInfo ) override {
365 std::cout << "\nEvent: assertionStarting:\n";
366 print( std::cout, 1, "- assertionInfo", assertionInfo );
367 }
368
369 void assertionEnded( Catch::AssertionStats const& assertionStats ) override {
370 std::cout << "\nEvent: assertionEnded:\n";
371 print( std::cout, 1, "- assertionStats", assertionStats );
372 }
373 };
374
375 } // end anonymous namespace
376
377 CATCH_REGISTER_LISTENER( MyListener )
378
379 // Get rid of Wweak-tables
380 MyListener::~MyListener() {}
381
382
383 // -----------------------------------------------------------------------
384 // 3. Test cases:
385 //
386
387 TEST_CASE( "1: Hidden testcase", "[.hidden]" ) {
388 }
389
390 TEST_CASE( "2: Testcase with sections", "[tag-A][tag-B]" ) {
391
392 int i = 42;
393
394 REQUIRE( i == 42 );
395
396 SECTION("Section 1") {
397 INFO("Section 1");
398 i = 7;
399 SECTION("Section 1.1") {
400 INFO("Section 1.1");
401 REQUIRE( i == 42 );
402 }
403 }
404
405 SECTION("Section 2") {
406 INFO("Section 2");
407 REQUIRE( i == 42 );
408 }
409 WARN("At end of test case");
410 }
411
412 struct Fixture {
413 int fortytwo() const {
414 return 42;
415 }
416 };
417
418 TEST_CASE_METHOD( Fixture, "3: Testcase with class-based fixture", "[tag-C][tag-D]" ) {
419 REQUIRE( fortytwo() == 42 );
420 }
421
422 // Compile & run:
423 // - g++ -std=c++14 -Wall -I$(CATCH_SINGLE_INCLUDE) -o 210-Evt-EventListeners 210-Evt-EventListeners.cpp && 210-Evt-EventListeners --success
424 // - cl -EHsc -I%CATCH_SINGLE_INCLUDE% 210-Evt-EventListeners.cpp && 210-Evt-EventListeners --success
425
426 // Expected compact output (all assertions):
427 //
428 // prompt> 210-Evt-EventListeners --reporter compact --success
429 // result omitted for brevity.