diff options
Diffstat (limited to 'googletest/test/googletest-param-test-test.cc')
-rw-r--r-- | googletest/test/googletest-param-test-test.cc | 218 |
1 files changed, 136 insertions, 82 deletions
diff --git a/googletest/test/googletest-param-test-test.cc b/googletest/test/googletest-param-test-test.cc index 023aa46d..c9c5e78e 100644 --- a/googletest/test/googletest-param-test-test.cc +++ b/googletest/test/googletest-param-test-test.cc @@ -32,25 +32,27 @@ // generators objects produce correct parameter sequences and that // Google Test runtime instantiates correct tests from those sequences. -#include "gtest/gtest.h" +#include "test/googletest-param-test-test.h" -# include <algorithm> -# include <iostream> -# include <list> -# include <set> -# include <sstream> -# include <string> -# include <vector> +#include <algorithm> +#include <iostream> +#include <list> +#include <set> +#include <sstream> +#include <string> +#include <tuple> +#include <vector> -# include "src/gtest-internal-inl.h" // for UnitTestOptions -# include "test/googletest-param-test-test.h" +#include "gtest/gtest.h" +#include "src/gtest-internal-inl.h" // for UnitTestOptions -using ::std::vector; using ::std::sort; +using ::std::vector; using ::testing::AddGlobalTestEnvironment; using ::testing::Bool; using ::testing::Combine; +using ::testing::ConvertGenerator; using ::testing::Message; using ::testing::Range; using ::testing::TestWithParam; @@ -85,15 +87,14 @@ void VerifyGenerator(const ParamGenerator<T>& generator, // We cannot use EXPECT_EQ() here as the values may be tuples, // which don't support <<. EXPECT_TRUE(expected_values[i] == *it) - << "where i is " << i - << ", expected_values[i] is " << PrintValue(expected_values[i]) - << ", *it is " << PrintValue(*it) + << "where i is " << i << ", expected_values[i] is " + << PrintValue(expected_values[i]) << ", *it is " << PrintValue(*it) << ", and 'it' is an iterator created with the copy constructor.\n"; ++it; } EXPECT_TRUE(it == generator.end()) - << "At the presumed end of sequence when accessing via an iterator " - << "created with the copy constructor.\n"; + << "At the presumed end of sequence when accessing via an iterator " + << "created with the copy constructor.\n"; // Test the iterator assignment. The following lines verify that // the sequence accessed via an iterator initialized via the @@ -105,15 +106,14 @@ void VerifyGenerator(const ParamGenerator<T>& generator, << "At element " << i << " when accessing via an iterator " << "created with the assignment operator.\n"; EXPECT_TRUE(expected_values[i] == *it) - << "where i is " << i - << ", expected_values[i] is " << PrintValue(expected_values[i]) - << ", *it is " << PrintValue(*it) + << "where i is " << i << ", expected_values[i] is " + << PrintValue(expected_values[i]) << ", *it is " << PrintValue(*it) << ", and 'it' is an iterator created with the copy constructor.\n"; ++it; } EXPECT_TRUE(it == generator.end()) - << "At the presumed end of sequence when accessing via an iterator " - << "created with the assignment operator.\n"; + << "At the presumed end of sequence when accessing via an iterator " + << "created with the assignment operator.\n"; } template <typename T> @@ -216,18 +216,15 @@ class DogAdder { DogAdder(const DogAdder& other) : value_(other.value_.c_str()) {} DogAdder operator=(const DogAdder& other) { - if (this != &other) - value_ = other.value_; + if (this != &other) value_ = other.value_; return *this; } DogAdder operator+(const DogAdder& other) const { Message msg; - msg << value_.c_str() << other.value_.c_str(); + msg << value_ << other.value_; return DogAdder(msg.GetString().c_str()); } - bool operator<(const DogAdder& other) const { - return value_ < other.value_; - } + bool operator<(const DogAdder& other) const { return value_ < other.value_; } const std::string& value() const { return value_; } private: @@ -372,19 +369,17 @@ TEST(ValuesTest, ValuesWorksForValuesOfCompatibleTypes) { } TEST(ValuesTest, ValuesWorksForMaxLengthList) { - const ParamGenerator<int> gen = Values( - 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, - 110, 120, 130, 140, 150, 160, 170, 180, 190, 200, - 210, 220, 230, 240, 250, 260, 270, 280, 290, 300, - 310, 320, 330, 340, 350, 360, 370, 380, 390, 400, - 410, 420, 430, 440, 450, 460, 470, 480, 490, 500); + const ParamGenerator<int> gen = + Values(10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, + 160, 170, 180, 190, 200, 210, 220, 230, 240, 250, 260, 270, 280, + 290, 300, 310, 320, 330, 340, 350, 360, 370, 380, 390, 400, 410, + 420, 430, 440, 450, 460, 470, 480, 490, 500); const int expected_values[] = { - 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, - 110, 120, 130, 140, 150, 160, 170, 180, 190, 200, - 210, 220, 230, 240, 250, 260, 270, 280, 290, 300, - 310, 320, 330, 340, 350, 360, 370, 380, 390, 400, - 410, 420, 430, 440, 450, 460, 470, 480, 490, 500}; + 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, + 140, 150, 160, 170, 180, 190, 200, 210, 220, 230, 240, 250, 260, + 270, 280, 290, 300, 310, 320, 330, 340, 350, 360, 370, 380, 390, + 400, 410, 420, 430, 440, 450, 460, 470, 480, 490, 500}; VerifyGenerator(gen, expected_values); } @@ -409,7 +404,7 @@ TEST(BoolTest, BoolWorks) { TEST(CombineTest, CombineWithTwoParameters) { const char* foo = "foo"; const char* bar = "bar"; - const ParamGenerator<std::tuple<const char*, int> > gen = + const ParamGenerator<std::tuple<const char*, int>> gen = Combine(Values(foo, bar), Values(3, 4)); std::tuple<const char*, int> expected_values[] = { @@ -420,7 +415,7 @@ TEST(CombineTest, CombineWithTwoParameters) { // Tests that Combine() with three parameters generates the expected sequence. TEST(CombineTest, CombineWithThreeParameters) { - const ParamGenerator<std::tuple<int, int, int> > gen = + const ParamGenerator<std::tuple<int, int, int>> gen = Combine(Values(0, 1), Values(3, 4), Values(5, 6)); std::tuple<int, int, int> expected_values[] = { std::make_tuple(0, 3, 5), std::make_tuple(0, 3, 6), @@ -434,7 +429,7 @@ TEST(CombineTest, CombineWithThreeParameters) { // sequence generates a sequence with the number of elements equal to the // number of elements in the sequence generated by the second parameter. TEST(CombineTest, CombineWithFirstParameterSingleValue) { - const ParamGenerator<std::tuple<int, int> > gen = + const ParamGenerator<std::tuple<int, int>> gen = Combine(Values(42), Values(0, 1)); std::tuple<int, int> expected_values[] = {std::make_tuple(42, 0), @@ -446,7 +441,7 @@ TEST(CombineTest, CombineWithFirstParameterSingleValue) { // sequence generates a sequence with the number of elements equal to the // number of elements in the sequence generated by the first parameter. TEST(CombineTest, CombineWithSecondParameterSingleValue) { - const ParamGenerator<std::tuple<int, int> > gen = + const ParamGenerator<std::tuple<int, int>> gen = Combine(Values(0, 1), Values(42)); std::tuple<int, int> expected_values[] = {std::make_tuple(0, 42), @@ -457,7 +452,7 @@ TEST(CombineTest, CombineWithSecondParameterSingleValue) { // Tests that when the first parameter produces an empty sequence, // Combine() produces an empty sequence, too. TEST(CombineTest, CombineWithFirstParameterEmptyRange) { - const ParamGenerator<std::tuple<int, int> > gen = + const ParamGenerator<std::tuple<int, int>> gen = Combine(Range(0, 0), Values(0, 1)); VerifyGeneratorIsEmpty(gen); } @@ -465,7 +460,7 @@ TEST(CombineTest, CombineWithFirstParameterEmptyRange) { // Tests that when the second parameter produces an empty sequence, // Combine() produces an empty sequence, too. TEST(CombineTest, CombineWithSecondParameterEmptyRange) { - const ParamGenerator<std::tuple<int, int> > gen = + const ParamGenerator<std::tuple<int, int>> gen = Combine(Values(0, 1), Range(1, 1)); VerifyGeneratorIsEmpty(gen); } @@ -476,7 +471,7 @@ TEST(CombineTest, CombineWithMaxNumberOfParameters) { const char* foo = "foo"; const char* bar = "bar"; const ParamGenerator< - std::tuple<const char*, int, int, int, int, int, int, int, int, int> > + std::tuple<const char*, int, int, int, int, int, int, int, int, int>> gen = Combine(Values(foo, bar), Values(1), Values(2), Values(3), Values(4), Values(5), Values(6), Values(7), Values(8), Values(9)); @@ -504,11 +499,11 @@ class NonDefaultConstructAssignString { }; TEST(CombineTest, NonDefaultConstructAssign) { - const ParamGenerator<std::tuple<int, NonDefaultConstructAssignString> > gen = + const ParamGenerator<std::tuple<int, NonDefaultConstructAssignString>> gen = Combine(Values(0, 1), Values(NonDefaultConstructAssignString("A"), NonDefaultConstructAssignString("B"))); - ParamGenerator<std::tuple<int, NonDefaultConstructAssignString> >::iterator + ParamGenerator<std::tuple<int, NonDefaultConstructAssignString>>::iterator it = gen.begin(); EXPECT_EQ(0, std::get<0>(*it)); @@ -530,6 +525,63 @@ TEST(CombineTest, NonDefaultConstructAssign) { EXPECT_TRUE(it == gen.end()); } +template <typename T> +class ConstructFromT { + public: + explicit ConstructFromT(const T& t) : t_(t) {} + template <typename... Args, + typename std::enable_if<sizeof...(Args) != 1, int>::type = 0> + ConstructFromT(Args&&... args) : t_(std::forward<Args>(args)...) {} + + bool operator==(const ConstructFromT& other) const { return other.t_ == t_; } + + const T& get() const { return t_; } + + private: + T t_; +}; + +TEST(ConvertTest, CombineWithTwoParameters) { + const char* foo = "foo"; + const char* bar = "bar"; + const ParamGenerator<ConstructFromT<std::tuple<const char*, int>>> gen = + ConvertGenerator<std::tuple<const char*, int>>( + Combine(Values(foo, bar), Values(3, 4))); + + ConstructFromT<std::tuple<const char*, int>> expected_values[] = { + {foo, 3}, {foo, 4}, {bar, 3}, {bar, 4}}; + VerifyGenerator(gen, expected_values); +} + +TEST(ConvertTest, NonDefaultConstructAssign) { + const ParamGenerator< + ConstructFromT<std::tuple<int, NonDefaultConstructAssignString>>> + gen = ConvertGenerator<std::tuple<int, NonDefaultConstructAssignString>>( + Combine(Values(0, 1), Values(NonDefaultConstructAssignString("A"), + NonDefaultConstructAssignString("B")))); + + ParamGenerator<ConstructFromT< + std::tuple<int, NonDefaultConstructAssignString>>>::iterator it = + gen.begin(); + + EXPECT_EQ(0, std::get<0>(it->get())); + EXPECT_EQ("A", std::get<1>(it->get()).str()); + ++it; + + EXPECT_EQ(0, std::get<0>(it->get())); + EXPECT_EQ("B", std::get<1>(it->get()).str()); + ++it; + + EXPECT_EQ(1, std::get<0>(it->get())); + EXPECT_EQ("A", std::get<1>(it->get()).str()); + ++it; + + EXPECT_EQ(1, std::get<0>(it->get())); + EXPECT_EQ("B", std::get<1>(it->get()).str()); + ++it; + + EXPECT_TRUE(it == gen.end()); +} // Tests that an generator produces correct sequence after being // assigned from another generator. @@ -573,7 +625,7 @@ class TestGenerationEnvironment : public ::testing::Environment { Message msg; msg << "TestsExpandedAndRun/" << i; if (UnitTestOptions::FilterMatchesTest( - "TestExpansionModule/MultipleTestGenerationTest", + "TestExpansionModule/MultipleTestGenerationTest", msg.GetString().c_str())) { perform_check = true; } @@ -595,15 +647,20 @@ class TestGenerationEnvironment : public ::testing::Environment { } private: - TestGenerationEnvironment() : fixture_constructor_count_(0), set_up_count_(0), - tear_down_count_(0), test_body_count_(0) {} + TestGenerationEnvironment() + : fixture_constructor_count_(0), + set_up_count_(0), + tear_down_count_(0), + test_body_count_(0) {} int fixture_constructor_count_; int set_up_count_; int tear_down_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}; @@ -612,7 +669,7 @@ class TestGenerationTest : public TestWithParam<int> { public: enum { 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; @@ -636,9 +693,9 @@ class TestGenerationTest : public TestWithParam<int> { for (int i = 0; i < PARAMETER_COUNT; ++i) { Message test_name; test_name << "TestsExpandedAndRun/" << i; - if ( !UnitTestOptions::FilterMatchesTest( - "TestExpansionModule/MultipleTestGenerationTest", - test_name.GetString())) { + if (!UnitTestOptions::FilterMatchesTest( + "TestExpansionModule/MultipleTestGenerationTest", + test_name.GetString())) { all_tests_in_test_case_selected = false; } } @@ -668,7 +725,8 @@ class TestGenerationTest : public TestWithParam<int> { static vector<int> collected_parameters_; private: - GTEST_DISALLOW_COPY_AND_ASSIGN_(TestGenerationTest); + TestGenerationTest(const TestGenerationTest&) = delete; + TestGenerationTest& operator=(const TestGenerationTest&) = delete; }; vector<int> TestGenerationTest::collected_parameters_; @@ -729,8 +787,7 @@ TEST_P(ExternalInstantiationTest, IsMultipleOf33) { // Tests that a parameterized test case can be instantiated with multiple // generators. 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(Sequence2, MultipleInstantiationTest, Range(3, 5)); @@ -780,7 +837,7 @@ class NamingTest : public TestWithParam<int> {}; TEST_P(NamingTest, TestsReportCorrectNamesAndParameters) { 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()); @@ -801,7 +858,7 @@ class MacroNamingTest : public TestWithParam<int> {}; TEST_P(PREFIX_WITH_MACRO(NamingTest), PREFIX_WITH_FOO(SomeTestName)) { 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("FooSomeTestName/0", test_info->name()); @@ -815,7 +872,7 @@ class MacroNamingTestNonParametrized : public ::testing::Test {}; TEST_F(PREFIX_WITH_MACRO(NamingTestNonParametrized), PREFIX_WITH_FOO(SomeTestName)) { 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("FooSomeTestName", test_info->name()); @@ -824,7 +881,7 @@ TEST_F(PREFIX_WITH_MACRO(NamingTestNonParametrized), TEST(MacroNameing, LookupNames) { std::set<std::string> know_suite_names, know_test_names; - auto ins = testing::UnitTest::GetInstance(); + const auto& ins = testing::UnitTest::GetInstance(); int ts = 0; while (const testing::TestSuite* suite = ins->GetTestSuite(ts++)) { know_suite_names.insert(suite->name()); @@ -839,9 +896,8 @@ TEST(MacroNameing, LookupNames) { EXPECT_NE( // know_suite_names.find("FortyTwo/MacroNamingTest"), know_suite_names.end()); - EXPECT_NE( - know_suite_names.find("MacroNamingTestNonParametrized"), - know_suite_names.end()); + EXPECT_NE(know_suite_names.find("MacroNamingTestNonParametrized"), + know_suite_names.end()); // Check that the expected form of the test name actually exists. EXPECT_NE( // know_test_names.find("FortyTwo/MacroNamingTest.FooSomeTestName/0"), @@ -901,7 +957,7 @@ INSTANTIATE_TEST_SUITE_P(CustomParamNameLambda, CustomLambdaNamingTest, }); TEST(CustomNamingTest, CheckNameRegistry) { - ::testing::UnitTest* unit_test = ::testing::UnitTest::GetInstance(); + const auto& unit_test = ::testing::UnitTest::GetInstance(); std::set<std::string> test_names; for (int suite_num = 0; suite_num < unit_test->total_test_suite_count(); ++suite_num) { @@ -924,7 +980,7 @@ class CustomIntegerNamingTest : public TestWithParam<int> {}; TEST_P(CustomIntegerNamingTest, TestsReportCorrectNames) { const ::testing::TestInfo* const test_info = - ::testing::UnitTest::GetInstance()->current_test_info(); + ::testing::UnitTest::GetInstance()->current_test_info(); Message test_name_stream; test_name_stream << "TestsReportCorrectNames/" << GetParam(); EXPECT_STREQ(test_name_stream.GetString().c_str(), test_info->name()); @@ -949,7 +1005,7 @@ class CustomStructNamingTest : public TestWithParam<CustomStruct> {}; TEST_P(CustomStructNamingTest, TestsReportCorrectNames) { const ::testing::TestInfo* const test_info = - ::testing::UnitTest::GetInstance()->current_test_info(); + ::testing::UnitTest::GetInstance()->current_test_info(); Message test_name_stream; test_name_stream << "TestsReportCorrectNames/" << GetParam(); EXPECT_STREQ(test_name_stream.GetString().c_str(), test_info->name()); @@ -979,7 +1035,7 @@ class StatefulNamingTest : public ::testing::TestWithParam<int> { TEST_P(StatefulNamingTest, TestsReportCorrectNames) { const ::testing::TestInfo* const test_info = - ::testing::UnitTest::GetInstance()->current_test_info(); + ::testing::UnitTest::GetInstance()->current_test_info(); sum_ += GetParam(); Message test_name_stream; test_name_stream << "TestsReportCorrectNames/" << sum_; @@ -1007,7 +1063,7 @@ class CommentTest : public TestWithParam<Unstreamable> {}; TEST_P(CommentTest, TestsCorrectlyReportUnstreamableParams) { 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()); } @@ -1021,7 +1077,8 @@ INSTANTIATE_TEST_SUITE_P(InstantiationWithComments, CommentTest, // perform simple tests on both. class NonParameterizedBaseTest : public ::testing::Test { public: - NonParameterizedBaseTest() : n_(17) { } + NonParameterizedBaseTest() : n_(17) {} + protected: int n_; }; @@ -1029,16 +1086,14 @@ class NonParameterizedBaseTest : public ::testing::Test { class ParameterizedDerivedTest : public NonParameterizedBaseTest, public ::testing::WithParamInterface<int> { protected: - ParameterizedDerivedTest() : count_(0) { } + ParameterizedDerivedTest() : count_(0) {} int count_; static int global_count_; }; int ParameterizedDerivedTest::global_count_ = 0; -TEST_F(NonParameterizedBaseTest, FixtureIsInitialized) { - EXPECT_EQ(17, n_); -} +TEST_F(NonParameterizedBaseTest, FixtureIsInitialized) { EXPECT_EQ(17, n_); } TEST_P(ParameterizedDerivedTest, SeesSequence) { EXPECT_EQ(17, n_); @@ -1046,11 +1101,10 @@ TEST_P(ParameterizedDerivedTest, SeesSequence) { EXPECT_EQ(GetParam(), global_count_++); } -class ParameterizedDeathTest : public ::testing::TestWithParam<int> { }; +class ParameterizedDeathTest : public ::testing::TestWithParam<int> {}; TEST_F(ParameterizedDeathTest, GetParamDiesFromTestF) { - EXPECT_DEATH_IF_SUPPORTED(GetParam(), - ".* value-parameterized test .*"); + EXPECT_DEATH_IF_SUPPORTED(GetParam(), ".* value-parameterized test .*"); } INSTANTIATE_TEST_SUITE_P(RangeZeroToFive, ParameterizedDerivedTest, @@ -1084,11 +1138,11 @@ class NotInstantiatedTest : public testing::TestWithParam<int> {}; // ... we mark is as allowed. GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(NotInstantiatedTest); -TEST_P(NotInstantiatedTest, Used) { } +TEST_P(NotInstantiatedTest, Used) {} using OtherName = NotInstantiatedTest; GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(OtherName); -TEST_P(OtherName, Used) { } +TEST_P(OtherName, Used) {} // Used but not instantiated, this would fail. but... template <typename T> @@ -1097,11 +1151,11 @@ TYPED_TEST_SUITE_P(NotInstantiatedTypeTest); // ... we mark is as allowed. GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(NotInstantiatedTypeTest); -TYPED_TEST_P(NotInstantiatedTypeTest, Used) { } +TYPED_TEST_P(NotInstantiatedTypeTest, Used) {} REGISTER_TYPED_TEST_SUITE_P(NotInstantiatedTypeTest, Used); } // namespace works_here -int main(int argc, char **argv) { +int main(int argc, char** argv) { // Used in TestGenerationTest test suite. AddGlobalTestEnvironment(TestGenerationTest::Environment::Instance()); // Used in GeneratorEvaluationTest test suite. Tests that the updated value |