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 <set>
00033 #include <vector>
00034 
00035 #include "test/gtest-typed-test_test.h"
00036 #include "gtest/gtest.h"
00037 
00038 using testing::Test;
00039 
00040 // Used for testing that SetUpTestCase()/TearDownTestCase(), fixture
00041 // ctor/dtor, and SetUp()/TearDown() work correctly in typed tests and
00042 // type-parameterized test.
00043 template <typename T>
00044 class CommonTest : public Test {
00045   // For some technical reason, SetUpTestCase() and TearDownTestCase()
00046   // must be public.
00047  public:
00048   static void SetUpTestCase() {
00049     shared_ = new T(5);
00050   }
00051 
00052   static void TearDownTestCase() {
00053     delete shared_;
00054     shared_ = NULL;
00055   }
00056 
00057   // This 'protected:' is optional.  There's no harm in making all
00058   // members of this fixture class template public.
00059  protected:
00060   // We used to use std::list here, but switched to std::vector since
00061   // MSVC's <list> doesn't compile cleanly with /W4.
00062   typedef std::vector<T> Vector;
00063   typedef std::set<int> IntSet;
00064 
00065   CommonTest() : value_(1) {}
00066 
00067   virtual ~CommonTest() { EXPECT_EQ(3, value_); }
00068 
00069   virtual void SetUp() {
00070     EXPECT_EQ(1, value_);
00071     value_++;
00072   }
00073 
00074   virtual void TearDown() {
00075     EXPECT_EQ(2, value_);
00076     value_++;
00077   }
00078 
00079   T value_;
00080   static T* shared_;
00081 };
00082 
00083 template <typename T>
00084 T* CommonTest<T>::shared_ = NULL;
00085 
00086 // This #ifdef block tests typed tests.
00087 #if GTEST_HAS_TYPED_TEST
00088 
00089 using testing::Types;
00090 
00091 // Tests that SetUpTestCase()/TearDownTestCase(), fixture ctor/dtor,
00092 // and SetUp()/TearDown() work correctly in typed tests
00093 
00094 typedef Types<char, int> TwoTypes;
00095 TYPED_TEST_CASE(CommonTest, TwoTypes);
00096 
00097 TYPED_TEST(CommonTest, ValuesAreCorrect) {
00098   // Static members of the fixture class template can be visited via
00099   // the TestFixture:: prefix.
00100   EXPECT_EQ(5, *TestFixture::shared_);
00101 
00102   // Typedefs in the fixture class template can be visited via the
00103   // "typename TestFixture::" prefix.
00104   typename TestFixture::Vector empty;
00105   EXPECT_EQ(0U, empty.size());
00106 
00107   typename TestFixture::IntSet empty2;
00108   EXPECT_EQ(0U, empty2.size());
00109 
00110   // Non-static members of the fixture class must be visited via
00111   // 'this', as required by C++ for class templates.
00112   EXPECT_EQ(2, this->value_);
00113 }
00114 
00115 // The second test makes sure shared_ is not deleted after the first
00116 // test.
00117 TYPED_TEST(CommonTest, ValuesAreStillCorrect) {
00118   // Static members of the fixture class template can also be visited
00119   // via 'this'.
00120   ASSERT_TRUE(this->shared_ != NULL);
00121   EXPECT_EQ(5, *this->shared_);
00122 
00123   // TypeParam can be used to refer to the type parameter.
00124   EXPECT_EQ(static_cast<TypeParam>(2), this->value_);
00125 }
00126 
00127 // Tests that multiple TYPED_TEST_CASE's can be defined in the same
00128 // translation unit.
00129 
00130 template <typename T>
00131 class TypedTest1 : public Test {
00132 };
00133 
00134 // Verifies that the second argument of TYPED_TEST_CASE can be a
00135 // single type.
00136 TYPED_TEST_CASE(TypedTest1, int);
00137 TYPED_TEST(TypedTest1, A) {}
00138 
00139 template <typename T>
00140 class TypedTest2 : public Test {
00141 };
00142 
00143 // Verifies that the second argument of TYPED_TEST_CASE can be a
00144 // Types<...> type list.
00145 TYPED_TEST_CASE(TypedTest2, Types<int>);
00146 
00147 // This also verifies that tests from different typed test cases can
00148 // share the same name.
00149 TYPED_TEST(TypedTest2, A) {}
00150 
00151 // Tests that a typed test case can be defined in a namespace.
00152 
00153 namespace library1 {
00154 
00155 template <typename T>
00156 class NumericTest : public Test {
00157 };
00158 
00159 typedef Types<int, long> NumericTypes;
00160 TYPED_TEST_CASE(NumericTest, NumericTypes);
00161 
00162 TYPED_TEST(NumericTest, DefaultIsZero) {
00163   EXPECT_EQ(0, TypeParam());
00164 }
00165 
00166 }  // namespace library1
00167 
00168 #endif  // GTEST_HAS_TYPED_TEST
00169 
00170 // This #ifdef block tests type-parameterized tests.
00171 #if GTEST_HAS_TYPED_TEST_P
00172 
00173 using testing::Types;
00174 using testing::internal::TypedTestCasePState;
00175 
00176 // Tests TypedTestCasePState.
00177 
00178 class TypedTestCasePStateTest : public Test {
00179  protected:
00180   virtual void SetUp() {
00181     state_.AddTestName("foo.cc", 0, "FooTest", "A");
00182     state_.AddTestName("foo.cc", 0, "FooTest", "B");
00183     state_.AddTestName("foo.cc", 0, "FooTest", "C");
00184   }
00185 
00186   TypedTestCasePState state_;
00187 };
00188 
00189 TEST_F(TypedTestCasePStateTest, SucceedsForMatchingList) {
00190   const char* tests = "A, B, C";
00191   EXPECT_EQ(tests,
00192             state_.VerifyRegisteredTestNames("foo.cc", 1, tests));
00193 }
00194 
00195 // Makes sure that the order of the tests and spaces around the names
00196 // don't matter.
00197 TEST_F(TypedTestCasePStateTest, IgnoresOrderAndSpaces) {
00198   const char* tests = "A,C,   B";
00199   EXPECT_EQ(tests,
00200             state_.VerifyRegisteredTestNames("foo.cc", 1, tests));
00201 }
00202 
00203 typedef TypedTestCasePStateTest TypedTestCasePStateDeathTest;
00204 
00205 TEST_F(TypedTestCasePStateDeathTest, DetectsDuplicates) {
00206   EXPECT_DEATH_IF_SUPPORTED(
00207       state_.VerifyRegisteredTestNames("foo.cc", 1, "A, B, A, C"),
00208       "foo\\.cc.1.?: Test A is listed more than once\\.");
00209 }
00210 
00211 TEST_F(TypedTestCasePStateDeathTest, DetectsExtraTest) {
00212   EXPECT_DEATH_IF_SUPPORTED(
00213       state_.VerifyRegisteredTestNames("foo.cc", 1, "A, B, C, D"),
00214       "foo\\.cc.1.?: No test named D can be found in this test case\\.");
00215 }
00216 
00217 TEST_F(TypedTestCasePStateDeathTest, DetectsMissedTest) {
00218   EXPECT_DEATH_IF_SUPPORTED(
00219       state_.VerifyRegisteredTestNames("foo.cc", 1, "A, C"),
00220       "foo\\.cc.1.?: You forgot to list test B\\.");
00221 }
00222 
00223 // Tests that defining a test for a parameterized test case generates
00224 // a run-time error if the test case has been registered.
00225 TEST_F(TypedTestCasePStateDeathTest, DetectsTestAfterRegistration) {
00226   state_.VerifyRegisteredTestNames("foo.cc", 1, "A, B, C");
00227   EXPECT_DEATH_IF_SUPPORTED(
00228       state_.AddTestName("foo.cc", 2, "FooTest", "D"),
00229       "foo\\.cc.2.?: Test D must be defined before REGISTER_TYPED_TEST_CASE_P"
00230       "\\(FooTest, \\.\\.\\.\\)\\.");
00231 }
00232 
00233 // Tests that SetUpTestCase()/TearDownTestCase(), fixture ctor/dtor,
00234 // and SetUp()/TearDown() work correctly in type-parameterized tests.
00235 
00236 template <typename T>
00237 class DerivedTest : public CommonTest<T> {
00238 };
00239 
00240 TYPED_TEST_CASE_P(DerivedTest);
00241 
00242 TYPED_TEST_P(DerivedTest, ValuesAreCorrect) {
00243   // Static members of the fixture class template can be visited via
00244   // the TestFixture:: prefix.
00245   EXPECT_EQ(5, *TestFixture::shared_);
00246 
00247   // Non-static members of the fixture class must be visited via
00248   // 'this', as required by C++ for class templates.
00249   EXPECT_EQ(2, this->value_);
00250 }
00251 
00252 // The second test makes sure shared_ is not deleted after the first
00253 // test.
00254 TYPED_TEST_P(DerivedTest, ValuesAreStillCorrect) {
00255   // Static members of the fixture class template can also be visited
00256   // via 'this'.
00257   ASSERT_TRUE(this->shared_ != NULL);
00258   EXPECT_EQ(5, *this->shared_);
00259   EXPECT_EQ(2, this->value_);
00260 }
00261 
00262 REGISTER_TYPED_TEST_CASE_P(DerivedTest,
00263                            ValuesAreCorrect, ValuesAreStillCorrect);
00264 
00265 typedef Types<short, long> MyTwoTypes;
00266 INSTANTIATE_TYPED_TEST_CASE_P(My, DerivedTest, MyTwoTypes);
00267 
00268 // Tests that multiple TYPED_TEST_CASE_P's can be defined in the same
00269 // translation unit.
00270 
00271 template <typename T>
00272 class TypedTestP1 : public Test {
00273 };
00274 
00275 TYPED_TEST_CASE_P(TypedTestP1);
00276 
00277 // For testing that the code between TYPED_TEST_CASE_P() and
00278 // TYPED_TEST_P() is not enclosed in a namespace.
00279 typedef int IntAfterTypedTestCaseP;
00280 
00281 TYPED_TEST_P(TypedTestP1, A) {}
00282 TYPED_TEST_P(TypedTestP1, B) {}
00283 
00284 // For testing that the code between TYPED_TEST_P() and
00285 // REGISTER_TYPED_TEST_CASE_P() is not enclosed in a namespace.
00286 typedef int IntBeforeRegisterTypedTestCaseP;
00287 
00288 REGISTER_TYPED_TEST_CASE_P(TypedTestP1, A, B);
00289 
00290 template <typename T>
00291 class TypedTestP2 : public Test {
00292 };
00293 
00294 TYPED_TEST_CASE_P(TypedTestP2);
00295 
00296 // This also verifies that tests from different type-parameterized
00297 // test cases can share the same name.
00298 TYPED_TEST_P(TypedTestP2, A) {}
00299 
00300 REGISTER_TYPED_TEST_CASE_P(TypedTestP2, A);
00301 
00302 // Verifies that the code between TYPED_TEST_CASE_P() and
00303 // REGISTER_TYPED_TEST_CASE_P() is not enclosed in a namespace.
00304 IntAfterTypedTestCaseP after = 0;
00305 IntBeforeRegisterTypedTestCaseP before = 0;
00306 
00307 // Verifies that the last argument of INSTANTIATE_TYPED_TEST_CASE_P()
00308 // can be either a single type or a Types<...> type list.
00309 INSTANTIATE_TYPED_TEST_CASE_P(Int, TypedTestP1, int);
00310 INSTANTIATE_TYPED_TEST_CASE_P(Int, TypedTestP2, Types<int>);
00311 
00312 // Tests that the same type-parameterized test case can be
00313 // instantiated more than once in the same translation unit.
00314 INSTANTIATE_TYPED_TEST_CASE_P(Double, TypedTestP2, Types<double>);
00315 
00316 // Tests that the same type-parameterized test case can be
00317 // instantiated in different translation units linked together.
00318 // (ContainerTest is also instantiated in gtest-typed-test_test.cc.)
00319 typedef Types<std::vector<double>, std::set<char> > MyContainers;
00320 INSTANTIATE_TYPED_TEST_CASE_P(My, ContainerTest, MyContainers);
00321 
00322 // Tests that a type-parameterized test case can be defined and
00323 // instantiated in a namespace.
00324 
00325 namespace library2 {
00326 
00327 template <typename T>
00328 class NumericTest : public Test {
00329 };
00330 
00331 TYPED_TEST_CASE_P(NumericTest);
00332 
00333 TYPED_TEST_P(NumericTest, DefaultIsZero) {
00334   EXPECT_EQ(0, TypeParam());
00335 }
00336 
00337 TYPED_TEST_P(NumericTest, ZeroIsLessThanOne) {
00338   EXPECT_LT(TypeParam(0), TypeParam(1));
00339 }
00340 
00341 REGISTER_TYPED_TEST_CASE_P(NumericTest,
00342                            DefaultIsZero, ZeroIsLessThanOne);
00343 typedef Types<int, double> NumericTypes;
00344 INSTANTIATE_TYPED_TEST_CASE_P(My, NumericTest, NumericTypes);
00345 
00346 }  // namespace library2
00347 
00348 #endif  // GTEST_HAS_TYPED_TEST_P
00349 
00350 #if !defined(GTEST_HAS_TYPED_TEST) && !defined(GTEST_HAS_TYPED_TEST_P)
00351 
00352 // Google Test may not support type-parameterized tests with some
00353 // compilers. If we use conditional compilation to compile out all
00354 // code referring to the gtest_main library, MSVC linker will not link
00355 // that library at all and consequently complain about missing entry
00356 // point defined in that library (fatal error LNK1561: entry point
00357 // must be defined). This dummy test keeps gtest_main linked in.
00358 TEST(DummyTest, TypedTestsAreNotSupportedOnThisPlatform) {}
00359 
00360 #endif  // #if !defined(GTEST_HAS_TYPED_TEST) && !defined(GTEST_HAS_TYPED_TEST_P)


ros_opcua_impl_freeopcua
Author(s): Denis Štogl
autogenerated on Sat Jun 8 2019 18:24:48