gtest-typed-test_test.cc
Go to the documentation of this file.
00001 // Copyright 2008 Google Inc.
00002 // All Rights Reserved.
00003 //
00004 // Redistribution and use in source and binary forms, with or without
00005 // modification, are permitted provided that the following conditions are
00006 // met:
00007 //
00008 //     * Redistributions of source code must retain the above copyright
00009 // notice, this list of conditions and the following disclaimer.
00010 //     * Redistributions in binary form must reproduce the above
00011 // copyright notice, this list of conditions and the following disclaimer
00012 // in the documentation and/or other materials provided with the
00013 // distribution.
00014 //     * Neither the name of Google Inc. nor the names of its
00015 // contributors may be used to endorse or promote products derived from
00016 // this software without specific prior written permission.
00017 //
00018 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00019 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00020 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00021 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00022 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00023 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00024 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00025 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00026 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00027 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00028 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029 //
00030 // Author: wan@google.com (Zhanyong Wan)
00031 
00032 #include "test/gtest-typed-test_test.h"
00033 
00034 #include <set>
00035 #include <vector>
00036 
00037 #include "gtest/gtest.h"
00038 
00039 using testing::Test;
00040 
00041 // Used for testing that SetUpTestCase()/TearDownTestCase(), fixture
00042 // ctor/dtor, and SetUp()/TearDown() work correctly in typed tests and
00043 // type-parameterized test.
00044 template <typename T>
00045 class CommonTest : public Test {
00046   // For some technical reason, SetUpTestCase() and TearDownTestCase()
00047   // must be public.
00048  public:
00049   static void SetUpTestCase() {
00050     shared_ = new T(5);
00051   }
00052 
00053   static void TearDownTestCase() {
00054     delete shared_;
00055     shared_ = NULL;
00056   }
00057 
00058   // This 'protected:' is optional.  There's no harm in making all
00059   // members of this fixture class template public.
00060  protected:
00061   // We used to use std::list here, but switched to std::vector since
00062   // MSVC's <list> doesn't compile cleanly with /W4.
00063   typedef std::vector<T> Vector;
00064   typedef std::set<int> IntSet;
00065 
00066   CommonTest() : value_(1) {}
00067 
00068   virtual ~CommonTest() { EXPECT_EQ(3, value_); }
00069 
00070   virtual void SetUp() {
00071     EXPECT_EQ(1, value_);
00072     value_++;
00073   }
00074 
00075   virtual void TearDown() {
00076     EXPECT_EQ(2, value_);
00077     value_++;
00078   }
00079 
00080   T value_;
00081   static T* shared_;
00082 };
00083 
00084 template <typename T>
00085 T* CommonTest<T>::shared_ = NULL;
00086 
00087 // This #ifdef block tests typed tests.
00088 #if GTEST_HAS_TYPED_TEST
00089 
00090 using testing::Types;
00091 
00092 // Tests that SetUpTestCase()/TearDownTestCase(), fixture ctor/dtor,
00093 // and SetUp()/TearDown() work correctly in typed tests
00094 
00095 typedef Types<char, int> TwoTypes;
00096 TYPED_TEST_CASE(CommonTest, TwoTypes);
00097 
00098 TYPED_TEST(CommonTest, ValuesAreCorrect) {
00099   // Static members of the fixture class template can be visited via
00100   // the TestFixture:: prefix.
00101   EXPECT_EQ(5, *TestFixture::shared_);
00102 
00103   // Typedefs in the fixture class template can be visited via the
00104   // "typename TestFixture::" prefix.
00105   typename TestFixture::Vector empty;
00106   EXPECT_EQ(0U, empty.size());
00107 
00108   typename TestFixture::IntSet empty2;
00109   EXPECT_EQ(0U, empty2.size());
00110 
00111   // Non-static members of the fixture class must be visited via
00112   // 'this', as required by C++ for class templates.
00113   EXPECT_EQ(2, this->value_);
00114 }
00115 
00116 // The second test makes sure shared_ is not deleted after the first
00117 // test.
00118 TYPED_TEST(CommonTest, ValuesAreStillCorrect) {
00119   // Static members of the fixture class template can also be visited
00120   // via 'this'.
00121   ASSERT_TRUE(this->shared_ != NULL);
00122   EXPECT_EQ(5, *this->shared_);
00123 
00124   // TypeParam can be used to refer to the type parameter.
00125   EXPECT_EQ(static_cast<TypeParam>(2), this->value_);
00126 }
00127 
00128 // Tests that multiple TYPED_TEST_CASE's can be defined in the same
00129 // translation unit.
00130 
00131 template <typename T>
00132 class TypedTest1 : public Test {
00133 };
00134 
00135 // Verifies that the second argument of TYPED_TEST_CASE can be a
00136 // single type.
00137 TYPED_TEST_CASE(TypedTest1, int);
00138 TYPED_TEST(TypedTest1, A) {}
00139 
00140 template <typename T>
00141 class TypedTest2 : public Test {
00142 };
00143 
00144 // Verifies that the second argument of TYPED_TEST_CASE can be a
00145 // Types<...> type list.
00146 TYPED_TEST_CASE(TypedTest2, Types<int>);
00147 
00148 // This also verifies that tests from different typed test cases can
00149 // share the same name.
00150 TYPED_TEST(TypedTest2, A) {}
00151 
00152 // Tests that a typed test case can be defined in a namespace.
00153 
00154 namespace library1 {
00155 
00156 template <typename T>
00157 class NumericTest : public Test {
00158 };
00159 
00160 typedef Types<int, long> NumericTypes;
00161 TYPED_TEST_CASE(NumericTest, NumericTypes);
00162 
00163 TYPED_TEST(NumericTest, DefaultIsZero) {
00164   EXPECT_EQ(0, TypeParam());
00165 }
00166 
00167 }  // namespace library1
00168 
00169 #endif  // GTEST_HAS_TYPED_TEST
00170 
00171 // This #ifdef block tests type-parameterized tests.
00172 #if GTEST_HAS_TYPED_TEST_P
00173 
00174 using testing::Types;
00175 using testing::internal::TypedTestCasePState;
00176 
00177 // Tests TypedTestCasePState.
00178 
00179 class TypedTestCasePStateTest : public Test {
00180  protected:
00181   virtual void SetUp() {
00182     state_.AddTestName("foo.cc", 0, "FooTest", "A");
00183     state_.AddTestName("foo.cc", 0, "FooTest", "B");
00184     state_.AddTestName("foo.cc", 0, "FooTest", "C");
00185   }
00186 
00187   TypedTestCasePState state_;
00188 };
00189 
00190 TEST_F(TypedTestCasePStateTest, SucceedsForMatchingList) {
00191   const char* tests = "A, B, C";
00192   EXPECT_EQ(tests,
00193             state_.VerifyRegisteredTestNames("foo.cc", 1, tests));
00194 }
00195 
00196 // Makes sure that the order of the tests and spaces around the names
00197 // don't matter.
00198 TEST_F(TypedTestCasePStateTest, IgnoresOrderAndSpaces) {
00199   const char* tests = "A,C,   B";
00200   EXPECT_EQ(tests,
00201             state_.VerifyRegisteredTestNames("foo.cc", 1, tests));
00202 }
00203 
00204 typedef TypedTestCasePStateTest TypedTestCasePStateDeathTest;
00205 
00206 TEST_F(TypedTestCasePStateDeathTest, DetectsDuplicates) {
00207   EXPECT_DEATH_IF_SUPPORTED(
00208       state_.VerifyRegisteredTestNames("foo.cc", 1, "A, B, A, C"),
00209       "foo\\.cc.1.?: Test A is listed more than once\\.");
00210 }
00211 
00212 TEST_F(TypedTestCasePStateDeathTest, DetectsExtraTest) {
00213   EXPECT_DEATH_IF_SUPPORTED(
00214       state_.VerifyRegisteredTestNames("foo.cc", 1, "A, B, C, D"),
00215       "foo\\.cc.1.?: No test named D can be found in this test case\\.");
00216 }
00217 
00218 TEST_F(TypedTestCasePStateDeathTest, DetectsMissedTest) {
00219   EXPECT_DEATH_IF_SUPPORTED(
00220       state_.VerifyRegisteredTestNames("foo.cc", 1, "A, C"),
00221       "foo\\.cc.1.?: You forgot to list test B\\.");
00222 }
00223 
00224 // Tests that defining a test for a parameterized test case generates
00225 // a run-time error if the test case has been registered.
00226 TEST_F(TypedTestCasePStateDeathTest, DetectsTestAfterRegistration) {
00227   state_.VerifyRegisteredTestNames("foo.cc", 1, "A, B, C");
00228   EXPECT_DEATH_IF_SUPPORTED(
00229       state_.AddTestName("foo.cc", 2, "FooTest", "D"),
00230       "foo\\.cc.2.?: Test D must be defined before REGISTER_TYPED_TEST_CASE_P"
00231       "\\(FooTest, \\.\\.\\.\\)\\.");
00232 }
00233 
00234 // Tests that SetUpTestCase()/TearDownTestCase(), fixture ctor/dtor,
00235 // and SetUp()/TearDown() work correctly in type-parameterized tests.
00236 
00237 template <typename T>
00238 class DerivedTest : public CommonTest<T> {
00239 };
00240 
00241 TYPED_TEST_CASE_P(DerivedTest);
00242 
00243 TYPED_TEST_P(DerivedTest, ValuesAreCorrect) {
00244   // Static members of the fixture class template can be visited via
00245   // the TestFixture:: prefix.
00246   EXPECT_EQ(5, *TestFixture::shared_);
00247 
00248   // Non-static members of the fixture class must be visited via
00249   // 'this', as required by C++ for class templates.
00250   EXPECT_EQ(2, this->value_);
00251 }
00252 
00253 // The second test makes sure shared_ is not deleted after the first
00254 // test.
00255 TYPED_TEST_P(DerivedTest, ValuesAreStillCorrect) {
00256   // Static members of the fixture class template can also be visited
00257   // via 'this'.
00258   ASSERT_TRUE(this->shared_ != NULL);
00259   EXPECT_EQ(5, *this->shared_);
00260   EXPECT_EQ(2, this->value_);
00261 }
00262 
00263 REGISTER_TYPED_TEST_CASE_P(DerivedTest,
00264                            ValuesAreCorrect, ValuesAreStillCorrect);
00265 
00266 typedef Types<short, long> MyTwoTypes;
00267 INSTANTIATE_TYPED_TEST_CASE_P(My, DerivedTest, MyTwoTypes);
00268 
00269 // Tests that multiple TYPED_TEST_CASE_P's can be defined in the same
00270 // translation unit.
00271 
00272 template <typename T>
00273 class TypedTestP1 : public Test {
00274 };
00275 
00276 TYPED_TEST_CASE_P(TypedTestP1);
00277 
00278 // For testing that the code between TYPED_TEST_CASE_P() and
00279 // TYPED_TEST_P() is not enclosed in a namespace.
00280 typedef int IntAfterTypedTestCaseP;
00281 
00282 TYPED_TEST_P(TypedTestP1, A) {}
00283 TYPED_TEST_P(TypedTestP1, B) {}
00284 
00285 // For testing that the code between TYPED_TEST_P() and
00286 // REGISTER_TYPED_TEST_CASE_P() is not enclosed in a namespace.
00287 typedef int IntBeforeRegisterTypedTestCaseP;
00288 
00289 REGISTER_TYPED_TEST_CASE_P(TypedTestP1, A, B);
00290 
00291 template <typename T>
00292 class TypedTestP2 : public Test {
00293 };
00294 
00295 TYPED_TEST_CASE_P(TypedTestP2);
00296 
00297 // This also verifies that tests from different type-parameterized
00298 // test cases can share the same name.
00299 TYPED_TEST_P(TypedTestP2, A) {}
00300 
00301 REGISTER_TYPED_TEST_CASE_P(TypedTestP2, A);
00302 
00303 // Verifies that the code between TYPED_TEST_CASE_P() and
00304 // REGISTER_TYPED_TEST_CASE_P() is not enclosed in a namespace.
00305 IntAfterTypedTestCaseP after = 0;
00306 IntBeforeRegisterTypedTestCaseP before = 0;
00307 
00308 // Verifies that the last argument of INSTANTIATE_TYPED_TEST_CASE_P()
00309 // can be either a single type or a Types<...> type list.
00310 INSTANTIATE_TYPED_TEST_CASE_P(Int, TypedTestP1, int);
00311 INSTANTIATE_TYPED_TEST_CASE_P(Int, TypedTestP2, Types<int>);
00312 
00313 // Tests that the same type-parameterized test case can be
00314 // instantiated more than once in the same translation unit.
00315 INSTANTIATE_TYPED_TEST_CASE_P(Double, TypedTestP2, Types<double>);
00316 
00317 // Tests that the same type-parameterized test case can be
00318 // instantiated in different translation units linked together.
00319 // (ContainerTest is also instantiated in gtest-typed-test_test.cc.)
00320 typedef Types<std::vector<double>, std::set<char> > MyContainers;
00321 INSTANTIATE_TYPED_TEST_CASE_P(My, ContainerTest, MyContainers);
00322 
00323 // Tests that a type-parameterized test case can be defined and
00324 // instantiated in a namespace.
00325 
00326 namespace library2 {
00327 
00328 template <typename T>
00329 class NumericTest : public Test {
00330 };
00331 
00332 TYPED_TEST_CASE_P(NumericTest);
00333 
00334 TYPED_TEST_P(NumericTest, DefaultIsZero) {
00335   EXPECT_EQ(0, TypeParam());
00336 }
00337 
00338 TYPED_TEST_P(NumericTest, ZeroIsLessThanOne) {
00339   EXPECT_LT(TypeParam(0), TypeParam(1));
00340 }
00341 
00342 REGISTER_TYPED_TEST_CASE_P(NumericTest,
00343                            DefaultIsZero, ZeroIsLessThanOne);
00344 typedef Types<int, double> NumericTypes;
00345 INSTANTIATE_TYPED_TEST_CASE_P(My, NumericTest, NumericTypes);
00346 
00347 static const char* GetTestName() {
00348   return testing::UnitTest::GetInstance()->current_test_info()->name();
00349 }
00350 // Test the stripping of space from test names
00351 template <typename T> class TrimmedTest : public Test { };
00352 TYPED_TEST_CASE_P(TrimmedTest);
00353 TYPED_TEST_P(TrimmedTest, Test1) { EXPECT_STREQ("Test1", GetTestName()); }
00354 TYPED_TEST_P(TrimmedTest, Test2) { EXPECT_STREQ("Test2", GetTestName()); }
00355 TYPED_TEST_P(TrimmedTest, Test3) { EXPECT_STREQ("Test3", GetTestName()); }
00356 TYPED_TEST_P(TrimmedTest, Test4) { EXPECT_STREQ("Test4", GetTestName()); }
00357 TYPED_TEST_P(TrimmedTest, Test5) { EXPECT_STREQ("Test5", GetTestName()); }
00358 REGISTER_TYPED_TEST_CASE_P(
00359     TrimmedTest,
00360     Test1, Test2,Test3 , Test4 ,Test5 );  // NOLINT
00361 template <typename T1, typename T2> struct MyPair {};
00362 // Be sure to try a type with a comma in its name just in case it matters.
00363 typedef Types<int, double, MyPair<int, int> > TrimTypes;
00364 INSTANTIATE_TYPED_TEST_CASE_P(My, TrimmedTest, TrimTypes);
00365 
00366 }  // namespace library2
00367 
00368 #endif  // GTEST_HAS_TYPED_TEST_P
00369 
00370 #if !defined(GTEST_HAS_TYPED_TEST) && !defined(GTEST_HAS_TYPED_TEST_P)
00371 
00372 // Google Test may not support type-parameterized tests with some
00373 // compilers. If we use conditional compilation to compile out all
00374 // code referring to the gtest_main library, MSVC linker will not link
00375 // that library at all and consequently complain about missing entry
00376 // point defined in that library (fatal error LNK1561: entry point
00377 // must be defined). This dummy test keeps gtest_main linked in.
00378 TEST(DummyTest, TypedTestsAreNotSupportedOnThisPlatform) {}
00379 
00380 #endif  // #if !defined(GTEST_HAS_TYPED_TEST) && !defined(GTEST_HAS_TYPED_TEST_P)


rc_visard_driver
Author(s): Heiko Hirschmueller , Christian Emmerich , Felix Ruess
autogenerated on Thu Jun 6 2019 20:43:04