diff options
author | zhanyong.wan <zhanyong.wan@8415998a-534a-0410-bf83-d39667b30386> | 2009-08-14 04:50:02 +0000 |
---|---|---|
committer | zhanyong.wan <zhanyong.wan@8415998a-534a-0410-bf83-d39667b30386> | 2009-08-14 04:50:02 +0000 |
commit | 0ea67f88aea208e7ef13c91b374b352679beab38 (patch) | |
tree | 86cd0753aeb1d884472594283499fa5bc8fabf3f | |
parent | 9571b28675a5a602ed3c8a7acf270d03aca69c96 (diff) | |
download | googletest-0ea67f88aea208e7ef13c91b374b352679beab38.tar.gz googletest-0ea67f88aea208e7ef13c91b374b352679beab38.tar.bz2 googletest-0ea67f88aea208e7ef13c91b374b352679beab38.zip |
Improves protobuf print format.
-rw-r--r-- | include/gmock/gmock-printers.h | 15 | ||||
-rw-r--r-- | test/gmock-internal-utils_test.cc | 7 | ||||
-rw-r--r-- | test/gmock-printers_test.cc | 25 |
3 files changed, 40 insertions, 7 deletions
diff --git a/include/gmock/gmock-printers.h b/include/gmock/gmock-printers.h index 561de3d9..69eee120 100644 --- a/include/gmock/gmock-printers.h +++ b/include/gmock/gmock-printers.h @@ -129,14 +129,21 @@ class TypeWithoutFormatter { sizeof(value), os); } }; + +// We print a protobuf using its ShortDebugString() when the string +// doesn't exceed this many characters; otherwise we print it using +// DebugString() for better readability. +const size_t kProtobufOneLinerMaxLength = 50; + template <typename T> class TypeWithoutFormatter<T, true> { public: static void PrintValue(const T& value, ::std::ostream* os) { - // Both ProtocolMessage and proto2::Message have the - // ShortDebugString() method, so the same implementation works for - // both. - ::std::operator<<(*os, "<" + value.ShortDebugString() + ">"); + const ::testing::internal::string short_str = value.ShortDebugString(); + const ::testing::internal::string pretty_str = + short_str.length() <= kProtobufOneLinerMaxLength ? + short_str : ("\n" + value.DebugString()); + ::std::operator<<(*os, "<" + pretty_str + ">"); } }; diff --git a/test/gmock-internal-utils_test.cc b/test/gmock-internal-utils_test.cc index 20289288..c949dd75 100644 --- a/test/gmock-internal-utils_test.cc +++ b/test/gmock-internal-utils_test.cc @@ -48,6 +48,12 @@ #include <sys/types.h> // For ssize_t. NOLINT #endif +class ProtocolMessage; + +namespace proto2 { +class Message; +} // namespace proto2 + namespace testing { namespace internal { @@ -384,6 +390,7 @@ TEST(IsAProtocolMessageTest, ValueIsCompileTimeConstant) { // Tests that IsAProtocolMessage<T>::value is true when T is // ProtocolMessage or a sub-class of it. TEST(IsAProtocolMessageTest, ValueIsTrueWhenTypeIsAProtocolMessage) { + EXPECT_TRUE(IsAProtocolMessage< ::proto2::Message>::value); EXPECT_TRUE(IsAProtocolMessage<ProtocolMessage>::value); #if GMOCK_HAS_PROTOBUF_ EXPECT_TRUE(IsAProtocolMessage<const TestMessage>::value); diff --git a/test/gmock-printers_test.cc b/test/gmock-printers_test.cc index 8c03ec46..af2e83c0 100644 --- a/test/gmock-printers_test.cc +++ b/test/gmock-printers_test.cc @@ -919,12 +919,31 @@ TEST(PrintProtocolMessageTest, PrintsShortDebugString) { EXPECT_EQ("<member:\"yes\">", Print(msg)); } -// Tests printing a proto2 message. -TEST(PrintProto2MessageTest, PrintsShortDebugString) { +// Tests printing a short proto2 message. +TEST(PrintProto2MessageTest, PrintsShortDebugStringWhenItIsShort) { testing::internal::FooMessage msg; msg.set_int_field(2); + msg.set_string_field("hello"); EXPECT_PRED2(RE::FullMatch, Print(msg), - "<int_field:\\s*2\\s*>"); + "<int_field:\\s*2\\s+string_field:\\s*\"hello\">"); +} + +// Tests printing a long proto2 message. +TEST(PrintProto2MessageTest, PrintsDebugStringWhenItIsLong) { + testing::internal::FooMessage msg; + msg.set_int_field(2); + msg.set_string_field("hello"); + msg.add_names("peter"); + msg.add_names("paul"); + msg.add_names("mary"); + EXPECT_PRED2(RE::FullMatch, Print(msg), + "<\n" + "int_field:\\s*2\n" + "string_field:\\s*\"hello\"\n" + "names:\\s*\"peter\"\n" + "names:\\s*\"paul\"\n" + "names:\\s*\"mary\"\n" + ">"); } #endif // GMOCK_HAS_PROTOBUF_ |