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