Skip to main content

deferred_reporter_base Class

Base class for a deferred reporter that collects messages into a string. More...

Declaration

class micro_os_plus::micro_test_plus::detail::deferred_reporter_base { ... }

Included Headers

Derived Classes

classdeferred_reporter

Deferred reporter class for a specific expression. More...

Public Constructors Index

deferred_reporter_base (bool value, const reflection::source_location &location, subtest &subtest)

Constructs a deferred reporter base. More...

deferred_reporter_base (const deferred_reporter_base &)=delete

Deleted copy constructor to prevent copying. More...

deferred_reporter_base (deferred_reporter_base &&)=delete

Deleted move constructor to prevent moving. More...

Public Destructor Index

~deferred_reporter_base ()

Destructor for the deferred reporter base. More...

Public Operators Index

template <class T>
auto &operator<< (const T &msg)

Appends a message to the reporter. More...

deferred_reporter_base &operator= (const deferred_reporter_base &)=delete

Deleted copy assignment operator to prevent copying. More...

deferred_reporter_base &operator= (deferred_reporter_base &&)=delete

Deleted move assignment operator to prevent moving. More...

Public Member Functions Index

boolvalue () const

Retrieves the result value. More...

Protected Member Attributes Index

boolabort_ = false

Indicates whether the reporting should abort further processing. More...

std::stringdeferred_output_ {}

String to collect the expectation message passed via operator<<(). More...

boolhas_expression_ = false

Indicates whether the reporter has an associated expression. More...

const reflection::source_locationlocation_ {}

Stores the source location associated with the report. More...

subtest &subtest_

Reference to the test case invoking this report. More...

boolvalue_ {}

Stores the result value of the report. More...

Description

Base class for a deferred reporter that collects messages into a string.

The deferred_reporter_base class serves as the foundational component for deferred reporting within the framework. It is responsible for collecting expectation messages, typically passed via the operator<<(), into a string for later reporting.

This class maintains the result value, abort status, and the source location associated with the report. It is intended exclusively for internal use and is implemented in the include/micro-os-plus/micro-test-plus folder to ensure a structured and modular codebase.

Definition at line 109 of file deferred-reporter.h.

Public Constructors

deferred_reporter_base()

micro_os_plus::micro_test_plus::detail::deferred_reporter_base::deferred_reporter_base (bool value, const reflection::source_location & location, subtest & subtest)

Constructs a deferred reporter base.

Parameters
value

The result value associated with the report.

location

The source location relevant to the report.

subtest

The subtest that owns this deferred report.

Stores the evaluated Boolean value, the location identifying the source line of the assertion, and a reference to the owning subtest. The subtest's check index counter is incremented immediately so that the first check is reported as check #1.

Declaration at line 119 of file deferred-reporter.h, definition at line 75 of file deferred-reporter.cpp.

76 bool value, const reflection::source_location& location,
78 : value_{ value }, location_{ location }, subtest_{ subtest }
79 {
80 // The index starts at 0, must be incremented before the first check is
81 // reported, to ensure that the first check is reported as check #1.
82 subtest_.increment_subtest_index ();
83 }

References location_, subtest_, value and value_.

Referenced by micro_os_plus::micro_test_plus::detail::deferred_reporter::deferred_reporter, deferred_reporter_base, deferred_reporter_base, operator= and operator=.

deferred_reporter_base()

micro_os_plus::micro_test_plus::detail::deferred_reporter_base::deferred_reporter_base (const deferred_reporter_base &)
delete

Deleted copy constructor to prevent copying.

Definition at line 126 of file deferred-reporter.h.

Reference deferred_reporter_base.

deferred_reporter_base()

micro_os_plus::micro_test_plus::detail::deferred_reporter_base::deferred_reporter_base (deferred_reporter_base &&)
delete

Deleted move constructor to prevent moving.

Definition at line 131 of file deferred-reporter.h.

Reference deferred_reporter_base.

Public Destructor

~deferred_reporter_base()

micro_os_plus::micro_test_plus::detail::deferred_reporter_base::~deferred_reporter_base ()

Destructor for the deferred reporter base.

The destructor finalises the deferred reporting process for a test expression. If the evaluated expression is true, the reporter records a successful outcome along with any accumulated message. If the expression is false, the reporter records a failure, including the abort status, message, and source location for comprehensive reporting.

This mechanism ensures that all relevant information about the test outcome is captured and reported accurately when the deferred reporter goes out of scope.

The destructor ensures that if an abort condition is set and the test expression has failed, the test output is flushed and the process is terminated. This mechanism guarantees immediate feedback and halts further execution upon critical test failures, aiding in rapid identification and resolution of issues during test runs.

Declaration at line 150 of file deferred-reporter.h, definition at line 103 of file deferred-reporter.cpp.

104 {
105#if defined(MICRO_OS_PLUS_MICRO_TEST_PLUS_TRACE_CONSTRUCTORS_ENABLED)
106 trace::printf ("%s\n", __PRETTY_FUNCTION__);
107#endif // MICRO_OS_PLUS_MICRO_TEST_PLUS_TRACE_CONSTRUCTORS_ENABLED
108
109 auto& expression_str = subtest_.reporter ().expression ().str ();
110
111 if (value_) [[likely]]
112 {
113 subtest_.reporter ().pass (deferred_output_, expression_str,
114 subtest_);
115 subtest_.totals ().increment_successful_checks ();
116 }
117 else
118 {
119 subtest_.reporter ().fail (abort_, deferred_output_, expression_str,
121 subtest_.totals ().increment_failed_checks ();
122 }
123
124 if (abort_ && !value_) [[unlikely]]
125 {
126 subtest_.reporter ().write_buffer_to_stdout ();
127 subtest_.reporter ().flush ();
128 subtest_.abort (location_);
129 }
130 }

References abort_, deferred_output_, has_expression_, location_, subtest_ and value_.

Public Operators

operator<<()

template <class T>
auto & micro_os_plus::micro_test_plus::detail::deferred_reporter_base::operator<< (const T & msg)

Appends a message to the reporter.

Template Parameters
T

The type of the message to append.

Parameters
msg

The message to append.

Returns

Reference to the current reporter instance.

This operator overload enables the deferred reporter to accumulate expectation messages by appending the provided value to the internal message string.

If the argument is a char, it is appended directly as a character. If the argument is of another arithmetic type, it is converted to a string using a fixed-size buffer and std::to_chars to avoid dynamic memory allocation. For all other types, the value is appended directly.

Declaration at line 162 of file deferred-reporter.h, definition at line 109 of file deferred-reporter-inlines.h.

110 {
111 if constexpr (std::is_same_v<T, char>)
112 {
113 deferred_output_.push_back (msg);
114 }
115 else if constexpr (std::is_arithmetic_v<T>)
116 {
117 // Optimise to avoid dynamic memory allocation in std::to_string by
118 // using a fixed-size buffer and std::to_chars.
119#if defined(__GNUC__)
120#pragma GCC diagnostic push
121
122#if defined(__clang__)
123#pragma clang diagnostic ignored "-Wunsafe-buffer-usage"
124#endif // defined(__clang__)
125#endif // defined(__GNUC__)
126
127 char buf[64];
128 // 64 bytes is sufficient for the longest decimal representation
129 // of any standard arithmetic type (long double ~45 chars).
130 static_assert (sizeof (buf) >= 50,
131 "buf must be large enough for any arithmetic type");
132 auto [ptr, ec] = std::to_chars (buf, buf + sizeof (buf), msg);
133 if (ec == std::errc{})
134 deferred_output_.append (buf, ptr);
135
136#if defined(__GNUC__)
137#pragma GCC diagnostic pop
138#endif // defined(__GNUC__)
139 }
140 else
141 {
142 deferred_output_.append (msg);
143 }
144 return *this;
145 }

Reference deferred_output_.

operator=()

deferred_reporter_base & micro_os_plus::micro_test_plus::detail::deferred_reporter_base::operator= (const deferred_reporter_base &)
delete

Deleted copy assignment operator to prevent copying.

Definition at line 137 of file deferred-reporter.h.

Reference deferred_reporter_base.

operator=()

deferred_reporter_base & micro_os_plus::micro_test_plus::detail::deferred_reporter_base::operator= (deferred_reporter_base &&)
delete

Deleted move assignment operator to prevent moving.

Definition at line 144 of file deferred-reporter.h.

Reference deferred_reporter_base.

Public Member Functions

value()

bool micro_os_plus::micro_test_plus::detail::deferred_reporter_base::value ()
inline nodiscard

Retrieves the result value.

Parameters

None.

Return Values
true

The reported condition was met.

false

The reported condition was not met.

Returns the result value stored in value_.

Declaration at line 174 of file deferred-reporter.h, definition at line 88 of file deferred-reporter-inlines.h.

89 {
90 return value_;
91 }

Reference value_.

Referenced by deferred_reporter_base.

Protected Member Attributes

abort_

bool micro_os_plus::micro_test_plus::detail::deferred_reporter_base::abort_ = false
protected

Indicates whether the reporting should abort further processing.

Definition at line 186 of file deferred-reporter.h.

186 bool abort_ = false;

Referenced by micro_os_plus::micro_test_plus::detail::deferred_reporter::deferred_reporter and ~deferred_reporter_base.

deferred_output_

std::string micro_os_plus::micro_test_plus::detail::deferred_reporter_base::deferred_output_ {}
protected

String to collect the expectation message passed via operator<<().

Definition at line 202 of file deferred-reporter.h.

202 std::string deferred_output_{};

Referenced by ~deferred_reporter_base and operator<<.

has_expression_

bool micro_os_plus::micro_test_plus::detail::deferred_reporter_base::has_expression_ = false
protected

Indicates whether the reporter has an associated expression.

Definition at line 191 of file deferred-reporter.h.

191 bool has_expression_ = false;

Referenced by micro_os_plus::micro_test_plus::detail::deferred_reporter::deferred_reporter and ~deferred_reporter_base.

location_

const reflection::source_location micro_os_plus::micro_test_plus::detail::deferred_reporter_base::location_ {}
protected

Stores the source location associated with the report.

Definition at line 196 of file deferred-reporter.h.

Referenced by deferred_reporter_base and ~deferred_reporter_base.

subtest_

subtest& micro_os_plus::micro_test_plus::detail::deferred_reporter_base::subtest_
protected

Reference to the test case invoking this report.

Definition at line 207 of file deferred-reporter.h.

Referenced by deferred_reporter_base and ~deferred_reporter_base.

value_

bool micro_os_plus::micro_test_plus::detail::deferred_reporter_base::value_ {}
protected

Stores the result value of the report.

Definition at line 180 of file deferred-reporter.h.

180 bool value_{};

Referenced by deferred_reporter_base, ~deferred_reporter_base and value.


The documentation for this class was generated from the following files:


Generated via doxygen2docusaurus 2.2.2 by Doxygen 1.17.0.