googletest-param-test-test.cc (googletest-release-1.11.0) | : | googletest-param-test-test.cc (googletest-release-1.12.0) | ||
---|---|---|---|---|
skipping to change at line 35 | skipping to change at line 35 | |||
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |||
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |||
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |||
// | // | |||
// Tests for Google Test itself. This file verifies that the parameter | // Tests for Google Test itself. This file verifies that the parameter | |||
// generators objects produce correct parameter sequences and that | // generators objects produce correct parameter sequences and that | |||
// Google Test runtime instantiates correct tests from those sequences. | // Google Test runtime instantiates correct tests from those sequences. | |||
#include "gtest/gtest.h" | #include "test/googletest-param-test-test.h" | |||
# include <algorithm> | #include <algorithm> | |||
# include <iostream> | #include <iostream> | |||
# include <list> | #include <list> | |||
# include <set> | #include <set> | |||
# include <sstream> | #include <sstream> | |||
# include <string> | #include <string> | |||
# include <vector> | #include <vector> | |||
# include "src/gtest-internal-inl.h" // for UnitTestOptions | #include "gtest/gtest.h" | |||
# include "test/googletest-param-test-test.h" | #include "src/gtest-internal-inl.h" // for UnitTestOptions | |||
using ::std::vector; | ||||
using ::std::sort; | using ::std::sort; | |||
using ::std::vector; | ||||
using ::testing::AddGlobalTestEnvironment; | using ::testing::AddGlobalTestEnvironment; | |||
using ::testing::Bool; | using ::testing::Bool; | |||
using ::testing::Combine; | using ::testing::Combine; | |||
using ::testing::Message; | using ::testing::Message; | |||
using ::testing::Range; | using ::testing::Range; | |||
using ::testing::TestWithParam; | using ::testing::TestWithParam; | |||
using ::testing::Values; | using ::testing::Values; | |||
using ::testing::ValuesIn; | using ::testing::ValuesIn; | |||
skipping to change at line 88 | skipping to change at line 88 | |||
void VerifyGenerator(const ParamGenerator<T>& generator, | void VerifyGenerator(const ParamGenerator<T>& generator, | |||
const T (&expected_values)[N]) { | const T (&expected_values)[N]) { | |||
typename ParamGenerator<T>::iterator it = generator.begin(); | typename ParamGenerator<T>::iterator it = generator.begin(); | |||
for (size_t i = 0; i < N; ++i) { | for (size_t i = 0; i < N; ++i) { | |||
ASSERT_FALSE(it == generator.end()) | ASSERT_FALSE(it == generator.end()) | |||
<< "At element " << i << " when accessing via an iterator " | << "At element " << i << " when accessing via an iterator " | |||
<< "created with the copy constructor.\n"; | << "created with the copy constructor.\n"; | |||
// We cannot use EXPECT_EQ() here as the values may be tuples, | // We cannot use EXPECT_EQ() here as the values may be tuples, | |||
// which don't support <<. | // which don't support <<. | |||
EXPECT_TRUE(expected_values[i] == *it) | EXPECT_TRUE(expected_values[i] == *it) | |||
<< "where i is " << i | << "where i is " << i << ", expected_values[i] is " | |||
<< ", expected_values[i] is " << PrintValue(expected_values[i]) | << PrintValue(expected_values[i]) << ", *it is " << PrintValue(*it) | |||
<< ", *it is " << PrintValue(*it) | ||||
<< ", and 'it' is an iterator created with the copy constructor.\n"; | << ", and 'it' is an iterator created with the copy constructor.\n"; | |||
++it; | ++it; | |||
} | } | |||
EXPECT_TRUE(it == generator.end()) | EXPECT_TRUE(it == generator.end()) | |||
<< "At the presumed end of sequence when accessing via an iterator " | << "At the presumed end of sequence when accessing via an iterator " | |||
<< "created with the copy constructor.\n"; | << "created with the copy constructor.\n"; | |||
// Test the iterator assignment. The following lines verify that | // Test the iterator assignment. The following lines verify that | |||
// the sequence accessed via an iterator initialized via the | // the sequence accessed via an iterator initialized via the | |||
// assignment operator (as opposed to a copy constructor) matches | // assignment operator (as opposed to a copy constructor) matches | |||
// just the same. | // just the same. | |||
it = generator.begin(); | it = generator.begin(); | |||
for (size_t i = 0; i < N; ++i) { | for (size_t i = 0; i < N; ++i) { | |||
ASSERT_FALSE(it == generator.end()) | ASSERT_FALSE(it == generator.end()) | |||
<< "At element " << i << " when accessing via an iterator " | << "At element " << i << " when accessing via an iterator " | |||
<< "created with the assignment operator.\n"; | << "created with the assignment operator.\n"; | |||
EXPECT_TRUE(expected_values[i] == *it) | EXPECT_TRUE(expected_values[i] == *it) | |||
<< "where i is " << i | << "where i is " << i << ", expected_values[i] is " | |||
<< ", expected_values[i] is " << PrintValue(expected_values[i]) | << PrintValue(expected_values[i]) << ", *it is " << PrintValue(*it) | |||
<< ", *it is " << PrintValue(*it) | ||||
<< ", and 'it' is an iterator created with the copy constructor.\n"; | << ", and 'it' is an iterator created with the copy constructor.\n"; | |||
++it; | ++it; | |||
} | } | |||
EXPECT_TRUE(it == generator.end()) | EXPECT_TRUE(it == generator.end()) | |||
<< "At the presumed end of sequence when accessing via an iterator " | << "At the presumed end of sequence when accessing via an iterator " | |||
<< "created with the assignment operator.\n"; | << "created with the assignment operator.\n"; | |||
} | } | |||
template <typename T> | template <typename T> | |||
void VerifyGeneratorIsEmpty(const ParamGenerator<T>& generator) { | void VerifyGeneratorIsEmpty(const ParamGenerator<T>& generator) { | |||
typename ParamGenerator<T>::iterator it = generator.begin(); | typename ParamGenerator<T>::iterator it = generator.begin(); | |||
EXPECT_TRUE(it == generator.end()); | EXPECT_TRUE(it == generator.end()); | |||
it = generator.begin(); | it = generator.begin(); | |||
EXPECT_TRUE(it == generator.end()); | EXPECT_TRUE(it == generator.end()); | |||
} | } | |||
skipping to change at line 219 | skipping to change at line 217 | |||
} | } | |||
// Verifies that Range works with user-defined types that define | // Verifies that Range works with user-defined types that define | |||
// copy constructor, operator=(), operator+(), and operator<(). | // copy constructor, operator=(), operator+(), and operator<(). | |||
class DogAdder { | class DogAdder { | |||
public: | public: | |||
explicit DogAdder(const char* a_value) : value_(a_value) {} | explicit DogAdder(const char* a_value) : value_(a_value) {} | |||
DogAdder(const DogAdder& other) : value_(other.value_.c_str()) {} | DogAdder(const DogAdder& other) : value_(other.value_.c_str()) {} | |||
DogAdder operator=(const DogAdder& other) { | DogAdder operator=(const DogAdder& other) { | |||
if (this != &other) | if (this != &other) value_ = other.value_; | |||
value_ = other.value_; | ||||
return *this; | return *this; | |||
} | } | |||
DogAdder operator+(const DogAdder& other) const { | DogAdder operator+(const DogAdder& other) const { | |||
Message msg; | Message msg; | |||
msg << value_.c_str() << other.value_.c_str(); | msg << value_.c_str() << other.value_.c_str(); | |||
return DogAdder(msg.GetString().c_str()); | return DogAdder(msg.GetString().c_str()); | |||
} | } | |||
bool operator<(const DogAdder& other) const { | bool operator<(const DogAdder& other) const { return value_ < other.value_; } | |||
return value_ < other.value_; | ||||
} | ||||
const std::string& value() const { return value_; } | const std::string& value() const { return value_; } | |||
private: | private: | |||
std::string value_; | std::string value_; | |||
}; | }; | |||
TEST(RangeTest, WorksWithACustomType) { | TEST(RangeTest, WorksWithACustomType) { | |||
const ParamGenerator<DogAdder> gen = | const ParamGenerator<DogAdder> gen = | |||
Range(DogAdder("cat"), DogAdder("catdogdog"), DogAdder("dog")); | Range(DogAdder("cat"), DogAdder("catdogdog"), DogAdder("dog")); | |||
ParamGenerator<DogAdder>::iterator it = gen.begin(); | ParamGenerator<DogAdder>::iterator it = gen.begin(); | |||
skipping to change at line 375 | skipping to change at line 370 | |||
// Tests that Values() generates the expected sequences from elements of | // Tests that Values() generates the expected sequences from elements of | |||
// different types convertible to ParamGenerator's parameter type. | // different types convertible to ParamGenerator's parameter type. | |||
TEST(ValuesTest, ValuesWorksForValuesOfCompatibleTypes) { | TEST(ValuesTest, ValuesWorksForValuesOfCompatibleTypes) { | |||
const ParamGenerator<double> gen = Values(3, 5.0f, 8.0); | const ParamGenerator<double> gen = Values(3, 5.0f, 8.0); | |||
const double expected_values[] = {3.0, 5.0, 8.0}; | const double expected_values[] = {3.0, 5.0, 8.0}; | |||
VerifyGenerator(gen, expected_values); | VerifyGenerator(gen, expected_values); | |||
} | } | |||
TEST(ValuesTest, ValuesWorksForMaxLengthList) { | TEST(ValuesTest, ValuesWorksForMaxLengthList) { | |||
const ParamGenerator<int> gen = Values( | const ParamGenerator<int> gen = | |||
10, 20, 30, 40, 50, 60, 70, 80, 90, 100, | Values(10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, | |||
110, 120, 130, 140, 150, 160, 170, 180, 190, 200, | 160, 170, 180, 190, 200, 210, 220, 230, 240, 250, 260, 270, 280, | |||
210, 220, 230, 240, 250, 260, 270, 280, 290, 300, | 290, 300, 310, 320, 330, 340, 350, 360, 370, 380, 390, 400, 410, | |||
310, 320, 330, 340, 350, 360, 370, 380, 390, 400, | 420, 430, 440, 450, 460, 470, 480, 490, 500); | |||
410, 420, 430, 440, 450, 460, 470, 480, 490, 500); | ||||
const int expected_values[] = { | const int expected_values[] = { | |||
10, 20, 30, 40, 50, 60, 70, 80, 90, 100, | 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, | |||
110, 120, 130, 140, 150, 160, 170, 180, 190, 200, | 140, 150, 160, 170, 180, 190, 200, 210, 220, 230, 240, 250, 260, | |||
210, 220, 230, 240, 250, 260, 270, 280, 290, 300, | 270, 280, 290, 300, 310, 320, 330, 340, 350, 360, 370, 380, 390, | |||
310, 320, 330, 340, 350, 360, 370, 380, 390, 400, | 400, 410, 420, 430, 440, 450, 460, 470, 480, 490, 500}; | |||
410, 420, 430, 440, 450, 460, 470, 480, 490, 500}; | ||||
VerifyGenerator(gen, expected_values); | VerifyGenerator(gen, expected_values); | |||
} | } | |||
// Edge case test. Tests that single-parameter Values() generates the sequence | // Edge case test. Tests that single-parameter Values() generates the sequence | |||
// with the single value. | // with the single value. | |||
TEST(ValuesTest, ValuesWithSingleParameter) { | TEST(ValuesTest, ValuesWithSingleParameter) { | |||
const ParamGenerator<int> gen = Values(42); | const ParamGenerator<int> gen = Values(42); | |||
const int expected_values[] = {42}; | const int expected_values[] = {42}; | |||
VerifyGenerator(gen, expected_values); | VerifyGenerator(gen, expected_values); | |||
skipping to change at line 575 | skipping to change at line 568 | |||
void TearDown() override { | void TearDown() override { | |||
// If all MultipleTestGenerationTest tests have been de-selected | // If all MultipleTestGenerationTest tests have been de-selected | |||
// by the filter flag, the following checks make no sense. | // by the filter flag, the following checks make no sense. | |||
bool perform_check = false; | bool perform_check = false; | |||
for (int i = 0; i < kExpectedCalls; ++i) { | for (int i = 0; i < kExpectedCalls; ++i) { | |||
Message msg; | Message msg; | |||
msg << "TestsExpandedAndRun/" << i; | msg << "TestsExpandedAndRun/" << i; | |||
if (UnitTestOptions::FilterMatchesTest( | if (UnitTestOptions::FilterMatchesTest( | |||
"TestExpansionModule/MultipleTestGenerationTest", | "TestExpansionModule/MultipleTestGenerationTest", | |||
msg.GetString().c_str())) { | msg.GetString().c_str())) { | |||
perform_check = true; | perform_check = true; | |||
} | } | |||
} | } | |||
if (perform_check) { | if (perform_check) { | |||
EXPECT_EQ(kExpectedCalls, fixture_constructor_count_) | EXPECT_EQ(kExpectedCalls, fixture_constructor_count_) | |||
<< "Fixture constructor of ParamTestGenerationTest test case " | << "Fixture constructor of ParamTestGenerationTest test case " | |||
<< "has not been run as expected."; | << "has not been run as expected."; | |||
EXPECT_EQ(kExpectedCalls, set_up_count_) | EXPECT_EQ(kExpectedCalls, set_up_count_) | |||
<< "Fixture SetUp method of ParamTestGenerationTest test case " | << "Fixture SetUp method of ParamTestGenerationTest test case " | |||
skipping to change at line 597 | skipping to change at line 590 | |||
EXPECT_EQ(kExpectedCalls, tear_down_count_) | EXPECT_EQ(kExpectedCalls, tear_down_count_) | |||
<< "Fixture TearDown method of ParamTestGenerationTest test case " | << "Fixture TearDown method of ParamTestGenerationTest test case " | |||
<< "has not been run as expected."; | << "has not been run as expected."; | |||
EXPECT_EQ(kExpectedCalls, test_body_count_) | EXPECT_EQ(kExpectedCalls, test_body_count_) | |||
<< "Test in ParamTestGenerationTest test case " | << "Test in ParamTestGenerationTest test case " | |||
<< "has not been run as expected."; | << "has not been run as expected."; | |||
} | } | |||
} | } | |||
private: | private: | |||
TestGenerationEnvironment() : fixture_constructor_count_(0), set_up_count_(0), | TestGenerationEnvironment() | |||
tear_down_count_(0), test_body_count_(0) {} | : fixture_constructor_count_(0), | |||
set_up_count_(0), | ||||
tear_down_count_(0), | ||||
test_body_count_(0) {} | ||||
int fixture_constructor_count_; | int fixture_constructor_count_; | |||
int set_up_count_; | int set_up_count_; | |||
int tear_down_count_; | int tear_down_count_; | |||
int test_body_count_; | int test_body_count_; | |||
GTEST_DISALLOW_COPY_AND_ASSIGN_(TestGenerationEnvironment); | TestGenerationEnvironment(const TestGenerationEnvironment&) = delete; | |||
TestGenerationEnvironment& operator=(const TestGenerationEnvironment&) = | ||||
delete; | ||||
}; | }; | |||
const int test_generation_params[] = {36, 42, 72}; | const int test_generation_params[] = {36, 42, 72}; | |||
class TestGenerationTest : public TestWithParam<int> { | class TestGenerationTest : public TestWithParam<int> { | |||
public: | public: | |||
enum { | enum { | |||
PARAMETER_COUNT = | PARAMETER_COUNT = | |||
sizeof(test_generation_params)/sizeof(test_generation_params[0]) | sizeof(test_generation_params) / sizeof(test_generation_params[0]) | |||
}; | }; | |||
typedef TestGenerationEnvironment<PARAMETER_COUNT> Environment; | typedef TestGenerationEnvironment<PARAMETER_COUNT> Environment; | |||
TestGenerationTest() { | TestGenerationTest() { | |||
Environment::Instance()->FixtureConstructorExecuted(); | Environment::Instance()->FixtureConstructorExecuted(); | |||
current_parameter_ = GetParam(); | current_parameter_ = GetParam(); | |||
} | } | |||
void SetUp() override { | void SetUp() override { | |||
Environment::Instance()->SetUpExecuted(); | Environment::Instance()->SetUpExecuted(); | |||
skipping to change at line 638 | skipping to change at line 636 | |||
Environment::Instance()->TearDownExecuted(); | Environment::Instance()->TearDownExecuted(); | |||
EXPECT_EQ(current_parameter_, GetParam()); | EXPECT_EQ(current_parameter_, GetParam()); | |||
} | } | |||
static void SetUpTestSuite() { | static void SetUpTestSuite() { | |||
bool all_tests_in_test_case_selected = true; | bool all_tests_in_test_case_selected = true; | |||
for (int i = 0; i < PARAMETER_COUNT; ++i) { | for (int i = 0; i < PARAMETER_COUNT; ++i) { | |||
Message test_name; | Message test_name; | |||
test_name << "TestsExpandedAndRun/" << i; | test_name << "TestsExpandedAndRun/" << i; | |||
if ( !UnitTestOptions::FilterMatchesTest( | if (!UnitTestOptions::FilterMatchesTest( | |||
"TestExpansionModule/MultipleTestGenerationTest", | "TestExpansionModule/MultipleTestGenerationTest", | |||
test_name.GetString())) { | test_name.GetString())) { | |||
all_tests_in_test_case_selected = false; | all_tests_in_test_case_selected = false; | |||
} | } | |||
} | } | |||
EXPECT_TRUE(all_tests_in_test_case_selected) | EXPECT_TRUE(all_tests_in_test_case_selected) | |||
<< "When running the TestGenerationTest test case all of its tests\n" | << "When running the TestGenerationTest test case all of its tests\n" | |||
<< "must be selected by the filter flag for the test case to pass.\n" | << "must be selected by the filter flag for the test case to pass.\n" | |||
<< "If not all of them are enabled, we can't reliably conclude\n" | << "If not all of them are enabled, we can't reliably conclude\n" | |||
<< "that the correct number of tests have been generated."; | << "that the correct number of tests have been generated."; | |||
collected_parameters_.clear(); | collected_parameters_.clear(); | |||
skipping to change at line 670 | skipping to change at line 668 | |||
sort(collected_parameters_.begin(), collected_parameters_.end()); | sort(collected_parameters_.begin(), collected_parameters_.end()); | |||
EXPECT_TRUE(collected_parameters_ == expected_values); | EXPECT_TRUE(collected_parameters_ == expected_values); | |||
} | } | |||
protected: | protected: | |||
int current_parameter_; | int current_parameter_; | |||
static vector<int> collected_parameters_; | static vector<int> collected_parameters_; | |||
private: | private: | |||
GTEST_DISALLOW_COPY_AND_ASSIGN_(TestGenerationTest); | TestGenerationTest(const TestGenerationTest&) = delete; | |||
TestGenerationTest& operator=(const TestGenerationTest&) = delete; | ||||
}; | }; | |||
vector<int> TestGenerationTest::collected_parameters_; | vector<int> TestGenerationTest::collected_parameters_; | |||
TEST_P(TestGenerationTest, TestsExpandedAndRun) { | TEST_P(TestGenerationTest, TestsExpandedAndRun) { | |||
Environment::Instance()->TestBodyExecuted(); | Environment::Instance()->TestBodyExecuted(); | |||
EXPECT_EQ(current_parameter_, GetParam()); | EXPECT_EQ(current_parameter_, GetParam()); | |||
collected_parameters_.push_back(GetParam()); | collected_parameters_.push_back(GetParam()); | |||
} | } | |||
INSTANTIATE_TEST_SUITE_P(TestExpansionModule, TestGenerationTest, | INSTANTIATE_TEST_SUITE_P(TestExpansionModule, TestGenerationTest, | |||
ValuesIn(test_generation_params)); | ValuesIn(test_generation_params)); | |||
skipping to change at line 731 | skipping to change at line 730 | |||
// unit and instantiated in another. This test will be instantiated in | // unit and instantiated in another. This test will be instantiated in | |||
// gtest-param-test_test2.cc. ExternalInstantiationTest fixture class is | // gtest-param-test_test2.cc. ExternalInstantiationTest fixture class is | |||
// defined in gtest-param-test_test.h. | // defined in gtest-param-test_test.h. | |||
TEST_P(ExternalInstantiationTest, IsMultipleOf33) { | TEST_P(ExternalInstantiationTest, IsMultipleOf33) { | |||
EXPECT_EQ(0, GetParam() % 33); | EXPECT_EQ(0, GetParam() % 33); | |||
} | } | |||
// Tests that a parameterized test case can be instantiated with multiple | // Tests that a parameterized test case can be instantiated with multiple | |||
// generators. | // generators. | |||
class MultipleInstantiationTest : public TestWithParam<int> {}; | class MultipleInstantiationTest : public TestWithParam<int> {}; | |||
TEST_P(MultipleInstantiationTest, AllowsMultipleInstances) { | TEST_P(MultipleInstantiationTest, AllowsMultipleInstances) {} | |||
} | ||||
INSTANTIATE_TEST_SUITE_P(Sequence1, MultipleInstantiationTest, Values(1, 2)); | INSTANTIATE_TEST_SUITE_P(Sequence1, MultipleInstantiationTest, Values(1, 2)); | |||
INSTANTIATE_TEST_SUITE_P(Sequence2, MultipleInstantiationTest, Range(3, 5)); | INSTANTIATE_TEST_SUITE_P(Sequence2, MultipleInstantiationTest, Range(3, 5)); | |||
// Tests that a parameterized test case can be instantiated | // Tests that a parameterized test case can be instantiated | |||
// in multiple translation units. This test will be instantiated | // in multiple translation units. This test will be instantiated | |||
// here and in gtest-param-test_test2.cc. | // here and in gtest-param-test_test2.cc. | |||
// InstantiationInMultipleTranslationUnitsTest fixture class | // InstantiationInMultipleTranslationUnitsTest fixture class | |||
// is defined in gtest-param-test_test.h. | // is defined in gtest-param-test_test.h. | |||
TEST_P(InstantiationInMultipleTranslationUnitsTest, IsMultipleOf42) { | TEST_P(InstantiationInMultipleTranslationUnitsTest, IsMultipleOf42) { | |||
EXPECT_EQ(0, GetParam() % 42); | EXPECT_EQ(0, GetParam() % 42); | |||
skipping to change at line 782 | skipping to change at line 780 | |||
// Tests that all instantiations of a test have named appropriately. Test | // Tests that all instantiations of a test have named appropriately. Test | |||
// defined with TEST_P(TestSuiteName, TestName) and instantiated with | // defined with TEST_P(TestSuiteName, TestName) and instantiated with | |||
// INSTANTIATE_TEST_SUITE_P(SequenceName, TestSuiteName, generator) must be | // INSTANTIATE_TEST_SUITE_P(SequenceName, TestSuiteName, generator) must be | |||
// named SequenceName/TestSuiteName.TestName/i, where i is the 0-based index of | // named SequenceName/TestSuiteName.TestName/i, where i is the 0-based index of | |||
// the sequence element used to instantiate the test. | // the sequence element used to instantiate the test. | |||
class NamingTest : public TestWithParam<int> {}; | class NamingTest : public TestWithParam<int> {}; | |||
TEST_P(NamingTest, TestsReportCorrectNamesAndParameters) { | TEST_P(NamingTest, TestsReportCorrectNamesAndParameters) { | |||
const ::testing::TestInfo* const test_info = | const ::testing::TestInfo* const test_info = | |||
::testing::UnitTest::GetInstance()->current_test_info(); | ::testing::UnitTest::GetInstance()->current_test_info(); | |||
EXPECT_STREQ("ZeroToFiveSequence/NamingTest", test_info->test_suite_name()); | EXPECT_STREQ("ZeroToFiveSequence/NamingTest", test_info->test_suite_name()); | |||
Message index_stream; | Message index_stream; | |||
index_stream << "TestsReportCorrectNamesAndParameters/" << GetParam(); | index_stream << "TestsReportCorrectNamesAndParameters/" << GetParam(); | |||
EXPECT_STREQ(index_stream.GetString().c_str(), test_info->name()); | EXPECT_STREQ(index_stream.GetString().c_str(), test_info->name()); | |||
EXPECT_EQ(::testing::PrintToString(GetParam()), test_info->value_param()); | EXPECT_EQ(::testing::PrintToString(GetParam()), test_info->value_param()); | |||
} | } | |||
INSTANTIATE_TEST_SUITE_P(ZeroToFiveSequence, NamingTest, Range(0, 5)); | INSTANTIATE_TEST_SUITE_P(ZeroToFiveSequence, NamingTest, Range(0, 5)); | |||
// Tests that macros in test names are expanded correctly. | // Tests that macros in test names are expanded correctly. | |||
class MacroNamingTest : public TestWithParam<int> {}; | class MacroNamingTest : public TestWithParam<int> {}; | |||
#define PREFIX_WITH_FOO(test_name) Foo##test_name | #define PREFIX_WITH_FOO(test_name) Foo##test_name | |||
#define PREFIX_WITH_MACRO(test_name) Macro##test_name | #define PREFIX_WITH_MACRO(test_name) Macro##test_name | |||
TEST_P(PREFIX_WITH_MACRO(NamingTest), PREFIX_WITH_FOO(SomeTestName)) { | TEST_P(PREFIX_WITH_MACRO(NamingTest), PREFIX_WITH_FOO(SomeTestName)) { | |||
const ::testing::TestInfo* const test_info = | const ::testing::TestInfo* const test_info = | |||
::testing::UnitTest::GetInstance()->current_test_info(); | ::testing::UnitTest::GetInstance()->current_test_info(); | |||
EXPECT_STREQ("FortyTwo/MacroNamingTest", test_info->test_suite_name()); | EXPECT_STREQ("FortyTwo/MacroNamingTest", test_info->test_suite_name()); | |||
EXPECT_STREQ("FooSomeTestName/0", test_info->name()); | EXPECT_STREQ("FooSomeTestName/0", test_info->name()); | |||
} | } | |||
INSTANTIATE_TEST_SUITE_P(FortyTwo, MacroNamingTest, Values(42)); | INSTANTIATE_TEST_SUITE_P(FortyTwo, MacroNamingTest, Values(42)); | |||
// Tests the same thing for non-parametrized tests. | // Tests the same thing for non-parametrized tests. | |||
class MacroNamingTestNonParametrized : public ::testing::Test {}; | class MacroNamingTestNonParametrized : public ::testing::Test {}; | |||
TEST_F(PREFIX_WITH_MACRO(NamingTestNonParametrized), | TEST_F(PREFIX_WITH_MACRO(NamingTestNonParametrized), | |||
PREFIX_WITH_FOO(SomeTestName)) { | PREFIX_WITH_FOO(SomeTestName)) { | |||
const ::testing::TestInfo* const test_info = | const ::testing::TestInfo* const test_info = | |||
::testing::UnitTest::GetInstance()->current_test_info(); | ::testing::UnitTest::GetInstance()->current_test_info(); | |||
EXPECT_STREQ("MacroNamingTestNonParametrized", test_info->test_suite_name()); | EXPECT_STREQ("MacroNamingTestNonParametrized", test_info->test_suite_name()); | |||
EXPECT_STREQ("FooSomeTestName", test_info->name()); | EXPECT_STREQ("FooSomeTestName", test_info->name()); | |||
} | } | |||
TEST(MacroNameing, LookupNames) { | TEST(MacroNameing, LookupNames) { | |||
std::set<std::string> know_suite_names, know_test_names; | std::set<std::string> know_suite_names, know_test_names; | |||
auto ins = testing::UnitTest::GetInstance(); | auto ins = testing::UnitTest::GetInstance(); | |||
int ts = 0; | int ts = 0; | |||
skipping to change at line 841 | skipping to change at line 839 | |||
int ti = 0; | int ti = 0; | |||
while (const testing::TestInfo* info = suite->GetTestInfo(ti++)) { | while (const testing::TestInfo* info = suite->GetTestInfo(ti++)) { | |||
know_test_names.insert(std::string(suite->name()) + "." + info->name()); | know_test_names.insert(std::string(suite->name()) + "." + info->name()); | |||
} | } | |||
} | } | |||
// Check that the expected form of the test suit name actually exists. | // Check that the expected form of the test suit name actually exists. | |||
EXPECT_NE( // | EXPECT_NE( // | |||
know_suite_names.find("FortyTwo/MacroNamingTest"), | know_suite_names.find("FortyTwo/MacroNamingTest"), | |||
know_suite_names.end()); | know_suite_names.end()); | |||
EXPECT_NE( | EXPECT_NE(know_suite_names.find("MacroNamingTestNonParametrized"), | |||
know_suite_names.find("MacroNamingTestNonParametrized"), | know_suite_names.end()); | |||
know_suite_names.end()); | ||||
// Check that the expected form of the test name actually exists. | // Check that the expected form of the test name actually exists. | |||
EXPECT_NE( // | EXPECT_NE( // | |||
know_test_names.find("FortyTwo/MacroNamingTest.FooSomeTestName/0"), | know_test_names.find("FortyTwo/MacroNamingTest.FooSomeTestName/0"), | |||
know_test_names.end()); | know_test_names.end()); | |||
EXPECT_NE( | EXPECT_NE( | |||
know_test_names.find("MacroNamingTestNonParametrized.FooSomeTestName"), | know_test_names.find("MacroNamingTestNonParametrized.FooSomeTestName"), | |||
know_test_names.end()); | know_test_names.end()); | |||
} | } | |||
// Tests that user supplied custom parameter names are working correctly. | // Tests that user supplied custom parameter names are working correctly. | |||
skipping to change at line 926 | skipping to change at line 923 | |||
EXPECT_EQ(1u, test_names.count("CustomTestNames/FunctionNameP")); | EXPECT_EQ(1u, test_names.count("CustomTestNames/FunctionNameP")); | |||
EXPECT_EQ(1u, test_names.count("CustomTestNames/LambdaName")); | EXPECT_EQ(1u, test_names.count("CustomTestNames/LambdaName")); | |||
} | } | |||
// Test a numeric name to ensure PrintToStringParamName works correctly. | // Test a numeric name to ensure PrintToStringParamName works correctly. | |||
class CustomIntegerNamingTest : public TestWithParam<int> {}; | class CustomIntegerNamingTest : public TestWithParam<int> {}; | |||
TEST_P(CustomIntegerNamingTest, TestsReportCorrectNames) { | TEST_P(CustomIntegerNamingTest, TestsReportCorrectNames) { | |||
const ::testing::TestInfo* const test_info = | const ::testing::TestInfo* const test_info = | |||
::testing::UnitTest::GetInstance()->current_test_info(); | ::testing::UnitTest::GetInstance()->current_test_info(); | |||
Message test_name_stream; | Message test_name_stream; | |||
test_name_stream << "TestsReportCorrectNames/" << GetParam(); | test_name_stream << "TestsReportCorrectNames/" << GetParam(); | |||
EXPECT_STREQ(test_name_stream.GetString().c_str(), test_info->name()); | EXPECT_STREQ(test_name_stream.GetString().c_str(), test_info->name()); | |||
} | } | |||
INSTANTIATE_TEST_SUITE_P(PrintToString, CustomIntegerNamingTest, Range(0, 5), | INSTANTIATE_TEST_SUITE_P(PrintToString, CustomIntegerNamingTest, Range(0, 5), | |||
::testing::PrintToStringParamName()); | ::testing::PrintToStringParamName()); | |||
// Test a custom struct with PrintToString. | // Test a custom struct with PrintToString. | |||
skipping to change at line 951 | skipping to change at line 948 | |||
std::ostream& operator<<(std::ostream& stream, const CustomStruct& val) { | std::ostream& operator<<(std::ostream& stream, const CustomStruct& val) { | |||
stream << val.x; | stream << val.x; | |||
return stream; | return stream; | |||
} | } | |||
class CustomStructNamingTest : public TestWithParam<CustomStruct> {}; | class CustomStructNamingTest : public TestWithParam<CustomStruct> {}; | |||
TEST_P(CustomStructNamingTest, TestsReportCorrectNames) { | TEST_P(CustomStructNamingTest, TestsReportCorrectNames) { | |||
const ::testing::TestInfo* const test_info = | const ::testing::TestInfo* const test_info = | |||
::testing::UnitTest::GetInstance()->current_test_info(); | ::testing::UnitTest::GetInstance()->current_test_info(); | |||
Message test_name_stream; | Message test_name_stream; | |||
test_name_stream << "TestsReportCorrectNames/" << GetParam(); | test_name_stream << "TestsReportCorrectNames/" << GetParam(); | |||
EXPECT_STREQ(test_name_stream.GetString().c_str(), test_info->name()); | EXPECT_STREQ(test_name_stream.GetString().c_str(), test_info->name()); | |||
} | } | |||
INSTANTIATE_TEST_SUITE_P(PrintToString, CustomStructNamingTest, | INSTANTIATE_TEST_SUITE_P(PrintToString, CustomStructNamingTest, | |||
Values(CustomStruct(0), CustomStruct(1)), | Values(CustomStruct(0), CustomStruct(1)), | |||
::testing::PrintToStringParamName()); | ::testing::PrintToStringParamName()); | |||
// Test that using a stateful parameter naming function works as expected. | // Test that using a stateful parameter naming function works as expected. | |||
skipping to change at line 981 | skipping to change at line 978 | |||
}; | }; | |||
class StatefulNamingTest : public ::testing::TestWithParam<int> { | class StatefulNamingTest : public ::testing::TestWithParam<int> { | |||
protected: | protected: | |||
StatefulNamingTest() : sum_(0) {} | StatefulNamingTest() : sum_(0) {} | |||
int sum_; | int sum_; | |||
}; | }; | |||
TEST_P(StatefulNamingTest, TestsReportCorrectNames) { | TEST_P(StatefulNamingTest, TestsReportCorrectNames) { | |||
const ::testing::TestInfo* const test_info = | const ::testing::TestInfo* const test_info = | |||
::testing::UnitTest::GetInstance()->current_test_info(); | ::testing::UnitTest::GetInstance()->current_test_info(); | |||
sum_ += GetParam(); | sum_ += GetParam(); | |||
Message test_name_stream; | Message test_name_stream; | |||
test_name_stream << "TestsReportCorrectNames/" << sum_; | test_name_stream << "TestsReportCorrectNames/" << sum_; | |||
EXPECT_STREQ(test_name_stream.GetString().c_str(), test_info->name()); | EXPECT_STREQ(test_name_stream.GetString().c_str(), test_info->name()); | |||
} | } | |||
INSTANTIATE_TEST_SUITE_P(StatefulNamingFunctor, StatefulNamingTest, Range(0, 5), | INSTANTIATE_TEST_SUITE_P(StatefulNamingFunctor, StatefulNamingTest, Range(0, 5), | |||
StatefulNamingFunctor()); | StatefulNamingFunctor()); | |||
// Class that cannot be streamed into an ostream. It needs to be copyable | // Class that cannot be streamed into an ostream. It needs to be copyable | |||
skipping to change at line 1009 | skipping to change at line 1006 | |||
const int& dummy_value() const { return value_; } | const int& dummy_value() const { return value_; } | |||
private: | private: | |||
int value_; | int value_; | |||
}; | }; | |||
class CommentTest : public TestWithParam<Unstreamable> {}; | class CommentTest : public TestWithParam<Unstreamable> {}; | |||
TEST_P(CommentTest, TestsCorrectlyReportUnstreamableParams) { | TEST_P(CommentTest, TestsCorrectlyReportUnstreamableParams) { | |||
const ::testing::TestInfo* const test_info = | const ::testing::TestInfo* const test_info = | |||
::testing::UnitTest::GetInstance()->current_test_info(); | ::testing::UnitTest::GetInstance()->current_test_info(); | |||
EXPECT_EQ(::testing::PrintToString(GetParam()), test_info->value_param()); | EXPECT_EQ(::testing::PrintToString(GetParam()), test_info->value_param()); | |||
} | } | |||
INSTANTIATE_TEST_SUITE_P(InstantiationWithComments, CommentTest, | INSTANTIATE_TEST_SUITE_P(InstantiationWithComments, CommentTest, | |||
Values(Unstreamable(1))); | Values(Unstreamable(1))); | |||
// Verify that we can create a hierarchy of test fixtures, where the base | // Verify that we can create a hierarchy of test fixtures, where the base | |||
// class fixture is not parameterized and the derived class is. In this case | // class fixture is not parameterized and the derived class is. In this case | |||
// ParameterizedDerivedTest inherits from NonParameterizedBaseTest. We | // ParameterizedDerivedTest inherits from NonParameterizedBaseTest. We | |||
// perform simple tests on both. | // perform simple tests on both. | |||
class NonParameterizedBaseTest : public ::testing::Test { | class NonParameterizedBaseTest : public ::testing::Test { | |||
public: | public: | |||
NonParameterizedBaseTest() : n_(17) { } | NonParameterizedBaseTest() : n_(17) {} | |||
protected: | protected: | |||
int n_; | int n_; | |||
}; | }; | |||
class ParameterizedDerivedTest : public NonParameterizedBaseTest, | class ParameterizedDerivedTest : public NonParameterizedBaseTest, | |||
public ::testing::WithParamInterface<int> { | public ::testing::WithParamInterface<int> { | |||
protected: | protected: | |||
ParameterizedDerivedTest() : count_(0) { } | ParameterizedDerivedTest() : count_(0) {} | |||
int count_; | int count_; | |||
static int global_count_; | static int global_count_; | |||
}; | }; | |||
int ParameterizedDerivedTest::global_count_ = 0; | int ParameterizedDerivedTest::global_count_ = 0; | |||
TEST_F(NonParameterizedBaseTest, FixtureIsInitialized) { | TEST_F(NonParameterizedBaseTest, FixtureIsInitialized) { EXPECT_EQ(17, n_); } | |||
EXPECT_EQ(17, n_); | ||||
} | ||||
TEST_P(ParameterizedDerivedTest, SeesSequence) { | TEST_P(ParameterizedDerivedTest, SeesSequence) { | |||
EXPECT_EQ(17, n_); | EXPECT_EQ(17, n_); | |||
EXPECT_EQ(0, count_++); | EXPECT_EQ(0, count_++); | |||
EXPECT_EQ(GetParam(), global_count_++); | EXPECT_EQ(GetParam(), global_count_++); | |||
} | } | |||
class ParameterizedDeathTest : public ::testing::TestWithParam<int> { }; | class ParameterizedDeathTest : public ::testing::TestWithParam<int> {}; | |||
TEST_F(ParameterizedDeathTest, GetParamDiesFromTestF) { | TEST_F(ParameterizedDeathTest, GetParamDiesFromTestF) { | |||
EXPECT_DEATH_IF_SUPPORTED(GetParam(), | EXPECT_DEATH_IF_SUPPORTED(GetParam(), ".* value-parameterized test .*"); | |||
".* value-parameterized test .*"); | ||||
} | } | |||
INSTANTIATE_TEST_SUITE_P(RangeZeroToFive, ParameterizedDerivedTest, | INSTANTIATE_TEST_SUITE_P(RangeZeroToFive, ParameterizedDerivedTest, | |||
Range(0, 5)); | Range(0, 5)); | |||
// Tests param generator working with Enums | // Tests param generator working with Enums | |||
enum MyEnums { | enum MyEnums { | |||
ENUM1 = 1, | ENUM1 = 1, | |||
ENUM2 = 3, | ENUM2 = 3, | |||
ENUM3 = 8, | ENUM3 = 8, | |||
skipping to change at line 1086 | skipping to change at line 1081 | |||
// Never used not instantiated, this should work. | // Never used not instantiated, this should work. | |||
template <typename T> | template <typename T> | |||
class NotUsedTypeTest : public testing::Test {}; | class NotUsedTypeTest : public testing::Test {}; | |||
TYPED_TEST_SUITE_P(NotUsedTypeTest); | TYPED_TEST_SUITE_P(NotUsedTypeTest); | |||
// Used but not instantiated, this would fail. but... | // Used but not instantiated, this would fail. but... | |||
class NotInstantiatedTest : public testing::TestWithParam<int> {}; | class NotInstantiatedTest : public testing::TestWithParam<int> {}; | |||
// ... we mark is as allowed. | // ... we mark is as allowed. | |||
GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(NotInstantiatedTest); | GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(NotInstantiatedTest); | |||
TEST_P(NotInstantiatedTest, Used) { } | TEST_P(NotInstantiatedTest, Used) {} | |||
using OtherName = NotInstantiatedTest; | using OtherName = NotInstantiatedTest; | |||
GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(OtherName); | GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(OtherName); | |||
TEST_P(OtherName, Used) { } | TEST_P(OtherName, Used) {} | |||
// Used but not instantiated, this would fail. but... | // Used but not instantiated, this would fail. but... | |||
template <typename T> | template <typename T> | |||
class NotInstantiatedTypeTest : public testing::Test {}; | class NotInstantiatedTypeTest : public testing::Test {}; | |||
TYPED_TEST_SUITE_P(NotInstantiatedTypeTest); | TYPED_TEST_SUITE_P(NotInstantiatedTypeTest); | |||
// ... we mark is as allowed. | // ... we mark is as allowed. | |||
GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(NotInstantiatedTypeTest); | GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(NotInstantiatedTypeTest); | |||
TYPED_TEST_P(NotInstantiatedTypeTest, Used) { } | TYPED_TEST_P(NotInstantiatedTypeTest, Used) {} | |||
REGISTER_TYPED_TEST_SUITE_P(NotInstantiatedTypeTest, Used); | REGISTER_TYPED_TEST_SUITE_P(NotInstantiatedTypeTest, Used); | |||
} // namespace works_here | } // namespace works_here | |||
int main(int argc, char **argv) { | int main(int argc, char** argv) { | |||
// Used in TestGenerationTest test suite. | // Used in TestGenerationTest test suite. | |||
AddGlobalTestEnvironment(TestGenerationTest::Environment::Instance()); | AddGlobalTestEnvironment(TestGenerationTest::Environment::Instance()); | |||
// Used in GeneratorEvaluationTest test suite. Tests that the updated value | // Used in GeneratorEvaluationTest test suite. Tests that the updated value | |||
// will be picked up for instantiating tests in GeneratorEvaluationTest. | // will be picked up for instantiating tests in GeneratorEvaluationTest. | |||
GeneratorEvaluationTest::set_param_value(1); | GeneratorEvaluationTest::set_param_value(1); | |||
::testing::InitGoogleTest(&argc, argv); | ::testing::InitGoogleTest(&argc, argv); | |||
// Used in GeneratorEvaluationTest test suite. Tests that value updated | // Used in GeneratorEvaluationTest test suite. Tests that value updated | |||
// here will NOT be used for instantiating tests in | // here will NOT be used for instantiating tests in | |||
End of changes. 37 change blocks. | ||||
70 lines changed or deleted | 65 lines changed or added |