Debian Codegraph @

src/mpt/test/test.hpp

raw
1 /* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
2
3 #ifndef MPT_TEST_TEST_HPP
4 #define MPT_TEST_TEST_HPP
5
6
7
8 #include "mpt/base/namespace.hpp"
9 #include "mpt/base/source_location.hpp"
10
11 #include <functional>
12 #include <iostream>
13 #include <map>
14 #include <sstream>
15 #include <stdexcept>
16 #include <type_traits>
17 #include <typeinfo>
18 #include <utility>
19 #include <variant>
20
21 #include <cstddef>
22 #include <cstdlib>
23
24
25
26 namespace mpt {
27 inline namespace MPT_INLINE_NS {
28
29
30
31 namespace test {
32
33
34
35 template <typename S, typename T, typename = void>
36 struct is_to_stream_writable : std::false_type { };
37
38 template <typename S, typename T>
39 struct is_to_stream_writable<S, T, std::void_t<decltype(std::declval<S &>() << std::declval<T>())>> : std::true_type { };
40
41 template <typename T>
42 inline auto format(const T & x) -> typename std::enable_if<mpt::test::is_to_stream_writable<std::ostringstream, T>::value, std::string>::type {
43 std::ostringstream s;
44 s << x;
45 return std::move(s).str();
46 }
47
48 template <typename T>
49 inline auto format(const T & x) -> typename std::enable_if<!mpt::test::is_to_stream_writable<std::ostringstream, T>::value, std::string>::type {
50 return typeid(x).name();
51 }
52
53 inline std::string get_exception_text() {
54 std::string result;
55 try {
56 // cppcheck false-positive
57 // cppcheck-suppress rethrowNoCurrentException
58 throw;
59 } catch (const std::exception & e) {
60 result = e.what();
61 } catch (...) {
62 result = "unknown exception";
63 }
64 return result;
65 }
66
67 struct result_success {
68 };
69 struct result_failure {
70 std::string text{};
71 };
72 struct result_unexpected_exception {
73 std::string text{};
74 };
75
76 struct result {
77 std::variant<std::monostate, result_success, result_failure, result_unexpected_exception> info{std::monostate{}};
78 };
79
80 struct statistics_counters {
81 std::size_t total{0};
82 std::size_t run{0};
83 std::size_t successes{0};
84 std::size_t failures{0};
85 std::size_t unexpected_exceptions{0};
86 std::size_t completed{0};
87 constexpr statistics_counters & operator+=(const statistics_counters & other) noexcept {
88 total += other.total;
89 run += other.run;
90 successes += other.successes;
91 failures += other.failures;
92 unexpected_exceptions += other.unexpected_exceptions;
93 completed += other.completed;
94 return *this;
95 }
96 };
97
98 struct group_statistics {
99 statistics_counters tests{};
100 statistics_counters cases{};
101 statistics_counters local_cases{};
102 };
103
104 struct global_statistics {
105 statistics_counters groups{};
106 statistics_counters tests{};
107 statistics_counters cases{};
108 std::map<std::string, group_statistics> individual_group_statistics{};
109 explicit constexpr operator bool() noexcept {
110 return succeeded();
111 }
112 constexpr bool operator!() noexcept {
113 return failed();
114 }
115 constexpr bool succeeded() noexcept {
116 return groups.successes == groups.run;
117 }
118 constexpr bool failed() noexcept {
119 return groups.failures > 0 || groups.unexpected_exceptions > 0;
120 }
121 };
122
123 class reporter_interface {
124 protected:
125 virtual ~reporter_interface() = default;
126
127 public:
128 virtual void run_begin(const mpt::source_location & loc) = 0;
129 virtual void group_begin(const mpt::source_location & loc, const char * name) = 0;
130 virtual void test_begin(const mpt::source_location & loc, const char * name) = 0;
131 virtual void case_run(const mpt::source_location & loc) = 0;
132 virtual void case_run(const mpt::source_location & loc, const char * text_e) = 0;
133 virtual void case_run(const mpt::source_location & loc, const char * text_ex, const char * text_e) = 0;
134 virtual void case_run(const mpt::source_location & loc, const char * text_a, const char * text_cmp, const char * text_b) = 0;
135 virtual void case_result(const mpt::source_location & loc, const mpt::test::result & result) = 0;
136 virtual void test_end(const mpt::source_location & loc, const char * name, const statistics_counters & counters) = 0;
137 virtual void group_end(const mpt::source_location & loc, const char * name, const group_statistics & statistics) = 0;
138 virtual void run_end(const mpt::source_location & loc, const global_statistics & statistics) = 0;
139 virtual void immediate_breakpoint() = 0;
140 };
141
142 class silent_reporter
143 : public reporter_interface {
144 public:
145 silent_reporter() = default;
146 ~silent_reporter() override = default;
147
148 public:
149 virtual void run_begin(const mpt::source_location &) override {
150 }
151 virtual void group_begin(const mpt::source_location &, const char *) override {
152 }
153 virtual void test_begin(const mpt::source_location &, const char *) override {
154 }
155 virtual void case_run(const mpt::source_location &) override {
156 }
157 virtual void case_run(const mpt::source_location &, const char *) override {
158 }
159 virtual void case_run(const mpt::source_location &, const char *, const char *) override {
160 }
161 virtual void case_run(const mpt::source_location &, const char *, const char *, const char *) override {
162 }
163 virtual void case_result(const mpt::source_location &, const mpt::test::result &) override {
164 }
165 virtual void test_end(const mpt::source_location &, const char *, const statistics_counters &) override {
166 }
167 virtual void group_end(const mpt::source_location &, const char *, const group_statistics &) override {
168 }
169 virtual void run_end(const mpt::source_location &, const global_statistics &) override {
170 }
171 virtual void immediate_breakpoint() override {
172 }
173 };
174
175 class simple_reporter : public reporter_interface {
176 private:
177 std::ostream & s;
178
179 public:
180 simple_reporter(std::ostream & s_)
181 : s(s_) {
182 s.flush();
183 }
184 ~simple_reporter() override {
185 s.flush();
186 }
187
188 public:
189 void run_begin(const mpt::source_location & loc) override {
190 static_cast<void>(loc);
191 s << "Running test suite ..." << std::endl;
192 }
193 void group_begin(const mpt::source_location & loc, const char * name) override {
194 static_cast<void>(loc);
195 s << "Running group '" << name << "' ..." << std::endl;
196 }
197 void test_begin(const mpt::source_location & loc, const char * name) override {
198 static_cast<void>(loc);
199 s << " Running test '" << name << "' ..." << std::endl;
200 }
201 void case_run(const mpt::source_location & loc) override {
202 static_cast<void>(loc);
203 s << " Checking ..." << std::endl;
204 }
205 void case_run(const mpt::source_location & loc, const char * text_e) override {
206 static_cast<void>(loc);
207 s << " Checking '" << text_e << "' ..." << std::endl;
208 }
209 void case_run(const mpt::source_location & loc, const char * text_ex, const char * text_e) override {
210 static_cast<void>(loc);
211 if (text_ex) {
212 s << " Checking '" << text_e << " throws " << text_ex << "' ..." << std::endl;
213 } else {
214 s << " Checking '" << text_e << " throws' ..." << std::endl;
215 }
216 }
217 void case_run(const mpt::source_location & loc, const char * text_a, const char * text_cmp, const char * text_b) override {
218 static_cast<void>(loc);
219 s << " Checking '" << text_a << " " << text_cmp << " " << text_b << "' ..." << std::endl;
220 }
221 void case_result(const mpt::source_location & loc, const mpt::test::result & result) override {
222 static_cast<void>(loc);
223 s << " Checking done: ";
224 if (std::holds_alternative<result_success>(result.info)) {
225 s << "Success.";
226 } else if (std::holds_alternative<result_failure>(result.info)) {
227 s << "FAILURE: " << std::get<result_failure>(result.info).text;
228 } else if (std::holds_alternative<result_unexpected_exception>(result.info)) {
229 s << "UNEXPECTED EXCEPTION: " << std::get<result_unexpected_exception>(result.info).text;
230 }
231 s << std::endl;
232 }
233 void test_end(const mpt::source_location & loc, const char * name, const statistics_counters & counters) override {
234 static_cast<void>(loc);
235 static_cast<void>(counters);
236 s << " Running test '" << name << "' done." << std::endl;
237 }
238 void group_end(const mpt::source_location & loc, const char * name, const group_statistics & statistics) override {
239 static_cast<void>(loc);
240 static_cast<void>(statistics);
241 s << "Running group '" << name << "' done." << std::endl;
242 }
243 void run_end(const mpt::source_location & loc, const global_statistics & statistics) override {
244 static_cast<void>(loc);
245 s << "Running test suite done." << std::endl;
246 s << "groups: " << statistics.groups.total << " | " << statistics.groups.successes << " passed";
247 if (statistics.groups.failures || statistics.groups.unexpected_exceptions) {
248 s << " | " << statistics.groups.failures << " FAILED";
249 if (statistics.groups.unexpected_exceptions) {
250 s << " | " << statistics.groups.unexpected_exceptions << " UNEXPECTED EXCEPTIONS";
251 }
252 }
253 s << std::endl;
254 s << "tests: " << statistics.tests.total << " | " << statistics.tests.successes << " passed";
255 if (statistics.tests.failures || statistics.tests.unexpected_exceptions) {
256 s << " | " << statistics.tests.failures << " FAILED";
257 if (statistics.tests.unexpected_exceptions) {
258 s << " | " << statistics.tests.unexpected_exceptions << " UNEXPECTED EXCEPTIONS";
259 }
260 }
261 s << std::endl;
262 s << "checks: " << statistics.cases.total << " | " << statistics.cases.successes << " passed";
263 if (statistics.cases.failures || statistics.cases.unexpected_exceptions) {
264 s << " | " << statistics.cases.failures << " FAILED";
265 if (statistics.cases.unexpected_exceptions) {
266 s << " | " << statistics.cases.unexpected_exceptions << " UNEXPECTED EXCEPTIONS";
267 }
268 }
269 s << std::endl;
270 }
271 void immediate_breakpoint() override {
272 return;
273 }
274 };
275
276 struct group;
277
278 struct context {
279 mpt::test::group & group;
280 mpt::test::reporter_interface & reporter;
281 mpt::test::group_statistics statistics{};
282 };
283
284 using void_context_function = void (*)(mpt::test::context &);
285
286 struct group {
287 group * next{nullptr};
288 const char * name{""};
289 void_context_function func{nullptr};
290 inline group(const char * name_, void_context_function f)
291 : name(name_)
292 , func(f) {
293 next = group_list();
294 group_list() = this;
295 }
296 group_statistics run(mpt::test::reporter_interface & reporter, const mpt::source_location & loc = mpt::source_location::current()) {
297 mpt::test::context context{*this, reporter};
298 context.reporter.group_begin(loc, name);
299 if (func) {
300 func(context);
301 }
302 context.reporter.group_end(loc, name, context.statistics);
303 return context.statistics;
304 }
305
306 public:
307 [[nodiscard]] static inline group *& group_list() noexcept {
308 static group * group_list = nullptr;
309 return group_list;
310 }
311 };
312
313 inline global_statistics run_all(mpt::test::reporter_interface & reporter, const mpt::source_location & loc = mpt::source_location::current()) {
314 global_statistics statistics{};
315 reporter.run_begin(loc);
316 for (group * g = group::group_list(); g; g = g->next) {
317 statistics.groups.total++;
318 statistics.groups.run++;
319 group_statistics s = g->run(reporter, loc);
320 if (s.tests.unexpected_exceptions) {
321 statistics.groups.unexpected_exceptions++;
322 } else if (s.tests.failures) {
323 statistics.groups.failures++;
324 } else {
325 statistics.groups.successes++;
326 }
327 statistics.tests += s.tests;
328 statistics.cases += s.cases;
329 statistics.groups.completed++;
330 statistics.individual_group_statistics[g->name] = s;
331 }
332 reporter.run_end(loc, statistics);
333 return statistics;
334 }
335
336 struct test {
337
338 mpt::test::context & context;
339 const char * name{""};
340 mpt::source_location source_location{mpt::source_location::current()};
341 void (*breakpoint)(void){nullptr};
342
343 test(const test &) = delete;
344 test & operator=(const test &) = delete;
345
346 inline test(mpt::test::context & context_, void (*breakpoint_)(void) = nullptr, const mpt::source_location & source_location_ = mpt::source_location::current())
347 : context(context_)
348 , source_location(source_location_)
349 , breakpoint(breakpoint_) {
350 report_test_begin();
351 }
352 inline test(mpt::test::context & context_, const char * name_, void (*breakpoint_)(void) = nullptr, const mpt::source_location & source_location_ = mpt::source_location::current())
353 : context(context_)
354 , name(name_)
355 , source_location(source_location_)
356 , breakpoint(breakpoint_) {
357 report_test_begin();
358 }
359
360 inline ~test() {
361 report_test_end();
362 }
363
364 inline void immediate_breakpoint() {
365 if (breakpoint) {
366 breakpoint();
367 } else {
368 context.reporter.immediate_breakpoint();
369 }
370 }
371
372 void report_test_begin() {
373 context.statistics.tests.total++;
374 context.statistics.tests.run++;
375 context.statistics.local_cases = statistics_counters{};
376 context.reporter.test_begin(source_location, name);
377 }
378
379 void report_run() {
380 context.statistics.local_cases.total++;
381 context.statistics.local_cases.run++;
382 context.reporter.case_run(source_location);
383 }
384 void report_run(const char * text_e) {
385 context.statistics.local_cases.total++;
386 context.statistics.local_cases.run++;
387 context.reporter.case_run(source_location, text_e);
388 }
389 void report_run(const char * text_ex, const char * text_e) {
390 context.statistics.local_cases.total++;
391 context.statistics.local_cases.run++;
392 context.reporter.case_run(source_location, text_ex, text_e);
393 }
394 void report_run(const char * text_a, const char * text_cmp, const char * text_b) {
395 context.statistics.local_cases.total++;
396 context.statistics.local_cases.run++;
397 context.reporter.case_run(source_location, text_a, text_cmp, text_b);
398 }
399
400 void report_result(mpt::test::result result) {
401 if (std::holds_alternative<result_success>(result.info)) {
402 context.statistics.local_cases.successes++;
403 } else if (std::holds_alternative<result_failure>(result.info)) {
404 context.statistics.local_cases.failures++;
405 } else if (std::holds_alternative<result_unexpected_exception>(result.info)) {
406 context.statistics.local_cases.unexpected_exceptions++;
407 }
408 context.statistics.local_cases.completed++;
409 context.reporter.case_result(source_location, result);
410 }
411
412 void report_test_end() {
413 context.statistics.cases += context.statistics.local_cases;
414 if (context.statistics.local_cases.unexpected_exceptions) {
415 context.statistics.tests.unexpected_exceptions++;
416 } else if (context.statistics.local_cases.failures) {
417 context.statistics.tests.failures++;
418 } else {
419 context.statistics.tests.successes++;
420 }
421 context.statistics.tests.completed++;
422 context.reporter.test_end(source_location, name, context.statistics.local_cases);
423 }
424
425 template <typename Texception, typename Tcallable, typename std::enable_if<std::is_invocable<Tcallable>::value, bool>::type = true>
426 inline test & expect_throws(Tcallable c, const char * text_ex = nullptr, const char * text_e = nullptr) {
427 const std::type_info & tiexception = typeid(Texception);
428 const std::type_info & tic = typeid(decltype(c()));
429 report_run(text_ex ? text_ex : tiexception.name(), text_e ? text_e : tic.name());
430 mpt::test::result result;
431 try {
432 c();
433 immediate_breakpoint();
434 result.info = mpt::test::result_failure{};
435 } catch (const Texception &) {
436 result.info = mpt::test::result_success{};
437 } catch (...) {
438 immediate_breakpoint();
439 result.info = mpt::test::result_unexpected_exception{mpt::test::get_exception_text()};
440 }
441 report_result(result);
442 return *this;
443 }
444
445 template <typename Tcallable, typename std::enable_if<std::is_invocable<Tcallable>::value, bool>::type = true>
446 inline test & expect_throws_any(Tcallable c, const char * text_e = nullptr) {
447 const std::type_info & tic = typeid(decltype(c()));
448 report_run(nullptr, text_e ? text_e : tic.name());
449 mpt::test::result result;
450 try {
451 c();
452 immediate_breakpoint();
453 result.info = mpt::test::result_failure{};
454 } catch (...) {
455 result.info = mpt::test::result_success{};
456 }
457 report_result(result);
458 return *this;
459 }
460
461 template <typename Texpr, typename std::enable_if<std::is_invocable<Texpr>::value, bool>::type = true>
462 inline test & expect(Texpr e, const char * text_e = nullptr) {
463 const std::type_info & tie = typeid(decltype(std::invoke(e)));
464 report_run(text_e ? text_e : tie.name());
465 mpt::test::result result;
466 try {
467 const auto ve = std::invoke(e);
468 if (!ve) {
469 immediate_breakpoint();
470 result.info = mpt::test::result_failure{/*mpt::test::format(ve)*/};
471 } else {
472 result.info = mpt::test::result_success{};
473 }
474 } catch (...) {
475 immediate_breakpoint();
476 result.info = mpt::test::result_unexpected_exception{mpt::test::get_exception_text()};
477 }
478 report_result(result);
479 return *this;
480 }
481
482 template <typename Ta, typename Tcmp, typename Tb, typename std::enable_if<std::is_invocable<Ta>::value, bool>::type = true, typename std::enable_if<std::is_invocable<Tb>::value, bool>::type = true>
483 inline test & expect(Ta && a, Tcmp cmp, Tb && b, const char * text_a = nullptr, const char * text_cmp = nullptr, const char * text_b = nullptr) {
484 const std::type_info & tia = typeid(decltype(std::invoke(a)));
485 const std::type_info & ticmp = typeid(decltype(cmp));
486 const std::type_info & tib = typeid(decltype(std::invoke(b)));
487 report_run(text_a ? text_a : tia.name(), text_cmp ? text_cmp : ticmp.name(), text_b ? text_b : tib.name());
488 mpt::test::result result;
489 try {
490 const auto va = std::invoke(a);
491 const auto vb = std::invoke(b);
492 if (!cmp(va, vb)) {
493 immediate_breakpoint();
494 result.info = mpt::test::result_failure{mpt::test::format(va) + " " + mpt::test::format(cmp) + " " + mpt::test::format(vb)};
495 } else {
496 result.info = mpt::test::result_success{};
497 }
498 } catch (...) {
499 immediate_breakpoint();
500 result.info = mpt::test::result_unexpected_exception{mpt::test::get_exception_text()};
501 }
502 report_result(result);
503 return *this;
504 }
505
506 template <typename Texpr, typename std::enable_if<!std::is_invocable<Texpr>::value, bool>::type = true>
507 inline test & expect(Texpr && e, const char * text_e = nullptr) {
508 const std::type_info & tie = typeid(decltype(std::forward<Texpr>(e)));
509 report_run(text_e ? text_e : tie.name());
510 mpt::test::result result;
511 try {
512 const auto ve = std::forward<Texpr>(e);
513 if (!ve) {
514 immediate_breakpoint();
515 result.info = mpt::test::result_failure{/*mpt::test::format(ve)*/};
516 } else {
517 result.info = mpt::test::result_success{};
518 }
519 } catch (...) {
520 immediate_breakpoint();
521 result.info = mpt::test::result_unexpected_exception{mpt::test::get_exception_text()};
522 }
523 report_result(result);
524 return *this;
525 }
526
527 template <typename Ta, typename Tcmp, typename Tb, typename std::enable_if<!std::is_invocable<Ta>::value, bool>::type = true, typename std::enable_if<!std::is_invocable<Tb>::value, bool>::type = true>
528 inline test & expect(Ta && a, Tcmp cmp, Tb && b, const char * text_a = nullptr, const char * text_cmp = nullptr, const char * text_b = nullptr) {
529 const std::type_info & tia = typeid(decltype(std::forward<Ta>(a)));
530 const std::type_info & ticmp = typeid(decltype(cmp));
531 const std::type_info & tib = typeid(decltype(std::forward<Tb>(b)));
532 report_run(text_a ? text_a : tia.name(), text_cmp ? text_cmp : ticmp.name(), text_b ? text_b : tib.name());
533 mpt::test::result result;
534 try {
535 const auto va = std::forward<Ta>(a);
536 const auto vb = std::forward<Tb>(b);
537 if (!cmp(va, vb)) {
538 immediate_breakpoint();
539 result.info = mpt::test::result_failure{mpt::test::format(va) + " " + mpt::test::format(cmp) + " " + mpt::test::format(vb)};
540 } else {
541 result.info = mpt::test::result_success{};
542 }
543 } catch (...) {
544 immediate_breakpoint();
545 result.info = mpt::test::result_unexpected_exception{mpt::test::get_exception_text()};
546 }
547 report_result(result);
548 return *this;
549 }
550 };
551
552
553
554 } // namespace test
555
556
557
558 } // namespace MPT_INLINE_NS
559 } // namespace mpt
560
561
562
563 #endif // MPT_TEST_TEST_HPP