googletest-param-test-test.cc 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119
  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. // Tests for Google Test itself. This file verifies that the parameter
  31. // generators objects produce correct parameter sequences and that
  32. // Google Test runtime instantiates correct tests from those sequences.
  33. #include "gtest/gtest.h"
  34. # include <algorithm>
  35. # include <iostream>
  36. # include <list>
  37. # include <set>
  38. # include <sstream>
  39. # include <string>
  40. # include <vector>
  41. # include "src/gtest-internal-inl.h" // for UnitTestOptions
  42. # include "test/googletest-param-test-test.h"
  43. using ::std::vector;
  44. using ::std::sort;
  45. using ::testing::AddGlobalTestEnvironment;
  46. using ::testing::Bool;
  47. using ::testing::Combine;
  48. using ::testing::Message;
  49. using ::testing::Range;
  50. using ::testing::TestWithParam;
  51. using ::testing::Values;
  52. using ::testing::ValuesIn;
  53. using ::testing::internal::ParamGenerator;
  54. using ::testing::internal::UnitTestOptions;
  55. // Prints a value to a string.
  56. //
  57. // FIXME: remove PrintValue() when we move matchers and
  58. // EXPECT_THAT() from Google Mock to Google Test. At that time, we
  59. // can write EXPECT_THAT(x, Eq(y)) to compare two tuples x and y, as
  60. // EXPECT_THAT() and the matchers know how to print tuples.
  61. template <typename T>
  62. ::std::string PrintValue(const T& value) {
  63. return testing::PrintToString(value);
  64. }
  65. // Verifies that a sequence generated by the generator and accessed
  66. // via the iterator object matches the expected one using Google Test
  67. // assertions.
  68. template <typename T, size_t N>
  69. void VerifyGenerator(const ParamGenerator<T>& generator,
  70. const T (&expected_values)[N]) {
  71. typename ParamGenerator<T>::iterator it = generator.begin();
  72. for (size_t i = 0; i < N; ++i) {
  73. ASSERT_FALSE(it == generator.end())
  74. << "At element " << i << " when accessing via an iterator "
  75. << "created with the copy constructor.\n";
  76. // We cannot use EXPECT_EQ() here as the values may be tuples,
  77. // which don't support <<.
  78. EXPECT_TRUE(expected_values[i] == *it)
  79. << "where i is " << i
  80. << ", expected_values[i] is " << PrintValue(expected_values[i])
  81. << ", *it is " << PrintValue(*it)
  82. << ", and 'it' is an iterator created with the copy constructor.\n";
  83. ++it;
  84. }
  85. EXPECT_TRUE(it == generator.end())
  86. << "At the presumed end of sequence when accessing via an iterator "
  87. << "created with the copy constructor.\n";
  88. // Test the iterator assignment. The following lines verify that
  89. // the sequence accessed via an iterator initialized via the
  90. // assignment operator (as opposed to a copy constructor) matches
  91. // just the same.
  92. it = generator.begin();
  93. for (size_t i = 0; i < N; ++i) {
  94. ASSERT_FALSE(it == generator.end())
  95. << "At element " << i << " when accessing via an iterator "
  96. << "created with the assignment operator.\n";
  97. EXPECT_TRUE(expected_values[i] == *it)
  98. << "where i is " << i
  99. << ", expected_values[i] is " << PrintValue(expected_values[i])
  100. << ", *it is " << PrintValue(*it)
  101. << ", and 'it' is an iterator created with the copy constructor.\n";
  102. ++it;
  103. }
  104. EXPECT_TRUE(it == generator.end())
  105. << "At the presumed end of sequence when accessing via an iterator "
  106. << "created with the assignment operator.\n";
  107. }
  108. template <typename T>
  109. void VerifyGeneratorIsEmpty(const ParamGenerator<T>& generator) {
  110. typename ParamGenerator<T>::iterator it = generator.begin();
  111. EXPECT_TRUE(it == generator.end());
  112. it = generator.begin();
  113. EXPECT_TRUE(it == generator.end());
  114. }
  115. // Generator tests. They test that each of the provided generator functions
  116. // generates an expected sequence of values. The general test pattern
  117. // instantiates a generator using one of the generator functions,
  118. // checks the sequence produced by the generator using its iterator API,
  119. // and then resets the iterator back to the beginning of the sequence
  120. // and checks the sequence again.
  121. // Tests that iterators produced by generator functions conform to the
  122. // ForwardIterator concept.
  123. TEST(IteratorTest, ParamIteratorConformsToForwardIteratorConcept) {
  124. const ParamGenerator<int> gen = Range(0, 10);
  125. ParamGenerator<int>::iterator it = gen.begin();
  126. // Verifies that iterator initialization works as expected.
  127. ParamGenerator<int>::iterator it2 = it;
  128. EXPECT_TRUE(*it == *it2) << "Initialized iterators must point to the "
  129. << "element same as its source points to";
  130. // Verifies that iterator assignment works as expected.
  131. ++it;
  132. EXPECT_FALSE(*it == *it2);
  133. it2 = it;
  134. EXPECT_TRUE(*it == *it2) << "Assigned iterators must point to the "
  135. << "element same as its source points to";
  136. // Verifies that prefix operator++() returns *this.
  137. EXPECT_EQ(&it, &(++it)) << "Result of the prefix operator++ must be "
  138. << "refer to the original object";
  139. // Verifies that the result of the postfix operator++ points to the value
  140. // pointed to by the original iterator.
  141. int original_value = *it; // Have to compute it outside of macro call to be
  142. // unaffected by the parameter evaluation order.
  143. EXPECT_EQ(original_value, *(it++));
  144. // Verifies that prefix and postfix operator++() advance an iterator
  145. // all the same.
  146. it2 = it;
  147. ++it;
  148. ++it2;
  149. EXPECT_TRUE(*it == *it2);
  150. }
  151. // Tests that Range() generates the expected sequence.
  152. TEST(RangeTest, IntRangeWithDefaultStep) {
  153. const ParamGenerator<int> gen = Range(0, 3);
  154. const int expected_values[] = {0, 1, 2};
  155. VerifyGenerator(gen, expected_values);
  156. }
  157. // Edge case. Tests that Range() generates the single element sequence
  158. // as expected when provided with range limits that are equal.
  159. TEST(RangeTest, IntRangeSingleValue) {
  160. const ParamGenerator<int> gen = Range(0, 1);
  161. const int expected_values[] = {0};
  162. VerifyGenerator(gen, expected_values);
  163. }
  164. // Edge case. Tests that Range() with generates empty sequence when
  165. // supplied with an empty range.
  166. TEST(RangeTest, IntRangeEmpty) {
  167. const ParamGenerator<int> gen = Range(0, 0);
  168. VerifyGeneratorIsEmpty(gen);
  169. }
  170. // Tests that Range() with custom step (greater then one) generates
  171. // the expected sequence.
  172. TEST(RangeTest, IntRangeWithCustomStep) {
  173. const ParamGenerator<int> gen = Range(0, 9, 3);
  174. const int expected_values[] = {0, 3, 6};
  175. VerifyGenerator(gen, expected_values);
  176. }
  177. // Tests that Range() with custom step (greater then one) generates
  178. // the expected sequence when the last element does not fall on the
  179. // upper range limit. Sequences generated by Range() must not have
  180. // elements beyond the range limits.
  181. TEST(RangeTest, IntRangeWithCustomStepOverUpperBound) {
  182. const ParamGenerator<int> gen = Range(0, 4, 3);
  183. const int expected_values[] = {0, 3};
  184. VerifyGenerator(gen, expected_values);
  185. }
  186. // Verifies that Range works with user-defined types that define
  187. // copy constructor, operator=(), operator+(), and operator<().
  188. class DogAdder {
  189. public:
  190. explicit DogAdder(const char* a_value) : value_(a_value) {}
  191. DogAdder(const DogAdder& other) : value_(other.value_.c_str()) {}
  192. DogAdder operator=(const DogAdder& other) {
  193. if (this != &other)
  194. value_ = other.value_;
  195. return *this;
  196. }
  197. DogAdder operator+(const DogAdder& other) const {
  198. Message msg;
  199. msg << value_.c_str() << other.value_.c_str();
  200. return DogAdder(msg.GetString().c_str());
  201. }
  202. bool operator<(const DogAdder& other) const {
  203. return value_ < other.value_;
  204. }
  205. const std::string& value() const { return value_; }
  206. private:
  207. std::string value_;
  208. };
  209. TEST(RangeTest, WorksWithACustomType) {
  210. const ParamGenerator<DogAdder> gen =
  211. Range(DogAdder("cat"), DogAdder("catdogdog"), DogAdder("dog"));
  212. ParamGenerator<DogAdder>::iterator it = gen.begin();
  213. ASSERT_FALSE(it == gen.end());
  214. EXPECT_STREQ("cat", it->value().c_str());
  215. ASSERT_FALSE(++it == gen.end());
  216. EXPECT_STREQ("catdog", it->value().c_str());
  217. EXPECT_TRUE(++it == gen.end());
  218. }
  219. class IntWrapper {
  220. public:
  221. explicit IntWrapper(int a_value) : value_(a_value) {}
  222. IntWrapper(const IntWrapper& other) : value_(other.value_) {}
  223. IntWrapper operator=(const IntWrapper& other) {
  224. value_ = other.value_;
  225. return *this;
  226. }
  227. // operator+() adds a different type.
  228. IntWrapper operator+(int other) const { return IntWrapper(value_ + other); }
  229. bool operator<(const IntWrapper& other) const {
  230. return value_ < other.value_;
  231. }
  232. int value() const { return value_; }
  233. private:
  234. int value_;
  235. };
  236. TEST(RangeTest, WorksWithACustomTypeWithDifferentIncrementType) {
  237. const ParamGenerator<IntWrapper> gen = Range(IntWrapper(0), IntWrapper(2));
  238. ParamGenerator<IntWrapper>::iterator it = gen.begin();
  239. ASSERT_FALSE(it == gen.end());
  240. EXPECT_EQ(0, it->value());
  241. ASSERT_FALSE(++it == gen.end());
  242. EXPECT_EQ(1, it->value());
  243. EXPECT_TRUE(++it == gen.end());
  244. }
  245. // Tests that ValuesIn() with an array parameter generates
  246. // the expected sequence.
  247. TEST(ValuesInTest, ValuesInArray) {
  248. int array[] = {3, 5, 8};
  249. const ParamGenerator<int> gen = ValuesIn(array);
  250. VerifyGenerator(gen, array);
  251. }
  252. // Tests that ValuesIn() with a const array parameter generates
  253. // the expected sequence.
  254. TEST(ValuesInTest, ValuesInConstArray) {
  255. const int array[] = {3, 5, 8};
  256. const ParamGenerator<int> gen = ValuesIn(array);
  257. VerifyGenerator(gen, array);
  258. }
  259. // Edge case. Tests that ValuesIn() with an array parameter containing a
  260. // single element generates the single element sequence.
  261. TEST(ValuesInTest, ValuesInSingleElementArray) {
  262. int array[] = {42};
  263. const ParamGenerator<int> gen = ValuesIn(array);
  264. VerifyGenerator(gen, array);
  265. }
  266. // Tests that ValuesIn() generates the expected sequence for an STL
  267. // container (vector).
  268. TEST(ValuesInTest, ValuesInVector) {
  269. typedef ::std::vector<int> ContainerType;
  270. ContainerType values;
  271. values.push_back(3);
  272. values.push_back(5);
  273. values.push_back(8);
  274. const ParamGenerator<int> gen = ValuesIn(values);
  275. const int expected_values[] = {3, 5, 8};
  276. VerifyGenerator(gen, expected_values);
  277. }
  278. // Tests that ValuesIn() generates the expected sequence.
  279. TEST(ValuesInTest, ValuesInIteratorRange) {
  280. typedef ::std::vector<int> ContainerType;
  281. ContainerType values;
  282. values.push_back(3);
  283. values.push_back(5);
  284. values.push_back(8);
  285. const ParamGenerator<int> gen = ValuesIn(values.begin(), values.end());
  286. const int expected_values[] = {3, 5, 8};
  287. VerifyGenerator(gen, expected_values);
  288. }
  289. // Edge case. Tests that ValuesIn() provided with an iterator range specifying a
  290. // single value generates a single-element sequence.
  291. TEST(ValuesInTest, ValuesInSingleElementIteratorRange) {
  292. typedef ::std::vector<int> ContainerType;
  293. ContainerType values;
  294. values.push_back(42);
  295. const ParamGenerator<int> gen = ValuesIn(values.begin(), values.end());
  296. const int expected_values[] = {42};
  297. VerifyGenerator(gen, expected_values);
  298. }
  299. // Edge case. Tests that ValuesIn() provided with an empty iterator range
  300. // generates an empty sequence.
  301. TEST(ValuesInTest, ValuesInEmptyIteratorRange) {
  302. typedef ::std::vector<int> ContainerType;
  303. ContainerType values;
  304. const ParamGenerator<int> gen = ValuesIn(values.begin(), values.end());
  305. VerifyGeneratorIsEmpty(gen);
  306. }
  307. // Tests that the Values() generates the expected sequence.
  308. TEST(ValuesTest, ValuesWorks) {
  309. const ParamGenerator<int> gen = Values(3, 5, 8);
  310. const int expected_values[] = {3, 5, 8};
  311. VerifyGenerator(gen, expected_values);
  312. }
  313. // Tests that Values() generates the expected sequences from elements of
  314. // different types convertible to ParamGenerator's parameter type.
  315. TEST(ValuesTest, ValuesWorksForValuesOfCompatibleTypes) {
  316. const ParamGenerator<double> gen = Values(3, 5.0f, 8.0);
  317. const double expected_values[] = {3.0, 5.0, 8.0};
  318. VerifyGenerator(gen, expected_values);
  319. }
  320. TEST(ValuesTest, ValuesWorksForMaxLengthList) {
  321. const ParamGenerator<int> gen = Values(
  322. 10, 20, 30, 40, 50, 60, 70, 80, 90, 100,
  323. 110, 120, 130, 140, 150, 160, 170, 180, 190, 200,
  324. 210, 220, 230, 240, 250, 260, 270, 280, 290, 300,
  325. 310, 320, 330, 340, 350, 360, 370, 380, 390, 400,
  326. 410, 420, 430, 440, 450, 460, 470, 480, 490, 500);
  327. const int expected_values[] = {
  328. 10, 20, 30, 40, 50, 60, 70, 80, 90, 100,
  329. 110, 120, 130, 140, 150, 160, 170, 180, 190, 200,
  330. 210, 220, 230, 240, 250, 260, 270, 280, 290, 300,
  331. 310, 320, 330, 340, 350, 360, 370, 380, 390, 400,
  332. 410, 420, 430, 440, 450, 460, 470, 480, 490, 500};
  333. VerifyGenerator(gen, expected_values);
  334. }
  335. // Edge case test. Tests that single-parameter Values() generates the sequence
  336. // with the single value.
  337. TEST(ValuesTest, ValuesWithSingleParameter) {
  338. const ParamGenerator<int> gen = Values(42);
  339. const int expected_values[] = {42};
  340. VerifyGenerator(gen, expected_values);
  341. }
  342. // Tests that Bool() generates sequence (false, true).
  343. TEST(BoolTest, BoolWorks) {
  344. const ParamGenerator<bool> gen = Bool();
  345. const bool expected_values[] = {false, true};
  346. VerifyGenerator(gen, expected_values);
  347. }
  348. // Tests that Combine() with two parameters generates the expected sequence.
  349. TEST(CombineTest, CombineWithTwoParameters) {
  350. const char* foo = "foo";
  351. const char* bar = "bar";
  352. const ParamGenerator<std::tuple<const char*, int> > gen =
  353. Combine(Values(foo, bar), Values(3, 4));
  354. std::tuple<const char*, int> expected_values[] = {
  355. std::make_tuple(foo, 3), std::make_tuple(foo, 4), std::make_tuple(bar, 3),
  356. std::make_tuple(bar, 4)};
  357. VerifyGenerator(gen, expected_values);
  358. }
  359. // Tests that Combine() with three parameters generates the expected sequence.
  360. TEST(CombineTest, CombineWithThreeParameters) {
  361. const ParamGenerator<std::tuple<int, int, int> > gen =
  362. Combine(Values(0, 1), Values(3, 4), Values(5, 6));
  363. std::tuple<int, int, int> expected_values[] = {
  364. std::make_tuple(0, 3, 5), std::make_tuple(0, 3, 6),
  365. std::make_tuple(0, 4, 5), std::make_tuple(0, 4, 6),
  366. std::make_tuple(1, 3, 5), std::make_tuple(1, 3, 6),
  367. std::make_tuple(1, 4, 5), std::make_tuple(1, 4, 6)};
  368. VerifyGenerator(gen, expected_values);
  369. }
  370. // Tests that the Combine() with the first parameter generating a single value
  371. // sequence generates a sequence with the number of elements equal to the
  372. // number of elements in the sequence generated by the second parameter.
  373. TEST(CombineTest, CombineWithFirstParameterSingleValue) {
  374. const ParamGenerator<std::tuple<int, int> > gen =
  375. Combine(Values(42), Values(0, 1));
  376. std::tuple<int, int> expected_values[] = {std::make_tuple(42, 0),
  377. std::make_tuple(42, 1)};
  378. VerifyGenerator(gen, expected_values);
  379. }
  380. // Tests that the Combine() with the second parameter generating a single value
  381. // sequence generates a sequence with the number of elements equal to the
  382. // number of elements in the sequence generated by the first parameter.
  383. TEST(CombineTest, CombineWithSecondParameterSingleValue) {
  384. const ParamGenerator<std::tuple<int, int> > gen =
  385. Combine(Values(0, 1), Values(42));
  386. std::tuple<int, int> expected_values[] = {std::make_tuple(0, 42),
  387. std::make_tuple(1, 42)};
  388. VerifyGenerator(gen, expected_values);
  389. }
  390. // Tests that when the first parameter produces an empty sequence,
  391. // Combine() produces an empty sequence, too.
  392. TEST(CombineTest, CombineWithFirstParameterEmptyRange) {
  393. const ParamGenerator<std::tuple<int, int> > gen =
  394. Combine(Range(0, 0), Values(0, 1));
  395. VerifyGeneratorIsEmpty(gen);
  396. }
  397. // Tests that when the second parameter produces an empty sequence,
  398. // Combine() produces an empty sequence, too.
  399. TEST(CombineTest, CombineWithSecondParameterEmptyRange) {
  400. const ParamGenerator<std::tuple<int, int> > gen =
  401. Combine(Values(0, 1), Range(1, 1));
  402. VerifyGeneratorIsEmpty(gen);
  403. }
  404. // Edge case. Tests that combine works with the maximum number
  405. // of parameters supported by Google Test (currently 10).
  406. TEST(CombineTest, CombineWithMaxNumberOfParameters) {
  407. const char* foo = "foo";
  408. const char* bar = "bar";
  409. const ParamGenerator<
  410. std::tuple<const char*, int, int, int, int, int, int, int, int, int> >
  411. gen =
  412. Combine(Values(foo, bar), Values(1), Values(2), Values(3), Values(4),
  413. Values(5), Values(6), Values(7), Values(8), Values(9));
  414. std::tuple<const char*, int, int, int, int, int, int, int, int, int>
  415. expected_values[] = {std::make_tuple(foo, 1, 2, 3, 4, 5, 6, 7, 8, 9),
  416. std::make_tuple(bar, 1, 2, 3, 4, 5, 6, 7, 8, 9)};
  417. VerifyGenerator(gen, expected_values);
  418. }
  419. class NonDefaultConstructAssignString {
  420. public:
  421. NonDefaultConstructAssignString(const std::string& s) : str_(s) {}
  422. NonDefaultConstructAssignString() = delete;
  423. NonDefaultConstructAssignString(const NonDefaultConstructAssignString&) =
  424. default;
  425. NonDefaultConstructAssignString& operator=(
  426. const NonDefaultConstructAssignString&) = delete;
  427. ~NonDefaultConstructAssignString() = default;
  428. const std::string& str() const { return str_; }
  429. private:
  430. std::string str_;
  431. };
  432. TEST(CombineTest, NonDefaultConstructAssign) {
  433. const ParamGenerator<std::tuple<int, NonDefaultConstructAssignString> > gen =
  434. Combine(Values(0, 1), Values(NonDefaultConstructAssignString("A"),
  435. NonDefaultConstructAssignString("B")));
  436. ParamGenerator<std::tuple<int, NonDefaultConstructAssignString> >::iterator
  437. it = gen.begin();
  438. EXPECT_EQ(0, std::get<0>(*it));
  439. EXPECT_EQ("A", std::get<1>(*it).str());
  440. ++it;
  441. EXPECT_EQ(0, std::get<0>(*it));
  442. EXPECT_EQ("B", std::get<1>(*it).str());
  443. ++it;
  444. EXPECT_EQ(1, std::get<0>(*it));
  445. EXPECT_EQ("A", std::get<1>(*it).str());
  446. ++it;
  447. EXPECT_EQ(1, std::get<0>(*it));
  448. EXPECT_EQ("B", std::get<1>(*it).str());
  449. ++it;
  450. EXPECT_TRUE(it == gen.end());
  451. }
  452. // Tests that an generator produces correct sequence after being
  453. // assigned from another generator.
  454. TEST(ParamGeneratorTest, AssignmentWorks) {
  455. ParamGenerator<int> gen = Values(1, 2);
  456. const ParamGenerator<int> gen2 = Values(3, 4);
  457. gen = gen2;
  458. const int expected_values[] = {3, 4};
  459. VerifyGenerator(gen, expected_values);
  460. }
  461. // This test verifies that the tests are expanded and run as specified:
  462. // one test per element from the sequence produced by the generator
  463. // specified in INSTANTIATE_TEST_SUITE_P. It also verifies that the test's
  464. // fixture constructor, SetUp(), and TearDown() have run and have been
  465. // supplied with the correct parameters.
  466. // The use of environment object allows detection of the case where no test
  467. // case functionality is run at all. In this case TearDownTestSuite will not
  468. // be able to detect missing tests, naturally.
  469. template <int kExpectedCalls>
  470. class TestGenerationEnvironment : public ::testing::Environment {
  471. public:
  472. static TestGenerationEnvironment* Instance() {
  473. static TestGenerationEnvironment* instance = new TestGenerationEnvironment;
  474. return instance;
  475. }
  476. void FixtureConstructorExecuted() { fixture_constructor_count_++; }
  477. void SetUpExecuted() { set_up_count_++; }
  478. void TearDownExecuted() { tear_down_count_++; }
  479. void TestBodyExecuted() { test_body_count_++; }
  480. void TearDown() override {
  481. // If all MultipleTestGenerationTest tests have been de-selected
  482. // by the filter flag, the following checks make no sense.
  483. bool perform_check = false;
  484. for (int i = 0; i < kExpectedCalls; ++i) {
  485. Message msg;
  486. msg << "TestsExpandedAndRun/" << i;
  487. if (UnitTestOptions::FilterMatchesTest(
  488. "TestExpansionModule/MultipleTestGenerationTest",
  489. msg.GetString().c_str())) {
  490. perform_check = true;
  491. }
  492. }
  493. if (perform_check) {
  494. EXPECT_EQ(kExpectedCalls, fixture_constructor_count_)
  495. << "Fixture constructor of ParamTestGenerationTest test case "
  496. << "has not been run as expected.";
  497. EXPECT_EQ(kExpectedCalls, set_up_count_)
  498. << "Fixture SetUp method of ParamTestGenerationTest test case "
  499. << "has not been run as expected.";
  500. EXPECT_EQ(kExpectedCalls, tear_down_count_)
  501. << "Fixture TearDown method of ParamTestGenerationTest test case "
  502. << "has not been run as expected.";
  503. EXPECT_EQ(kExpectedCalls, test_body_count_)
  504. << "Test in ParamTestGenerationTest test case "
  505. << "has not been run as expected.";
  506. }
  507. }
  508. private:
  509. TestGenerationEnvironment() : fixture_constructor_count_(0), set_up_count_(0),
  510. tear_down_count_(0), test_body_count_(0) {}
  511. int fixture_constructor_count_;
  512. int set_up_count_;
  513. int tear_down_count_;
  514. int test_body_count_;
  515. GTEST_DISALLOW_COPY_AND_ASSIGN_(TestGenerationEnvironment);
  516. };
  517. const int test_generation_params[] = {36, 42, 72};
  518. class TestGenerationTest : public TestWithParam<int> {
  519. public:
  520. enum {
  521. PARAMETER_COUNT =
  522. sizeof(test_generation_params)/sizeof(test_generation_params[0])
  523. };
  524. typedef TestGenerationEnvironment<PARAMETER_COUNT> Environment;
  525. TestGenerationTest() {
  526. Environment::Instance()->FixtureConstructorExecuted();
  527. current_parameter_ = GetParam();
  528. }
  529. void SetUp() override {
  530. Environment::Instance()->SetUpExecuted();
  531. EXPECT_EQ(current_parameter_, GetParam());
  532. }
  533. void TearDown() override {
  534. Environment::Instance()->TearDownExecuted();
  535. EXPECT_EQ(current_parameter_, GetParam());
  536. }
  537. static void SetUpTestSuite() {
  538. bool all_tests_in_test_case_selected = true;
  539. for (int i = 0; i < PARAMETER_COUNT; ++i) {
  540. Message test_name;
  541. test_name << "TestsExpandedAndRun/" << i;
  542. if ( !UnitTestOptions::FilterMatchesTest(
  543. "TestExpansionModule/MultipleTestGenerationTest",
  544. test_name.GetString())) {
  545. all_tests_in_test_case_selected = false;
  546. }
  547. }
  548. EXPECT_TRUE(all_tests_in_test_case_selected)
  549. << "When running the TestGenerationTest test case all of its tests\n"
  550. << "must be selected by the filter flag for the test case to pass.\n"
  551. << "If not all of them are enabled, we can't reliably conclude\n"
  552. << "that the correct number of tests have been generated.";
  553. collected_parameters_.clear();
  554. }
  555. static void TearDownTestSuite() {
  556. vector<int> expected_values(test_generation_params,
  557. test_generation_params + PARAMETER_COUNT);
  558. // Test execution order is not guaranteed by Google Test,
  559. // so the order of values in collected_parameters_ can be
  560. // different and we have to sort to compare.
  561. sort(expected_values.begin(), expected_values.end());
  562. sort(collected_parameters_.begin(), collected_parameters_.end());
  563. EXPECT_TRUE(collected_parameters_ == expected_values);
  564. }
  565. protected:
  566. int current_parameter_;
  567. static vector<int> collected_parameters_;
  568. private:
  569. GTEST_DISALLOW_COPY_AND_ASSIGN_(TestGenerationTest);
  570. };
  571. vector<int> TestGenerationTest::collected_parameters_;
  572. TEST_P(TestGenerationTest, TestsExpandedAndRun) {
  573. Environment::Instance()->TestBodyExecuted();
  574. EXPECT_EQ(current_parameter_, GetParam());
  575. collected_parameters_.push_back(GetParam());
  576. }
  577. INSTANTIATE_TEST_SUITE_P(TestExpansionModule, TestGenerationTest,
  578. ValuesIn(test_generation_params));
  579. // This test verifies that the element sequence (third parameter of
  580. // INSTANTIATE_TEST_SUITE_P) is evaluated in InitGoogleTest() and neither at
  581. // the call site of INSTANTIATE_TEST_SUITE_P nor in RUN_ALL_TESTS(). For
  582. // that, we declare param_value_ to be a static member of
  583. // GeneratorEvaluationTest and initialize it to 0. We set it to 1 in
  584. // main(), just before invocation of InitGoogleTest(). After calling
  585. // InitGoogleTest(), we set the value to 2. If the sequence is evaluated
  586. // before or after InitGoogleTest, INSTANTIATE_TEST_SUITE_P will create a
  587. // test with parameter other than 1, and the test body will fail the
  588. // assertion.
  589. class GeneratorEvaluationTest : public TestWithParam<int> {
  590. public:
  591. static int param_value() { return param_value_; }
  592. static void set_param_value(int param_value) { param_value_ = param_value; }
  593. private:
  594. static int param_value_;
  595. };
  596. int GeneratorEvaluationTest::param_value_ = 0;
  597. TEST_P(GeneratorEvaluationTest, GeneratorsEvaluatedInMain) {
  598. EXPECT_EQ(1, GetParam());
  599. }
  600. INSTANTIATE_TEST_SUITE_P(GenEvalModule, GeneratorEvaluationTest,
  601. Values(GeneratorEvaluationTest::param_value()));
  602. // Tests that generators defined in a different translation unit are
  603. // functional. Generator extern_gen is defined in gtest-param-test_test2.cc.
  604. extern ParamGenerator<int> extern_gen;
  605. class ExternalGeneratorTest : public TestWithParam<int> {};
  606. TEST_P(ExternalGeneratorTest, ExternalGenerator) {
  607. // Sequence produced by extern_gen contains only a single value
  608. // which we verify here.
  609. EXPECT_EQ(GetParam(), 33);
  610. }
  611. INSTANTIATE_TEST_SUITE_P(ExternalGeneratorModule, ExternalGeneratorTest,
  612. extern_gen);
  613. // Tests that a parameterized test case can be defined in one translation
  614. // unit and instantiated in another. This test will be instantiated in
  615. // gtest-param-test_test2.cc. ExternalInstantiationTest fixture class is
  616. // defined in gtest-param-test_test.h.
  617. TEST_P(ExternalInstantiationTest, IsMultipleOf33) {
  618. EXPECT_EQ(0, GetParam() % 33);
  619. }
  620. // Tests that a parameterized test case can be instantiated with multiple
  621. // generators.
  622. class MultipleInstantiationTest : public TestWithParam<int> {};
  623. TEST_P(MultipleInstantiationTest, AllowsMultipleInstances) {
  624. }
  625. INSTANTIATE_TEST_SUITE_P(Sequence1, MultipleInstantiationTest, Values(1, 2));
  626. INSTANTIATE_TEST_SUITE_P(Sequence2, MultipleInstantiationTest, Range(3, 5));
  627. // Tests that a parameterized test case can be instantiated
  628. // in multiple translation units. This test will be instantiated
  629. // here and in gtest-param-test_test2.cc.
  630. // InstantiationInMultipleTranslationUnitsTest fixture class
  631. // is defined in gtest-param-test_test.h.
  632. TEST_P(InstantiationInMultipleTranslationUnitsTest, IsMultipleOf42) {
  633. EXPECT_EQ(0, GetParam() % 42);
  634. }
  635. INSTANTIATE_TEST_SUITE_P(Sequence1, InstantiationInMultipleTranslationUnitsTest,
  636. Values(42, 42 * 2));
  637. // Tests that each iteration of parameterized test runs in a separate test
  638. // object.
  639. class SeparateInstanceTest : public TestWithParam<int> {
  640. public:
  641. SeparateInstanceTest() : count_(0) {}
  642. static void TearDownTestSuite() {
  643. EXPECT_GE(global_count_, 2)
  644. << "If some (but not all) SeparateInstanceTest tests have been "
  645. << "filtered out this test will fail. Make sure that all "
  646. << "GeneratorEvaluationTest are selected or de-selected together "
  647. << "by the test filter.";
  648. }
  649. protected:
  650. int count_;
  651. static int global_count_;
  652. };
  653. int SeparateInstanceTest::global_count_ = 0;
  654. TEST_P(SeparateInstanceTest, TestsRunInSeparateInstances) {
  655. EXPECT_EQ(0, count_++);
  656. global_count_++;
  657. }
  658. INSTANTIATE_TEST_SUITE_P(FourElemSequence, SeparateInstanceTest, Range(1, 4));
  659. // Tests that all instantiations of a test have named appropriately. Test
  660. // defined with TEST_P(TestSuiteName, TestName) and instantiated with
  661. // INSTANTIATE_TEST_SUITE_P(SequenceName, TestSuiteName, generator) must be
  662. // named SequenceName/TestSuiteName.TestName/i, where i is the 0-based index of
  663. // the sequence element used to instantiate the test.
  664. class NamingTest : public TestWithParam<int> {};
  665. TEST_P(NamingTest, TestsReportCorrectNamesAndParameters) {
  666. const ::testing::TestInfo* const test_info =
  667. ::testing::UnitTest::GetInstance()->current_test_info();
  668. EXPECT_STREQ("ZeroToFiveSequence/NamingTest", test_info->test_suite_name());
  669. Message index_stream;
  670. index_stream << "TestsReportCorrectNamesAndParameters/" << GetParam();
  671. EXPECT_STREQ(index_stream.GetString().c_str(), test_info->name());
  672. EXPECT_EQ(::testing::PrintToString(GetParam()), test_info->value_param());
  673. }
  674. INSTANTIATE_TEST_SUITE_P(ZeroToFiveSequence, NamingTest, Range(0, 5));
  675. // Tests that macros in test names are expanded correctly.
  676. class MacroNamingTest : public TestWithParam<int> {};
  677. #define PREFIX_WITH_FOO(test_name) Foo##test_name
  678. #define PREFIX_WITH_MACRO(test_name) Macro##test_name
  679. TEST_P(PREFIX_WITH_MACRO(NamingTest), PREFIX_WITH_FOO(SomeTestName)) {
  680. const ::testing::TestInfo* const test_info =
  681. ::testing::UnitTest::GetInstance()->current_test_info();
  682. EXPECT_STREQ("FortyTwo/MacroNamingTest", test_info->test_suite_name());
  683. EXPECT_STREQ("FooSomeTestName/0", test_info->name());
  684. }
  685. INSTANTIATE_TEST_SUITE_P(FortyTwo, MacroNamingTest, Values(42));
  686. // Tests the same thing for non-parametrized tests.
  687. class MacroNamingTestNonParametrized : public ::testing::Test {};
  688. TEST_F(PREFIX_WITH_MACRO(NamingTestNonParametrized),
  689. PREFIX_WITH_FOO(SomeTestName)) {
  690. const ::testing::TestInfo* const test_info =
  691. ::testing::UnitTest::GetInstance()->current_test_info();
  692. EXPECT_STREQ("MacroNamingTestNonParametrized", test_info->test_suite_name());
  693. EXPECT_STREQ("FooSomeTestName", test_info->name());
  694. }
  695. TEST(MacroNameing, LookupNames) {
  696. std::set<std::string> know_suite_names, know_test_names;
  697. auto ins = testing::UnitTest::GetInstance();
  698. int ts = 0;
  699. while (const testing::TestSuite* suite = ins->GetTestSuite(ts++)) {
  700. know_suite_names.insert(suite->name());
  701. int ti = 0;
  702. while (const testing::TestInfo* info = suite->GetTestInfo(ti++)) {
  703. know_test_names.insert(std::string(suite->name()) + "." + info->name());
  704. }
  705. }
  706. // Check that the expected form of the test suit name actually exists.
  707. EXPECT_NE( //
  708. know_suite_names.find("FortyTwo/MacroNamingTest"),
  709. know_suite_names.end());
  710. EXPECT_NE(
  711. know_suite_names.find("MacroNamingTestNonParametrized"),
  712. know_suite_names.end());
  713. // Check that the expected form of the test name actually exists.
  714. EXPECT_NE( //
  715. know_test_names.find("FortyTwo/MacroNamingTest.FooSomeTestName/0"),
  716. know_test_names.end());
  717. EXPECT_NE(
  718. know_test_names.find("MacroNamingTestNonParametrized.FooSomeTestName"),
  719. know_test_names.end());
  720. }
  721. // Tests that user supplied custom parameter names are working correctly.
  722. // Runs the test with a builtin helper method which uses PrintToString,
  723. // as well as a custom function and custom functor to ensure all possible
  724. // uses work correctly.
  725. class CustomFunctorNamingTest : public TestWithParam<std::string> {};
  726. TEST_P(CustomFunctorNamingTest, CustomTestNames) {}
  727. struct CustomParamNameFunctor {
  728. std::string operator()(const ::testing::TestParamInfo<std::string>& inf) {
  729. return inf.param;
  730. }
  731. };
  732. INSTANTIATE_TEST_SUITE_P(CustomParamNameFunctor, CustomFunctorNamingTest,
  733. Values(std::string("FunctorName")),
  734. CustomParamNameFunctor());
  735. INSTANTIATE_TEST_SUITE_P(AllAllowedCharacters, CustomFunctorNamingTest,
  736. Values("abcdefghijklmnopqrstuvwxyz",
  737. "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "01234567890_"),
  738. CustomParamNameFunctor());
  739. inline std::string CustomParamNameFunction(
  740. const ::testing::TestParamInfo<std::string>& inf) {
  741. return inf.param;
  742. }
  743. class CustomFunctionNamingTest : public TestWithParam<std::string> {};
  744. TEST_P(CustomFunctionNamingTest, CustomTestNames) {}
  745. INSTANTIATE_TEST_SUITE_P(CustomParamNameFunction, CustomFunctionNamingTest,
  746. Values(std::string("FunctionName")),
  747. CustomParamNameFunction);
  748. INSTANTIATE_TEST_SUITE_P(CustomParamNameFunctionP, CustomFunctionNamingTest,
  749. Values(std::string("FunctionNameP")),
  750. &CustomParamNameFunction);
  751. // Test custom naming with a lambda
  752. class CustomLambdaNamingTest : public TestWithParam<std::string> {};
  753. TEST_P(CustomLambdaNamingTest, CustomTestNames) {}
  754. INSTANTIATE_TEST_SUITE_P(CustomParamNameLambda, CustomLambdaNamingTest,
  755. Values(std::string("LambdaName")),
  756. [](const ::testing::TestParamInfo<std::string>& inf) {
  757. return inf.param;
  758. });
  759. TEST(CustomNamingTest, CheckNameRegistry) {
  760. ::testing::UnitTest* unit_test = ::testing::UnitTest::GetInstance();
  761. std::set<std::string> test_names;
  762. for (int suite_num = 0; suite_num < unit_test->total_test_suite_count();
  763. ++suite_num) {
  764. const ::testing::TestSuite* test_suite = unit_test->GetTestSuite(suite_num);
  765. for (int test_num = 0; test_num < test_suite->total_test_count();
  766. ++test_num) {
  767. const ::testing::TestInfo* test_info = test_suite->GetTestInfo(test_num);
  768. test_names.insert(std::string(test_info->name()));
  769. }
  770. }
  771. EXPECT_EQ(1u, test_names.count("CustomTestNames/FunctorName"));
  772. EXPECT_EQ(1u, test_names.count("CustomTestNames/FunctionName"));
  773. EXPECT_EQ(1u, test_names.count("CustomTestNames/FunctionNameP"));
  774. EXPECT_EQ(1u, test_names.count("CustomTestNames/LambdaName"));
  775. }
  776. // Test a numeric name to ensure PrintToStringParamName works correctly.
  777. class CustomIntegerNamingTest : public TestWithParam<int> {};
  778. TEST_P(CustomIntegerNamingTest, TestsReportCorrectNames) {
  779. const ::testing::TestInfo* const test_info =
  780. ::testing::UnitTest::GetInstance()->current_test_info();
  781. Message test_name_stream;
  782. test_name_stream << "TestsReportCorrectNames/" << GetParam();
  783. EXPECT_STREQ(test_name_stream.GetString().c_str(), test_info->name());
  784. }
  785. INSTANTIATE_TEST_SUITE_P(PrintToString, CustomIntegerNamingTest, Range(0, 5),
  786. ::testing::PrintToStringParamName());
  787. // Test a custom struct with PrintToString.
  788. struct CustomStruct {
  789. explicit CustomStruct(int value) : x(value) {}
  790. int x;
  791. };
  792. std::ostream& operator<<(std::ostream& stream, const CustomStruct& val) {
  793. stream << val.x;
  794. return stream;
  795. }
  796. class CustomStructNamingTest : public TestWithParam<CustomStruct> {};
  797. TEST_P(CustomStructNamingTest, TestsReportCorrectNames) {
  798. const ::testing::TestInfo* const test_info =
  799. ::testing::UnitTest::GetInstance()->current_test_info();
  800. Message test_name_stream;
  801. test_name_stream << "TestsReportCorrectNames/" << GetParam();
  802. EXPECT_STREQ(test_name_stream.GetString().c_str(), test_info->name());
  803. }
  804. INSTANTIATE_TEST_SUITE_P(PrintToString, CustomStructNamingTest,
  805. Values(CustomStruct(0), CustomStruct(1)),
  806. ::testing::PrintToStringParamName());
  807. // Test that using a stateful parameter naming function works as expected.
  808. struct StatefulNamingFunctor {
  809. StatefulNamingFunctor() : sum(0) {}
  810. std::string operator()(const ::testing::TestParamInfo<int>& info) {
  811. int value = info.param + sum;
  812. sum += info.param;
  813. return ::testing::PrintToString(value);
  814. }
  815. int sum;
  816. };
  817. class StatefulNamingTest : public ::testing::TestWithParam<int> {
  818. protected:
  819. StatefulNamingTest() : sum_(0) {}
  820. int sum_;
  821. };
  822. TEST_P(StatefulNamingTest, TestsReportCorrectNames) {
  823. const ::testing::TestInfo* const test_info =
  824. ::testing::UnitTest::GetInstance()->current_test_info();
  825. sum_ += GetParam();
  826. Message test_name_stream;
  827. test_name_stream << "TestsReportCorrectNames/" << sum_;
  828. EXPECT_STREQ(test_name_stream.GetString().c_str(), test_info->name());
  829. }
  830. INSTANTIATE_TEST_SUITE_P(StatefulNamingFunctor, StatefulNamingTest, Range(0, 5),
  831. StatefulNamingFunctor());
  832. // Class that cannot be streamed into an ostream. It needs to be copyable
  833. // (and, in case of MSVC, also assignable) in order to be a test parameter
  834. // type. Its default copy constructor and assignment operator do exactly
  835. // what we need.
  836. class Unstreamable {
  837. public:
  838. explicit Unstreamable(int value) : value_(value) {}
  839. // -Wunused-private-field: dummy accessor for `value_`.
  840. const int& dummy_value() const { return value_; }
  841. private:
  842. int value_;
  843. };
  844. class CommentTest : public TestWithParam<Unstreamable> {};
  845. TEST_P(CommentTest, TestsCorrectlyReportUnstreamableParams) {
  846. const ::testing::TestInfo* const test_info =
  847. ::testing::UnitTest::GetInstance()->current_test_info();
  848. EXPECT_EQ(::testing::PrintToString(GetParam()), test_info->value_param());
  849. }
  850. INSTANTIATE_TEST_SUITE_P(InstantiationWithComments, CommentTest,
  851. Values(Unstreamable(1)));
  852. // Verify that we can create a hierarchy of test fixtures, where the base
  853. // class fixture is not parameterized and the derived class is. In this case
  854. // ParameterizedDerivedTest inherits from NonParameterizedBaseTest. We
  855. // perform simple tests on both.
  856. class NonParameterizedBaseTest : public ::testing::Test {
  857. public:
  858. NonParameterizedBaseTest() : n_(17) { }
  859. protected:
  860. int n_;
  861. };
  862. class ParameterizedDerivedTest : public NonParameterizedBaseTest,
  863. public ::testing::WithParamInterface<int> {
  864. protected:
  865. ParameterizedDerivedTest() : count_(0) { }
  866. int count_;
  867. static int global_count_;
  868. };
  869. int ParameterizedDerivedTest::global_count_ = 0;
  870. TEST_F(NonParameterizedBaseTest, FixtureIsInitialized) {
  871. EXPECT_EQ(17, n_);
  872. }
  873. TEST_P(ParameterizedDerivedTest, SeesSequence) {
  874. EXPECT_EQ(17, n_);
  875. EXPECT_EQ(0, count_++);
  876. EXPECT_EQ(GetParam(), global_count_++);
  877. }
  878. class ParameterizedDeathTest : public ::testing::TestWithParam<int> { };
  879. TEST_F(ParameterizedDeathTest, GetParamDiesFromTestF) {
  880. EXPECT_DEATH_IF_SUPPORTED(GetParam(),
  881. ".* value-parameterized test .*");
  882. }
  883. INSTANTIATE_TEST_SUITE_P(RangeZeroToFive, ParameterizedDerivedTest,
  884. Range(0, 5));
  885. // Tests param generator working with Enums
  886. enum MyEnums {
  887. ENUM1 = 1,
  888. ENUM2 = 3,
  889. ENUM3 = 8,
  890. };
  891. class MyEnumTest : public testing::TestWithParam<MyEnums> {};
  892. TEST_P(MyEnumTest, ChecksParamMoreThanZero) { EXPECT_GE(10, GetParam()); }
  893. INSTANTIATE_TEST_SUITE_P(MyEnumTests, MyEnumTest,
  894. ::testing::Values(ENUM1, ENUM2, 0));
  895. namespace works_here {
  896. // Never used not instantiated, this should work.
  897. class NotUsedTest : public testing::TestWithParam<int> {};
  898. ///////
  899. // Never used not instantiated, this should work.
  900. template <typename T>
  901. class NotUsedTypeTest : public testing::Test {};
  902. TYPED_TEST_SUITE_P(NotUsedTypeTest);
  903. // Used but not instantiated, this would fail. but...
  904. class NotInstantiatedTest : public testing::TestWithParam<int> {};
  905. // ... we mark is as allowed.
  906. GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(NotInstantiatedTest);
  907. TEST_P(NotInstantiatedTest, Used) { }
  908. using OtherName = NotInstantiatedTest;
  909. GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(OtherName);
  910. TEST_P(OtherName, Used) { }
  911. // Used but not instantiated, this would fail. but...
  912. template <typename T>
  913. class NotInstantiatedTypeTest : public testing::Test {};
  914. TYPED_TEST_SUITE_P(NotInstantiatedTypeTest);
  915. // ... we mark is as allowed.
  916. GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(NotInstantiatedTypeTest);
  917. TYPED_TEST_P(NotInstantiatedTypeTest, Used) { }
  918. REGISTER_TYPED_TEST_SUITE_P(NotInstantiatedTypeTest, Used);
  919. } // namespace works_here
  920. int main(int argc, char **argv) {
  921. // Used in TestGenerationTest test suite.
  922. AddGlobalTestEnvironment(TestGenerationTest::Environment::Instance());
  923. // Used in GeneratorEvaluationTest test suite. Tests that the updated value
  924. // will be picked up for instantiating tests in GeneratorEvaluationTest.
  925. GeneratorEvaluationTest::set_param_value(1);
  926. ::testing::InitGoogleTest(&argc, argv);
  927. // Used in GeneratorEvaluationTest test suite. Tests that value updated
  928. // here will NOT be used for instantiating tests in
  929. // GeneratorEvaluationTest.
  930. GeneratorEvaluationTest::set_param_value(2);
  931. return RUN_ALL_TESTS();
  932. }