micro-test-plus 5.0.0
µ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_TEST_PLUS_DEFERRED_REPORTER_INLINES_H_
46#define MICRO_TEST_PLUS_DEFERRED_REPORTER_INLINES_H_
47
48// ----------------------------------------------------------------------------
49
50#ifdef __cplusplus
51
52// ----------------------------------------------------------------------------
53
54#include <charconv>
55#include <cstdio>
56
57#include <micro-os-plus/diag/trace.h>
58
59// ----------------------------------------------------------------------------
60
61#if defined(__GNUC__)
62#pragma GCC diagnostic push
63#pragma GCC diagnostic ignored "-Waggregate-return"
64#if defined(__clang__)
65#pragma clang diagnostic ignored "-Wc++98-compat"
66#pragma clang diagnostic ignored "-Wc++98-compat-pedantic"
67#endif
68#endif
69
70// ============================================================================
71
73{
74 // --------------------------------------------------------------------------
75
76 namespace detail
77 {
78 // ========================================================================
79
84 inline bool
86 {
87 return value_;
88 }
89
90 // ------------------------------------------------------------------------
91
103 template <class T>
105 auto&
107 {
108 if constexpr (std::is_same_v<T, char>)
109 {
110 deferred_output_.push_back (msg);
111 }
112 else if constexpr (std::is_arithmetic_v<T>)
113 {
114 // Optimise to avoid dynamic memory allocation in std::to_string by
115 // using a fixed-size buffer and std::to_chars.
116#if defined(__GNUC__)
117#pragma GCC diagnostic push
118#if defined(__clang__)
119#pragma clang diagnostic ignored "-Wunsafe-buffer-usage"
120#endif
121#endif
122 char buf[64];
123 // 64 bytes is sufficient for the longest decimal representation
124 // of any standard arithmetic type (long double ~45 chars).
125 static_assert (sizeof (buf) >= 50,
126 "buf must be large enough for any arithmetic type");
127 auto [ptr, ec] = std::to_chars (buf, buf + sizeof (buf), msg);
128 if (ec == std::errc{})
129 deferred_output_.append (buf, ptr);
130#if defined(__GNUC__)
131#pragma GCC diagnostic pop
132#endif
133 }
134 else
135 {
136 deferred_output_.append (msg);
137 }
138 return *this;
139 }
140
141 // ------------------------------------------------------------------------
142
154 template <class Expr_T>
156 const Expr_T& expr, bool abort,
158 expression_formatter& expression)
159 : deferred_reporter_base{ static_cast<bool> (expr), location, subtest }
160
161 {
162#if defined(MICRO_OS_PLUS_MICRO_TEST_PLUS_TRACE_CONSTRUCTORS_ENABLED)
163 trace::printf ("%s\n", __PRETTY_FUNCTION__);
164#endif // MICRO_OS_PLUS_MICRO_TEST_PLUS_TRACE_CONSTRUCTORS_ENABLED
165 abort_ = abort;
167
168 expression.clear ();
169
170 expression << expr;
171 }
172
173 // ------------------------------------------------------------------------
174 } // namespace detail
175
176 // --------------------------------------------------------------------------
177} // namespace micro_os_plus::micro_test_plus
178
179#if defined(__GNUC__)
180#pragma GCC diagnostic pop
181#endif
182
183// ----------------------------------------------------------------------------
184
185#endif // __cplusplus
186
187// ----------------------------------------------------------------------------
188
189#endif // MICRO_TEST_PLUS_DEFERRED_REPORTER_INLINES_H_
190
191// ----------------------------------------------------------------------------
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:138
A named, runnable test case that lives inside a suite.
Definition test.h:542
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.