micro-test-plus 5.0.1
µ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_OS_PLUS_MICRO_TEST_PLUS_EXPRESSION_FORMATTER_H_
48#define MICRO_OS_PLUS_MICRO_TEST_PLUS_EXPRESSION_FORMATTER_H_
49
50// ----------------------------------------------------------------------------
51
52#if defined(__cplusplus)
53
54// ----------------------------------------------------------------------------
55
56#if __has_include("micro-os-plus/project-config.h")
57#include "micro-os-plus/project-config.h"
58#endif // __has_include("micro-os-plus/project-config.h")
59
60#if __has_include("micro-os-plus/micro-test-plus-defines.h")
61#include "micro-os-plus/micro-test-plus-defines.h"
62#endif // __has_include("micro-os-plus/micro-test-plus-defines.h")
63
64#include "type-traits.h"
65#include "detail.h"
66
67#include <charconv>
68#include <cstdio>
69#include <string>
70#include <string_view>
71
72// ----------------------------------------------------------------------------
73
74#if defined(__GNUC__)
75#pragma GCC diagnostic push
76
77#pragma GCC diagnostic ignored "-Wpadded"
78#pragma GCC diagnostic ignored "-Waggregate-return"
79#if defined(__clang__)
80#pragma clang diagnostic ignored "-Wc++98-compat"
81#pragma clang diagnostic ignored "-Wc++98-compat-pedantic"
82#endif // defined(__clang__)
83#endif // defined(__GNUC__)
84
85// ============================================================================
86
88{
89 // --------------------------------------------------------------------------
90
91 namespace detail
92 {
93 // ------------------------------------------------------------------------
94
123 struct colours
124 {
125 const char* none = "";
126 const char* pass
127 = "";
128 const char* fail
129 = "";
130 };
131
132 inline constexpr colours colours_red_green = {
133 "\033[0m",
134 "\033[32m",
135 "\033[31m"
136 };
137
138 // ========================================================================
139
160 {
161 public:
169
174
179
185 = delete;
186
192 = delete;
193
198
199 // ----------------------------------------------------------------------
200 // Primitive operator<< overloads.
201
209 operator<< (std::string_view sv);
210
218 operator<< (char c);
219
227 operator<< (const char* s);
228
236 operator<< (bool v);
237
243 expression_formatter& operator<< (std::nullptr_t);
244
252 operator<< (signed char c);
253
261 operator<< (unsigned char c);
262
270 operator<< (signed short v);
271
279 operator<< (unsigned short v);
280
288 operator<< (signed int v);
289
297 operator<< (unsigned int v);
298
306 operator<< (signed long v);
307
315 operator<< (unsigned long v);
316
324 operator<< (signed long long v);
325
333 operator<< (unsigned long long v);
334
342 operator<< (float v);
343
351 operator<< (double v);
352
360 operator<< (long double v);
361
362 // ----------------------------------------------------------------------
363 // Template operator<< overloads.
364
376 template <typename T>
378 operator<< (T* v);
379
387 template <class T>
388 requires type_traits::is_op<T>
390 operator<< (const T& t);
391
399 template <class T>
402
415 template <class T>
419 operator<< (const T& t);
420
421 // ----------------------------------------------------------------------
422 // Compound expression operator<< overloads (without colour).
423
432 template <class Lhs_T, class Rhs_T>
435
444 template <class Lhs_T, class Rhs_T>
447
456 template <class Lhs_T, class Rhs_T>
459
468 template <class Lhs_T, class Rhs_T>
471
480 template <class Lhs_T, class Rhs_T>
483
492 template <class Lhs_T, class Rhs_T>
495
504 template <class Lhs_T, class Rhs_T>
507
516 template <class Lhs_T, class Rhs_T>
519
527 template <class T>
529 operator<< (const detail::not_<T>& op);
530
531#if defined(__cpp_exceptions)
532
542 template <class Callable_T, class Exception_T>
545
553 template <class Callable_T>
556
564 template <class Callable_T>
567
568#endif // defined(__cpp_exceptions)
569
570 // ----------------------------------------------------------------------
571 // Buffer accessors.
572
580 [[nodiscard]] const std::string&
581 str () const noexcept;
582
590 [[nodiscard]] const char*
591 c_str () const noexcept;
592
601 void
602 clear () noexcept;
603
612 [[nodiscard]] bool
613 empty () const noexcept;
614
623 void
624 append (size_t count, char ch);
625
633 void
634 reserve (size_t capacity);
635
636 protected:
643 [[nodiscard]] inline auto
644 colour_ (const bool cond) const;
645
650
654 std::string buffer_{};
655 };
656
657 // ------------------------------------------------------------------------
658 } // namespace detail
659
660 // --------------------------------------------------------------------------
661} // namespace micro_os_plus::micro_test_plus
662
663#if defined(__GNUC__)
664#pragma GCC diagnostic pop
665#endif // defined(__GNUC__)
666
667// ----------------------------------------------------------------------------
668
669#endif // defined(__cplusplus)
670
671// ============================================================================
672// Templates, inlines & constexpr implementations.
673
675
676// ----------------------------------------------------------------------------
677
678#endif // MICRO_OS_PLUS_MICRO_TEST_PLUS_EXPRESSION_FORMATTER_H_
679
680// ----------------------------------------------------------------------------
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:512
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: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
Struct template representing a genuine integral value.
C++ header file with declarations for the µTest++ type trait utilities and metaprogramming support.