Skip to content

Commit c2f7799

Browse files
committed
[GTest] Should Register test case itself if there are not SHOULD clauses registered
1 parent 7e9e729 commit c2f7799

File tree

2 files changed

+35
-8
lines changed

2 files changed

+35
-8
lines changed

include/GUnit/GTest.h

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -89,25 +89,46 @@ class GTestAutoRegister {
8989
void MakeAndRegisterTestInfo(const TestCaseInfo& ti,
9090
detail::type<TestInfo*(const char*, const char*, const char*, const char*, const void*,
9191
void (*)(), void (*)(), internal::TestFactoryBase*)>) {
92-
internal::MakeAndRegisterTestInfo((ti.type + ti.name).c_str(), (std::string{"should "} + ti.should).c_str(), nullptr,
93-
nullptr, internal::GetTestTypeId(), Test::SetUpTestCase, Test::TearDownTestCase,
94-
new detail::GTestFactoryImpl<T>{});
92+
if (ti.should.empty()) {
93+
internal::MakeAndRegisterTestInfo(ti.type.c_str(), ti.name.c_str(), nullptr, nullptr, internal::GetTestTypeId(),
94+
Test::SetUpTestCase, Test::TearDownTestCase, new detail::GTestFactoryImpl<T>{});
95+
} else {
96+
internal::MakeAndRegisterTestInfo((ti.type + ti.name).c_str(), (std::string{"should "} + ti.should).c_str(), nullptr,
97+
nullptr, internal::GetTestTypeId(), Test::SetUpTestCase, Test::TearDownTestCase,
98+
new detail::GTestFactoryImpl<T>{});
99+
}
95100
}
96101

97102
template <class... Ts>
98103
void MakeAndRegisterTestInfo(const TestCaseInfo& ti, detail::type<TestInfo*(Ts...)>) {
99-
internal::MakeAndRegisterTestInfo((ti.type + ti.name).c_str(), (std::string{"should "} + ti.should).c_str(), nullptr,
100-
nullptr, {ti.file.c_str(), ti.line}, internal::GetTestTypeId(), Test::SetUpTestCase,
101-
Test::TearDownTestCase, new detail::GTestFactoryImpl<T>{});
104+
if (ti.should.empty()) {
105+
internal::MakeAndRegisterTestInfo(ti.type.c_str(), ti.name.c_str(), nullptr, nullptr, {ti.file.c_str(), ti.line},
106+
internal::GetTestTypeId(), Test::SetUpTestCase, Test::TearDownTestCase,
107+
new detail::GTestFactoryImpl<T>{});
108+
} else {
109+
internal::MakeAndRegisterTestInfo((ti.type + ti.name).c_str(), (std::string{"should "} + ti.should).c_str(), nullptr,
110+
nullptr, {ti.file.c_str(), ti.line}, internal::GetTestTypeId(), Test::SetUpTestCase,
111+
Test::TearDownTestCase, new detail::GTestFactoryImpl<T>{});
112+
}
102113
}
103114

104-
public:
105-
GTestAutoRegister() {
115+
bool RegisterShouldSections() {
116+
auto registered = false;
106117
for (const auto& ti : tests()) {
107118
if (get_type_name<typename T::TEST_TYPE>() == ti.type && T::TEST_NAME::c_str() == ti.name) {
108119
MakeAndRegisterTestInfo(ti, detail::type<decltype(::testing::internal::MakeAndRegisterTestInfo)>{});
120+
registered = true;
109121
}
110122
}
123+
return registered;
124+
}
125+
126+
public:
127+
GTestAutoRegister() {
128+
if (!RegisterShouldSections()) {
129+
MakeAndRegisterTestInfo({get_type_name<typename T::TEST_TYPE>(), T::TEST_NAME::c_str(), T::TEST_FILE, T::TEST_LINE, {}},
130+
detail::type<decltype(::testing::internal::MakeAndRegisterTestInfo)>{});
131+
}
111132
}
112133
};
113134

@@ -163,6 +184,8 @@ class GTest : public detail::GTest<T> {};
163184
struct GTEST<TYPE> : ::testing::detail::GTest<TYPE> { \
164185
using TEST_TYPE = TYPE; \
165186
using TEST_NAME = ::testing::detail::string<>; \
187+
static constexpr auto TEST_FILE = __FILE__; \
188+
static constexpr auto TEST_LINE = __LINE__; \
166189
void test(); \
167190
}; \
168191
::testing::detail::GTestAutoRegister<GTEST<TYPE>> __GUNIT_CAT(ar, __LINE__){}; \
@@ -176,6 +199,8 @@ class GTest : public detail::GTest<T> {};
176199
struct GTEST<TYPE, decltype(__GUNIT_CAT(GTESET_IMPL_TEST_NAME, __LINE__))> : ::testing::detail::GTest<TYPE> { \
177200
using TEST_TYPE = TYPE; \
178201
using TEST_NAME = decltype(__GUNIT_CAT(GTESET_IMPL_TEST_NAME, __LINE__)()); \
202+
static constexpr auto TEST_FILE = __FILE__; \
203+
static constexpr auto TEST_LINE = __LINE__; \
179204
void test(); \
180205
}; \
181206
::testing::detail::GTestAutoRegister<GTEST<TYPE, decltype(__GUNIT_CAT(GTESET_IMPL_TEST_NAME, __LINE__))>> __GUNIT_CAT( \

test/GTest.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -816,6 +816,8 @@ GTEST(MyTest, "[Custom Test]") {
816816
}
817817
}
818818

819+
GTEST(class TestWithoutShould, "Should Register the test case itself") { EXPECT_TRUE(true); }
820+
819821
#if __has_include(<boost / di.hpp>)
820822
class di_example {
821823
public:

0 commit comments

Comments
 (0)