micro-test-plus 6.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
12// ----------------------------------------------------------------------------
13
38
39#ifndef MICRO_OS_PLUS_MICRO_TEST_PLUS_REPORTER_H_
40#define MICRO_OS_PLUS_MICRO_TEST_PLUS_REPORTER_H_
41
42// ----------------------------------------------------------------------------
43
44#if defined(__cplusplus)
45
46// ----------------------------------------------------------------------------
47
48#if __has_include("micro-os-plus/project-config.h")
49#include "micro-os-plus/project-config.h"
50#endif // __has_include("micro-os-plus/project-config.h")
51
52#if __has_include("micro-os-plus/micro-test-plus-defines.h")
53#include "micro-os-plus/micro-test-plus-defines.h"
54#endif // __has_include("micro-os-plus/micro-test-plus-defines.h")
55
56#include "check-result.h"
57#include "detail.h"
59#include "extension.h"
60#include "reflection.h"
61#include "runner-options.h"
62
63#include <cstdint>
64#include <cstdio>
65#include <limits>
66#include <string_view>
67#include <string>
68#include <memory>
69#include <vector>
70#include <charconv>
71
72// ----------------------------------------------------------------------------
73
74#if defined(__GNUC__)
75#pragma GCC diagnostic push
76
77#pragma GCC diagnostic ignored "-Wpadded"
78#pragma GCC diagnostic ignored "-Waggregate-return"
79#if defined(__clang__)
80#pragma clang diagnostic ignored "-Wc++98-compat"
81#pragma clang diagnostic ignored "-Wc++98-compat-pedantic"
82#endif // defined(__clang__)
83#endif // defined(__GNUC__)
84
85// ============================================================================
86
88{
89 // --------------------------------------------------------------------------
90
91 // Forward definitions.
92 class runner;
93 class suite;
94 class test_case;
95
96 namespace detail
97 {
98 class check_result;
99 class reporter;
100 class stopwatch;
101
114 reporter&
115 endl (reporter& stream);
116
117 // ------------------------------------------------------------------------
118
127 {
134 size_t level;
135 };
136
147 [[nodiscard]] indentation
148 indent (size_t level);
149
150 // ========================================================================
151
214
222 {
230
238 bool file;
239 };
240
241 // ========================================================================
242
276 {
285
293 friend class check_result;
294
302
303 public:
310 explicit reporter (const runner_options& opts);
311
319 virtual ~reporter ();
320
327 reporter&
328 operator<< (std::string_view sv);
329
336 reporter&
337 operator<< (char c);
338
350 reporter&
351 operator<< (const char* s);
352
359 reporter&
360 operator<< (bool v);
361
367 reporter& operator<< (std::nullptr_t);
368
377 template <class T>
378 requires std::is_arithmetic_v<T>
379 reporter&
380 operator<< (T v);
381
395 template <typename T>
396 requires (not std::is_same_v<T, char>)
397 reporter&
398 operator<< (T* v);
399
406 reporter&
407 operator<< (reporter& (*func) (reporter&));
408
418 reporter&
420
421 // ----------------------------------------------------------------------
422
436 void
437 end_line (void);
438
451 void
452 flush_streams (void);
453
468 [[nodiscard]] bool
469 has_output_file_error (void) const noexcept;
470
471#if defined(MICRO_OS_PLUS_MICRO_TEST_PLUS_DEPRECATED_ENABLED)
476
490 [[deprecated ("use runner::verbosity_level() (since 6.0.0)"),
491 nodiscard]] auto
492 verbosity (void) const noexcept -> micro_test_plus::verbosity;
493
497#endif // defined(MICRO_OS_PLUS_MICRO_TEST_PLUS_DEPRECATED_ENABLED)
498
499 // ----------------------------------------------------------------------
500
510 void
511 pass (const std::string& message, const std::string& expression,
512 const test_case& test_case);
513
530 virtual void
532 = 0;
533
545 void
546 fail (const failure_info& failure, const std::string& expression,
547 const test_case& test_case);
548
549 // ----------------------------------------------------------------------
550
562 virtual void
564 = 0;
565
577 virtual void
579 = 0;
580
593 virtual void
595 = 0;
596
611 virtual void
613 = 0;
614
630 virtual void
632 = 0;
633
655 virtual void
656 fail_suite (const std::string& message, const suite& suite)
657 = 0;
658
672 virtual void
674 = 0;
675
687 virtual void
689 = 0;
690
707 virtual void
709 = 0;
710
741 virtual void
743 bool is_requirement)
744 = 0;
745
757 virtual const char*
758 comment_prefix (void) const noexcept
759 = 0;
760
774 virtual const char*
775 error_prefix (void) const noexcept;
776
795 [[nodiscard]] bool
796 writes_passed_checks (void) const noexcept;
797
798 // ----------------------------------------------------------------------
799
800 protected:
815 void
817
846 [[nodiscard]] output_route
847 route (record_kind kind) const noexcept;
848
865 [[nodiscard]] std::string_view
866 console_lines (bool failed) const noexcept;
867
880 void
881 write_console_text (std::string_view text);
882
896 void
897 mark_failure (size_t begin);
898
912 void
913 enter_suite (const suite& suite, bool displayed);
914
930 void
931 write_console (const char* format, ...)
932#if defined(__GNUC__)
933 __attribute__ ((format (printf, 2, 3)))
934#endif // defined(__GNUC__)
935 ;
936
949 void
950 write_file (const char* format, ...)
951#if defined(__GNUC__)
952 __attribute__ ((format (printf, 2, 3)))
953#endif // defined(__GNUC__)
954 ;
955
976 void
977 write_line (bool to_console, const char* format, ...)
978#if defined(__GNUC__)
979 __attribute__ ((format (printf, 3, 4)))
980#endif // defined(__GNUC__)
981 ;
982
993 void
994 write_info (const std::vector<std::string>& arguments);
995
1013 void
1014 announce_output_file (void);
1015
1028 void
1029 clear_buffer (void);
1030
1040 static constexpr size_t elapsed_time_size_ = 40;
1041
1060 static void
1062 char (&message)[elapsed_time_size_]);
1063
1082 static constexpr size_t totals_size_
1083 = 94 + 6 * (std::numeric_limits<size_t>::digits10 + 1);
1084
1103 static void
1105 char (&message)[totals_size_]);
1106
1124 static void
1125 format_suite_totals (const suite& suite, char (&message)[totals_size_]);
1126
1145 static void
1147 char (&message)[totals_size_]);
1148
1162 virtual void
1164 = 0;
1165
1180 void
1181 commit_outer_results (void);
1182
1195 virtual void
1196 output_pass_prefix (const std::string& message,
1197 const test_case& test_case)
1198 = 0;
1199
1210 virtual void
1212 = 0;
1213
1229 virtual void
1230 output_fail_prefix (const std::string& message,
1231 const bool has_expression,
1232 const reflection::source_location& location,
1233 const test_case& test_case)
1234 = 0;
1235
1250 virtual void
1252 check_kind kind, const test_case& test_case)
1253 = 0;
1254
1255 protected:
1263 static constexpr size_t indent_size_ = 4;
1264
1274
1285
1294 std::string buffer_{};
1295
1306
1316 std::string failures_{};
1317
1325 {
1333
1343 };
1344
1352 std::vector<open_test_case> open_test_cases_{};
1353
1371 void
1372 display_open_test_cases (bool include_last);
1373
1389 virtual void
1390 display_open_heading (const open_test_case& open, bool is_first)
1391 = 0;
1392
1399 const suite* current_suite_{ nullptr };
1400
1410 bool suite_displayed_{ false };
1411
1424
1434 bool add_empty_line_{ true };
1435
1445
1454 std::string output_file_path_{};
1455
1465 FILE* output_file_{ nullptr };
1466 };
1467
1468 // ------------------------------------------------------------------------
1469 } // namespace detail
1470} // namespace micro_os_plus::micro_test_plus
1471
1472#if defined(__GNUC__)
1473#pragma GCC diagnostic pop
1474#endif // defined(__GNUC__)
1475
1476// ----------------------------------------------------------------------------
1477
1478#endif // defined(__cplusplus)
1479
1480// ============================================================================
1481// Templates, inlines & constexpr implementations.
1482
1484
1485// ----------------------------------------------------------------------------
1486
1487#endif // MICRO_OS_PLUS_MICRO_TEST_PLUS_REPORTER_H_
1488
1489// ----------------------------------------------------------------------------
C++ header file with declarations for the µTest++ check result.
The result of one check, reported when destroyed.
Formats values and expressions into an owned string buffer.
Abstract base class for the reporters that display test results, including operand values for failure...
Definition reporter.h:276
virtual void begin_session(const runner &runner)=0
Marks the beginning of a test session.
virtual void skip_suite(const suite &suite)=0
Reports a test suite skipped by the filter.
bool has_output_file_error(void) const noexcept
Returns whether the output file could not be opened.
Definition reporter.cpp:636
std::string buffer_
Output accumulation buffer.
Definition reporter.h:1294
bool has_output_file_error_
Whether the output file could not be opened.
Definition reporter.h:1444
bool writes_passed_checks(void) const noexcept
Returns whether the passed checks are written anywhere.
Definition reporter.cpp:320
virtual void end_test_case(const test_case &test_case)=0
Marks the end of a test case.
virtual void skip_test_case(const test_case &test_case)=0
Reports a test case skipped by the filter.
virtual void fail_suite(const std::string &message, const suite &suite)=0
Reports a failure of a test suite outside its test cases.
virtual void end_session(const runner &runner)=0
Marks the end of a test session.
virtual void abort_session(const reflection::source_location &location, bool is_requirement)=0
Reports the abort of the test execution.
virtual void output_fail_prefix(const std::string &message, const bool has_expression, const reflection::source_location &location, const test_case &test_case)=0
Outputs the prefix for a failing condition.
const micro_test_plus::verbosity verbosity_
The verbosity level for test reporting.
Definition reporter.h:1273
FILE * output_file_
Optional output file, receiving a copy of the test report.
Definition reporter.h:1465
void end_line(void)
Appends a line ending to the internal output buffer.
Definition reporter.cpp:232
static constexpr size_t elapsed_time_size_
Size of the array that receives the elapsed time text.
Definition reporter.h:1040
void mark_failure(size_t begin)
Records that the lines appended to the buffer from a given position are a failure.
Definition reporter.cpp:367
virtual const char * comment_prefix(void) const noexcept=0
Returns the comment-prefix string used by this reporter format.
void write_buffer_to_file(void)
Writes the current buffered content to the output file.
Definition reporter.cpp:425
bool add_empty_line_
Whether to write an empty line before the next console output.
Definition reporter.h:1434
void announce_output_file(void)
Tells the console where the output file was written.
Definition reporter.cpp:244
void fail(const failure_info &failure, const std::string &expression, const test_case &test_case)
Reports a failed condition.
Definition reporter.cpp:771
auto verbosity(void) const noexcept -> micro_test_plus::verbosity
Deprecated access to the verbosity level.
reporter & operator<<(std::string_view sv)
Output operator for std::string_view.
Definition reporter.cpp:677
static void format_suite_totals(const suite &suite, char(&message)[totals_size_])
Formats the totals of a suite.
Definition reporter.cpp:933
void clear_buffer(void)
Clears the output buffer.
Definition reporter.cpp:795
void commit_outer_results(void)
Writes the results of the outer test case, if any, before a nested test case begins or is skipped.
void enter_suite(const suite &suite, bool displayed)
Notes the suite that begins, for the headings displayed later.
Definition reporter.cpp:409
static void format_test_case_totals(const test_case &test_case, char(&message)[totals_size_])
Formats the totals of a test case.
Definition reporter.cpp:970
virtual void pass_without_text(const test_case &test_case)=0
Reports a passed condition whose text is not written.
friend class micro_test_plus::runner
The runner, which lists the test cases.
Definition reporter.h:301
colours colours_
ANSI colour codes for output formatting.
Definition reporter.h:1284
bool suite_displayed_
Whether the heading of the current suite was displayed on the console.
Definition reporter.h:1410
void write_info(const std::vector< std::string > &arguments)
Writes the informational (non-result) lines to the console and to the output file.
Definition reporter.cpp:547
static void format_session_totals(const runner &runner, char(&message)[totals_size_])
Formats the totals of the test session.
Definition reporter.cpp:896
reporter(const runner_options &opts)
Constructor for the reporter class.
Definition reporter.cpp:162
static constexpr size_t indent_size_
Number of spaces per nesting level.
Definition reporter.h:1263
void flush_streams(void)
Flushes the standard output stream and the output file.
Definition reporter.cpp:622
virtual void output_pass_suffix(const test_case &test_case)=0
Outputs the suffix for a passing condition.
virtual void commit_buffer(void)=0
Writes the results buffered for the outer test case before a nested test case begins or is skipped.
void write_console_text(std::string_view text)
Writes a text to the console, unchanged.
Definition reporter.cpp:355
virtual void end_suite(const suite &suite)=0
Marks the end of a test suite.
friend class micro_test_plus::test_case
The test case, which creates the checks.
Definition reporter.h:284
void write_file(const char *format,...)
Writes formatted text to the output file.
Definition reporter.cpp:458
bool buffer_has_failures_
Whether the output buffer contains a failed check.
Definition reporter.h:1305
virtual const char * error_prefix(void) const noexcept
Returns the prefix of the lines that report an error.
Definition reporter.cpp:267
virtual void begin_test_case(const test_case &test_case)=0
Marks the beginning of a test case.
void write_line(bool to_console, const char *format,...)
Writes formatted lines to the output file and, when requested, to the console.
Definition reporter.cpp:485
static constexpr size_t totals_size_
Size of the array that receives the totals text.
Definition reporter.h:1083
std::string failures_
The lines of the failed checks, for the quiet verbosity.
Definition reporter.h:1316
void write_console(const char *format,...)
Writes formatted text to the console.
Definition reporter.cpp:444
std::string output_file_path_
Path of the optional output file.
Definition reporter.h:1454
static void format_elapsed_time(const stopwatch &stopwatch, char(&message)[elapsed_time_size_])
Formats the elapsed time of a suite or of the session.
Definition reporter.cpp:810
expression_formatter expression_
Expression formatter for pass and fail reporting.
Definition reporter.h:1423
virtual void begin_suite(const suite &suite)=0
Marks the beginning of a test suite.
const suite * current_suite_
The suite that has begun and not yet ended.
Definition reporter.h:1399
std::vector< open_test_case > open_test_cases_
The test cases that have begun and not yet ended, outermost first.
Definition reporter.h:1352
void pass(const std::string &message, const std::string &expression, const test_case &test_case)
Reports a passed condition.
Definition reporter.cpp:751
std::string_view console_lines(bool failed) const noexcept
Returns the buffered lines that the console displays when a test case or a suite ends,...
Definition reporter.cpp:332
virtual void output_fail_suffix(const reflection::source_location &location, check_kind kind, const test_case &test_case)=0
Outputs the suffix for a failing condition.
void display_open_test_cases(bool include_last)
Displays the headings not yet displayed, of the open test cases.
Definition reporter.cpp:382
friend class check_result
The check result, which formats the expressions.
Definition reporter.h:293
virtual void output_pass_prefix(const std::string &message, const test_case &test_case)=0
Outputs the prefix for a passing condition.
output_route route(record_kind kind) const noexcept
Tells where a kind of record is written.
Definition reporter.cpp:280
virtual void display_open_heading(const open_test_case &open, bool is_first)=0
Writes the heading of an open test case.
Measures the elapsed time of an operation.
Definition stopwatch.h:138
Local implementation of source location information for diagnostics.
Definition reflection.h:143
The configuration of a test session, and its command-line parser.
The test runner for the µTest++ framework.
Definition runner.h:187
A named, runnable test suite registered with the test runner.
Definition test.h:2059
A named, runnable test case that lives inside a suite.
Definition test.h:1362
C++ header file with declarations for the µTest++ internals.
C++ header file with declarations for the µTest++ expression formatter.
C++ header file with declarations for the µTest++ extension concepts.
verbosity
The verbosity levels for test reporting.
Internal implementation details for the µTest++ framework.
check_kind
The kind of a check, which determines what happens when it fails.
reporter & endl(reporter &stream)
Output stream manipulator for ending a line in test reports.
Definition reporter.cpp:218
indentation indent(size_t level)
Factory function that creates an indentation manipulator.
record_kind
The kinds of records written by the reporters.
Definition reporter.h:161
@ test_case_result
The result line of a test case.
Definition reporter.h:186
@ test_case_heading
The heading that marks the beginning of a test case.
Definition reporter.h:181
@ abort
The report of an aborted test execution.
Definition reporter.h:212
@ suite_heading
The heading that marks the beginning of a suite.
Definition reporter.h:171
@ info
The informational lines written at the beginning of the session.
Definition reporter.h:166
@ failed_check
The lines of a failed check, or the failure of a suite.
Definition reporter.h:202
@ skipped
The line of a suite or of a test case skipped by the filter.
Definition reporter.h:192
Primary namespace for the µTest++ testing framework.
C++ header file with declarations for the µTest++ reflection utilities.
C++ header file with inline implementations for the µTest++ test reporter.
C++ header file with declarations for the µTest++ runner options.
Colours used to highlight pass and fail results in test reports.
The details of a check, displayed when it fails.
Parameterised stream manipulator for outputting indentation.
Definition reporter.h:127
size_t level
Number of four-space indentation levels.
Definition reporter.h:134
bool console
Whether the record is written to the console.
Definition reporter.h:229
bool file
Whether the record is written to the output file.
Definition reporter.h:238
A test case that has begun and not yet ended.
Definition reporter.h:1325
bool name_displayed
Whether the heading of the test case was displayed.
Definition reporter.h:1342