1 // Copyright 2008 Google Inc.
2 // All Rights Reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
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
13 // distribution.
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.
17 //
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.
29 //
30 // Author: wan@google.com (Zhanyong Wan)
31
32 #include <list>
33 #include <set>
34
35 #include "test/gtest-typed-test_test.h"
36 #include <gtest/gtest.h>
37
38 using testing::Test;
39
40 // Used for testing that SetUpTestCase()/TearDownTestCase(), fixture
41 // ctor/dtor, and SetUp()/TearDown() work correctly in typed tests and
42 // type-parameterized test.
43 template <typename T>
44 class CommonTest : public Test {
45 // For some technical reason, SetUpTestCase() and TearDownTestCase()
46 // must be public.
47 public:
SetUpTestCase()48 static void SetUpTestCase() {
49 shared_ = new T(5);
50 }
51
TearDownTestCase()52 static void TearDownTestCase() {
53 delete shared_;
54 shared_ = NULL;
55 }
56
57 // This 'protected:' is optional. There's no harm in making all
58 // members of this fixture class template public.
59 protected:
60 typedef std::list<T> List;
61 typedef std::set<int> IntSet;
62
CommonTest()63 CommonTest() : value_(1) {}
64
~CommonTest()65 virtual ~CommonTest() { EXPECT_EQ(3, value_); }
66
SetUp()67 virtual void SetUp() {
68 EXPECT_EQ(1, value_);
69 value_++;
70 }
71
TearDown()72 virtual void TearDown() {
73 EXPECT_EQ(2, value_);
74 value_++;
75 }
76
77 T value_;
78 static T* shared_;
79 };
80
81 template <typename T>
82 T* CommonTest<T>::shared_ = NULL;
83
84 // This #ifdef block tests typed tests.
85 #if GTEST_HAS_TYPED_TEST
86
87 using testing::Types;
88
89 // Tests that SetUpTestCase()/TearDownTestCase(), fixture ctor/dtor,
90 // and SetUp()/TearDown() work correctly in typed tests
91
92 typedef Types<char, int> TwoTypes;
93 TYPED_TEST_CASE(CommonTest, TwoTypes);
94
TYPED_TEST(CommonTest,ValuesAreCorrect)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_);
99
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());
104
105 typename TestFixture::IntSet empty2;
106 EXPECT_EQ(0, empty2.size());
107
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_);
111 }
112
113 // The second test makes sure shared_ is not deleted after the first
114 // test.
TYPED_TEST(CommonTest,ValuesAreStillCorrect)115 TYPED_TEST(CommonTest, ValuesAreStillCorrect) {
116 // Static members of the fixture class template can also be visited
117 // via 'this'.
118 ASSERT_TRUE(this->shared_ != NULL);
119 EXPECT_EQ(5, *this->shared_);
120
121 // TypeParam can be used to refer to the type parameter.
122 EXPECT_EQ(static_cast<TypeParam>(2), this->value_);
123 }
124
125 // Tests that multiple TYPED_TEST_CASE's can be defined in the same
126 // translation unit.
127
128 template <typename T>
129 class TypedTest1 : public Test {
130 };
131
132 // Verifies that the second argument of TYPED_TEST_CASE can be a
133 // single type.
134 TYPED_TEST_CASE(TypedTest1, int);
TYPED_TEST(TypedTest1,A)135 TYPED_TEST(TypedTest1, A) {}
136
137 template <typename T>
138 class TypedTest2 : public Test {
139 };
140
141 // Verifies that the second argument of TYPED_TEST_CASE can be a
142 // Types<...> type list.
143 TYPED_TEST_CASE(TypedTest2, Types<int>);
144
145 // This also verifies that tests from different typed test cases can
146 // share the same name.
TYPED_TEST(TypedTest2,A)147 TYPED_TEST(TypedTest2, A) {}
148
149 // Tests that a typed test case can be defined in a namespace.
150
151 namespace library1 {
152
153 template <typename T>
154 class NumericTest : public Test {
155 };
156
157 typedef Types<int, long> NumericTypes;
158 TYPED_TEST_CASE(NumericTest, NumericTypes);
159
TYPED_TEST(NumericTest,DefaultIsZero)160 TYPED_TEST(NumericTest, DefaultIsZero) {
161 EXPECT_EQ(0, TypeParam());
162 }
163
164 } // namespace library1
165
166 #endif // GTEST_HAS_TYPED_TEST
167
168 // This #ifdef block tests type-parameterized tests.
169 #if GTEST_HAS_TYPED_TEST_P
170
171 using testing::Types;
172 using testing::internal::TypedTestCasePState;
173
174 // Tests TypedTestCasePState.
175
176 class TypedTestCasePStateTest : public Test {
177 protected:
SetUp()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");
182 }
183
184 TypedTestCasePState state_;
185 };
186
TEST_F(TypedTestCasePStateTest,SucceedsForMatchingList)187 TEST_F(TypedTestCasePStateTest, SucceedsForMatchingList) {
188 const char* tests = "A, B, C";
189 EXPECT_EQ(tests,
190 state_.VerifyRegisteredTestNames("foo.cc", 1, tests));
191 }
192
193 // Makes sure that the order of the tests and spaces around the names
194 // don't matter.
TEST_F(TypedTestCasePStateTest,IgnoresOrderAndSpaces)195 TEST_F(TypedTestCasePStateTest, IgnoresOrderAndSpaces) {
196 const char* tests = "A,C, B";
197 EXPECT_EQ(tests,
198 state_.VerifyRegisteredTestNames("foo.cc", 1, tests));
199 }
200
201 #if GTEST_HAS_DEATH_TEST
202
203 typedef TypedTestCasePStateTest TypedTestCasePStateDeathTest;
204
TEST_F(TypedTestCasePStateDeathTest,DetectsDuplicates)205 TEST_F(TypedTestCasePStateDeathTest, DetectsDuplicates) {
206 EXPECT_DEATH(
207 state_.VerifyRegisteredTestNames("foo.cc", 1, "A, B, A, C"),
208 "foo\\.cc.1.?: Test A is listed more than once\\.");
209 }
210
TEST_F(TypedTestCasePStateDeathTest,DetectsExtraTest)211 TEST_F(TypedTestCasePStateDeathTest, DetectsExtraTest) {
212 EXPECT_DEATH(
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\\.");
215 }
216
TEST_F(TypedTestCasePStateDeathTest,DetectsMissedTest)217 TEST_F(TypedTestCasePStateDeathTest, DetectsMissedTest) {
218 EXPECT_DEATH(
219 state_.VerifyRegisteredTestNames("foo.cc", 1, "A, C"),
220 "foo\\.cc.1.?: You forgot to list test B\\.");
221 }
222
223 // Tests that defining a test for a parameterized test case generates
224 // a run-time error if the test case has been registered.
TEST_F(TypedTestCasePStateDeathTest,DetectsTestAfterRegistration)225 TEST_F(TypedTestCasePStateDeathTest, DetectsTestAfterRegistration) {
226 state_.VerifyRegisteredTestNames("foo.cc", 1, "A, B, C");
227 EXPECT_DEATH(
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, \\.\\.\\.\\)\\.");
231 }
232
233 #endif // GTEST_HAS_DEATH_TEST
234
235 // Tests that SetUpTestCase()/TearDownTestCase(), fixture ctor/dtor,
236 // and SetUp()/TearDown() work correctly in type-parameterized tests.
237
238 template <typename T>
239 class DerivedTest : public CommonTest<T> {
240 };
241
242 TYPED_TEST_CASE_P(DerivedTest);
243
TYPED_TEST_P(DerivedTest,ValuesAreCorrect)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_);
248
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_);
252 }
253
254 // The second test makes sure shared_ is not deleted after the first
255 // test.
TYPED_TEST_P(DerivedTest,ValuesAreStillCorrect)256 TYPED_TEST_P(DerivedTest, ValuesAreStillCorrect) {
257 // Static members of the fixture class template can also be visited
258 // via 'this'.
259 ASSERT_TRUE(this->shared_ != NULL);
260 EXPECT_EQ(5, *this->shared_);
261 EXPECT_EQ(2, this->value_);
262 }
263
264 REGISTER_TYPED_TEST_CASE_P(DerivedTest,
265 ValuesAreCorrect, ValuesAreStillCorrect);
266
267 typedef Types<short, long> MyTwoTypes;
268 INSTANTIATE_TYPED_TEST_CASE_P(My, DerivedTest, MyTwoTypes);
269
270 // Tests that multiple TYPED_TEST_CASE_P's can be defined in the same
271 // translation unit.
272
273 template <typename T>
274 class TypedTestP1 : public Test {
275 };
276
277 TYPED_TEST_CASE_P(TypedTestP1);
278
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;
282
TYPED_TEST_P(TypedTestP1,A)283 TYPED_TEST_P(TypedTestP1, A) {}
TYPED_TEST_P(TypedTestP1,B)284 TYPED_TEST_P(TypedTestP1, B) {}
285
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;
289
290 REGISTER_TYPED_TEST_CASE_P(TypedTestP1, A, B);
291
292 template <typename T>
293 class TypedTestP2 : public Test {
294 };
295
296 TYPED_TEST_CASE_P(TypedTestP2);
297
298 // This also verifies that tests from different type-parameterized
299 // test cases can share the same name.
TYPED_TEST_P(TypedTestP2,A)300 TYPED_TEST_P(TypedTestP2, A) {}
301
302 REGISTER_TYPED_TEST_CASE_P(TypedTestP2, A);
303
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;
308
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>);
313
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>);
317
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);
323
324 // Tests that a type-parameterized test case can be defined and
325 // instantiated in a namespace.
326
327 namespace library2 {
328
329 template <typename T>
330 class NumericTest : public Test {
331 };
332
333 TYPED_TEST_CASE_P(NumericTest);
334
TYPED_TEST_P(NumericTest,DefaultIsZero)335 TYPED_TEST_P(NumericTest, DefaultIsZero) {
336 EXPECT_EQ(0, TypeParam());
337 }
338
TYPED_TEST_P(NumericTest,ZeroIsLessThanOne)339 TYPED_TEST_P(NumericTest, ZeroIsLessThanOne) {
340 EXPECT_LT(TypeParam(0), TypeParam(1));
341 }
342
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);
347
348 } // namespace library2
349
350 #endif // GTEST_HAS_TYPED_TEST_P
351
352 #if !defined(GTEST_HAS_TYPED_TEST) && !defined(GTEST_HAS_TYPED_TEST_P)
353
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.
TEST(DummyTest,TypedTestsAreNotSupportedOnThisPlatform)360 TEST(DummyTest, TypedTestsAreNotSupportedOnThisPlatform) {}
361
362 #endif // #if !defined(GTEST_HAS_TYPED_TEST) && !defined(GTEST_HAS_TYPED_TEST_P)
363