micro-test-plus 5.0.1
µTest++ Testing Framework
Loading...
Searching...
No Matches
deferred-reporter-inlines.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
44
45#ifndef MICRO_OS_PLUS_MICRO_TEST_PLUS_INLINES_DEFERRED_REPORTER_INLINES_H_
46#define MICRO_OS_PLUS_MICRO_TEST_PLUS_INLINES_DEFERRED_REPORTER_INLINES_H_
47
48// ----------------------------------------------------------------------------
49
50#if defined(__cplusplus)
51
52// ----------------------------------------------------------------------------
53
54#if __has_include("micro-os-plus/diag/trace.h")
55#include "micro-os-plus/diag/trace.h"
56#endif // __has_include("micro-os-plus/diag/trace.h")
57
58#include <charconv>
59#include <cstdio>
60
61// ----------------------------------------------------------------------------
62
63#if defined(__GNUC__)
64#pragma GCC diagnostic push
65
66#pragma GCC diagnostic ignored "-Waggregate-return"
67#if defined(__clang__)
68#pragma clang diagnostic ignored "-Wc++98-compat"
69#pragma clang diagnostic ignored "-Wc++98-compat-pedantic"
70#endif // defined(__clang__)
71#endif // defined(__GNUC__)
72
73// ============================================================================
74
76{
77 // --------------------------------------------------------------------------
78
79 namespace detail
80 {
81 // ========================================================================
82
87 inline bool
89 {
90 return value_;
91 }
92
93 // ------------------------------------------------------------------------
94
106 template <class T>
108 auto&
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 }
146
147 // ------------------------------------------------------------------------
148
160 template <class Expr_T>
162 const Expr_T& expr, bool abort,
164 expression_formatter& expression)
165 : deferred_reporter_base{ static_cast<bool> (expr), location, subtest }
166
167 {
168#if defined(MICRO_OS_PLUS_MICRO_TEST_PLUS_TRACE_CONSTRUCTORS_ENABLED)
169 trace::printf ("%s\n", __PRETTY_FUNCTION__);
170#endif // MICRO_OS_PLUS_MICRO_TEST_PLUS_TRACE_CONSTRUCTORS_ENABLED
171 abort_ = abort;
173
174 expression.clear ();
175
176 expression << expr;
177 }
178
179 // ------------------------------------------------------------------------
180 } // namespace detail
181
182 // --------------------------------------------------------------------------
183} // namespace micro_os_plus::micro_test_plus
184
185#if defined(__GNUC__)
186#pragma GCC diagnostic pop
187#endif // defined(__GNUC__)
188
189// ----------------------------------------------------------------------------
190
191#endif // defined(__cplusplus)
192
193// ----------------------------------------------------------------------------
194
195#endif // MICRO_OS_PLUS_MICRO_TEST_PLUS_INLINES_DEFERRED_REPORTER_INLINES_H_
196
197// ----------------------------------------------------------------------------
std::string deferred_output_
String to collect the expectation message passed via operator<<().
auto & operator<<(const T &msg)
Appends a message to the reporter.
bool has_expression_
Indicates whether the reporter has an associated expression.
bool abort_
Indicates whether the reporting should abort further processing.
deferred_reporter_base(bool value, const reflection::source_location &location, subtest &subtest)
Constructs a deferred reporter base.
deferred_reporter(const Expr_T &expr, bool abort, const reflection::source_location &location, subtest &subtest, expression_formatter &expression)
Constructs a deferred reporter for a specific expression.
Formats values and expressions into an owned string buffer.
Local implementation of source location information for diagnostics.
Definition reflection.h:150
A named, runnable test case that lives inside a suite.
Definition test.h:551
C++20 concept satisfied when a type derives from op.
C++20 concept satisfied when a type can be appended to the deferred reporter's output via operator<<.
Internal implementation details for the µTest++ framework.
Primary namespace for the µTest++ testing framework.