micro-test-plus 4.1.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 "-Wc++98-compat"
64#pragma clang diagnostic ignored "-Wc++98-compat-pedantic"
65#endif
66#endif
67
68// ============================================================================
69
71{
72 // --------------------------------------------------------------------------
73
83
84 // --------------------------------------------------------------------------
85
91 inline auto
92 expression_formatter::colour_ (const bool cond) const
93 {
94 return cond ? colours_.pass : colours_.fail;
95 }
96
97 // --------------------------------------------------------------------------
98
103 inline const std::string&
105 {
106 return buffer_;
107 }
108
114 inline const char*
116 {
117 return buffer_.c_str ();
118 }
119
125 inline void
127 {
128 buffer_.clear ();
129 }
130
136 inline bool
138 {
139 return buffer_.empty ();
140 }
141
147 inline void
148 expression_formatter::append (size_t count, char ch)
149 {
150 buffer_.append (count, ch);
151 }
152
158 inline void
160 {
161 buffer_.reserve (capacity);
162 }
163
164 // --------------------------------------------------------------------------
165
166#if defined(__GNUC__)
167#pragma GCC diagnostic push
168#if defined(__clang__)
169#pragma clang diagnostic ignored "-Wunsafe-buffer-usage"
170#pragma clang diagnostic ignored "-Wunsafe-buffer-usage-in-libc-call"
171#endif
172#endif
173
174 // --------------------------------------------------------------------------
175
183 template <typename T>
186 {
187 if (v == nullptr)
188 {
189 buffer_.append ("0x0");
190 return *this;
191 }
192#if defined(__GNUC__)
193#pragma GCC diagnostic push
194#if defined(__clang__)
195#pragma clang diagnostic ignored "-Wunsafe-buffer-usage-in-libc-call"
196#endif
197#endif
198 char buff[20];
199 snprintf (buff, sizeof (buff), "%p", reinterpret_cast<void*> (v));
200 buffer_.append (buff);
201#if defined(__GNUC__)
202#pragma GCC diagnostic pop
203#endif
204 return *this;
205 }
206
213 template <class T>
214 requires type_traits::is_op<T>
217 {
218 *this << detail::get (t);
219 return *this;
220 }
221
227 template <class T>
231 {
232 detail::append_number_ (buffer_, static_cast<long long> (v.get ()));
233 return *this;
234 }
235
242 template <class T>
245 expression_formatter::operator<< (const T& t)
246 {
247 *this << '{';
248 auto first = true;
249 for (const auto& arg : t)
250 {
251 *this << (first ? "" : ", ") << arg;
252 first = false;
253 }
254 *this << '}';
255 return *this;
256 }
257
258 // --------------------------------------------------------------------------
259 // Compound expression operators (without colour codes).
260
272 template <class Lhs_T, class Rhs_T>
275 {
276 return (*this << colour_ (op) << op.lhs () << " == " << op.rhs ()
277 << colours_.none);
278 }
279
292 template <class Lhs_T, class Rhs_T>
295 {
296 return (*this << colour_ (op) << op.lhs () << " != " << op.rhs ()
297 << colours_.none);
298 }
299
312 template <class Lhs_T, class Rhs_T>
315 {
316 return (*this << colour_ (op) << op.lhs () << " > " << op.rhs ()
317 << colours_.none);
318 }
319
333 template <class Lhs_T, class Rhs_T>
336 {
337 return (*this << colour_ (op) << op.lhs () << " >= " << op.rhs ()
338 << colours_.none);
339 }
340
352 template <class Lhs_T, class Rhs_T>
355 {
356 return (*this << colour_ (op) << op.lhs () << " < " << op.rhs ()
357 << colours_.none);
358 }
359
373 template <class Lhs_T, class Rhs_T>
376 {
377 return (*this << colour_ (op) << op.lhs () << " <= " << op.rhs ()
378 << colours_.none);
379 }
380
393 template <class Lhs_T, class Rhs_T>
396 {
397 return (*this << '(' << op.lhs () << colour_ (op) << " and "
398 << colours_.none << op.rhs () << ')');
399 }
400
413 template <class Lhs_T, class Rhs_T>
416 {
417 return (*this << '(' << op.lhs () << colour_ (op) << " or "
418 << colours_.none << op.rhs () << ')');
419 }
420
429 template <class T>
432 {
433 return (*this << colour_ (op) << "not " << op.operand () << colours_.none);
434 }
435
436#if defined(__cpp_exceptions)
449 template <class Callable_T, class Exception_T>
453 {
454 return (*this << colour_ (op) << "throws<"
456 << colours_.none);
457 }
458
469 template <class Callable_T>
473 {
474 return (*this << colour_ (op) << "throws" << colours_.none);
475 }
476
487 template <class Callable_T>
490 {
491 return (*this << colour_ (op) << "nothrow" << colours_.none);
492 }
493#endif
494
495 // --------------------------------------------------------------------------
496} // namespace micro_os_plus::micro_test_plus::detail
497
498#if defined(__GNUC__)
499#pragma GCC diagnostic pop
500#endif
501
502// ----------------------------------------------------------------------------
503
504#endif // __cplusplus
505
506// ----------------------------------------------------------------------------
507
508#endif // MICRO_TEST_PLUS_EXPRESSION_FORMATTER_INLINES_H_
509
510// ----------------------------------------------------------------------------
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.