gtest-typed-test.h 13 KB

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