gtest-typed-test.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. #ifndef GTEST_INCLUDE_GTEST_GTEST_TYPED_TEST_H_
  32. #define GTEST_INCLUDE_GTEST_GTEST_TYPED_TEST_H_
  33. // This header implements typed tests and type-parameterized tests.
  34. // Typed (aka type-driven) tests repeat the same test for types in a
  35. // list. You must know which types you want to test with when writing
  36. // typed tests. Here's how you do it:
  37. #if 0
  38. // First, define a fixture class template. It should be parameterized
  39. // by a type. Remember to derive it from testing::Test.
  40. template <typename T>
  41. class FooTest : public testing::Test {
  42. public:
  43. ...
  44. typedef std::list<T> List;
  45. static T shared_;
  46. T value_;
  47. };
  48. // Next, associate a list of types with the test case, which will be
  49. // repeated for each type in the list. The typedef is necessary for
  50. // the macro to parse correctly.
  51. typedef testing::Types<char, int, unsigned int> MyTypes;
  52. TYPED_TEST_CASE(FooTest, MyTypes);
  53. // If the type list contains only one type, you can write that type
  54. // directly without Types<...>:
  55. // TYPED_TEST_CASE(FooTest, int);
  56. // Then, use TYPED_TEST() instead of TEST_F() to define as many typed
  57. // tests for this test case as you want.
  58. TYPED_TEST(FooTest, DoesBlah) {
  59. // Inside a test, refer to TypeParam to get the type parameter.
  60. // Since we are inside a derived class template, C++ requires use to
  61. // visit the members of FooTest via 'this'.
  62. TypeParam n = this->value_;
  63. // To visit static members of the fixture, add the TestFixture::
  64. // prefix.
  65. n += TestFixture::shared_;
  66. // To refer to typedefs in the fixture, add the "typename
  67. // TestFixture::" prefix.
  68. typename TestFixture::List values;
  69. values.push_back(n);
  70. ...
  71. }
  72. TYPED_TEST(FooTest, HasPropertyA) { ... }
  73. #endif // 0
  74. // Type-parameterized tests are abstract test patterns parameterized
  75. // by a type. Compared with typed tests, type-parameterized tests
  76. // allow you to define the test pattern without knowing what the type
  77. // parameters are. The defined pattern can be instantiated with
  78. // different types any number of times, in any number of translation
  79. // units.
  80. //
  81. // If you are designing an interface or concept, you can define a
  82. // suite of type-parameterized tests to verify properties that any
  83. // valid implementation of the interface/concept should have. Then,
  84. // each implementation can easily instantiate the test suite to verify
  85. // that it conforms to the requirements, without having to write
  86. // similar tests repeatedly. Here's an example:
  87. #if 0
  88. // First, define a fixture class template. It should be parameterized
  89. // by a type. Remember to derive it from testing::Test.
  90. template <typename T>
  91. class FooTest : public testing::Test {
  92. ...
  93. };
  94. // Next, declare that you will define a type-parameterized test case
  95. // (the _P suffix is for "parameterized" or "pattern", whichever you
  96. // prefer):
  97. TYPED_TEST_CASE_P(FooTest);
  98. // Then, use TYPED_TEST_P() to define as many type-parameterized tests
  99. // for this type-parameterized test case as you want.
  100. TYPED_TEST_P(FooTest, DoesBlah) {
  101. // Inside a test, refer to TypeParam to get the type parameter.
  102. TypeParam n = 0;
  103. ...
  104. }
  105. TYPED_TEST_P(FooTest, HasPropertyA) { ... }
  106. // Now the tricky part: you need to register all test patterns before
  107. // you can instantiate them. The first argument of the macro is the
  108. // test case name; the rest are the names of the tests in this test
  109. // case.
  110. REGISTER_TYPED_TEST_CASE_P(FooTest,
  111. DoesBlah, HasPropertyA);
  112. // Finally, you are free to instantiate the pattern with the types you
  113. // want. If you put the above code in a header file, you can #include
  114. // it in multiple C++ source files and instantiate it multiple times.
  115. //
  116. // To distinguish different instances of the pattern, the first
  117. // argument to the INSTANTIATE_* macro is a prefix that will be added
  118. // to the actual test case name. Remember to pick unique prefixes for
  119. // different instances.
  120. typedef testing::Types<char, int, unsigned int> MyTypes;
  121. INSTANTIATE_TYPED_TEST_CASE_P(My, FooTest, MyTypes);
  122. // If the type list contains only one type, you can write that type
  123. // directly without Types<...>:
  124. // INSTANTIATE_TYPED_TEST_CASE_P(My, FooTest, int);
  125. #endif // 0
  126. #include "gtest/internal/gtest-port.h"
  127. #include "gtest/internal/gtest-type-util.h"
  128. // Implements typed tests.
  129. #if GTEST_HAS_TYPED_TEST
  130. // INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE.
  131. //
  132. // Expands to the name of the typedef for the type parameters of the
  133. // given test case.
  134. # define GTEST_TYPE_PARAMS_(TestCaseName) gtest_type_params_##TestCaseName##_
  135. // The 'Types' template argument below must have spaces around it
  136. // since some compilers may choke on '>>' when passing a template
  137. // instance (e.g. Types<int>)
  138. # define TYPED_TEST_CASE(CaseName, Types) \
  139. typedef ::testing::internal::TypeList< Types >::type \
  140. GTEST_TYPE_PARAMS_(CaseName)
  141. # define TYPED_TEST(CaseName, TestName) \
  142. template <typename gtest_TypeParam_> \
  143. class GTEST_TEST_CLASS_NAME_(CaseName, TestName) \
  144. : public CaseName<gtest_TypeParam_> { \
  145. private: \
  146. typedef CaseName<gtest_TypeParam_> TestFixture; \
  147. typedef gtest_TypeParam_ TypeParam; \
  148. virtual void TestBody(); \
  149. }; \
  150. bool gtest_##CaseName##_##TestName##_registered_ GTEST_ATTRIBUTE_UNUSED_ = \
  151. ::testing::internal::TypeParameterizedTest< \
  152. CaseName, \
  153. ::testing::internal::TemplateSel< \
  154. GTEST_TEST_CLASS_NAME_(CaseName, TestName)>, \
  155. GTEST_TYPE_PARAMS_(CaseName)>::Register(\
  156. "", #CaseName, #TestName, 0); \
  157. template <typename gtest_TypeParam_> \
  158. void GTEST_TEST_CLASS_NAME_(CaseName, TestName)<gtest_TypeParam_>::TestBody()
  159. #endif // GTEST_HAS_TYPED_TEST
  160. // Implements type-parameterized tests.
  161. #if GTEST_HAS_TYPED_TEST_P
  162. // INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE.
  163. //
  164. // Expands to the namespace name that the type-parameterized tests for
  165. // the given type-parameterized test case are defined in. The exact
  166. // name of the namespace is subject to change without notice.
  167. # define GTEST_CASE_NAMESPACE_(TestCaseName) \
  168. gtest_case_##TestCaseName##_
  169. // INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE.
  170. //
  171. // Expands to the name of the variable used to remember the names of
  172. // the defined tests in the given test case.
  173. # define GTEST_TYPED_TEST_CASE_P_STATE_(TestCaseName) \
  174. gtest_typed_test_case_p_state_##TestCaseName##_
  175. // INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE DIRECTLY.
  176. //
  177. // Expands to the name of the variable used to remember the names of
  178. // the registered tests in the given test case.
  179. # define GTEST_REGISTERED_TEST_NAMES_(TestCaseName) \
  180. gtest_registered_test_names_##TestCaseName##_
  181. // The variables defined in the type-parameterized test macros are
  182. // static as typically these macros are used in a .h file that can be
  183. // #included in multiple translation units linked together.
  184. # define TYPED_TEST_CASE_P(CaseName) \
  185. static ::testing::internal::TypedTestCasePState \
  186. GTEST_TYPED_TEST_CASE_P_STATE_(CaseName)
  187. # define TYPED_TEST_P(CaseName, TestName) \
  188. namespace GTEST_CASE_NAMESPACE_(CaseName) { \
  189. template <typename gtest_TypeParam_> \
  190. class TestName : public CaseName<gtest_TypeParam_> { \
  191. private: \
  192. typedef CaseName<gtest_TypeParam_> TestFixture; \
  193. typedef gtest_TypeParam_ TypeParam; \
  194. virtual void TestBody(); \
  195. }; \
  196. static bool gtest_##TestName##_defined_ GTEST_ATTRIBUTE_UNUSED_ = \
  197. GTEST_TYPED_TEST_CASE_P_STATE_(CaseName).AddTestName(\
  198. __FILE__, __LINE__, #CaseName, #TestName); \
  199. } \
  200. template <typename gtest_TypeParam_> \
  201. void GTEST_CASE_NAMESPACE_(CaseName)::TestName<gtest_TypeParam_>::TestBody()
  202. # define REGISTER_TYPED_TEST_CASE_P(CaseName, ...) \
  203. namespace GTEST_CASE_NAMESPACE_(CaseName) { \
  204. typedef ::testing::internal::Templates<__VA_ARGS__>::type gtest_AllTests_; \
  205. } \
  206. static const char* const GTEST_REGISTERED_TEST_NAMES_(CaseName) = \
  207. GTEST_TYPED_TEST_CASE_P_STATE_(CaseName).VerifyRegisteredTestNames(\
  208. __FILE__, __LINE__, #__VA_ARGS__)
  209. // The 'Types' template argument below must have spaces around it
  210. // since some compilers may choke on '>>' when passing a template
  211. // instance (e.g. Types<int>)
  212. # define INSTANTIATE_TYPED_TEST_CASE_P(Prefix, CaseName, Types) \
  213. bool gtest_##Prefix##_##CaseName GTEST_ATTRIBUTE_UNUSED_ = \
  214. ::testing::internal::TypeParameterizedTestCase<CaseName, \
  215. GTEST_CASE_NAMESPACE_(CaseName)::gtest_AllTests_, \
  216. ::testing::internal::TypeList< Types >::type>::Register(\
  217. #Prefix, #CaseName, GTEST_REGISTERED_TEST_NAMES_(CaseName))
  218. #endif // GTEST_HAS_TYPED_TEST_P
  219. #endif // GTEST_INCLUDE_GTEST_GTEST_TYPED_TEST_H_