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