Skip to content

Commit 2826446

Browse files
authored
Merge pull request #311 from m-pilia/gtest-type-parameterized-fixture
Add GTest type-parameterized fixture
2 parents 1128686 + 3d78467 commit 2826446

3 files changed

Lines changed: 71 additions & 0 deletions

File tree

doc/gtest.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,20 @@ Analogous to the Google Test `TEST_F` macro, defines a RapidCheck property as a
3939

4040
Since this macro is implemented in terms of Google Test's `TEST` macro and Google Test does not allow mixing of `TEST` and `TEST_F` for the same test case, test cases, a property tied to a fixture named `Fixture` will be registered under a test case named `Fixed_RapidCheck`. This is usually not a big issue but is something to be aware of, in particular when filtering Google Test case names from the command line.
4141

42+
### `RC_GTEST_TYPED_FIXTURE_PROP(Fixture, Name, (args...))`
43+
44+
Analogous to the Google Test `TYPED_TEST` macro, defines a RapidCheck property as a Google Test typed (or type-parameterized) fixture based test. `Fixture` names the test fixture class, which must take a template parameter and is reinstantiated for every test case that is run.
45+
46+
Similarly to `TYPED_TEST`, the type parameter can be accessed as `TypeParam` from both the argument list and the body of the property:
47+
```C++
48+
RC_GTEST_TYPED_FIXTURE_PROP(MyTypedFixture,
49+
genericSum,
50+
(TypeParam a, TypeParam b)) {
51+
const TypeParam result = std::plus<TypeParam>{}(a, b);
52+
RC_ASSERT(result == (a + b));
53+
}
54+
```
55+
4256
## Assertions
4357

4458
RapidCheck will treat any exception as a property failure so you should be able to use any assertion mechanism that signals failures as exceptions. However, Google Test assertions are not implemented using exceptions which means that you should avoid them and use RapidCheck assertions such as `RC_ASSERT` in your RapidCheck properties instead.

examples/gtest/main.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,37 @@ TEST_F(MyFixture, incrementIncrementsByOne) {
6060
ASSERT_EQ(1U, counter);
6161
}
6262

63+
// Typed test fixtures are also supported:
64+
template <typename TypeParam>
65+
class MyTypedFixture : public ::testing::Test {
66+
protected:
67+
void add(const TypeParam x) { sum += x; }
68+
69+
TypeParam sum{};
70+
};
71+
72+
// A typed suite can be defined as usual ...
73+
using TestTypes = ::testing::Types<int, float, double>;
74+
TYPED_TEST_SUITE(MyTypedFixture, TestTypes, );
75+
76+
// ... and used with properties ...
77+
RC_GTEST_TYPED_FIXTURE_PROP(MyTypedFixture,
78+
typeParameterizedProperty,
79+
(TypeParam a, TypeParam b)) {
80+
this->add(a);
81+
this->add(b);
82+
RC_ASSERT(this->sum == (a + b));
83+
}
84+
85+
// ...as with regular typed tests:
86+
TYPED_TEST(MyTypedFixture, test) {
87+
const auto a = static_cast<TypeParam>(10);
88+
const auto b = static_cast<TypeParam>(20);
89+
this->add(a);
90+
this->add(b);
91+
ASSERT_EQ(this->sum, a + b);
92+
}
93+
6394
int main(int argc, char **argv) {
6495
::testing::InitGoogleTest(&argc, argv);
6596
return RUN_ALL_TESTS();

extras/gtest/include/rapidcheck/gtest.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,29 @@ void checkGTest(Testable &&testable) {
6060
} \
6161
\
6262
void RapidCheckPropImpl_##Fixture##_##Name::operator() ArgList
63+
64+
/// Defines a RapidCheck property as a typed Google Test.
65+
///
66+
/// TypedFixture is a Google Test typed (or type-parameterized) test
67+
/// fixture. The test type can be accessed from ArgList or from the
68+
/// property body as TypeParam (similarly to a TYPED_TEST).
69+
///
70+
/// The fixture is reinstantiated for each test case of the property.
71+
#define RC_GTEST_TYPED_FIXTURE_PROP(Fixture, Name, ArgList) \
72+
template <typename TypeParam> \
73+
class RapidCheckPropImpl_##Fixture##_##Name : public Fixture<TypeParam> { \
74+
public: \
75+
void rapidCheck_fixtureSetUp() { this->SetUp(); } \
76+
void TestBody() override {} \
77+
void operator() ArgList; \
78+
void rapidCheck_fixtureTearDown() { this->TearDown(); } \
79+
}; \
80+
\
81+
TYPED_TEST(Fixture, Name) { \
82+
::rc::detail::checkGTest( \
83+
&rc::detail::ExecFixture< \
84+
RapidCheckPropImpl_##Fixture##_##Name<TypeParam>>::exec); \
85+
} \
86+
\
87+
template <typename TypeParam> \
88+
void RapidCheckPropImpl_##Fixture##_##Name<TypeParam>::operator() ArgList

0 commit comments

Comments
 (0)