Commit e2504d85 authored by Mohammed Das's avatar Mohammed Das Committed by Facebook GitHub Bot

Add CO_TYPED_TEST to GtestHelpers.h (#1669)

Summary:
Pull Request resolved: https://github.com/facebook/folly/pull/1669

Similar to CO_TEST_[F|P], adding CO_TYPED_TEST which allows test cases to use co_await/co_return and blocks on the Task<> separately.

Reviewed By: iahs

Differential Revision: D31621981

fbshipit-source-id: 83618de2d106954bde819d90afde0c0140674d54
parent e9943344
...@@ -144,6 +144,48 @@ ...@@ -144,6 +144,48 @@
folly::coro::Task<void> GTEST_TEST_CLASS_NAME_( \ folly::coro::Task<void> GTEST_TEST_CLASS_NAME_( \
test_suite_name, test_name)::co_TestBody() test_suite_name, test_name)::co_TestBody()
#define CO_TYPED_TEST(CaseName, TestName) \
static_assert( \
sizeof(GTEST_STRINGIFY_(TestName)) > 1, "test-name must not be empty"); \
template <typename gtest_TypeParam_> \
class GTEST_TEST_CLASS_NAME_(CaseName, TestName) \
: public CaseName<gtest_TypeParam_> { \
private: \
typedef CaseName<gtest_TypeParam_> TestFixture; \
typedef gtest_TypeParam_ TypeParam; \
void TestBody() override; \
folly::coro::Task<void> co_TestBody(); \
}; \
static bool gtest_##CaseName##_##TestName##_registered_ \
GTEST_ATTRIBUTE_UNUSED_ = ::testing::internal::TypeParameterizedTest< \
CaseName, \
::testing::internal::TemplateSel<GTEST_TEST_CLASS_NAME_( \
CaseName, TestName)>, \
GTEST_TYPE_PARAMS_(CaseName)>:: \
Register( \
"", \
::testing::internal::CodeLocation(__FILE__, __LINE__), \
GTEST_STRINGIFY_(CaseName), \
GTEST_STRINGIFY_(TestName), \
0, \
::testing::internal::GenerateNames< \
GTEST_NAME_GENERATOR_(CaseName), \
GTEST_TYPE_PARAMS_(CaseName)>()); \
template <typename gtest_TypeParam_> \
void GTEST_TEST_CLASS_NAME_( \
CaseName, TestName)<gtest_TypeParam_>::TestBody() { \
try { \
folly::coro::blockingWait(co_TestBody()); \
} catch (const std::exception& ex) { \
GTEST_LOG_(ERROR) << ex.what() << ", async stack trace: " \
<< folly::exception_tracer::getAsyncTrace(ex); \
throw; \
} \
} \
template <typename gtest_TypeParam_> \
folly::coro::Task<void> GTEST_TEST_CLASS_NAME_( \
CaseName, TestName)<gtest_TypeParam_>::co_TestBody()
/** /**
* Coroutine versions of GTests's Assertion predicate macros. Use these in place * Coroutine versions of GTests's Assertion predicate macros. Use these in place
* of ASSERT_* in CO_TEST or coroutine functions. * of ASSERT_* in CO_TEST or coroutine functions.
......
...@@ -51,3 +51,43 @@ INSTANTIATE_TEST_CASE_P( ...@@ -51,3 +51,43 @@ INSTANTIATE_TEST_CASE_P(
{2, 2, 4}, {2, 2, 4},
{-1, -6, 6}, {-1, -6, 6},
})); }));
namespace {
// allow same test case to be used over different types.
template <typename T>
struct GtestHelpersTypedTest : public ::testing::Test {
using type = T;
folly::coro::Task<std::string> type_str() const {
co_return std::string(folly::demangle(typeid(type).name()));
}
};
struct Foo {};
// also allow same test case to be used with different fixture classes.
template <>
struct GtestHelpersTypedTest<Foo> : public ::testing::Test {
using type = Foo;
folly::coro::Task<std::string> type_str() const { co_return "Foo_x"; }
};
using CoroTypedTests = ::testing::Types<int, char, Foo>;
TYPED_TEST_SUITE(GtestHelpersTypedTest, CoroTypedTests);
CO_TYPED_TEST(GtestHelpersTypedTest, Test_type_str) {
using type = typename std::remove_reference_t<decltype(*this)>::type;
if constexpr (std::is_same_v<Foo, type>) {
EXPECT_EQ(co_await this->type_str(), "Foo_x");
} else if constexpr (std::is_same_v<int, type>) {
EXPECT_EQ(co_await this->type_str(), "int");
} else if constexpr (std::is_same_v<char, type>) {
EXPECT_EQ(co_await this->type_str(), "char");
}
}
} // namespace
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment