iceoryx2
C++ Language Bindings
Loading...
Searching...
No Matches
fatal_failure.hpp
Go to the documentation of this file.
1// Copyright (c) 2023 by Apex.AI Inc. All rights reserved.
2// Copyright (c) 2025 Contributors to the Eclipse Foundation
3//
4// See the NOTICE file(s) distributed with this work for additional
5// information regarding copyright ownership.
6//
7// This program and the accompanying materials are made available under the
8// terms of the Apache Software License 2.0 which is available at
9// https://www.apache.org/licenses/LICENSE-2.0, or the MIT license
10// which is available at https://opensource.org/licenses/MIT.
11//
12// SPDX-License-Identifier: Apache-2.0 OR MIT
13
14#ifndef IOX2_BB_TESTING_FATAL_FAILURE_HPP
15#define IOX2_BB_TESTING_FATAL_FAILURE_HPP
16
21
23
24#include <gmock/gmock.h>
25#include <gtest/gtest.h>
26
27namespace iox2 {
28namespace bb {
29namespace testing {
43template <typename ErrorType,
44 std::enable_if_t<!std::is_same<ErrorType, legacy::er::FatalKind>::value
45 && !std::is_same<ErrorType, legacy::er::EnforceViolationKind>::value
46 && !std::is_same<ErrorType, legacy::er::AssertViolationKind>::value,
47 bool> = true>
48inline auto expect_fatal_failure(const bb::StaticFunction<void()>& test_function, const ErrorType expected_error)
49 -> bool {
50 ErrorHandler::instance().reset();
51 run_in_test_thread([&]() -> auto { test_function(); });
54
55 const auto has_expected_error = has_error(expected_error);
56 if (!has_expected_error) {
57 IOX2_LOG(Error, "Expected an '" << expected_error << "' error but it did not happen!");
58 }
59
60 EXPECT_TRUE(has_expected_error);
61 return has_expected_error && has_panicked;
62}
63
64template <typename ErrorType, std::enable_if_t<std::is_same<ErrorType, legacy::er::FatalKind>::value, bool> = true>
65inline auto expect_fatal_failure(const bb::StaticFunction<void()>& test_function,
66 const ErrorType expected_error IOX2_MAYBE_UNUSED) -> bool {
67 ErrorHandler::instance().reset();
68 run_in_test_thread([&]() -> auto { test_function(); });
71
72 const auto has_expected_error = has_panicked;
73 if (!has_expected_error) {
74 IOX2_LOG(Error, "Expected '" << legacy::er::FatalKind::name << "' but it did not happen!");
75 }
76
77 EXPECT_TRUE(has_expected_error);
78 return has_expected_error && has_panicked;
79}
80
81template <typename ErrorType,
82 std::enable_if_t<std::is_same<ErrorType, legacy::er::EnforceViolationKind>::value, bool> = true>
83inline auto expect_fatal_failure(const bb::StaticFunction<void()>& test_function,
84 const ErrorType expected_error IOX2_MAYBE_UNUSED) -> bool {
85 ErrorHandler::instance().reset();
86 run_in_test_thread([&]() -> auto { test_function(); });
89
90 const auto has_expected_error = has_enforce_violation();
91 if (!has_expected_error) {
92 IOX2_LOG(Error, "Expected '" << legacy::er::EnforceViolationKind::name << "' but it did not happen!");
93 }
94
95 EXPECT_TRUE(has_expected_error);
96 return has_expected_error && has_panicked;
97}
98
99template <typename ErrorType,
100 std::enable_if_t<std::is_same<ErrorType, legacy::er::AssertViolationKind>::value, bool> = true>
101inline auto expect_fatal_failure(const bb::StaticFunction<void()>& test_function,
102 const ErrorType expected_error IOX2_MAYBE_UNUSED) -> bool {
103 ErrorHandler::instance().reset();
104 run_in_test_thread([&]() -> auto { test_function(); });
106 const auto has_panicked = testing::has_panicked();
107
108 const auto has_expected_error = has_assert_violation();
109 if (!has_expected_error) {
110 IOX2_LOG(Error, "Expected '" << legacy::er::AssertViolationKind::name << "' but it did not happen!");
111 }
112
113 EXPECT_TRUE(has_expected_error);
114 return has_expected_error && has_panicked;
115}
116
128inline auto expect_no_fatal_failure(const bb::StaticFunction<void()>& test_function) -> bool {
129 run_in_test_thread([&]() -> auto { test_function(); });
130 return !has_panicked();
131}
132
133} // namespace testing
134} // namespace bb
135} // namespace iox2
136
137
138// NOLINTBEGIN(cppcoreguidelines-macro-usage) this is meant to blend in with the gTest macros
139
140#define IOX2_TESTING_EXPECT_FATAL_FAILURE(test_function, expected_error) \
141 iox2::bb::testing::expect_fatal_failure(test_function, expected_error)
142
143#define IOX2_TESTING_EXPECT_NO_FATAL_FAILURE(test_function) iox2::bb::testing::expect_no_fatal_failure(test_function)
144
145// NOLINTEND(cppcoreguidelines-macro-usage)
146
147
148#endif // IOX2_BB_TESTING_FATAL_FAILURE_HPP
#define IOX2_MAYBE_UNUSED
IOX2_MAYBE_UNUSED adds the [[gnu::unused]] attribute when it is available for the current compiler or...
static T & instance(Args &&... args) noexcept
Construct the instance to be guarded with constructor arguments.
#define IOX2_LOG(level, msg_stream)
Macro for logging.
Definition logging.hpp:63
auto has_panicked() -> bool
indicates whether the test error handler invoked panic
auto expect_no_fatal_failure(const bb::StaticFunction< void()> &test_function) -> bool
This function is used in cases no fatal failure is expected but could potentially occur....
auto expect_fatal_failure(const bb::StaticFunction< void()> &test_function, const ErrorType expected_error) -> bool
This function is used in cases a fatal failure is expected. The function only works in combination wi...
auto has_assert_violation() -> bool
indicates whether the test error handler registered an assert violation
auto has_error() -> bool
indicates whether the test error handler registered any error
auto run_in_test_thread(const bb::StaticFunction< void()> test_function) -> void
runs test_function in a test context that can detect fatal failures; runs in a separate thread
auto has_enforce_violation() -> bool
indicates whether the test error handler registered an enforce violation
detail::StaticFunction< Capacity, Signature > StaticFunction
A static memory replacement for std::function.
static constexpr char const * name
static constexpr char const * name
static constexpr char const * name
#define IOX2_TESTING_EXPECT_PANIC()