micro-test-plus 4.1.0
µ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_TEST_PLUS_TEST_REPORTER_H_
49#define MICRO_TEST_PLUS_TEST_REPORTER_H_
50
51// ----------------------------------------------------------------------------
52
53#ifdef __cplusplus
54
55// ----------------------------------------------------------------------------
56
57#include <cstdio>
58#include <string_view>
59#include <string>
60#include <memory>
61#include <vector>
62#include <charconv>
63
64#include "type-traits.h"
65#include "detail.h"
67#include "reflection.h"
68
69// ----------------------------------------------------------------------------
70
71#if defined(__GNUC__)
72#pragma GCC diagnostic push
73#pragma GCC diagnostic ignored "-Wpadded"
74#pragma GCC diagnostic ignored "-Waggregate-return"
75#if defined(__clang__)
76#pragma clang diagnostic ignored "-Wc++98-compat"
77#pragma clang diagnostic ignored "-Wc++98-compat-pedantic"
78#endif
79#endif
80
81// =============================================================================
82
84{
85 // --------------------------------------------------------------------------
86
101 enum class verbosity
102 {
103 silent = 0,
104 quiet = 1,
105 normal = 2,
108 };
109
110 // Forward definitions.
111 class reporter;
112 class runner;
113 class suite;
114 class subtest;
115
123 reporter&
124 endl (reporter& stream);
125
126 namespace detail
127 {
128 // ------------------------------------------------------------------------
129
137 struct indent_t
138 {
139 size_t level;
140 };
141
142 // ------------------------------------------------------------------------
143 } // namespace detail
144
151 [[nodiscard]] detail::indent_t
152 indent (size_t level);
153
154 // ==========================================================================
155
186 {
187 public:
194 reporter (std::unique_ptr<std::vector<std::string_view>> argvs);
195
199 virtual ~reporter ();
200
207 reporter&
208 operator<< (std::string_view sv);
209
216 reporter&
217 operator<< (char c);
218
225 reporter&
226 operator<< (const char* s);
227
235 operator<< (bool v);
236
241 reporter& operator<< (std::nullptr_t);
242
251 template <class T>
252 requires std::is_arithmetic_v<T>
253 reporter&
254 operator<< (T v);
255
264 template <typename T>
265 reporter&
266 operator<< (T* v);
267
274 reporter&
275 operator<< (reporter& (*func) (reporter&));
276
277 // ------------------------------------------------------------------------
278 // Specific operators.
279
280 // ------------------------------------------------------------------------
281
290 void
291 endline (void);
292
306 void
308
317 void
318 flush (void);
319
320 // ------------------------------------------------------------------------
321
331 void
332 pass (std::string& message, const std::string& expression,
334
348 void
349 fail (bool abort, std::string& message, const std::string& expression,
350 bool has_expression, const reflection::source_location& location,
352
353 // ------------------------------------------------------------------------
354
362 virtual void
364 = 0;
365
373 virtual void
375 = 0;
376
384 virtual void
386 = 0;
387
395 virtual void
397 = 0;
398
406 virtual void
408 = 0;
409
417 virtual void
419 = 0;
420
428 virtual const char*
430 = 0;
431
439 auto
441
450 detail::expression_formatter&
451 expression ();
452
453 // ------------------------------------------------------------------------
454
455 protected:
462 [[nodiscard]] inline auto
463 colour_ (const bool cond) const;
464
465 void
467
476 void
477 write_info_ (void);
478
487 virtual void
488 output_pass_prefix_ (std::string& message, subtest& subtest)
489 = 0;
490
498 virtual void
500 = 0;
501
513 virtual void
514 output_fail_prefix_ (std::string& message, const bool has_expression,
515 const reflection::source_location& location,
517 = 0;
518
528 virtual void
529 output_fail_suffix_ (const reflection::source_location& location,
530 bool abort, subtest& subtest)
531 = 0;
532
533 protected:
538
542 detail::colours colours_{};
543
552 std::string buffer_{};
553
564
572 bool add_empty_line_{ true };
573
581 const char* output_file_path_{ nullptr };
582
591 FILE* output_file_{ nullptr };
592
596 std::unique_ptr<std::vector<std::string_view>> argvs_{};
597 };
598
599 // --------------------------------------------------------------------------
600} // namespace micro_os_plus::micro_test_plus
601
602#if defined(__GNUC__)
603#pragma GCC diagnostic pop
604#endif
605
606// ----------------------------------------------------------------------------
607
608#endif // __cplusplus
609
610// ============================================================================
611// Templates & constexpr implementations.
612
614
615// ----------------------------------------------------------------------------
616
617#endif // MICRO_TEST_PLUS_TEST_REPORTER_H_
618
619// ----------------------------------------------------------------------------
Formats values and expressions into an owned string buffer.
Local implementation of source location information for diagnostics.
Definition reflection.h:138
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:463
reporter(std::unique_ptr< std::vector< std::string_view > > argvs)
Constructor for the reporter class.
Definition reporter.cpp:79
std::unique_ptr< std::vector< std::string_view > > argvs_
Owns the command-line arguments passed to the test runner.
Definition reporter.h:596
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:167
void pass(std::string &message, const std::string &expression, subtest &subtest)
Report a passed condition.
Definition reporter.cpp:444
FILE * output_file_
Optional output file for redirecting test report output.
Definition reporter.h:591
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:237
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:572
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:563
std::string buffer_
Output accumulation buffer.
Definition reporter.h:552
void write_info_(void)
Appends informational (non-result) text to the output buffer.
Definition reporter.cpp:270
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:581
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:402
detail::colours colours_
ANSI colour codes for output formatting.
Definition reporter.h:542
void flush(void)
Flush the current buffered content.
Definition reporter.cpp:365
enum verbosity verbosity_
The verbosity level for test reporting.
Definition reporter.h:537
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:221
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:111
A named, runnable test case that lives inside a suite.
Definition test.h:542
A named, runnable test suite registered with the test runner.
Definition test.h:714
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:102
reporter & endl(reporter &stream)
Output stream manipulator for ending a line in test reports.
Definition reporter.cpp:206
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:138
size_t level
Number of four-space indentation levels.
Definition reporter.h:139
C++ header file with declarations for the µTest++ type trait utilities and metaprogramming support.