michael@0: // Copyright 2008, Google Inc. michael@0: // All rights reserved. michael@0: // michael@0: // Redistribution and use in source and binary forms, with or without michael@0: // modification, are permitted provided that the following conditions are michael@0: // met: michael@0: // michael@0: // * Redistributions of source code must retain the above copyright michael@0: // notice, this list of conditions and the following disclaimer. michael@0: // * Redistributions in binary form must reproduce the above michael@0: // copyright notice, this list of conditions and the following disclaimer michael@0: // in the documentation and/or other materials provided with the michael@0: // distribution. michael@0: // * Neither the name of Google Inc. nor the names of its michael@0: // contributors may be used to endorse or promote products derived from michael@0: // this software without specific prior written permission. michael@0: // michael@0: // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS michael@0: // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT michael@0: // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR michael@0: // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT michael@0: // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, michael@0: // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT michael@0: // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, michael@0: // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY michael@0: // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT michael@0: // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE michael@0: // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: // michael@0: // Author: vladl@google.com (Vlad Losev) michael@0: // michael@0: // Tests for Google Test itself. This file verifies that the parameter michael@0: // generators objects produce correct parameter sequences and that michael@0: // Google Test runtime instantiates correct tests from those sequences. michael@0: michael@0: #include "gtest/gtest.h" michael@0: michael@0: #if GTEST_HAS_PARAM_TEST michael@0: michael@0: # include michael@0: # include michael@0: # include michael@0: # include michael@0: # include michael@0: # include michael@0: michael@0: // To include gtest-internal-inl.h. michael@0: # define GTEST_IMPLEMENTATION_ 1 michael@0: # include "src/gtest-internal-inl.h" // for UnitTestOptions michael@0: # undef GTEST_IMPLEMENTATION_ michael@0: michael@0: # include "test/gtest-param-test_test.h" michael@0: michael@0: using ::std::vector; michael@0: using ::std::sort; michael@0: michael@0: using ::testing::AddGlobalTestEnvironment; michael@0: using ::testing::Bool; michael@0: using ::testing::Message; michael@0: using ::testing::Range; michael@0: using ::testing::TestWithParam; michael@0: using ::testing::Values; michael@0: using ::testing::ValuesIn; michael@0: michael@0: # if GTEST_HAS_COMBINE michael@0: using ::testing::Combine; michael@0: using ::std::tr1::get; michael@0: using ::std::tr1::make_tuple; michael@0: using ::std::tr1::tuple; michael@0: # endif // GTEST_HAS_COMBINE michael@0: michael@0: using ::testing::internal::ParamGenerator; michael@0: using ::testing::internal::UnitTestOptions; michael@0: michael@0: // Prints a value to a string. michael@0: // michael@0: // TODO(wan@google.com): remove PrintValue() when we move matchers and michael@0: // EXPECT_THAT() from Google Mock to Google Test. At that time, we michael@0: // can write EXPECT_THAT(x, Eq(y)) to compare two tuples x and y, as michael@0: // EXPECT_THAT() and the matchers know how to print tuples. michael@0: template michael@0: ::std::string PrintValue(const T& value) { michael@0: ::std::stringstream stream; michael@0: stream << value; michael@0: return stream.str(); michael@0: } michael@0: michael@0: # if GTEST_HAS_COMBINE michael@0: michael@0: // These overloads allow printing tuples in our tests. We cannot michael@0: // define an operator<< for tuples, as that definition needs to be in michael@0: // the std namespace in order to be picked up by Google Test via michael@0: // Argument-Dependent Lookup, yet defining anything in the std michael@0: // namespace in non-STL code is undefined behavior. michael@0: michael@0: template michael@0: ::std::string PrintValue(const tuple& value) { michael@0: ::std::stringstream stream; michael@0: stream << "(" << get<0>(value) << ", " << get<1>(value) << ")"; michael@0: return stream.str(); michael@0: } michael@0: michael@0: template michael@0: ::std::string PrintValue(const tuple& value) { michael@0: ::std::stringstream stream; michael@0: stream << "(" << get<0>(value) << ", " << get<1>(value) michael@0: << ", "<< get<2>(value) << ")"; michael@0: return stream.str(); michael@0: } michael@0: michael@0: template michael@0: ::std::string PrintValue( michael@0: const tuple& value) { michael@0: ::std::stringstream stream; michael@0: stream << "(" << get<0>(value) << ", " << get<1>(value) michael@0: << ", "<< get<2>(value) << ", " << get<3>(value) michael@0: << ", "<< get<4>(value) << ", " << get<5>(value) michael@0: << ", "<< get<6>(value) << ", " << get<7>(value) michael@0: << ", "<< get<8>(value) << ", " << get<9>(value) << ")"; michael@0: return stream.str(); michael@0: } michael@0: michael@0: # endif // GTEST_HAS_COMBINE michael@0: michael@0: // Verifies that a sequence generated by the generator and accessed michael@0: // via the iterator object matches the expected one using Google Test michael@0: // assertions. michael@0: template michael@0: void VerifyGenerator(const ParamGenerator& generator, michael@0: const T (&expected_values)[N]) { michael@0: typename ParamGenerator::iterator it = generator.begin(); michael@0: for (size_t i = 0; i < N; ++i) { michael@0: ASSERT_FALSE(it == generator.end()) michael@0: << "At element " << i << " when accessing via an iterator " michael@0: << "created with the copy constructor.\n"; michael@0: // We cannot use EXPECT_EQ() here as the values may be tuples, michael@0: // which don't support <<. michael@0: EXPECT_TRUE(expected_values[i] == *it) michael@0: << "where i is " << i michael@0: << ", expected_values[i] is " << PrintValue(expected_values[i]) michael@0: << ", *it is " << PrintValue(*it) michael@0: << ", and 'it' is an iterator created with the copy constructor.\n"; michael@0: it++; michael@0: } michael@0: EXPECT_TRUE(it == generator.end()) michael@0: << "At the presumed end of sequence when accessing via an iterator " michael@0: << "created with the copy constructor.\n"; michael@0: michael@0: // Test the iterator assignment. The following lines verify that michael@0: // the sequence accessed via an iterator initialized via the michael@0: // assignment operator (as opposed to a copy constructor) matches michael@0: // just the same. michael@0: it = generator.begin(); michael@0: for (size_t i = 0; i < N; ++i) { michael@0: ASSERT_FALSE(it == generator.end()) michael@0: << "At element " << i << " when accessing via an iterator " michael@0: << "created with the assignment operator.\n"; michael@0: EXPECT_TRUE(expected_values[i] == *it) michael@0: << "where i is " << i michael@0: << ", expected_values[i] is " << PrintValue(expected_values[i]) michael@0: << ", *it is " << PrintValue(*it) michael@0: << ", and 'it' is an iterator created with the copy constructor.\n"; michael@0: it++; michael@0: } michael@0: EXPECT_TRUE(it == generator.end()) michael@0: << "At the presumed end of sequence when accessing via an iterator " michael@0: << "created with the assignment operator.\n"; michael@0: } michael@0: michael@0: template michael@0: void VerifyGeneratorIsEmpty(const ParamGenerator& generator) { michael@0: typename ParamGenerator::iterator it = generator.begin(); michael@0: EXPECT_TRUE(it == generator.end()); michael@0: michael@0: it = generator.begin(); michael@0: EXPECT_TRUE(it == generator.end()); michael@0: } michael@0: michael@0: // Generator tests. They test that each of the provided generator functions michael@0: // generates an expected sequence of values. The general test pattern michael@0: // instantiates a generator using one of the generator functions, michael@0: // checks the sequence produced by the generator using its iterator API, michael@0: // and then resets the iterator back to the beginning of the sequence michael@0: // and checks the sequence again. michael@0: michael@0: // Tests that iterators produced by generator functions conform to the michael@0: // ForwardIterator concept. michael@0: TEST(IteratorTest, ParamIteratorConformsToForwardIteratorConcept) { michael@0: const ParamGenerator gen = Range(0, 10); michael@0: ParamGenerator::iterator it = gen.begin(); michael@0: michael@0: // Verifies that iterator initialization works as expected. michael@0: ParamGenerator::iterator it2 = it; michael@0: EXPECT_TRUE(*it == *it2) << "Initialized iterators must point to the " michael@0: << "element same as its source points to"; michael@0: michael@0: // Verifies that iterator assignment works as expected. michael@0: it++; michael@0: EXPECT_FALSE(*it == *it2); michael@0: it2 = it; michael@0: EXPECT_TRUE(*it == *it2) << "Assigned iterators must point to the " michael@0: << "element same as its source points to"; michael@0: michael@0: // Verifies that prefix operator++() returns *this. michael@0: EXPECT_EQ(&it, &(++it)) << "Result of the prefix operator++ must be " michael@0: << "refer to the original object"; michael@0: michael@0: // Verifies that the result of the postfix operator++ points to the value michael@0: // pointed to by the original iterator. michael@0: int original_value = *it; // Have to compute it outside of macro call to be michael@0: // unaffected by the parameter evaluation order. michael@0: EXPECT_EQ(original_value, *(it++)); michael@0: michael@0: // Verifies that prefix and postfix operator++() advance an iterator michael@0: // all the same. michael@0: it2 = it; michael@0: it++; michael@0: ++it2; michael@0: EXPECT_TRUE(*it == *it2); michael@0: } michael@0: michael@0: // Tests that Range() generates the expected sequence. michael@0: TEST(RangeTest, IntRangeWithDefaultStep) { michael@0: const ParamGenerator gen = Range(0, 3); michael@0: const int expected_values[] = {0, 1, 2}; michael@0: VerifyGenerator(gen, expected_values); michael@0: } michael@0: michael@0: // Edge case. Tests that Range() generates the single element sequence michael@0: // as expected when provided with range limits that are equal. michael@0: TEST(RangeTest, IntRangeSingleValue) { michael@0: const ParamGenerator gen = Range(0, 1); michael@0: const int expected_values[] = {0}; michael@0: VerifyGenerator(gen, expected_values); michael@0: } michael@0: michael@0: // Edge case. Tests that Range() with generates empty sequence when michael@0: // supplied with an empty range. michael@0: TEST(RangeTest, IntRangeEmpty) { michael@0: const ParamGenerator gen = Range(0, 0); michael@0: VerifyGeneratorIsEmpty(gen); michael@0: } michael@0: michael@0: // Tests that Range() with custom step (greater then one) generates michael@0: // the expected sequence. michael@0: TEST(RangeTest, IntRangeWithCustomStep) { michael@0: const ParamGenerator gen = Range(0, 9, 3); michael@0: const int expected_values[] = {0, 3, 6}; michael@0: VerifyGenerator(gen, expected_values); michael@0: } michael@0: michael@0: // Tests that Range() with custom step (greater then one) generates michael@0: // the expected sequence when the last element does not fall on the michael@0: // upper range limit. Sequences generated by Range() must not have michael@0: // elements beyond the range limits. michael@0: TEST(RangeTest, IntRangeWithCustomStepOverUpperBound) { michael@0: const ParamGenerator gen = Range(0, 4, 3); michael@0: const int expected_values[] = {0, 3}; michael@0: VerifyGenerator(gen, expected_values); michael@0: } michael@0: michael@0: // Verifies that Range works with user-defined types that define michael@0: // copy constructor, operator=(), operator+(), and operator<(). michael@0: class DogAdder { michael@0: public: michael@0: explicit DogAdder(const char* a_value) : value_(a_value) {} michael@0: DogAdder(const DogAdder& other) : value_(other.value_.c_str()) {} michael@0: michael@0: DogAdder operator=(const DogAdder& other) { michael@0: if (this != &other) michael@0: value_ = other.value_; michael@0: return *this; michael@0: } michael@0: DogAdder operator+(const DogAdder& other) const { michael@0: Message msg; michael@0: msg << value_.c_str() << other.value_.c_str(); michael@0: return DogAdder(msg.GetString().c_str()); michael@0: } michael@0: bool operator<(const DogAdder& other) const { michael@0: return value_ < other.value_; michael@0: } michael@0: const ::testing::internal::String& value() const { return value_; } michael@0: michael@0: private: michael@0: ::testing::internal::String value_; michael@0: }; michael@0: michael@0: TEST(RangeTest, WorksWithACustomType) { michael@0: const ParamGenerator gen = michael@0: Range(DogAdder("cat"), DogAdder("catdogdog"), DogAdder("dog")); michael@0: ParamGenerator::iterator it = gen.begin(); michael@0: michael@0: ASSERT_FALSE(it == gen.end()); michael@0: EXPECT_STREQ("cat", it->value().c_str()); michael@0: michael@0: ASSERT_FALSE(++it == gen.end()); michael@0: EXPECT_STREQ("catdog", it->value().c_str()); michael@0: michael@0: EXPECT_TRUE(++it == gen.end()); michael@0: } michael@0: michael@0: class IntWrapper { michael@0: public: michael@0: explicit IntWrapper(int a_value) : value_(a_value) {} michael@0: IntWrapper(const IntWrapper& other) : value_(other.value_) {} michael@0: michael@0: IntWrapper operator=(const IntWrapper& other) { michael@0: value_ = other.value_; michael@0: return *this; michael@0: } michael@0: // operator+() adds a different type. michael@0: IntWrapper operator+(int other) const { return IntWrapper(value_ + other); } michael@0: bool operator<(const IntWrapper& other) const { michael@0: return value_ < other.value_; michael@0: } michael@0: int value() const { return value_; } michael@0: michael@0: private: michael@0: int value_; michael@0: }; michael@0: michael@0: TEST(RangeTest, WorksWithACustomTypeWithDifferentIncrementType) { michael@0: const ParamGenerator gen = Range(IntWrapper(0), IntWrapper(2)); michael@0: ParamGenerator::iterator it = gen.begin(); michael@0: michael@0: ASSERT_FALSE(it == gen.end()); michael@0: EXPECT_EQ(0, it->value()); michael@0: michael@0: ASSERT_FALSE(++it == gen.end()); michael@0: EXPECT_EQ(1, it->value()); michael@0: michael@0: EXPECT_TRUE(++it == gen.end()); michael@0: } michael@0: michael@0: // Tests that ValuesIn() with an array parameter generates michael@0: // the expected sequence. michael@0: TEST(ValuesInTest, ValuesInArray) { michael@0: int array[] = {3, 5, 8}; michael@0: const ParamGenerator gen = ValuesIn(array); michael@0: VerifyGenerator(gen, array); michael@0: } michael@0: michael@0: // Tests that ValuesIn() with a const array parameter generates michael@0: // the expected sequence. michael@0: TEST(ValuesInTest, ValuesInConstArray) { michael@0: const int array[] = {3, 5, 8}; michael@0: const ParamGenerator gen = ValuesIn(array); michael@0: VerifyGenerator(gen, array); michael@0: } michael@0: michael@0: // Edge case. Tests that ValuesIn() with an array parameter containing a michael@0: // single element generates the single element sequence. michael@0: TEST(ValuesInTest, ValuesInSingleElementArray) { michael@0: int array[] = {42}; michael@0: const ParamGenerator gen = ValuesIn(array); michael@0: VerifyGenerator(gen, array); michael@0: } michael@0: michael@0: // Tests that ValuesIn() generates the expected sequence for an STL michael@0: // container (vector). michael@0: TEST(ValuesInTest, ValuesInVector) { michael@0: typedef ::std::vector ContainerType; michael@0: ContainerType values; michael@0: values.push_back(3); michael@0: values.push_back(5); michael@0: values.push_back(8); michael@0: const ParamGenerator gen = ValuesIn(values); michael@0: michael@0: const int expected_values[] = {3, 5, 8}; michael@0: VerifyGenerator(gen, expected_values); michael@0: } michael@0: michael@0: // Tests that ValuesIn() generates the expected sequence. michael@0: TEST(ValuesInTest, ValuesInIteratorRange) { michael@0: typedef ::std::vector ContainerType; michael@0: ContainerType values; michael@0: values.push_back(3); michael@0: values.push_back(5); michael@0: values.push_back(8); michael@0: const ParamGenerator gen = ValuesIn(values.begin(), values.end()); michael@0: michael@0: const int expected_values[] = {3, 5, 8}; michael@0: VerifyGenerator(gen, expected_values); michael@0: } michael@0: michael@0: // Edge case. Tests that ValuesIn() provided with an iterator range specifying a michael@0: // single value generates a single-element sequence. michael@0: TEST(ValuesInTest, ValuesInSingleElementIteratorRange) { michael@0: typedef ::std::vector ContainerType; michael@0: ContainerType values; michael@0: values.push_back(42); michael@0: const ParamGenerator gen = ValuesIn(values.begin(), values.end()); michael@0: michael@0: const int expected_values[] = {42}; michael@0: VerifyGenerator(gen, expected_values); michael@0: } michael@0: michael@0: // Edge case. Tests that ValuesIn() provided with an empty iterator range michael@0: // generates an empty sequence. michael@0: TEST(ValuesInTest, ValuesInEmptyIteratorRange) { michael@0: typedef ::std::vector ContainerType; michael@0: ContainerType values; michael@0: const ParamGenerator gen = ValuesIn(values.begin(), values.end()); michael@0: michael@0: VerifyGeneratorIsEmpty(gen); michael@0: } michael@0: michael@0: // Tests that the Values() generates the expected sequence. michael@0: TEST(ValuesTest, ValuesWorks) { michael@0: const ParamGenerator gen = Values(3, 5, 8); michael@0: michael@0: const int expected_values[] = {3, 5, 8}; michael@0: VerifyGenerator(gen, expected_values); michael@0: } michael@0: michael@0: // Tests that Values() generates the expected sequences from elements of michael@0: // different types convertible to ParamGenerator's parameter type. michael@0: TEST(ValuesTest, ValuesWorksForValuesOfCompatibleTypes) { michael@0: const ParamGenerator gen = Values(3, 5.0f, 8.0); michael@0: michael@0: const double expected_values[] = {3.0, 5.0, 8.0}; michael@0: VerifyGenerator(gen, expected_values); michael@0: } michael@0: michael@0: TEST(ValuesTest, ValuesWorksForMaxLengthList) { michael@0: const ParamGenerator gen = Values( michael@0: 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, michael@0: 110, 120, 130, 140, 150, 160, 170, 180, 190, 200, michael@0: 210, 220, 230, 240, 250, 260, 270, 280, 290, 300, michael@0: 310, 320, 330, 340, 350, 360, 370, 380, 390, 400, michael@0: 410, 420, 430, 440, 450, 460, 470, 480, 490, 500); michael@0: michael@0: const int expected_values[] = { michael@0: 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, michael@0: 110, 120, 130, 140, 150, 160, 170, 180, 190, 200, michael@0: 210, 220, 230, 240, 250, 260, 270, 280, 290, 300, michael@0: 310, 320, 330, 340, 350, 360, 370, 380, 390, 400, michael@0: 410, 420, 430, 440, 450, 460, 470, 480, 490, 500}; michael@0: VerifyGenerator(gen, expected_values); michael@0: } michael@0: michael@0: // Edge case test. Tests that single-parameter Values() generates the sequence michael@0: // with the single value. michael@0: TEST(ValuesTest, ValuesWithSingleParameter) { michael@0: const ParamGenerator gen = Values(42); michael@0: michael@0: const int expected_values[] = {42}; michael@0: VerifyGenerator(gen, expected_values); michael@0: } michael@0: michael@0: // Tests that Bool() generates sequence (false, true). michael@0: TEST(BoolTest, BoolWorks) { michael@0: const ParamGenerator gen = Bool(); michael@0: michael@0: const bool expected_values[] = {false, true}; michael@0: VerifyGenerator(gen, expected_values); michael@0: } michael@0: michael@0: # if GTEST_HAS_COMBINE michael@0: michael@0: // Tests that Combine() with two parameters generates the expected sequence. michael@0: TEST(CombineTest, CombineWithTwoParameters) { michael@0: const char* foo = "foo"; michael@0: const char* bar = "bar"; michael@0: const ParamGenerator > gen = michael@0: Combine(Values(foo, bar), Values(3, 4)); michael@0: michael@0: tuple expected_values[] = { michael@0: make_tuple(foo, 3), make_tuple(foo, 4), michael@0: make_tuple(bar, 3), make_tuple(bar, 4)}; michael@0: VerifyGenerator(gen, expected_values); michael@0: } michael@0: michael@0: // Tests that Combine() with three parameters generates the expected sequence. michael@0: TEST(CombineTest, CombineWithThreeParameters) { michael@0: const ParamGenerator > gen = Combine(Values(0, 1), michael@0: Values(3, 4), michael@0: Values(5, 6)); michael@0: tuple expected_values[] = { michael@0: make_tuple(0, 3, 5), make_tuple(0, 3, 6), michael@0: make_tuple(0, 4, 5), make_tuple(0, 4, 6), michael@0: make_tuple(1, 3, 5), make_tuple(1, 3, 6), michael@0: make_tuple(1, 4, 5), make_tuple(1, 4, 6)}; michael@0: VerifyGenerator(gen, expected_values); michael@0: } michael@0: michael@0: // Tests that the Combine() with the first parameter generating a single value michael@0: // sequence generates a sequence with the number of elements equal to the michael@0: // number of elements in the sequence generated by the second parameter. michael@0: TEST(CombineTest, CombineWithFirstParameterSingleValue) { michael@0: const ParamGenerator > gen = Combine(Values(42), michael@0: Values(0, 1)); michael@0: michael@0: tuple expected_values[] = {make_tuple(42, 0), make_tuple(42, 1)}; michael@0: VerifyGenerator(gen, expected_values); michael@0: } michael@0: michael@0: // Tests that the Combine() with the second parameter generating a single value michael@0: // sequence generates a sequence with the number of elements equal to the michael@0: // number of elements in the sequence generated by the first parameter. michael@0: TEST(CombineTest, CombineWithSecondParameterSingleValue) { michael@0: const ParamGenerator > gen = Combine(Values(0, 1), michael@0: Values(42)); michael@0: michael@0: tuple expected_values[] = {make_tuple(0, 42), make_tuple(1, 42)}; michael@0: VerifyGenerator(gen, expected_values); michael@0: } michael@0: michael@0: // Tests that when the first parameter produces an empty sequence, michael@0: // Combine() produces an empty sequence, too. michael@0: TEST(CombineTest, CombineWithFirstParameterEmptyRange) { michael@0: const ParamGenerator > gen = Combine(Range(0, 0), michael@0: Values(0, 1)); michael@0: VerifyGeneratorIsEmpty(gen); michael@0: } michael@0: michael@0: // Tests that when the second parameter produces an empty sequence, michael@0: // Combine() produces an empty sequence, too. michael@0: TEST(CombineTest, CombineWithSecondParameterEmptyRange) { michael@0: const ParamGenerator > gen = Combine(Values(0, 1), michael@0: Range(1, 1)); michael@0: VerifyGeneratorIsEmpty(gen); michael@0: } michael@0: michael@0: // Edge case. Tests that combine works with the maximum number michael@0: // of parameters supported by Google Test (currently 10). michael@0: TEST(CombineTest, CombineWithMaxNumberOfParameters) { michael@0: const char* foo = "foo"; michael@0: const char* bar = "bar"; michael@0: const ParamGenerator > gen = Combine(Values(foo, bar), michael@0: Values(1), Values(2), michael@0: Values(3), Values(4), michael@0: Values(5), Values(6), michael@0: Values(7), Values(8), michael@0: Values(9)); michael@0: michael@0: tuple michael@0: expected_values[] = {make_tuple(foo, 1, 2, 3, 4, 5, 6, 7, 8, 9), michael@0: make_tuple(bar, 1, 2, 3, 4, 5, 6, 7, 8, 9)}; michael@0: VerifyGenerator(gen, expected_values); michael@0: } michael@0: michael@0: # endif // GTEST_HAS_COMBINE michael@0: michael@0: // Tests that an generator produces correct sequence after being michael@0: // assigned from another generator. michael@0: TEST(ParamGeneratorTest, AssignmentWorks) { michael@0: ParamGenerator gen = Values(1, 2); michael@0: const ParamGenerator gen2 = Values(3, 4); michael@0: gen = gen2; michael@0: michael@0: const int expected_values[] = {3, 4}; michael@0: VerifyGenerator(gen, expected_values); michael@0: } michael@0: michael@0: // This test verifies that the tests are expanded and run as specified: michael@0: // one test per element from the sequence produced by the generator michael@0: // specified in INSTANTIATE_TEST_CASE_P. It also verifies that the test's michael@0: // fixture constructor, SetUp(), and TearDown() have run and have been michael@0: // supplied with the correct parameters. michael@0: michael@0: // The use of environment object allows detection of the case where no test michael@0: // case functionality is run at all. In this case TestCaseTearDown will not michael@0: // be able to detect missing tests, naturally. michael@0: template michael@0: class TestGenerationEnvironment : public ::testing::Environment { michael@0: public: michael@0: static TestGenerationEnvironment* Instance() { michael@0: static TestGenerationEnvironment* instance = new TestGenerationEnvironment; michael@0: return instance; michael@0: } michael@0: michael@0: void FixtureConstructorExecuted() { fixture_constructor_count_++; } michael@0: void SetUpExecuted() { set_up_count_++; } michael@0: void TearDownExecuted() { tear_down_count_++; } michael@0: void TestBodyExecuted() { test_body_count_++; } michael@0: michael@0: virtual void TearDown() { michael@0: // If all MultipleTestGenerationTest tests have been de-selected michael@0: // by the filter flag, the following checks make no sense. michael@0: bool perform_check = false; michael@0: michael@0: for (int i = 0; i < kExpectedCalls; ++i) { michael@0: Message msg; michael@0: msg << "TestsExpandedAndRun/" << i; michael@0: if (UnitTestOptions::FilterMatchesTest( michael@0: "TestExpansionModule/MultipleTestGenerationTest", michael@0: msg.GetString().c_str())) { michael@0: perform_check = true; michael@0: } michael@0: } michael@0: if (perform_check) { michael@0: EXPECT_EQ(kExpectedCalls, fixture_constructor_count_) michael@0: << "Fixture constructor of ParamTestGenerationTest test case " michael@0: << "has not been run as expected."; michael@0: EXPECT_EQ(kExpectedCalls, set_up_count_) michael@0: << "Fixture SetUp method of ParamTestGenerationTest test case " michael@0: << "has not been run as expected."; michael@0: EXPECT_EQ(kExpectedCalls, tear_down_count_) michael@0: << "Fixture TearDown method of ParamTestGenerationTest test case " michael@0: << "has not been run as expected."; michael@0: EXPECT_EQ(kExpectedCalls, test_body_count_) michael@0: << "Test in ParamTestGenerationTest test case " michael@0: << "has not been run as expected."; michael@0: } michael@0: } michael@0: michael@0: private: michael@0: TestGenerationEnvironment() : fixture_constructor_count_(0), set_up_count_(0), michael@0: tear_down_count_(0), test_body_count_(0) {} michael@0: michael@0: int fixture_constructor_count_; michael@0: int set_up_count_; michael@0: int tear_down_count_; michael@0: int test_body_count_; michael@0: michael@0: GTEST_DISALLOW_COPY_AND_ASSIGN_(TestGenerationEnvironment); michael@0: }; michael@0: michael@0: const int test_generation_params[] = {36, 42, 72}; michael@0: michael@0: class TestGenerationTest : public TestWithParam { michael@0: public: michael@0: enum { michael@0: PARAMETER_COUNT = michael@0: sizeof(test_generation_params)/sizeof(test_generation_params[0]) michael@0: }; michael@0: michael@0: typedef TestGenerationEnvironment Environment; michael@0: michael@0: TestGenerationTest() { michael@0: Environment::Instance()->FixtureConstructorExecuted(); michael@0: current_parameter_ = GetParam(); michael@0: } michael@0: virtual void SetUp() { michael@0: Environment::Instance()->SetUpExecuted(); michael@0: EXPECT_EQ(current_parameter_, GetParam()); michael@0: } michael@0: virtual void TearDown() { michael@0: Environment::Instance()->TearDownExecuted(); michael@0: EXPECT_EQ(current_parameter_, GetParam()); michael@0: } michael@0: michael@0: static void SetUpTestCase() { michael@0: bool all_tests_in_test_case_selected = true; michael@0: michael@0: for (int i = 0; i < PARAMETER_COUNT; ++i) { michael@0: Message test_name; michael@0: test_name << "TestsExpandedAndRun/" << i; michael@0: if ( !UnitTestOptions::FilterMatchesTest( michael@0: "TestExpansionModule/MultipleTestGenerationTest", michael@0: test_name.GetString())) { michael@0: all_tests_in_test_case_selected = false; michael@0: } michael@0: } michael@0: EXPECT_TRUE(all_tests_in_test_case_selected) michael@0: << "When running the TestGenerationTest test case all of its tests\n" michael@0: << "must be selected by the filter flag for the test case to pass.\n" michael@0: << "If not all of them are enabled, we can't reliably conclude\n" michael@0: << "that the correct number of tests have been generated."; michael@0: michael@0: collected_parameters_.clear(); michael@0: } michael@0: michael@0: static void TearDownTestCase() { michael@0: vector expected_values(test_generation_params, michael@0: test_generation_params + PARAMETER_COUNT); michael@0: // Test execution order is not guaranteed by Google Test, michael@0: // so the order of values in collected_parameters_ can be michael@0: // different and we have to sort to compare. michael@0: sort(expected_values.begin(), expected_values.end()); michael@0: sort(collected_parameters_.begin(), collected_parameters_.end()); michael@0: michael@0: EXPECT_TRUE(collected_parameters_ == expected_values); michael@0: } michael@0: michael@0: protected: michael@0: int current_parameter_; michael@0: static vector collected_parameters_; michael@0: michael@0: private: michael@0: GTEST_DISALLOW_COPY_AND_ASSIGN_(TestGenerationTest); michael@0: }; michael@0: vector TestGenerationTest::collected_parameters_; michael@0: michael@0: TEST_P(TestGenerationTest, TestsExpandedAndRun) { michael@0: Environment::Instance()->TestBodyExecuted(); michael@0: EXPECT_EQ(current_parameter_, GetParam()); michael@0: collected_parameters_.push_back(GetParam()); michael@0: } michael@0: INSTANTIATE_TEST_CASE_P(TestExpansionModule, TestGenerationTest, michael@0: ValuesIn(test_generation_params)); michael@0: michael@0: // This test verifies that the element sequence (third parameter of michael@0: // INSTANTIATE_TEST_CASE_P) is evaluated in InitGoogleTest() and neither at michael@0: // the call site of INSTANTIATE_TEST_CASE_P nor in RUN_ALL_TESTS(). For michael@0: // that, we declare param_value_ to be a static member of michael@0: // GeneratorEvaluationTest and initialize it to 0. We set it to 1 in michael@0: // main(), just before invocation of InitGoogleTest(). After calling michael@0: // InitGoogleTest(), we set the value to 2. If the sequence is evaluated michael@0: // before or after InitGoogleTest, INSTANTIATE_TEST_CASE_P will create a michael@0: // test with parameter other than 1, and the test body will fail the michael@0: // assertion. michael@0: class GeneratorEvaluationTest : public TestWithParam { michael@0: public: michael@0: static int param_value() { return param_value_; } michael@0: static void set_param_value(int param_value) { param_value_ = param_value; } michael@0: michael@0: private: michael@0: static int param_value_; michael@0: }; michael@0: int GeneratorEvaluationTest::param_value_ = 0; michael@0: michael@0: TEST_P(GeneratorEvaluationTest, GeneratorsEvaluatedInMain) { michael@0: EXPECT_EQ(1, GetParam()); michael@0: } michael@0: INSTANTIATE_TEST_CASE_P(GenEvalModule, michael@0: GeneratorEvaluationTest, michael@0: Values(GeneratorEvaluationTest::param_value())); michael@0: michael@0: // Tests that generators defined in a different translation unit are michael@0: // functional. Generator extern_gen is defined in gtest-param-test_test2.cc. michael@0: extern ParamGenerator extern_gen; michael@0: class ExternalGeneratorTest : public TestWithParam {}; michael@0: TEST_P(ExternalGeneratorTest, ExternalGenerator) { michael@0: // Sequence produced by extern_gen contains only a single value michael@0: // which we verify here. michael@0: EXPECT_EQ(GetParam(), 33); michael@0: } michael@0: INSTANTIATE_TEST_CASE_P(ExternalGeneratorModule, michael@0: ExternalGeneratorTest, michael@0: extern_gen); michael@0: michael@0: // Tests that a parameterized test case can be defined in one translation michael@0: // unit and instantiated in another. This test will be instantiated in michael@0: // gtest-param-test_test2.cc. ExternalInstantiationTest fixture class is michael@0: // defined in gtest-param-test_test.h. michael@0: TEST_P(ExternalInstantiationTest, IsMultipleOf33) { michael@0: EXPECT_EQ(0, GetParam() % 33); michael@0: } michael@0: michael@0: // Tests that a parameterized test case can be instantiated with multiple michael@0: // generators. michael@0: class MultipleInstantiationTest : public TestWithParam {}; michael@0: TEST_P(MultipleInstantiationTest, AllowsMultipleInstances) { michael@0: } michael@0: INSTANTIATE_TEST_CASE_P(Sequence1, MultipleInstantiationTest, Values(1, 2)); michael@0: INSTANTIATE_TEST_CASE_P(Sequence2, MultipleInstantiationTest, Range(3, 5)); michael@0: michael@0: // Tests that a parameterized test case can be instantiated michael@0: // in multiple translation units. This test will be instantiated michael@0: // here and in gtest-param-test_test2.cc. michael@0: // InstantiationInMultipleTranslationUnitsTest fixture class michael@0: // is defined in gtest-param-test_test.h. michael@0: TEST_P(InstantiationInMultipleTranslaionUnitsTest, IsMultipleOf42) { michael@0: EXPECT_EQ(0, GetParam() % 42); michael@0: } michael@0: INSTANTIATE_TEST_CASE_P(Sequence1, michael@0: InstantiationInMultipleTranslaionUnitsTest, michael@0: Values(42, 42*2)); michael@0: michael@0: // Tests that each iteration of parameterized test runs in a separate test michael@0: // object. michael@0: class SeparateInstanceTest : public TestWithParam { michael@0: public: michael@0: SeparateInstanceTest() : count_(0) {} michael@0: michael@0: static void TearDownTestCase() { michael@0: EXPECT_GE(global_count_, 2) michael@0: << "If some (but not all) SeparateInstanceTest tests have been " michael@0: << "filtered out this test will fail. Make sure that all " michael@0: << "GeneratorEvaluationTest are selected or de-selected together " michael@0: << "by the test filter."; michael@0: } michael@0: michael@0: protected: michael@0: int count_; michael@0: static int global_count_; michael@0: }; michael@0: int SeparateInstanceTest::global_count_ = 0; michael@0: michael@0: TEST_P(SeparateInstanceTest, TestsRunInSeparateInstances) { michael@0: EXPECT_EQ(0, count_++); michael@0: global_count_++; michael@0: } michael@0: INSTANTIATE_TEST_CASE_P(FourElemSequence, SeparateInstanceTest, Range(1, 4)); michael@0: michael@0: // Tests that all instantiations of a test have named appropriately. Test michael@0: // defined with TEST_P(TestCaseName, TestName) and instantiated with michael@0: // INSTANTIATE_TEST_CASE_P(SequenceName, TestCaseName, generator) must be named michael@0: // SequenceName/TestCaseName.TestName/i, where i is the 0-based index of the michael@0: // sequence element used to instantiate the test. michael@0: class NamingTest : public TestWithParam {}; michael@0: michael@0: TEST_P(NamingTest, TestsReportCorrectNamesAndParameters) { michael@0: const ::testing::TestInfo* const test_info = michael@0: ::testing::UnitTest::GetInstance()->current_test_info(); michael@0: michael@0: EXPECT_STREQ("ZeroToFiveSequence/NamingTest", test_info->test_case_name()); michael@0: michael@0: Message index_stream; michael@0: index_stream << "TestsReportCorrectNamesAndParameters/" << GetParam(); michael@0: EXPECT_STREQ(index_stream.GetString().c_str(), test_info->name()); michael@0: michael@0: EXPECT_EQ(::testing::PrintToString(GetParam()), test_info->value_param()); michael@0: } michael@0: michael@0: INSTANTIATE_TEST_CASE_P(ZeroToFiveSequence, NamingTest, Range(0, 5)); michael@0: michael@0: // Class that cannot be streamed into an ostream. It needs to be copyable michael@0: // (and, in case of MSVC, also assignable) in order to be a test parameter michael@0: // type. Its default copy constructor and assignment operator do exactly michael@0: // what we need. michael@0: class Unstreamable { michael@0: public: michael@0: explicit Unstreamable(int value) : value_(value) {} michael@0: michael@0: private: michael@0: int value_; michael@0: }; michael@0: michael@0: class CommentTest : public TestWithParam {}; michael@0: michael@0: TEST_P(CommentTest, TestsCorrectlyReportUnstreamableParams) { michael@0: const ::testing::TestInfo* const test_info = michael@0: ::testing::UnitTest::GetInstance()->current_test_info(); michael@0: michael@0: EXPECT_EQ(::testing::PrintToString(GetParam()), test_info->value_param()); michael@0: } michael@0: michael@0: INSTANTIATE_TEST_CASE_P(InstantiationWithComments, michael@0: CommentTest, michael@0: Values(Unstreamable(1))); michael@0: michael@0: // Verify that we can create a hierarchy of test fixtures, where the base michael@0: // class fixture is not parameterized and the derived class is. In this case michael@0: // ParameterizedDerivedTest inherits from NonParameterizedBaseTest. We michael@0: // perform simple tests on both. michael@0: class NonParameterizedBaseTest : public ::testing::Test { michael@0: public: michael@0: NonParameterizedBaseTest() : n_(17) { } michael@0: protected: michael@0: int n_; michael@0: }; michael@0: michael@0: class ParameterizedDerivedTest : public NonParameterizedBaseTest, michael@0: public ::testing::WithParamInterface { michael@0: protected: michael@0: ParameterizedDerivedTest() : count_(0) { } michael@0: int count_; michael@0: static int global_count_; michael@0: }; michael@0: michael@0: int ParameterizedDerivedTest::global_count_ = 0; michael@0: michael@0: TEST_F(NonParameterizedBaseTest, FixtureIsInitialized) { michael@0: EXPECT_EQ(17, n_); michael@0: } michael@0: michael@0: TEST_P(ParameterizedDerivedTest, SeesSequence) { michael@0: EXPECT_EQ(17, n_); michael@0: EXPECT_EQ(0, count_++); michael@0: EXPECT_EQ(GetParam(), global_count_++); michael@0: } michael@0: michael@0: INSTANTIATE_TEST_CASE_P(RangeZeroToFive, ParameterizedDerivedTest, Range(0, 5)); michael@0: michael@0: #endif // GTEST_HAS_PARAM_TEST michael@0: michael@0: TEST(CompileTest, CombineIsDefinedOnlyWhenGtestHasParamTestIsDefined) { michael@0: #if GTEST_HAS_COMBINE && !GTEST_HAS_PARAM_TEST michael@0: FAIL() << "GTEST_HAS_COMBINE is defined while GTEST_HAS_PARAM_TEST is not\n" michael@0: #endif michael@0: } michael@0: michael@0: int main(int argc, char **argv) { michael@0: #if GTEST_HAS_PARAM_TEST michael@0: // Used in TestGenerationTest test case. michael@0: AddGlobalTestEnvironment(TestGenerationTest::Environment::Instance()); michael@0: // Used in GeneratorEvaluationTest test case. Tests that the updated value michael@0: // will be picked up for instantiating tests in GeneratorEvaluationTest. michael@0: GeneratorEvaluationTest::set_param_value(1); michael@0: #endif // GTEST_HAS_PARAM_TEST michael@0: michael@0: ::testing::InitGoogleTest(&argc, argv); michael@0: michael@0: #if GTEST_HAS_PARAM_TEST michael@0: // Used in GeneratorEvaluationTest test case. Tests that value updated michael@0: // here will NOT be used for instantiating tests in michael@0: // GeneratorEvaluationTest. michael@0: GeneratorEvaluationTest::set_param_value(2); michael@0: #endif // GTEST_HAS_PARAM_TEST michael@0: michael@0: return RUN_ALL_TESTS(); michael@0: }