micro-test-plus 5.0.1
µTest++ Testing Framework
Loading...
Searching...
No Matches
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_OS_PLUS_MICRO_TEST_PLUS_TEST_REPORTER_H_
49#define MICRO_OS_PLUS_MICRO_TEST_PLUS_TEST_REPORTER_H_
50
51// ----------------------------------------------------------------------------
52
53#if defined(__cplusplus)
54
55// ----------------------------------------------------------------------------
56
57#if __has_include("micro-os-plus/project-config.h")
58#include "micro-os-plus/project-config.h"
59#endif // __has_include("micro-os-plus/project-config.h")
60
61#if __has_include("micro-os-plus/micro-test-plus-defines.h")
62#include "micro-os-plus/micro-test-plus-defines.h"
63#endif // __has_include("micro-os-plus/micro-test-plus-defines.h")
64
65#include "type-traits.h"
66#include "detail.h"
68#include "reflection.h"
69
70#include <cstdio>
71#include <string_view>
72#include <string>
73#include <memory>
74#include <vector>
75#include <charconv>
76
77// ----------------------------------------------------------------------------
78
79#if defined(__GNUC__)
80#pragma GCC diagnostic push
81
82#pragma GCC diagnostic ignored "-Wpadded"
83#pragma GCC diagnostic ignored "-Waggregate-return"
84#if defined(__clang__)
85#pragma clang diagnostic ignored "-Wc++98-compat"
86#pragma clang diagnostic ignored "-Wc++98-compat-pedantic"
87#endif // defined(__clang__)
88#endif // defined(__GNUC__)
89
90// =============================================================================
91
93{
94 // --------------------------------------------------------------------------
95
110 enum class verbosity
111 {
112 silent = 0,
113 quiet = 1,
114 normal = 2,
117 };
118
119 // Forward definitions.
120 class reporter;
121 class runner;
122 class suite;
123 class subtest;
124
132 reporter&
133 endl (reporter& stream);
134
135 namespace detail
136 {
137 // ------------------------------------------------------------------------
138
146 struct indent_t
147 {
148 size_t level;
149 };
150
151 // ------------------------------------------------------------------------
152 } // namespace detail
153
160 [[nodiscard]] detail::indent_t
161 indent (size_t level);
162
163 // ==========================================================================
164
195 {
196 public:
203 reporter (std::unique_ptr<std::vector<std::string_view>> argvs);
204
208 virtual ~reporter ();
209
216 reporter&
217 operator<< (std::string_view sv);
218
225 reporter&
226 operator<< (char c);
227
234 reporter&
235 operator<< (const char* s);
236
244 operator<< (bool v);
245
250 reporter& operator<< (std::nullptr_t);
251
260 template <class T>
261 requires std::is_arithmetic_v<T>
262 reporter&
263 operator<< (T v);
264
273 template <typename T>
274 reporter&
275 operator<< (T* v);
276
283 reporter&
284 operator<< (reporter& (*func) (reporter&));
285
286 // ------------------------------------------------------------------------
287 // Specific operators.
288
289 // ------------------------------------------------------------------------
290
299 void
300 endline (void);
301
315 void
317
326 void
327 flush (void);
328
329 // ------------------------------------------------------------------------
330
340 void
341 pass (std::string& message, const std::string& expression,
343
357 void
358 fail (bool abort, std::string& message, const std::string& expression,
359 bool has_expression, const reflection::source_location& location,
361
362 // ------------------------------------------------------------------------
363
371 virtual void
373 = 0;
374
382 virtual void
384 = 0;
385
393 virtual void
395 = 0;
396
404 virtual void
406 = 0;
407
415 virtual void
417 = 0;
418
426 virtual void
428 = 0;
429
437 virtual const char*
439 = 0;
440
448 auto
450
459 detail::expression_formatter&
460 expression ();
461
462 // ------------------------------------------------------------------------
463
464 protected:
471 [[nodiscard]] inline auto
472 colour_ (const bool cond) const;
473
474 void
476
485 void
486 write_info_ (void);
487
496 virtual void
497 output_pass_prefix_ (std::string& message, subtest& subtest)
498 = 0;
499
507 virtual void
509 = 0;
510
522 virtual void
523 output_fail_prefix_ (std::string& message, const bool has_expression,
524 const reflection::source_location& location,
526 = 0;
527
537 virtual void
538 output_fail_suffix_ (const reflection::source_location& location,
539 bool abort, subtest& subtest)
540 = 0;
541
542 protected:
547
551 detail::colours colours_{};
552
561 std::string buffer_{};
562
573
581 bool add_empty_line_{ true };
582
590 const char* output_file_path_{ nullptr };
591
600 FILE* output_file_{ nullptr };
601
605 std::unique_ptr<std::vector<std::string_view>> argvs_{};
606 };
607
608 // --------------------------------------------------------------------------
609} // namespace micro_os_plus::micro_test_plus
610
611#if defined(__GNUC__)
612#pragma GCC diagnostic pop
613#endif // defined(__GNUC__)
614
615// ----------------------------------------------------------------------------
616
617#endif // defined(__cplusplus)
618
619// ============================================================================
620// Templates, inlines & constexpr implementations.
621
623
624// ----------------------------------------------------------------------------
625
626#endif // MICRO_OS_PLUS_MICRO_TEST_PLUS_TEST_REPORTER_H_
627
628// ----------------------------------------------------------------------------
Formats values and expressions into an owned string buffer.
Local implementation of source location information for diagnostics.
Definition reflection.h:150
void fail(bool abort, std::string &message, const std::string &expression, bool has_expression, const reflection::source_location &location, subtest &subtest)
Report a failed condition.
Definition reporter.cpp:446
reporter(std::unique_ptr< std::vector< std::string_view > > argvs)
Constructor for the reporter class.
Definition reporter.cpp:80
std::unique_ptr< std::vector< std::string_view > > argvs_
Owns the command-line arguments passed to the test runner.
Definition reporter.h:605
virtual void output_pass_suffix_(subtest &subtest)=0
Outputs the suffix for a passing condition.
virtual ~reporter()
Virtual destructor for the reporter class.
Definition reporter.cpp:158
void pass(std::string &message, const std::string &expression, subtest &subtest)
Report a passed condition.
Definition reporter.cpp:427
FILE * output_file_
Optional output file for redirecting test report output.
Definition reporter.h:600
auto verbosity() const -> micro_test_plus::verbosity
Returns the current verbosity level.
void write_buffer_to_stdout(void)
Output the current buffered content.
Definition reporter.cpp:218
virtual void begin_subtest(subtest &subtest)=0
Mark the beginning of a subtest.
bool add_empty_line_
Controls whether to add an empty line between successful test cases.
Definition reporter.h:581
virtual void output_fail_prefix_(std::string &message, const bool has_expression, const reflection::source_location &location, subtest &subtest)=0
Outputs the prefix for a failing condition.
detail::expression_formatter expression_
Expression formatter for pass and fail reporting.
Definition reporter.h:572
std::string buffer_
Output accumulation buffer.
Definition reporter.h:561
void write_info_(void)
Appends informational (non-result) text to the output buffer.
Definition reporter.cpp:251
virtual void begin_suite(suite &suite)=0
Mark the beginning of a test suite.
virtual const char * get_comment_prefix(void)=0
Returns the comment-prefix string used by this reporter format.
virtual void end_suite(suite &suite)=0
Mark the end of a test suite.
virtual void output_pass_prefix_(std::string &message, subtest &subtest)=0
Outputs the prefix for a passing condition.
auto colour_(const bool cond) const
Selects the appropriate colour code based on a condition.
const char * output_file_path_
Optional file path for redirecting test report output.
Definition reporter.h:590
detail::expression_formatter & expression()
Provides access to the expression formatter for this reporter.
virtual void begin_session(runner &runner)=0
Mark the beginning of a test session.
reporter & operator<<(std::string_view sv)
Output operator for std::string_view.
Definition reporter.cpp:385
detail::colours colours_
ANSI colour codes for output formatting.
Definition reporter.h:551
void flush(void)
Flush the current buffered content.
Definition reporter.cpp:348
enum verbosity verbosity_
The verbosity level for test reporting.
Definition reporter.h:546
virtual void end_subtest(subtest &subtest)=0
Mark the end of a subtest.
void endline(void)
Inserts a line ending into the output buffer.
Definition reporter.cpp:202
virtual void output_fail_suffix_(const reflection::source_location &location, bool abort, subtest &subtest)=0
Outputs the suffix for a failing condition.
virtual void end_session(runner &runner)=0
Mark the end of a test session.
The test runner for the µTest++ framework.
Definition runner.h:144
A named, runnable test case that lives inside a suite.
Definition test.h:551
A named, runnable test suite registered with the test runner.
Definition test.h:723
C++ header file with declarations for the µTest++ internals.
C++ header file with declarations for the µTest++ expression formatter.
Internal implementation details for the µTest++ framework.
Reflection utilities for the µTest++ testing framework.
Primary namespace for the µTest++ testing framework.
detail::indent_t indent(size_t level)
Factory function that creates an indent_t manipulator.
verbosity
The verbosity levels for test reporting.
Definition reporter.h:111
reporter & endl(reporter &stream)
Output stream manipulator for ending a line in test reports.
Definition reporter.cpp:187
C++ header file with declarations for the µTest++ reflection utilities.
C++ header file with inline implementations for the µTest++ test reporter.
Parameterised stream manipulator for outputting indentation.
Definition reporter.h:147
size_t level
Number of four-space indentation levels.
Definition reporter.h:148
C++ header file with declarations for the µTest++ type trait utilities and metaprogramming support.