micro-test-plus 3.2.0
µTest++, a lightweight testing framework for embedded platforms
Loading...
Searching...
No Matches
test-reporter-inlines.h
Go to the documentation of this file.
1/*
2 * This file is part of the µOS++ distribution.
3 * (https://github.com/micro-os-plus/)
4 * Copyright (c) 2021 Liviu Ionescu.
5 *
6 * Permission to use, copy, modify, and/or distribute this software
7 * for any purpose is hereby granted, under the terms of the MIT license.
8 *
9 * If a copy of the license was not distributed with this file, it can
10 * be obtained from <https://opensource.org/licenses/MIT/>.
11 *
12 * Major parts of the code are inspired from v1.1.8 of the Boost UT project,
13 * released under the terms of the Boost Version 1.0 Software License,
14 * which can be obtained from <https://www.boost.org/LICENSE_1_0.txt>.
15 */
16
17#ifndef MICRO_TEST_PLUS_TEST_REPORTER_INLINES_H_
18#define MICRO_TEST_PLUS_TEST_REPORTER_INLINES_H_
19
20// ----------------------------------------------------------------------------
21
22#ifdef __cplusplus
23
24// ----------------------------------------------------------------------------
25
26#include <stdio.h>
27#include <cstring>
28
29// ----------------------------------------------------------------------------
30
31#if defined(__GNUC__)
32#pragma GCC diagnostic push
33#pragma GCC diagnostic ignored "-Waggregate-return"
34#if defined(__clang__)
35#pragma clang diagnostic ignored "-Wc++98-compat"
36#pragma clang diagnostic ignored "-Wc++98-compat-pedantic"
37#endif
38#endif
39
41{
42 // --------------------------------------------------------------------------
43
44 template <typename T>
47 {
48 char buff[20];
49 snprintf (buff, sizeof (buff), "%p", reinterpret_cast<void*> (v));
50 out_.append (buff);
51
52 return *this;
53 }
54
55#if 0
56 template <class T>
59 {
60 *this << detail::get (t);
61 return *this;
62 }
63#endif
64
65 template <class T>
68 {
69 out_.append (std::to_string (static_cast<long long> (v.get ())));
70 return *this;
71 }
72
73 template <class T,
78 {
79 *this << '{';
80 auto first = true;
81 for (const auto& arg : t)
82 {
83 *this << (first ? "" : ", ") << arg;
84 first = false;
85 }
86 *this << '}';
87 return *this;
88 }
89
90 template <class Lhs_T, class Rhs_T>
93 {
94 return (*this << color (op) << op.lhs () << " == " << op.rhs ()
95 << colors_.none);
96 }
97
98 template <class Lhs_T, class Rhs_T>
101 {
102 return (*this << color (op) << op.lhs () << " != " << op.rhs ()
103 << colors_.none);
104 }
105
106 template <class Lhs_T, class Rhs_T>
109 {
110 return (*this << color (op) << op.lhs () << " > " << op.rhs ()
111 << colors_.none);
112 }
113
114 template <class Lhs_T, class Rhs_T>
117 {
118 return (*this << color (op) << op.lhs () << " >= " << op.rhs ()
119 << colors_.none);
120 }
121
122 template <class Lhs_T, class Rhs_T>
125 {
126 return (*this << color (op) << op.lhs () << " < " << op.rhs ()
127 << colors_.none);
128 }
129
130 template <class Lhs_T, class Rhs_T>
133 {
134 return (*this << color (op) << op.lhs () << " <= " << op.rhs ()
135 << colors_.none);
136 }
137
138 template <class Lhs_T, class Rhs_T>
141 {
142 return (*this << '(' << op.lhs () << color (op) << " and " << colors_.none
143 << op.rhs () << ')');
144 }
145
146 template <class Lhs_T, class Rhs_T>
149 {
150 return (*this << '(' << op.lhs () << color (op) << " or " << colors_.none
151 << op.rhs () << ')');
152 }
153
154 template <class T>
157 {
158 return (*this << color (op) << "not " << op.value () << colors_.none);
159 }
160
161#if defined(__cpp_exceptions)
162 template <class Expr_T, class Exception_T>
165 {
166 return (*this << color (op) << "throws<"
167 << reflection::type_name<Exception_T> () << ">"
168 << colors_.none);
169 }
170
171 template <class Expr_T>
174 {
175 return (*this << color (op) << "throws" << colors_.none);
176 }
177
178 template <class Expr_T>
181 {
182 return (*this << color (op) << "nothrow" << colors_.none);
183 }
184#endif
185
186 template <class Expr_T>
187 void
188 test_reporter::pass (Expr_T& expr, std::string& message)
189 {
190 output_pass_prefix_ (message);
191
192 if (message.empty ())
193 {
194 // If there is no message, display the evaluated expression.
195 *this << expr;
196 }
197
199 }
200
201 template <class Expr_T>
202 void
203 test_reporter::fail (Expr_T& expr, bool abort, std::string& message,
204 const reflection::source_location& location)
205 {
206 output_fail_prefix_ (message, location);
207
208 if constexpr (type_traits::is_op_v<Expr_T>)
209 {
210 *this << ", " << expr;
211 }
212
213 output_fail_suffix_ (abort);
214 }
215
216 // --------------------------------------------------------------------------
217} // namespace micro_os_plus::micro_test_plus
218
219#if defined(__GNUC__)
220#pragma GCC diagnostic pop
221#endif
222
223// ----------------------------------------------------------------------------
224
225#endif // __cplusplus
226
227// ----------------------------------------------------------------------------
228
229#endif // MICRO_TEST_PLUS_TEST_REPORTER_INLINES_H_
230
231// ----------------------------------------------------------------------------
Local implementation of the std::source_location.
Definition reflection.h:58
Reporter to display the test results. For failed tests it prints the actual values of the operands,...
void pass(Expr_T &expr, std::string &message)
Report a passed condition.
void fail(Expr_T &expr, bool abort, std::string &message, const reflection::source_location &location)
Report a failed condition.
test_reporter & operator<<(std::string_view sv)
void output_fail_prefix_(std::string &message, const reflection::source_location &location)
constexpr auto get(const T &t)
Generic getter, calling the getter implementation.
Definition detail.h:89
typename requires_< Cond >::type requires_t
Greater than or equal comparator.
Definition detail.h:322
Less than or equal comparator.
Definition detail.h:439
Operator to check if the expression does not throw any exception.
Definition detail.h:666
Operator to check if the expression throws a specific exception.
Definition detail.h:600