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.
30
// Author: vladl@google.com (Vlad Losev)
32
// Tests for Google Test itself. This file verifies that the parameter
33
// generators objects produce correct parameter sequences and that
34
// Google Test runtime instantiates correct tests from those sequences.
36
#include <gtest/gtest.h>
38
#if GTEST_HAS_PARAM_TEST
45
// To include gtest-internal-inl.h.
46
#define GTEST_IMPLEMENTATION_ 1
47
#include "src/gtest-internal-inl.h" // for UnitTestOptions
48
#undef GTEST_IMPLEMENTATION_
50
#include "test/gtest-param-test_test.h"
55
using ::testing::AddGlobalTestEnvironment;
56
using ::testing::Bool;
57
using ::testing::Message;
58
using ::testing::Range;
59
using ::testing::TestWithParam;
60
using ::testing::Values;
61
using ::testing::ValuesIn;
64
using ::testing::Combine;
65
using ::std::tr1::get;
66
using ::std::tr1::make_tuple;
67
using ::std::tr1::tuple;
68
#endif // GTEST_HAS_COMBINE
70
using ::testing::internal::ParamGenerator;
71
using ::testing::internal::UnitTestOptions;
73
// Verifies that a sequence generated by the generator and accessed
74
// via the iterator object matches the expected one using Google Test
76
template <typename T, size_t N>
77
void VerifyGenerator(const ParamGenerator<T>& generator,
78
const T (&expected_values)[N]) {
79
typename ParamGenerator<T>::iterator it = generator.begin();
80
for (size_t i = 0; i < N; ++i) {
81
ASSERT_FALSE(it == generator.end())
82
<< "At element " << i << " when accessing via an iterator "
83
<< "created with the copy constructor." << std::endl;
84
EXPECT_EQ(expected_values[i], *it)
85
<< "At element " << i << " when accessing via an iterator "
86
<< "created with the copy constructor." << std::endl;
89
EXPECT_TRUE(it == generator.end())
90
<< "At the presumed end of sequence when accessing via an iterator "
91
<< "created with the copy constructor." << std::endl;
93
// Test the iterator assignment. The following lines verify that
94
// the sequence accessed via an iterator initialized via the
95
// assignment operator (as opposed to a copy constructor) matches
97
it = generator.begin();
98
for (size_t i = 0; i < N; ++i) {
99
ASSERT_FALSE(it == generator.end())
100
<< "At element " << i << " when accessing via an iterator "
101
<< "created with the assignment operator." << std::endl;
102
EXPECT_EQ(expected_values[i], *it)
103
<< "At element " << i << " when accessing via an iterator "
104
<< "created with the assignment operator." << std::endl;
107
EXPECT_TRUE(it == generator.end())
108
<< "At the presumed end of sequence when accessing via an iterator "
109
<< "created with the assignment operator." << std::endl;
112
template <typename T>
113
void VerifyGeneratorIsEmpty(const ParamGenerator<T>& generator) {
114
typename ParamGenerator<T>::iterator it = generator.begin();
115
EXPECT_TRUE(it == generator.end());
117
it = generator.begin();
118
EXPECT_TRUE(it == generator.end());
121
// Generator tests. They test that each of the provided generator functions
122
// generates an expected sequence of values. The general test pattern
123
// instantiates a generator using one of the generator functions,
124
// checks the sequence produced by the generator using its iterator API,
125
// and then resets the iterator back to the beginning of the sequence
126
// and checks the sequence again.
128
// Tests that iterators produced by generator functions conform to the
129
// ForwardIterator concept.
130
TEST(IteratorTest, ParamIteratorConformsToForwardIteratorConcept) {
131
const ParamGenerator<int> gen = Range(0, 10);
132
ParamGenerator<int>::iterator it = gen.begin();
134
// Verifies that iterator initialization works as expected.
135
ParamGenerator<int>::iterator it2 = it;
136
EXPECT_TRUE(*it == *it2) << "Initialized iterators must point to the "
137
<< "element same as its source points to";
139
// Verifies that iterator assignment works as expected.
141
EXPECT_FALSE(*it == *it2);
143
EXPECT_TRUE(*it == *it2) << "Assigned iterators must point to the "
144
<< "element same as its source points to";
146
// Verifies that prefix operator++() returns *this.
147
EXPECT_EQ(&it, &(++it)) << "Result of the prefix operator++ must be "
148
<< "refer to the original object";
150
// Verifies that the result of the postfix operator++ points to the value
151
// pointed to by the original iterator.
152
int original_value = *it; // Have to compute it outside of macro call to be
153
// unaffected by the parameter evaluation order.
154
EXPECT_EQ(original_value, *(it++));
156
// Verifies that prefix and postfix operator++() advance an iterator
161
EXPECT_TRUE(*it == *it2);
164
// Tests that Range() generates the expected sequence.
165
TEST(RangeTest, IntRangeWithDefaultStep) {
166
const ParamGenerator<int> gen = Range(0, 3);
167
const int expected_values[] = {0, 1, 2};
168
VerifyGenerator(gen, expected_values);
171
// Edge case. Tests that Range() generates the single element sequence
172
// as expected when provided with range limits that are equal.
173
TEST(RangeTest, IntRangeSingleValue) {
174
const ParamGenerator<int> gen = Range(0, 1);
175
const int expected_values[] = {0};
176
VerifyGenerator(gen, expected_values);
179
// Edge case. Tests that Range() with generates empty sequence when
180
// supplied with an empty range.
181
TEST(RangeTest, IntRangeEmpty) {
182
const ParamGenerator<int> gen = Range(0, 0);
183
VerifyGeneratorIsEmpty(gen);
186
// Tests that Range() with custom step (greater then one) generates
187
// the expected sequence.
188
TEST(RangeTest, IntRangeWithCustomStep) {
189
const ParamGenerator<int> gen = Range(0, 9, 3);
190
const int expected_values[] = {0, 3, 6};
191
VerifyGenerator(gen, expected_values);
194
// Tests that Range() with custom step (greater then one) generates
195
// the expected sequence when the last element does not fall on the
196
// upper range limit. Sequences generated by Range() must not have
197
// elements beyond the range limits.
198
TEST(RangeTest, IntRangeWithCustomStepOverUpperBound) {
199
const ParamGenerator<int> gen = Range(0, 4, 3);
200
const int expected_values[] = {0, 3};
201
VerifyGenerator(gen, expected_values);
204
// Verifies that Range works with user-defined types that define
205
// copy constructor, operator=(), operator+(), and operator<().
208
explicit DogAdder(const char* value) : value_(value) {}
209
DogAdder(const DogAdder& other) : value_(other.value_.c_str()) {}
211
DogAdder operator=(const DogAdder& other) {
213
value_ = other.value_;
216
DogAdder operator+(const DogAdder& other) const {
218
msg << value_.c_str() << other.value_.c_str();
219
return DogAdder(msg.GetString().c_str());
221
bool operator<(const DogAdder& other) const {
222
return value_ < other.value_;
224
const ::testing::internal::String& value() const { return value_; }
227
::testing::internal::String value_;
230
TEST(RangeTest, WorksWithACustomType) {
231
const ParamGenerator<DogAdder> gen =
232
Range(DogAdder("cat"), DogAdder("catdogdog"), DogAdder("dog"));
233
ParamGenerator<DogAdder>::iterator it = gen.begin();
235
ASSERT_FALSE(it == gen.end());
236
EXPECT_STREQ("cat", it->value().c_str());
238
ASSERT_FALSE(++it == gen.end());
239
EXPECT_STREQ("catdog", it->value().c_str());
241
EXPECT_TRUE(++it == gen.end());
246
explicit IntWrapper(int value) : value_(value) {}
247
IntWrapper(const IntWrapper& other) : value_(other.value_) {}
249
IntWrapper operator=(const IntWrapper& other) {
250
value_ = other.value_;
253
// operator+() adds a different type.
254
IntWrapper operator+(int other) const { return IntWrapper(value_ + other); }
255
bool operator<(const IntWrapper& other) const {
256
return value_ < other.value_;
258
int value() const { return value_; }
264
TEST(RangeTest, WorksWithACustomTypeWithDifferentIncrementType) {
265
const ParamGenerator<IntWrapper> gen = Range(IntWrapper(0), IntWrapper(2));
266
ParamGenerator<IntWrapper>::iterator it = gen.begin();
268
ASSERT_FALSE(it == gen.end());
269
EXPECT_EQ(0, it->value());
271
ASSERT_FALSE(++it == gen.end());
272
EXPECT_EQ(1, it->value());
274
EXPECT_TRUE(++it == gen.end());
277
// Tests that ValuesIn() with an array parameter generates
278
// the expected sequence.
279
TEST(ValuesInTest, ValuesInArray) {
280
int array[] = {3, 5, 8};
281
const ParamGenerator<int> gen = ValuesIn(array);
282
VerifyGenerator(gen, array);
285
// Tests that ValuesIn() with a const array parameter generates
286
// the expected sequence.
287
TEST(ValuesInTest, ValuesInConstArray) {
288
const int array[] = {3, 5, 8};
289
const ParamGenerator<int> gen = ValuesIn(array);
290
VerifyGenerator(gen, array);
293
// Edge case. Tests that ValuesIn() with an array parameter containing a
294
// single element generates the single element sequence.
295
TEST(ValuesInTest, ValuesInSingleElementArray) {
297
const ParamGenerator<int> gen = ValuesIn(array);
298
VerifyGenerator(gen, array);
301
// Tests that ValuesIn() generates the expected sequence for an STL
302
// container (vector).
303
TEST(ValuesInTest, ValuesInVector) {
304
typedef ::std::vector<int> ContainerType;
305
ContainerType values;
309
const ParamGenerator<int> gen = ValuesIn(values);
311
const int expected_values[] = {3, 5, 8};
312
VerifyGenerator(gen, expected_values);
315
// Tests that ValuesIn() generates the expected sequence.
316
TEST(ValuesInTest, ValuesInIteratorRange) {
317
typedef ::std::vector<int> ContainerType;
318
ContainerType values;
322
const ParamGenerator<int> gen = ValuesIn(values.begin(), values.end());
324
const int expected_values[] = {3, 5, 8};
325
VerifyGenerator(gen, expected_values);
328
// Edge case. Tests that ValuesIn() provided with an iterator range specifying a
329
// single value generates a single-element sequence.
330
TEST(ValuesInTest, ValuesInSingleElementIteratorRange) {
331
typedef ::std::vector<int> ContainerType;
332
ContainerType values;
333
values.push_back(42);
334
const ParamGenerator<int> gen = ValuesIn(values.begin(), values.end());
336
const int expected_values[] = {42};
337
VerifyGenerator(gen, expected_values);
340
// Edge case. Tests that ValuesIn() provided with an empty iterator range
341
// generates an empty sequence.
342
TEST(ValuesInTest, ValuesInEmptyIteratorRange) {
343
typedef ::std::vector<int> ContainerType;
344
ContainerType values;
345
const ParamGenerator<int> gen = ValuesIn(values.begin(), values.end());
347
VerifyGeneratorIsEmpty(gen);
350
// Tests that the Values() generates the expected sequence.
351
TEST(ValuesTest, ValuesWorks) {
352
const ParamGenerator<int> gen = Values(3, 5, 8);
354
const int expected_values[] = {3, 5, 8};
355
VerifyGenerator(gen, expected_values);
358
// Tests that Values() generates the expected sequences from elements of
359
// different types convertible to ParamGenerator's parameter type.
360
TEST(ValuesTest, ValuesWorksForValuesOfCompatibleTypes) {
361
const ParamGenerator<double> gen = Values(3, 5.0f, 8.0);
363
const double expected_values[] = {3.0, 5.0, 8.0};
364
VerifyGenerator(gen, expected_values);
367
TEST(ValuesTest, ValuesWorksForMaxLengthList) {
368
const ParamGenerator<int> gen = Values(
369
10, 20, 30, 40, 50, 60, 70, 80, 90, 100,
370
110, 120, 130, 140, 150, 160, 170, 180, 190, 200,
371
210, 220, 230, 240, 250, 260, 270, 280, 290, 300,
372
310, 320, 330, 340, 350, 360, 370, 380, 390, 400,
373
410, 420, 430, 440, 450, 460, 470, 480, 490, 500);
375
const int expected_values[] = {
376
10, 20, 30, 40, 50, 60, 70, 80, 90, 100,
377
110, 120, 130, 140, 150, 160, 170, 180, 190, 200,
378
210, 220, 230, 240, 250, 260, 270, 280, 290, 300,
379
310, 320, 330, 340, 350, 360, 370, 380, 390, 400,
380
410, 420, 430, 440, 450, 460, 470, 480, 490, 500};
381
VerifyGenerator(gen, expected_values);
384
// Edge case test. Tests that single-parameter Values() generates the sequence
385
// with the single value.
386
TEST(ValuesTest, ValuesWithSingleParameter) {
387
const ParamGenerator<int> gen = Values(42);
389
const int expected_values[] = {42};
390
VerifyGenerator(gen, expected_values);
393
// Tests that Bool() generates sequence (false, true).
394
TEST(BoolTest, BoolWorks) {
395
const ParamGenerator<bool> gen = Bool();
397
const bool expected_values[] = {false, true};
398
VerifyGenerator(gen, expected_values);
401
#if GTEST_HAS_COMBINE
403
template <typename T1, typename T2>
404
::std::ostream& operator<<(::std::ostream& stream, const tuple<T1, T2>& value) {
405
stream << "(" << get<0>(value) << ", " << get<1>(value) << ")";
409
template <typename T1, typename T2, typename T3>
410
::std::ostream& operator<<(::std::ostream& stream,
411
const tuple<T1, T2, T3>& value) {
412
stream << "(" << get<0>(value) << ", " << get<1>(value)
413
<< ", "<< get<2>(value) << ")";
417
template <typename T1, typename T2, typename T3, typename T4, typename T5,
418
typename T6, typename T7, typename T8, typename T9, typename T10>
419
::std::ostream& operator<<(
420
::std::ostream& stream,
421
const tuple<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>& value) {
422
stream << "(" << get<0>(value) << ", " << get<1>(value)
423
<< ", "<< get<2>(value) << ", " << get<3>(value)
424
<< ", "<< get<4>(value) << ", " << get<5>(value)
425
<< ", "<< get<6>(value) << ", " << get<7>(value)
426
<< ", "<< get<8>(value) << ", " << get<9>(value) << ")";
430
// Tests that Combine() with two parameters generates the expected sequence.
431
TEST(CombineTest, CombineWithTwoParameters) {
432
const char* foo = "foo";
433
const char* bar = "bar";
434
const ParamGenerator<tuple<const char*, int> > gen =
435
Combine(Values(foo, bar), Values(3, 4));
437
tuple<const char*, int> expected_values[] = {
438
make_tuple(foo, 3), make_tuple(foo, 4),
439
make_tuple(bar, 3), make_tuple(bar, 4)};
440
VerifyGenerator(gen, expected_values);
443
// Tests that Combine() with three parameters generates the expected sequence.
444
TEST(CombineTest, CombineWithThreeParameters) {
445
const ParamGenerator<tuple<int, int, int> > gen = Combine(Values(0, 1),
448
tuple<int, int, int> expected_values[] = {
449
make_tuple(0, 3, 5), make_tuple(0, 3, 6),
450
make_tuple(0, 4, 5), make_tuple(0, 4, 6),
451
make_tuple(1, 3, 5), make_tuple(1, 3, 6),
452
make_tuple(1, 4, 5), make_tuple(1, 4, 6)};
453
VerifyGenerator(gen, expected_values);
456
// Tests that the Combine() with the first parameter generating a single value
457
// sequence generates a sequence with the number of elements equal to the
458
// number of elements in the sequence generated by the second parameter.
459
TEST(CombineTest, CombineWithFirstParameterSingleValue) {
460
const ParamGenerator<tuple<int, int> > gen = Combine(Values(42),
463
tuple<int, int> expected_values[] = {make_tuple(42, 0), make_tuple(42, 1)};
464
VerifyGenerator(gen, expected_values);
467
// Tests that the Combine() with the second parameter generating a single value
468
// sequence generates a sequence with the number of elements equal to the
469
// number of elements in the sequence generated by the first parameter.
470
TEST(CombineTest, CombineWithSecondParameterSingleValue) {
471
const ParamGenerator<tuple<int, int> > gen = Combine(Values(0, 1),
474
tuple<int, int> expected_values[] = {make_tuple(0, 42), make_tuple(1, 42)};
475
VerifyGenerator(gen, expected_values);
478
// Tests that when the first parameter produces an empty sequence,
479
// Combine() produces an empty sequence, too.
480
TEST(CombineTest, CombineWithFirstParameterEmptyRange) {
481
const ParamGenerator<tuple<int, int> > gen = Combine(Range(0, 0),
483
VerifyGeneratorIsEmpty(gen);
486
// Tests that when the second parameter produces an empty sequence,
487
// Combine() produces an empty sequence, too.
488
TEST(CombineTest, CombineWithSecondParameterEmptyRange) {
489
const ParamGenerator<tuple<int, int> > gen = Combine(Values(0, 1),
491
VerifyGeneratorIsEmpty(gen);
494
// Edge case. Tests that combine works with the maximum number
495
// of parameters supported by Google Test (currently 10).
496
TEST(CombineTest, CombineWithMaxNumberOfParameters) {
497
const char* foo = "foo";
498
const char* bar = "bar";
499
const ParamGenerator<tuple<const char*, int, int, int, int, int, int, int,
500
int, int> > gen = Combine(Values(foo, bar),
501
Values(1), Values(2),
502
Values(3), Values(4),
503
Values(5), Values(6),
504
Values(7), Values(8),
507
tuple<const char*, int, int, int, int, int, int, int, int, int>
508
expected_values[] = {make_tuple(foo, 1, 2, 3, 4, 5, 6, 7, 8, 9),
509
make_tuple(bar, 1, 2, 3, 4, 5, 6, 7, 8, 9)};
510
VerifyGenerator(gen, expected_values);
513
#endif // GTEST_HAS_COMBINE
515
// Tests that an generator produces correct sequence after being
516
// assigned from another generator.
517
TEST(ParamGeneratorTest, AssignmentWorks) {
518
ParamGenerator<int> gen = Values(1, 2);
519
const ParamGenerator<int> gen2 = Values(3, 4);
522
const int expected_values[] = {3, 4};
523
VerifyGenerator(gen, expected_values);
526
// This test verifies that the tests are expanded and run as specified:
527
// one test per element from the sequence produced by the generator
528
// specified in INSTANTIATE_TEST_CASE_P. It also verifies that the test's
529
// fixture constructor, SetUp(), and TearDown() have run and have been
530
// supplied with the correct parameters.
532
// The use of environment object allows detection of the case where no test
533
// case functionality is run at all. In this case TestCaseTearDown will not
534
// be able to detect missing tests, naturally.
535
template <int kExpectedCalls>
536
class TestGenerationEnvironment : public ::testing::Environment {
538
static TestGenerationEnvironment* Instance() {
539
static TestGenerationEnvironment* instance = new TestGenerationEnvironment;
543
void FixtureConstructorExecuted() { fixture_constructor_count_++; }
544
void SetUpExecuted() { set_up_count_++; }
545
void TearDownExecuted() { tear_down_count_++; }
546
void TestBodyExecuted() { test_body_count_++; }
548
virtual void TearDown() {
549
// If all MultipleTestGenerationTest tests have been de-selected
550
// by the filter flag, the following checks make no sense.
551
bool perform_check = false;
553
for (int i = 0; i < kExpectedCalls; ++i) {
555
msg << "TestsExpandedAndRun/" << i;
556
if (UnitTestOptions::FilterMatchesTest(
557
"TestExpansionModule/MultipleTestGenerationTest",
558
msg.GetString().c_str())) {
559
perform_check = true;
563
EXPECT_EQ(kExpectedCalls, fixture_constructor_count_)
564
<< "Fixture constructor of ParamTestGenerationTest test case "
565
<< "has not been run as expected.";
566
EXPECT_EQ(kExpectedCalls, set_up_count_)
567
<< "Fixture SetUp method of ParamTestGenerationTest test case "
568
<< "has not been run as expected.";
569
EXPECT_EQ(kExpectedCalls, tear_down_count_)
570
<< "Fixture TearDown method of ParamTestGenerationTest test case "
571
<< "has not been run as expected.";
572
EXPECT_EQ(kExpectedCalls, test_body_count_)
573
<< "Test in ParamTestGenerationTest test case "
574
<< "has not been run as expected.";
578
TestGenerationEnvironment() : fixture_constructor_count_(0), set_up_count_(0),
579
tear_down_count_(0), test_body_count_(0) {}
581
int fixture_constructor_count_;
583
int tear_down_count_;
584
int test_body_count_;
586
GTEST_DISALLOW_COPY_AND_ASSIGN_(TestGenerationEnvironment);
589
const int test_generation_params[] = {36, 42, 72};
591
class TestGenerationTest : public TestWithParam<int> {
595
sizeof(test_generation_params)/sizeof(test_generation_params[0])
598
typedef TestGenerationEnvironment<PARAMETER_COUNT> Environment;
600
TestGenerationTest() {
601
Environment::Instance()->FixtureConstructorExecuted();
602
current_parameter_ = GetParam();
604
virtual void SetUp() {
605
Environment::Instance()->SetUpExecuted();
606
EXPECT_EQ(current_parameter_, GetParam());
608
virtual void TearDown() {
609
Environment::Instance()->TearDownExecuted();
610
EXPECT_EQ(current_parameter_, GetParam());
613
static void SetUpTestCase() {
614
bool all_tests_in_test_case_selected = true;
616
for (int i = 0; i < PARAMETER_COUNT; ++i) {
618
test_name << "TestsExpandedAndRun/" << i;
619
if ( !UnitTestOptions::FilterMatchesTest(
620
"TestExpansionModule/MultipleTestGenerationTest",
621
test_name.GetString())) {
622
all_tests_in_test_case_selected = false;
625
EXPECT_TRUE(all_tests_in_test_case_selected)
626
<< "When running the TestGenerationTest test case all of its tests\n"
627
<< "must be selected by the filter flag for the test case to pass.\n"
628
<< "If not all of them are enabled, we can't reliably conclude\n"
629
<< "that the correct number of tests have been generated.";
631
collected_parameters_.clear();
634
static void TearDownTestCase() {
635
vector<int> expected_values(test_generation_params,
636
test_generation_params + PARAMETER_COUNT);
637
// Test execution order is not guaranteed by Google Test,
638
// so the order of values in collected_parameters_ can be
639
// different and we have to sort to compare.
640
sort(expected_values.begin(), expected_values.end());
641
sort(collected_parameters_.begin(), collected_parameters_.end());
643
EXPECT_TRUE(collected_parameters_ == expected_values);
646
int current_parameter_;
647
static vector<int> collected_parameters_;
650
GTEST_DISALLOW_COPY_AND_ASSIGN_(TestGenerationTest);
652
vector<int> TestGenerationTest::collected_parameters_;
654
TEST_P(TestGenerationTest, TestsExpandedAndRun) {
655
Environment::Instance()->TestBodyExecuted();
656
EXPECT_EQ(current_parameter_, GetParam());
657
collected_parameters_.push_back(GetParam());
659
INSTANTIATE_TEST_CASE_P(TestExpansionModule, TestGenerationTest,
660
ValuesIn(test_generation_params));
662
// This test verifies that the element sequence (third parameter of
663
// INSTANTIATE_TEST_CASE_P) is evaluated in RUN_ALL_TESTS and not at the call
664
// site of INSTANTIATE_TEST_CASE_P.
665
// For that, we declare param_value_ to be a static member of
666
// GeneratorEvaluationTest and initialize it to 0. We set it to 1 in main(),
667
// just before invocation of RUN_ALL_TESTS. If the sequence is evaluated
668
// before that moment, INSTANTIATE_TEST_CASE_P will create a test with
669
// parameter 0, and the test body will fail the assertion.
670
class GeneratorEvaluationTest : public TestWithParam<int> {
672
static int param_value() { return param_value_; }
673
static void set_param_value(int param_value) { param_value_ = param_value; }
676
static int param_value_;
678
int GeneratorEvaluationTest::param_value_ = 0;
680
TEST_P(GeneratorEvaluationTest, GeneratorsEvaluatedInMain) {
681
EXPECT_EQ(1, GetParam());
683
INSTANTIATE_TEST_CASE_P(GenEvalModule,
684
GeneratorEvaluationTest,
685
Values(GeneratorEvaluationTest::param_value()));
687
// Tests that generators defined in a different translation unit are
688
// functional. Generator extern_gen is defined in gtest-param-test_test2.cc.
689
extern ParamGenerator<int> extern_gen;
690
class ExternalGeneratorTest : public TestWithParam<int> {};
691
TEST_P(ExternalGeneratorTest, ExternalGenerator) {
692
// Sequence produced by extern_gen contains only a single value
693
// which we verify here.
694
EXPECT_EQ(GetParam(), 33);
696
INSTANTIATE_TEST_CASE_P(ExternalGeneratorModule,
697
ExternalGeneratorTest,
700
// Tests that a parameterized test case can be defined in one translation
701
// unit and instantiated in another. This test will be instantiated in
702
// gtest-param-test_test2.cc. ExternalInstantiationTest fixture class is
703
// defined in gtest-param-test_test.h.
704
TEST_P(ExternalInstantiationTest, IsMultipleOf33) {
705
EXPECT_EQ(0, GetParam() % 33);
708
// Tests that a parameterized test case can be instantiated with multiple
710
class MultipleInstantiationTest : public TestWithParam<int> {};
711
TEST_P(MultipleInstantiationTest, AllowsMultipleInstances) {
713
INSTANTIATE_TEST_CASE_P(Sequence1, MultipleInstantiationTest, Values(1, 2));
714
INSTANTIATE_TEST_CASE_P(Sequence2, MultipleInstantiationTest, Range(3, 5));
716
// Tests that a parameterized test case can be instantiated
717
// in multiple translation units. This test will be instantiated
718
// here and in gtest-param-test_test2.cc.
719
// InstantiationInMultipleTranslationUnitsTest fixture class
720
// is defined in gtest-param-test_test.h.
721
TEST_P(InstantiationInMultipleTranslaionUnitsTest, IsMultipleOf42) {
722
EXPECT_EQ(0, GetParam() % 42);
724
INSTANTIATE_TEST_CASE_P(Sequence1,
725
InstantiationInMultipleTranslaionUnitsTest,
728
// Tests that each iteration of parameterized test runs in a separate test
730
class SeparateInstanceTest : public TestWithParam<int> {
732
SeparateInstanceTest() : count_(0) {}
734
static void TearDownTestCase() {
735
EXPECT_GE(global_count_, 2)
736
<< "If some (but not all) SeparateInstanceTest tests have been "
737
<< "filtered out this test will fail. Make sure that all "
738
<< "GeneratorEvaluationTest are selected or de-selected together "
739
<< "by the test filter.";
744
static int global_count_;
746
int SeparateInstanceTest::global_count_ = 0;
748
TEST_P(SeparateInstanceTest, TestsRunInSeparateInstances) {
749
EXPECT_EQ(0, count_++);
752
INSTANTIATE_TEST_CASE_P(FourElemSequence, SeparateInstanceTest, Range(1, 4));
754
// Tests that all instantiations of a test have named appropriately. Test
755
// defined with TEST_P(TestCaseName, TestName) and instantiated with
756
// INSTANTIATE_TEST_CASE_P(SequenceName, TestCaseName, generator) must be named
757
// SequenceName/TestCaseName.TestName/i, where i is the 0-based index of the
758
// sequence element used to instantiate the test.
759
class NamingTest : public TestWithParam<int> {};
761
TEST_P(NamingTest, TestsAreNamedAppropriately) {
762
const ::testing::TestInfo* const test_info =
763
::testing::UnitTest::GetInstance()->current_test_info();
765
EXPECT_STREQ("ZeroToFiveSequence/NamingTest", test_info->test_case_name());
768
msg << "TestsAreNamedAppropriately/" << GetParam();
769
EXPECT_STREQ(msg.GetString().c_str(), test_info->name());
772
INSTANTIATE_TEST_CASE_P(ZeroToFiveSequence, NamingTest, Range(0, 5));
774
#endif // GTEST_HAS_PARAM_TEST
776
TEST(CompileTest, CombineIsDefinedOnlyWhenGtestHasParamTestIsDefined) {
777
#if GTEST_HAS_COMBINE && !GTEST_HAS_PARAM_TEST
778
FAIL() << "GTEST_HAS_COMBINE is defined while GTEST_HAS_PARAM_TEST is not\n"
782
int main(int argc, char **argv) {
783
#if GTEST_HAS_PARAM_TEST
784
// Used in TestGenerationTest test case.
785
AddGlobalTestEnvironment(TestGenerationTest::Environment::Instance());
786
// Used in GeneratorEvaluationTest test case.
787
GeneratorEvaluationTest::set_param_value(1);
788
#endif // GTEST_HAS_PARAM_TEST
790
::testing::InitGoogleTest(&argc, argv);
791
return RUN_ALL_TESTS();