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 // Macros and functions for implementing parameterized tests
31 // in Google C++ Testing and Mocking Framework (Google Test)
32 #ifndef GOOGLETEST_INCLUDE_GTEST_GTEST_PARAM_TEST_H_
33 #define GOOGLETEST_INCLUDE_GTEST_GTEST_PARAM_TEST_H_
34 
35 // Value-parameterized tests allow you to test your code with different
36 // parameters without writing multiple copies of the same test.
37 //
38 // Here is how you use value-parameterized tests:
39 
40 #if 0
41 
42 // To write value-parameterized tests, first you should define a fixture
43 // class. It is usually derived from testing::TestWithParam<T> (see below for
44 // another inheritance scheme that's sometimes useful in more complicated
45 // class hierarchies), where the type of your parameter values.
46 // TestWithParam<T> is itself derived from testing::Test. T can be any
47 // copyable type. If it's a raw pointer, you are responsible for managing the
48 // lifespan of the pointed values.
49 
50 class FooTest : public ::testing::TestWithParam<const char*> {
51   // You can implement all the usual class fixture members here.
52 };
53 
54 // Then, use the TEST_P macro to define as many parameterized tests
55 // for this fixture as you want. The _P suffix is for "parameterized"
56 // or "pattern", whichever you prefer to think.
57 
58 TEST_P(FooTest, DoesBlah) {
59   // Inside a test, access the test parameter with the GetParam() method
60   // of the TestWithParam<T> class:
61   EXPECT_TRUE(foo.Blah(GetParam()));
62   ...
63 }
64 
65 TEST_P(FooTest, HasBlahBlah) {
66   ...
67 }
68 
69 // Finally, you can use INSTANTIATE_TEST_SUITE_P to instantiate the test
70 // case with any set of parameters you want. Google Test defines a number
71 // of functions for generating test parameters. They return what we call
72 // (surprise!) parameter generators. Here is a summary of them, which
73 // are all in the testing namespace:
74 //
75 //
76 //  Range(begin, end [, step]) - Yields values {begin, begin+step,
77 //                               begin+step+step, ...}. The values do not
78 //                               include end. step defaults to 1.
79 //  Values(v1, v2, ..., vN)    - Yields values {v1, v2, ..., vN}.
80 //  ValuesIn(container)        - Yields values from a C-style array, an STL
81 //  ValuesIn(begin,end)          container, or an iterator range [begin, end).
82 //  Bool()                     - Yields sequence {false, true}.
83 //  Combine(g1, g2, ..., gN)   - Yields all combinations (the Cartesian product
84 //                               for the math savvy) of the values generated
85 //                               by the N generators.
86 //
87 // For more details, see comments at the definitions of these functions below
88 // in this file.
89 //
90 // The following statement will instantiate tests from the FooTest test suite
91 // each with parameter values "meeny", "miny", and "moe".
92 
93 INSTANTIATE_TEST_SUITE_P(InstantiationName,
94                          FooTest,
95                          Values("meeny", "miny", "moe"));
96 
97 // To distinguish different instances of the pattern, (yes, you
98 // can instantiate it more than once) the first argument to the
99 // INSTANTIATE_TEST_SUITE_P macro is a prefix that will be added to the
100 // actual test suite name. Remember to pick unique prefixes for different
101 // instantiations. The tests from the instantiation above will have
102 // these names:
103 //
104 //    * InstantiationName/FooTest.DoesBlah/0 for "meeny"
105 //    * InstantiationName/FooTest.DoesBlah/1 for "miny"
106 //    * InstantiationName/FooTest.DoesBlah/2 for "moe"
107 //    * InstantiationName/FooTest.HasBlahBlah/0 for "meeny"
108 //    * InstantiationName/FooTest.HasBlahBlah/1 for "miny"
109 //    * InstantiationName/FooTest.HasBlahBlah/2 for "moe"
110 //
111 // You can use these names in --gtest_filter.
112 //
113 // This statement will instantiate all tests from FooTest again, each
114 // with parameter values "cat" and "dog":
115 
116 const char* pets[] = {"cat", "dog"};
117 INSTANTIATE_TEST_SUITE_P(AnotherInstantiationName, FooTest, ValuesIn(pets));
118 
119 // The tests from the instantiation above will have these names:
120 //
121 //    * AnotherInstantiationName/FooTest.DoesBlah/0 for "cat"
122 //    * AnotherInstantiationName/FooTest.DoesBlah/1 for "dog"
123 //    * AnotherInstantiationName/FooTest.HasBlahBlah/0 for "cat"
124 //    * AnotherInstantiationName/FooTest.HasBlahBlah/1 for "dog"
125 //
126 // Please note that INSTANTIATE_TEST_SUITE_P will instantiate all tests
127 // in the given test suite, whether their definitions come before or
128 // AFTER the INSTANTIATE_TEST_SUITE_P statement.
129 //
130 // Please also note that generator expressions (including parameters to the
131 // generators) are evaluated in InitGoogleTest(), after main() has started.
132 // This allows the user on one hand, to adjust generator parameters in order
133 // to dynamically determine a set of tests to run and on the other hand,
134 // give the user a chance to inspect the generated tests with Google Test
135 // reflection API before RUN_ALL_TESTS() is executed.
136 //
137 // You can see samples/sample7_unittest.cc and samples/sample8_unittest.cc
138 // for more examples.
139 //
140 // In the future, we plan to publish the API for defining new parameter
141 // generators. But for now this interface remains part of the internal
142 // implementation and is subject to change.
143 //
144 //
145 // A parameterized test fixture must be derived from testing::Test and from
146 // testing::WithParamInterface<T>, where T is the type of the parameter
147 // values. Inheriting from TestWithParam<T> satisfies that requirement because
148 // TestWithParam<T> inherits from both Test and WithParamInterface. In more
149 // complicated hierarchies, however, it is occasionally useful to inherit
150 // separately from Test and WithParamInterface. For example:
151 
152 class BaseTest : public ::testing::Test {
153   // You can inherit all the usual members for a non-parameterized test
154   // fixture here.
155 };
156 
157 class DerivedTest : public BaseTest, public ::testing::WithParamInterface<int> {
158   // The usual test fixture members go here too.
159 };
160 
161 TEST_F(BaseTest, HasFoo) {
162   // This is an ordinary non-parameterized test.
163 }
164 
165 TEST_P(DerivedTest, DoesBlah) {
166   // GetParam works just the same here as if you inherit from TestWithParam.
167   EXPECT_TRUE(foo.Blah(GetParam()));
168 }
169 
170 #endif  // 0
171 
172 #include <iterator>
173 #include <utility>
174 
175 #include "gtest/internal/gtest-internal.h"
176 #include "gtest/internal/gtest-param-util.h"
177 #include "gtest/internal/gtest-port.h"
178 
179 namespace testing {
180 
181 // Functions producing parameter generators.
182 //
183 // Google Test uses these generators to produce parameters for value-
184 // parameterized tests. When a parameterized test suite is instantiated
185 // with a particular generator, Google Test creates and runs tests
186 // for each element in the sequence produced by the generator.
187 //
188 // In the following sample, tests from test suite FooTest are instantiated
189 // each three times with parameter values 3, 5, and 8:
190 //
191 // class FooTest : public TestWithParam<int> { ... };
192 //
193 // TEST_P(FooTest, TestThis) {
194 // }
195 // TEST_P(FooTest, TestThat) {
196 // }
197 // INSTANTIATE_TEST_SUITE_P(TestSequence, FooTest, Values(3, 5, 8));
198 //
199 
200 // Range() returns generators providing sequences of values in a range.
201 //
202 // Synopsis:
203 // Range(start, end)
204 //   - returns a generator producing a sequence of values {start, start+1,
205 //     start+2, ..., }.
206 // Range(start, end, step)
207 //   - returns a generator producing a sequence of values {start, start+step,
208 //     start+step+step, ..., }.
209 // Notes:
210 //   * The generated sequences never include end. For example, Range(1, 5)
211 //     returns a generator producing a sequence {1, 2, 3, 4}. Range(1, 9, 2)
212 //     returns a generator producing {1, 3, 5, 7}.
213 //   * start and end must have the same type. That type may be any integral or
214 //     floating-point type or a user defined type satisfying these conditions:
215 //     * It must be assignable (have operator=() defined).
216 //     * It must have operator+() (operator+(int-compatible type) for
217 //       two-operand version).
218 //     * It must have operator<() defined.
219 //     Elements in the resulting sequences will also have that type.
220 //   * Condition start < end must be satisfied in order for resulting sequences
221 //     to contain any elements.
222 //
223 template <typename T, typename IncrementT>
Range(T start,T end,IncrementT step)224 internal::ParamGenerator<T> Range(T start, T end, IncrementT step) {
225   return internal::ParamGenerator<T>(
226       new internal::RangeGenerator<T, IncrementT>(start, end, step));
227 }
228 
229 template <typename T>
Range(T start,T end)230 internal::ParamGenerator<T> Range(T start, T end) {
231   return Range(start, end, 1);
232 }
233 
234 // ValuesIn() function allows generation of tests with parameters coming from
235 // a container.
236 //
237 // Synopsis:
238 // ValuesIn(const T (&array)[N])
239 //   - returns a generator producing sequences with elements from
240 //     a C-style array.
241 // ValuesIn(const Container& container)
242 //   - returns a generator producing sequences with elements from
243 //     an STL-style container.
244 // ValuesIn(Iterator begin, Iterator end)
245 //   - returns a generator producing sequences with elements from
246 //     a range [begin, end) defined by a pair of STL-style iterators. These
247 //     iterators can also be plain C pointers.
248 //
249 // Please note that ValuesIn copies the values from the containers
250 // passed in and keeps them to generate tests in RUN_ALL_TESTS().
251 //
252 // Examples:
253 //
254 // This instantiates tests from test suite StringTest
255 // each with C-string values of "foo", "bar", and "baz":
256 //
257 // const char* strings[] = {"foo", "bar", "baz"};
258 // INSTANTIATE_TEST_SUITE_P(StringSequence, StringTest, ValuesIn(strings));
259 //
260 // This instantiates tests from test suite StlStringTest
261 // each with STL strings with values "a" and "b":
262 //
263 // ::std::vector< ::std::string> GetParameterStrings() {
264 //   ::std::vector< ::std::string> v;
265 //   v.push_back("a");
266 //   v.push_back("b");
267 //   return v;
268 // }
269 //
270 // INSTANTIATE_TEST_SUITE_P(CharSequence,
271 //                          StlStringTest,
272 //                          ValuesIn(GetParameterStrings()));
273 //
274 //
275 // This will also instantiate tests from CharTest
276 // each with parameter values 'a' and 'b':
277 //
278 // ::std::list<char> GetParameterChars() {
279 //   ::std::list<char> list;
280 //   list.push_back('a');
281 //   list.push_back('b');
282 //   return list;
283 // }
284 // ::std::list<char> l = GetParameterChars();
285 // INSTANTIATE_TEST_SUITE_P(CharSequence2,
286 //                          CharTest,
287 //                          ValuesIn(l.begin(), l.end()));
288 //
289 template <typename ForwardIterator>
290 internal::ParamGenerator<
291     typename std::iterator_traits<ForwardIterator>::value_type>
ValuesIn(ForwardIterator begin,ForwardIterator end)292 ValuesIn(ForwardIterator begin, ForwardIterator end) {
293   typedef typename std::iterator_traits<ForwardIterator>::value_type ParamType;
294   return internal::ParamGenerator<ParamType>(
295       new internal::ValuesInIteratorRangeGenerator<ParamType>(begin, end));
296 }
297 
298 template <typename T, size_t N>
ValuesIn(const T (& array)[N])299 internal::ParamGenerator<T> ValuesIn(const T (&array)[N]) {
300   return ValuesIn(array, array + N);
301 }
302 
303 template <class Container>
ValuesIn(const Container & container)304 internal::ParamGenerator<typename Container::value_type> ValuesIn(
305     const Container& container) {
306   return ValuesIn(container.begin(), container.end());
307 }
308 
309 // Values() allows generating tests from explicitly specified list of
310 // parameters.
311 //
312 // Synopsis:
313 // Values(T v1, T v2, ..., T vN)
314 //   - returns a generator producing sequences with elements v1, v2, ..., vN.
315 //
316 // For example, this instantiates tests from test suite BarTest each
317 // with values "one", "two", and "three":
318 //
319 // INSTANTIATE_TEST_SUITE_P(NumSequence,
320 //                          BarTest,
321 //                          Values("one", "two", "three"));
322 //
323 // This instantiates tests from test suite BazTest each with values 1, 2, 3.5.
324 // The exact type of values will depend on the type of parameter in BazTest.
325 //
326 // INSTANTIATE_TEST_SUITE_P(FloatingNumbers, BazTest, Values(1, 2, 3.5));
327 //
328 //
329 template <typename... T>
Values(T...v)330 internal::ValueArray<T...> Values(T... v) {
331   return internal::ValueArray<T...>(std::move(v)...);
332 }
333 
334 // Bool() allows generating tests with parameters in a set of (false, true).
335 //
336 // Synopsis:
337 // Bool()
338 //   - returns a generator producing sequences with elements {false, true}.
339 //
340 // It is useful when testing code that depends on Boolean flags. Combinations
341 // of multiple flags can be tested when several Bool()'s are combined using
342 // Combine() function.
343 //
344 // In the following example all tests in the test suite FlagDependentTest
345 // will be instantiated twice with parameters false and true.
346 //
347 // class FlagDependentTest : public testing::TestWithParam<bool> {
348 //   virtual void SetUp() {
349 //     external_flag = GetParam();
350 //   }
351 // }
352 // INSTANTIATE_TEST_SUITE_P(BoolSequence, FlagDependentTest, Bool());
353 //
Bool()354 inline internal::ParamGenerator<bool> Bool() {
355   return Values(false, true);
356 }
357 
358 // Combine() allows the user to combine two or more sequences to produce
359 // values of a Cartesian product of those sequences' elements.
360 //
361 // Synopsis:
362 // Combine(gen1, gen2, ..., genN)
363 //   - returns a generator producing sequences with elements coming from
364 //     the Cartesian product of elements from the sequences generated by
365 //     gen1, gen2, ..., genN. The sequence elements will have a type of
366 //     std::tuple<T1, T2, ..., TN> where T1, T2, ..., TN are the types
367 //     of elements from sequences produces by gen1, gen2, ..., genN.
368 //
369 // Example:
370 //
371 // This will instantiate tests in test suite AnimalTest each one with
372 // the parameter values tuple("cat", BLACK), tuple("cat", WHITE),
373 // tuple("dog", BLACK), and tuple("dog", WHITE):
374 //
375 // enum Color { BLACK, GRAY, WHITE };
376 // class AnimalTest
377 //     : public testing::TestWithParam<std::tuple<const char*, Color> > {...};
378 //
379 // TEST_P(AnimalTest, AnimalLooksNice) {...}
380 //
381 // INSTANTIATE_TEST_SUITE_P(AnimalVariations, AnimalTest,
382 //                          Combine(Values("cat", "dog"),
383 //                                  Values(BLACK, WHITE)));
384 //
385 // This will instantiate tests in FlagDependentTest with all variations of two
386 // Boolean flags:
387 //
388 // class FlagDependentTest
389 //     : public testing::TestWithParam<std::tuple<bool, bool> > {
390 //   virtual void SetUp() {
391 //     // Assigns external_flag_1 and external_flag_2 values from the tuple.
392 //     std::tie(external_flag_1, external_flag_2) = GetParam();
393 //   }
394 // };
395 //
396 // TEST_P(FlagDependentTest, TestFeature1) {
397 //   // Test your code using external_flag_1 and external_flag_2 here.
398 // }
399 // INSTANTIATE_TEST_SUITE_P(TwoBoolSequence, FlagDependentTest,
400 //                          Combine(Bool(), Bool()));
401 //
402 template <typename... Generator>
Combine(const Generator &...g)403 internal::CartesianProductHolder<Generator...> Combine(const Generator&... g) {
404   return internal::CartesianProductHolder<Generator...>(g...);
405 }
406 
407 #define TEST_P(test_suite_name, test_name)                                     \
408   class GTEST_TEST_CLASS_NAME_(test_suite_name, test_name)                     \
409       : public test_suite_name {                                               \
410    public:                                                                     \
411     GTEST_TEST_CLASS_NAME_(test_suite_name, test_name)() {}                    \
412     void TestBody() override;                                                  \
413                                                                                \
414    private:                                                                    \
415     static int AddToRegistry() {                                               \
416       ::testing::UnitTest::GetInstance()                                       \
417           ->parameterized_test_registry()                                      \
418           .GetTestSuitePatternHolder<test_suite_name>(                         \
419               GTEST_STRINGIFY_(test_suite_name),                               \
420               ::testing::internal::CodeLocation(__FILE__, __LINE__))           \
421           ->AddTestPattern(                                                    \
422               GTEST_STRINGIFY_(test_suite_name), GTEST_STRINGIFY_(test_name),  \
423               new ::testing::internal::TestMetaFactory<GTEST_TEST_CLASS_NAME_( \
424                   test_suite_name, test_name)>(),                              \
425               ::testing::internal::CodeLocation(__FILE__, __LINE__));          \
426       return 0;                                                                \
427     }                                                                          \
428     static int gtest_registering_dummy_ GTEST_ATTRIBUTE_UNUSED_;               \
429     GTEST_DISALLOW_COPY_AND_ASSIGN_(GTEST_TEST_CLASS_NAME_(test_suite_name,    \
430                                                            test_name));        \
431   };                                                                           \
432   int GTEST_TEST_CLASS_NAME_(test_suite_name,                                  \
433                              test_name)::gtest_registering_dummy_ =            \
434       GTEST_TEST_CLASS_NAME_(test_suite_name, test_name)::AddToRegistry();     \
435   void GTEST_TEST_CLASS_NAME_(test_suite_name, test_name)::TestBody()
436 
437 // The last argument to INSTANTIATE_TEST_SUITE_P allows the user to specify
438 // generator and an optional function or functor that generates custom test name
439 // suffixes based on the test parameters. Such a function or functor should
440 // accept one argument of type testing::TestParamInfo<class ParamType>, and
441 // return std::string.
442 //
443 // testing::PrintToStringParamName is a builtin test suffix generator that
444 // returns the value of testing::PrintToString(GetParam()).
445 //
446 // Note: test names must be non-empty, unique, and may only contain ASCII
447 // alphanumeric characters or underscore. Because PrintToString adds quotes
448 // to std::string and C strings, it won't work for these types.
449 
450 #define GTEST_EXPAND_(arg) arg
451 #define GTEST_GET_FIRST_(first, ...) first
452 #define GTEST_GET_SECOND_(first, second, ...) second
453 
454 #define INSTANTIATE_TEST_SUITE_P(prefix, test_suite_name, ...)                \
455   static ::testing::internal::ParamGenerator<test_suite_name::ParamType>      \
456       gtest_##prefix##test_suite_name##_EvalGenerator_() {                    \
457     return GTEST_EXPAND_(GTEST_GET_FIRST_(__VA_ARGS__, DUMMY_PARAM_));        \
458   }                                                                           \
459   static ::std::string gtest_##prefix##test_suite_name##_EvalGenerateName_(   \
460       const ::testing::TestParamInfo<test_suite_name::ParamType>& info) {     \
461     if (::testing::internal::AlwaysFalse()) {                                 \
462       ::testing::internal::TestNotEmpty(GTEST_EXPAND_(GTEST_GET_SECOND_(      \
463           __VA_ARGS__,                                                        \
464           ::testing::internal::DefaultParamName<test_suite_name::ParamType>,  \
465           DUMMY_PARAM_)));                                                    \
466       auto t = std::make_tuple(__VA_ARGS__);                                  \
467       static_assert(std::tuple_size<decltype(t)>::value <= 2,                 \
468                     "Too Many Args!");                                        \
469     }                                                                         \
470     return ((GTEST_EXPAND_(GTEST_GET_SECOND_(                                 \
471         __VA_ARGS__,                                                          \
472         ::testing::internal::DefaultParamName<test_suite_name::ParamType>,    \
473         DUMMY_PARAM_))))(info);                                               \
474   }                                                                           \
475   static int gtest_##prefix##test_suite_name##_dummy_                         \
476       GTEST_ATTRIBUTE_UNUSED_ =                                               \
477           ::testing::UnitTest::GetInstance()                                  \
478               ->parameterized_test_registry()                                 \
479               .GetTestSuitePatternHolder<test_suite_name>(                    \
480                   GTEST_STRINGIFY_(test_suite_name),                          \
481                   ::testing::internal::CodeLocation(__FILE__, __LINE__))      \
482               ->AddTestSuiteInstantiation(                                    \
483                   GTEST_STRINGIFY_(prefix),                                   \
484                   &gtest_##prefix##test_suite_name##_EvalGenerator_,          \
485                   &gtest_##prefix##test_suite_name##_EvalGenerateName_,       \
486                   __FILE__, __LINE__)
487 
488 
489 // Allow Marking a Parameterized test class as not needing to be instantiated.
490 #define GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(T)                   \
491   namespace gtest_do_not_use_outside_namespace_scope {}                   \
492   static const ::testing::internal::MarkAsIgnored gtest_allow_ignore_##T( \
493       GTEST_STRINGIFY_(T))
494 
495 // Legacy API is deprecated but still available
496 #ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_
497 #define INSTANTIATE_TEST_CASE_P                                            \
498   static_assert(::testing::internal::InstantiateTestCase_P_IsDeprecated(), \
499                 "");                                                       \
500   INSTANTIATE_TEST_SUITE_P
501 #endif  // GTEST_REMOVE_LEGACY_TEST_CASEAPI_
502 
503 }  // namespace testing
504 
505 #endif  // GOOGLETEST_INCLUDE_GTEST_GTEST_PARAM_TEST_H_
506