iceoryx2
C++ Language Bindings
Loading...
Searching...
No Matches
testing_support.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_ERROR_REPORTING_TESTING_SUPPORT_HPP
15#define IOX2_BB_TESTING_ERROR_REPORTING_TESTING_SUPPORT_HPP
16
17#include <gtest/gtest.h>
18
21
22#include <thread>
23#include <utility>
24
25namespace iox2 {
26namespace bb {
27namespace testing {
28
30template <typename Code>
31inline auto has_error(Code&& code) -> bool {
32 auto err = legacy::er::toError(std::forward<Code>(code));
33 return ErrorHandler::instance().has_error(err.code(), err.module());
34}
35
37inline auto has_panicked() -> bool {
38 return ErrorHandler::instance().has_panicked();
39}
40
42inline auto has_error() -> bool {
43 return ErrorHandler::instance().has_error();
44}
45
47inline auto has_enforce_violation() -> bool {
49 return ErrorHandler::instance().has_violation(code);
50}
51
53inline auto has_assert_violation() -> bool {
55 return ErrorHandler::instance().has_violation(code);
56}
57
59inline auto has_violation() -> bool {
61}
62
64inline auto is_in_normal_state() -> bool {
65 return !(has_panicked() || has_error() || has_violation());
66}
67
71inline auto run_in_test_thread(const bb::StaticFunction<void()> test_function) -> void {
72 auto thread = std::thread([&]() -> auto {
73 auto successfull_run = ErrorHandler::instance().fatal_failure_test_context(test_function);
74 if (!successfull_run) {
75 GTEST_FAIL() << "This should not fail! Incorrect usage!";
76 }
77 });
78
79 if (thread.joinable()) {
80 thread.join();
81 }
82}
83
84} // namespace testing
85} // namespace bb
86} // namespace iox2
87
88// Use macros to preserve line numbers in tests (failure case).
89
90// ASSERT_* aborts test if the check fails.
91
92// NOLINTBEGIN(cppcoreguidelines-macro-usage) macro required for source location in tests
93
94#define IOX2_TESTING_ASSERT_OK() ASSERT_TRUE(iox2::bb::testing::is_in_normal_state())
95
96#define IOX2_TESTING_ASSERT_NO_PANIC() ASSERT_FALSE(iox2::bb::testing::has_panicked())
97
98#define IOX2_TESTING_ASSERT_PANIC() ASSERT_TRUE(iox2::bb::testing::has_panicked())
99
100#define IOX2_TESTING_ASSERT_ERROR(code) ASSERT_TRUE(iox2::bb::testing::has_error(code))
101
102#define IOX2_TESTING_ASSERT_NO_ERROR() ASSERT_FALSE(iox2::bb::testing::has_error())
103
104#define IOX2_TESTING_ASSERT_VIOLATION() ASSERT_TRUE(iox2::bb::testing::has_violation())
105
106#define IOX2_TESTING_ASSERT_NO_VIOLATION() ASSERT_FALSE(iox2::bb::testing::has_violation())
107
108#define IOX2_TESTING_ASSERT_ASSERT_VIOLATION() ASSERT_TRUE(iox2::bb::testing::has_assert_violation())
109
110#define IOX2_TESTING_ASSERT_ENFORCE_VIOLATION() ASSERT_TRUE(iox2::bb::testing::has_enforce_violation())
111
112// EXPECT_* continues with test if the check fails.
113
114#define IOX2_TESTING_EXPECT_OK() EXPECT_TRUE(iox2::bb::testing::is_in_normal_state())
115
116#define IOX2_TESTING_EXPECT_NO_PANIC() EXPECT_FALSE(iox2::bb::testing::has_panicked())
117
118#define IOX2_TESTING_EXPECT_PANIC() EXPECT_TRUE(iox2::bb::testing::has_panicked())
119
120#define IOX2_TESTING_EXPECT_ERROR(code) EXPECT_TRUE(iox2::bb::testing::has_error(code))
121
122#define IOX2_TESTING_EXPECT_NO_ERROR() EXPECT_FALSE(iox2::bb::testing::has_error())
123
124#define IOX2_TESTING_EXPECT_VIOLATION() EXPECT_TRUE(iox2::bb::testing::has_violation())
125
126#define IOX2_TESTING_EXPECT_NO_VIOLATION() EXPECT_FALSE(iox2::bb::testing::has_violation())
127
128#define IOX2_TESTING_EXPECT_ASSERT_VIOLATION() EXPECT_TRUE(iox2::bb::testing::has_assert_violation())
129
130#define IOX2_TESTING_EXPECT_ENFORCE_VIOLATION() EXPECT_TRUE(iox2::bb::testing::has_enforce_violation())
131
132// NOLINTEND(cppcoreguidelines-macro-usage)
133
134#endif
static T & instance(Args &&... args) noexcept
Construct the instance to be guarded with constructor arguments.
ErrorCode code() const
Definition violation.hpp:54
auto is_in_normal_state() -> bool
indicates there is no error, violation or panic.
auto has_violation() -> bool
indicates whether the test error handler registered violation (there are only two kinds).
auto has_panicked() -> bool
indicates whether the test error handler invoked panic
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
constexpr auto err(const E &error) -> Unexpected< E >
Definition expected.hpp:33
HoofsErrorType toError(HoofsError code)