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 39 GTEST_DISABLE_MSC_WARNINGS_PUSH_(4127 /* conditional expression is constant */) 40 #endif // _MSC_VER 41 42 using 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. 47 template <typename T> 48 class 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 87 template <typename T> 88 T* CommonTest<T>::shared_ = NULL; 89 90 // This #ifdef block tests typed tests. 91 #if GTEST_HAS_TYPED_TEST 92 93 using testing::Types; 94 95 // Tests that SetUpTestCase()/TearDownTestCase(), fixture ctor/dtor, 96 // and SetUp()/TearDown() work correctly in typed tests 97 98 typedef Types<char, int> TwoTypes; 99 TYPED_TEST_CASE(CommonTest, TwoTypes); 100 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_); 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. 121 TYPED_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 134 template <typename T> 135 class TypedTest1 : public Test { 136 }; 137 138 // Verifies that the second argument of TYPED_TEST_CASE can be a 139 // single type. 140 TYPED_TEST_CASE(TypedTest1, int); 141 TYPED_TEST(TypedTest1, A) {} 142 143 template <typename T> 144 class TypedTest2 : public Test { 145 }; 146 147 // Verifies that the second argument of TYPED_TEST_CASE can be a 148 // Types<...> type list. 149 TYPED_TEST_CASE(TypedTest2, Types<int>); 150 151 // This also verifies that tests from different typed test cases can 152 // share the same name. 153 TYPED_TEST(TypedTest2, A) {} 154 155 // Tests that a typed test case can be defined in a namespace. 156 157 namespace library1 { 158 159 template <typename T> 160 class NumericTest : public Test { 161 }; 162 163 typedef Types<int, long> NumericTypes; 164 TYPED_TEST_CASE(NumericTest, NumericTypes); 165 166 TYPED_TEST(NumericTest, DefaultIsZero) { 167 EXPECT_EQ(0, TypeParam()); 168 } 169 170 } // namespace library1 171 172 // Tests that custom names work. 173 template <typename T> 174 class TypedTestWithNames : public Test {}; 175 176 class 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 189 TYPED_TEST_CASE(TypedTestWithNames, TwoTypes, TypedTestNames); 190 191 TYPED_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 211 using testing::Types; 212 using testing::internal::TypedTestCasePState; 213 214 // Tests TypedTestCasePState. 215 216 class 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 227 TEST_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. 235 TEST_F(TypedTestCasePStateTest, IgnoresOrderAndSpaces) { 236 const char* tests = "A,C, B"; 237 EXPECT_EQ(tests, 238 state_.VerifyRegisteredTestNames("foo.cc", 1, tests)); 239 } 240 241 typedef TypedTestCasePStateTest TypedTestCasePStateDeathTest; 242 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\\."); 247 } 248 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\\."); 253 } 254 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\\."); 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. 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, \\.\\.\\.\\)\\."); 269 } 270 271 // Tests that SetUpTestCase()/TearDownTestCase(), fixture ctor/dtor, 272 // and SetUp()/TearDown() work correctly in type-parameterized tests. 273 274 template <typename T> 275 class DerivedTest : public CommonTest<T> { 276 }; 277 278 TYPED_TEST_CASE_P(DerivedTest); 279 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_); 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. 292 TYPED_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 300 REGISTER_TYPED_TEST_CASE_P(DerivedTest, 301 ValuesAreCorrect, ValuesAreStillCorrect); 302 303 typedef Types<short, long> MyTwoTypes; 304 INSTANTIATE_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. 308 template <typename T> 309 class TypeParametrizedTestWithNames : public Test {}; 310 311 TYPED_TEST_CASE_P(TypeParametrizedTestWithNames); 312 313 TYPED_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 328 REGISTER_TYPED_TEST_CASE_P(TypeParametrizedTestWithNames, TestCaseName); 329 330 class 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 343 INSTANTIATE_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 349 template <typename T> 350 class TypedTestP1 : public Test { 351 }; 352 353 TYPED_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. 357 typedef int IntAfterTypedTestCaseP; 358 359 TYPED_TEST_P(TypedTestP1, A) {} 360 TYPED_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. 364 typedef int IntBeforeRegisterTypedTestCaseP; 365 366 REGISTER_TYPED_TEST_CASE_P(TypedTestP1, A, B); 367 368 template <typename T> 369 class TypedTestP2 : public Test { 370 }; 371 372 TYPED_TEST_CASE_P(TypedTestP2); 373 374 // This also verifies that tests from different type-parameterized 375 // test cases can share the same name. 376 TYPED_TEST_P(TypedTestP2, A) {} 377 378 REGISTER_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. 382 IntAfterTypedTestCaseP after = 0; 383 IntBeforeRegisterTypedTestCaseP 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. 387 INSTANTIATE_TYPED_TEST_CASE_P(Int, TypedTestP1, int); 388 INSTANTIATE_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. 392 INSTANTIATE_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.) 397 typedef Types<std::vector<double>, std::set<char> > MyContainers; 398 INSTANTIATE_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 403 namespace library2 { 404 405 template <typename T> 406 class NumericTest : public Test { 407 }; 408 409 TYPED_TEST_CASE_P(NumericTest); 410 411 TYPED_TEST_P(NumericTest, DefaultIsZero) { 412 EXPECT_EQ(0, TypeParam()); 413 } 414 415 TYPED_TEST_P(NumericTest, ZeroIsLessThanOne) { 416 EXPECT_LT(TypeParam(0), TypeParam(1)); 417 } 418 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); 423 424 static const char* GetTestName() { 425 return testing::UnitTest::GetInstance()->current_test_info()->name(); 426 } 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( 436 TrimmedTest, 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); 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. 455 TEST(DummyTest, TypedTestsAreNotSupportedOnThisPlatform) {} 456 457 #if _MSC_VER 458 GTEST_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