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.
31 #include "test/gtest-typed-test_test.h"
36 #include "gtest/gtest.h"
39 GTEST_DISABLE_MSC_WARNINGS_PUSH_(4127 /* conditional expression is constant */)
44 // Used for testing that SetUpTestCase()/TearDownTestCase(), fixture
45 // ctor/dtor, and SetUp()/TearDown() work correctly in typed tests and
46 // type-parameterized test.
48 class CommonTest : public Test {
49 // For some technical reason, SetUpTestCase() and TearDownTestCase()
52 static void SetUpTestCase() {
56 static void TearDownTestCase() {
61 // This 'protected:' is optional. There's no harm in making all
62 // members of this fixture class template public.
64 // We used to use std::list here, but switched to std::vector since
65 // MSVC's <list> doesn't compile cleanly with /W4.
66 typedef std::vector<T> Vector;
67 typedef std::set<int> IntSet;
69 CommonTest() : value_(1) {}
71 virtual ~CommonTest() { EXPECT_EQ(3, value_); }
73 virtual void SetUp() {
78 virtual void TearDown() {
88 T* CommonTest<T>::shared_ = NULL;
90 // This #ifdef block tests typed tests.
91 #if GTEST_HAS_TYPED_TEST
95 // Tests that SetUpTestCase()/TearDownTestCase(), fixture ctor/dtor,
96 // and SetUp()/TearDown() work correctly in typed tests
98 typedef Types<char, int> TwoTypes;
99 TYPED_TEST_CASE(CommonTest, TwoTypes);
101 TYPED_TEST(CommonTest, ValuesAreCorrect) {
102 // Static members of the fixture class template can be visited via
103 // the TestFixture:: prefix.
104 EXPECT_EQ(5, *TestFixture::shared_);
106 // Typedefs in the fixture class template can be visited via the
107 // "typename TestFixture::" prefix.
108 typename TestFixture::Vector empty;
109 EXPECT_EQ(0U, empty.size());
111 typename TestFixture::IntSet empty2;
112 EXPECT_EQ(0U, empty2.size());
114 // Non-static members of the fixture class must be visited via
115 // 'this', as required by C++ for class templates.
116 EXPECT_EQ(2, this->value_);
119 // The second test makes sure shared_ is not deleted after the first
121 TYPED_TEST(CommonTest, ValuesAreStillCorrect) {
122 // Static members of the fixture class template can also be visited
124 ASSERT_TRUE(this->shared_ != NULL);
125 EXPECT_EQ(5, *this->shared_);
127 // TypeParam can be used to refer to the type parameter.
128 EXPECT_EQ(static_cast<TypeParam>(2), this->value_);
131 // Tests that multiple TYPED_TEST_CASE's can be defined in the same
134 template <typename T>
135 class TypedTest1 : public Test {
138 // Verifies that the second argument of TYPED_TEST_CASE can be a
140 TYPED_TEST_CASE(TypedTest1, int);
141 TYPED_TEST(TypedTest1, A) {}
143 template <typename T>
144 class TypedTest2 : public Test {
147 // Verifies that the second argument of TYPED_TEST_CASE can be a
148 // Types<...> type list.
149 TYPED_TEST_CASE(TypedTest2, Types<int>);
151 // This also verifies that tests from different typed test cases can
152 // share the same name.
153 TYPED_TEST(TypedTest2, A) {}
155 // Tests that a typed test case can be defined in a namespace.
159 template <typename T>
160 class NumericTest : public Test {
163 typedef Types<int, long> NumericTypes;
164 TYPED_TEST_CASE(NumericTest, NumericTypes);
166 TYPED_TEST(NumericTest, DefaultIsZero) {
167 EXPECT_EQ(0, TypeParam());
170 } // namespace library1
172 // Tests that custom names work.
173 template <typename T>
174 class TypedTestWithNames : public Test {};
176 class TypedTestNames {
178 template <typename T>
179 static std::string GetName(int i) {
180 if (testing::internal::IsSame<T, char>::value) {
181 return std::string("char") + ::testing::PrintToString(i);
183 if (testing::internal::IsSame<T, int>::value) {
184 return std::string("int") + ::testing::PrintToString(i);
189 TYPED_TEST_CASE(TypedTestWithNames, TwoTypes, TypedTestNames);
191 TYPED_TEST(TypedTestWithNames, TestCaseName) {
192 if (testing::internal::IsSame<TypeParam, char>::value) {
193 EXPECT_STREQ(::testing::UnitTest::GetInstance()
194 ->current_test_info()
196 "TypedTestWithNames/char0");
198 if (testing::internal::IsSame<TypeParam, int>::value) {
199 EXPECT_STREQ(::testing::UnitTest::GetInstance()
200 ->current_test_info()
202 "TypedTestWithNames/int1");
206 #endif // GTEST_HAS_TYPED_TEST
208 // This #ifdef block tests type-parameterized tests.
209 #if GTEST_HAS_TYPED_TEST_P
211 using testing::Types;
212 using testing::internal::TypedTestCasePState;
214 // Tests TypedTestCasePState.
216 class TypedTestCasePStateTest : public Test {
218 virtual void SetUp() {
219 state_.AddTestName("foo.cc", 0, "FooTest", "A");
220 state_.AddTestName("foo.cc", 0, "FooTest", "B");
221 state_.AddTestName("foo.cc", 0, "FooTest", "C");
224 TypedTestCasePState state_;
227 TEST_F(TypedTestCasePStateTest, SucceedsForMatchingList) {
228 const char* tests = "A, B, C";
230 state_.VerifyRegisteredTestNames("foo.cc", 1, tests));
233 // Makes sure that the order of the tests and spaces around the names
235 TEST_F(TypedTestCasePStateTest, IgnoresOrderAndSpaces) {
236 const char* tests = "A,C, B";
238 state_.VerifyRegisteredTestNames("foo.cc", 1, tests));
241 typedef TypedTestCasePStateTest TypedTestCasePStateDeathTest;
243 TEST_F(TypedTestCasePStateDeathTest, DetectsDuplicates) {
244 EXPECT_DEATH_IF_SUPPORTED(
245 state_.VerifyRegisteredTestNames("foo.cc", 1, "A, B, A, C"),
246 "foo\\.cc.1.?: Test A is listed more than once\\.");
249 TEST_F(TypedTestCasePStateDeathTest, DetectsExtraTest) {
250 EXPECT_DEATH_IF_SUPPORTED(
251 state_.VerifyRegisteredTestNames("foo.cc", 1, "A, B, C, D"),
252 "foo\\.cc.1.?: No test named D can be found in this test case\\.");
255 TEST_F(TypedTestCasePStateDeathTest, DetectsMissedTest) {
256 EXPECT_DEATH_IF_SUPPORTED(
257 state_.VerifyRegisteredTestNames("foo.cc", 1, "A, C"),
258 "foo\\.cc.1.?: You forgot to list test B\\.");
261 // Tests that defining a test for a parameterized test case generates
262 // a run-time error if the test case has been registered.
263 TEST_F(TypedTestCasePStateDeathTest, DetectsTestAfterRegistration) {
264 state_.VerifyRegisteredTestNames("foo.cc", 1, "A, B, C");
265 EXPECT_DEATH_IF_SUPPORTED(
266 state_.AddTestName("foo.cc", 2, "FooTest", "D"),
267 "foo\\.cc.2.?: Test D must be defined before REGISTER_TYPED_TEST_CASE_P"
268 "\\(FooTest, \\.\\.\\.\\)\\.");
271 // Tests that SetUpTestCase()/TearDownTestCase(), fixture ctor/dtor,
272 // and SetUp()/TearDown() work correctly in type-parameterized tests.
274 template <typename T>
275 class DerivedTest : public CommonTest<T> {
278 TYPED_TEST_CASE_P(DerivedTest);
280 TYPED_TEST_P(DerivedTest, ValuesAreCorrect) {
281 // Static members of the fixture class template can be visited via
282 // the TestFixture:: prefix.
283 EXPECT_EQ(5, *TestFixture::shared_);
285 // Non-static members of the fixture class must be visited via
286 // 'this', as required by C++ for class templates.
287 EXPECT_EQ(2, this->value_);
290 // The second test makes sure shared_ is not deleted after the first
292 TYPED_TEST_P(DerivedTest, ValuesAreStillCorrect) {
293 // Static members of the fixture class template can also be visited
295 ASSERT_TRUE(this->shared_ != NULL);
296 EXPECT_EQ(5, *this->shared_);
297 EXPECT_EQ(2, this->value_);
300 REGISTER_TYPED_TEST_CASE_P(DerivedTest,
301 ValuesAreCorrect, ValuesAreStillCorrect);
303 typedef Types<short, long> MyTwoTypes;
304 INSTANTIATE_TYPED_TEST_CASE_P(My, DerivedTest, MyTwoTypes);
306 // Tests that custom names work with type parametrized tests. We reuse the
307 // TwoTypes from above here.
308 template <typename T>
309 class TypeParametrizedTestWithNames : public Test {};
311 TYPED_TEST_CASE_P(TypeParametrizedTestWithNames);
313 TYPED_TEST_P(TypeParametrizedTestWithNames, TestCaseName) {
314 if (testing::internal::IsSame<TypeParam, char>::value) {
315 EXPECT_STREQ(::testing::UnitTest::GetInstance()
316 ->current_test_info()
318 "CustomName/TypeParametrizedTestWithNames/parChar0");
320 if (testing::internal::IsSame<TypeParam, int>::value) {
321 EXPECT_STREQ(::testing::UnitTest::GetInstance()
322 ->current_test_info()
324 "CustomName/TypeParametrizedTestWithNames/parInt1");
328 REGISTER_TYPED_TEST_CASE_P(TypeParametrizedTestWithNames, TestCaseName);
330 class TypeParametrizedTestNames {
332 template <typename T>
333 static std::string GetName(int i) {
334 if (testing::internal::IsSame<T, char>::value) {
335 return std::string("parChar") + ::testing::PrintToString(i);
337 if (testing::internal::IsSame<T, int>::value) {
338 return std::string("parInt") + ::testing::PrintToString(i);
343 INSTANTIATE_TYPED_TEST_CASE_P(CustomName, TypeParametrizedTestWithNames,
344 TwoTypes, TypeParametrizedTestNames);
346 // Tests that multiple TYPED_TEST_CASE_P's can be defined in the same
349 template <typename T>
350 class TypedTestP1 : public Test {
353 TYPED_TEST_CASE_P(TypedTestP1);
355 // For testing that the code between TYPED_TEST_CASE_P() and
356 // TYPED_TEST_P() is not enclosed in a namespace.
357 typedef int IntAfterTypedTestCaseP;
359 TYPED_TEST_P(TypedTestP1, A) {}
360 TYPED_TEST_P(TypedTestP1, B) {}
362 // For testing that the code between TYPED_TEST_P() and
363 // REGISTER_TYPED_TEST_CASE_P() is not enclosed in a namespace.
364 typedef int IntBeforeRegisterTypedTestCaseP;
366 REGISTER_TYPED_TEST_CASE_P(TypedTestP1, A, B);
368 template <typename T>
369 class TypedTestP2 : public Test {
372 TYPED_TEST_CASE_P(TypedTestP2);
374 // This also verifies that tests from different type-parameterized
375 // test cases can share the same name.
376 TYPED_TEST_P(TypedTestP2, A) {}
378 REGISTER_TYPED_TEST_CASE_P(TypedTestP2, A);
380 // Verifies that the code between TYPED_TEST_CASE_P() and
381 // REGISTER_TYPED_TEST_CASE_P() is not enclosed in a namespace.
382 IntAfterTypedTestCaseP after = 0;
383 IntBeforeRegisterTypedTestCaseP before = 0;
385 // Verifies that the last argument of INSTANTIATE_TYPED_TEST_CASE_P()
386 // can be either a single type or a Types<...> type list.
387 INSTANTIATE_TYPED_TEST_CASE_P(Int, TypedTestP1, int);
388 INSTANTIATE_TYPED_TEST_CASE_P(Int, TypedTestP2, Types<int>);
390 // Tests that the same type-parameterized test case can be
391 // instantiated more than once in the same translation unit.
392 INSTANTIATE_TYPED_TEST_CASE_P(Double, TypedTestP2, Types<double>);
394 // Tests that the same type-parameterized test case can be
395 // instantiated in different translation units linked together.
396 // (ContainerTest is also instantiated in gtest-typed-test_test.cc.)
397 typedef Types<std::vector<double>, std::set<char> > MyContainers;
398 INSTANTIATE_TYPED_TEST_CASE_P(My, ContainerTest, MyContainers);
400 // Tests that a type-parameterized test case can be defined and
401 // instantiated in a namespace.
405 template <typename T>
406 class NumericTest : public Test {
409 TYPED_TEST_CASE_P(NumericTest);
411 TYPED_TEST_P(NumericTest, DefaultIsZero) {
412 EXPECT_EQ(0, TypeParam());
415 TYPED_TEST_P(NumericTest, ZeroIsLessThanOne) {
416 EXPECT_LT(TypeParam(0), TypeParam(1));
419 REGISTER_TYPED_TEST_CASE_P(NumericTest,
420 DefaultIsZero, ZeroIsLessThanOne);
421 typedef Types<int, double> NumericTypes;
422 INSTANTIATE_TYPED_TEST_CASE_P(My, NumericTest, NumericTypes);
424 static const char* GetTestName() {
425 return testing::UnitTest::GetInstance()->current_test_info()->name();
427 // Test the stripping of space from test names
428 template <typename T> class TrimmedTest : public Test { };
429 TYPED_TEST_CASE_P(TrimmedTest);
430 TYPED_TEST_P(TrimmedTest, Test1) { EXPECT_STREQ("Test1", GetTestName()); }
431 TYPED_TEST_P(TrimmedTest, Test2) { EXPECT_STREQ("Test2", GetTestName()); }
432 TYPED_TEST_P(TrimmedTest, Test3) { EXPECT_STREQ("Test3", GetTestName()); }
433 TYPED_TEST_P(TrimmedTest, Test4) { EXPECT_STREQ("Test4", GetTestName()); }
434 TYPED_TEST_P(TrimmedTest, Test5) { EXPECT_STREQ("Test5", GetTestName()); }
435 REGISTER_TYPED_TEST_CASE_P(
437 Test1, Test2,Test3 , Test4 ,Test5 ); // NOLINT
438 template <typename T1, typename T2> struct MyPair {};
439 // Be sure to try a type with a comma in its name just in case it matters.
440 typedef Types<int, double, MyPair<int, int> > TrimTypes;
441 INSTANTIATE_TYPED_TEST_CASE_P(My, TrimmedTest, TrimTypes);
443 } // namespace library2
445 #endif // GTEST_HAS_TYPED_TEST_P
447 #if !defined(GTEST_HAS_TYPED_TEST) && !defined(GTEST_HAS_TYPED_TEST_P)
449 // Google Test may not support type-parameterized tests with some
450 // compilers. If we use conditional compilation to compile out all
451 // code referring to the gtest_main library, MSVC linker will not link
452 // that library at all and consequently complain about missing entry
453 // point defined in that library (fatal error LNK1561: entry point
454 // must be defined). This dummy test keeps gtest_main linked in.
455 TEST(DummyTest, TypedTestsAreNotSupportedOnThisPlatform) {}
458 GTEST_DISABLE_MSC_WARNINGS_POP_() // 4127
461 #endif // #if !defined(GTEST_HAS_TYPED_TEST) && !defined(GTEST_HAS_TYPED_TEST_P)