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,
35 std::function<std::unique_ptr<EnrichedTestBase>()> factory);
37void RunSetUpTestSuite(
void (*set_up_test_suite)());
38void RunTearDownTestSuite(
void (*tear_down_test_suite)());
45template <
typename UserFixture>
47class EnrichedFixture :
public UserFixture,
public EnrichedTestBase {
49 void SetUp()
override {
return UserFixture::SetUp(); }
50 void TearDown()
override {
return UserFixture::TearDown(); }
53 using EnrichedTestBase::SetThreadCount;
54 using EnrichedTestBase::TestBody;
57template <
typename Base,
typename UserFixture>
58class WithStaticMethods :
public Base {
60 static void SetUpTestSuite() {
61 RunSetUpTestSuite(&UserFixture::SetUpTestSuite);
64 static void TearDownTestSuite() {
65 RunTearDownTestSuite(&UserFixture::TearDownTestSuite);
71template <
typename UserFixture>
72class TestLauncher :
public WithStaticMethods<::testing::Test, UserFixture> {
75 template <
typename EnrichedTest>
76 static void RunTest(std::size_t thread_count,
bool death_tests_enabled) {
77 utest::impl::DoRunTest(thread_count, DeathTestsEnabled{death_tests_enabled},
78 [] {
return std::make_unique<EnrichedTest>(); });
82template <
typename UserFixture>
83class TestLauncherParametric
84 :
public WithStaticMethods<
85 ::testing::TestWithParam<
typename UserFixture::ParamType>,
89 template <
typename EnrichedTest>
90 static void RunTest(std::size_t thread_count,
bool death_tests_enabled) {
91 using ParamType =
typename UserFixture::ParamType;
92 const auto& parameter = ::testing::TestWithParam<ParamType>::GetParam();
96 auto factory = std::make_unique<
97 testing::internal::ParameterizedTestFactory<EnrichedTest>>(parameter);
99 utest::impl::DoRunTest(
100 thread_count, DeathTestsEnabled{death_tests_enabled}, [&] {
101 return std::unique_ptr<EnrichedTest>{
102 dynamic_cast<EnrichedTest*>(factory->CreateTest())};
107template <
template <
typename>
typename UserFixture,
typename T>
108using TestLauncherTyped = TestLauncher<UserFixture<T>>;
111struct DefaultNameGenerator
final {
112 template <
typename T>
113 static std::string GetName(
int i) {
114 return std::to_string(i);
118constexpr bool CheckTestSuiteNameSuffix(std::string_view str,
119 std::string_view suffix) {
120 return str.size() >= suffix.size() &&
121 str.substr(str.size() - suffix.size()) == suffix;
129#define IMPL_UTEST_NON_PARENTHESIZED(identifier) identifier
132#define IMPL_UTEST_NAMESPACE_NAME(test_suite_name) test_suite_name##_##Utest
137#define IMPL_UTEST_HIDE_ENRICHED_FROM_IDE(test_suite_name, test_name)
138 test_suite_name##_##test_name##_##Utest
141#define IMPL_UTEST_USER_FIXTURE(test_suite_name)
142 test_suite_name##_##UtestFixture
145#define IMPL_UTEST_MAKE_USER_FIXTURE_ALIAS(test_suite_name)
150#define IMPL_UTEST_MAKE_USER_FIXTURE_ALIAS_TYPED(test_suite_name)
151 template <typename UtestTypeParamImpl>
156#define IMPL_UTEST_HIDE_USER_FIXTURE_BY_TEST_LAUNCHER(test_suite_name,
157 test_launcher_template)
163#define IMPL_UTEST_HIDE_USER_FIXTURE_BY_TEST_LAUNCHER_TYPED(test_suite_name)
164 template <typename UtestTypeParamImpl>
166 USERVER_NAMESPACE
::utest::impl::TestLauncherTyped<
170#define IMPL_UTEST_TEST(test_suite_name, test_name, thread_count,
173 class EnrichedTest final
174 : public USERVER_NAMESPACE
::utest::impl::EnrichedFixture<
176 void TestBody() override;
179 TEST
(test_suite_name, test_name) {
181 test_suite_name, test_name)::EnrichedTest;
182 USERVER_NAMESPACE
::utest::impl::TestLauncher<::testing::Test>::RunTest<
183 EnrichedTest>(thread_count, death_tests_enabled);
186 test_name)::EnrichedTest::TestBody()
189#define IMPL_UTEST_ANY_BEGIN(test_suite_name, test_name,
190 test_launcher_template)
193 class EnrichedTest final
194 : public USERVER_NAMESPACE
::utest::impl::EnrichedFixture<
196 void TestBody() override;
200
203 test_launcher_template)
206#define IMPL_UTEST_ANY_END(test_suite_name, test_name, thread_count,
210 test_suite_name, test_name)::EnrichedTest;
211 this->RunTest<EnrichedTest>(thread_count, death_tests_enabled);
215 test_name)::EnrichedTest::TestBody()
218#define IMPL_UTEST_TEST_F(test_suite_name, test_name, thread_count,
221 USERVER_NAMESPACE
::utest::impl::TestLauncher)
222 TEST_F
(test_suite_name, test_name)
227#define IMPL_UTEST_TEST_P(test_suite_name, test_name, thread_count,
230 USERVER_NAMESPACE
::utest::impl::TestLauncherParametric)
231 TEST_P
(test_suite_name, test_name)
236#define IMPL_UTEST_TYPED_ANY_BEGIN(test_suite_name, test_name)
239 template <typename UtestTypeParamImpl>
240 using UtestUserFixture =
243 template <typename UtestTypeParamImpl>
245 : public USERVER_NAMESPACE
::utest::impl::EnrichedFixture<
246 UtestUserFixture<UtestTypeParamImpl>> {
247 using TypeParam = UtestTypeParamImpl;
250 using USERVER_NAMESPACE
::utest::impl::EnrichedTestBase::GetThreadCount;
251 void TestBody() override;
255
259#define IMPL_UTEST_TYPED_ANY_END(test_suite_name, test_name, thread_count,
263 test_suite_name, test_name)::EnrichedTest<TypeParam>;
264 this->template RunTest<EnrichedTest>(thread_count, death_tests_enabled);
267 template <typename UtestTypeParamImpl>
270 test_name)::EnrichedTest<UtestTypeParamImpl>::TestBody()
273#define IMPL_UTEST_TYPED_TEST(test_suite_name, test_name, thread_count,
276 TYPED_TEST
(test_suite_name, test_name)
281#define IMPL_UTEST_TYPED_TEST_P(test_suite_name, test_name, thread_count,
284 TYPED_TEST_P
(test_suite_name, test_name)