micro-test-plus 3.3.1
µ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-2026 Liviu Ionescu. All rights reserved.
4 *
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * 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 be
9 * 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 = "";
109 const char* pass = "";
110 const char* fail = "";
111 };
112
114 "\033[0m",
115 "\033[32m",
116 "\033[31m"
117 };
118
133 enum class verbosity
134 {
135 silent = 0,
136 quiet = 1,
137 normal = 2,
140 };
141
155
156 // Forward definition.
157 class test_reporter;
158
167 endl (test_reporter& stream);
168
169 // Requires events::assertion_* for and detailed operators.
170
171 class test_runner;
172
203 {
204 public:
208 virtual ~test_reporter ();
209
220 [[nodiscard]] inline auto
221 color (const bool cond)
222 {
223 return cond ? colors_.pass : colors_.fail;
224 }
225
233 operator<< (std::string_view sv);
234
242 operator<< (char c);
243
251 operator<< (const char* s);
252
260 operator<< (char* s);
261
269 operator<< (bool v);
270
275 test_reporter& operator<< (std::nullptr_t);
276
284 operator<< (signed char c);
285
293 operator<< (unsigned char c);
294
302 operator<< (signed short v);
303
311 operator<< (unsigned short v);
312
320 operator<< (signed int v);
321
329 operator<< (unsigned int v);
330
338 operator<< (signed long v);
339
347 operator<< (unsigned long v);
348
356 operator<< (signed long long v);
357
365 operator<< (unsigned long long v);
366
374 operator<< (float v);
375
383 operator<< (double v);
384
392 operator<< (long double v);
393
402 template <typename T>
404 operator<< (T* v);
405
414
415 // ------------------------------------------------------------------------
416 // Specific operators.
417
426 template <class T>
428 operator<< (const T& t);
429
439 template <class T>
442
451 template <class T,
454 = 0>
456 operator<< (T&& t);
457
467 template <class Lhs_T, class Rhs_T>
470
480 template <class Lhs_T, class Rhs_T>
483
493 template <class Lhs_T, class Rhs_T>
496
506 template <class Lhs_T, class Rhs_T>
509
519 template <class Lhs_T, class Rhs_T>
522
532 template <class Lhs_T, class Rhs_T>
535
545 template <class Lhs_T, class Rhs_T>
548
558 template <class Lhs_T, class Rhs_T>
561
570 template <class T>
572 operator<< (const detail::not_<T>& op);
573
574#if defined(__cpp_exceptions)
585 template <class Expr_T, class Exception_T>
588
597 template <class Expr_T>
600
609 template <class Expr_T>
612#endif
613
622 virtual void
623 endline (void)
624 = 0;
625
626 // ------------------------------------------------------------------------
627
638 template <class Expr_T>
639 void
640 pass (Expr_T& expr, std::string& message);
641
654 template <class Expr_T>
655 void
656 fail (Expr_T& expr, bool abort, std::string& message,
657 const reflection::source_location& location);
658
666 virtual void
667 begin_test_case (const char* name)
668 = 0;
669
677 virtual void
678 end_test_case (const char* name)
679 = 0;
680
688 virtual void
689 begin_test_suite (const char* name)
690 = 0;
691
699 virtual void
701 = 0;
702
710 virtual void
711 begin_test (size_t test_suites_count)
712 = 0;
713
721 virtual void
723 = 0;
724
733 virtual void
734 flush (void)
735 = 0;
736
745 virtual void
746 output (void)
747 = 0;
748
756 bool add_empty_line{ true };
757
762
763 protected:
771 virtual void
772 output_pass_prefix_ (std::string& message)
773 = 0;
774
783 virtual void
785 = 0;
786
797 virtual void
798 output_fail_prefix_ (std::string& message, const bool hasExpression,
799 const reflection::source_location& location)
800 = 0;
801
810 virtual void
812 bool abort)
813 = 0;
814
819
823 std::string out_{};
824
828 bool is_in_test_case_ = false;
829 };
830
831 // --------------------------------------------------------------------------
832} // namespace micro_os_plus::micro_test_plus
833
834#if defined(__GNUC__)
835#pragma GCC diagnostic pop
836#endif
837
838// ----------------------------------------------------------------------------
839
840#endif // __cplusplus
841
842// ----------------------------------------------------------------------------
843
844#endif // MICRO_TEST_PLUS_TEST_REPORTER_H_
845
846// ----------------------------------------------------------------------------
Local implementation of source location information for diagnostics.
Definition reflection.h:137
Reporter to display test results, including operand values and types for failures.
virtual void output_pass_suffix_(void)=0
Outputs the suffix for a passing condition.
colors colors_
ANSI colour codes for output formatting.
virtual void endline(void)=0
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.
virtual void output_fail_suffix_(const reflection::source_location &location, bool abort)=0
Outputs the suffix for a failing condition.
virtual void output_fail_prefix_(std::string &message, const bool hasExpression, const reflection::source_location &location)=0
Outputs the prefix for a failing condition.
virtual void end_test(test_runner &runner)=0
Mark the end of a test.
virtual void end_test_case(const char *name)=0
Mark the end of a test case.
virtual void begin_test(size_t test_suites_count)=0
Mark the beginning of a test.
virtual void output_pass_prefix_(std::string &message)=0
Outputs the prefix for a passing condition.
virtual void begin_test_suite(const char *name)=0
Mark the beginning of a test suite.
virtual void flush(void)=0
Flush the current buffered content.
void pass(Expr_T &expr, std::string &message)
Report a passed condition.
virtual ~test_reporter()
Default constructor for the test_reporter class.
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)
Output operator for std::string_view.
verbosity_t verbosity
The verbosity level for test reporting.
virtual void end_test_suite(test_suite_base &suite)=0
Mark the end of a test suite.
auto color(const bool cond)
Selects the appropriate colour code based on a condition.
virtual void output(void)=0
Output the current buffered content.
bool add_empty_line
Controls whether to add an empty line between successful test cases.
virtual void begin_test_case(const char *name)=0
Mark the beginning of a test case.
The test runner for the µTest++ framework.
Definition test-runner.h:99
Base class for all test suites.
Definition test-suite.h:100
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.
test_runner runner
Global instance of test_runner.
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:1198
Equality comparator struct template.
Definition detail.h:260
Greater than or equal comparator struct template.
Definition detail.h:752
Greater than comparator struct template.
Definition detail.h:605
Less than or equal comparator struct template.
Definition detail.h:1049
Less than comparator struct template.
Definition detail.h:901
Non-equality comparator struct template.
Definition detail.h:438
Logical NOT comparator struct template.
Definition detail.h:1435
Operator struct template to check if an expression does not throw any exception.
Definition detail.h:1695
Logical OR comparator struct template.
Definition detail.h:1317
Operator struct template to check if an expression throws a specific exception.
Definition detail.h:1532
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.