gtest-typed-test.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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 GOOGLETEST_INCLUDE_GTEST_GTEST_TYPED_TEST_H_
  31. #define GOOGLETEST_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 suite, 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_SUITE(FooTest, MyTypes);
  52. // If the type list contains only one type, you can write that type
  53. // directly without Types<...>:
  54. // TYPED_TEST_SUITE(FooTest, int);
  55. // Then, use TYPED_TEST() instead of TEST_F() to define as many typed
  56. // tests for this test suite as you want.
  57. TYPED_TEST(FooTest, DoesBlah) {
  58. // Inside a test, refer to the special name TypeParam to get the type
  59. // parameter. Since we are inside a derived class template, C++ requires
  60. // us to 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_SUITE 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_SUITE(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 suite
  111. // (the _P suffix is for "parameterized" or "pattern", whichever you
  112. // prefer):
  113. TYPED_TEST_SUITE_P(FooTest);
  114. // Then, use TYPED_TEST_P() to define as many type-parameterized tests
  115. // for this type-parameterized test suite 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 suite name; the rest are the names of the tests in this test
  125. // case.
  126. REGISTER_TYPED_TEST_SUITE_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 suite name. Remember to pick unique prefixes for
  135. // different instances.
  136. typedef testing::Types<char, int, unsigned int> MyTypes;
  137. INSTANTIATE_TYPED_TEST_SUITE_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_SUITE_P(My, FooTest, int);
  141. //
  142. // Similar to the optional argument of TYPED_TEST_SUITE above,
  143. // INSTANTIATE_TEST_SUITE_P takes an optional fourth argument which allows to
  144. // generate custom names.
  145. // INSTANTIATE_TYPED_TEST_SUITE_P(My, FooTest, MyTypes, MyTypeNames);
  146. #endif // 0
  147. #include "gtest/internal/gtest-internal.h"
  148. #include "gtest/internal/gtest-port.h"
  149. #include "gtest/internal/gtest-type-util.h"
  150. // Implements typed tests.
  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 suite.
  155. #define GTEST_TYPE_PARAMS_(TestSuiteName) gtest_type_params_##TestSuiteName##_
  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_(TestSuiteName) \
  159. gtest_type_params_##TestSuiteName##_NameGenerator
  160. #define TYPED_TEST_SUITE(CaseName, Types, ...) \
  161. typedef ::testing::internal::GenerateTypeList<Types>::type \
  162. GTEST_TYPE_PARAMS_(CaseName); \
  163. typedef ::testing::internal::NameGeneratorSelector<__VA_ARGS__>::type \
  164. GTEST_NAME_GENERATOR_(CaseName)
  165. #define TYPED_TEST(CaseName, TestName) \
  166. static_assert(sizeof(GTEST_STRINGIFY_(TestName)) > 1, \
  167. "test-name must not be empty"); \
  168. template <typename gtest_TypeParam_> \
  169. class GTEST_TEST_CLASS_NAME_(CaseName, TestName) \
  170. : public CaseName<gtest_TypeParam_> { \
  171. private: \
  172. typedef CaseName<gtest_TypeParam_> TestFixture; \
  173. typedef gtest_TypeParam_ TypeParam; \
  174. void TestBody() override; \
  175. }; \
  176. static bool gtest_##CaseName##_##TestName##_registered_ \
  177. GTEST_ATTRIBUTE_UNUSED_ = ::testing::internal::TypeParameterizedTest< \
  178. CaseName, \
  179. ::testing::internal::TemplateSel<GTEST_TEST_CLASS_NAME_(CaseName, \
  180. TestName)>, \
  181. GTEST_TYPE_PARAMS_( \
  182. CaseName)>::Register("", \
  183. ::testing::internal::CodeLocation( \
  184. __FILE__, __LINE__), \
  185. GTEST_STRINGIFY_(CaseName), \
  186. GTEST_STRINGIFY_(TestName), 0, \
  187. ::testing::internal::GenerateNames< \
  188. GTEST_NAME_GENERATOR_(CaseName), \
  189. GTEST_TYPE_PARAMS_(CaseName)>()); \
  190. template <typename gtest_TypeParam_> \
  191. void GTEST_TEST_CLASS_NAME_(CaseName, \
  192. TestName)<gtest_TypeParam_>::TestBody()
  193. // Legacy API is deprecated but still available
  194. #ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_
  195. #define TYPED_TEST_CASE \
  196. static_assert(::testing::internal::TypedTestCaseIsDeprecated(), ""); \
  197. TYPED_TEST_SUITE
  198. #endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_
  199. // Implements type-parameterized tests.
  200. // INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE.
  201. //
  202. // Expands to the namespace name that the type-parameterized tests for
  203. // the given type-parameterized test suite are defined in. The exact
  204. // name of the namespace is subject to change without notice.
  205. #define GTEST_SUITE_NAMESPACE_(TestSuiteName) gtest_suite_##TestSuiteName##_
  206. // INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE.
  207. //
  208. // Expands to the name of the variable used to remember the names of
  209. // the defined tests in the given test suite.
  210. #define GTEST_TYPED_TEST_SUITE_P_STATE_(TestSuiteName) \
  211. gtest_typed_test_suite_p_state_##TestSuiteName##_
  212. // INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE DIRECTLY.
  213. //
  214. // Expands to the name of the variable used to remember the names of
  215. // the registered tests in the given test suite.
  216. #define GTEST_REGISTERED_TEST_NAMES_(TestSuiteName) \
  217. gtest_registered_test_names_##TestSuiteName##_
  218. // The variables defined in the type-parameterized test macros are
  219. // static as typically these macros are used in a .h file that can be
  220. // #included in multiple translation units linked together.
  221. #define TYPED_TEST_SUITE_P(SuiteName) \
  222. static ::testing::internal::TypedTestSuitePState \
  223. GTEST_TYPED_TEST_SUITE_P_STATE_(SuiteName)
  224. // Legacy API is deprecated but still available
  225. #ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_
  226. #define TYPED_TEST_CASE_P \
  227. static_assert(::testing::internal::TypedTestCase_P_IsDeprecated(), ""); \
  228. TYPED_TEST_SUITE_P
  229. #endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_
  230. #define TYPED_TEST_P(SuiteName, TestName) \
  231. namespace GTEST_SUITE_NAMESPACE_(SuiteName) { \
  232. template <typename gtest_TypeParam_> \
  233. class TestName : public SuiteName<gtest_TypeParam_> { \
  234. private: \
  235. typedef SuiteName<gtest_TypeParam_> TestFixture; \
  236. typedef gtest_TypeParam_ TypeParam; \
  237. void TestBody() override; \
  238. }; \
  239. static bool gtest_##TestName##_defined_ GTEST_ATTRIBUTE_UNUSED_ = \
  240. GTEST_TYPED_TEST_SUITE_P_STATE_(SuiteName).AddTestName( \
  241. __FILE__, __LINE__, GTEST_STRINGIFY_(SuiteName), \
  242. GTEST_STRINGIFY_(TestName)); \
  243. } \
  244. template <typename gtest_TypeParam_> \
  245. void GTEST_SUITE_NAMESPACE_( \
  246. SuiteName)::TestName<gtest_TypeParam_>::TestBody()
  247. // Note: this won't work correctly if the trailing arguments are macros.
  248. #define REGISTER_TYPED_TEST_SUITE_P(SuiteName, ...) \
  249. namespace GTEST_SUITE_NAMESPACE_(SuiteName) { \
  250. typedef ::testing::internal::Templates<__VA_ARGS__> gtest_AllTests_; \
  251. } \
  252. static const char* const GTEST_REGISTERED_TEST_NAMES_( \
  253. SuiteName) GTEST_ATTRIBUTE_UNUSED_ = \
  254. GTEST_TYPED_TEST_SUITE_P_STATE_(SuiteName).VerifyRegisteredTestNames( \
  255. GTEST_STRINGIFY_(SuiteName), __FILE__, __LINE__, #__VA_ARGS__)
  256. // Legacy API is deprecated but still available
  257. #ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_
  258. #define REGISTER_TYPED_TEST_CASE_P \
  259. static_assert(::testing::internal::RegisterTypedTestCase_P_IsDeprecated(), \
  260. ""); \
  261. REGISTER_TYPED_TEST_SUITE_P
  262. #endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_
  263. #define INSTANTIATE_TYPED_TEST_SUITE_P(Prefix, SuiteName, Types, ...) \
  264. static_assert(sizeof(GTEST_STRINGIFY_(Prefix)) > 1, \
  265. "test-suit-prefix must not be empty"); \
  266. static bool gtest_##Prefix##_##SuiteName GTEST_ATTRIBUTE_UNUSED_ = \
  267. ::testing::internal::TypeParameterizedTestSuite< \
  268. SuiteName, GTEST_SUITE_NAMESPACE_(SuiteName)::gtest_AllTests_, \
  269. ::testing::internal::GenerateTypeList<Types>::type>:: \
  270. Register(GTEST_STRINGIFY_(Prefix), \
  271. ::testing::internal::CodeLocation(__FILE__, __LINE__), \
  272. &GTEST_TYPED_TEST_SUITE_P_STATE_(SuiteName), \
  273. GTEST_STRINGIFY_(SuiteName), \
  274. GTEST_REGISTERED_TEST_NAMES_(SuiteName), \
  275. ::testing::internal::GenerateNames< \
  276. ::testing::internal::NameGeneratorSelector< \
  277. __VA_ARGS__>::type, \
  278. ::testing::internal::GenerateTypeList<Types>::type>())
  279. // Legacy API is deprecated but still available
  280. #ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_
  281. #define INSTANTIATE_TYPED_TEST_CASE_P \
  282. static_assert( \
  283. ::testing::internal::InstantiateTypedTestCase_P_IsDeprecated(), ""); \
  284. INSTANTIATE_TYPED_TEST_SUITE_P
  285. #endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_
  286. #endif // GOOGLETEST_INCLUDE_GTEST_GTEST_TYPED_TEST_H_