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