micro-test-plus 3.2.2
µTest++ Testing Framework
Loading...
Searching...
No Matches
test-reporter.h
Go to the documentation of this file.
1/*
2 * This file is part of the µOS++ project (https://micro-os-plus.github.io/).
3 * Copyright (c) 2021 Liviu Ionescu. All rights reserved.
4 *
5 * Permission to use, copy, modify, and/or distribute this software
6 * for any purpose is hereby granted, under the terms of the MIT license.
7 *
8 * If a copy of the license was not distributed with this file, it can
9 * be obtained from https://opensource.org/licenses/mit.
10 *
11 * Major parts of the code are inspired from v1.1.8 of the Boost UT project,
12 * released under the terms of the Boost Version 1.0 Software License,
13 * which can be obtained from https://www.boost.org/LICENSE_1_0.txt.
14 */
15
16// ----------------------------------------------------------------------------
17
47
48#ifndef MICRO_TEST_PLUS_TEST_REPORTER_H_
49#define MICRO_TEST_PLUS_TEST_REPORTER_H_
50
51// ----------------------------------------------------------------------------
52
53#ifdef __cplusplus
54
55// ----------------------------------------------------------------------------
56
57// #include <functional>
58#include <string_view>
59#include <string>
60
61#include "type-traits.h"
62#include "test-suite.h"
63#include "detail.h"
64
65// ----------------------------------------------------------------------------
66
67#if defined(__GNUC__)
68#pragma GCC diagnostic push
69#pragma GCC diagnostic ignored "-Wpadded"
70#pragma GCC diagnostic ignored "-Waggregate-return"
71#if defined(__clang__)
72#pragma clang diagnostic ignored "-Wc++98-compat"
73#pragma clang diagnostic ignored "-Wc++98-compat-pedantic"
74#endif
75#endif
76
78{
79 // --------------------------------------------------------------------------
80
106 struct colors
107 {
108 const char* none = "\033[0m";
109 const char* pass = "\033[32m";
110 const char* fail = "\033[31m";
111 };
112
127 enum class verbosity
128 {
129 silent = 0,
130 quiet = 1,
131 normal = 2,
134 };
135
149
150 // Forward definition.
151 class test_reporter;
152
161 endl (test_reporter& stream);
162
163 // Requires events::assertion_* for and detailed operators.
164
195 {
196 public:
200 test_reporter () = default;
201
212 [[nodiscard]] inline auto
213 color (const bool cond)
214 {
215 return cond ? colors_.pass : colors_.fail;
216 }
217
225 operator<< (std::string_view sv);
226
234 operator<< (char c);
235
243 operator<< (const char* s);
244
252 operator<< (char* s);
253
261 operator<< (bool v);
262
267 test_reporter& operator<< (std::nullptr_t);
268
276 operator<< (signed char c);
277
285 operator<< (unsigned char c);
286
294 operator<< (signed short v);
295
303 operator<< (unsigned short v);
304
312 operator<< (signed int v);
313
321 operator<< (unsigned int v);
322
330 operator<< (signed long v);
331
339 operator<< (unsigned long v);
340
348 operator<< (signed long long v);
349
357 operator<< (unsigned long long v);
358
366 operator<< (float v);
367
375 operator<< (double v);
376
384 operator<< (long double v);
385
394 template <typename T>
396 operator<< (T* v);
397
406
407 // ------------------------------------------------------------------------
408 // Specific operators.
409
418 template <class T>
420 operator<< (const T& t);
421
431 template <class T>
434
443 template <class T,
446 = 0>
448 operator<< (T&& t);
449
459 template <class Lhs_T, class Rhs_T>
462
472 template <class Lhs_T, class Rhs_T>
475
485 template <class Lhs_T, class Rhs_T>
488
498 template <class Lhs_T, class Rhs_T>
501
511 template <class Lhs_T, class Rhs_T>
514
524 template <class Lhs_T, class Rhs_T>
527
537 template <class Lhs_T, class Rhs_T>
540
550 template <class Lhs_T, class Rhs_T>
553
562 template <class T>
564 operator<< (const detail::not_<T>& op);
565
566#if defined(__cpp_exceptions)
577 template <class Expr_T, class Exception_T>
580
589 template <class Expr_T>
592
601 template <class Expr_T>
604#endif
605
614 void
615 endline (void);
616
617 // ------------------------------------------------------------------------
618
629 template <class Expr_T>
630 void
631 pass (Expr_T& expr, std::string& message);
632
645 template <class Expr_T>
646 void
647 fail (Expr_T& expr, bool abort, std::string& message,
648 const reflection::source_location& location);
649
657 void
658 begin_test_case (const char* name);
659
667 void
668 end_test_case (const char* name);
669
677 void
678 begin_test_suite (const char* name);
679
687 void
689
698 void
699 flush (void);
700
709 void
710 output (void);
711
719 bool add_empty_line{ true };
720
725
726 protected:
734 void
735 output_pass_prefix_ (std::string& message);
736
745 void
746 output_pass_suffix_ (void);
747
756 void
757 output_fail_prefix_ (std::string& message,
758 const reflection::source_location& location);
759
767 void
768 output_fail_suffix_ (bool abort);
769
774
778 std::string out_{};
779
783 bool is_in_test_case_ = false;
784 };
785
786 // --------------------------------------------------------------------------
787} // namespace micro_os_plus::micro_test_plus
788
789#if defined(__GNUC__)
790#pragma GCC diagnostic pop
791#endif
792
793// ----------------------------------------------------------------------------
794
795#endif // __cplusplus
796
797// ----------------------------------------------------------------------------
798
799#endif // MICRO_TEST_PLUS_TEST_REPORTER_H_
800
801// ----------------------------------------------------------------------------
Local implementation of source location information for diagnostics.
Definition reflection.h:137
Reporter to display test results, including operand values and types for failures.
colors colors_
ANSI colour codes for output formatting.
void endline(void)
Inserts a line ending into the output buffer.
bool is_in_test_case_
Indicates whether the reporter is currently within a test case.
std::string out_
Internal output buffer for accumulating report content.
test_reporter()=default
Default constructor for the test_reporter class.
void output_pass_suffix_(void)
Outputs the suffix for a passing condition.
void output(void)
Output the current buffered content.
void pass(Expr_T &expr, std::string &message)
Report a passed condition.
void output_fail_suffix_(bool abort)
Outputs the suffix for a failing 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.
void end_test_case(const char *name)
Mark the end of a test case.
void begin_test_case(const char *name)
Mark the beginning of a test case.
test_reporter & operator<<(std::string_view sv)
Output operator for std::string_view.
void end_test_suite(test_suite_base &suite)
Mark the end of a test suite.
void begin_test_suite(const char *name)
Mark the beginning of a test suite.
verbosity_t verbosity
The verbosity level for test reporting.
void output_fail_prefix_(std::string &message, const reflection::source_location &location)
Outputs the prefix for a failing condition.
auto color(const bool cond)
Selects the appropriate colour code based on a condition.
bool add_empty_line
Controls whether to add an empty line between successful test cases.
void output_pass_prefix_(std::string &message)
Outputs the prefix for a passing condition.
Base class for all test suites.
Definition test-suite.h:98
C++ header file with declarations for the µTest++ internals.
static constexpr auto has_npos_v
Variable template to determine if a type provides a static npos member.
typename requires_< Cond >::type requires_t
Alias template for extracting the type member from requires_.
Primary namespace for the µTest++ testing framework.
verbosity verbosity_t
Type alias for the verbosity enumeration used in test reporting.
verbosity
The verbosity levels for test reporting.
test_reporter & endl(test_reporter &stream)
Output stream manipulator for ending a line in test reports.
Colours used to highlight pass and fail results in test reports.
const char * none
Default colour.
Logical AND comparator struct template.
Definition detail.h:1178
Equality comparator struct template.
Definition detail.h:260
Greater than or equal comparator struct template.
Definition detail.h:738
Greater than comparator struct template.
Definition detail.h:593
Less than or equal comparator struct template.
Definition detail.h:1031
Less than comparator struct template.
Definition detail.h:885
Non-equality comparator struct template.
Definition detail.h:432
Logical NOT comparator struct template.
Definition detail.h:1415
Operator struct template to check if an expression does not throw any exception.
Definition detail.h:1673
Logical OR comparator struct template.
Definition detail.h:1297
Operator struct template to check if an expression throws a specific exception.
Definition detail.h:1512
Struct template representing a genuine integral value.
C++ header file with declarations for the µTest++ test suite.
C++ header file with declarations for the µTest++ type trait utilities and metaprogramming support.