micro-test-plus 3.2.0
µTest++, a lightweight testing framework for embedded platforms
Loading...
Searching...
No Matches
test-reporter.h
Go to the documentation of this file.
1/*
2 * This file is part of the µOS++ distribution.
3 * (https://github.com/micro-os-plus/)
4 * Copyright (c) 2021 Liviu Ionescu. All rights reserved.
5 *
6 * Permission to use, copy, modify, and/or distribute this software
7 * for any purpose is hereby granted, under the terms of the MIT license.
8 *
9 * If a copy of the license was not distributed with this file, it can
10 * be obtained from <https://opensource.org/licenses/MIT/>.
11 *
12 * Major parts of the code are inspired from v1.1.8 of the Boost UT project,
13 * released under the terms of the Boost Version 1.0 Software License,
14 * which can be obtained from <https://www.boost.org/LICENSE_1_0.txt>.
15 */
16
17#ifndef MICRO_TEST_PLUS_TEST_REPORTER_H_
18#define MICRO_TEST_PLUS_TEST_REPORTER_H_
19
20// ----------------------------------------------------------------------------
21
22#ifdef __cplusplus
23
24// ----------------------------------------------------------------------------
25
26// #include <functional>
27#include <string_view>
28#include <string>
29
30#include "type-traits.h"
31#include "test-suite.h"
32#include "detail.h"
33
34// ----------------------------------------------------------------------------
35
36#if defined(__GNUC__)
37#pragma GCC diagnostic push
38#pragma GCC diagnostic ignored "-Wpadded"
39#pragma GCC diagnostic ignored "-Waggregate-return"
40#if defined(__clang__)
41#pragma clang diagnostic ignored "-Wc++98-compat"
42#pragma clang diagnostic ignored "-Wc++98-compat-pedantic"
43#endif
44#endif
45
47{
48 // --------------------------------------------------------------------------
49
54 struct colors
55 {
56 const char* none = "\033[0m";
57 const char* pass = "\033[32m";
58 const char* fail = "\033[31m";
59 };
60
65 enum class verbosity
66 {
67 silent = 0, // Nothing, only return the exit code
68 quiet = 1, // Test suites results
69 normal = 2, // Test suites results and failed test cases
70 verbose = 3 // All, including passed checks
71 };
72
74
75 class test_reporter;
76
78 endl (test_reporter& stream);
79
80 // Requires events::assertion_* for and detailed operators.
81
88 {
89 public:
90 test_reporter () = default;
91
92 [[nodiscard]] inline auto
93 color (const bool cond)
94 {
95 return cond ? colors_.pass : colors_.fail;
96 }
97
99 operator<< (std::string_view sv);
100
102 operator<< (char c);
103
105 operator<< (const char* s);
106
108 operator<< (char* s);
109
111 operator<< (bool v);
112
113 test_reporter& operator<< (std::nullptr_t);
114
116 operator<< (signed char c);
117
119 operator<< (unsigned char c);
120
122 operator<< (signed short c);
123
125 operator<< (unsigned short c);
126
128 operator<< (signed int v);
129
131 operator<< (unsigned int v);
132
134 operator<< (signed long v);
135
137 operator<< (unsigned long v);
138
140 operator<< (signed long long v);
141
143 operator<< (unsigned long long v);
144
146 operator<< (float v);
147
149 operator<< (double v);
150
152 operator<< (long double v);
153
157 template <typename T>
159 operator<< (T* v);
160
166
167 // ------------------------------------------------------------------------
168 // Specific operators.
169
173 template <class T>
175 operator<< (const T& t);
176
181 template <class T>
184
188 template <class T,
191 = 0>
193 operator<< (T&& t);
194
198 template <class Lhs_T, class Rhs_T>
201
205 template <class Lhs_T, class Rhs_T>
208
212 template <class Lhs_T, class Rhs_T>
215
219 template <class Lhs_T, class Rhs_T>
222
226 template <class Lhs_T, class Rhs_T>
229
233 template <class Lhs_T, class Rhs_T>
236
240 template <class Lhs_T, class Rhs_T>
243
247 template <class Lhs_T, class Rhs_T>
250
254 template <class T>
256 operator<< (const detail::not_<T>& op);
257
258#if defined(__cpp_exceptions)
259 template <class Expr_T, class Exception_T>
262
263 template <class Expr_T>
266
267 template <class Expr_T>
270#endif
271
272 void
273 endline (void);
274
275 // ------------------------------------------------------------------------
276
280 template <class Expr_T>
281 void
282 pass (Expr_T& expr, std::string& message);
283
287 template <class Expr_T>
288 void
289 fail (Expr_T& expr, bool abort, std::string& message,
290 const reflection::source_location& location);
291
292 void
293 begin_test_case (const char* name);
294
295 void
296 end_test_case (const char* name);
297
298 void
299 begin_test_suite (const char* name);
300
301 void
303
307 void
308 flush (void);
309
310 void
311 output (void);
312
313 // Used to nicely format the output, without empty lines
314 // between successful test cases.
315 bool add_empty_line{ true };
316
318
319 protected:
320 // The prefix/suffix methods help shorten the code
321 // generated by the template methods.
322
323 void
324 output_pass_prefix_ (std::string& message);
325
326 void
327 output_pass_suffix_ (void);
328
329 void
330 output_fail_prefix_ (std::string& message,
331 const reflection::source_location& location);
332
333 void
334 output_fail_suffix_ (bool abort);
335
337 std::string out_{};
338
339 bool is_in_test_case_ = false;
340 };
341
342 // --------------------------------------------------------------------------
343} // namespace micro_os_plus::micro_test_plus
344
345#if defined(__GNUC__)
346#pragma GCC diagnostic pop
347#endif
348
349// ----------------------------------------------------------------------------
350
351#endif // __cplusplus
352
353// ----------------------------------------------------------------------------
354
355#endif // MICRO_TEST_PLUS_TEST_REPORTER_H_
356
357// ----------------------------------------------------------------------------
Local implementation of the std::source_location.
Definition reflection.h:58
Reporter to display the test results. For failed tests it prints the actual values of the operands,...
void pass(Expr_T &expr, std::string &message)
Report a passed condition.
void flush(void)
Flush the current buffered content.
void fail(Expr_T &expr, bool abort, std::string &message, const reflection::source_location &location)
Report a failed condition.
test_reporter & operator<<(std::string_view sv)
void output_fail_prefix_(std::string &message, const reflection::source_location &location)
Base class for all test suites.
Definition test-suite.h:51
typename requires_< Cond >::type requires_t
verbosity
The verbosity levels.
test_reporter & endl(test_reporter &stream)
Colours used to highlight pass vs. fail.
Greater than or equal comparator.
Definition detail.h:322
Less than or equal comparator.
Definition detail.h:439
Operator to check if the expression does not throw any exception.
Definition detail.h:666
Operator to check if the expression throws a specific exception.
Definition detail.h:600