gtest-typed-test_test.cc revision 1be2c9def7187e4e643c00a31dd9986395795d7d
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 38using 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. 43template <typename T> 44class CommonTest : public Test { 45 // For some technical reason, SetUpTestCase() and TearDownTestCase() 46 // must be public. 47 public: 48 static void SetUpTestCase() { 49 shared_ = new T(5); 50 } 51 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 63 CommonTest() : value_(1) {} 64 65 virtual ~CommonTest() { EXPECT_EQ(3, value_); } 66 67 virtual void SetUp() { 68 EXPECT_EQ(1, value_); 69 value_++; 70 } 71 72 virtual void TearDown() { 73 EXPECT_EQ(2, value_); 74 value_++; 75 } 76 77 T value_; 78 static T* shared_; 79}; 80 81template <typename T> 82T* CommonTest<T>::shared_ = NULL; 83 84// This #ifdef block tests typed tests. 85#if GTEST_HAS_TYPED_TEST 86 87using testing::Types; 88 89// Tests that SetUpTestCase()/TearDownTestCase(), fixture ctor/dtor, 90// and SetUp()/TearDown() work correctly in typed tests 91 92typedef Types<char, int> TwoTypes; 93TYPED_TEST_CASE(CommonTest, TwoTypes); 94 95TYPED_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. 115TYPED_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 128template <typename T> 129class TypedTest1 : public Test { 130}; 131 132// Verifies that the second argument of TYPED_TEST_CASE can be a 133// single type. 134TYPED_TEST_CASE(TypedTest1, int); 135TYPED_TEST(TypedTest1, A) {} 136 137template <typename T> 138class TypedTest2 : public Test { 139}; 140 141// Verifies that the second argument of TYPED_TEST_CASE can be a 142// Types<...> type list. 143TYPED_TEST_CASE(TypedTest2, Types<int>); 144 145// This also verifies that tests from different typed test cases can 146// share the same name. 147TYPED_TEST(TypedTest2, A) {} 148 149// Tests that a typed test case can be defined in a namespace. 150 151namespace library1 { 152 153template <typename T> 154class NumericTest : public Test { 155}; 156 157typedef Types<int, long> NumericTypes; 158TYPED_TEST_CASE(NumericTest, NumericTypes); 159 160TYPED_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 171using testing::Types; 172using testing::internal::TypedTestCasePState; 173 174// Tests TypedTestCasePState. 175 176class TypedTestCasePStateTest : public Test { 177 protected: 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 187TEST_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. 195TEST_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 203typedef TypedTestCasePStateTest TypedTestCasePStateDeathTest; 204 205TEST_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 211TEST_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 217TEST_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. 225TEST_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 238template <typename T> 239class DerivedTest : public CommonTest<T> { 240}; 241 242TYPED_TEST_CASE_P(DerivedTest); 243 244TYPED_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. 256TYPED_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 264REGISTER_TYPED_TEST_CASE_P(DerivedTest, 265 ValuesAreCorrect, ValuesAreStillCorrect); 266 267typedef Types<short, long> MyTwoTypes; 268INSTANTIATE_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 273template <typename T> 274class TypedTestP1 : public Test { 275}; 276 277TYPED_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. 281typedef int IntAfterTypedTestCaseP; 282 283TYPED_TEST_P(TypedTestP1, A) {} 284TYPED_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. 288typedef int IntBeforeRegisterTypedTestCaseP; 289 290REGISTER_TYPED_TEST_CASE_P(TypedTestP1, A, B); 291 292template <typename T> 293class TypedTestP2 : public Test { 294}; 295 296TYPED_TEST_CASE_P(TypedTestP2); 297 298// This also verifies that tests from different type-parameterized 299// test cases can share the same name. 300TYPED_TEST_P(TypedTestP2, A) {} 301 302REGISTER_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. 306IntAfterTypedTestCaseP after = 0; 307IntBeforeRegisterTypedTestCaseP 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. 311INSTANTIATE_TYPED_TEST_CASE_P(Int, TypedTestP1, int); 312INSTANTIATE_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. 316INSTANTIATE_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.) 321typedef Types<std::list<double>, std::set<char> > MyContainers; 322INSTANTIATE_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 327namespace library2 { 328 329template <typename T> 330class NumericTest : public Test { 331}; 332 333TYPED_TEST_CASE_P(NumericTest); 334 335TYPED_TEST_P(NumericTest, DefaultIsZero) { 336 EXPECT_EQ(0, TypeParam()); 337} 338 339TYPED_TEST_P(NumericTest, ZeroIsLessThanOne) { 340 EXPECT_LT(TypeParam(0), TypeParam(1)); 341} 342 343REGISTER_TYPED_TEST_CASE_P(NumericTest, 344 DefaultIsZero, ZeroIsLessThanOne); 345typedef Types<int, double> NumericTypes; 346INSTANTIATE_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. 360TEST(DummyTest, TypedTestsAreNotSupportedOnThisPlatform) {} 361 362#endif // #if !defined(GTEST_HAS_TYPED_TEST) && !defined(GTEST_HAS_TYPED_TEST_P) 363