gtest-typed-test.h
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 #ifndef GTEST_INCLUDE_GTEST_GTEST_TYPED_TEST_H_
00033 #define GTEST_INCLUDE_GTEST_GTEST_TYPED_TEST_H_
00034 
00035 // This header implements typed tests and type-parameterized tests.
00036 
00037 // Typed (aka type-driven) tests repeat the same test for types in a
00038 // list.  You must know which types you want to test with when writing
00039 // typed tests. Here's how you do it:
00040 
00041 #if 0
00042 
00043 // First, define a fixture class template.  It should be parameterized
00044 // by a type.  Remember to derive it from testing::Test.
00045 template <typename T>
00046 class FooTest : public testing::Test {
00047  public:
00048   ...
00049   typedef std::list<T> List;
00050   static T shared_;
00051   T value_;
00052 };
00053 
00054 // Next, associate a list of types with the test case, which will be
00055 // repeated for each type in the list.  The typedef is necessary for
00056 // the macro to parse correctly.
00057 typedef testing::Types<char, int, unsigned int> MyTypes;
00058 TYPED_TEST_CASE(FooTest, MyTypes);
00059 
00060 // If the type list contains only one type, you can write that type
00061 // directly without Types<...>:
00062 //   TYPED_TEST_CASE(FooTest, int);
00063 
00064 // Then, use TYPED_TEST() instead of TEST_F() to define as many typed
00065 // tests for this test case as you want.
00066 TYPED_TEST(FooTest, DoesBlah) {
00067   // Inside a test, refer to TypeParam to get the type parameter.
00068   // Since we are inside a derived class template, C++ requires use to
00069   // visit the members of FooTest via 'this'.
00070   TypeParam n = this->value_;
00071 
00072   // To visit static members of the fixture, add the TestFixture::
00073   // prefix.
00074   n += TestFixture::shared_;
00075 
00076   // To refer to typedefs in the fixture, add the "typename
00077   // TestFixture::" prefix.
00078   typename TestFixture::List values;
00079   values.push_back(n);
00080   ...
00081 }
00082 
00083 TYPED_TEST(FooTest, HasPropertyA) { ... }
00084 
00085 #endif  // 0
00086 
00087 // Type-parameterized tests are abstract test patterns parameterized
00088 // by a type.  Compared with typed tests, type-parameterized tests
00089 // allow you to define the test pattern without knowing what the type
00090 // parameters are.  The defined pattern can be instantiated with
00091 // different types any number of times, in any number of translation
00092 // units.
00093 //
00094 // If you are designing an interface or concept, you can define a
00095 // suite of type-parameterized tests to verify properties that any
00096 // valid implementation of the interface/concept should have.  Then,
00097 // each implementation can easily instantiate the test suite to verify
00098 // that it conforms to the requirements, without having to write
00099 // similar tests repeatedly.  Here's an example:
00100 
00101 #if 0
00102 
00103 // First, define a fixture class template.  It should be parameterized
00104 // by a type.  Remember to derive it from testing::Test.
00105 template <typename T>
00106 class FooTest : public testing::Test {
00107   ...
00108 };
00109 
00110 // Next, declare that you will define a type-parameterized test case
00111 // (the _P suffix is for "parameterized" or "pattern", whichever you
00112 // prefer):
00113 TYPED_TEST_CASE_P(FooTest);
00114 
00115 // Then, use TYPED_TEST_P() to define as many type-parameterized tests
00116 // for this type-parameterized test case as you want.
00117 TYPED_TEST_P(FooTest, DoesBlah) {
00118   // Inside a test, refer to TypeParam to get the type parameter.
00119   TypeParam n = 0;
00120   ...
00121 }
00122 
00123 TYPED_TEST_P(FooTest, HasPropertyA) { ... }
00124 
00125 // Now the tricky part: you need to register all test patterns before
00126 // you can instantiate them.  The first argument of the macro is the
00127 // test case name; the rest are the names of the tests in this test
00128 // case.
00129 REGISTER_TYPED_TEST_CASE_P(FooTest,
00130                            DoesBlah, HasPropertyA);
00131 
00132 // Finally, you are free to instantiate the pattern with the types you
00133 // want.  If you put the above code in a header file, you can #include
00134 // it in multiple C++ source files and instantiate it multiple times.
00135 //
00136 // To distinguish different instances of the pattern, the first
00137 // argument to the INSTANTIATE_* macro is a prefix that will be added
00138 // to the actual test case name.  Remember to pick unique prefixes for
00139 // different instances.
00140 typedef testing::Types<char, int, unsigned int> MyTypes;
00141 INSTANTIATE_TYPED_TEST_CASE_P(My, FooTest, MyTypes);
00142 
00143 // If the type list contains only one type, you can write that type
00144 // directly without Types<...>:
00145 //   INSTANTIATE_TYPED_TEST_CASE_P(My, FooTest, int);
00146 
00147 #endif  // 0
00148 
00149 #include "gtest/internal/gtest-port.h"
00150 #include "gtest/internal/gtest-type-util.h"
00151 
00152 // Implements typed tests.
00153 
00154 #if GTEST_HAS_TYPED_TEST
00155 
00156 // INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE.
00157 //
00158 // Expands to the name of the typedef for the type parameters of the
00159 // given test case.
00160 # define GTEST_TYPE_PARAMS_(TestCaseName) gtest_type_params_##TestCaseName##_
00161 
00162 // The 'Types' template argument below must have spaces around it
00163 // since some compilers may choke on '>>' when passing a template
00164 // instance (e.g. Types<int>)
00165 # define TYPED_TEST_CASE(CaseName, Types) \
00166   typedef ::testing::internal::TypeList< Types >::type \
00167       GTEST_TYPE_PARAMS_(CaseName)
00168 
00169 # define TYPED_TEST(CaseName, TestName) \
00170   template <typename gtest_TypeParam_> \
00171   class GTEST_TEST_CLASS_NAME_(CaseName, TestName) \
00172       : public CaseName<gtest_TypeParam_> { \
00173    private: \
00174     typedef CaseName<gtest_TypeParam_> TestFixture; \
00175     typedef gtest_TypeParam_ TypeParam; \
00176     virtual void TestBody(); \
00177   }; \
00178   bool gtest_##CaseName##_##TestName##_registered_ GTEST_ATTRIBUTE_UNUSED_ = \
00179       ::testing::internal::TypeParameterizedTest< \
00180           CaseName, \
00181           ::testing::internal::TemplateSel< \
00182               GTEST_TEST_CLASS_NAME_(CaseName, TestName)>, \
00183           GTEST_TYPE_PARAMS_(CaseName)>::Register(\
00184               "", #CaseName, #TestName, 0); \
00185   template <typename gtest_TypeParam_> \
00186   void GTEST_TEST_CLASS_NAME_(CaseName, TestName)<gtest_TypeParam_>::TestBody()
00187 
00188 #endif  // GTEST_HAS_TYPED_TEST
00189 
00190 // Implements type-parameterized tests.
00191 
00192 #if GTEST_HAS_TYPED_TEST_P
00193 
00194 // INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE.
00195 //
00196 // Expands to the namespace name that the type-parameterized tests for
00197 // the given type-parameterized test case are defined in.  The exact
00198 // name of the namespace is subject to change without notice.
00199 # define GTEST_CASE_NAMESPACE_(TestCaseName) \
00200   gtest_case_##TestCaseName##_
00201 
00202 // INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE.
00203 //
00204 // Expands to the name of the variable used to remember the names of
00205 // the defined tests in the given test case.
00206 # define GTEST_TYPED_TEST_CASE_P_STATE_(TestCaseName) \
00207   gtest_typed_test_case_p_state_##TestCaseName##_
00208 
00209 // INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE DIRECTLY.
00210 //
00211 // Expands to the name of the variable used to remember the names of
00212 // the registered tests in the given test case.
00213 # define GTEST_REGISTERED_TEST_NAMES_(TestCaseName) \
00214   gtest_registered_test_names_##TestCaseName##_
00215 
00216 // The variables defined in the type-parameterized test macros are
00217 // static as typically these macros are used in a .h file that can be
00218 // #included in multiple translation units linked together.
00219 # define TYPED_TEST_CASE_P(CaseName) \
00220   static ::testing::internal::TypedTestCasePState \
00221       GTEST_TYPED_TEST_CASE_P_STATE_(CaseName)
00222 
00223 # define TYPED_TEST_P(CaseName, TestName) \
00224   namespace GTEST_CASE_NAMESPACE_(CaseName) { \
00225   template <typename gtest_TypeParam_> \
00226   class TestName : public CaseName<gtest_TypeParam_> { \
00227    private: \
00228     typedef CaseName<gtest_TypeParam_> TestFixture; \
00229     typedef gtest_TypeParam_ TypeParam; \
00230     virtual void TestBody(); \
00231   }; \
00232   static bool gtest_##TestName##_defined_ GTEST_ATTRIBUTE_UNUSED_ = \
00233       GTEST_TYPED_TEST_CASE_P_STATE_(CaseName).AddTestName(\
00234           __FILE__, __LINE__, #CaseName, #TestName); \
00235   } \
00236   template <typename gtest_TypeParam_> \
00237   void GTEST_CASE_NAMESPACE_(CaseName)::TestName<gtest_TypeParam_>::TestBody()
00238 
00239 # define REGISTER_TYPED_TEST_CASE_P(CaseName, ...) \
00240   namespace GTEST_CASE_NAMESPACE_(CaseName) { \
00241   typedef ::testing::internal::Templates<__VA_ARGS__>::type gtest_AllTests_; \
00242   } \
00243   static const char* const GTEST_REGISTERED_TEST_NAMES_(CaseName) = \
00244       GTEST_TYPED_TEST_CASE_P_STATE_(CaseName).VerifyRegisteredTestNames(\
00245           __FILE__, __LINE__, #__VA_ARGS__)
00246 
00247 // The 'Types' template argument below must have spaces around it
00248 // since some compilers may choke on '>>' when passing a template
00249 // instance (e.g. Types<int>)
00250 # define INSTANTIATE_TYPED_TEST_CASE_P(Prefix, CaseName, Types) \
00251   bool gtest_##Prefix##_##CaseName GTEST_ATTRIBUTE_UNUSED_ = \
00252       ::testing::internal::TypeParameterizedTestCase<CaseName, \
00253           GTEST_CASE_NAMESPACE_(CaseName)::gtest_AllTests_, \
00254           ::testing::internal::TypeList< Types >::type>::Register(\
00255               #Prefix, #CaseName, GTEST_REGISTERED_TEST_NAMES_(CaseName))
00256 
00257 #endif  // GTEST_HAS_TYPED_TEST_P
00258 
00259 #endif  // GTEST_INCLUDE_GTEST_GTEST_TYPED_TEST_H_


pcl
Author(s): Open Perception
autogenerated on Wed Aug 26 2015 15:24:40