micro-test-plus 5.0.1
µ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_OS_PLUS_MICRO_TEST_PLUS_INLINES_EXPRESSION_FORMATTER_INLINES_H_
45#define MICRO_OS_PLUS_MICRO_TEST_PLUS_INLINES_EXPRESSION_FORMATTER_INLINES_H_
46
47// ----------------------------------------------------------------------------
48
49#if defined(__cplusplus)
50
51// ----------------------------------------------------------------------------
52
54
55#include <cstdio>
56
57// ----------------------------------------------------------------------------
58
59#if defined(__GNUC__)
60#pragma GCC diagnostic push
61
62#pragma GCC diagnostic ignored "-Waggregate-return"
63#if defined(__clang__)
64#pragma clang diagnostic ignored "-Wunknown-warning-option"
65#pragma clang diagnostic ignored "-Wc++98-compat"
66#pragma clang diagnostic ignored "-Wc++98-compat-pedantic"
67#endif // defined(__clang__)
68#endif // defined(__GNUC__)
69
70// ============================================================================
71
73{
74 // --------------------------------------------------------------------------
75
85
86 // --------------------------------------------------------------------------
87
93 inline auto
94 expression_formatter::colour_ (const bool cond) const
95 {
96 return cond ? colours_.pass : colours_.fail;
97 }
98
99 // --------------------------------------------------------------------------
100
105 inline const std::string&
107 {
108 return buffer_;
109 }
110
116 inline const char*
118 {
119 return buffer_.c_str ();
120 }
121
127 inline void
129 {
130 buffer_.clear ();
131 }
132
138 inline bool
140 {
141 return buffer_.empty ();
142 }
143
149 inline void
150 expression_formatter::append (size_t count, char ch)
151 {
152 buffer_.append (count, ch);
153 }
154
160 inline void
162 {
163 buffer_.reserve (capacity);
164 }
165
166 // --------------------------------------------------------------------------
167
175 template <typename T>
178 {
179 if (v == nullptr)
180 {
181 buffer_.append ("0x0");
182 return *this;
183 }
184
185#if defined(__GNUC__)
186#pragma GCC diagnostic push
187
188#if defined(__clang__)
189#pragma clang diagnostic ignored "-Wunsafe-buffer-usage-in-libc-call"
190#endif // defined(__clang__)
191#endif // defined(__GNUC__)
192
193 char buff[20];
194 snprintf (buff, sizeof (buff), "%p", reinterpret_cast<void*> (v));
195 buffer_.append (buff);
196
197#if defined(__GNUC__)
198#pragma GCC diagnostic pop
199#endif // defined(__GNUC__)
200
201 return *this;
202 }
203
210 template <class T>
211 requires type_traits::is_op<T>
214 {
215 *this << detail::get (t);
216 return *this;
217 }
218
224 template <class T>
228 {
229 detail::append_number_ (buffer_, static_cast<long long> (v.get ()));
230 return *this;
231 }
232
239 template <class T>
242 expression_formatter::operator<< (const T& t)
243 {
244 *this << '{';
245 auto first = true;
246 for (const auto& arg : t)
247 {
248 *this << (first ? "" : ", ") << arg;
249 first = false;
250 }
251 *this << '}';
252 return *this;
253 }
254
255 // --------------------------------------------------------------------------
256 // Compound expression operators (without colour codes).
257
269 template <class Lhs_T, class Rhs_T>
272 {
273 return (*this << colour_ (op) << op.lhs () << " == " << op.rhs ()
274 << colours_.none);
275 }
276
289 template <class Lhs_T, class Rhs_T>
292 {
293 return (*this << colour_ (op) << op.lhs () << " != " << op.rhs ()
294 << colours_.none);
295 }
296
309 template <class Lhs_T, class Rhs_T>
312 {
313 return (*this << colour_ (op) << op.lhs () << " > " << op.rhs ()
314 << colours_.none);
315 }
316
330 template <class Lhs_T, class Rhs_T>
333 {
334 return (*this << colour_ (op) << op.lhs () << " >= " << op.rhs ()
335 << colours_.none);
336 }
337
349 template <class Lhs_T, class Rhs_T>
352 {
353 return (*this << colour_ (op) << op.lhs () << " < " << op.rhs ()
354 << colours_.none);
355 }
356
370 template <class Lhs_T, class Rhs_T>
373 {
374 return (*this << colour_ (op) << op.lhs () << " <= " << op.rhs ()
375 << colours_.none);
376 }
377
390 template <class Lhs_T, class Rhs_T>
393 {
394 return (*this << '(' << op.lhs () << colour_ (op) << " and "
395 << colours_.none << op.rhs () << ')');
396 }
397
410 template <class Lhs_T, class Rhs_T>
413 {
414 return (*this << '(' << op.lhs () << colour_ (op) << " or "
415 << colours_.none << op.rhs () << ')');
416 }
417
426 template <class T>
429 {
430 return (*this << colour_ (op) << "not " << op.operand () << colours_.none);
431 }
432
433#if defined(__cpp_exceptions)
446 template <class Callable_T, class Exception_T>
450 {
451 return (*this << colour_ (op) << "throws<"
453 << colours_.none);
454 }
455
466 template <class Callable_T>
470 {
471 return (*this << colour_ (op) << "throws" << colours_.none);
472 }
473
484 template <class Callable_T>
487 {
488 return (*this << colour_ (op) << "nothrow" << colours_.none);
489 }
490#endif // defined(__cpp_exceptions)
491
492 // --------------------------------------------------------------------------
493} // namespace micro_os_plus::micro_test_plus::detail
494
495#if defined(__GNUC__)
496#pragma GCC diagnostic pop
497#endif // defined(__GNUC__)
498
499// ----------------------------------------------------------------------------
500
501#endif // defined(__cplusplus)
502
503// ----------------------------------------------------------------------------
504
505#endif // MICRO_OS_PLUS_MICRO_TEST_PLUS_INLINES_EXPRESSION_FORMATTER_INLINES_H_
506
507// ----------------------------------------------------------------------------
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:512
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:315
Greater than or equal comparator struct template.
Definition detail.h:412
Greater than comparator struct template.
Definition detail.h:379
Less than or equal comparator struct template.
Definition detail.h:478
Less than comparator struct template.
Definition detail.h:445
Non-equality comparator struct template.
Definition detail.h:347
Logical NOT comparator struct template.
Definition detail.h:576
Operator struct template to check if an expression does not throw any exception.
Definition detail.h:711
Logical OR comparator struct template.
Definition detail.h:545
Operator struct template to check if an expression throws a specific exception.
Definition detail.h:654
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.