micro-test-plus 4.1.0
µTest++ Testing Framework
Loading...
Searching...
No Matches
expression-formatter.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
46
47#ifndef MICRO_TEST_PLUS_EXPRESSION_FORMATTER_H_
48#define MICRO_TEST_PLUS_EXPRESSION_FORMATTER_H_
49
50// ----------------------------------------------------------------------------
51
52#ifdef __cplusplus
53
54// ----------------------------------------------------------------------------
55
56#include <charconv>
57#include <cstdio>
58#include <string>
59#include <string_view>
60
61#include "type-traits.h"
62#include "detail.h"
63
64// ----------------------------------------------------------------------------
65
66#if defined(__GNUC__)
67#pragma GCC diagnostic push
68#pragma GCC diagnostic ignored "-Wpadded"
69#pragma GCC diagnostic ignored "-Waggregate-return"
70#if defined(__clang__)
71#pragma clang diagnostic ignored "-Wc++98-compat"
72#pragma clang diagnostic ignored "-Wc++98-compat-pedantic"
73#endif
74#endif
75
76// ============================================================================
77
79{
80 // --------------------------------------------------------------------------
81
82 namespace detail
83 {
84 // ------------------------------------------------------------------------
85
114 struct colours
115 {
116 const char* none = "";
117 const char* pass
118 = "";
119 const char* fail
120 = "";
121 };
122
123 inline constexpr colours colours_red_green = {
124 "\033[0m",
125 "\033[32m",
126 "\033[31m"
127 };
128
129 // ========================================================================
130
151 {
152 public:
160
165
170
176 = delete;
177
183 = delete;
184
189
190 // ----------------------------------------------------------------------
191 // Primitive operator<< overloads.
192
200 operator<< (std::string_view sv);
201
209 operator<< (char c);
210
218 operator<< (const char* s);
219
227 operator<< (bool v);
228
234 expression_formatter& operator<< (std::nullptr_t);
235
243 operator<< (signed char c);
244
252 operator<< (unsigned char c);
253
261 operator<< (signed short v);
262
270 operator<< (unsigned short v);
271
279 operator<< (signed int v);
280
288 operator<< (unsigned int v);
289
297 operator<< (signed long v);
298
306 operator<< (unsigned long v);
307
315 operator<< (signed long long v);
316
324 operator<< (unsigned long long v);
325
333 operator<< (float v);
334
342 operator<< (double v);
343
351 operator<< (long double v);
352
353 // ----------------------------------------------------------------------
354 // Template operator<< overloads.
355
367 template <typename T>
369 operator<< (T* v);
370
378 template <class T>
379 requires type_traits::is_op<T>
381 operator<< (const T& t);
382
390 template <class T>
393
406 template <class T>
410 operator<< (const T& t);
411
412 // ----------------------------------------------------------------------
413 // Compound expression operator<< overloads (without colour).
414
423 template <class Lhs_T, class Rhs_T>
426
435 template <class Lhs_T, class Rhs_T>
438
447 template <class Lhs_T, class Rhs_T>
450
459 template <class Lhs_T, class Rhs_T>
462
471 template <class Lhs_T, class Rhs_T>
474
483 template <class Lhs_T, class Rhs_T>
486
495 template <class Lhs_T, class Rhs_T>
498
507 template <class Lhs_T, class Rhs_T>
510
518 template <class T>
520 operator<< (const detail::not_<T>& op);
521
522#if defined(__cpp_exceptions)
532 template <class Callable_T, class Exception_T>
535
543 template <class Callable_T>
546
554 template <class Callable_T>
557#endif
558
559 // ----------------------------------------------------------------------
560 // Buffer accessors.
561
569 [[nodiscard]] const std::string&
570 str () const noexcept;
571
579 [[nodiscard]] const char*
580 c_str () const noexcept;
581
590 void
591 clear () noexcept;
592
601 [[nodiscard]] bool
602 empty () const noexcept;
603
612 void
613 append (size_t count, char ch);
614
622 void
623 reserve (size_t capacity);
624
625 protected:
632 [[nodiscard]] inline auto
633 colour_ (const bool cond) const;
634
639
643 std::string buffer_{};
644 };
645
646 // ------------------------------------------------------------------------
647 } // namespace detail
648
649 // --------------------------------------------------------------------------
650} // namespace micro_os_plus::micro_test_plus
651
652#if defined(__GNUC__)
653#pragma GCC diagnostic pop
654#endif
655
656// ----------------------------------------------------------------------------
657
658#endif // __cplusplus
659
660// ============================================================================
661// Templates & constexpr implementations.
662
664
665// ----------------------------------------------------------------------------
666
667#endif // MICRO_TEST_PLUS_EXPRESSION_FORMATTER_H_
668
669// ----------------------------------------------------------------------------
void append(size_t count, char ch)
Appends a sequence of identical characters to the buffer.
expression_formatter & operator=(const expression_formatter &)=delete
Deleted copy assignment operator to prevent copying.
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.
expression_formatter(expression_formatter &&)=delete
Deleted move constructor to prevent moving.
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.
expression_formatter(const expression_formatter &)=delete
Deleted copy constructor to prevent copying.
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.
C++ header file with declarations for the µTest++ internals.
C++ header file with inline implementations for the µTest++ expression formatter.
Internal implementation details for the µTest++ framework.
Primary namespace for the µTest++ testing framework.
Logical AND comparator struct template.
Definition detail.h:503
Colours used to highlight pass and fail results in test reports.
const char * none
Terminal colour reset sequence.
const char * fail
Terminal colour sequence for failing tests.
const char * pass
Terminal colour sequence for passing tests.
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
Struct template representing a genuine integral value.
C++ header file with declarations for the µTest++ type trait utilities and metaprogramming support.