Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
conditional_tests.hpp
Go to the documentation of this file.
1// Copyright 2026, Toyota Motor Corporation
2//
3// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4
5///
6/// @file conditional_tests.hpp
7/// @brief Macros to help define test cases that are run or not depending on a generic condition
8///
9
10#ifndef INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_TESTING_GTEST_WITH_GTEST_CONDITIONAL_TESTS_HPP_
11#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_TESTING_GTEST_WITH_GTEST_CONDITIONAL_TESTS_HPP_
12
13// IWYU pragma: private
14// IWYU pragma: friend "arene/base/testing/gtest/.*"
15
16#include <gtest/gtest.h>
17
18// parasoft-begin-suppress AUTOSAR-A16_2_2-a-2 "Arene Base aggregate headers permitted by A16-2-2 Permit #1"
19#include "arene/base/compiler_support/preprocessor.hpp"
20#include "arene/base/stdlib_choice/integral_constant.hpp"
21// parasoft-end-suppress AUTOSAR-A16_2_2-a-2
22
23// NOLINTBEGIN(cppcoreguidelines-macro-usage) This code interacts with Google Test's macros so it needs to be too.
24// parasoft-begin-suppress AUTOSAR-A16_0_1-d "Defining these macros is necessary because Google Test uses macros"
25// parasoft-begin-suppress CERT_C-PRE30-a "Token concatenation is necessary because Google Test does it too"
26// parasoft-begin-suppress AUTOSAR-M16_3_1-a "Multi-token concatenation is necessary because Google Test does it too"
27// parasoft-begin-suppress AUTOSAR-M16_0_6-a "Some args can not be parenthesized to preserve Google Test's API"
28
29/// @brief Adds a type-parameterized test to a Google Test suite which is conditionally run based on the third parameter
30/// @param SuiteName The name of a Google Test suite previously defined with TYPED_TEST_SUITE_P
31/// @param TestName The name of this test; needs to be registered later with REGISTER_TYPED_TEST_SUITE_P
32/// @param Condition A condition convertible to @c bool which depends on @c TypeParam
33#define CONDITIONAL_TYPED_TEST_P(SuiteName, TestName, ...)
34 namespace GTEST_SUITE_NAMESPACE_(SuiteName) {
35 template <typename TypeParam, bool CondOk>
36 class ARENE_CONCATENATE(TestName, _Impl)
37 : public SuiteName<TypeParam> {
38 private:
39 void TestBody() override {}
40 };
41 template <typename TypeParam_>
42 class ARENE_CONCATENATE(TestName, _Impl)<TypeParam_, true> : public SuiteName<TypeParam_> {
43 private:
44 using TestFixture = SuiteName<TypeParam_>;
45 using TypeParam = TypeParam_;
46 void TestBody() override;
47 };
48 template <typename TypeParam>
49 class TestName : public ARENE_CONCATENATE(TestName, _Impl)<TypeParam, (__VA_ARGS__)> {};
50 ARENE_MAYBE_UNUSED static bool const gtest_##TestName##_defined_ =
51 GTEST_TYPED_TEST_SUITE_P_STATE_(SuiteName)
52 .AddTestName(__FILE__, __LINE__, GTEST_STRINGIFY_(SuiteName), GTEST_STRINGIFY_(TestName));
53 } /* namespace GTEST_SUITE_NAMESPACE_(SuiteName) */
54 template <typename TypeParam_Internal_>
55 void GTEST_SUITE_NAMESPACE_(SuiteName)::ARENE_CONCATENATE(TestName, _Impl)<TypeParam_Internal_, true>::TestBody()
56
57/// @brief Adds a type-parameterized test to a Google Test suite which is conditionally run based on the third parameter
58/// @param SuiteName The name of a Google Test suite previously defined with TYPED_TEST_SUITE
59/// @param TestName The name of this test
60/// @param Condition A condition convertible to @c bool which depends on @c TypeParam
61#define CONDITIONAL_TYPED_TEST(testsuite, testcase, ...)
62 template <typename TypeParam_Internal_>
63 class GTEST_TEST_CLASS_NAME_(testsuite, testcase)
64 : public testsuite<TypeParam_Internal_> {
65 private:
66 using TestFixture = testsuite<TypeParam_Internal_>;
67 using TypeParam = TypeParam_Internal_;
68
69 void TestBody() override { do_conditional_test(std::integral_constant<bool, (__VA_ARGS__)>{}); }
70
71 void do_conditional_test(std::false_type) {
72 GTEST_SKIP() << "Disabled by condition: " << ARENE_STRINGIZE(__VA_ARGS__);
73 }
74
75 void do_conditional_test(std::true_type);
76 };
77 /* NOLINTNEXTLINE(fuchsia-statically-constructed-objects) */
78 static const bool gtest_##testsuite##_##testcase##_registered_ = ::testing::internal::TypeParameterizedTest<
79 testsuite,
80 ::testing::internal::TemplateSel<GTEST_TEST_CLASS_NAME_(testsuite, testcase)>,
81 GTEST_TYPE_PARAMS_(testsuite)>::
82 Register(
83 "",
84 ::testing::internal::CodeLocation(__FILE__, __LINE__),
85 GTEST_STRINGIFY_(testsuite),
86 GTEST_STRINGIFY_(testcase),
87 0,
88 ::testing::internal::GenerateNames<GTEST_NAME_GENERATOR_(testsuite), GTEST_TYPE_PARAMS_(testsuite)>()
89 );
90 template <typename TypeParam_Internal_>
91 void GTEST_TEST_CLASS_NAME_(testsuite, testcase)<TypeParam_Internal_>::do_conditional_test(std::true_type)
92
93// parasoft-end-suppress AUTOSAR-M16_0_6-a
94// parasoft-end-suppress AUTOSAR-M16_3_1-a
95// parasoft-end-suppress CERT_C-PRE30-a
96// parasoft-end-suppress AUTOSAR-A16_0_1-d
97
98// NOLINTEND(cppcoreguidelines-macro-usage)
99
100#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_TESTING_GTEST_WITH_GTEST_CONDITIONAL_TESTS_HPP_