9#include <gtest/gtest.h>
11USERVER_NAMESPACE_BEGIN
13namespace utest::impl {
15class EnrichedTestBase {
17 virtual ~EnrichedTestBase() =
default;
18 virtual void SetUp() = 0;
19 virtual void TearDown() = 0;
20 virtual void TestBody() = 0;
22 std::size_t GetThreadCount()
const {
return utest_thread_count_; }
23 void SetThreadCount(std::size_t count) { utest_thread_count_ = count; }
26 std::size_t utest_thread_count_ = 1;
29enum class DeathTestsEnabled :
bool {};
34void DoRunTest(std::size_t thread_count, DeathTestsEnabled, std::function<std::unique_ptr<EnrichedTestBase>()> factory);
36void RunSetUpTestSuite(
void (*set_up_test_suite)());
37void RunTearDownTestSuite(
void (*tear_down_test_suite)());
44template <
typename UserFixture>
46class EnrichedFixture :
public UserFixture,
public EnrichedTestBase {
48 void SetUp()
override {
return UserFixture::SetUp(); }
49 void TearDown()
override {
return UserFixture::TearDown(); }
52 using EnrichedTestBase::SetThreadCount;
53 using EnrichedTestBase::TestBody;
56template <
typename Base,
typename UserFixture>
57class WithStaticMethods :
public Base {
59 static void SetUpTestSuite() { RunSetUpTestSuite(&UserFixture::SetUpTestSuite); }
61 static void TearDownTestSuite() { RunTearDownTestSuite(&UserFixture::TearDownTestSuite); }
66template <
typename UserFixture>
67class TestLauncher :
public WithStaticMethods<::testing::Test, UserFixture> {
70 template <
typename EnrichedTest>
71 static void RunTest(std::size_t thread_count,
bool death_tests_enabled) {
72 utest::impl::DoRunTest(thread_count, DeathTestsEnabled{death_tests_enabled}, [] {
73 return std::make_unique<EnrichedTest>();
78template <
typename UserFixture>
79class TestLauncherParametric
80 :
public WithStaticMethods<::testing::TestWithParam<
typename UserFixture::ParamType>, UserFixture> {
83 template <
typename EnrichedTest>
84 static void RunTest(std::size_t thread_count,
bool death_tests_enabled) {
85 using ParamType =
typename UserFixture::ParamType;
86 const auto& parameter = ::testing::TestWithParam<ParamType>::GetParam();
90 auto factory = std::make_unique<testing::internal::ParameterizedTestFactory<EnrichedTest>>(parameter);
92 utest::impl::DoRunTest(thread_count, DeathTestsEnabled{death_tests_enabled}, [&] {
93 return std::unique_ptr<EnrichedTest>{
dynamic_cast<EnrichedTest*>(factory->CreateTest())};
98template <
template <
typename>
typename UserFixture,
typename T>
99using TestLauncherTyped = TestLauncher<UserFixture<T>>;
102struct DefaultNameGenerator
final {
103 template <
typename T>
104 static std::string GetName(
int i) {
105 return std::to_string(i);
109constexpr bool CheckTestSuiteNameSuffix(std::string_view str, std::string_view suffix) {
110 return str.size() >= suffix.size() && str.substr(str.size() - suffix.size()) == suffix;
118#define IMPL_UTEST_NON_PARENTHESIZED(identifier) identifier
121#define IMPL_UTEST_NAMESPACE_NAME(test_suite_name) test_suite_name##_##Utest
126#define IMPL_UTEST_HIDE_ENRICHED_FROM_IDE(test_suite_name, test_name) test_suite_name##_##test_name##_##Utest
129#define IMPL_UTEST_USER_FIXTURE(test_suite_name) test_suite_name##_##UtestFixture
132#define IMPL_UTEST_MAKE_USER_FIXTURE_ALIAS(test_suite_name)
136#define IMPL_UTEST_MAKE_USER_FIXTURE_ALIAS_TYPED(test_suite_name)
137 template <typename UtestTypeParamImpl>
141#define IMPL_UTEST_HIDE_USER_FIXTURE_BY_TEST_LAUNCHER(test_suite_name, test_launcher_template)
146#define IMPL_UTEST_HIDE_USER_FIXTURE_BY_TEST_LAUNCHER_TYPED(test_suite_name)
147 template <typename UtestTypeParamImpl>
152#define IMPL_UTEST_TEST(test_suite_name, test_name, thread_count, death_tests_enabled)
154 class EnrichedTest final : public USERVER_NAMESPACE
::utest::impl::EnrichedFixture<::testing::Test> {
155 void TestBody() override;
158 TEST
(test_suite_name, test_name) {
160 USERVER_NAMESPACE
::utest::impl::TestLauncher<::testing::Test>::RunTest<EnrichedTest>(
161 thread_count, death_tests_enabled
167#define IMPL_UTEST_ANY_BEGIN(test_suite_name, test_name, test_launcher_template)
170 class EnrichedTest final
172 void TestBody() override;
176
181#define IMPL_UTEST_ANY_END(test_suite_name, test_name, thread_count, death_tests_enabled)
184 this->RunTest<EnrichedTest>(thread_count, death_tests_enabled);
190#define IMPL_UTEST_TEST_F(test_suite_name, test_name, thread_count, death_tests_enabled)
192 TEST_F
(test_suite_name, test_name)
196#define IMPL_UTEST_TEST_P(test_suite_name, test_name, thread_count, death_tests_enabled)
197 IMPL_UTEST_ANY_BEGIN(test_suite_name, test_name, USERVER_NAMESPACE
::utest::impl::TestLauncherParametric)
198 TEST_P
(test_suite_name, test_name)
202#define IMPL_UTEST_TYPED_ANY_BEGIN(test_suite_name, test_name)
205 template <typename UtestTypeParamImpl>
208 template <typename UtestTypeParamImpl>
210 : public USERVER_NAMESPACE
::utest::impl::EnrichedFixture<UtestUserFixture<UtestTypeParamImpl>> {
211 using TypeParam = UtestTypeParamImpl;
213 using USERVER_NAMESPACE
::utest::impl::EnrichedTestBase::GetThreadCount;
214 void TestBody() override;
218
222#define IMPL_UTEST_TYPED_ANY_END(test_suite_name, test_name, thread_count, death_tests_enabled)
225 this->template RunTest<EnrichedTest>(thread_count, death_tests_enabled);
228 template <typename UtestTypeParamImpl>
232#define IMPL_UTEST_TYPED_TEST(test_suite_name, test_name, thread_count, death_tests_enabled)
234 TYPED_TEST
(test_suite_name, test_name)
238#define IMPL_UTEST_TYPED_TEST_P(test_suite_name, test_name, thread_count, death_tests_enabled)
240 TYPED_TEST_P
(test_suite_name, test_name)