Path: blob/master/dep/googletest/include/gtest/gtest-param-test.h
4806 views
// Copyright 2008, Google Inc.1// All rights reserved.2//3// Redistribution and use in source and binary forms, with or without4// modification, are permitted provided that the following conditions are5// met:6//7// * Redistributions of source code must retain the above copyright8// notice, this list of conditions and the following disclaimer.9// * Redistributions in binary form must reproduce the above10// copyright notice, this list of conditions and the following disclaimer11// in the documentation and/or other materials provided with the12// distribution.13// * Neither the name of Google Inc. nor the names of its14// contributors may be used to endorse or promote products derived from15// this software without specific prior written permission.16//17// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS18// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT19// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR20// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT21// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,22// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT23// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,24// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY25// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT26// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE27// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.2829// Macros and functions for implementing parameterized tests30// in Google C++ Testing and Mocking Framework (Google Test)3132// IWYU pragma: private, include "gtest/gtest.h"33// IWYU pragma: friend gtest/.*34// IWYU pragma: friend gmock/.*3536#ifndef GOOGLETEST_INCLUDE_GTEST_GTEST_PARAM_TEST_H_37#define GOOGLETEST_INCLUDE_GTEST_GTEST_PARAM_TEST_H_3839// Value-parameterized tests allow you to test your code with different40// parameters without writing multiple copies of the same test.41//42// Here is how you use value-parameterized tests:4344#if 04546// To write value-parameterized tests, first you should define a fixture47// class. It is usually derived from testing::TestWithParam<T> (see below for48// another inheritance scheme that's sometimes useful in more complicated49// class hierarchies), where the type of your parameter values.50// TestWithParam<T> is itself derived from testing::Test. T can be any51// copyable type. If it's a raw pointer, you are responsible for managing the52// lifespan of the pointed values.5354class FooTest : public ::testing::TestWithParam<const char*> {55// You can implement all the usual class fixture members here.56};5758// Then, use the TEST_P macro to define as many parameterized tests59// for this fixture as you want. The _P suffix is for "parameterized"60// or "pattern", whichever you prefer to think.6162TEST_P(FooTest, DoesBlah) {63// Inside a test, access the test parameter with the GetParam() method64// of the TestWithParam<T> class:65EXPECT_TRUE(foo.Blah(GetParam()));66...67}6869TEST_P(FooTest, HasBlahBlah) {70...71}7273// Finally, you can use INSTANTIATE_TEST_SUITE_P to instantiate the test74// case with any set of parameters you want. Google Test defines a number75// of functions for generating test parameters. They return what we call76// (surprise!) parameter generators. Here is a summary of them, which77// are all in the testing namespace:78//79//80// Range(begin, end [, step]) - Yields values {begin, begin+step,81// begin+step+step, ...}. The values do not82// include end. step defaults to 1.83// Values(v1, v2, ..., vN) - Yields values {v1, v2, ..., vN}.84// ValuesIn(container) - Yields values from a C-style array, an STL85// ValuesIn(begin,end) container, or an iterator range [begin, end).86// Bool() - Yields sequence {false, true}.87// Combine(g1, g2, ..., gN) - Yields all combinations (the Cartesian product88// for the math savvy) of the values generated89// by the N generators.90//91// For more details, see comments at the definitions of these functions below92// in this file.93//94// The following statement will instantiate tests from the FooTest test suite95// each with parameter values "meeny", "miny", and "moe".9697INSTANTIATE_TEST_SUITE_P(InstantiationName,98FooTest,99Values("meeny", "miny", "moe"));100101// To distinguish different instances of the pattern, (yes, you102// can instantiate it more than once) the first argument to the103// INSTANTIATE_TEST_SUITE_P macro is a prefix that will be added to the104// actual test suite name. Remember to pick unique prefixes for different105// instantiations. The tests from the instantiation above will have106// these names:107//108// * InstantiationName/FooTest.DoesBlah/0 for "meeny"109// * InstantiationName/FooTest.DoesBlah/1 for "miny"110// * InstantiationName/FooTest.DoesBlah/2 for "moe"111// * InstantiationName/FooTest.HasBlahBlah/0 for "meeny"112// * InstantiationName/FooTest.HasBlahBlah/1 for "miny"113// * InstantiationName/FooTest.HasBlahBlah/2 for "moe"114//115// You can use these names in --gtest_filter.116//117// This statement will instantiate all tests from FooTest again, each118// with parameter values "cat" and "dog":119120const char* pets[] = {"cat", "dog"};121INSTANTIATE_TEST_SUITE_P(AnotherInstantiationName, FooTest, ValuesIn(pets));122123// The tests from the instantiation above will have these names:124//125// * AnotherInstantiationName/FooTest.DoesBlah/0 for "cat"126// * AnotherInstantiationName/FooTest.DoesBlah/1 for "dog"127// * AnotherInstantiationName/FooTest.HasBlahBlah/0 for "cat"128// * AnotherInstantiationName/FooTest.HasBlahBlah/1 for "dog"129//130// Please note that INSTANTIATE_TEST_SUITE_P will instantiate all tests131// in the given test suite, whether their definitions come before or132// AFTER the INSTANTIATE_TEST_SUITE_P statement.133//134// Please also note that generator expressions (including parameters to the135// generators) are evaluated in InitGoogleTest(), after main() has started.136// This allows the user on one hand, to adjust generator parameters in order137// to dynamically determine a set of tests to run and on the other hand,138// give the user a chance to inspect the generated tests with Google Test139// reflection API before RUN_ALL_TESTS() is executed.140//141// You can see samples/sample7_unittest.cc and samples/sample8_unittest.cc142// for more examples.143//144// In the future, we plan to publish the API for defining new parameter145// generators. But for now this interface remains part of the internal146// implementation and is subject to change.147//148//149// A parameterized test fixture must be derived from testing::Test and from150// testing::WithParamInterface<T>, where T is the type of the parameter151// values. Inheriting from TestWithParam<T> satisfies that requirement because152// TestWithParam<T> inherits from both Test and WithParamInterface. In more153// complicated hierarchies, however, it is occasionally useful to inherit154// separately from Test and WithParamInterface. For example:155156class BaseTest : public ::testing::Test {157// You can inherit all the usual members for a non-parameterized test158// fixture here.159};160161class DerivedTest : public BaseTest, public ::testing::WithParamInterface<int> {162// The usual test fixture members go here too.163};164165TEST_F(BaseTest, HasFoo) {166// This is an ordinary non-parameterized test.167}168169TEST_P(DerivedTest, DoesBlah) {170// GetParam works just the same here as if you inherit from TestWithParam.171EXPECT_TRUE(foo.Blah(GetParam()));172}173174#endif // 0175176#include <functional>177#include <iterator>178#include <utility>179180#include "gtest/internal/gtest-internal.h"181#include "gtest/internal/gtest-param-util.h" // IWYU pragma: export182#include "gtest/internal/gtest-port.h"183184namespace testing {185186// Functions producing parameter generators.187//188// Google Test uses these generators to produce parameters for value-189// parameterized tests. When a parameterized test suite is instantiated190// with a particular generator, Google Test creates and runs tests191// for each element in the sequence produced by the generator.192//193// In the following sample, tests from test suite FooTest are instantiated194// each three times with parameter values 3, 5, and 8:195//196// class FooTest : public TestWithParam<int> { ... };197//198// TEST_P(FooTest, TestThis) {199// }200// TEST_P(FooTest, TestThat) {201// }202// INSTANTIATE_TEST_SUITE_P(TestSequence, FooTest, Values(3, 5, 8));203//204205// Range() returns generators providing sequences of values in a range.206//207// Synopsis:208// Range(start, end)209// - returns a generator producing a sequence of values {start, start+1,210// start+2, ..., }.211// Range(start, end, step)212// - returns a generator producing a sequence of values {start, start+step,213// start+step+step, ..., }.214// Notes:215// * The generated sequences never include end. For example, Range(1, 5)216// returns a generator producing a sequence {1, 2, 3, 4}. Range(1, 9, 2)217// returns a generator producing {1, 3, 5, 7}.218// * start and end must have the same type. That type may be any integral or219// floating-point type or a user defined type satisfying these conditions:220// * It must be assignable (have operator=() defined).221// * It must have operator+() (operator+(int-compatible type) for222// two-operand version).223// * It must have operator<() defined.224// Elements in the resulting sequences will also have that type.225// * Condition start < end must be satisfied in order for resulting sequences226// to contain any elements.227//228template <typename T, typename IncrementT>229internal::ParamGenerator<T> Range(T start, T end, IncrementT step) {230return internal::ParamGenerator<T>(231new internal::RangeGenerator<T, IncrementT>(start, end, step));232}233234template <typename T>235internal::ParamGenerator<T> Range(T start, T end) {236return Range(start, end, 1);237}238239// ValuesIn() function allows generation of tests with parameters coming from240// a container.241//242// Synopsis:243// ValuesIn(const T (&array)[N])244// - returns a generator producing sequences with elements from245// a C-style array.246// ValuesIn(const Container& container)247// - returns a generator producing sequences with elements from248// an STL-style container.249// ValuesIn(Iterator begin, Iterator end)250// - returns a generator producing sequences with elements from251// a range [begin, end) defined by a pair of STL-style iterators. These252// iterators can also be plain C pointers.253//254// Please note that ValuesIn copies the values from the containers255// passed in and keeps them to generate tests in RUN_ALL_TESTS().256//257// Examples:258//259// This instantiates tests from test suite StringTest260// each with C-string values of "foo", "bar", and "baz":261//262// const char* strings[] = {"foo", "bar", "baz"};263// INSTANTIATE_TEST_SUITE_P(StringSequence, StringTest, ValuesIn(strings));264//265// This instantiates tests from test suite StlStringTest266// each with STL strings with values "a" and "b":267//268// ::std::vector< ::std::string> GetParameterStrings() {269// ::std::vector< ::std::string> v;270// v.push_back("a");271// v.push_back("b");272// return v;273// }274//275// INSTANTIATE_TEST_SUITE_P(CharSequence,276// StlStringTest,277// ValuesIn(GetParameterStrings()));278//279//280// This will also instantiate tests from CharTest281// each with parameter values 'a' and 'b':282//283// ::std::list<char> GetParameterChars() {284// ::std::list<char> list;285// list.push_back('a');286// list.push_back('b');287// return list;288// }289// ::std::list<char> l = GetParameterChars();290// INSTANTIATE_TEST_SUITE_P(CharSequence2,291// CharTest,292// ValuesIn(l.begin(), l.end()));293//294template <typename ForwardIterator>295internal::ParamGenerator<296typename std::iterator_traits<ForwardIterator>::value_type>297ValuesIn(ForwardIterator begin, ForwardIterator end) {298typedef typename std::iterator_traits<ForwardIterator>::value_type ParamType;299return internal::ParamGenerator<ParamType>(300new internal::ValuesInIteratorRangeGenerator<ParamType>(begin, end));301}302303template <typename T, size_t N>304internal::ParamGenerator<T> ValuesIn(const T (&array)[N]) {305return ValuesIn(array, array + N);306}307308template <class Container>309internal::ParamGenerator<typename Container::value_type> ValuesIn(310const Container& container) {311return ValuesIn(container.begin(), container.end());312}313314// Values() allows generating tests from explicitly specified list of315// parameters.316//317// Synopsis:318// Values(T v1, T v2, ..., T vN)319// - returns a generator producing sequences with elements v1, v2, ..., vN.320//321// For example, this instantiates tests from test suite BarTest each322// with values "one", "two", and "three":323//324// INSTANTIATE_TEST_SUITE_P(NumSequence,325// BarTest,326// Values("one", "two", "three"));327//328// This instantiates tests from test suite BazTest each with values 1, 2, 3.5.329// The exact type of values will depend on the type of parameter in BazTest.330//331// INSTANTIATE_TEST_SUITE_P(FloatingNumbers, BazTest, Values(1, 2, 3.5));332//333//334template <typename... T>335internal::ValueArray<T...> Values(T... v) {336return internal::ValueArray<T...>(std::move(v)...);337}338339// Bool() allows generating tests with parameters in a set of (false, true).340//341// Synopsis:342// Bool()343// - returns a generator producing sequences with elements {false, true}.344//345// It is useful when testing code that depends on Boolean flags. Combinations346// of multiple flags can be tested when several Bool()'s are combined using347// Combine() function.348//349// In the following example all tests in the test suite FlagDependentTest350// will be instantiated twice with parameters false and true.351//352// class FlagDependentTest : public testing::TestWithParam<bool> {353// virtual void SetUp() {354// external_flag = GetParam();355// }356// }357// INSTANTIATE_TEST_SUITE_P(BoolSequence, FlagDependentTest, Bool());358//359inline internal::ParamGenerator<bool> Bool() { return Values(false, true); }360361// Combine() allows the user to combine two or more sequences to produce362// values of a Cartesian product of those sequences' elements.363//364// Synopsis:365// Combine(gen1, gen2, ..., genN)366// - returns a generator producing sequences with elements coming from367// the Cartesian product of elements from the sequences generated by368// gen1, gen2, ..., genN. The sequence elements will have a type of369// std::tuple<T1, T2, ..., TN> where T1, T2, ..., TN are the types370// of elements from sequences produces by gen1, gen2, ..., genN.371//372// Example:373//374// This will instantiate tests in test suite AnimalTest each one with375// the parameter values tuple("cat", BLACK), tuple("cat", WHITE),376// tuple("dog", BLACK), and tuple("dog", WHITE):377//378// enum Color { BLACK, GRAY, WHITE };379// class AnimalTest380// : public testing::TestWithParam<std::tuple<const char*, Color> > {...};381//382// TEST_P(AnimalTest, AnimalLooksNice) {...}383//384// INSTANTIATE_TEST_SUITE_P(AnimalVariations, AnimalTest,385// Combine(Values("cat", "dog"),386// Values(BLACK, WHITE)));387//388// This will instantiate tests in FlagDependentTest with all variations of two389// Boolean flags:390//391// class FlagDependentTest392// : public testing::TestWithParam<std::tuple<bool, bool> > {393// virtual void SetUp() {394// // Assigns external_flag_1 and external_flag_2 values from the tuple.395// std::tie(external_flag_1, external_flag_2) = GetParam();396// }397// };398//399// TEST_P(FlagDependentTest, TestFeature1) {400// // Test your code using external_flag_1 and external_flag_2 here.401// }402// INSTANTIATE_TEST_SUITE_P(TwoBoolSequence, FlagDependentTest,403// Combine(Bool(), Bool()));404//405template <typename... Generator>406internal::CartesianProductHolder<Generator...> Combine(const Generator&... g) {407return internal::CartesianProductHolder<Generator...>(g...);408}409410// ConvertGenerator() wraps a parameter generator in order to cast each produced411// value through a known type before supplying it to the test suite412//413// Synopsis:414// ConvertGenerator<T>(gen)415// - returns a generator producing the same elements as generated by gen, but416// each T-typed element is static_cast to a type deduced from the interface417// that accepts this generator, and then returned418//419// It is useful when using the Combine() function to get the generated420// parameters in a custom type instead of std::tuple421//422// Example:423//424// This will instantiate tests in test suite AnimalTest each one with425// the parameter values tuple("cat", BLACK), tuple("cat", WHITE),426// tuple("dog", BLACK), and tuple("dog", WHITE):427//428// enum Color { BLACK, GRAY, WHITE };429// struct ParamType {430// using TupleT = std::tuple<const char*, Color>;431// std::string animal;432// Color color;433// ParamType(TupleT t) : animal(std::get<0>(t)), color(std::get<1>(t)) {}434// };435// class AnimalTest436// : public testing::TestWithParam<ParamType> {...};437//438// TEST_P(AnimalTest, AnimalLooksNice) {...}439//440// INSTANTIATE_TEST_SUITE_P(AnimalVariations, AnimalTest,441// ConvertGenerator<ParamType::TupleT>(442// Combine(Values("cat", "dog"),443// Values(BLACK, WHITE))));444//445template <typename RequestedT>446internal::ParamConverterGenerator<RequestedT> ConvertGenerator(447internal::ParamGenerator<RequestedT> gen) {448return internal::ParamConverterGenerator<RequestedT>(std::move(gen));449}450451// As above, but takes a callable as a second argument. The callable converts452// the generated parameter to the test fixture's parameter type. This allows you453// to use a parameter type that does not have a converting constructor from the454// generated type.455//456// Example:457//458// This will instantiate tests in test suite AnimalTest each one with459// the parameter values tuple("cat", BLACK), tuple("cat", WHITE),460// tuple("dog", BLACK), and tuple("dog", WHITE):461//462// enum Color { BLACK, GRAY, WHITE };463// struct ParamType {464// std::string animal;465// Color color;466// };467// class AnimalTest468// : public testing::TestWithParam<ParamType> {...};469//470// TEST_P(AnimalTest, AnimalLooksNice) {...}471//472// INSTANTIATE_TEST_SUITE_P(473// AnimalVariations, AnimalTest,474// ConvertGenerator(Combine(Values("cat", "dog"), Values(BLACK, WHITE)),475// [](std::tuple<std::string, Color> t) {476// return ParamType{.animal = std::get<0>(t),477// .color = std::get<1>(t)};478// }));479//480template <typename T, int&... ExplicitArgumentBarrier, typename Gen,481typename Func,482typename StdFunction = decltype(std::function(std::declval<Func>()))>483internal::ParamConverterGenerator<T, StdFunction> ConvertGenerator(Gen&& gen,484Func&& f) {485return internal::ParamConverterGenerator<T, StdFunction>(486std::forward<Gen>(gen), std::forward<Func>(f));487}488489// As above, but infers the T from the supplied std::function instead of490// having the caller specify it.491template <int&... ExplicitArgumentBarrier, typename Gen, typename Func,492typename StdFunction = decltype(std::function(std::declval<Func>()))>493auto ConvertGenerator(Gen&& gen, Func&& f) {494constexpr bool is_single_arg_std_function =495internal::IsSingleArgStdFunction<StdFunction>::value;496if constexpr (is_single_arg_std_function) {497return ConvertGenerator<498typename internal::FuncSingleParamType<StdFunction>::type>(499std::forward<Gen>(gen), std::forward<Func>(f));500} else {501static_assert(is_single_arg_std_function,502"The call signature must contain a single argument.");503}504}505506#define TEST_P(test_suite_name, test_name) \507class GTEST_TEST_CLASS_NAME_(test_suite_name, test_name) \508: public test_suite_name, \509private ::testing::internal::GTestNonCopyable { \510public: \511GTEST_TEST_CLASS_NAME_(test_suite_name, test_name)() {} \512void TestBody() override; \513\514private: \515static int AddToRegistry() { \516::testing::UnitTest::GetInstance() \517->parameterized_test_registry() \518.GetTestSuitePatternHolder<test_suite_name>( \519GTEST_STRINGIFY_(test_suite_name), \520::testing::internal::CodeLocation(__FILE__, __LINE__)) \521->AddTestPattern( \522GTEST_STRINGIFY_(test_suite_name), GTEST_STRINGIFY_(test_name), \523new ::testing::internal::TestMetaFactory<GTEST_TEST_CLASS_NAME_( \524test_suite_name, test_name)>(), \525::testing::internal::CodeLocation(__FILE__, __LINE__)); \526return 0; \527} \528[[maybe_unused]] static int gtest_registering_dummy_; \529}; \530int GTEST_TEST_CLASS_NAME_(test_suite_name, \531test_name)::gtest_registering_dummy_ = \532GTEST_TEST_CLASS_NAME_(test_suite_name, test_name)::AddToRegistry(); \533void GTEST_TEST_CLASS_NAME_(test_suite_name, test_name)::TestBody()534535// The last argument to INSTANTIATE_TEST_SUITE_P allows the user to specify536// generator and an optional function or functor that generates custom test name537// suffixes based on the test parameters. Such a function or functor should538// accept one argument of type testing::TestParamInfo<class ParamType>, and539// return std::string.540//541// testing::PrintToStringParamName is a builtin test suffix generator that542// returns the value of testing::PrintToString(GetParam()).543//544// Note: test names must be non-empty, unique, and may only contain ASCII545// alphanumeric characters or underscore. Because PrintToString adds quotes546// to std::string and C strings, it won't work for these types.547548#define GTEST_EXPAND_(arg) arg549#define GTEST_GET_FIRST_(first, ...) first550#define GTEST_GET_SECOND_(first, second, ...) second551552#define INSTANTIATE_TEST_SUITE_P(prefix, test_suite_name, ...) \553static ::testing::internal::ParamGenerator<test_suite_name::ParamType> \554gtest_##prefix##test_suite_name##_EvalGenerator_() { \555return GTEST_EXPAND_(GTEST_GET_FIRST_(__VA_ARGS__, DUMMY_PARAM_)); \556} \557static ::std::string gtest_##prefix##test_suite_name##_EvalGenerateName_( \558const ::testing::TestParamInfo<test_suite_name::ParamType>& info) { \559if (::testing::internal::AlwaysFalse()) { \560::testing::internal::TestNotEmpty(GTEST_EXPAND_(GTEST_GET_SECOND_( \561__VA_ARGS__, \562::testing::internal::DefaultParamName<test_suite_name::ParamType>, \563DUMMY_PARAM_))); \564auto t = std::make_tuple(__VA_ARGS__); \565static_assert(std::tuple_size<decltype(t)>::value <= 2, \566"Too Many Args!"); \567} \568return ((GTEST_EXPAND_(GTEST_GET_SECOND_( \569__VA_ARGS__, \570::testing::internal::DefaultParamName<test_suite_name::ParamType>, \571DUMMY_PARAM_))))(info); \572} \573[[maybe_unused]] static int gtest_##prefix##test_suite_name##_dummy_ = \574::testing::UnitTest::GetInstance() \575->parameterized_test_registry() \576.GetTestSuitePatternHolder<test_suite_name>( \577GTEST_STRINGIFY_(test_suite_name), \578::testing::internal::CodeLocation(__FILE__, __LINE__)) \579->AddTestSuiteInstantiation( \580GTEST_STRINGIFY_(prefix), \581>est_##prefix##test_suite_name##_EvalGenerator_, \582>est_##prefix##test_suite_name##_EvalGenerateName_, __FILE__, \583__LINE__)584585// Allow Marking a Parameterized test class as not needing to be instantiated.586#define GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(T) \587namespace gtest_do_not_use_outside_namespace_scope {} \588static const ::testing::internal::MarkAsIgnored gtest_allow_ignore_##T( \589GTEST_STRINGIFY_(T))590591// Legacy API is deprecated but still available592#ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_593#define INSTANTIATE_TEST_CASE_P \594static_assert(::testing::internal::InstantiateTestCase_P_IsDeprecated(), \595""); \596INSTANTIATE_TEST_SUITE_P597#endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_598599} // namespace testing600601#endif // GOOGLETEST_INCLUDE_GTEST_GTEST_PARAM_TEST_H_602603604