1 // Copyright 2008, Google Inc.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
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
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.
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.
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.
35 #include "test/googletest-param-test-test.h"
46 #include "gtest/gtest.h"
47 #include "src/gtest-internal-inl.h" // for UnitTestOptions
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;
62 using ::testing::internal::ParamGenerator;
63 using ::testing::internal::UnitTestOptions;
65 // Prints a value to a string.
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.
72 ::std::string PrintValue(const T& value) {
73 return testing::PrintToString(value);
76 // Verifies that a sequence generated by the generator and accessed
77 // via the iterator object matches the expected one using Google Test
79 template <typename T, size_t 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";
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";
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
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";
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";
119 template <typename T>
120 void VerifyGeneratorIsEmpty(const ParamGenerator<T>& generator) {
121 typename ParamGenerator<T>::iterator it = generator.begin();
122 EXPECT_TRUE(it == generator.end());
124 it = generator.begin();
125 EXPECT_TRUE(it == generator.end());
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.
135 // Tests that iterators produced by generator functions conform to the
136 // ForwardIterator concept.
137 TEST(IteratorTest, ParamIteratorConformsToForwardIteratorConcept) {
138 const ParamGenerator<int> gen = Range(0, 10);
139 ParamGenerator<int>::iterator it = gen.begin();
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";
146 // Verifies that iterator assignment works as expected.
148 EXPECT_FALSE(*it == *it2);
150 EXPECT_TRUE(*it == *it2) << "Assigned iterators must point to the "
151 << "element same as its source points to";
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";
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++));
163 // Verifies that prefix and postfix operator++() advance an iterator
168 EXPECT_TRUE(*it == *it2);
171 // Tests that Range() generates the expected sequence.
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);
178 // Edge case. Tests that Range() generates the single element sequence
179 // as expected when provided with range limits that are equal.
180 TEST(RangeTest, IntRangeSingleValue) {
181 const ParamGenerator<int> gen = Range(0, 1);
182 const int expected_values[] = {0};
183 VerifyGenerator(gen, expected_values);
186 // Edge case. Tests that Range() with generates empty sequence when
187 // supplied with an empty range.
188 TEST(RangeTest, IntRangeEmpty) {
189 const ParamGenerator<int> gen = Range(0, 0);
190 VerifyGeneratorIsEmpty(gen);
193 // Tests that Range() with custom step (greater then one) generates
194 // the expected sequence.
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);
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.
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);
211 // Verifies that Range works with user-defined types that define
212 // copy constructor, operator=(), operator+(), and operator<().
215 explicit DogAdder(const char* a_value) : value_(a_value) {}
216 DogAdder(const DogAdder& other) : value_(other.value_.c_str()) {}
218 DogAdder operator=(const DogAdder& other) {
219 if (this != &other) value_ = other.value_;
222 DogAdder operator+(const DogAdder& other) const {
224 msg << value_ << other.value_;
225 return DogAdder(msg.GetString().c_str());
227 bool operator<(const DogAdder& other) const { return value_ < other.value_; }
228 const std::string& value() const { return value_; }
234 TEST(RangeTest, WorksWithACustomType) {
235 const ParamGenerator<DogAdder> gen =
236 Range(DogAdder("cat"), DogAdder("catdogdog"), DogAdder("dog"));
237 ParamGenerator<DogAdder>::iterator it = gen.begin();
239 ASSERT_FALSE(it == gen.end());
240 EXPECT_STREQ("cat", it->value().c_str());
242 ASSERT_FALSE(++it == gen.end());
243 EXPECT_STREQ("catdog", it->value().c_str());
245 EXPECT_TRUE(++it == gen.end());
250 explicit IntWrapper(int a_value) : value_(a_value) {}
251 IntWrapper(const IntWrapper& other) : value_(other.value_) {}
253 IntWrapper operator=(const IntWrapper& other) {
254 value_ = other.value_;
257 // operator+() adds a different type.
258 IntWrapper operator+(int other) const { return IntWrapper(value_ + other); }
259 bool operator<(const IntWrapper& other) const {
260 return value_ < other.value_;
262 int value() const { return value_; }
268 TEST(RangeTest, WorksWithACustomTypeWithDifferentIncrementType) {
269 const ParamGenerator<IntWrapper> gen = Range(IntWrapper(0), IntWrapper(2));
270 ParamGenerator<IntWrapper>::iterator it = gen.begin();
272 ASSERT_FALSE(it == gen.end());
273 EXPECT_EQ(0, it->value());
275 ASSERT_FALSE(++it == gen.end());
276 EXPECT_EQ(1, it->value());
278 EXPECT_TRUE(++it == gen.end());
281 // Tests that ValuesIn() with an array parameter generates
282 // the expected sequence.
283 TEST(ValuesInTest, ValuesInArray) {
284 int array[] = {3, 5, 8};
285 const ParamGenerator<int> gen = ValuesIn(array);
286 VerifyGenerator(gen, array);
289 // Tests that ValuesIn() with a const array parameter generates
290 // the expected sequence.
291 TEST(ValuesInTest, ValuesInConstArray) {
292 const int array[] = {3, 5, 8};
293 const ParamGenerator<int> gen = ValuesIn(array);
294 VerifyGenerator(gen, array);
297 // Edge case. Tests that ValuesIn() with an array parameter containing a
298 // single element generates the single element sequence.
299 TEST(ValuesInTest, ValuesInSingleElementArray) {
301 const ParamGenerator<int> gen = ValuesIn(array);
302 VerifyGenerator(gen, array);
305 // Tests that ValuesIn() generates the expected sequence for an STL
306 // container (vector).
307 TEST(ValuesInTest, ValuesInVector) {
308 typedef ::std::vector<int> ContainerType;
309 ContainerType values;
313 const ParamGenerator<int> gen = ValuesIn(values);
315 const int expected_values[] = {3, 5, 8};
316 VerifyGenerator(gen, expected_values);
319 // Tests that ValuesIn() generates the expected sequence.
320 TEST(ValuesInTest, ValuesInIteratorRange) {
321 typedef ::std::vector<int> ContainerType;
322 ContainerType values;
326 const ParamGenerator<int> gen = ValuesIn(values.begin(), values.end());
328 const int expected_values[] = {3, 5, 8};
329 VerifyGenerator(gen, expected_values);
332 // Edge case. Tests that ValuesIn() provided with an iterator range specifying a
333 // single value generates a single-element sequence.
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());
340 const int expected_values[] = {42};
341 VerifyGenerator(gen, expected_values);
344 // Edge case. Tests that ValuesIn() provided with an empty iterator range
345 // generates an empty sequence.
346 TEST(ValuesInTest, ValuesInEmptyIteratorRange) {
347 typedef ::std::vector<int> ContainerType;
348 ContainerType values;
349 const ParamGenerator<int> gen = ValuesIn(values.begin(), values.end());
351 VerifyGeneratorIsEmpty(gen);
354 // Tests that the Values() generates the expected sequence.
355 TEST(ValuesTest, ValuesWorks) {
356 const ParamGenerator<int> gen = Values(3, 5, 8);
358 const int expected_values[] = {3, 5, 8};
359 VerifyGenerator(gen, expected_values);
362 // Tests that Values() generates the expected sequences from elements of
363 // different types convertible to ParamGenerator's parameter type.
364 TEST(ValuesTest, ValuesWorksForValuesOfCompatibleTypes) {
365 const ParamGenerator<double> gen = Values(3, 5.0f, 8.0);
367 const double expected_values[] = {3.0, 5.0, 8.0};
368 VerifyGenerator(gen, expected_values);
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);
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);
386 // Edge case test. Tests that single-parameter Values() generates the sequence
387 // with the single value.
388 TEST(ValuesTest, ValuesWithSingleParameter) {
389 const ParamGenerator<int> gen = Values(42);
391 const int expected_values[] = {42};
392 VerifyGenerator(gen, expected_values);
395 // Tests that Bool() generates sequence (false, true).
396 TEST(BoolTest, BoolWorks) {
397 const ParamGenerator<bool> gen = Bool();
399 const bool expected_values[] = {false, true};
400 VerifyGenerator(gen, expected_values);
403 // Tests that Combine() with two parameters generates the expected sequence.
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));
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);
416 // Tests that Combine() with three parameters generates the expected sequence.
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);
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.
431 TEST(CombineTest, CombineWithFirstParameterSingleValue) {
432 const ParamGenerator<std::tuple<int, int>> gen =
433 Combine(Values(42), Values(0, 1));
435 std::tuple<int, int> expected_values[] = {std::make_tuple(42, 0),
436 std::make_tuple(42, 1)};
437 VerifyGenerator(gen, expected_values);
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.
443 TEST(CombineTest, CombineWithSecondParameterSingleValue) {
444 const ParamGenerator<std::tuple<int, int>> gen =
445 Combine(Values(0, 1), Values(42));
447 std::tuple<int, int> expected_values[] = {std::make_tuple(0, 42),
448 std::make_tuple(1, 42)};
449 VerifyGenerator(gen, expected_values);
452 // Tests that when the first parameter produces an empty sequence,
453 // Combine() produces an empty sequence, too.
454 TEST(CombineTest, CombineWithFirstParameterEmptyRange) {
455 const ParamGenerator<std::tuple<int, int>> gen =
456 Combine(Range(0, 0), Values(0, 1));
457 VerifyGeneratorIsEmpty(gen);
460 // Tests that when the second parameter produces an empty sequence,
461 // Combine() produces an empty sequence, too.
462 TEST(CombineTest, CombineWithSecondParameterEmptyRange) {
463 const ParamGenerator<std::tuple<int, int>> gen =
464 Combine(Values(0, 1), Range(1, 1));
465 VerifyGeneratorIsEmpty(gen);
468 // Edge case. Tests that combine works with the maximum number
469 // of parameters supported by Google Test (currently 10).
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>>
476 Combine(Values(foo, bar), Values(1), Values(2), Values(3), Values(4),
477 Values(5), Values(6), Values(7), Values(8), Values(9));
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);
485 class NonDefaultConstructAssignString {
487 NonDefaultConstructAssignString(const std::string& s) : str_(s) {}
488 NonDefaultConstructAssignString() = delete;
489 NonDefaultConstructAssignString(const NonDefaultConstructAssignString&) =
491 NonDefaultConstructAssignString& operator=(
492 const NonDefaultConstructAssignString&) = delete;
493 ~NonDefaultConstructAssignString() = default;
495 const std::string& str() const { return str_; }
501 TEST(CombineTest, NonDefaultConstructAssign) {
502 const ParamGenerator<std::tuple<int, NonDefaultConstructAssignString>> gen =
503 Combine(Values(0, 1), Values(NonDefaultConstructAssignString("A"),
504 NonDefaultConstructAssignString("B")));
506 ParamGenerator<std::tuple<int, NonDefaultConstructAssignString>>::iterator
509 EXPECT_EQ(0, std::get<0>(*it));
510 EXPECT_EQ("A", std::get<1>(*it).str());
513 EXPECT_EQ(0, std::get<0>(*it));
514 EXPECT_EQ("B", std::get<1>(*it).str());
517 EXPECT_EQ(1, std::get<0>(*it));
518 EXPECT_EQ("A", std::get<1>(*it).str());
521 EXPECT_EQ(1, std::get<0>(*it));
522 EXPECT_EQ("B", std::get<1>(*it).str());
525 EXPECT_TRUE(it == gen.end());
528 template <typename T>
529 class ConstructFromT {
531 explicit ConstructFromT(const T& t) : t_(t) {}
532 template <typename... Args,
533 typename std::enable_if<sizeof...(Args) != 1, int>::type = 0>
534 ConstructFromT(Args&&... args) : t_(std::forward<Args>(args)...) {}
536 bool operator==(const ConstructFromT& other) const { return other.t_ == t_; }
538 const T& get() const { return t_; }
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)));
551 ConstructFromT<std::tuple<const char*, int>> expected_values[] = {
552 {foo, 3}, {foo, 4}, {bar, 3}, {bar, 4}};
553 VerifyGenerator(gen, expected_values);
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"))));
563 ParamGenerator<ConstructFromT<
564 std::tuple<int, NonDefaultConstructAssignString>>>::iterator it =
567 EXPECT_EQ(0, std::get<0>(it->get()));
568 EXPECT_EQ("A", std::get<1>(it->get()).str());
571 EXPECT_EQ(0, std::get<0>(it->get()));
572 EXPECT_EQ("B", std::get<1>(it->get()).str());
575 EXPECT_EQ(1, std::get<0>(it->get()));
576 EXPECT_EQ("A", std::get<1>(it->get()).str());
579 EXPECT_EQ(1, std::get<0>(it->get()));
580 EXPECT_EQ("B", std::get<1>(it->get()).str());
583 EXPECT_TRUE(it == gen.end());
586 // Tests that an generator produces correct sequence after being
587 // assigned from another generator.
588 TEST(ParamGeneratorTest, AssignmentWorks) {
589 ParamGenerator<int> gen = Values(1, 2);
590 const ParamGenerator<int> gen2 = Values(3, 4);
593 const int expected_values[] = {3, 4};
594 VerifyGenerator(gen, expected_values);
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.
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 {
609 static TestGenerationEnvironment* Instance() {
610 static TestGenerationEnvironment* instance = new TestGenerationEnvironment;
614 void FixtureConstructorExecuted() { fixture_constructor_count_++; }
615 void SetUpExecuted() { set_up_count_++; }
616 void TearDownExecuted() { tear_down_count_++; }
617 void TestBodyExecuted() { test_body_count_++; }
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;
624 for (int i = 0; i < kExpectedCalls; ++i) {
626 msg << "TestsExpandedAndRun/" << i;
627 if (UnitTestOptions::FilterMatchesTest(
628 "TestExpansionModule/MultipleTestGenerationTest",
629 msg.GetString().c_str())) {
630 perform_check = true;
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.";
650 TestGenerationEnvironment()
651 : fixture_constructor_count_(0),
654 test_body_count_(0) {}
656 int fixture_constructor_count_;
658 int tear_down_count_;
659 int test_body_count_;
661 TestGenerationEnvironment(const TestGenerationEnvironment&) = delete;
662 TestGenerationEnvironment& operator=(const TestGenerationEnvironment&) =
666 const int test_generation_params[] = {36, 42, 72};
668 class TestGenerationTest : public TestWithParam<int> {
672 sizeof(test_generation_params) / sizeof(test_generation_params[0])
675 typedef TestGenerationEnvironment<PARAMETER_COUNT> Environment;
677 TestGenerationTest() {
678 Environment::Instance()->FixtureConstructorExecuted();
679 current_parameter_ = GetParam();
681 void SetUp() override {
682 Environment::Instance()->SetUpExecuted();
683 EXPECT_EQ(current_parameter_, GetParam());
685 void TearDown() override {
686 Environment::Instance()->TearDownExecuted();
687 EXPECT_EQ(current_parameter_, GetParam());
690 static void SetUpTestSuite() {
691 bool all_tests_in_test_case_selected = true;
693 for (int i = 0; i < PARAMETER_COUNT; ++i) {
695 test_name << "TestsExpandedAndRun/" << i;
696 if (!UnitTestOptions::FilterMatchesTest(
697 "TestExpansionModule/MultipleTestGenerationTest",
698 test_name.GetString())) {
699 all_tests_in_test_case_selected = false;
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.";
708 collected_parameters_.clear();
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());
720 EXPECT_TRUE(collected_parameters_ == expected_values);
724 int current_parameter_;
725 static vector<int> collected_parameters_;
728 TestGenerationTest(const TestGenerationTest&) = delete;
729 TestGenerationTest& operator=(const TestGenerationTest&) = delete;
731 vector<int> TestGenerationTest::collected_parameters_;
733 TEST_P(TestGenerationTest, TestsExpandedAndRun) {
734 Environment::Instance()->TestBodyExecuted();
735 EXPECT_EQ(current_parameter_, GetParam());
736 collected_parameters_.push_back(GetParam());
738 INSTANTIATE_TEST_SUITE_P(TestExpansionModule, TestGenerationTest,
739 ValuesIn(test_generation_params));
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
751 class GeneratorEvaluationTest : public TestWithParam<int> {
753 static int param_value() { return param_value_; }
754 static void set_param_value(int param_value) { param_value_ = param_value; }
757 static int param_value_;
759 int GeneratorEvaluationTest::param_value_ = 0;
761 TEST_P(GeneratorEvaluationTest, GeneratorsEvaluatedInMain) {
762 EXPECT_EQ(1, GetParam());
764 INSTANTIATE_TEST_SUITE_P(GenEvalModule, GeneratorEvaluationTest,
765 Values(GeneratorEvaluationTest::param_value()));
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> {};
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);
776 INSTANTIATE_TEST_SUITE_P(ExternalGeneratorModule, ExternalGeneratorTest,
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.
783 TEST_P(ExternalInstantiationTest, IsMultipleOf33) {
784 EXPECT_EQ(0, GetParam() % 33);
787 // Tests that a parameterized test case can be instantiated with multiple
789 class MultipleInstantiationTest : public TestWithParam<int> {};
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));
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.
799 TEST_P(InstantiationInMultipleTranslationUnitsTest, IsMultipleOf42) {
800 EXPECT_EQ(0, GetParam() % 42);
802 INSTANTIATE_TEST_SUITE_P(Sequence1, InstantiationInMultipleTranslationUnitsTest,
805 // Tests that each iteration of parameterized test runs in a separate test
807 class SeparateInstanceTest : public TestWithParam<int> {
809 SeparateInstanceTest() : count_(0) {}
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.";
821 static int global_count_;
823 int SeparateInstanceTest::global_count_ = 0;
825 TEST_P(SeparateInstanceTest, TestsRunInSeparateInstances) {
826 EXPECT_EQ(0, count_++);
829 INSTANTIATE_TEST_SUITE_P(FourElemSequence, SeparateInstanceTest, Range(1, 4));
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> {};
838 TEST_P(NamingTest, TestsReportCorrectNamesAndParameters) {
839 const ::testing::TestInfo* const test_info =
840 ::testing::UnitTest::GetInstance()->current_test_info();
842 EXPECT_STREQ("ZeroToFiveSequence/NamingTest", test_info->test_suite_name());
844 Message index_stream;
845 index_stream << "TestsReportCorrectNamesAndParameters/" << GetParam();
846 EXPECT_STREQ(index_stream.GetString().c_str(), test_info->name());
848 EXPECT_EQ(::testing::PrintToString(GetParam()), test_info->value_param());
851 INSTANTIATE_TEST_SUITE_P(ZeroToFiveSequence, NamingTest, Range(0, 5));
853 // Tests that macros in test names are expanded correctly.
854 class MacroNamingTest : public TestWithParam<int> {};
856 #define PREFIX_WITH_FOO(test_name) Foo##test_name
857 #define PREFIX_WITH_MACRO(test_name) Macro##test_name
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();
863 EXPECT_STREQ("FortyTwo/MacroNamingTest", test_info->test_suite_name());
864 EXPECT_STREQ("FooSomeTestName/0", test_info->name());
867 INSTANTIATE_TEST_SUITE_P(FortyTwo, MacroNamingTest, Values(42));
869 // Tests the same thing for non-parametrized tests.
870 class MacroNamingTestNonParametrized : public ::testing::Test {};
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();
877 EXPECT_STREQ("MacroNamingTestNonParametrized", test_info->test_suite_name());
878 EXPECT_STREQ("FooSomeTestName", test_info->name());
881 TEST(MacroNameing, LookupNames) {
882 std::set<std::string> know_suite_names, know_test_names;
884 const auto& ins = testing::UnitTest::GetInstance();
886 while (const testing::TestSuite* suite = ins->GetTestSuite(ts++)) {
887 know_suite_names.insert(suite->name());
890 while (const testing::TestInfo* info = suite->GetTestInfo(ti++)) {
891 know_test_names.insert(std::string(suite->name()) + "." + info->name());
895 // Check that the expected form of the test suit name actually exists.
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.
903 know_test_names.find("FortyTwo/MacroNamingTest.FooSomeTestName/0"),
904 know_test_names.end());
906 know_test_names.find("MacroNamingTestNonParametrized.FooSomeTestName"),
907 know_test_names.end());
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> {};
915 TEST_P(CustomFunctorNamingTest, CustomTestNames) {}
917 struct CustomParamNameFunctor {
918 std::string operator()(const ::testing::TestParamInfo<std::string>& inf) {
923 INSTANTIATE_TEST_SUITE_P(CustomParamNameFunctor, CustomFunctorNamingTest,
924 Values(std::string("FunctorName")),
925 CustomParamNameFunctor());
927 INSTANTIATE_TEST_SUITE_P(AllAllowedCharacters, CustomFunctorNamingTest,
928 Values("abcdefghijklmnopqrstuvwxyz",
929 "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "01234567890_"),
930 CustomParamNameFunctor());
932 inline std::string CustomParamNameFunction(
933 const ::testing::TestParamInfo<std::string>& inf) {
937 class CustomFunctionNamingTest : public TestWithParam<std::string> {};
938 TEST_P(CustomFunctionNamingTest, CustomTestNames) {}
940 INSTANTIATE_TEST_SUITE_P(CustomParamNameFunction, CustomFunctionNamingTest,
941 Values(std::string("FunctionName")),
942 CustomParamNameFunction);
944 INSTANTIATE_TEST_SUITE_P(CustomParamNameFunctionP, CustomFunctionNamingTest,
945 Values(std::string("FunctionNameP")),
946 &CustomParamNameFunction);
948 // Test custom naming with a lambda
950 class CustomLambdaNamingTest : public TestWithParam<std::string> {};
951 TEST_P(CustomLambdaNamingTest, CustomTestNames) {}
953 INSTANTIATE_TEST_SUITE_P(CustomParamNameLambda, CustomLambdaNamingTest,
954 Values(std::string("LambdaName")),
955 [](const ::testing::TestParamInfo<std::string>& inf) {
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();
964 const ::testing::TestSuite* test_suite = unit_test->GetTestSuite(suite_num);
965 for (int test_num = 0; test_num < test_suite->total_test_count();
967 const ::testing::TestInfo* test_info = test_suite->GetTestInfo(test_num);
968 test_names.insert(std::string(test_info->name()));
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"));
977 // Test a numeric name to ensure PrintToStringParamName works correctly.
979 class CustomIntegerNamingTest : public TestWithParam<int> {};
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());
989 INSTANTIATE_TEST_SUITE_P(PrintToString, CustomIntegerNamingTest, Range(0, 5),
990 ::testing::PrintToStringParamName());
992 // Test a custom struct with PrintToString.
994 struct CustomStruct {
995 explicit CustomStruct(int value) : x(value) {}
999 std::ostream& operator<<(std::ostream& stream, const CustomStruct& val) {
1004 class CustomStructNamingTest : public TestWithParam<CustomStruct> {};
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());
1014 INSTANTIATE_TEST_SUITE_P(PrintToString, CustomStructNamingTest,
1015 Values(CustomStruct(0), CustomStruct(1)),
1016 ::testing::PrintToStringParamName());
1018 // Test that using a stateful parameter naming function works as expected.
1020 struct StatefulNamingFunctor {
1021 StatefulNamingFunctor() : sum(0) {}
1022 std::string operator()(const ::testing::TestParamInfo<int>& info) {
1023 int value = info.param + sum;
1025 return ::testing::PrintToString(value);
1030 class StatefulNamingTest : public ::testing::TestWithParam<int> {
1032 StatefulNamingTest() : sum_(0) {}
1036 TEST_P(StatefulNamingTest, TestsReportCorrectNames) {
1037 const ::testing::TestInfo* const test_info =
1038 ::testing::UnitTest::GetInstance()->current_test_info();
1040 Message test_name_stream;
1041 test_name_stream << "TestsReportCorrectNames/" << sum_;
1042 EXPECT_STREQ(test_name_stream.GetString().c_str(), test_info->name());
1045 INSTANTIATE_TEST_SUITE_P(StatefulNamingFunctor, StatefulNamingTest, Range(0, 5),
1046 StatefulNamingFunctor());
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
1052 class Unstreamable {
1054 explicit Unstreamable(int value) : value_(value) {}
1055 // -Wunused-private-field: dummy accessor for `value_`.
1056 const int& dummy_value() const { return value_; }
1062 class CommentTest : public TestWithParam<Unstreamable> {};
1064 TEST_P(CommentTest, TestsCorrectlyReportUnstreamableParams) {
1065 const ::testing::TestInfo* const test_info =
1066 ::testing::UnitTest::GetInstance()->current_test_info();
1068 EXPECT_EQ(::testing::PrintToString(GetParam()), test_info->value_param());
1071 INSTANTIATE_TEST_SUITE_P(InstantiationWithComments, CommentTest,
1072 Values(Unstreamable(1)));
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 {
1080 NonParameterizedBaseTest() : n_(17) {}
1086 class ParameterizedDerivedTest : public NonParameterizedBaseTest,
1087 public ::testing::WithParamInterface<int> {
1089 ParameterizedDerivedTest() : count_(0) {}
1091 static int global_count_;
1094 int ParameterizedDerivedTest::global_count_ = 0;
1096 TEST_F(NonParameterizedBaseTest, FixtureIsInitialized) { EXPECT_EQ(17, n_); }
1098 TEST_P(ParameterizedDerivedTest, SeesSequence) {
1100 EXPECT_EQ(0, count_++);
1101 EXPECT_EQ(GetParam(), global_count_++);
1104 class ParameterizedDeathTest : public ::testing::TestWithParam<int> {};
1106 TEST_F(ParameterizedDeathTest, GetParamDiesFromTestF) {
1107 EXPECT_DEATH_IF_SUPPORTED(GetParam(), ".* value-parameterized test .*");
1110 INSTANTIATE_TEST_SUITE_P(RangeZeroToFive, ParameterizedDerivedTest,
1113 // Tests param generator working with Enums
1120 class MyEnumTest : public testing::TestWithParam<MyEnums> {};
1122 TEST_P(MyEnumTest, ChecksParamMoreThanZero) { EXPECT_GE(10, GetParam()); }
1123 INSTANTIATE_TEST_SUITE_P(MyEnumTests, MyEnumTest,
1124 ::testing::Values(ENUM1, ENUM2, 0));
1126 namespace works_here {
1127 // Never used not instantiated, this should work.
1128 class NotUsedTest : public testing::TestWithParam<int> {};
1131 // Never used not instantiated, this should work.
1132 template <typename T>
1133 class NotUsedTypeTest : public testing::Test {};
1134 TYPED_TEST_SUITE_P(NotUsedTypeTest);
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);
1141 TEST_P(NotInstantiatedTest, Used) {}
1143 using OtherName = NotInstantiatedTest;
1144 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(OtherName);
1145 TEST_P(OtherName, Used) {}
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);
1154 TYPED_TEST_P(NotInstantiatedTypeTest, Used) {}
1155 REGISTER_TYPED_TEST_SUITE_P(NotInstantiatedTypeTest, Used);
1156 } // namespace works_here
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);
1165 ::testing::InitGoogleTest(&argc, argv);
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);
1172 return RUN_ALL_TESTS();