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