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 31#include "test/gtest-typed-test_test.h" 32 33#include <set> 34#include <vector> 35 36#include "gtest/gtest.h" 37 38#if _MSC_VER 39GTEST_DISABLE_MSC_WARNINGS_PUSH_(4127 /* conditional expression is constant */) 40#endif // _MSC_VER 41 42using testing::Test; 43 44// Used for testing that SetUpTestCase()/TearDownTestCase(), fixture 45// ctor/dtor, and SetUp()/TearDown() work correctly in typed tests and 46// type-parameterized test. 47template <typename T> 48class CommonTest : public Test { 49 // For some technical reason, SetUpTestCase() and TearDownTestCase() 50 // must be public. 51 public: 52 static void SetUpTestCase() { 53 shared_ = new T(5); 54 } 55 56 static void TearDownTestCase() { 57 delete shared_; 58 shared_ = NULL; 59 } 60 61 // This 'protected:' is optional. There's no harm in making all 62 // members of this fixture class template public. 63 protected: 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; 68 69 CommonTest() : value_(1) {} 70 71 virtual ~CommonTest() { EXPECT_EQ(3, value_); } 72 73 virtual void SetUp() { 74 EXPECT_EQ(1, value_); 75 value_++; 76 } 77 78 virtual void TearDown() { 79 EXPECT_EQ(2, value_); 80 value_++; 81 } 82 83 T value_; 84 static T* shared_; 85}; 86 87template <typename T> 88T* CommonTest<T>::shared_ = NULL; 89 90// This #ifdef block tests typed tests. 91#if GTEST_HAS_TYPED_TEST 92 93using testing::Types; 94 95// Tests that SetUpTestCase()/TearDownTestCase(), fixture ctor/dtor, 96// and SetUp()/TearDown() work correctly in typed tests 97 98typedef Types<char, int> TwoTypes; 99TYPED_TEST_CASE(CommonTest, TwoTypes); 100 101TYPED_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_); 105 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()); 110 111 typename TestFixture::IntSet empty2; 112 EXPECT_EQ(0U, empty2.size()); 113 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_); 117} 118 119// The second test makes sure shared_ is not deleted after the first 120// test. 121TYPED_TEST(CommonTest, ValuesAreStillCorrect) { 122 // Static members of the fixture class template can also be visited 123 // via 'this'. 124 ASSERT_TRUE(this->shared_ != NULL); 125 EXPECT_EQ(5, *this->shared_); 126 127 // TypeParam can be used to refer to the type parameter. 128 EXPECT_EQ(static_cast<TypeParam>(2), this->value_); 129} 130 131// Tests that multiple TYPED_TEST_CASE's can be defined in the same 132// translation unit. 133 134template <typename T> 135class TypedTest1 : public Test { 136}; 137 138// Verifies that the second argument of TYPED_TEST_CASE can be a 139// single type. 140TYPED_TEST_CASE(TypedTest1, int); 141TYPED_TEST(TypedTest1, A) {} 142 143template <typename T> 144class TypedTest2 : public Test { 145}; 146 147// Verifies that the second argument of TYPED_TEST_CASE can be a 148// Types<...> type list. 149TYPED_TEST_CASE(TypedTest2, Types<int>); 150 151// This also verifies that tests from different typed test cases can 152// share the same name. 153TYPED_TEST(TypedTest2, A) {} 154 155// Tests that a typed test case can be defined in a namespace. 156 157namespace library1 { 158 159template <typename T> 160class NumericTest : public Test { 161}; 162 163typedef Types<int, long> NumericTypes; 164TYPED_TEST_CASE(NumericTest, NumericTypes); 165 166TYPED_TEST(NumericTest, DefaultIsZero) { 167 EXPECT_EQ(0, TypeParam()); 168} 169 170} // namespace library1 171 172// Tests that custom names work. 173template <typename T> 174class TypedTestWithNames : public Test {}; 175 176class TypedTestNames { 177 public: 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); 182 } 183 if (testing::internal::IsSame<T, int>::value) { 184 return std::string("int") + ::testing::PrintToString(i); 185 } 186 } 187}; 188 189TYPED_TEST_CASE(TypedTestWithNames, TwoTypes, TypedTestNames); 190 191TYPED_TEST(TypedTestWithNames, TestCaseName) { 192 if (testing::internal::IsSame<TypeParam, char>::value) { 193 EXPECT_STREQ(::testing::UnitTest::GetInstance() 194 ->current_test_info() 195 ->test_case_name(), 196 "TypedTestWithNames/char0"); 197 } 198 if (testing::internal::IsSame<TypeParam, int>::value) { 199 EXPECT_STREQ(::testing::UnitTest::GetInstance() 200 ->current_test_info() 201 ->test_case_name(), 202 "TypedTestWithNames/int1"); 203 } 204} 205 206#endif // GTEST_HAS_TYPED_TEST 207 208// This #ifdef block tests type-parameterized tests. 209#if GTEST_HAS_TYPED_TEST_P 210 211using testing::Types; 212using testing::internal::TypedTestCasePState; 213 214// Tests TypedTestCasePState. 215 216class TypedTestCasePStateTest : public Test { 217 protected: 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"); 222 } 223 224 TypedTestCasePState state_; 225}; 226 227TEST_F(TypedTestCasePStateTest, SucceedsForMatchingList) { 228 const char* tests = "A, B, C"; 229 EXPECT_EQ(tests, 230 state_.VerifyRegisteredTestNames("foo.cc", 1, tests)); 231} 232 233// Makes sure that the order of the tests and spaces around the names 234// don't matter. 235TEST_F(TypedTestCasePStateTest, IgnoresOrderAndSpaces) { 236 const char* tests = "A,C, B"; 237 EXPECT_EQ(tests, 238 state_.VerifyRegisteredTestNames("foo.cc", 1, tests)); 239} 240 241typedef TypedTestCasePStateTest TypedTestCasePStateDeathTest; 242 243TEST_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\\."); 247} 248 249TEST_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\\."); 253} 254 255TEST_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\\."); 259} 260 261// Tests that defining a test for a parameterized test case generates 262// a run-time error if the test case has been registered. 263TEST_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, \\.\\.\\.\\)\\."); 269} 270 271// Tests that SetUpTestCase()/TearDownTestCase(), fixture ctor/dtor, 272// and SetUp()/TearDown() work correctly in type-parameterized tests. 273 274template <typename T> 275class DerivedTest : public CommonTest<T> { 276}; 277 278TYPED_TEST_CASE_P(DerivedTest); 279 280TYPED_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_); 284 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_); 288} 289 290// The second test makes sure shared_ is not deleted after the first 291// test. 292TYPED_TEST_P(DerivedTest, ValuesAreStillCorrect) { 293 // Static members of the fixture class template can also be visited 294 // via 'this'. 295 ASSERT_TRUE(this->shared_ != NULL); 296 EXPECT_EQ(5, *this->shared_); 297 EXPECT_EQ(2, this->value_); 298} 299 300REGISTER_TYPED_TEST_CASE_P(DerivedTest, 301 ValuesAreCorrect, ValuesAreStillCorrect); 302 303typedef Types<short, long> MyTwoTypes; 304INSTANTIATE_TYPED_TEST_CASE_P(My, DerivedTest, MyTwoTypes); 305 306// Tests that custom names work with type parametrized tests. We reuse the 307// TwoTypes from above here. 308template <typename T> 309class TypeParametrizedTestWithNames : public Test {}; 310 311TYPED_TEST_CASE_P(TypeParametrizedTestWithNames); 312 313TYPED_TEST_P(TypeParametrizedTestWithNames, TestCaseName) { 314 if (testing::internal::IsSame<TypeParam, char>::value) { 315 EXPECT_STREQ(::testing::UnitTest::GetInstance() 316 ->current_test_info() 317 ->test_case_name(), 318 "CustomName/TypeParametrizedTestWithNames/parChar0"); 319 } 320 if (testing::internal::IsSame<TypeParam, int>::value) { 321 EXPECT_STREQ(::testing::UnitTest::GetInstance() 322 ->current_test_info() 323 ->test_case_name(), 324 "CustomName/TypeParametrizedTestWithNames/parInt1"); 325 } 326} 327 328REGISTER_TYPED_TEST_CASE_P(TypeParametrizedTestWithNames, TestCaseName); 329 330class TypeParametrizedTestNames { 331 public: 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); 336 } 337 if (testing::internal::IsSame<T, int>::value) { 338 return std::string("parInt") + ::testing::PrintToString(i); 339 } 340 } 341}; 342 343INSTANTIATE_TYPED_TEST_CASE_P(CustomName, TypeParametrizedTestWithNames, 344 TwoTypes, TypeParametrizedTestNames); 345 346// Tests that multiple TYPED_TEST_CASE_P's can be defined in the same 347// translation unit. 348 349template <typename T> 350class TypedTestP1 : public Test { 351}; 352 353TYPED_TEST_CASE_P(TypedTestP1); 354 355// For testing that the code between TYPED_TEST_CASE_P() and 356// TYPED_TEST_P() is not enclosed in a namespace. 357typedef int IntAfterTypedTestCaseP; 358 359TYPED_TEST_P(TypedTestP1, A) {} 360TYPED_TEST_P(TypedTestP1, B) {} 361 362// For testing that the code between TYPED_TEST_P() and 363// REGISTER_TYPED_TEST_CASE_P() is not enclosed in a namespace. 364typedef int IntBeforeRegisterTypedTestCaseP; 365 366REGISTER_TYPED_TEST_CASE_P(TypedTestP1, A, B); 367 368template <typename T> 369class TypedTestP2 : public Test { 370}; 371 372TYPED_TEST_CASE_P(TypedTestP2); 373 374// This also verifies that tests from different type-parameterized 375// test cases can share the same name. 376TYPED_TEST_P(TypedTestP2, A) {} 377 378REGISTER_TYPED_TEST_CASE_P(TypedTestP2, A); 379 380// Verifies that the code between TYPED_TEST_CASE_P() and 381// REGISTER_TYPED_TEST_CASE_P() is not enclosed in a namespace. 382IntAfterTypedTestCaseP after = 0; 383IntBeforeRegisterTypedTestCaseP before = 0; 384 385// Verifies that the last argument of INSTANTIATE_TYPED_TEST_CASE_P() 386// can be either a single type or a Types<...> type list. 387INSTANTIATE_TYPED_TEST_CASE_P(Int, TypedTestP1, int); 388INSTANTIATE_TYPED_TEST_CASE_P(Int, TypedTestP2, Types<int>); 389 390// Tests that the same type-parameterized test case can be 391// instantiated more than once in the same translation unit. 392INSTANTIATE_TYPED_TEST_CASE_P(Double, TypedTestP2, Types<double>); 393 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.) 397typedef Types<std::vector<double>, std::set<char> > MyContainers; 398INSTANTIATE_TYPED_TEST_CASE_P(My, ContainerTest, MyContainers); 399 400// Tests that a type-parameterized test case can be defined and 401// instantiated in a namespace. 402 403namespace library2 { 404 405template <typename T> 406class NumericTest : public Test { 407}; 408 409TYPED_TEST_CASE_P(NumericTest); 410 411TYPED_TEST_P(NumericTest, DefaultIsZero) { 412 EXPECT_EQ(0, TypeParam()); 413} 414 415TYPED_TEST_P(NumericTest, ZeroIsLessThanOne) { 416 EXPECT_LT(TypeParam(0), TypeParam(1)); 417} 418 419REGISTER_TYPED_TEST_CASE_P(NumericTest, 420 DefaultIsZero, ZeroIsLessThanOne); 421typedef Types<int, double> NumericTypes; 422INSTANTIATE_TYPED_TEST_CASE_P(My, NumericTest, NumericTypes); 423 424static const char* GetTestName() { 425 return testing::UnitTest::GetInstance()->current_test_info()->name(); 426} 427// Test the stripping of space from test names 428template <typename T> class TrimmedTest : public Test { }; 429TYPED_TEST_CASE_P(TrimmedTest); 430TYPED_TEST_P(TrimmedTest, Test1) { EXPECT_STREQ("Test1", GetTestName()); } 431TYPED_TEST_P(TrimmedTest, Test2) { EXPECT_STREQ("Test2", GetTestName()); } 432TYPED_TEST_P(TrimmedTest, Test3) { EXPECT_STREQ("Test3", GetTestName()); } 433TYPED_TEST_P(TrimmedTest, Test4) { EXPECT_STREQ("Test4", GetTestName()); } 434TYPED_TEST_P(TrimmedTest, Test5) { EXPECT_STREQ("Test5", GetTestName()); } 435REGISTER_TYPED_TEST_CASE_P( 436 TrimmedTest, 437 Test1, Test2,Test3 , Test4 ,Test5 ); // NOLINT 438template <typename T1, typename T2> struct MyPair {}; 439// Be sure to try a type with a comma in its name just in case it matters. 440typedef Types<int, double, MyPair<int, int> > TrimTypes; 441INSTANTIATE_TYPED_TEST_CASE_P(My, TrimmedTest, TrimTypes); 442 443} // namespace library2 444 445#endif // GTEST_HAS_TYPED_TEST_P 446 447#if !defined(GTEST_HAS_TYPED_TEST) && !defined(GTEST_HAS_TYPED_TEST_P) 448 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. 455TEST(DummyTest, TypedTestsAreNotSupportedOnThisPlatform) {} 456 457#if _MSC_VER 458GTEST_DISABLE_MSC_WARNINGS_POP_() // 4127 459#endif // _MSC_VER 460 461#endif // #if !defined(GTEST_HAS_TYPED_TEST) && !defined(GTEST_HAS_TYPED_TEST_P) 462