temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 1 | // Protocol Buffers - Google's data interchange format |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 2 | // Copyright 2008 Google Inc. All rights reserved. |
Feng Xiao | e428862 | 2014-10-01 16:26:23 -0700 | [diff] [blame] | 3 | // https://developers.google.com/protocol-buffers/ |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 4 | // |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 5 | // Redistribution and use in source and binary forms, with or without |
| 6 | // modification, are permitted provided that the following conditions are |
| 7 | // met: |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 8 | // |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 9 | // * Redistributions of source code must retain the above copyright |
| 10 | // notice, this list of conditions and the following disclaimer. |
| 11 | // * Redistributions in binary form must reproduce the above |
| 12 | // copyright notice, this list of conditions and the following disclaimer |
| 13 | // in the documentation and/or other materials provided with the |
| 14 | // distribution. |
| 15 | // * Neither the name of Google Inc. nor the names of its |
| 16 | // contributors may be used to endorse or promote products derived from |
| 17 | // this software without specific prior written permission. |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 18 | // |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 23 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 25 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 30 | |
| 31 | // Author: jschorr@google.com (Joseph Schorr) |
| 32 | // Based on original Protocol Buffers design by |
| 33 | // Sanjay Ghemawat, Jeff Dean, and others. |
| 34 | |
Bo Yang | 5db2173 | 2015-05-21 14:28:59 -0700 | [diff] [blame] | 35 | #include <google/protobuf/text_format.h> |
| 36 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 37 | #include <math.h> |
| 38 | #include <stdlib.h> |
| 39 | #include <limits> |
Jisi Liu | 46e8ff6 | 2015-10-05 11:59:43 -0700 | [diff] [blame] | 40 | #include <memory> |
| 41 | #ifndef _SHARED_PTR_H |
| 42 | #include <google/protobuf/stubs/shared_ptr.h> |
| 43 | #endif |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 44 | |
Feng Xiao | eee38b0 | 2015-08-22 18:25:48 -0700 | [diff] [blame] | 45 | #include <google/protobuf/stubs/logging.h> |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 46 | #include <google/protobuf/stubs/common.h> |
Jisi Liu | 3b3c8ab | 2016-03-30 11:39:59 -0700 | [diff] [blame] | 47 | #include <google/protobuf/stubs/logging.h> |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 48 | #include <google/protobuf/testing/file.h> |
Bo Yang | 5db2173 | 2015-05-21 14:28:59 -0700 | [diff] [blame] | 49 | #include <google/protobuf/test_util.h> |
| 50 | #include <google/protobuf/unittest.pb.h> |
| 51 | #include <google/protobuf/unittest_mset.pb.h> |
Feng Xiao | eee38b0 | 2015-08-22 18:25:48 -0700 | [diff] [blame] | 52 | #include <google/protobuf/unittest_mset_wire_format.pb.h> |
Bo Yang | 5db2173 | 2015-05-21 14:28:59 -0700 | [diff] [blame] | 53 | #include <google/protobuf/io/tokenizer.h> |
| 54 | #include <google/protobuf/io/zero_copy_stream_impl.h> |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 55 | #include <google/protobuf/stubs/strutil.h> |
Karol Ostrovsky | ee35402 | 2015-06-29 16:13:33 +0200 | [diff] [blame] | 56 | #include <google/protobuf/stubs/mathlimits.h> |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 57 | #include <google/protobuf/stubs/substitute.h> |
Bo Yang | 5db2173 | 2015-05-21 14:28:59 -0700 | [diff] [blame] | 58 | #include <google/protobuf/testing/googletest.h> |
| 59 | #include <gtest/gtest.h> |
| 60 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 61 | |
| 62 | namespace google { |
| 63 | namespace protobuf { |
kenton@google.com | a2a32c2 | 2008-11-14 17:29:32 +0000 | [diff] [blame] | 64 | |
| 65 | // Can't use an anonymous namespace here due to brokenness of Tru64 compiler. |
| 66 | namespace text_format_unittest { |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 67 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 68 | // A basic string with different escapable characters for testing. |
| 69 | const string kEscapeTestString = |
| 70 | "\"A string with ' characters \n and \r newlines and \t tabs and \001 " |
kenton@google.com | 26bd9ee | 2008-11-21 00:06:27 +0000 | [diff] [blame] | 71 | "slashes \\ and multiple spaces"; |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 72 | |
| 73 | // A representation of the above string with all the characters escaped. |
| 74 | const string kEscapeTestStringEscaped = |
| 75 | "\"\\\"A string with \\' characters \\n and \\r newlines " |
kenton@google.com | 26bd9ee | 2008-11-21 00:06:27 +0000 | [diff] [blame] | 76 | "and \\t tabs and \\001 slashes \\\\ and multiple spaces\""; |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 77 | |
| 78 | class TextFormatTest : public testing::Test { |
| 79 | public: |
| 80 | static void SetUpTestCase() { |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 81 | GOOGLE_CHECK_OK(File::GetContents( |
| 82 | TestSourceDir() + |
| 83 | "/google/protobuf/" |
| 84 | "testdata/text_format_unittest_data_oneof_implemented.txt", |
| 85 | &static_proto_debug_string_, true)); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | TextFormatTest() : proto_debug_string_(static_proto_debug_string_) {} |
| 89 | |
| 90 | protected: |
| 91 | // Debug string read from text_format_unittest_data.txt. |
| 92 | const string proto_debug_string_; |
| 93 | unittest::TestAllTypes proto_; |
| 94 | |
| 95 | private: |
| 96 | static string static_proto_debug_string_; |
| 97 | }; |
| 98 | string TextFormatTest::static_proto_debug_string_; |
| 99 | |
| 100 | class TextFormatExtensionsTest : public testing::Test { |
| 101 | public: |
| 102 | static void SetUpTestCase() { |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 103 | GOOGLE_CHECK_OK(File::GetContents(TestSourceDir() + |
| 104 | "/google/protobuf/testdata/" |
| 105 | "text_format_unittest_extensions_data.txt", |
| 106 | &static_proto_debug_string_, true)); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | TextFormatExtensionsTest() |
| 110 | : proto_debug_string_(static_proto_debug_string_) {} |
| 111 | |
| 112 | protected: |
| 113 | // Debug string read from text_format_unittest_data.txt. |
| 114 | const string proto_debug_string_; |
| 115 | unittest::TestAllExtensions proto_; |
| 116 | |
| 117 | private: |
| 118 | static string static_proto_debug_string_; |
| 119 | }; |
| 120 | string TextFormatExtensionsTest::static_proto_debug_string_; |
| 121 | |
| 122 | |
| 123 | TEST_F(TextFormatTest, Basic) { |
| 124 | TestUtil::SetAllFields(&proto_); |
| 125 | EXPECT_EQ(proto_debug_string_, proto_.DebugString()); |
| 126 | } |
| 127 | |
| 128 | TEST_F(TextFormatExtensionsTest, Extensions) { |
| 129 | TestUtil::SetAllExtensions(&proto_); |
| 130 | EXPECT_EQ(proto_debug_string_, proto_.DebugString()); |
| 131 | } |
| 132 | |
kenton@google.com | 26bd9ee | 2008-11-21 00:06:27 +0000 | [diff] [blame] | 133 | TEST_F(TextFormatTest, ShortDebugString) { |
| 134 | proto_.set_optional_int32(1); |
| 135 | proto_.set_optional_string("hello"); |
| 136 | proto_.mutable_optional_nested_message()->set_bb(2); |
| 137 | proto_.mutable_optional_foreign_message(); |
| 138 | |
| 139 | EXPECT_EQ("optional_int32: 1 optional_string: \"hello\" " |
| 140 | "optional_nested_message { bb: 2 } " |
| 141 | "optional_foreign_message { }", |
| 142 | proto_.ShortDebugString()); |
| 143 | } |
| 144 | |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 145 | TEST_F(TextFormatTest, ShortPrimitiveRepeateds) { |
| 146 | proto_.set_optional_int32(123); |
| 147 | proto_.add_repeated_int32(456); |
| 148 | proto_.add_repeated_int32(789); |
| 149 | proto_.add_repeated_string("foo"); |
| 150 | proto_.add_repeated_string("bar"); |
| 151 | proto_.add_repeated_nested_message()->set_bb(2); |
| 152 | proto_.add_repeated_nested_message()->set_bb(3); |
| 153 | proto_.add_repeated_nested_enum(unittest::TestAllTypes::FOO); |
| 154 | proto_.add_repeated_nested_enum(unittest::TestAllTypes::BAR); |
| 155 | |
| 156 | TextFormat::Printer printer; |
| 157 | printer.SetUseShortRepeatedPrimitives(true); |
| 158 | string text; |
| 159 | printer.PrintToString(proto_, &text); |
| 160 | |
| 161 | EXPECT_EQ("optional_int32: 123\n" |
| 162 | "repeated_int32: [456, 789]\n" |
| 163 | "repeated_string: \"foo\"\n" |
| 164 | "repeated_string: \"bar\"\n" |
| 165 | "repeated_nested_message {\n bb: 2\n}\n" |
| 166 | "repeated_nested_message {\n bb: 3\n}\n" |
| 167 | "repeated_nested_enum: [FOO, BAR]\n", |
| 168 | text); |
| 169 | |
| 170 | // Try in single-line mode. |
| 171 | printer.SetSingleLineMode(true); |
| 172 | printer.PrintToString(proto_, &text); |
| 173 | |
| 174 | EXPECT_EQ("optional_int32: 123 " |
| 175 | "repeated_int32: [456, 789] " |
| 176 | "repeated_string: \"foo\" " |
| 177 | "repeated_string: \"bar\" " |
| 178 | "repeated_nested_message { bb: 2 } " |
| 179 | "repeated_nested_message { bb: 3 } " |
| 180 | "repeated_nested_enum: [FOO, BAR] ", |
| 181 | text); |
| 182 | } |
| 183 | |
| 184 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 185 | TEST_F(TextFormatTest, StringEscape) { |
| 186 | // Set the string value to test. |
| 187 | proto_.set_optional_string(kEscapeTestString); |
| 188 | |
| 189 | // Get the DebugString from the proto. |
| 190 | string debug_string = proto_.DebugString(); |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 191 | string utf8_debug_string = proto_.Utf8DebugString(); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 192 | |
| 193 | // Hardcode a correct value to test against. |
| 194 | string correct_string = "optional_string: " |
| 195 | + kEscapeTestStringEscaped |
| 196 | + "\n"; |
| 197 | |
| 198 | // Compare. |
| 199 | EXPECT_EQ(correct_string, debug_string); |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 200 | // UTF-8 string is the same as non-UTF-8 because |
| 201 | // the protocol buffer contains no UTF-8 text. |
| 202 | EXPECT_EQ(correct_string, utf8_debug_string); |
kenton@google.com | 26bd9ee | 2008-11-21 00:06:27 +0000 | [diff] [blame] | 203 | |
| 204 | string expected_short_debug_string = "optional_string: " |
| 205 | + kEscapeTestStringEscaped; |
| 206 | EXPECT_EQ(expected_short_debug_string, proto_.ShortDebugString()); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 207 | } |
| 208 | |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 209 | TEST_F(TextFormatTest, Utf8DebugString) { |
| 210 | // Set the string value to test. |
| 211 | proto_.set_optional_string("\350\260\267\346\255\214"); |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 212 | proto_.set_optional_bytes("\350\260\267\346\255\214"); |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 213 | |
| 214 | // Get the DebugString from the proto. |
| 215 | string debug_string = proto_.DebugString(); |
| 216 | string utf8_debug_string = proto_.Utf8DebugString(); |
| 217 | |
| 218 | // Hardcode a correct value to test against. |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 219 | string correct_utf8_string = |
| 220 | "optional_string: " |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 221 | "\"\350\260\267\346\255\214\"" |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 222 | "\n" |
| 223 | "optional_bytes: " |
| 224 | "\"\\350\\260\\267\\346\\255\\214\"" |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 225 | "\n"; |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 226 | string correct_string = |
| 227 | "optional_string: " |
| 228 | "\"\\350\\260\\267\\346\\255\\214\"" |
| 229 | "\n" |
| 230 | "optional_bytes: " |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 231 | "\"\\350\\260\\267\\346\\255\\214\"" |
| 232 | "\n"; |
| 233 | |
| 234 | // Compare. |
| 235 | EXPECT_EQ(correct_utf8_string, utf8_debug_string); |
| 236 | EXPECT_EQ(correct_string, debug_string); |
| 237 | } |
| 238 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 239 | TEST_F(TextFormatTest, PrintUnknownFields) { |
| 240 | // Test printing of unknown fields in a message. |
| 241 | |
| 242 | unittest::TestEmptyMessage message; |
| 243 | UnknownFieldSet* unknown_fields = message.mutable_unknown_fields(); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 244 | |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 245 | unknown_fields->AddVarint(5, 1); |
| 246 | unknown_fields->AddFixed32(5, 2); |
| 247 | unknown_fields->AddFixed64(5, 3); |
| 248 | unknown_fields->AddLengthDelimited(5, "4"); |
| 249 | unknown_fields->AddGroup(5)->AddVarint(10, 5); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 250 | |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 251 | unknown_fields->AddVarint(8, 1); |
| 252 | unknown_fields->AddVarint(8, 2); |
| 253 | unknown_fields->AddVarint(8, 3); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 254 | |
| 255 | EXPECT_EQ( |
| 256 | "5: 1\n" |
| 257 | "5: 0x00000002\n" |
| 258 | "5: 0x0000000000000003\n" |
| 259 | "5: \"4\"\n" |
| 260 | "5 {\n" |
| 261 | " 10: 5\n" |
| 262 | "}\n" |
| 263 | "8: 1\n" |
| 264 | "8: 2\n" |
| 265 | "8: 3\n", |
| 266 | message.DebugString()); |
| 267 | } |
| 268 | |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 269 | TEST_F(TextFormatTest, PrintUnknownFieldsHidden) { |
Veres Lajos | c768072 | 2014-11-08 22:59:34 +0000 | [diff] [blame] | 270 | // Test printing of unknown fields in a message when suppressed. |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 271 | |
| 272 | unittest::OneString message; |
| 273 | message.set_data("data"); |
| 274 | UnknownFieldSet* unknown_fields = message.mutable_unknown_fields(); |
| 275 | |
| 276 | unknown_fields->AddVarint(5, 1); |
| 277 | unknown_fields->AddFixed32(5, 2); |
| 278 | unknown_fields->AddFixed64(5, 3); |
| 279 | unknown_fields->AddLengthDelimited(5, "4"); |
| 280 | unknown_fields->AddGroup(5)->AddVarint(10, 5); |
| 281 | |
| 282 | unknown_fields->AddVarint(8, 1); |
| 283 | unknown_fields->AddVarint(8, 2); |
| 284 | unknown_fields->AddVarint(8, 3); |
| 285 | |
| 286 | TextFormat::Printer printer; |
| 287 | printer.SetHideUnknownFields(true); |
| 288 | string output; |
| 289 | printer.PrintToString(message, &output); |
| 290 | |
| 291 | EXPECT_EQ("data: \"data\"\n", output); |
| 292 | } |
| 293 | |
temporal | a0f27fc | 2008-08-06 01:12:21 +0000 | [diff] [blame] | 294 | TEST_F(TextFormatTest, PrintUnknownMessage) { |
| 295 | // Test heuristic printing of messages in an UnknownFieldSet. |
| 296 | |
| 297 | protobuf_unittest::TestAllTypes message; |
| 298 | |
| 299 | // Cases which should not be interpreted as sub-messages. |
| 300 | |
| 301 | // 'a' is a valid FIXED64 tag, so for the string to be parseable as a message |
| 302 | // it should be followed by 8 bytes. Since this string only has two |
| 303 | // subsequent bytes, it should be treated as a string. |
| 304 | message.add_repeated_string("abc"); |
| 305 | |
| 306 | // 'd' happens to be a valid ENDGROUP tag. So, |
| 307 | // UnknownFieldSet::MergeFromCodedStream() will successfully parse "def", but |
| 308 | // the ConsumedEntireMessage() check should fail. |
| 309 | message.add_repeated_string("def"); |
| 310 | |
| 311 | // A zero-length string should never be interpreted as a message even though |
| 312 | // it is technically valid as one. |
| 313 | message.add_repeated_string(""); |
| 314 | |
| 315 | // Case which should be interpreted as a sub-message. |
| 316 | |
| 317 | // An actual nested message with content should always be interpreted as a |
| 318 | // nested message. |
| 319 | message.add_repeated_nested_message()->set_bb(123); |
| 320 | |
| 321 | string data; |
| 322 | message.SerializeToString(&data); |
| 323 | |
| 324 | string text; |
| 325 | UnknownFieldSet unknown_fields; |
| 326 | EXPECT_TRUE(unknown_fields.ParseFromString(data)); |
| 327 | EXPECT_TRUE(TextFormat::PrintUnknownFieldsToString(unknown_fields, &text)); |
| 328 | EXPECT_EQ( |
| 329 | "44: \"abc\"\n" |
| 330 | "44: \"def\"\n" |
| 331 | "44: \"\"\n" |
| 332 | "48 {\n" |
| 333 | " 1: 123\n" |
| 334 | "}\n", |
| 335 | text); |
| 336 | } |
| 337 | |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 338 | TEST_F(TextFormatTest, PrintMessageWithIndent) { |
| 339 | // Test adding an initial indent to printing. |
| 340 | |
| 341 | protobuf_unittest::TestAllTypes message; |
| 342 | |
| 343 | message.add_repeated_string("abc"); |
| 344 | message.add_repeated_string("def"); |
| 345 | message.add_repeated_nested_message()->set_bb(123); |
| 346 | |
| 347 | string text; |
| 348 | TextFormat::Printer printer; |
| 349 | printer.SetInitialIndentLevel(1); |
| 350 | EXPECT_TRUE(printer.PrintToString(message, &text)); |
| 351 | EXPECT_EQ( |
| 352 | " repeated_string: \"abc\"\n" |
| 353 | " repeated_string: \"def\"\n" |
| 354 | " repeated_nested_message {\n" |
| 355 | " bb: 123\n" |
| 356 | " }\n", |
| 357 | text); |
| 358 | } |
| 359 | |
| 360 | TEST_F(TextFormatTest, PrintMessageSingleLine) { |
| 361 | // Test printing a message on a single line. |
| 362 | |
| 363 | protobuf_unittest::TestAllTypes message; |
| 364 | |
| 365 | message.add_repeated_string("abc"); |
| 366 | message.add_repeated_string("def"); |
| 367 | message.add_repeated_nested_message()->set_bb(123); |
| 368 | |
| 369 | string text; |
| 370 | TextFormat::Printer printer; |
| 371 | printer.SetInitialIndentLevel(1); |
| 372 | printer.SetSingleLineMode(true); |
| 373 | EXPECT_TRUE(printer.PrintToString(message, &text)); |
| 374 | EXPECT_EQ( |
| 375 | " repeated_string: \"abc\" repeated_string: \"def\" " |
| 376 | "repeated_nested_message { bb: 123 } ", |
| 377 | text); |
| 378 | } |
| 379 | |
xiaofeng@google.com | b55a20f | 2012-09-22 02:40:50 +0000 | [diff] [blame] | 380 | TEST_F(TextFormatTest, PrintBufferTooSmall) { |
| 381 | // Test printing a message to a buffer that is too small. |
| 382 | |
| 383 | protobuf_unittest::TestAllTypes message; |
| 384 | |
| 385 | message.add_repeated_string("abc"); |
| 386 | message.add_repeated_string("def"); |
| 387 | |
| 388 | char buffer[1] = ""; |
| 389 | io::ArrayOutputStream output_stream(buffer, 1); |
| 390 | EXPECT_FALSE(TextFormat::Print(message, &output_stream)); |
| 391 | EXPECT_EQ(buffer[0], 'r'); |
| 392 | EXPECT_EQ(output_stream.ByteCount(), 1); |
| 393 | } |
| 394 | |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 395 | // A printer that appends 'u' to all unsigned int32. |
| 396 | class CustomUInt32FieldValuePrinter : public TextFormat::FieldValuePrinter { |
| 397 | public: |
| 398 | virtual string PrintUInt32(uint32 val) const { |
| 399 | return StrCat(FieldValuePrinter::PrintUInt32(val), "u"); |
| 400 | } |
| 401 | }; |
| 402 | |
| 403 | TEST_F(TextFormatTest, DefaultCustomFieldPrinter) { |
| 404 | protobuf_unittest::TestAllTypes message; |
| 405 | |
| 406 | message.set_optional_uint32(42); |
| 407 | message.add_repeated_uint32(1); |
| 408 | message.add_repeated_uint32(2); |
| 409 | message.add_repeated_uint32(3); |
| 410 | |
| 411 | TextFormat::Printer printer; |
| 412 | printer.SetDefaultFieldValuePrinter(new CustomUInt32FieldValuePrinter()); |
| 413 | // Let's see if that works well together with the repeated primitives: |
| 414 | printer.SetUseShortRepeatedPrimitives(true); |
| 415 | string text; |
| 416 | printer.PrintToString(message, &text); |
| 417 | EXPECT_EQ("optional_uint32: 42u\nrepeated_uint32: [1u, 2u, 3u]\n", text); |
| 418 | } |
| 419 | |
| 420 | class CustomInt32FieldValuePrinter : public TextFormat::FieldValuePrinter { |
| 421 | public: |
| 422 | virtual string PrintInt32(int32 val) const { |
| 423 | return StrCat("value-is(", FieldValuePrinter::PrintInt32(val), ")"); |
| 424 | } |
| 425 | }; |
| 426 | |
| 427 | TEST_F(TextFormatTest, FieldSpecificCustomPrinter) { |
| 428 | protobuf_unittest::TestAllTypes message; |
| 429 | |
| 430 | message.set_optional_int32(42); // This will be handled by our Printer. |
| 431 | message.add_repeated_int32(42); // This will be printed as number. |
| 432 | |
| 433 | TextFormat::Printer printer; |
| 434 | EXPECT_TRUE(printer.RegisterFieldValuePrinter( |
| 435 | message.GetDescriptor()->FindFieldByName("optional_int32"), |
| 436 | new CustomInt32FieldValuePrinter())); |
| 437 | string text; |
| 438 | printer.PrintToString(message, &text); |
| 439 | EXPECT_EQ("optional_int32: value-is(42)\nrepeated_int32: 42\n", text); |
| 440 | } |
| 441 | |
| 442 | TEST_F(TextFormatTest, ErrorCasesRegisteringFieldValuePrinterShouldFail) { |
| 443 | protobuf_unittest::TestAllTypes message; |
| 444 | TextFormat::Printer printer; |
| 445 | // NULL printer. |
| 446 | EXPECT_FALSE(printer.RegisterFieldValuePrinter( |
| 447 | message.GetDescriptor()->FindFieldByName("optional_int32"), |
| 448 | NULL)); |
| 449 | // Because registration fails, the ownership of this printer is never taken. |
| 450 | TextFormat::FieldValuePrinter my_field_printer; |
| 451 | // NULL field |
| 452 | EXPECT_FALSE(printer.RegisterFieldValuePrinter(NULL, &my_field_printer)); |
| 453 | } |
| 454 | |
| 455 | class CustomMessageFieldValuePrinter : public TextFormat::FieldValuePrinter { |
| 456 | public: |
| 457 | virtual string PrintInt32(int32 v) const { |
Bo Yang | 5db2173 | 2015-05-21 14:28:59 -0700 | [diff] [blame] | 458 | return StrCat(FieldValuePrinter::PrintInt32(v), " # x", strings::Hex(v)); |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | virtual string PrintMessageStart(const Message& message, |
| 462 | int field_index, |
| 463 | int field_count, |
| 464 | bool single_line_mode) const { |
| 465 | if (single_line_mode) { |
| 466 | return " { "; |
| 467 | } |
| 468 | return StrCat( |
| 469 | " { # ", message.GetDescriptor()->name(), ": ", field_index, "\n"); |
| 470 | } |
| 471 | }; |
| 472 | |
| 473 | TEST_F(TextFormatTest, CustomPrinterForComments) { |
| 474 | protobuf_unittest::TestAllTypes message; |
| 475 | message.mutable_optional_nested_message(); |
| 476 | message.mutable_optional_import_message()->set_d(42); |
| 477 | message.add_repeated_nested_message(); |
| 478 | message.add_repeated_nested_message(); |
| 479 | message.add_repeated_import_message()->set_d(43); |
| 480 | message.add_repeated_import_message()->set_d(44); |
| 481 | TextFormat::Printer printer; |
| 482 | CustomMessageFieldValuePrinter my_field_printer; |
| 483 | printer.SetDefaultFieldValuePrinter(new CustomMessageFieldValuePrinter()); |
| 484 | string text; |
| 485 | printer.PrintToString(message, &text); |
| 486 | EXPECT_EQ( |
| 487 | "optional_nested_message { # NestedMessage: -1\n" |
| 488 | "}\n" |
| 489 | "optional_import_message { # ImportMessage: -1\n" |
| 490 | " d: 42 # x2a\n" |
| 491 | "}\n" |
| 492 | "repeated_nested_message { # NestedMessage: 0\n" |
| 493 | "}\n" |
| 494 | "repeated_nested_message { # NestedMessage: 1\n" |
| 495 | "}\n" |
| 496 | "repeated_import_message { # ImportMessage: 0\n" |
| 497 | " d: 43 # x2b\n" |
| 498 | "}\n" |
| 499 | "repeated_import_message { # ImportMessage: 1\n" |
| 500 | " d: 44 # x2c\n" |
| 501 | "}\n", |
| 502 | text); |
| 503 | } |
| 504 | |
| 505 | class CustomMultilineCommentPrinter : public TextFormat::FieldValuePrinter { |
| 506 | public: |
| 507 | virtual string PrintMessageStart(const Message& message, |
| 508 | int field_index, |
| 509 | int field_count, |
| 510 | bool single_line_comment) const { |
| 511 | return StrCat(" { # 1\n", " # 2\n"); |
| 512 | } |
| 513 | }; |
| 514 | |
| 515 | TEST_F(TextFormatTest, CustomPrinterForMultilineComments) { |
| 516 | protobuf_unittest::TestAllTypes message; |
| 517 | message.mutable_optional_nested_message(); |
| 518 | message.mutable_optional_import_message()->set_d(42); |
| 519 | TextFormat::Printer printer; |
| 520 | CustomMessageFieldValuePrinter my_field_printer; |
| 521 | printer.SetDefaultFieldValuePrinter(new CustomMultilineCommentPrinter()); |
| 522 | string text; |
| 523 | printer.PrintToString(message, &text); |
| 524 | EXPECT_EQ( |
| 525 | "optional_nested_message { # 1\n" |
| 526 | " # 2\n" |
| 527 | "}\n" |
| 528 | "optional_import_message { # 1\n" |
| 529 | " # 2\n" |
| 530 | " d: 42\n" |
| 531 | "}\n", |
| 532 | text); |
| 533 | } |
| 534 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 535 | TEST_F(TextFormatTest, ParseBasic) { |
| 536 | io::ArrayInputStream input_stream(proto_debug_string_.data(), |
| 537 | proto_debug_string_.size()); |
| 538 | TextFormat::Parse(&input_stream, &proto_); |
| 539 | TestUtil::ExpectAllFieldsSet(proto_); |
| 540 | } |
| 541 | |
| 542 | TEST_F(TextFormatExtensionsTest, ParseExtensions) { |
| 543 | io::ArrayInputStream input_stream(proto_debug_string_.data(), |
| 544 | proto_debug_string_.size()); |
| 545 | TextFormat::Parse(&input_stream, &proto_); |
| 546 | TestUtil::ExpectAllExtensionsSet(proto_); |
| 547 | } |
| 548 | |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 549 | TEST_F(TextFormatTest, ParseEnumFieldFromNumber) { |
| 550 | // Create a parse string with a numerical value for an enum field. |
| 551 | string parse_string = strings::Substitute("optional_nested_enum: $0", |
| 552 | unittest::TestAllTypes::BAZ); |
| 553 | EXPECT_TRUE(TextFormat::ParseFromString(parse_string, &proto_)); |
| 554 | EXPECT_TRUE(proto_.has_optional_nested_enum()); |
| 555 | EXPECT_EQ(unittest::TestAllTypes::BAZ, proto_.optional_nested_enum()); |
| 556 | } |
| 557 | |
| 558 | TEST_F(TextFormatTest, ParseEnumFieldFromNegativeNumber) { |
| 559 | ASSERT_LT(unittest::SPARSE_E, 0); |
| 560 | string parse_string = strings::Substitute("sparse_enum: $0", |
| 561 | unittest::SPARSE_E); |
| 562 | unittest::SparseEnumMessage proto; |
| 563 | EXPECT_TRUE(TextFormat::ParseFromString(parse_string, &proto)); |
| 564 | EXPECT_TRUE(proto.has_sparse_enum()); |
| 565 | EXPECT_EQ(unittest::SPARSE_E, proto.sparse_enum()); |
| 566 | } |
| 567 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 568 | TEST_F(TextFormatTest, ParseStringEscape) { |
| 569 | // Create a parse string with escpaed characters in it. |
| 570 | string parse_string = "optional_string: " |
| 571 | + kEscapeTestStringEscaped |
| 572 | + "\n"; |
| 573 | |
| 574 | io::ArrayInputStream input_stream(parse_string.data(), |
| 575 | parse_string.size()); |
| 576 | TextFormat::Parse(&input_stream, &proto_); |
| 577 | |
| 578 | // Compare. |
| 579 | EXPECT_EQ(kEscapeTestString, proto_.optional_string()); |
| 580 | } |
| 581 | |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 582 | TEST_F(TextFormatTest, ParseConcatenatedString) { |
| 583 | // Create a parse string with multiple parts on one line. |
| 584 | string parse_string = "optional_string: \"foo\" \"bar\"\n"; |
| 585 | |
| 586 | io::ArrayInputStream input_stream1(parse_string.data(), |
| 587 | parse_string.size()); |
| 588 | TextFormat::Parse(&input_stream1, &proto_); |
| 589 | |
| 590 | // Compare. |
| 591 | EXPECT_EQ("foobar", proto_.optional_string()); |
| 592 | |
Veres Lajos | c768072 | 2014-11-08 22:59:34 +0000 | [diff] [blame] | 593 | // Create a parse string with multiple parts on separate lines. |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 594 | parse_string = "optional_string: \"foo\"\n" |
| 595 | "\"bar\"\n"; |
| 596 | |
| 597 | io::ArrayInputStream input_stream2(parse_string.data(), |
| 598 | parse_string.size()); |
| 599 | TextFormat::Parse(&input_stream2, &proto_); |
| 600 | |
| 601 | // Compare. |
| 602 | EXPECT_EQ("foobar", proto_.optional_string()); |
| 603 | } |
| 604 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 605 | TEST_F(TextFormatTest, ParseFloatWithSuffix) { |
| 606 | // Test that we can parse a floating-point value with 'f' appended to the |
| 607 | // end. This is needed for backwards-compatibility with proto1. |
| 608 | |
| 609 | // Have it parse a float with the 'f' suffix. |
| 610 | string parse_string = "optional_float: 1.0f\n"; |
| 611 | |
| 612 | io::ArrayInputStream input_stream(parse_string.data(), |
| 613 | parse_string.size()); |
| 614 | |
| 615 | TextFormat::Parse(&input_stream, &proto_); |
| 616 | |
| 617 | // Compare. |
| 618 | EXPECT_EQ(1.0, proto_.optional_float()); |
| 619 | } |
| 620 | |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 621 | TEST_F(TextFormatTest, ParseShortRepeatedForm) { |
| 622 | string parse_string = |
| 623 | // Mixed short-form and long-form are simply concatenated. |
| 624 | "repeated_int32: 1\n" |
| 625 | "repeated_int32: [456, 789]\n" |
| 626 | "repeated_nested_enum: [ FOO ,BAR, # comment\n" |
| 627 | " 3]\n" |
| 628 | // Note that while the printer won't print repeated strings in short-form, |
| 629 | // the parser will accept them. |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 630 | "repeated_string: [ \"foo\", 'bar' ]\n" |
| 631 | // Repeated message |
| 632 | "repeated_nested_message: [ { bb: 1 }, { bb : 2 }]\n" |
| 633 | // Repeated group |
| 634 | "RepeatedGroup [{ a: 3 },{ a: 4 }]\n"; |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 635 | |
| 636 | ASSERT_TRUE(TextFormat::ParseFromString(parse_string, &proto_)); |
| 637 | |
| 638 | ASSERT_EQ(3, proto_.repeated_int32_size()); |
| 639 | EXPECT_EQ(1, proto_.repeated_int32(0)); |
| 640 | EXPECT_EQ(456, proto_.repeated_int32(1)); |
| 641 | EXPECT_EQ(789, proto_.repeated_int32(2)); |
| 642 | |
| 643 | ASSERT_EQ(3, proto_.repeated_nested_enum_size()); |
| 644 | EXPECT_EQ(unittest::TestAllTypes::FOO, proto_.repeated_nested_enum(0)); |
| 645 | EXPECT_EQ(unittest::TestAllTypes::BAR, proto_.repeated_nested_enum(1)); |
| 646 | EXPECT_EQ(unittest::TestAllTypes::BAZ, proto_.repeated_nested_enum(2)); |
| 647 | |
| 648 | ASSERT_EQ(2, proto_.repeated_string_size()); |
| 649 | EXPECT_EQ("foo", proto_.repeated_string(0)); |
| 650 | EXPECT_EQ("bar", proto_.repeated_string(1)); |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 651 | |
| 652 | ASSERT_EQ(2, proto_.repeated_nested_message_size()); |
| 653 | EXPECT_EQ(1, proto_.repeated_nested_message(0).bb()); |
| 654 | EXPECT_EQ(2, proto_.repeated_nested_message(1).bb()); |
| 655 | |
| 656 | ASSERT_EQ(2, proto_.repeatedgroup_size()); |
| 657 | EXPECT_EQ(3, proto_.repeatedgroup(0).a()); |
| 658 | EXPECT_EQ(4, proto_.repeatedgroup(1).a()); |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 659 | } |
| 660 | |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 661 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 662 | TEST_F(TextFormatTest, Comments) { |
| 663 | // Test that comments are ignored. |
| 664 | |
| 665 | string parse_string = "optional_int32: 1 # a comment\n" |
| 666 | "optional_int64: 2 # another comment"; |
| 667 | |
| 668 | io::ArrayInputStream input_stream(parse_string.data(), |
| 669 | parse_string.size()); |
| 670 | |
| 671 | TextFormat::Parse(&input_stream, &proto_); |
| 672 | |
| 673 | // Compare. |
| 674 | EXPECT_EQ(1, proto_.optional_int32()); |
| 675 | EXPECT_EQ(2, proto_.optional_int64()); |
| 676 | } |
| 677 | |
| 678 | TEST_F(TextFormatTest, OptionalColon) { |
| 679 | // Test that we can place a ':' after the field name of a nested message, |
| 680 | // even though we don't have to. |
| 681 | |
| 682 | string parse_string = "optional_nested_message: { bb: 1}\n"; |
| 683 | |
| 684 | io::ArrayInputStream input_stream(parse_string.data(), |
| 685 | parse_string.size()); |
| 686 | |
| 687 | TextFormat::Parse(&input_stream, &proto_); |
| 688 | |
| 689 | // Compare. |
| 690 | EXPECT_TRUE(proto_.has_optional_nested_message()); |
| 691 | EXPECT_EQ(1, proto_.optional_nested_message().bb()); |
| 692 | } |
| 693 | |
| 694 | // Some platforms (e.g. Windows) insist on padding the exponent to three |
| 695 | // digits when one or two would be just fine. |
| 696 | static string RemoveRedundantZeros(string text) { |
| 697 | text = StringReplace(text, "e+0", "e+", true); |
| 698 | text = StringReplace(text, "e-0", "e-", true); |
| 699 | return text; |
| 700 | } |
| 701 | |
| 702 | TEST_F(TextFormatTest, PrintExotic) { |
| 703 | unittest::TestAllTypes message; |
| 704 | |
| 705 | // Note: In C, a negative integer literal is actually the unary negation |
| 706 | // operator being applied to a positive integer literal, and |
| 707 | // 9223372036854775808 is outside the range of int64. However, it is not |
| 708 | // outside the range of uint64. Confusingly, this means that everything |
| 709 | // works if we make the literal unsigned, even though we are negating it. |
| 710 | message.add_repeated_int64(-GOOGLE_ULONGLONG(9223372036854775808)); |
| 711 | message.add_repeated_uint64(GOOGLE_ULONGLONG(18446744073709551615)); |
| 712 | message.add_repeated_double(123.456); |
| 713 | message.add_repeated_double(1.23e21); |
| 714 | message.add_repeated_double(1.23e-18); |
| 715 | message.add_repeated_double(std::numeric_limits<double>::infinity()); |
| 716 | message.add_repeated_double(-std::numeric_limits<double>::infinity()); |
| 717 | message.add_repeated_double(std::numeric_limits<double>::quiet_NaN()); |
| 718 | message.add_repeated_string(string("\000\001\a\b\f\n\r\t\v\\\'\"", 12)); |
| 719 | |
| 720 | // Fun story: We used to use 1.23e22 instead of 1.23e21 above, but this |
| 721 | // seemed to trigger an odd case on MinGW/GCC 3.4.5 where GCC's parsing of |
| 722 | // the value differed from strtod()'s parsing. That is to say, the |
| 723 | // following assertion fails on MinGW: |
| 724 | // assert(1.23e22 == strtod("1.23e22", NULL)); |
| 725 | // As a result, SimpleDtoa() would print the value as |
| 726 | // "1.2300000000000001e+22" to make sure strtod() produce the exact same |
| 727 | // result. Our goal is to test runtime parsing, not compile-time parsing, |
| 728 | // so this wasn't our problem. It was found that using 1.23e21 did not |
| 729 | // have this problem, so we switched to that instead. |
| 730 | |
| 731 | EXPECT_EQ( |
| 732 | "repeated_int64: -9223372036854775808\n" |
| 733 | "repeated_uint64: 18446744073709551615\n" |
| 734 | "repeated_double: 123.456\n" |
| 735 | "repeated_double: 1.23e+21\n" |
| 736 | "repeated_double: 1.23e-18\n" |
| 737 | "repeated_double: inf\n" |
| 738 | "repeated_double: -inf\n" |
| 739 | "repeated_double: nan\n" |
| 740 | "repeated_string: \"\\000\\001\\007\\010\\014\\n\\r\\t\\013\\\\\\'\\\"\"\n", |
| 741 | RemoveRedundantZeros(message.DebugString())); |
| 742 | } |
| 743 | |
| 744 | TEST_F(TextFormatTest, PrintFloatPrecision) { |
| 745 | unittest::TestAllTypes message; |
| 746 | |
| 747 | message.add_repeated_float(1.2); |
| 748 | message.add_repeated_float(1.23); |
| 749 | message.add_repeated_float(1.234); |
| 750 | message.add_repeated_float(1.2345); |
| 751 | message.add_repeated_float(1.23456); |
| 752 | message.add_repeated_float(1.2e10); |
| 753 | message.add_repeated_float(1.23e10); |
| 754 | message.add_repeated_float(1.234e10); |
| 755 | message.add_repeated_float(1.2345e10); |
| 756 | message.add_repeated_float(1.23456e10); |
| 757 | message.add_repeated_double(1.2); |
| 758 | message.add_repeated_double(1.23); |
| 759 | message.add_repeated_double(1.234); |
| 760 | message.add_repeated_double(1.2345); |
| 761 | message.add_repeated_double(1.23456); |
| 762 | message.add_repeated_double(1.234567); |
| 763 | message.add_repeated_double(1.2345678); |
| 764 | message.add_repeated_double(1.23456789); |
| 765 | message.add_repeated_double(1.234567898); |
| 766 | message.add_repeated_double(1.2345678987); |
| 767 | message.add_repeated_double(1.23456789876); |
| 768 | message.add_repeated_double(1.234567898765); |
| 769 | message.add_repeated_double(1.2345678987654); |
| 770 | message.add_repeated_double(1.23456789876543); |
| 771 | message.add_repeated_double(1.2e100); |
| 772 | message.add_repeated_double(1.23e100); |
| 773 | message.add_repeated_double(1.234e100); |
| 774 | message.add_repeated_double(1.2345e100); |
| 775 | message.add_repeated_double(1.23456e100); |
| 776 | message.add_repeated_double(1.234567e100); |
| 777 | message.add_repeated_double(1.2345678e100); |
| 778 | message.add_repeated_double(1.23456789e100); |
| 779 | message.add_repeated_double(1.234567898e100); |
| 780 | message.add_repeated_double(1.2345678987e100); |
| 781 | message.add_repeated_double(1.23456789876e100); |
| 782 | message.add_repeated_double(1.234567898765e100); |
| 783 | message.add_repeated_double(1.2345678987654e100); |
| 784 | message.add_repeated_double(1.23456789876543e100); |
| 785 | |
| 786 | EXPECT_EQ( |
| 787 | "repeated_float: 1.2\n" |
| 788 | "repeated_float: 1.23\n" |
| 789 | "repeated_float: 1.234\n" |
| 790 | "repeated_float: 1.2345\n" |
| 791 | "repeated_float: 1.23456\n" |
| 792 | "repeated_float: 1.2e+10\n" |
| 793 | "repeated_float: 1.23e+10\n" |
| 794 | "repeated_float: 1.234e+10\n" |
| 795 | "repeated_float: 1.2345e+10\n" |
| 796 | "repeated_float: 1.23456e+10\n" |
| 797 | "repeated_double: 1.2\n" |
| 798 | "repeated_double: 1.23\n" |
| 799 | "repeated_double: 1.234\n" |
| 800 | "repeated_double: 1.2345\n" |
| 801 | "repeated_double: 1.23456\n" |
| 802 | "repeated_double: 1.234567\n" |
| 803 | "repeated_double: 1.2345678\n" |
| 804 | "repeated_double: 1.23456789\n" |
| 805 | "repeated_double: 1.234567898\n" |
| 806 | "repeated_double: 1.2345678987\n" |
| 807 | "repeated_double: 1.23456789876\n" |
| 808 | "repeated_double: 1.234567898765\n" |
| 809 | "repeated_double: 1.2345678987654\n" |
| 810 | "repeated_double: 1.23456789876543\n" |
| 811 | "repeated_double: 1.2e+100\n" |
| 812 | "repeated_double: 1.23e+100\n" |
| 813 | "repeated_double: 1.234e+100\n" |
| 814 | "repeated_double: 1.2345e+100\n" |
| 815 | "repeated_double: 1.23456e+100\n" |
| 816 | "repeated_double: 1.234567e+100\n" |
| 817 | "repeated_double: 1.2345678e+100\n" |
| 818 | "repeated_double: 1.23456789e+100\n" |
| 819 | "repeated_double: 1.234567898e+100\n" |
| 820 | "repeated_double: 1.2345678987e+100\n" |
| 821 | "repeated_double: 1.23456789876e+100\n" |
| 822 | "repeated_double: 1.234567898765e+100\n" |
| 823 | "repeated_double: 1.2345678987654e+100\n" |
| 824 | "repeated_double: 1.23456789876543e+100\n", |
| 825 | RemoveRedundantZeros(message.DebugString())); |
| 826 | } |
| 827 | |
| 828 | |
| 829 | TEST_F(TextFormatTest, AllowPartial) { |
| 830 | unittest::TestRequired message; |
| 831 | TextFormat::Parser parser; |
| 832 | parser.AllowPartialMessage(true); |
| 833 | EXPECT_TRUE(parser.ParseFromString("a: 1", &message)); |
| 834 | EXPECT_EQ(1, message.a()); |
| 835 | EXPECT_FALSE(message.has_b()); |
| 836 | EXPECT_FALSE(message.has_c()); |
| 837 | } |
| 838 | |
| 839 | TEST_F(TextFormatTest, ParseExotic) { |
| 840 | unittest::TestAllTypes message; |
| 841 | ASSERT_TRUE(TextFormat::ParseFromString( |
| 842 | "repeated_int32: -1\n" |
| 843 | "repeated_int32: -2147483648\n" |
| 844 | "repeated_int64: -1\n" |
| 845 | "repeated_int64: -9223372036854775808\n" |
| 846 | "repeated_uint32: 4294967295\n" |
| 847 | "repeated_uint32: 2147483648\n" |
| 848 | "repeated_uint64: 18446744073709551615\n" |
| 849 | "repeated_uint64: 9223372036854775808\n" |
| 850 | "repeated_double: 123.0\n" |
| 851 | "repeated_double: 123.5\n" |
| 852 | "repeated_double: 0.125\n" |
| 853 | "repeated_double: 1.23E17\n" |
| 854 | "repeated_double: 1.235E+22\n" |
| 855 | "repeated_double: 1.235e-18\n" |
| 856 | "repeated_double: 123.456789\n" |
| 857 | "repeated_double: inf\n" |
| 858 | "repeated_double: Infinity\n" |
| 859 | "repeated_double: -inf\n" |
| 860 | "repeated_double: -Infinity\n" |
| 861 | "repeated_double: nan\n" |
| 862 | "repeated_double: NaN\n" |
| 863 | "repeated_string: \"\\000\\001\\a\\b\\f\\n\\r\\t\\v\\\\\\'\\\"\"\n", |
| 864 | &message)); |
| 865 | |
| 866 | ASSERT_EQ(2, message.repeated_int32_size()); |
| 867 | EXPECT_EQ(-1, message.repeated_int32(0)); |
| 868 | // Note: In C, a negative integer literal is actually the unary negation |
| 869 | // operator being applied to a positive integer literal, and 2147483648 is |
| 870 | // outside the range of int32. However, it is not outside the range of |
| 871 | // uint32. Confusingly, this means that everything works if we make the |
| 872 | // literal unsigned, even though we are negating it. |
| 873 | EXPECT_EQ(-2147483648u, message.repeated_int32(1)); |
| 874 | |
| 875 | ASSERT_EQ(2, message.repeated_int64_size()); |
| 876 | EXPECT_EQ(-1, message.repeated_int64(0)); |
| 877 | // Note: In C, a negative integer literal is actually the unary negation |
| 878 | // operator being applied to a positive integer literal, and |
| 879 | // 9223372036854775808 is outside the range of int64. However, it is not |
| 880 | // outside the range of uint64. Confusingly, this means that everything |
| 881 | // works if we make the literal unsigned, even though we are negating it. |
| 882 | EXPECT_EQ(-GOOGLE_ULONGLONG(9223372036854775808), message.repeated_int64(1)); |
| 883 | |
| 884 | ASSERT_EQ(2, message.repeated_uint32_size()); |
| 885 | EXPECT_EQ(4294967295u, message.repeated_uint32(0)); |
| 886 | EXPECT_EQ(2147483648u, message.repeated_uint32(1)); |
| 887 | |
| 888 | ASSERT_EQ(2, message.repeated_uint64_size()); |
| 889 | EXPECT_EQ(GOOGLE_ULONGLONG(18446744073709551615), message.repeated_uint64(0)); |
| 890 | EXPECT_EQ(GOOGLE_ULONGLONG(9223372036854775808), message.repeated_uint64(1)); |
| 891 | |
| 892 | ASSERT_EQ(13, message.repeated_double_size()); |
| 893 | EXPECT_EQ(123.0 , message.repeated_double(0)); |
| 894 | EXPECT_EQ(123.5 , message.repeated_double(1)); |
| 895 | EXPECT_EQ(0.125 , message.repeated_double(2)); |
| 896 | EXPECT_EQ(1.23E17 , message.repeated_double(3)); |
| 897 | EXPECT_EQ(1.235E22 , message.repeated_double(4)); |
| 898 | EXPECT_EQ(1.235E-18 , message.repeated_double(5)); |
| 899 | EXPECT_EQ(123.456789, message.repeated_double(6)); |
| 900 | EXPECT_EQ(message.repeated_double(7), numeric_limits<double>::infinity()); |
| 901 | EXPECT_EQ(message.repeated_double(8), numeric_limits<double>::infinity()); |
| 902 | EXPECT_EQ(message.repeated_double(9), -numeric_limits<double>::infinity()); |
| 903 | EXPECT_EQ(message.repeated_double(10), -numeric_limits<double>::infinity()); |
Karol Ostrovsky | ee35402 | 2015-06-29 16:13:33 +0200 | [diff] [blame] | 904 | EXPECT_TRUE(MathLimits<double>::IsNaN(message.repeated_double(11))); |
| 905 | EXPECT_TRUE(MathLimits<double>::IsNaN(message.repeated_double(12))); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 906 | |
| 907 | // Note: Since these string literals have \0's in them, we must explicitly |
| 908 | // pass their sizes to string's constructor. |
| 909 | ASSERT_EQ(1, message.repeated_string_size()); |
| 910 | EXPECT_EQ(string("\000\001\a\b\f\n\r\t\v\\\'\"", 12), |
| 911 | message.repeated_string(0)); |
| 912 | } |
| 913 | |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 914 | TEST_F(TextFormatTest, PrintFieldsInIndexOrder) { |
| 915 | protobuf_unittest::TestFieldOrderings message; |
| 916 | // Fields are listed in index order instead of field number. |
| 917 | message.set_my_string("Test String"); // Field number 11 |
| 918 | message.set_my_int(12345); // Field number 1 |
| 919 | message.set_my_float(0.999); // Field number 101 |
| 920 | TextFormat::Printer printer; |
| 921 | string text; |
| 922 | |
| 923 | // By default, print in field number order. |
| 924 | printer.PrintToString(message, &text); |
| 925 | EXPECT_EQ("my_int: 12345\nmy_string: \"Test String\"\nmy_float: 0.999\n", |
| 926 | text); |
| 927 | |
| 928 | // Print in index order. |
| 929 | printer.SetPrintMessageFieldsInIndexOrder(true); |
| 930 | printer.PrintToString(message, &text); |
| 931 | EXPECT_EQ("my_string: \"Test String\"\nmy_int: 12345\nmy_float: 0.999\n", |
| 932 | text); |
| 933 | } |
| 934 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 935 | class TextFormatParserTest : public testing::Test { |
| 936 | protected: |
| 937 | void ExpectFailure(const string& input, const string& message, int line, |
| 938 | int col) { |
Jisi Liu | 46e8ff6 | 2015-10-05 11:59:43 -0700 | [diff] [blame] | 939 | google::protobuf::scoped_ptr<unittest::TestAllTypes> proto(new unittest::TestAllTypes); |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 940 | ExpectFailure(input, message, line, col, proto.get()); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 941 | } |
| 942 | |
| 943 | void ExpectFailure(const string& input, const string& message, int line, |
| 944 | int col, Message* proto) { |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 945 | ExpectMessage(input, message, line, col, proto, false); |
| 946 | } |
| 947 | |
| 948 | void ExpectMessage(const string& input, const string& message, int line, |
| 949 | int col, Message* proto, bool expected_result) { |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 950 | TextFormat::Parser parser; |
| 951 | MockErrorCollector error_collector; |
| 952 | parser.RecordErrorsTo(&error_collector); |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 953 | EXPECT_EQ(expected_result, parser.ParseFromString(input, proto)) |
| 954 | << input << " -> " << proto->DebugString(); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 955 | EXPECT_EQ(SimpleItoa(line) + ":" + SimpleItoa(col) + ": " + message + "\n", |
| 956 | error_collector.text_); |
| 957 | } |
| 958 | |
xiaofeng@google.com | b55a20f | 2012-09-22 02:40:50 +0000 | [diff] [blame] | 959 | void ExpectSuccessAndTree(const string& input, Message* proto, |
| 960 | TextFormat::ParseInfoTree* info_tree) { |
| 961 | TextFormat::Parser parser; |
| 962 | MockErrorCollector error_collector; |
| 963 | parser.RecordErrorsTo(&error_collector); |
| 964 | parser.WriteLocationsTo(info_tree); |
| 965 | |
| 966 | EXPECT_TRUE(parser.ParseFromString(input, proto)); |
| 967 | } |
| 968 | |
| 969 | void ExpectLocation(TextFormat::ParseInfoTree* tree, |
| 970 | const Descriptor* d, const string& field_name, |
| 971 | int index, int line, int column) { |
| 972 | TextFormat::ParseLocation location = tree->GetLocation( |
| 973 | d->FindFieldByName(field_name), index); |
| 974 | EXPECT_EQ(line, location.line); |
| 975 | EXPECT_EQ(column, location.column); |
| 976 | } |
| 977 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 978 | // An error collector which simply concatenates all its errors into a big |
| 979 | // block of text which can be checked. |
| 980 | class MockErrorCollector : public io::ErrorCollector { |
| 981 | public: |
| 982 | MockErrorCollector() {} |
| 983 | ~MockErrorCollector() {} |
| 984 | |
| 985 | string text_; |
| 986 | |
| 987 | // implements ErrorCollector ------------------------------------- |
| 988 | void AddError(int line, int column, const string& message) { |
| 989 | strings::SubstituteAndAppend(&text_, "$0:$1: $2\n", |
| 990 | line + 1, column + 1, message); |
| 991 | } |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 992 | |
| 993 | void AddWarning(int line, int column, const string& message) { |
| 994 | AddError(line, column, "WARNING:" + message); |
| 995 | } |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 996 | }; |
| 997 | }; |
| 998 | |
xiaofeng@google.com | b55a20f | 2012-09-22 02:40:50 +0000 | [diff] [blame] | 999 | TEST_F(TextFormatParserTest, ParseInfoTreeBuilding) { |
Jisi Liu | 46e8ff6 | 2015-10-05 11:59:43 -0700 | [diff] [blame] | 1000 | google::protobuf::scoped_ptr<unittest::TestAllTypes> message(new unittest::TestAllTypes); |
xiaofeng@google.com | b55a20f | 2012-09-22 02:40:50 +0000 | [diff] [blame] | 1001 | const Descriptor* d = message->GetDescriptor(); |
| 1002 | |
| 1003 | string stringData = |
| 1004 | "optional_int32: 1\n" |
| 1005 | "optional_int64: 2\n" |
| 1006 | " optional_double: 2.4\n" |
| 1007 | "repeated_int32: 5\n" |
| 1008 | "repeated_int32: 10\n" |
| 1009 | "optional_nested_message <\n" |
| 1010 | " bb: 78\n" |
| 1011 | ">\n" |
| 1012 | "repeated_nested_message <\n" |
| 1013 | " bb: 79\n" |
| 1014 | ">\n" |
| 1015 | "repeated_nested_message <\n" |
| 1016 | " bb: 80\n" |
| 1017 | ">"; |
| 1018 | |
| 1019 | |
| 1020 | TextFormat::ParseInfoTree tree; |
| 1021 | ExpectSuccessAndTree(stringData, message.get(), &tree); |
| 1022 | |
| 1023 | // Verify that the tree has the correct positions. |
| 1024 | ExpectLocation(&tree, d, "optional_int32", -1, 0, 0); |
| 1025 | ExpectLocation(&tree, d, "optional_int64", -1, 1, 0); |
| 1026 | ExpectLocation(&tree, d, "optional_double", -1, 2, 2); |
| 1027 | |
| 1028 | ExpectLocation(&tree, d, "repeated_int32", 0, 3, 0); |
| 1029 | ExpectLocation(&tree, d, "repeated_int32", 1, 4, 0); |
| 1030 | |
| 1031 | ExpectLocation(&tree, d, "optional_nested_message", -1, 5, 0); |
| 1032 | ExpectLocation(&tree, d, "repeated_nested_message", 0, 8, 0); |
| 1033 | ExpectLocation(&tree, d, "repeated_nested_message", 1, 11, 0); |
| 1034 | |
| 1035 | // Check for fields not set. For an invalid field, the location returned |
| 1036 | // should be -1, -1. |
| 1037 | ExpectLocation(&tree, d, "repeated_int64", 0, -1, -1); |
| 1038 | ExpectLocation(&tree, d, "repeated_int32", 6, -1, -1); |
| 1039 | ExpectLocation(&tree, d, "some_unknown_field", -1, -1, -1); |
| 1040 | |
| 1041 | // Verify inside the nested message. |
| 1042 | const FieldDescriptor* nested_field = |
| 1043 | d->FindFieldByName("optional_nested_message"); |
| 1044 | |
| 1045 | TextFormat::ParseInfoTree* nested_tree = |
| 1046 | tree.GetTreeForNested(nested_field, -1); |
| 1047 | ExpectLocation(nested_tree, nested_field->message_type(), "bb", -1, 6, 2); |
| 1048 | |
| 1049 | // Verify inside another nested message. |
| 1050 | nested_field = d->FindFieldByName("repeated_nested_message"); |
| 1051 | nested_tree = tree.GetTreeForNested(nested_field, 0); |
| 1052 | ExpectLocation(nested_tree, nested_field->message_type(), "bb", -1, 9, 2); |
| 1053 | |
| 1054 | nested_tree = tree.GetTreeForNested(nested_field, 1); |
| 1055 | ExpectLocation(nested_tree, nested_field->message_type(), "bb", -1, 12, 2); |
| 1056 | |
| 1057 | // Verify a NULL tree for an unknown nested field. |
| 1058 | TextFormat::ParseInfoTree* unknown_nested_tree = |
| 1059 | tree.GetTreeForNested(nested_field, 2); |
| 1060 | |
| 1061 | EXPECT_EQ(NULL, unknown_nested_tree); |
| 1062 | } |
| 1063 | |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 1064 | TEST_F(TextFormatParserTest, ParseFieldValueFromString) { |
Jisi Liu | 46e8ff6 | 2015-10-05 11:59:43 -0700 | [diff] [blame] | 1065 | google::protobuf::scoped_ptr<unittest::TestAllTypes> message(new unittest::TestAllTypes); |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 1066 | const Descriptor* d = message->GetDescriptor(); |
| 1067 | |
| 1068 | #define EXPECT_FIELD(name, value, valuestring) \ |
| 1069 | EXPECT_TRUE(TextFormat::ParseFieldValueFromString( \ |
| 1070 | valuestring, d->FindFieldByName("optional_" #name), message.get())); \ |
| 1071 | EXPECT_EQ(value, message->optional_##name()); \ |
| 1072 | EXPECT_TRUE(message->has_optional_##name()); |
| 1073 | |
liujisi@google.com | 2bee6e6 | 2012-12-05 22:29:30 +0000 | [diff] [blame] | 1074 | #define EXPECT_BOOL_FIELD(name, value, valuestring) \ |
| 1075 | EXPECT_TRUE(TextFormat::ParseFieldValueFromString( \ |
| 1076 | valuestring, d->FindFieldByName("optional_" #name), message.get())); \ |
| 1077 | EXPECT_TRUE(message->optional_##name() == value); \ |
| 1078 | EXPECT_TRUE(message->has_optional_##name()); |
| 1079 | |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 1080 | #define EXPECT_FLOAT_FIELD(name, value, valuestring) \ |
| 1081 | EXPECT_TRUE(TextFormat::ParseFieldValueFromString( \ |
| 1082 | valuestring, d->FindFieldByName("optional_" #name), message.get())); \ |
| 1083 | EXPECT_FLOAT_EQ(value, message->optional_##name()); \ |
| 1084 | EXPECT_TRUE(message->has_optional_##name()); |
| 1085 | |
| 1086 | #define EXPECT_DOUBLE_FIELD(name, value, valuestring) \ |
| 1087 | EXPECT_TRUE(TextFormat::ParseFieldValueFromString( \ |
| 1088 | valuestring, d->FindFieldByName("optional_" #name), message.get())); \ |
| 1089 | EXPECT_DOUBLE_EQ(value, message->optional_##name()); \ |
| 1090 | EXPECT_TRUE(message->has_optional_##name()); |
| 1091 | |
| 1092 | #define EXPECT_INVALID(name, valuestring) \ |
| 1093 | EXPECT_FALSE(TextFormat::ParseFieldValueFromString( \ |
| 1094 | valuestring, d->FindFieldByName("optional_" #name), message.get())); |
| 1095 | |
| 1096 | // int32 |
| 1097 | EXPECT_FIELD(int32, 1, "1"); |
| 1098 | EXPECT_FIELD(int32, -1, "-1"); |
| 1099 | EXPECT_FIELD(int32, 0x1234, "0x1234"); |
| 1100 | EXPECT_INVALID(int32, "a"); |
| 1101 | EXPECT_INVALID(int32, "999999999999999999999999999999999999"); |
| 1102 | EXPECT_INVALID(int32, "1,2"); |
| 1103 | |
| 1104 | // int64 |
| 1105 | EXPECT_FIELD(int64, 1, "1"); |
| 1106 | EXPECT_FIELD(int64, -1, "-1"); |
| 1107 | EXPECT_FIELD(int64, 0x1234567812345678LL, "0x1234567812345678"); |
| 1108 | EXPECT_INVALID(int64, "a"); |
| 1109 | EXPECT_INVALID(int64, "999999999999999999999999999999999999"); |
| 1110 | EXPECT_INVALID(int64, "1,2"); |
| 1111 | |
| 1112 | // uint64 |
| 1113 | EXPECT_FIELD(uint64, 1, "1"); |
| 1114 | EXPECT_FIELD(uint64, 0xf234567812345678ULL, "0xf234567812345678"); |
| 1115 | EXPECT_INVALID(uint64, "-1"); |
| 1116 | EXPECT_INVALID(uint64, "a"); |
| 1117 | EXPECT_INVALID(uint64, "999999999999999999999999999999999999"); |
| 1118 | EXPECT_INVALID(uint64, "1,2"); |
| 1119 | |
| 1120 | // fixed32 |
| 1121 | EXPECT_FIELD(fixed32, 1, "1"); |
| 1122 | EXPECT_FIELD(fixed32, 0x12345678, "0x12345678"); |
| 1123 | EXPECT_INVALID(fixed32, "-1"); |
| 1124 | EXPECT_INVALID(fixed32, "a"); |
| 1125 | EXPECT_INVALID(fixed32, "999999999999999999999999999999999999"); |
| 1126 | EXPECT_INVALID(fixed32, "1,2"); |
| 1127 | |
| 1128 | // fixed64 |
| 1129 | EXPECT_FIELD(fixed64, 1, "1"); |
| 1130 | EXPECT_FIELD(fixed64, 0x1234567812345678ULL, "0x1234567812345678"); |
| 1131 | EXPECT_INVALID(fixed64, "-1"); |
| 1132 | EXPECT_INVALID(fixed64, "a"); |
| 1133 | EXPECT_INVALID(fixed64, "999999999999999999999999999999999999"); |
| 1134 | EXPECT_INVALID(fixed64, "1,2"); |
| 1135 | |
| 1136 | // bool |
liujisi@google.com | 2bee6e6 | 2012-12-05 22:29:30 +0000 | [diff] [blame] | 1137 | EXPECT_BOOL_FIELD(bool, true, "true"); |
| 1138 | EXPECT_BOOL_FIELD(bool, false, "false"); |
| 1139 | EXPECT_BOOL_FIELD(bool, true, "1"); |
| 1140 | EXPECT_BOOL_FIELD(bool, true, "t"); |
| 1141 | EXPECT_BOOL_FIELD(bool, false, "0"); |
| 1142 | EXPECT_BOOL_FIELD(bool, false, "f"); |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 1143 | EXPECT_FIELD(bool, true, "True"); |
| 1144 | EXPECT_FIELD(bool, false, "False"); |
| 1145 | EXPECT_INVALID(bool, "tRue"); |
| 1146 | EXPECT_INVALID(bool, "faLse"); |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 1147 | EXPECT_INVALID(bool, "2"); |
| 1148 | EXPECT_INVALID(bool, "-0"); |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 1149 | EXPECT_INVALID(bool, "on"); |
| 1150 | EXPECT_INVALID(bool, "a"); |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 1151 | |
| 1152 | // float |
| 1153 | EXPECT_FIELD(float, 1, "1"); |
| 1154 | EXPECT_FLOAT_FIELD(float, 1.5, "1.5"); |
| 1155 | EXPECT_FLOAT_FIELD(float, 1.5e3, "1.5e3"); |
| 1156 | EXPECT_FLOAT_FIELD(float, -4.55, "-4.55"); |
| 1157 | EXPECT_INVALID(float, "a"); |
| 1158 | EXPECT_INVALID(float, "1,2"); |
| 1159 | |
| 1160 | // double |
| 1161 | EXPECT_FIELD(double, 1, "1"); |
| 1162 | EXPECT_FIELD(double, -1, "-1"); |
| 1163 | EXPECT_DOUBLE_FIELD(double, 2.3, "2.3"); |
| 1164 | EXPECT_DOUBLE_FIELD(double, 3e5, "3e5"); |
| 1165 | EXPECT_INVALID(double, "a"); |
| 1166 | EXPECT_INVALID(double, "1,2"); |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 1167 | // Rejects hex and oct numbers for a double field. |
| 1168 | EXPECT_INVALID(double, "0xf"); |
| 1169 | EXPECT_INVALID(double, "012"); |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 1170 | |
| 1171 | // string |
| 1172 | EXPECT_FIELD(string, "hello", "\"hello\""); |
| 1173 | EXPECT_FIELD(string, "-1.87", "'-1.87'"); |
| 1174 | EXPECT_INVALID(string, "hello"); // without quote for value |
| 1175 | |
| 1176 | // enum |
| 1177 | EXPECT_FIELD(nested_enum, unittest::TestAllTypes::BAR, "BAR"); |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 1178 | EXPECT_FIELD(nested_enum, unittest::TestAllTypes::BAZ, |
| 1179 | SimpleItoa(unittest::TestAllTypes::BAZ)); |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 1180 | EXPECT_INVALID(nested_enum, "FOOBAR"); |
| 1181 | |
| 1182 | // message |
| 1183 | EXPECT_TRUE(TextFormat::ParseFieldValueFromString( |
| 1184 | "<bb:12>", d->FindFieldByName("optional_nested_message"), message.get())); |
| 1185 | EXPECT_EQ(12, message->optional_nested_message().bb()); \ |
| 1186 | EXPECT_TRUE(message->has_optional_nested_message()); |
| 1187 | EXPECT_INVALID(nested_message, "any"); |
| 1188 | |
| 1189 | #undef EXPECT_FIELD |
liujisi@google.com | 2bee6e6 | 2012-12-05 22:29:30 +0000 | [diff] [blame] | 1190 | #undef EXPECT_BOOL_FIELD |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 1191 | #undef EXPECT_FLOAT_FIELD |
| 1192 | #undef EXPECT_DOUBLE_FIELD |
| 1193 | #undef EXPECT_INVALID |
| 1194 | } |
| 1195 | |
| 1196 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 1197 | TEST_F(TextFormatParserTest, InvalidToken) { |
| 1198 | ExpectFailure("optional_bool: true\n-5\n", "Expected identifier.", |
| 1199 | 2, 1); |
| 1200 | |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 1201 | ExpectFailure("optional_bool: true!\n", "Expected identifier.", 1, 20); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 1202 | ExpectFailure("\"some string\"", "Expected identifier.", 1, 1); |
| 1203 | } |
| 1204 | |
| 1205 | TEST_F(TextFormatParserTest, InvalidFieldName) { |
| 1206 | ExpectFailure( |
| 1207 | "invalid_field: somevalue\n", |
| 1208 | "Message type \"protobuf_unittest.TestAllTypes\" has no field named " |
| 1209 | "\"invalid_field\".", |
| 1210 | 1, 14); |
| 1211 | } |
| 1212 | |
| 1213 | TEST_F(TextFormatParserTest, InvalidCapitalization) { |
| 1214 | // We require that group names be exactly as they appear in the .proto. |
| 1215 | ExpectFailure( |
| 1216 | "optionalgroup {\na: 15\n}\n", |
| 1217 | "Message type \"protobuf_unittest.TestAllTypes\" has no field named " |
| 1218 | "\"optionalgroup\".", |
| 1219 | 1, 15); |
| 1220 | ExpectFailure( |
| 1221 | "OPTIONALgroup {\na: 15\n}\n", |
| 1222 | "Message type \"protobuf_unittest.TestAllTypes\" has no field named " |
| 1223 | "\"OPTIONALgroup\".", |
| 1224 | 1, 15); |
| 1225 | ExpectFailure( |
| 1226 | "Optional_Double: 10.0\n", |
| 1227 | "Message type \"protobuf_unittest.TestAllTypes\" has no field named " |
| 1228 | "\"Optional_Double\".", |
| 1229 | 1, 16); |
| 1230 | } |
| 1231 | |
jieluo@google.com | 4de8f55 | 2014-07-18 00:47:59 +0000 | [diff] [blame] | 1232 | TEST_F(TextFormatParserTest, AllowIgnoreCapitalizationError) { |
| 1233 | TextFormat::Parser parser; |
| 1234 | protobuf_unittest::TestAllTypes proto; |
| 1235 | |
| 1236 | // These fields have a mismatching case. |
| 1237 | EXPECT_FALSE(parser.ParseFromString("Optional_Double: 10.0", &proto)); |
| 1238 | EXPECT_FALSE(parser.ParseFromString("oPtIoNaLgRoUp { a: 15 }", &proto)); |
| 1239 | |
| 1240 | // ... but are parsed correctly if we match case insensitive. |
| 1241 | parser.AllowCaseInsensitiveField(true); |
| 1242 | EXPECT_TRUE(parser.ParseFromString("Optional_Double: 10.0", &proto)); |
| 1243 | EXPECT_EQ(10.0, proto.optional_double()); |
| 1244 | EXPECT_TRUE(parser.ParseFromString("oPtIoNaLgRoUp { a: 15 }", &proto)); |
| 1245 | EXPECT_EQ(15, proto.optionalgroup().a()); |
| 1246 | } |
| 1247 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 1248 | TEST_F(TextFormatParserTest, InvalidFieldValues) { |
| 1249 | // Invalid values for a double/float field. |
| 1250 | ExpectFailure("optional_double: \"hello\"\n", "Expected double.", 1, 18); |
| 1251 | ExpectFailure("optional_double: true\n", "Expected double.", 1, 18); |
| 1252 | ExpectFailure("optional_double: !\n", "Expected double.", 1, 18); |
| 1253 | ExpectFailure("optional_double {\n \n}\n", "Expected \":\", found \"{\".", |
| 1254 | 1, 17); |
| 1255 | |
| 1256 | // Invalid values for a signed integer field. |
| 1257 | ExpectFailure("optional_int32: \"hello\"\n", "Expected integer.", 1, 17); |
| 1258 | ExpectFailure("optional_int32: true\n", "Expected integer.", 1, 17); |
| 1259 | ExpectFailure("optional_int32: 4.5\n", "Expected integer.", 1, 17); |
| 1260 | ExpectFailure("optional_int32: !\n", "Expected integer.", 1, 17); |
| 1261 | ExpectFailure("optional_int32 {\n \n}\n", "Expected \":\", found \"{\".", |
| 1262 | 1, 16); |
| 1263 | ExpectFailure("optional_int32: 0x80000000\n", |
| 1264 | "Integer out of range.", 1, 17); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 1265 | ExpectFailure("optional_int64: 0x8000000000000000\n", |
| 1266 | "Integer out of range.", 1, 17); |
xiaofeng@google.com | b55a20f | 2012-09-22 02:40:50 +0000 | [diff] [blame] | 1267 | ExpectFailure("optional_int32: -0x80000001\n", |
| 1268 | "Integer out of range.", 1, 18); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 1269 | ExpectFailure("optional_int64: -0x8000000000000001\n", |
| 1270 | "Integer out of range.", 1, 18); |
| 1271 | |
| 1272 | // Invalid values for an unsigned integer field. |
| 1273 | ExpectFailure("optional_uint64: \"hello\"\n", "Expected integer.", 1, 18); |
| 1274 | ExpectFailure("optional_uint64: true\n", "Expected integer.", 1, 18); |
| 1275 | ExpectFailure("optional_uint64: 4.5\n", "Expected integer.", 1, 18); |
| 1276 | ExpectFailure("optional_uint64: -5\n", "Expected integer.", 1, 18); |
| 1277 | ExpectFailure("optional_uint64: !\n", "Expected integer.", 1, 18); |
| 1278 | ExpectFailure("optional_uint64 {\n \n}\n", "Expected \":\", found \"{\".", |
| 1279 | 1, 17); |
| 1280 | ExpectFailure("optional_uint32: 0x100000000\n", |
| 1281 | "Integer out of range.", 1, 18); |
| 1282 | ExpectFailure("optional_uint64: 0x10000000000000000\n", |
| 1283 | "Integer out of range.", 1, 18); |
| 1284 | |
| 1285 | // Invalid values for a boolean field. |
| 1286 | ExpectFailure("optional_bool: \"hello\"\n", "Expected identifier.", 1, 16); |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 1287 | ExpectFailure("optional_bool: 5\n", "Integer out of range.", 1, 16); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 1288 | ExpectFailure("optional_bool: -7.5\n", "Expected identifier.", 1, 16); |
| 1289 | ExpectFailure("optional_bool: !\n", "Expected identifier.", 1, 16); |
| 1290 | |
| 1291 | ExpectFailure( |
| 1292 | "optional_bool: meh\n", |
| 1293 | "Invalid value for boolean field \"optional_bool\". Value: \"meh\".", |
| 1294 | 2, 1); |
| 1295 | |
| 1296 | ExpectFailure("optional_bool {\n \n}\n", "Expected \":\", found \"{\".", |
| 1297 | 1, 15); |
| 1298 | |
| 1299 | // Invalid values for a string field. |
| 1300 | ExpectFailure("optional_string: true\n", "Expected string.", 1, 18); |
| 1301 | ExpectFailure("optional_string: 5\n", "Expected string.", 1, 18); |
| 1302 | ExpectFailure("optional_string: -7.5\n", "Expected string.", 1, 18); |
| 1303 | ExpectFailure("optional_string: !\n", "Expected string.", 1, 18); |
| 1304 | ExpectFailure("optional_string {\n \n}\n", "Expected \":\", found \"{\".", |
| 1305 | 1, 17); |
| 1306 | |
| 1307 | // Invalid values for an enumeration field. |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 1308 | ExpectFailure("optional_nested_enum: \"hello\"\n", |
| 1309 | "Expected integer or identifier.", 1, 23); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 1310 | |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 1311 | // Valid token, but enum value is not defined. |
| 1312 | ExpectFailure("optional_nested_enum: 5\n", |
| 1313 | "Unknown enumeration value of \"5\" for field " |
| 1314 | "\"optional_nested_enum\".", 2, 1); |
| 1315 | // We consume the negative sign, so the error position starts one character |
| 1316 | // later. |
| 1317 | ExpectFailure("optional_nested_enum: -7.5\n", "Expected integer.", 1, 24); |
| 1318 | ExpectFailure("optional_nested_enum: !\n", |
| 1319 | "Expected integer or identifier.", 1, 23); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 1320 | |
| 1321 | ExpectFailure( |
| 1322 | "optional_nested_enum: grah\n", |
| 1323 | "Unknown enumeration value of \"grah\" for field " |
| 1324 | "\"optional_nested_enum\".", 2, 1); |
| 1325 | |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 1326 | ExpectFailure( |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 1327 | "optional_nested_enum {\n \n}\n", |
| 1328 | "Expected \":\", found \"{\".", 1, 22); |
| 1329 | } |
| 1330 | |
Veres Lajos | c768072 | 2014-11-08 22:59:34 +0000 | [diff] [blame] | 1331 | TEST_F(TextFormatParserTest, MessageDelimiters) { |
| 1332 | // Non-matching delimiters. |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 1333 | ExpectFailure("OptionalGroup <\n \n}\n", "Expected \">\", found \"}\".", |
| 1334 | 3, 1); |
| 1335 | |
Veres Lajos | c768072 | 2014-11-08 22:59:34 +0000 | [diff] [blame] | 1336 | // Invalid delimiters. |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 1337 | ExpectFailure("OptionalGroup [\n \n]\n", "Expected \"{\", found \"[\".", |
| 1338 | 1, 15); |
| 1339 | |
| 1340 | // Unending message. |
| 1341 | ExpectFailure("optional_nested_message {\n \nbb: 118\n", |
| 1342 | "Expected identifier.", |
| 1343 | 4, 1); |
| 1344 | } |
| 1345 | |
| 1346 | TEST_F(TextFormatParserTest, UnknownExtension) { |
Veres Lajos | c768072 | 2014-11-08 22:59:34 +0000 | [diff] [blame] | 1347 | // Non-matching delimiters. |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 1348 | ExpectFailure("[blahblah]: 123", |
| 1349 | "Extension \"blahblah\" is not defined or is not an " |
| 1350 | "extension of \"protobuf_unittest.TestAllTypes\".", |
| 1351 | 1, 11); |
| 1352 | } |
| 1353 | |
| 1354 | TEST_F(TextFormatParserTest, MissingRequired) { |
| 1355 | unittest::TestRequired message; |
| 1356 | ExpectFailure("a: 1", |
| 1357 | "Message missing required fields: b, c", |
| 1358 | 0, 1, &message); |
| 1359 | } |
| 1360 | |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 1361 | TEST_F(TextFormatParserTest, ParseDuplicateRequired) { |
| 1362 | unittest::TestRequired message; |
| 1363 | ExpectFailure("a: 1 b: 2 c: 3 a: 1", |
| 1364 | "Non-repeated field \"a\" is specified multiple times.", |
| 1365 | 1, 17, &message); |
| 1366 | } |
| 1367 | |
| 1368 | TEST_F(TextFormatParserTest, ParseDuplicateOptional) { |
| 1369 | unittest::ForeignMessage message; |
| 1370 | ExpectFailure("c: 1 c: 2", |
| 1371 | "Non-repeated field \"c\" is specified multiple times.", |
| 1372 | 1, 7, &message); |
| 1373 | } |
| 1374 | |
| 1375 | TEST_F(TextFormatParserTest, MergeDuplicateRequired) { |
| 1376 | unittest::TestRequired message; |
| 1377 | TextFormat::Parser parser; |
| 1378 | EXPECT_TRUE(parser.MergeFromString("a: 1 b: 2 c: 3 a: 4", &message)); |
| 1379 | EXPECT_EQ(4, message.a()); |
| 1380 | } |
| 1381 | |
| 1382 | TEST_F(TextFormatParserTest, MergeDuplicateOptional) { |
| 1383 | unittest::ForeignMessage message; |
| 1384 | TextFormat::Parser parser; |
| 1385 | EXPECT_TRUE(parser.MergeFromString("c: 1 c: 2", &message)); |
| 1386 | EXPECT_EQ(2, message.c()); |
| 1387 | } |
| 1388 | |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame] | 1389 | TEST_F(TextFormatParserTest, ExplicitDelimiters) { |
| 1390 | unittest::TestRequired message; |
| 1391 | EXPECT_TRUE(TextFormat::ParseFromString("a:1,b:2;c:3", &message)); |
| 1392 | EXPECT_EQ(1, message.a()); |
| 1393 | EXPECT_EQ(2, message.b()); |
| 1394 | EXPECT_EQ(3, message.c()); |
| 1395 | } |
| 1396 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 1397 | TEST_F(TextFormatParserTest, PrintErrorsToStderr) { |
| 1398 | vector<string> errors; |
| 1399 | |
| 1400 | { |
| 1401 | ScopedMemoryLog log; |
| 1402 | unittest::TestAllTypes proto; |
| 1403 | EXPECT_FALSE(TextFormat::ParseFromString("no_such_field: 1", &proto)); |
| 1404 | errors = log.GetMessages(ERROR); |
| 1405 | } |
| 1406 | |
| 1407 | ASSERT_EQ(1, errors.size()); |
| 1408 | EXPECT_EQ("Error parsing text-format protobuf_unittest.TestAllTypes: " |
| 1409 | "1:14: Message type \"protobuf_unittest.TestAllTypes\" has no field " |
| 1410 | "named \"no_such_field\".", |
| 1411 | errors[0]); |
| 1412 | } |
| 1413 | |
kenton@google.com | 26bd9ee | 2008-11-21 00:06:27 +0000 | [diff] [blame] | 1414 | TEST_F(TextFormatParserTest, FailsOnTokenizationError) { |
| 1415 | vector<string> errors; |
| 1416 | |
| 1417 | { |
| 1418 | ScopedMemoryLog log; |
| 1419 | unittest::TestAllTypes proto; |
| 1420 | EXPECT_FALSE(TextFormat::ParseFromString("\020", &proto)); |
| 1421 | errors = log.GetMessages(ERROR); |
| 1422 | } |
| 1423 | |
| 1424 | ASSERT_EQ(1, errors.size()); |
| 1425 | EXPECT_EQ("Error parsing text-format protobuf_unittest.TestAllTypes: " |
| 1426 | "1:1: Invalid control characters encountered in text.", |
| 1427 | errors[0]); |
| 1428 | } |
| 1429 | |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 1430 | TEST_F(TextFormatParserTest, ParseDeprecatedField) { |
| 1431 | unittest::TestDeprecatedFields message; |
| 1432 | ExpectMessage("deprecated_int32: 42", |
| 1433 | "WARNING:text format contains deprecated field " |
| 1434 | "\"deprecated_int32\"", 1, 21, &message, true); |
| 1435 | } |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 1436 | |
| 1437 | class TextFormatMessageSetTest : public testing::Test { |
| 1438 | protected: |
| 1439 | static const char proto_debug_string_[]; |
| 1440 | }; |
| 1441 | const char TextFormatMessageSetTest::proto_debug_string_[] = |
| 1442 | "message_set {\n" |
| 1443 | " [protobuf_unittest.TestMessageSetExtension1] {\n" |
| 1444 | " i: 23\n" |
| 1445 | " }\n" |
| 1446 | " [protobuf_unittest.TestMessageSetExtension2] {\n" |
| 1447 | " str: \"foo\"\n" |
| 1448 | " }\n" |
| 1449 | "}\n"; |
| 1450 | |
| 1451 | |
| 1452 | TEST_F(TextFormatMessageSetTest, Serialize) { |
| 1453 | protobuf_unittest::TestMessageSetContainer proto; |
| 1454 | protobuf_unittest::TestMessageSetExtension1* item_a = |
| 1455 | proto.mutable_message_set()->MutableExtension( |
| 1456 | protobuf_unittest::TestMessageSetExtension1::message_set_extension); |
| 1457 | item_a->set_i(23); |
| 1458 | protobuf_unittest::TestMessageSetExtension2* item_b = |
| 1459 | proto.mutable_message_set()->MutableExtension( |
| 1460 | protobuf_unittest::TestMessageSetExtension2::message_set_extension); |
| 1461 | item_b->set_str("foo"); |
| 1462 | EXPECT_EQ(proto_debug_string_, proto.DebugString()); |
| 1463 | } |
| 1464 | |
| 1465 | TEST_F(TextFormatMessageSetTest, Deserialize) { |
| 1466 | protobuf_unittest::TestMessageSetContainer proto; |
| 1467 | ASSERT_TRUE(TextFormat::ParseFromString(proto_debug_string_, &proto)); |
| 1468 | EXPECT_EQ(23, proto.message_set().GetExtension( |
| 1469 | protobuf_unittest::TestMessageSetExtension1::message_set_extension).i()); |
| 1470 | EXPECT_EQ("foo", proto.message_set().GetExtension( |
| 1471 | protobuf_unittest::TestMessageSetExtension2::message_set_extension).str()); |
| 1472 | |
| 1473 | // Ensure that these are the only entries present. |
| 1474 | vector<const FieldDescriptor*> descriptors; |
temporal | 779f61c | 2008-08-13 03:15:00 +0000 | [diff] [blame] | 1475 | proto.message_set().GetReflection()->ListFields( |
| 1476 | proto.message_set(), &descriptors); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 1477 | EXPECT_EQ(2, descriptors.size()); |
| 1478 | } |
| 1479 | |
xiaofeng@google.com | b55a20f | 2012-09-22 02:40:50 +0000 | [diff] [blame] | 1480 | |
kenton@google.com | a2a32c2 | 2008-11-14 17:29:32 +0000 | [diff] [blame] | 1481 | } // namespace text_format_unittest |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 1482 | } // namespace protobuf |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 1483 | } // namespace google |