diff options
Diffstat (limited to 'gtest/test/gtest-param-test_test.cc')
-rw-r--r-- | gtest/test/gtest-param-test_test.cc | 137 |
1 files changed, 47 insertions, 90 deletions
diff --git a/gtest/test/gtest-param-test_test.cc b/gtest/test/gtest-param-test_test.cc index d0a0e73..ecb5fdb 100644 --- a/gtest/test/gtest-param-test_test.cc +++ b/gtest/test/gtest-param-test_test.cc @@ -40,8 +40,6 @@ #include <algorithm> #include <iostream> #include <list> -#include <sstream> -#include <string> #include <vector> // To include gtest-internal-inl.h. @@ -72,57 +70,6 @@ using ::std::tr1::tuple; using ::testing::internal::ParamGenerator; using ::testing::internal::UnitTestOptions; -// Prints a value to a string. -// -// TODO(wan@google.com): remove PrintValue() when we move matchers and -// EXPECT_THAT() from Google Mock to Google Test. At that time, we -// can write EXPECT_THAT(x, Eq(y)) to compare two tuples x and y, as -// EXPECT_THAT() and the matchers know how to print tuples. -template <typename T> -::std::string PrintValue(const T& value) { - ::std::stringstream stream; - stream << value; - return stream.str(); -} - -#if GTEST_HAS_COMBINE - -// These overloads allow printing tuples in our tests. We cannot -// define an operator<< for tuples, as that definition needs to be in -// the std namespace in order to be picked up by Google Test via -// Argument-Dependent Lookup, yet defining anything in the std -// namespace in non-STL code is undefined behavior. - -template <typename T1, typename T2> -::std::string PrintValue(const tuple<T1, T2>& value) { - ::std::stringstream stream; - stream << "(" << get<0>(value) << ", " << get<1>(value) << ")"; - return stream.str(); -} - -template <typename T1, typename T2, typename T3> -::std::string PrintValue(const tuple<T1, T2, T3>& value) { - ::std::stringstream stream; - stream << "(" << get<0>(value) << ", " << get<1>(value) - << ", "<< get<2>(value) << ")"; - return stream.str(); -} - -template <typename T1, typename T2, typename T3, typename T4, typename T5, - typename T6, typename T7, typename T8, typename T9, typename T10> -::std::string PrintValue( - const tuple<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>& value) { - ::std::stringstream stream; - stream << "(" << get<0>(value) << ", " << get<1>(value) - << ", "<< get<2>(value) << ", " << get<3>(value) - << ", "<< get<4>(value) << ", " << get<5>(value) - << ", "<< get<6>(value) << ", " << get<7>(value) - << ", "<< get<8>(value) << ", " << get<9>(value) << ")"; - return stream.str(); -} - -#endif // GTEST_HAS_COMBINE - // Verifies that a sequence generated by the generator and accessed // via the iterator object matches the expected one using Google Test // assertions. @@ -133,19 +80,15 @@ void VerifyGenerator(const ParamGenerator<T>& generator, for (size_t i = 0; i < N; ++i) { ASSERT_FALSE(it == generator.end()) << "At element " << i << " when accessing via an iterator " - << "created with the copy constructor.\n"; - // 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) - << ", and 'it' is an iterator created with the copy constructor.\n"; + << "created with the copy constructor." << std::endl; + EXPECT_EQ(expected_values[i], *it) + << "At element " << i << " when accessing via an iterator " + << "created with the copy constructor." << std::endl; it++; } EXPECT_TRUE(it == generator.end()) << "At the presumed end of sequence when accessing via an iterator " - << "created with the copy constructor.\n"; + << "created with the copy constructor." << std::endl; // Test the iterator assignment. The following lines verify that // the sequence accessed via an iterator initialized via the @@ -155,17 +98,15 @@ void VerifyGenerator(const ParamGenerator<T>& generator, for (size_t i = 0; i < N; ++i) { ASSERT_FALSE(it == generator.end()) << "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) - << ", and 'it' is an iterator created with the copy constructor.\n"; + << "created with the assignment operator." << std::endl; + EXPECT_EQ(expected_values[i], *it) + << "At element " << i << " when accessing via an iterator " + << "created with the assignment operator." << std::endl; it++; } EXPECT_TRUE(it == generator.end()) << "At the presumed end of sequence when accessing via an iterator " - << "created with the assignment operator.\n"; + << "created with the assignment operator." << std::endl; } template <typename T> @@ -264,7 +205,7 @@ TEST(RangeTest, IntRangeWithCustomStepOverUpperBound) { // copy constructor, operator=(), operator+(), and operator<(). class DogAdder { public: - explicit DogAdder(const char* a_value) : value_(a_value) {} + explicit DogAdder(const char* value) : value_(value) {} DogAdder(const DogAdder& other) : value_(other.value_.c_str()) {} DogAdder operator=(const DogAdder& other) { @@ -302,7 +243,7 @@ TEST(RangeTest, WorksWithACustomType) { class IntWrapper { public: - explicit IntWrapper(int a_value) : value_(a_value) {} + explicit IntWrapper(int value) : value_(value) {} IntWrapper(const IntWrapper& other) : value_(other.value_) {} IntWrapper operator=(const IntWrapper& other) { @@ -459,6 +400,33 @@ TEST(BoolTest, BoolWorks) { #if GTEST_HAS_COMBINE +template <typename T1, typename T2> +::std::ostream& operator<<(::std::ostream& stream, const tuple<T1, T2>& value) { + stream << "(" << get<0>(value) << ", " << get<1>(value) << ")"; + return stream; +} + +template <typename T1, typename T2, typename T3> +::std::ostream& operator<<(::std::ostream& stream, + const tuple<T1, T2, T3>& value) { + stream << "(" << get<0>(value) << ", " << get<1>(value) + << ", "<< get<2>(value) << ")"; + return stream; +} + +template <typename T1, typename T2, typename T3, typename T4, typename T5, + typename T6, typename T7, typename T8, typename T9, typename T10> +::std::ostream& operator<<( + ::std::ostream& stream, + const tuple<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>& value) { + stream << "(" << get<0>(value) << ", " << get<1>(value) + << ", "<< get<2>(value) << ", " << get<3>(value) + << ", "<< get<4>(value) << ", " << get<5>(value) + << ", "<< get<6>(value) << ", " << get<7>(value) + << ", "<< get<8>(value) << ", " << get<9>(value) << ")"; + return stream; +} + // Tests that Combine() with two parameters generates the expected sequence. TEST(CombineTest, CombineWithTwoParameters) { const char* foo = "foo"; @@ -692,15 +660,13 @@ INSTANTIATE_TEST_CASE_P(TestExpansionModule, TestGenerationTest, ValuesIn(test_generation_params)); // This test verifies that the element sequence (third parameter of -// INSTANTIATE_TEST_CASE_P) is evaluated in InitGoogleTest() and neither at -// the call site of INSTANTIATE_TEST_CASE_P nor in RUN_ALL_TESTS(). For -// that, we declare param_value_ to be a static member of -// GeneratorEvaluationTest and initialize it to 0. We set it to 1 in -// main(), just before invocation of InitGoogleTest(). After calling -// InitGoogleTest(), we set the value to 2. If the sequence is evaluated -// before or after InitGoogleTest, INSTANTIATE_TEST_CASE_P will create a -// test with parameter other than 1, and the test body will fail the -// assertion. +// INSTANTIATE_TEST_CASE_P) is evaluated in RUN_ALL_TESTS and not at the call +// site of INSTANTIATE_TEST_CASE_P. +// For that, we declare param_value_ to be a static member of +// GeneratorEvaluationTest and initialize it to 0. We set it to 1 in main(), +// just before invocation of RUN_ALL_TESTS. If the sequence is evaluated +// before that moment, INSTANTIATE_TEST_CASE_P will create a test with +// parameter 0, and the test body will fail the assertion. class GeneratorEvaluationTest : public TestWithParam<int> { public: static int param_value() { return param_value_; } @@ -817,19 +783,10 @@ int main(int argc, char **argv) { #if GTEST_HAS_PARAM_TEST // Used in TestGenerationTest test case. AddGlobalTestEnvironment(TestGenerationTest::Environment::Instance()); - // Used in GeneratorEvaluationTest test case. Tests that the updated value - // will be picked up for instantiating tests in GeneratorEvaluationTest. + // Used in GeneratorEvaluationTest test case. GeneratorEvaluationTest::set_param_value(1); #endif // GTEST_HAS_PARAM_TEST ::testing::InitGoogleTest(&argc, argv); - -#if GTEST_HAS_PARAM_TEST - // Used in GeneratorEvaluationTest test case. Tests that value updated - // here will NOT be used for instantiating tests in - // GeneratorEvaluationTest. - GeneratorEvaluationTest::set_param_value(2); -#endif // GTEST_HAS_PARAM_TEST - return RUN_ALL_TESTS(); } |