micro-test-plus 5.0.0
µTest++ Testing Framework
Loading...
Searching...
No Matches
expression-formatter-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
43
44#ifndef MICRO_TEST_PLUS_EXPRESSION_FORMATTER_INLINES_H_
45#define MICRO_TEST_PLUS_EXPRESSION_FORMATTER_INLINES_H_
46
47// ----------------------------------------------------------------------------
48
49#ifdef __cplusplus
50
51// ----------------------------------------------------------------------------
52
53#include <cstdio>
54
56
57// ----------------------------------------------------------------------------
58
59#if defined(__GNUC__)
60#pragma GCC diagnostic push
61#pragma GCC diagnostic ignored "-Waggregate-return"
62#if defined(__clang__)
63#pragma clang diagnostic ignored "-Wunknown-warning-option"
64#pragma clang diagnostic ignored "-Wc++98-compat"
65#pragma clang diagnostic ignored "-Wc++98-compat-pedantic"
66#endif
67#endif
68
69// ============================================================================
70
72{
73 // --------------------------------------------------------------------------
74
84
85 // --------------------------------------------------------------------------
86
92 inline auto
93 expression_formatter::colour_ (const bool cond) const
94 {
95 return cond ? colours_.pass : colours_.fail;
96 }
97
98 // --------------------------------------------------------------------------
99
104 inline const std::string&
106 {
107 return buffer_;
108 }
109
115 inline const char*
117 {
118 return buffer_.c_str ();
119 }
120
126 inline void
128 {
129 buffer_.clear ();
130 }
131
137 inline bool
139 {
140 return buffer_.empty ();
141 }
142
148 inline void
149 expression_formatter::append (size_t count, char ch)
150 {
151 buffer_.append (count, ch);
152 }
153
159 inline void
161 {
162 buffer_.reserve (capacity);
163 }
164
165 // --------------------------------------------------------------------------
166
167#if defined(__GNUC__)
168#pragma GCC diagnostic push
169#if defined(__clang__)
170#pragma clang diagnostic ignored "-Wunsafe-buffer-usage"
171#pragma clang diagnostic ignored "-Wunsafe-buffer-usage-in-libc-call"
172#endif
173#endif
174
175 // --------------------------------------------------------------------------
176
184 template <typename T>
187 {
188 if (v == nullptr)
189 {
190 buffer_.append ("0x0");
191 return *this;
192 }
193#if defined(__GNUC__)
194#pragma GCC diagnostic push
195#if defined(__clang__)
196#pragma clang diagnostic ignored "-Wunsafe-buffer-usage-in-libc-call"
197#endif
198#endif
199 char buff[20];
200 snprintf (buff, sizeof (buff), "%p", reinterpret_cast<void*> (v));
201 buffer_.append (buff);
202#if defined(__GNUC__)
203#pragma GCC diagnostic pop
204#endif
205 return *this;
206 }
207
214 template <class T>
215 requires type_traits::is_op<T>
218 {
219 *this << detail::get (t);
220 return *this;
221 }
222
228 template <class T>
232 {
233 detail::append_number_ (buffer_, static_cast<long long> (v.get ()));
234 return *this;
235 }
236
243 template <class T>
246 expression_formatter::operator<< (const T& t)
247 {
248 *this << '{';
249 auto first = true;
250 for (const auto& arg : t)
251 {
252 *this << (first ? "" : ", ") << arg;
253 first = false;
254 }
255 *this << '}';
256 return *this;
257 }
258
259 // --------------------------------------------------------------------------
260 // Compound expression operators (without colour codes).
261
273 template <class Lhs_T, class Rhs_T>
276 {
277 return (*this << colour_ (op) << op.lhs () << " == " << op.rhs ()
278 << colours_.none);
279 }
280
293 template <class Lhs_T, class Rhs_T>
296 {
297 return (*this << colour_ (op) << op.lhs () << " != " << op.rhs ()
298 << colours_.none);
299 }
300
313 template <class Lhs_T, class Rhs_T>
316 {
317 return (*this << colour_ (op) << op.lhs () << " > " << op.rhs ()
318 << colours_.none);
319 }
320
334 template <class Lhs_T, class Rhs_T>
337 {
338 return (*this << colour_ (op) << op.lhs () << " >= " << op.rhs ()
339 << colours_.none);
340 }
341
353 template <class Lhs_T, class Rhs_T>
356 {
357 return (*this << colour_ (op) << op.lhs () << " < " << op.rhs ()
358 << colours_.none);
359 }
360
374 template <class Lhs_T, class Rhs_T>
377 {
378 return (*this << colour_ (op) << op.lhs () << " <= " << op.rhs ()
379 << colours_.none);
380 }
381
394 template <class Lhs_T, class Rhs_T>
397 {
398 return (*this << '(' << op.lhs () << colour_ (op) << " and "
399 << colours_.none << op.rhs () << ')');
400 }
401
414 template <class Lhs_T, class Rhs_T>
417 {
418 return (*this << '(' << op.lhs () << colour_ (op) << " or "
419 << colours_.none << op.rhs () << ')');
420 }
421
430 template <class T>
433 {
434 return (*this << colour_ (op) << "not " << op.operand () << colours_.none);
435 }
436
437#if defined(__cpp_exceptions)
450 template <class Callable_T, class Exception_T>
454 {
455 return (*this << colour_ (op) << "throws<"
457 << colours_.none);
458 }
459
470 template <class Callable_T>
474 {
475 return (*this << colour_ (op) << "throws" << colours_.none);
476 }
477
488 template <class Callable_T>
491 {
492 return (*this << colour_ (op) << "nothrow" << colours_.none);
493 }
494#endif
495
496 // --------------------------------------------------------------------------
497} // namespace micro_os_plus::micro_test_plus::detail
498
499#if defined(__GNUC__)
500#pragma GCC diagnostic pop
501#endif
502
503// ----------------------------------------------------------------------------
504
505#endif // __cplusplus
506
507// ----------------------------------------------------------------------------
508
509#endif // MICRO_TEST_PLUS_EXPRESSION_FORMATTER_INLINES_H_
510
511// ----------------------------------------------------------------------------
Formats values and expressions into an owned string buffer.
void append(size_t count, char ch)
Appends a sequence of identical characters to the buffer.
auto colour_(const bool cond) const
Selects the appropriate colour code based on a condition.
const char * c_str() const noexcept
Returns a pointer to the null-terminated buffer contents.
bool empty() const noexcept
Returns whether the internal buffer is empty.
expression_formatter & operator<<(std::string_view sv)
Appends a string view to the buffer.
expression_formatter(colours &colours) noexcept
Constructor with colour configuration.
const std::string & str() const noexcept
Returns a const reference to the internal buffer.
void reserve(size_t capacity)
Reserves storage in the internal buffer.
colours & colours_
ANSI colour codes for output formatting.
C++20 concept satisfied when T provides both begin() and end() member functions.
C++20 concept satisfied when T provides a npos member.
C++20 concept satisfied when a type derives from op.
Internal implementation details for the µTest++ framework.
void append_number_(std::string &buffer, T v)
Appends the string representation of a numeric value to a buffer, using std::to_chars for allocation-...
constexpr auto get(const T &t)
Generic getter function template for value retrieval.
constexpr auto type_name(void) -> std::string_view
Extract the type name from the __PRETTY_FUNCTION__ macro.
C++ header file with declarations for the µTest++ reflection utilities.
Logical AND comparator struct template.
Definition detail.h:503
constexpr auto lhs(void) const
Retrieves the left-hand operand.
constexpr auto rhs(void) const
Retrieves the right-hand operand.
Colours used to highlight pass and fail results in test reports.
Equality comparator struct template.
Definition detail.h:306
Greater than or equal comparator struct template.
Definition detail.h:403
Greater than comparator struct template.
Definition detail.h:370
Less than or equal comparator struct template.
Definition detail.h:469
Less than comparator struct template.
Definition detail.h:436
Non-equality comparator struct template.
Definition detail.h:338
Logical NOT comparator struct template.
Definition detail.h:567
Operator struct template to check if an expression does not throw any exception.
Definition detail.h:702
Logical OR comparator struct template.
Definition detail.h:536
Operator struct template to check if an expression throws a specific exception.
Definition detail.h:645
constexpr auto operand() const
Retrieves the wrapped operand expression.
Struct template representing a genuine integral value.
constexpr T get(void) const noexcept
Getter for the stored value.