micro-test-plus 3.2.2
µTest++ Testing Framework
Loading...
Searching...
No Matches
test-reporter-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 Liviu Ionescu. All rights reserved.
4 *
5 * Permission to use, copy, modify, and/or distribute this software
6 * for any 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
9 * be 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
48
49#ifndef MICRO_TEST_PLUS_TEST_REPORTER_INLINES_H_
50#define MICRO_TEST_PLUS_TEST_REPORTER_INLINES_H_
51
52// ----------------------------------------------------------------------------
53
54#ifdef __cplusplus
55
56// ----------------------------------------------------------------------------
57
58#include <stdio.h>
59#include <cstring>
60
61// ----------------------------------------------------------------------------
62
63#if defined(__GNUC__)
64#pragma GCC diagnostic push
65#pragma GCC diagnostic ignored "-Waggregate-return"
66#if defined(__clang__)
67#pragma clang diagnostic ignored "-Wc++98-compat"
68#pragma clang diagnostic ignored "-Wc++98-compat-pedantic"
69#endif
70#endif
71
73{
74 // --------------------------------------------------------------------------
75
90 template <typename T>
93 {
94 char buff[20];
95 snprintf (buff, sizeof (buff), "%p", reinterpret_cast<void*> (v));
96 out_.append (buff);
97
98 return *this;
99 }
100
101#if 0
110 template <class T>
113 {
114 *this << detail::get (t);
115 return *this;
116 }
117#endif
118
133 template <class T>
136 {
137 out_.append (std::to_string (static_cast<long long> (v.get ())));
138 return *this;
139 }
140
155 template <class T,
160 {
161 *this << '{';
162 auto first = true;
163 for (const auto& arg : t)
164 {
165 *this << (first ? "" : ", ") << arg;
166 first = false;
167 }
168 *this << '}';
169 return *this;
170 }
171
183 template <class Lhs_T, class Rhs_T>
186 {
187 return (*this << color (op) << op.lhs () << " == " << op.rhs ()
188 << colors_.none);
189 }
190
203 template <class Lhs_T, class Rhs_T>
206 {
207 return (*this << color (op) << op.lhs () << " != " << op.rhs ()
208 << colors_.none);
209 }
210
223 template <class Lhs_T, class Rhs_T>
226 {
227 return (*this << color (op) << op.lhs () << " > " << op.rhs ()
228 << colors_.none);
229 }
230
244 template <class Lhs_T, class Rhs_T>
247 {
248 return (*this << color (op) << op.lhs () << " >= " << op.rhs ()
249 << colors_.none);
250 }
251
263 template <class Lhs_T, class Rhs_T>
266 {
267 return (*this << color (op) << op.lhs () << " < " << op.rhs ()
268 << colors_.none);
269 }
270
284 template <class Lhs_T, class Rhs_T>
287 {
288 return (*this << color (op) << op.lhs () << " <= " << op.rhs ()
289 << colors_.none);
290 }
291
304 template <class Lhs_T, class Rhs_T>
307 {
308 return (*this << '(' << op.lhs () << color (op) << " and " << colors_.none
309 << op.rhs () << ')');
310 }
311
324 template <class Lhs_T, class Rhs_T>
327 {
328 return (*this << '(' << op.lhs () << color (op) << " or " << colors_.none
329 << op.rhs () << ')');
330 }
331
340 template <class T>
343 {
344 return (*this << color (op) << "not " << op.value () << colors_.none);
345 }
346
347#if defined(__cpp_exceptions)
360 template <class Expr_T, class Exception_T>
363 {
364 return (*this << color (op) << "throws<"
366 << colors_.none);
367 }
368
379 template <class Expr_T>
382 {
383 return (*this << color (op) << "throws" << colors_.none);
384 }
385
396 template <class Expr_T>
399 {
400 return (*this << color (op) << "nothrow" << colors_.none);
401 }
402#endif
403
417 template <class Expr_T>
418 void
419 test_reporter::pass (Expr_T& expr, std::string& message)
420 {
421 output_pass_prefix_ (message);
422
423 if (message.empty ())
424 {
425 // If there is no message, display the evaluated expression.
426 *this << expr;
427 }
428
430 }
431
441 template <class Expr_T>
442 void
443 test_reporter::fail (Expr_T& expr, bool abort, std::string& message,
444 const reflection::source_location& location)
445 {
446 output_fail_prefix_ (message, location);
447
448 if constexpr (type_traits::is_op_v<Expr_T>)
449 {
450 *this << ", " << expr;
451 }
452
453 output_fail_suffix_ (abort);
454 }
455
456 // --------------------------------------------------------------------------
457} // namespace micro_os_plus::micro_test_plus
458
459#if defined(__GNUC__)
460#pragma GCC diagnostic pop
461#endif
462
463// ----------------------------------------------------------------------------
464
465#endif // __cplusplus
466
467// ----------------------------------------------------------------------------
468
469#endif // MICRO_TEST_PLUS_TEST_REPORTER_INLINES_H_
470
471// ----------------------------------------------------------------------------
Local implementation of source location information for diagnostics.
Definition reflection.h:137
Reporter to display test results, including operand values and types for failures.
colors colors_
ANSI colour codes for output formatting.
std::string out_
Internal output buffer for accumulating report content.
test_reporter()=default
Default constructor for the test_reporter class.
void output_pass_suffix_(void)
Outputs the suffix for a passing condition.
void pass(Expr_T &expr, std::string &message)
Report a passed condition.
void output_fail_suffix_(bool abort)
Outputs the suffix for a failing 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)
Output operator for std::string_view.
void output_fail_prefix_(std::string &message, const reflection::source_location &location)
Outputs the prefix for a failing condition.
auto color(const bool cond)
Selects the appropriate colour code based on a condition.
void output_pass_prefix_(std::string &message)
Outputs the prefix for a passing condition.
constexpr auto get(const T &t)
Generic getter function template for value retrieval.
Definition detail.h:223
constexpr auto type_name(void) -> std::string_view
Extract the type name from the __PRETTY_FUNCTION__ macro.
static constexpr auto has_npos_v
Variable template to determine if a type provides a static npos member.
typename requires_< Cond >::type requires_t
Alias template for extracting the type member from requires_.
constexpr auto is_op_v
Variable template to determine if a type derives from op.
Primary namespace for the µTest++ testing framework.
Logical AND comparator struct template.
Definition detail.h:1178
constexpr auto rhs(void) const
Retrieves the right-hand operand.
Definition detail.h:1244
constexpr auto lhs(void) const
Retrieves the left-hand operand.
Definition detail.h:1226
Equality comparator struct template.
Definition detail.h:260
constexpr auto rhs(void) const
Retrieves the right-hand operand.
Definition detail.h:376
constexpr auto lhs(void) const
Retrieves the left-hand operand.
Definition detail.h:358
Greater than or equal comparator struct template.
Definition detail.h:738
constexpr auto rhs(void) const
Retrieves the right-hand operand.
Definition detail.h:831
constexpr auto lhs(void) const
Retrieves the left-hand operand.
Definition detail.h:813
Greater than comparator struct template.
Definition detail.h:593
constexpr auto lhs(void) const
Retrieves the left-hand operand.
Definition detail.h:666
constexpr auto rhs(void) const
Retrieves the right-hand operand.
Definition detail.h:684
Less than or equal comparator struct template.
Definition detail.h:1031
constexpr auto rhs(void) const
Retrieves the right-hand operand.
Definition detail.h:1125
constexpr auto lhs(void) const
Retrieves the left-hand operand.
Definition detail.h:1107
Less than comparator struct template.
Definition detail.h:885
constexpr auto lhs(void) const
Retrieves the left-hand operand.
Definition detail.h:958
constexpr auto rhs(void) const
Retrieves the right-hand operand.
Definition detail.h:976
Non-equality comparator struct template.
Definition detail.h:432
constexpr auto rhs(void) const
Retrieves the right-hand operand.
Definition detail.h:539
constexpr auto lhs(void) const
Retrieves the left-hand operand.
Definition detail.h:521
Logical NOT comparator struct template.
Definition detail.h:1415
constexpr auto value() const
Retrieves the value of the operand.
Definition detail.h:1460
Operator struct template to check if an expression does not throw any exception.
Definition detail.h:1673
Logical OR comparator struct template.
Definition detail.h:1297
constexpr auto rhs(void) const
Retrieves the right-hand operand.
Definition detail.h:1363
constexpr auto lhs(void) const
Retrieves the left-hand operand.
Definition detail.h:1345
Operator struct template to check if an expression throws a specific exception.
Definition detail.h:1512
Struct template representing a genuine integral value.
constexpr decltype(auto) get() const
Getter for the encapsulated value.