micro-test-plus 6.0.0
µTest++ Testing Framework
Loading...
Searching...
No Matches
check-result.cpp
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
12// ----------------------------------------------------------------------------
13
32
33// ----------------------------------------------------------------------------
34
37
38#if __has_include("micro-os-plus/diag/trace.h")
39#include "micro-os-plus/diag/trace.h"
40#endif // __has_include("micro-os-plus/diag/trace.h")
41
42#include <exception>
43
44// ----------------------------------------------------------------------------
45
46#if defined(__GNUC__)
47#pragma GCC diagnostic ignored "-Waggregate-return"
48#if defined(__clang__)
49#pragma clang diagnostic ignored "-Wc++98-compat"
50#pragma clang diagnostic ignored "-Wexit-time-destructors"
51#pragma clang diagnostic ignored "-Wglobal-constructors"
52#endif // defined(__clang__)
53#endif // defined(__GNUC__)
54
55// ============================================================================
56
57#if defined(MICRO_OS_PLUS_MICRO_TEST_PLUS_ENABLED)
58
59// ----------------------------------------------------------------------------
60
62{
63 // --------------------------------------------------------------------------
64 namespace detail
65 {
71 check_result::check_result (void) = default;
72
88 void
89 check_result::start (bool value, check_kind kind, bool has_expression,
90 const reflection::source_location& location,
91 test_case& test_case, bool writes_passed)
92 {
93 value_ = value;
94 is_written_ = !value || writes_passed;
95 info_ = failure_info{ kind, {}, has_expression, location };
97#if defined(__cpp_exceptions)
98 uncaught_exceptions_ = std::uncaught_exceptions ();
99#endif // defined(__cpp_exceptions)
100 }
101
139 check_result::~check_result () noexcept (false)
140 {
141#if defined(MICRO_OS_PLUS_MICRO_TEST_PLUS_TRACE_CONSTRUCTORS_ENABLED)
142 trace::printf ("%s\n", __PRETTY_FUNCTION__);
143#endif // MICRO_OS_PLUS_MICRO_TEST_PLUS_TRACE_CONSTRUCTORS_ENABLED
144
145 // A result that was not initialised, such as a default argument
146 // whose function did not run, reports nothing.
147 if (test_case_ == nullptr) [[unlikely]]
148 {
149 return;
150 }
151
152 // The index starts at 0; it must be incremented before the check is
153 // reported, so that the first check is reported as check #1.
155
156 // The text formatted by this check, even if the formatter was used by
157 // other checks in the meantime (when this result was kept).
158 auto& expression = test_case_->test_reporter ().expression_;
159 const std::string& expression_str = expression.check_text (this);
160
161 if (value_) [[likely]]
162 {
163 if (is_written_)
164 {
165 test_case_->test_reporter ().pass (info_.message, expression_str,
166 *test_case_);
167 }
168 else
169 {
171 }
173 }
174 else
175 {
176 test_case_->test_reporter ().fail (info_, expression_str,
177 *test_case_);
179 }
180
181 expression.end_check (this);
182
183 if (info_.kind == check_kind::requirement && !value_) [[unlikely]]
184 {
185#if defined(__cpp_exceptions)
186 if (std::uncaught_exceptions () <= uncaught_exceptions_)
187 {
188 throw failed_requirement{};
189 }
190#else
192#endif // defined(__cpp_exceptions)
193 }
194 }
195 } // namespace detail
196
197 // --------------------------------------------------------------------------
198} // namespace micro_os_plus::micro_test_plus
199
200// ----------------------------------------------------------------------------
201
202#endif // defined(MICRO_OS_PLUS_MICRO_TEST_PLUS_ENABLED)
203
204// ----------------------------------------------------------------------------
C++ header file with declarations for the µTest++ check result.
void increment_passed_checks(size_t count=1) noexcept
Increments the passed-checks counter.
void increment_failed_checks(size_t count=1) noexcept
Increments the failed-checks counter.
bool value(void) const &&noexcept
Retrieves the result value.
int uncaught_exceptions_
The number of uncaught exceptions when the check was created.
test_case * test_case_
Pointer to the test case invoking this check.
void start(bool value, check_kind kind, bool has_expression, const reflection::source_location &location, test_case &test_case, bool writes_passed)
Starts the result of a check from its parts.
check_result(void)
Constructs a result in the initial state.
bool is_written_
Whether the outcome of the check is written.
~check_result() noexcept(false)
Destructor that reports the check.
bool value_
Stores the result value of the check.
failure_info info_
The details displayed when the check fails.
const std::string & check_text(const void *owner) const noexcept
Returns the expression text of a check.
void fail(const failure_info &failure, const std::string &expression, const test_case &test_case)
Reports a failed condition.
Definition reporter.cpp:771
virtual void pass_without_text(const test_case &test_case)=0
Reports a passed condition whose text is not written.
expression_formatter expression_
Expression formatter for pass and fail reporting.
Definition reporter.h:1423
void pass(const std::string &message, const std::string &expression, const test_case &test_case)
Reports a passed condition.
Definition reporter.cpp:751
size_t next_test_point_number(void) noexcept
Advances to the next child test point, and returns its number.
void abort(const reflection::source_location &sl=reflection::source_location::current()) const
Aborts test execution via the owning runner, for a failed requirement.
Definition test.cpp:306
class reporter & test_reporter(void) const noexcept
Returns the test reporter associated with this test runnable.
Definition test.cpp:155
Local implementation of source location information for diagnostics.
Definition reflection.h:143
A named, runnable test case that lives inside a suite.
Definition test.h:1362
Internal implementation details for the µTest++ framework.
check_kind
The kind of a check, which determines what happens when it fails.
Primary namespace for the µTest++ testing framework.
The exception thrown to stop a test case after a failed require().
Definition detail.h:1322
The details of a check, displayed when it fails.
reflection::source_location location
The source location of the check.
std::string message
The message passed via operator<<(), if any.
C++ header file with declarations for the µTest++ test hierarchy.