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
typedef std::list<T> List;
61
typedef std::set<int> IntSet;
63
CommonTest() : value_(1) {}
65
virtual ~CommonTest() { EXPECT_EQ(3, value_); }
67
virtual void SetUp() {
72
virtual void TearDown() {
82
T* CommonTest<T>::shared_ = NULL;
84
// This #ifdef block tests typed tests.
85
#if GTEST_HAS_TYPED_TEST
89
// Tests that SetUpTestCase()/TearDownTestCase(), fixture ctor/dtor,
90
// and SetUp()/TearDown() work correctly in typed tests
92
typedef Types<char, int> TwoTypes;
93
TYPED_TEST_CASE(CommonTest, TwoTypes);
95
TYPED_TEST(CommonTest, ValuesAreCorrect) {
96
// Static members of the fixture class template can be visited via
97
// the TestFixture:: prefix.
98
EXPECT_EQ(5, *TestFixture::shared_);
100
// Typedefs in the fixture class template can be visited via the
101
// "typename TestFixture::" prefix.
102
typename TestFixture::List empty;
103
EXPECT_EQ(0, empty.size());
105
typename TestFixture::IntSet empty2;
106
EXPECT_EQ(0, empty2.size());
108
// Non-static members of the fixture class must be visited via
109
// 'this', as required by C++ for class templates.
110
EXPECT_EQ(2, this->value_);
113
// The second test makes sure shared_ is not deleted after the first
115
TYPED_TEST(CommonTest, ValuesAreStillCorrect) {
116
// Static members of the fixture class template can also be visited
118
ASSERT_TRUE(this->shared_ != NULL);
119
EXPECT_EQ(5, *this->shared_);
121
// TypeParam can be used to refer to the type parameter.
122
EXPECT_EQ(static_cast<TypeParam>(2), this->value_);
125
// Tests that multiple TYPED_TEST_CASE's can be defined in the same
128
template <typename T>
129
class TypedTest1 : public Test {
132
// Verifies that the second argument of TYPED_TEST_CASE can be a
134
TYPED_TEST_CASE(TypedTest1, int);
135
TYPED_TEST(TypedTest1, A) {}
137
template <typename T>
138
class TypedTest2 : public Test {
141
// Verifies that the second argument of TYPED_TEST_CASE can be a
142
// Types<...> type list.
143
TYPED_TEST_CASE(TypedTest2, Types<int>);
145
// This also verifies that tests from different typed test cases can
146
// share the same name.
147
TYPED_TEST(TypedTest2, A) {}
149
// Tests that a typed test case can be defined in a namespace.
153
template <typename T>
154
class NumericTest : public Test {
157
typedef Types<int, long> NumericTypes;
158
TYPED_TEST_CASE(NumericTest, NumericTypes);
160
TYPED_TEST(NumericTest, DefaultIsZero) {
161
EXPECT_EQ(0, TypeParam());
164
} // namespace library1
166
#endif // GTEST_HAS_TYPED_TEST
168
// This #ifdef block tests type-parameterized tests.
169
#if GTEST_HAS_TYPED_TEST_P
171
using testing::Types;
172
using testing::internal::TypedTestCasePState;
174
// Tests TypedTestCasePState.
176
class TypedTestCasePStateTest : public Test {
178
virtual void SetUp() {
179
state_.AddTestName("foo.cc", 0, "FooTest", "A");
180
state_.AddTestName("foo.cc", 0, "FooTest", "B");
181
state_.AddTestName("foo.cc", 0, "FooTest", "C");
184
TypedTestCasePState state_;
187
TEST_F(TypedTestCasePStateTest, SucceedsForMatchingList) {
188
const char* tests = "A, B, C";
190
state_.VerifyRegisteredTestNames("foo.cc", 1, tests));
193
// Makes sure that the order of the tests and spaces around the names
195
TEST_F(TypedTestCasePStateTest, IgnoresOrderAndSpaces) {
196
const char* tests = "A,C, B";
198
state_.VerifyRegisteredTestNames("foo.cc", 1, tests));
201
#if GTEST_HAS_DEATH_TEST
203
typedef TypedTestCasePStateTest TypedTestCasePStateDeathTest;
205
TEST_F(TypedTestCasePStateDeathTest, DetectsDuplicates) {
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) {
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) {
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");
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
#endif // GTEST_HAS_DEATH_TEST
235
// Tests that SetUpTestCase()/TearDownTestCase(), fixture ctor/dtor,
236
// and SetUp()/TearDown() work correctly in type-parameterized tests.
238
template <typename T>
239
class DerivedTest : public CommonTest<T> {
242
TYPED_TEST_CASE_P(DerivedTest);
244
TYPED_TEST_P(DerivedTest, ValuesAreCorrect) {
245
// Static members of the fixture class template can be visited via
246
// the TestFixture:: prefix.
247
EXPECT_EQ(5, *TestFixture::shared_);
249
// Non-static members of the fixture class must be visited via
250
// 'this', as required by C++ for class templates.
251
EXPECT_EQ(2, this->value_);
254
// The second test makes sure shared_ is not deleted after the first
256
TYPED_TEST_P(DerivedTest, ValuesAreStillCorrect) {
257
// Static members of the fixture class template can also be visited
259
ASSERT_TRUE(this->shared_ != NULL);
260
EXPECT_EQ(5, *this->shared_);
261
EXPECT_EQ(2, this->value_);
264
REGISTER_TYPED_TEST_CASE_P(DerivedTest,
265
ValuesAreCorrect, ValuesAreStillCorrect);
267
typedef Types<short, long> MyTwoTypes;
268
INSTANTIATE_TYPED_TEST_CASE_P(My, DerivedTest, MyTwoTypes);
270
// Tests that multiple TYPED_TEST_CASE_P's can be defined in the same
273
template <typename T>
274
class TypedTestP1 : public Test {
277
TYPED_TEST_CASE_P(TypedTestP1);
279
// For testing that the code between TYPED_TEST_CASE_P() and
280
// TYPED_TEST_P() is not enclosed in a namespace.
281
typedef int IntAfterTypedTestCaseP;
283
TYPED_TEST_P(TypedTestP1, A) {}
284
TYPED_TEST_P(TypedTestP1, B) {}
286
// For testing that the code between TYPED_TEST_P() and
287
// REGISTER_TYPED_TEST_CASE_P() is not enclosed in a namespace.
288
typedef int IntBeforeRegisterTypedTestCaseP;
290
REGISTER_TYPED_TEST_CASE_P(TypedTestP1, A, B);
292
template <typename T>
293
class TypedTestP2 : public Test {
296
TYPED_TEST_CASE_P(TypedTestP2);
298
// This also verifies that tests from different type-parameterized
299
// test cases can share the same name.
300
TYPED_TEST_P(TypedTestP2, A) {}
302
REGISTER_TYPED_TEST_CASE_P(TypedTestP2, A);
304
// Verifies that the code between TYPED_TEST_CASE_P() and
305
// REGISTER_TYPED_TEST_CASE_P() is not enclosed in a namespace.
306
IntAfterTypedTestCaseP after = 0;
307
IntBeforeRegisterTypedTestCaseP before = 0;
309
// Verifies that the last argument of INSTANTIATE_TYPED_TEST_CASE_P()
310
// can be either a single type or a Types<...> type list.
311
INSTANTIATE_TYPED_TEST_CASE_P(Int, TypedTestP1, int);
312
INSTANTIATE_TYPED_TEST_CASE_P(Int, TypedTestP2, Types<int>);
314
// Tests that the same type-parameterized test case can be
315
// instantiated more than once in the same translation unit.
316
INSTANTIATE_TYPED_TEST_CASE_P(Double, TypedTestP2, Types<double>);
318
// Tests that the same type-parameterized test case can be
319
// instantiated in different translation units linked together.
320
// (ContainerTest is also instantiated in gtest-typed-test_test.cc.)
321
typedef Types<std::list<double>, std::set<char> > MyContainers;
322
INSTANTIATE_TYPED_TEST_CASE_P(My, ContainerTest, MyContainers);
324
// Tests that a type-parameterized test case can be defined and
325
// instantiated in a namespace.
329
template <typename T>
330
class NumericTest : public Test {
333
TYPED_TEST_CASE_P(NumericTest);
335
TYPED_TEST_P(NumericTest, DefaultIsZero) {
336
EXPECT_EQ(0, TypeParam());
339
TYPED_TEST_P(NumericTest, ZeroIsLessThanOne) {
340
EXPECT_LT(TypeParam(0), TypeParam(1));
343
REGISTER_TYPED_TEST_CASE_P(NumericTest,
344
DefaultIsZero, ZeroIsLessThanOne);
345
typedef Types<int, double> NumericTypes;
346
INSTANTIATE_TYPED_TEST_CASE_P(My, NumericTest, NumericTypes);
348
} // namespace library2
350
#endif // GTEST_HAS_TYPED_TEST_P
352
#if !defined(GTEST_HAS_TYPED_TEST) && !defined(GTEST_HAS_TYPED_TEST_P)
354
// Google Test doesn't support type-parameterized tests on some platforms
355
// and compilers, such as MSVC 7.1. If we use conditional compilation to
356
// compile out all code referring to the gtest_main library, MSVC linker
357
// will not link that library at all and consequently complain about
358
// missing entry point defined in that library (fatal error LNK1561:
359
// entry point must be defined). This dummy test keeps gtest_main linked in.
360
TEST(DummyTest, TypedTestsAreNotSupportedOnThisPlatform) {}
362
#endif // #if !defined(GTEST_HAS_TYPED_TEST) && !defined(GTEST_HAS_TYPED_TEST_P)