Allows a value-parameterized test fixture to derive from Test and WithParamInterface<T> separately; contributed by Matt Austern.

This commit is contained in:
zhanyong.wan
2010-12-06 22:18:59 +00:00
parent b5eb6ed9e2
commit 915129ee6f
4 changed files with 127 additions and 18 deletions

View File

@@ -1589,9 +1589,13 @@ class GTEST_API_ AssertHelper {
} // namespace internal
#if GTEST_HAS_PARAM_TEST
// The abstract base class that all value-parameterized tests inherit from.
// The pure interface class that all value-parameterized tests inherit from.
// A value-parameterized class must inherit from both ::testing::Test and
// ::testing::WithParamInterface. In most cases that just means inheriting
// from ::testing::TestWithParam, but more complicated test hierarchies
// may need to inherit from Test and WithParamInterface at different levels.
//
// This class adds support for accessing the test parameter value via
// This interface has support for accessing the test parameter value via
// the GetParam() method.
//
// Use it with one of the parameter generator defining functions, like Range(),
@@ -1620,12 +1624,16 @@ class GTEST_API_ AssertHelper {
// INSTANTIATE_TEST_CASE_P(OneToTenRange, FooTest, ::testing::Range(1, 10));
template <typename T>
class TestWithParam : public Test {
class WithParamInterface {
public:
typedef T ParamType;
virtual ~WithParamInterface() {}
// The current parameter value. Is also available in the test fixture's
// constructor.
// constructor. This member function is non-static, even though it only
// references static data, to reduce the opportunity for incorrect uses
// like writing 'WithParamInterface<bool>::GetParam()' for a test that
// uses a fixture whose parameter type is int.
const ParamType& GetParam() const { return *parameter_; }
private:
@@ -1638,12 +1646,19 @@ class TestWithParam : public Test {
// Static value used for accessing parameter during a test lifetime.
static const ParamType* parameter_;
// TestClass must be a subclass of TestWithParam<T>.
// TestClass must be a subclass of WithParamInterface<T> and Test.
template <class TestClass> friend class internal::ParameterizedTestFactory;
};
template <typename T>
const T* TestWithParam<T>::parameter_ = NULL;
const T* WithParamInterface<T>::parameter_ = NULL;
// Most value-parameterized classes can ignore the existence of
// WithParamInterface, and can just inherit from ::testing::TestWithParam.
template <typename T>
class TestWithParam : public Test, public WithParamInterface<T> {
};
#endif // GTEST_HAS_PARAM_TEST