1
// Copyright 2008 Google Inc.
2
// All Rights Reserved.
4
// Redistribution and use in source and binary forms, with or without
5
// modification, are permitted provided that the following conditions are
8
// * Redistributions of source code must retain the above copyright
9
// notice, this list of conditions and the following disclaimer.
10
// * Redistributions in binary form must reproduce the above
11
// copyright notice, this list of conditions and the following disclaimer
12
// in the documentation and/or other materials provided with the
14
// * Neither the name of Google Inc. nor the names of its
15
// contributors may be used to endorse or promote products derived from
16
// this software without specific prior written permission.
18
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
// Author: wan@google.com (Zhanyong Wan)
35
#include "test/gtest-typed-test_test.h"
36
#include <gtest/gtest.h>
40
// Used for testing that SetUpTestCase()/TearDownTestCase(), fixture
41
// ctor/dtor, and SetUp()/TearDown() work correctly in typed tests and
42
// type-parameterized test.
44
class CommonTest : public Test {
45
// For some technical reason, SetUpTestCase() and TearDownTestCase()
48
static void SetUpTestCase() {
52
static void TearDownTestCase() {
57
// This 'protected:' is optional. There's no harm in making all
58
// members of this fixture class template public.
60
// We used to use std::list here, but switched to std::vector since
61
// MSVC's <list> doesn't compile cleanly with /W4.
62
typedef std::vector<T> Vector;
63
typedef std::set<int> IntSet;
65
CommonTest() : value_(1) {}
67
virtual ~CommonTest() { EXPECT_EQ(3, value_); }
69
virtual void SetUp() {
74
virtual void TearDown() {
84
T* CommonTest<T>::shared_ = NULL;
86
// This #ifdef block tests typed tests.
87
#if GTEST_HAS_TYPED_TEST
91
// Tests that SetUpTestCase()/TearDownTestCase(), fixture ctor/dtor,
92
// and SetUp()/TearDown() work correctly in typed tests
94
typedef Types<char, int> TwoTypes;
95
TYPED_TEST_CASE(CommonTest, TwoTypes);
97
TYPED_TEST(CommonTest, ValuesAreCorrect) {
98
// Static members of the fixture class template can be visited via
99
// the TestFixture:: prefix.
100
EXPECT_EQ(5, *TestFixture::shared_);
102
// Typedefs in the fixture class template can be visited via the
103
// "typename TestFixture::" prefix.
104
typename TestFixture::Vector empty;
105
EXPECT_EQ(0U, empty.size());
107
typename TestFixture::IntSet empty2;
108
EXPECT_EQ(0U, empty2.size());
110
// Non-static members of the fixture class must be visited via
111
// 'this', as required by C++ for class templates.
112
EXPECT_EQ(2, this->value_);
115
// The second test makes sure shared_ is not deleted after the first
117
TYPED_TEST(CommonTest, ValuesAreStillCorrect) {
118
// Static members of the fixture class template can also be visited
120
ASSERT_TRUE(this->shared_ != NULL);
121
EXPECT_EQ(5, *this->shared_);
123
// TypeParam can be used to refer to the type parameter.
124
EXPECT_EQ(static_cast<TypeParam>(2), this->value_);
127
// Tests that multiple TYPED_TEST_CASE's can be defined in the same
130
template <typename T>
131
class TypedTest1 : public Test {
134
// Verifies that the second argument of TYPED_TEST_CASE can be a
136
TYPED_TEST_CASE(TypedTest1, int);
137
TYPED_TEST(TypedTest1, A) {}
139
template <typename T>
140
class TypedTest2 : public Test {
143
// Verifies that the second argument of TYPED_TEST_CASE can be a
144
// Types<...> type list.
145
TYPED_TEST_CASE(TypedTest2, Types<int>);
147
// This also verifies that tests from different typed test cases can
148
// share the same name.
149
TYPED_TEST(TypedTest2, A) {}
151
// Tests that a typed test case can be defined in a namespace.
155
template <typename T>
156
class NumericTest : public Test {
159
typedef Types<int, long> NumericTypes;
160
TYPED_TEST_CASE(NumericTest, NumericTypes);
162
TYPED_TEST(NumericTest, DefaultIsZero) {
163
EXPECT_EQ(0, TypeParam());
166
} // namespace library1
168
#endif // GTEST_HAS_TYPED_TEST
170
// This #ifdef block tests type-parameterized tests.
171
#if GTEST_HAS_TYPED_TEST_P
173
using testing::Types;
174
using testing::internal::TypedTestCasePState;
176
// Tests TypedTestCasePState.
178
class TypedTestCasePStateTest : public Test {
180
virtual void SetUp() {
181
state_.AddTestName("foo.cc", 0, "FooTest", "A");
182
state_.AddTestName("foo.cc", 0, "FooTest", "B");
183
state_.AddTestName("foo.cc", 0, "FooTest", "C");
186
TypedTestCasePState state_;
189
TEST_F(TypedTestCasePStateTest, SucceedsForMatchingList) {
190
const char* tests = "A, B, C";
192
state_.VerifyRegisteredTestNames("foo.cc", 1, tests));
195
// Makes sure that the order of the tests and spaces around the names
197
TEST_F(TypedTestCasePStateTest, IgnoresOrderAndSpaces) {
198
const char* tests = "A,C, B";
200
state_.VerifyRegisteredTestNames("foo.cc", 1, tests));
203
typedef TypedTestCasePStateTest TypedTestCasePStateDeathTest;
205
TEST_F(TypedTestCasePStateDeathTest, DetectsDuplicates) {
206
EXPECT_DEATH_IF_SUPPORTED(
207
state_.VerifyRegisteredTestNames("foo.cc", 1, "A, B, A, C"),
208
"foo\\.cc.1.?: Test A is listed more than once\\.");
211
TEST_F(TypedTestCasePStateDeathTest, DetectsExtraTest) {
212
EXPECT_DEATH_IF_SUPPORTED(
213
state_.VerifyRegisteredTestNames("foo.cc", 1, "A, B, C, D"),
214
"foo\\.cc.1.?: No test named D can be found in this test case\\.");
217
TEST_F(TypedTestCasePStateDeathTest, DetectsMissedTest) {
218
EXPECT_DEATH_IF_SUPPORTED(
219
state_.VerifyRegisteredTestNames("foo.cc", 1, "A, C"),
220
"foo\\.cc.1.?: You forgot to list test B\\.");
223
// Tests that defining a test for a parameterized test case generates
224
// a run-time error if the test case has been registered.
225
TEST_F(TypedTestCasePStateDeathTest, DetectsTestAfterRegistration) {
226
state_.VerifyRegisteredTestNames("foo.cc", 1, "A, B, C");
227
EXPECT_DEATH_IF_SUPPORTED(
228
state_.AddTestName("foo.cc", 2, "FooTest", "D"),
229
"foo\\.cc.2.?: Test D must be defined before REGISTER_TYPED_TEST_CASE_P"
230
"\\(FooTest, \\.\\.\\.\\)\\.");
233
// Tests that SetUpTestCase()/TearDownTestCase(), fixture ctor/dtor,
234
// and SetUp()/TearDown() work correctly in type-parameterized tests.
236
template <typename T>
237
class DerivedTest : public CommonTest<T> {
240
TYPED_TEST_CASE_P(DerivedTest);
242
TYPED_TEST_P(DerivedTest, ValuesAreCorrect) {
243
// Static members of the fixture class template can be visited via
244
// the TestFixture:: prefix.
245
EXPECT_EQ(5, *TestFixture::shared_);
247
// Non-static members of the fixture class must be visited via
248
// 'this', as required by C++ for class templates.
249
EXPECT_EQ(2, this->value_);
252
// The second test makes sure shared_ is not deleted after the first
254
TYPED_TEST_P(DerivedTest, ValuesAreStillCorrect) {
255
// Static members of the fixture class template can also be visited
257
ASSERT_TRUE(this->shared_ != NULL);
258
EXPECT_EQ(5, *this->shared_);
259
EXPECT_EQ(2, this->value_);
262
REGISTER_TYPED_TEST_CASE_P(DerivedTest,
263
ValuesAreCorrect, ValuesAreStillCorrect);
265
typedef Types<short, long> MyTwoTypes;
266
INSTANTIATE_TYPED_TEST_CASE_P(My, DerivedTest, MyTwoTypes);
268
// Tests that multiple TYPED_TEST_CASE_P's can be defined in the same
271
template <typename T>
272
class TypedTestP1 : public Test {
275
TYPED_TEST_CASE_P(TypedTestP1);
277
// For testing that the code between TYPED_TEST_CASE_P() and
278
// TYPED_TEST_P() is not enclosed in a namespace.
279
typedef int IntAfterTypedTestCaseP;
281
TYPED_TEST_P(TypedTestP1, A) {}
282
TYPED_TEST_P(TypedTestP1, B) {}
284
// For testing that the code between TYPED_TEST_P() and
285
// REGISTER_TYPED_TEST_CASE_P() is not enclosed in a namespace.
286
typedef int IntBeforeRegisterTypedTestCaseP;
288
REGISTER_TYPED_TEST_CASE_P(TypedTestP1, A, B);
290
template <typename T>
291
class TypedTestP2 : public Test {
294
TYPED_TEST_CASE_P(TypedTestP2);
296
// This also verifies that tests from different type-parameterized
297
// test cases can share the same name.
298
TYPED_TEST_P(TypedTestP2, A) {}
300
REGISTER_TYPED_TEST_CASE_P(TypedTestP2, A);
302
// Verifies that the code between TYPED_TEST_CASE_P() and
303
// REGISTER_TYPED_TEST_CASE_P() is not enclosed in a namespace.
304
IntAfterTypedTestCaseP after = 0;
305
IntBeforeRegisterTypedTestCaseP before = 0;
307
// Verifies that the last argument of INSTANTIATE_TYPED_TEST_CASE_P()
308
// can be either a single type or a Types<...> type list.
309
INSTANTIATE_TYPED_TEST_CASE_P(Int, TypedTestP1, int);
310
INSTANTIATE_TYPED_TEST_CASE_P(Int, TypedTestP2, Types<int>);
312
// Tests that the same type-parameterized test case can be
313
// instantiated more than once in the same translation unit.
314
INSTANTIATE_TYPED_TEST_CASE_P(Double, TypedTestP2, Types<double>);
316
// Tests that the same type-parameterized test case can be
317
// instantiated in different translation units linked together.
318
// (ContainerTest is also instantiated in gtest-typed-test_test.cc.)
319
typedef Types<std::vector<double>, std::set<char> > MyContainers;
320
INSTANTIATE_TYPED_TEST_CASE_P(My, ContainerTest, MyContainers);
322
// Tests that a type-parameterized test case can be defined and
323
// instantiated in a namespace.
327
template <typename T>
328
class NumericTest : public Test {
331
TYPED_TEST_CASE_P(NumericTest);
333
TYPED_TEST_P(NumericTest, DefaultIsZero) {
334
EXPECT_EQ(0, TypeParam());
337
TYPED_TEST_P(NumericTest, ZeroIsLessThanOne) {
338
EXPECT_LT(TypeParam(0), TypeParam(1));
341
REGISTER_TYPED_TEST_CASE_P(NumericTest,
342
DefaultIsZero, ZeroIsLessThanOne);
343
typedef Types<int, double> NumericTypes;
344
INSTANTIATE_TYPED_TEST_CASE_P(My, NumericTest, NumericTypes);
346
} // namespace library2
348
#endif // GTEST_HAS_TYPED_TEST_P
350
#if !defined(GTEST_HAS_TYPED_TEST) && !defined(GTEST_HAS_TYPED_TEST_P)
352
// Google Test may not support type-parameterized tests with some
353
// compilers. If we use conditional compilation to compile out all
354
// code referring to the gtest_main library, MSVC linker will not link
355
// that library at all and consequently complain about missing entry
356
// point defined in that library (fatal error LNK1561: entry point
357
// must be defined). This dummy test keeps gtest_main linked in.
358
TEST(DummyTest, TypedTestsAreNotSupportedOnThisPlatform) {}
360
#endif // #if !defined(GTEST_HAS_TYPED_TEST) && !defined(GTEST_HAS_TYPED_TEST_P)