1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
3 // https://developers.google.com/protocol-buffers/
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
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
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.
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.
31 // Author: jschorr@google.com (Joseph Schorr)
32 // Based on original Protocol Buffers design by
33 // Sanjay Ghemawat, Jeff Dean, and others.
35 #include <google/protobuf/text_format.h>
42 #include <google/protobuf/stubs/logging.h>
43 #include <google/protobuf/stubs/common.h>
44 #include <google/protobuf/stubs/logging.h>
45 #include <google/protobuf/testing/file.h>
46 #include <google/protobuf/testing/file.h>
47 #include <google/protobuf/map_unittest.pb.h>
48 #include <google/protobuf/test_util.h>
49 #include <google/protobuf/test_util2.h>
50 #include <google/protobuf/unittest.pb.h>
51 #include <google/protobuf/unittest_mset.pb.h>
52 #include <google/protobuf/unittest_mset_wire_format.pb.h>
53 #include <google/protobuf/unittest_proto3.pb.h>
54 #include <google/protobuf/io/tokenizer.h>
55 #include <google/protobuf/io/zero_copy_stream_impl.h>
56 #include <google/protobuf/stubs/strutil.h>
59 #include <google/protobuf/stubs/substitute.h>
60 #include <google/protobuf/testing/googletest.h>
61 #include <gtest/gtest.h>
62 #include <google/protobuf/stubs/mathlimits.h>
65 #include <google/protobuf/port_def.inc>
70 // Can't use an anonymous namespace here due to brokenness of Tru64 compiler.
71 namespace text_format_unittest {
73 // A basic string with different escapable characters for testing.
74 const std::string kEscapeTestString =
75 "\"A string with ' characters \n and \r newlines and \t tabs and \001 "
76 "slashes \\ and multiple spaces";
78 // A representation of the above string with all the characters escaped.
79 const std::string kEscapeTestStringEscaped =
80 "\"\\\"A string with \\' characters \\n and \\r newlines "
81 "and \\t tabs and \\001 slashes \\\\ and multiple spaces\"";
83 class TextFormatTest : public testing::Test {
85 static void SetUpTestCase() {
86 GOOGLE_CHECK_OK(File::GetContents(
87 TestUtil::GetTestDataPath(
88 "net/proto2/internal/"
89 "testdata/text_format_unittest_data_oneof_implemented.txt"),
90 &static_proto_debug_string_, true));
91 CleanStringLineEndings(&static_proto_debug_string_, false);
94 TextFormatTest() : proto_debug_string_(static_proto_debug_string_) {}
97 // Debug string read from text_format_unittest_data.txt.
98 const std::string proto_debug_string_;
99 unittest::TestAllTypes proto_;
102 static std::string static_proto_debug_string_;
104 std::string TextFormatTest::static_proto_debug_string_;
106 class TextFormatExtensionsTest : public testing::Test {
108 static void SetUpTestCase() {
109 GOOGLE_CHECK_OK(File::GetContents(
110 TestUtil::GetTestDataPath("net/proto2/internal/testdata/"
111 "text_format_unittest_extensions_data.txt"),
112 &static_proto_debug_string_, true));
113 CleanStringLineEndings(&static_proto_debug_string_, false);
116 TextFormatExtensionsTest()
117 : proto_debug_string_(static_proto_debug_string_) {}
120 // Debug string read from text_format_unittest_data.txt.
121 const std::string proto_debug_string_;
122 unittest::TestAllExtensions proto_;
125 static std::string static_proto_debug_string_;
127 std::string TextFormatExtensionsTest::static_proto_debug_string_;
129 TEST_F(TextFormatTest, Basic) {
130 TestUtil::SetAllFields(&proto_);
131 EXPECT_EQ(proto_debug_string_, proto_.DebugString());
134 TEST_F(TextFormatExtensionsTest, Extensions) {
135 TestUtil::SetAllExtensions(&proto_);
136 EXPECT_EQ(proto_debug_string_, proto_.DebugString());
139 TEST_F(TextFormatTest, ShortDebugString) {
140 proto_.set_optional_int32(1);
141 proto_.set_optional_string("hello");
142 proto_.mutable_optional_nested_message()->set_bb(2);
143 proto_.mutable_optional_foreign_message();
146 "optional_int32: 1 optional_string: \"hello\" "
147 "optional_nested_message { bb: 2 } "
148 "optional_foreign_message { }",
149 proto_.ShortDebugString());
152 TEST_F(TextFormatTest, ShortPrimitiveRepeateds) {
153 proto_.set_optional_int32(123);
154 proto_.add_repeated_int32(456);
155 proto_.add_repeated_int32(789);
156 proto_.add_repeated_string("foo");
157 proto_.add_repeated_string("bar");
158 proto_.add_repeated_nested_message()->set_bb(2);
159 proto_.add_repeated_nested_message()->set_bb(3);
160 proto_.add_repeated_nested_enum(unittest::TestAllTypes::FOO);
161 proto_.add_repeated_nested_enum(unittest::TestAllTypes::BAR);
163 TextFormat::Printer printer;
164 printer.SetUseShortRepeatedPrimitives(true);
166 EXPECT_TRUE(printer.PrintToString(proto_, &text));
169 "optional_int32: 123\n"
170 "repeated_int32: [456, 789]\n"
171 "repeated_string: \"foo\"\n"
172 "repeated_string: \"bar\"\n"
173 "repeated_nested_message {\n bb: 2\n}\n"
174 "repeated_nested_message {\n bb: 3\n}\n"
175 "repeated_nested_enum: [FOO, BAR]\n",
178 // Verify that any existing data in the string is cleared when PrintToString()
180 text = "just some data here...\n\nblah blah";
181 EXPECT_TRUE(printer.PrintToString(proto_, &text));
184 "optional_int32: 123\n"
185 "repeated_int32: [456, 789]\n"
186 "repeated_string: \"foo\"\n"
187 "repeated_string: \"bar\"\n"
188 "repeated_nested_message {\n bb: 2\n}\n"
189 "repeated_nested_message {\n bb: 3\n}\n"
190 "repeated_nested_enum: [FOO, BAR]\n",
193 // Try in single-line mode.
194 printer.SetSingleLineMode(true);
195 EXPECT_TRUE(printer.PrintToString(proto_, &text));
198 "optional_int32: 123 "
199 "repeated_int32: [456, 789] "
200 "repeated_string: \"foo\" "
201 "repeated_string: \"bar\" "
202 "repeated_nested_message { bb: 2 } "
203 "repeated_nested_message { bb: 3 } "
204 "repeated_nested_enum: [FOO, BAR] ",
209 TEST_F(TextFormatTest, StringEscape) {
210 // Set the string value to test.
211 proto_.set_optional_string(kEscapeTestString);
213 // Get the DebugString from the proto.
214 std::string debug_string = proto_.DebugString();
215 std::string utf8_debug_string = proto_.Utf8DebugString();
217 // Hardcode a correct value to test against.
218 std::string correct_string =
219 "optional_string: " + kEscapeTestStringEscaped + "\n";
222 EXPECT_EQ(correct_string, debug_string);
223 // UTF-8 string is the same as non-UTF-8 because
224 // the protocol buffer contains no UTF-8 text.
225 EXPECT_EQ(correct_string, utf8_debug_string);
227 std::string expected_short_debug_string =
228 "optional_string: " + kEscapeTestStringEscaped;
229 EXPECT_EQ(expected_short_debug_string, proto_.ShortDebugString());
232 TEST_F(TextFormatTest, Utf8DebugString) {
233 // Set the string value to test.
234 proto_.set_optional_string("\350\260\267\346\255\214");
235 proto_.set_optional_bytes("\350\260\267\346\255\214");
237 // Get the DebugString from the proto.
238 std::string debug_string = proto_.DebugString();
239 std::string utf8_debug_string = proto_.Utf8DebugString();
241 // Hardcode a correct value to test against.
242 std::string correct_utf8_string =
244 "\"\350\260\267\346\255\214\""
247 "\"\\350\\260\\267\\346\\255\\214\""
249 std::string correct_string =
251 "\"\\350\\260\\267\\346\\255\\214\""
254 "\"\\350\\260\\267\\346\\255\\214\""
258 EXPECT_EQ(correct_utf8_string, utf8_debug_string);
259 EXPECT_EQ(correct_string, debug_string);
262 TEST_F(TextFormatTest, PrintUnknownFields) {
263 // Test printing of unknown fields in a message.
265 unittest::TestEmptyMessage message;
266 UnknownFieldSet* unknown_fields = message.mutable_unknown_fields();
268 unknown_fields->AddVarint(5, 1);
269 unknown_fields->AddFixed32(5, 2);
270 unknown_fields->AddFixed64(5, 3);
271 unknown_fields->AddLengthDelimited(5, "4");
272 unknown_fields->AddGroup(5)->AddVarint(10, 5);
274 unknown_fields->AddVarint(8, 1);
275 unknown_fields->AddVarint(8, 2);
276 unknown_fields->AddVarint(8, 3);
281 "5: 0x0000000000000003\n"
289 message.DebugString());
292 TEST_F(TextFormatTest, PrintUnknownFieldsHidden) {
293 // Test printing of unknown fields in a message when suppressed.
295 unittest::OneString message;
296 message.set_data("data");
297 UnknownFieldSet* unknown_fields = message.mutable_unknown_fields();
299 unknown_fields->AddVarint(5, 1);
300 unknown_fields->AddFixed32(5, 2);
301 unknown_fields->AddFixed64(5, 3);
302 unknown_fields->AddLengthDelimited(5, "4");
303 unknown_fields->AddGroup(5)->AddVarint(10, 5);
305 unknown_fields->AddVarint(8, 1);
306 unknown_fields->AddVarint(8, 2);
307 unknown_fields->AddVarint(8, 3);
309 TextFormat::Printer printer;
310 printer.SetHideUnknownFields(true);
312 printer.PrintToString(message, &output);
314 EXPECT_EQ("data: \"data\"\n", output);
317 TEST_F(TextFormatTest, PrintUnknownMessage) {
318 // Test heuristic printing of messages in an UnknownFieldSet.
320 protobuf_unittest::TestAllTypes message;
322 // Cases which should not be interpreted as sub-messages.
324 // 'a' is a valid FIXED64 tag, so for the string to be parseable as a message
325 // it should be followed by 8 bytes. Since this string only has two
326 // subsequent bytes, it should be treated as a string.
327 message.add_repeated_string("abc");
329 // 'd' happens to be a valid ENDGROUP tag. So,
330 // UnknownFieldSet::MergeFromCodedStream() will successfully parse "def", but
331 // the ConsumedEntireMessage() check should fail.
332 message.add_repeated_string("def");
334 // A zero-length string should never be interpreted as a message even though
335 // it is technically valid as one.
336 message.add_repeated_string("");
338 // Case which should be interpreted as a sub-message.
340 // An actual nested message with content should always be interpreted as a
342 message.add_repeated_nested_message()->set_bb(123);
345 message.SerializeToString(&data);
348 UnknownFieldSet unknown_fields;
349 EXPECT_TRUE(unknown_fields.ParseFromString(data));
350 EXPECT_TRUE(TextFormat::PrintUnknownFieldsToString(unknown_fields, &text));
361 TEST_F(TextFormatTest, PrintMessageWithIndent) {
362 // Test adding an initial indent to printing.
364 protobuf_unittest::TestAllTypes message;
366 message.add_repeated_string("abc");
367 message.add_repeated_string("def");
368 message.add_repeated_nested_message()->set_bb(123);
371 TextFormat::Printer printer;
372 printer.SetInitialIndentLevel(1);
373 EXPECT_TRUE(printer.PrintToString(message, &text));
375 " repeated_string: \"abc\"\n"
376 " repeated_string: \"def\"\n"
377 " repeated_nested_message {\n"
383 TEST_F(TextFormatTest, PrintMessageSingleLine) {
384 // Test printing a message on a single line.
386 protobuf_unittest::TestAllTypes message;
388 message.add_repeated_string("abc");
389 message.add_repeated_string("def");
390 message.add_repeated_nested_message()->set_bb(123);
393 TextFormat::Printer printer;
394 printer.SetInitialIndentLevel(1);
395 printer.SetSingleLineMode(true);
396 EXPECT_TRUE(printer.PrintToString(message, &text));
398 " repeated_string: \"abc\" repeated_string: \"def\" "
399 "repeated_nested_message { bb: 123 } ",
403 TEST_F(TextFormatTest, PrintBufferTooSmall) {
404 // Test printing a message to a buffer that is too small.
406 protobuf_unittest::TestAllTypes message;
408 message.add_repeated_string("abc");
409 message.add_repeated_string("def");
412 io::ArrayOutputStream output_stream(buffer, 1);
413 EXPECT_FALSE(TextFormat::Print(message, &output_stream));
414 EXPECT_EQ(buffer[0], 'r');
415 EXPECT_EQ(output_stream.ByteCount(), 1);
418 // A printer that appends 'u' to all unsigned int32.
419 class CustomUInt32FieldValuePrinter : public TextFormat::FieldValuePrinter {
421 virtual std::string PrintUInt32(uint32 val) const {
422 return StrCat(FieldValuePrinter::PrintUInt32(val), "u");
426 TEST_F(TextFormatTest, DefaultCustomFieldPrinter) {
427 protobuf_unittest::TestAllTypes message;
429 message.set_optional_uint32(42);
430 message.add_repeated_uint32(1);
431 message.add_repeated_uint32(2);
432 message.add_repeated_uint32(3);
434 TextFormat::Printer printer;
435 printer.SetDefaultFieldValuePrinter(new CustomUInt32FieldValuePrinter());
436 // Let's see if that works well together with the repeated primitives:
437 printer.SetUseShortRepeatedPrimitives(true);
439 printer.PrintToString(message, &text);
440 EXPECT_EQ("optional_uint32: 42u\nrepeated_uint32: [1u, 2u, 3u]\n", text);
443 class CustomInt32FieldValuePrinter : public TextFormat::FieldValuePrinter {
445 virtual std::string PrintInt32(int32 val) const {
446 return StrCat("value-is(", FieldValuePrinter::PrintInt32(val), ")");
450 TEST_F(TextFormatTest, FieldSpecificCustomPrinter) {
451 protobuf_unittest::TestAllTypes message;
453 message.set_optional_int32(42); // This will be handled by our Printer.
454 message.add_repeated_int32(42); // This will be printed as number.
456 TextFormat::Printer printer;
457 EXPECT_TRUE(printer.RegisterFieldValuePrinter(
458 message.GetDescriptor()->FindFieldByName("optional_int32"),
459 new CustomInt32FieldValuePrinter()));
461 printer.PrintToString(message, &text);
462 EXPECT_EQ("optional_int32: value-is(42)\nrepeated_int32: 42\n", text);
465 TEST_F(TextFormatTest, FieldSpecificCustomPrinterRegisterSameFieldTwice) {
466 protobuf_unittest::TestAllTypes message;
467 TextFormat::Printer printer;
468 const FieldDescriptor* const field =
469 message.GetDescriptor()->FindFieldByName("optional_int32");
470 ASSERT_TRUE(printer.RegisterFieldValuePrinter(
471 field, new CustomInt32FieldValuePrinter()));
472 const TextFormat::FieldValuePrinter* const rejected =
473 new CustomInt32FieldValuePrinter();
474 ASSERT_FALSE(printer.RegisterFieldValuePrinter(field, rejected));
478 TEST_F(TextFormatTest, ErrorCasesRegisteringFieldValuePrinterShouldFail) {
479 protobuf_unittest::TestAllTypes message;
480 TextFormat::Printer printer;
482 EXPECT_FALSE(printer.RegisterFieldValuePrinter(
483 message.GetDescriptor()->FindFieldByName("optional_int32"),
484 static_cast<const TextFormat::FieldValuePrinter*>(nullptr)));
485 EXPECT_FALSE(printer.RegisterFieldValuePrinter(
486 message.GetDescriptor()->FindFieldByName("optional_int32"),
487 static_cast<const TextFormat::FastFieldValuePrinter*>(nullptr)));
488 // Because registration fails, the ownership of this printer is never taken.
489 TextFormat::FieldValuePrinter my_field_printer;
491 EXPECT_FALSE(printer.RegisterFieldValuePrinter(nullptr, &my_field_printer));
494 class CustomMessageFieldValuePrinter : public TextFormat::FieldValuePrinter {
496 virtual std::string PrintInt32(int32 v) const {
497 return StrCat(FieldValuePrinter::PrintInt32(v), " # x",
501 virtual std::string PrintMessageStart(const Message& message, int field_index,
503 bool single_line_mode) const {
504 if (single_line_mode) {
507 return StrCat(" { # ", message.GetDescriptor()->name(), ": ",
512 TEST_F(TextFormatTest, CustomPrinterForComments) {
513 protobuf_unittest::TestAllTypes message;
514 message.mutable_optional_nested_message();
515 message.mutable_optional_import_message()->set_d(42);
516 message.add_repeated_nested_message();
517 message.add_repeated_nested_message();
518 message.add_repeated_import_message()->set_d(43);
519 message.add_repeated_import_message()->set_d(44);
520 TextFormat::Printer printer;
521 CustomMessageFieldValuePrinter my_field_printer;
522 printer.SetDefaultFieldValuePrinter(new CustomMessageFieldValuePrinter());
524 printer.PrintToString(message, &text);
526 "optional_nested_message { # NestedMessage: -1\n"
528 "optional_import_message { # ImportMessage: -1\n"
531 "repeated_nested_message { # NestedMessage: 0\n"
533 "repeated_nested_message { # NestedMessage: 1\n"
535 "repeated_import_message { # ImportMessage: 0\n"
538 "repeated_import_message { # ImportMessage: 1\n"
544 class CustomMultilineCommentPrinter : public TextFormat::FieldValuePrinter {
546 virtual std::string PrintMessageStart(const Message& message, int field_index,
548 bool single_line_comment) const {
549 return StrCat(" { # 1\n", " # 2\n");
553 TEST_F(TextFormatTest, CustomPrinterForMultilineComments) {
554 protobuf_unittest::TestAllTypes message;
555 message.mutable_optional_nested_message();
556 message.mutable_optional_import_message()->set_d(42);
557 TextFormat::Printer printer;
558 CustomMessageFieldValuePrinter my_field_printer;
559 printer.SetDefaultFieldValuePrinter(new CustomMultilineCommentPrinter());
561 printer.PrintToString(message, &text);
563 "optional_nested_message { # 1\n"
566 "optional_import_message { # 1\n"
573 // Achieve effects similar to SetUseShortRepeatedPrimitives for messages, using
574 // RegisterFieldValuePrinter. Use this to test the version of PrintFieldName
575 // that accepts repeated field index and count.
576 class CompactRepeatedFieldPrinter : public TextFormat::FastFieldValuePrinter {
578 void PrintFieldName(const Message& message, int field_index, int field_count,
579 const Reflection* reflection,
580 const FieldDescriptor* field,
581 TextFormat::BaseTextGenerator* generator) const override {
582 if (field_index == 0 || field_index == -1) {
583 generator->PrintString(field->name());
586 // To prevent compiler complaining about Woverloaded-virtual
587 void PrintFieldName(const Message& message, const Reflection* reflection,
588 const FieldDescriptor* field,
589 TextFormat::BaseTextGenerator* generator) const override {
591 void PrintMessageStart(
592 const Message& message, int field_index, int field_count,
593 bool single_line_mode,
594 TextFormat::BaseTextGenerator* generator) const override {
595 if (field_index == 0 || field_index == -1) {
596 if (single_line_mode) {
597 generator->PrintLiteral(" { ");
599 generator->PrintLiteral(" {\n");
603 void PrintMessageEnd(
604 const Message& message, int field_index, int field_count,
605 bool single_line_mode,
606 TextFormat::BaseTextGenerator* generator) const override {
607 if (field_index == field_count - 1 || field_index == -1) {
608 if (single_line_mode) {
609 generator->PrintLiteral("} ");
611 generator->PrintLiteral("}\n");
617 TEST_F(TextFormatTest, CompactRepeatedFieldPrinter) {
618 TextFormat::Printer printer;
619 ASSERT_TRUE(printer.RegisterFieldValuePrinter(
620 unittest::TestAllTypes::default_instance()
623 unittest::TestAllTypes::kRepeatedNestedMessageFieldNumber),
624 new CompactRepeatedFieldPrinter));
626 protobuf_unittest::TestAllTypes message;
627 message.add_repeated_nested_message()->set_bb(1);
628 message.add_repeated_nested_message()->set_bb(2);
629 message.add_repeated_nested_message()->set_bb(3);
632 ASSERT_TRUE(printer.PrintToString(message, &text));
634 "repeated_nested_message {\n"
642 // Print strings into multiple line, with indention. Use this to test
643 // BaseTextGenerator::Indent and BaseTextGenerator::Outdent.
644 class MultilineStringPrinter : public TextFormat::FastFieldValuePrinter {
646 void PrintString(const std::string& val,
647 TextFormat::BaseTextGenerator* generator) const override {
650 int newline_pos = val.find('\n');
651 while (newline_pos != std::string::npos) {
652 generator->PrintLiteral("\n");
653 TextFormat::FastFieldValuePrinter::PrintString(
654 val.substr(last_pos, newline_pos + 1 - last_pos), generator);
655 last_pos = newline_pos + 1;
656 newline_pos = val.find('\n', last_pos);
658 if (last_pos < val.size()) {
659 generator->PrintLiteral("\n");
660 TextFormat::FastFieldValuePrinter::PrintString(val.substr(last_pos),
663 generator->Outdent();
667 TEST_F(TextFormatTest, MultilineStringPrinter) {
668 TextFormat::Printer printer;
669 ASSERT_TRUE(printer.RegisterFieldValuePrinter(
670 unittest::TestAllTypes::default_instance()
673 unittest::TestAllTypes::kOptionalStringFieldNumber),
674 new MultilineStringPrinter));
676 protobuf_unittest::TestAllTypes message;
677 message.set_optional_string("first line\nsecond line\nthird line");
680 ASSERT_TRUE(printer.PrintToString(message, &text));
682 "optional_string: \n"
683 " \"first line\\n\"\n"
684 " \"second line\\n\"\n"
689 class CustomNestedMessagePrinter : public TextFormat::MessagePrinter {
691 CustomNestedMessagePrinter() {}
692 ~CustomNestedMessagePrinter() override {}
693 void Print(const Message& message, bool single_line_mode,
694 TextFormat::BaseTextGenerator* generator) const override {
695 generator->PrintLiteral("custom");
699 TEST_F(TextFormatTest, CustomMessagePrinter) {
700 TextFormat::Printer printer;
701 printer.RegisterMessagePrinter(
702 unittest::TestAllTypes::NestedMessage::default_instance().descriptor(),
703 new CustomNestedMessagePrinter);
705 unittest::TestAllTypes message;
707 EXPECT_TRUE(printer.PrintToString(message, &text));
710 message.mutable_optional_nested_message()->set_bb(1);
711 EXPECT_TRUE(printer.PrintToString(message, &text));
712 EXPECT_EQ("optional_nested_message {\n custom}\n", text);
715 TEST_F(TextFormatTest, ParseBasic) {
716 io::ArrayInputStream input_stream(proto_debug_string_.data(),
717 proto_debug_string_.size());
718 TextFormat::Parse(&input_stream, &proto_);
719 TestUtil::ExpectAllFieldsSet(proto_);
722 TEST_F(TextFormatExtensionsTest, ParseExtensions) {
723 io::ArrayInputStream input_stream(proto_debug_string_.data(),
724 proto_debug_string_.size());
725 TextFormat::Parse(&input_stream, &proto_);
726 TestUtil::ExpectAllExtensionsSet(proto_);
729 TEST_F(TextFormatTest, ParseEnumFieldFromNumber) {
730 // Create a parse string with a numerical value for an enum field.
731 std::string parse_string = strings::Substitute("optional_nested_enum: $0",
732 unittest::TestAllTypes::BAZ);
733 EXPECT_TRUE(TextFormat::ParseFromString(parse_string, &proto_));
734 EXPECT_TRUE(proto_.has_optional_nested_enum());
735 EXPECT_EQ(unittest::TestAllTypes::BAZ, proto_.optional_nested_enum());
738 TEST_F(TextFormatTest, ParseEnumFieldFromNegativeNumber) {
739 ASSERT_LT(unittest::SPARSE_E, 0);
740 std::string parse_string =
741 strings::Substitute("sparse_enum: $0", unittest::SPARSE_E);
742 unittest::SparseEnumMessage proto;
743 EXPECT_TRUE(TextFormat::ParseFromString(parse_string, &proto));
744 EXPECT_TRUE(proto.has_sparse_enum());
745 EXPECT_EQ(unittest::SPARSE_E, proto.sparse_enum());
748 TEST_F(TextFormatTest, PrintUnknownEnumFieldProto3) {
749 proto3_unittest::TestAllTypes proto;
751 proto.add_repeated_nested_enum(
752 static_cast<proto3_unittest::TestAllTypes::NestedEnum>(10));
753 proto.add_repeated_nested_enum(
754 static_cast<proto3_unittest::TestAllTypes::NestedEnum>(-10));
755 proto.add_repeated_nested_enum(
756 static_cast<proto3_unittest::TestAllTypes::NestedEnum>(2147483647));
757 proto.add_repeated_nested_enum(
758 static_cast<proto3_unittest::TestAllTypes::NestedEnum>(-2147483648));
761 "repeated_nested_enum: 10\n"
762 "repeated_nested_enum: -10\n"
763 "repeated_nested_enum: 2147483647\n"
764 "repeated_nested_enum: -2147483648\n",
765 proto.DebugString());
768 TEST_F(TextFormatTest, ParseUnknownEnumFieldProto3) {
769 proto3_unittest::TestAllTypes proto;
770 std::string parse_string =
771 "repeated_nested_enum: [10, -10, 2147483647, -2147483648]";
772 EXPECT_TRUE(TextFormat::ParseFromString(parse_string, &proto));
773 ASSERT_EQ(4, proto.repeated_nested_enum_size());
774 EXPECT_EQ(10, proto.repeated_nested_enum(0));
775 EXPECT_EQ(-10, proto.repeated_nested_enum(1));
776 EXPECT_EQ(2147483647, proto.repeated_nested_enum(2));
777 EXPECT_EQ(-2147483648, proto.repeated_nested_enum(3));
780 TEST_F(TextFormatTest, ParseStringEscape) {
781 // Create a parse string with escpaed characters in it.
782 std::string parse_string =
783 "optional_string: " + kEscapeTestStringEscaped + "\n";
785 io::ArrayInputStream input_stream(parse_string.data(), parse_string.size());
786 TextFormat::Parse(&input_stream, &proto_);
789 EXPECT_EQ(kEscapeTestString, proto_.optional_string());
792 TEST_F(TextFormatTest, ParseConcatenatedString) {
793 // Create a parse string with multiple parts on one line.
794 std::string parse_string = "optional_string: \"foo\" \"bar\"\n";
796 io::ArrayInputStream input_stream1(parse_string.data(), parse_string.size());
797 TextFormat::Parse(&input_stream1, &proto_);
800 EXPECT_EQ("foobar", proto_.optional_string());
802 // Create a parse string with multiple parts on separate lines.
804 "optional_string: \"foo\"\n"
807 io::ArrayInputStream input_stream2(parse_string.data(), parse_string.size());
808 TextFormat::Parse(&input_stream2, &proto_);
811 EXPECT_EQ("foobar", proto_.optional_string());
814 TEST_F(TextFormatTest, ParseFloatWithSuffix) {
815 // Test that we can parse a floating-point value with 'f' appended to the
816 // end. This is needed for backwards-compatibility with proto1.
818 // Have it parse a float with the 'f' suffix.
819 std::string parse_string = "optional_float: 1.0f\n";
821 io::ArrayInputStream input_stream(parse_string.data(), parse_string.size());
823 TextFormat::Parse(&input_stream, &proto_);
826 EXPECT_EQ(1.0, proto_.optional_float());
829 TEST_F(TextFormatTest, ParseShortRepeatedForm) {
830 std::string parse_string =
831 // Mixed short-form and long-form are simply concatenated.
832 "repeated_int32: 1\n"
833 "repeated_int32: [456, 789]\n"
834 "repeated_nested_enum: [ FOO ,BAR, # comment\n"
836 // Note that while the printer won't print repeated strings in short-form,
837 // the parser will accept them.
838 "repeated_string: [ \"foo\", 'bar' ]\n"
840 "repeated_nested_message: [ { bb: 1 }, { bb : 2 }]\n"
842 "RepeatedGroup [{ a: 3 },{ a: 4 }]\n";
844 ASSERT_TRUE(TextFormat::ParseFromString(parse_string, &proto_));
846 ASSERT_EQ(3, proto_.repeated_int32_size());
847 EXPECT_EQ(1, proto_.repeated_int32(0));
848 EXPECT_EQ(456, proto_.repeated_int32(1));
849 EXPECT_EQ(789, proto_.repeated_int32(2));
851 ASSERT_EQ(3, proto_.repeated_nested_enum_size());
852 EXPECT_EQ(unittest::TestAllTypes::FOO, proto_.repeated_nested_enum(0));
853 EXPECT_EQ(unittest::TestAllTypes::BAR, proto_.repeated_nested_enum(1));
854 EXPECT_EQ(unittest::TestAllTypes::BAZ, proto_.repeated_nested_enum(2));
856 ASSERT_EQ(2, proto_.repeated_string_size());
857 EXPECT_EQ("foo", proto_.repeated_string(0));
858 EXPECT_EQ("bar", proto_.repeated_string(1));
860 ASSERT_EQ(2, proto_.repeated_nested_message_size());
861 EXPECT_EQ(1, proto_.repeated_nested_message(0).bb());
862 EXPECT_EQ(2, proto_.repeated_nested_message(1).bb());
864 ASSERT_EQ(2, proto_.repeatedgroup_size());
865 EXPECT_EQ(3, proto_.repeatedgroup(0).a());
866 EXPECT_EQ(4, proto_.repeatedgroup(1).a());
869 TEST_F(TextFormatTest, ParseShortRepeatedWithTrailingComma) {
870 std::string parse_string = "repeated_int32: [456,]\n";
871 ASSERT_FALSE(TextFormat::ParseFromString(parse_string, &proto_));
872 parse_string = "repeated_nested_enum: [ FOO , ]";
873 ASSERT_FALSE(TextFormat::ParseFromString(parse_string, &proto_));
874 parse_string = "repeated_string: [ \"foo\", ]";
875 ASSERT_FALSE(TextFormat::ParseFromString(parse_string, &proto_));
876 parse_string = "repeated_nested_message: [ { bb: 1 }, ]";
877 ASSERT_FALSE(TextFormat::ParseFromString(parse_string, &proto_));
878 parse_string = "RepeatedGroup [{ a: 3 },]\n";
881 TEST_F(TextFormatTest, ParseShortRepeatedEmpty) {
882 std::string parse_string =
883 "repeated_int32: []\n"
884 "repeated_nested_enum: []\n"
885 "repeated_string: []\n"
886 "repeated_nested_message: []\n"
887 "RepeatedGroup []\n";
889 ASSERT_TRUE(TextFormat::ParseFromString(parse_string, &proto_));
891 EXPECT_EQ(0, proto_.repeated_int32_size());
892 EXPECT_EQ(0, proto_.repeated_nested_enum_size());
893 EXPECT_EQ(0, proto_.repeated_string_size());
894 EXPECT_EQ(0, proto_.repeated_nested_message_size());
895 EXPECT_EQ(0, proto_.repeatedgroup_size());
898 TEST_F(TextFormatTest, ParseShortRepeatedConcatenatedWithEmpty) {
899 std::string parse_string =
900 // Starting with empty [] should have no impact.
901 "repeated_int32: []\n"
902 "repeated_nested_enum: []\n"
903 "repeated_string: []\n"
904 "repeated_nested_message: []\n"
906 // Mixed short-form and long-form are simply concatenated.
907 "repeated_int32: 1\n"
908 "repeated_int32: [456, 789]\n"
909 "repeated_nested_enum: [ FOO ,BAR, # comment\n"
911 // Note that while the printer won't print repeated strings in short-form,
912 // the parser will accept them.
913 "repeated_string: [ \"foo\", 'bar' ]\n"
915 "repeated_nested_message: [ { bb: 1 }, { bb : 2 }]\n"
917 "RepeatedGroup [{ a: 3 },{ a: 4 }]\n"
918 // Adding empty [] should have no impact.
919 "repeated_int32: []\n"
920 "repeated_nested_enum: []\n"
921 "repeated_string: []\n"
922 "repeated_nested_message: []\n"
923 "RepeatedGroup []\n";
925 ASSERT_TRUE(TextFormat::ParseFromString(parse_string, &proto_));
927 ASSERT_EQ(3, proto_.repeated_int32_size());
928 EXPECT_EQ(1, proto_.repeated_int32(0));
929 EXPECT_EQ(456, proto_.repeated_int32(1));
930 EXPECT_EQ(789, proto_.repeated_int32(2));
932 ASSERT_EQ(3, proto_.repeated_nested_enum_size());
933 EXPECT_EQ(unittest::TestAllTypes::FOO, proto_.repeated_nested_enum(0));
934 EXPECT_EQ(unittest::TestAllTypes::BAR, proto_.repeated_nested_enum(1));
935 EXPECT_EQ(unittest::TestAllTypes::BAZ, proto_.repeated_nested_enum(2));
937 ASSERT_EQ(2, proto_.repeated_string_size());
938 EXPECT_EQ("foo", proto_.repeated_string(0));
939 EXPECT_EQ("bar", proto_.repeated_string(1));
941 ASSERT_EQ(2, proto_.repeated_nested_message_size());
942 EXPECT_EQ(1, proto_.repeated_nested_message(0).bb());
943 EXPECT_EQ(2, proto_.repeated_nested_message(1).bb());
945 ASSERT_EQ(2, proto_.repeatedgroup_size());
946 EXPECT_EQ(3, proto_.repeatedgroup(0).a());
947 EXPECT_EQ(4, proto_.repeatedgroup(1).a());
951 TEST_F(TextFormatTest, Comments) {
952 // Test that comments are ignored.
954 std::string parse_string =
955 "optional_int32: 1 # a comment\n"
956 "optional_int64: 2 # another comment";
958 io::ArrayInputStream input_stream(parse_string.data(), parse_string.size());
960 TextFormat::Parse(&input_stream, &proto_);
963 EXPECT_EQ(1, proto_.optional_int32());
964 EXPECT_EQ(2, proto_.optional_int64());
967 TEST_F(TextFormatTest, OptionalColon) {
968 // Test that we can place a ':' after the field name of a nested message,
969 // even though we don't have to.
971 std::string parse_string = "optional_nested_message: { bb: 1}\n";
973 io::ArrayInputStream input_stream(parse_string.data(), parse_string.size());
975 TextFormat::Parse(&input_stream, &proto_);
978 EXPECT_TRUE(proto_.has_optional_nested_message());
979 EXPECT_EQ(1, proto_.optional_nested_message().bb());
982 // Some platforms (e.g. Windows) insist on padding the exponent to three
983 // digits when one or two would be just fine.
984 static std::string RemoveRedundantZeros(std::string text) {
985 text = StringReplace(text, "e+0", "e+", true);
986 text = StringReplace(text, "e-0", "e-", true);
990 TEST_F(TextFormatTest, PrintExotic) {
991 unittest::TestAllTypes message;
993 // Note: In C, a negative integer literal is actually the unary negation
994 // operator being applied to a positive integer literal, and
995 // 9223372036854775808 is outside the range of int64. However, it is not
996 // outside the range of uint64. Confusingly, this means that everything
997 // works if we make the literal unsigned, even though we are negating it.
998 message.add_repeated_int64(-PROTOBUF_ULONGLONG(9223372036854775808));
999 message.add_repeated_uint64(PROTOBUF_ULONGLONG(18446744073709551615));
1000 message.add_repeated_double(123.456);
1001 message.add_repeated_double(1.23e21);
1002 message.add_repeated_double(1.23e-18);
1003 message.add_repeated_double(std::numeric_limits<double>::infinity());
1004 message.add_repeated_double(-std::numeric_limits<double>::infinity());
1005 message.add_repeated_double(std::numeric_limits<double>::quiet_NaN());
1006 message.add_repeated_string(std::string("\000\001\a\b\f\n\r\t\v\\\'\"", 12));
1008 // Fun story: We used to use 1.23e22 instead of 1.23e21 above, but this
1009 // seemed to trigger an odd case on MinGW/GCC 3.4.5 where GCC's parsing of
1010 // the value differed from strtod()'s parsing. That is to say, the
1011 // following assertion fails on MinGW:
1012 // assert(1.23e22 == strtod("1.23e22", NULL));
1013 // As a result, SimpleDtoa() would print the value as
1014 // "1.2300000000000001e+22" to make sure strtod() produce the exact same
1015 // result. Our goal is to test runtime parsing, not compile-time parsing,
1016 // so this wasn't our problem. It was found that using 1.23e21 did not
1017 // have this problem, so we switched to that instead.
1020 "repeated_int64: -9223372036854775808\n"
1021 "repeated_uint64: 18446744073709551615\n"
1022 "repeated_double: 123.456\n"
1023 "repeated_double: 1.23e+21\n"
1024 "repeated_double: 1.23e-18\n"
1025 "repeated_double: inf\n"
1026 "repeated_double: -inf\n"
1027 "repeated_double: nan\n"
1029 "\"\\000\\001\\007\\010\\014\\n\\r\\t\\013\\\\\\'\\\"\"\n",
1030 RemoveRedundantZeros(message.DebugString()));
1033 TEST_F(TextFormatTest, PrintFloatPrecision) {
1034 unittest::TestAllTypes message;
1036 message.add_repeated_float(1.0);
1037 message.add_repeated_float(1.2);
1038 message.add_repeated_float(1.23);
1039 message.add_repeated_float(1.234);
1040 message.add_repeated_float(1.2345);
1041 message.add_repeated_float(1.23456);
1042 message.add_repeated_float(1.2e10);
1043 message.add_repeated_float(1.23e10);
1044 message.add_repeated_float(1.234e10);
1045 message.add_repeated_float(1.2345e10);
1046 message.add_repeated_float(1.23456e10);
1047 message.add_repeated_double(1.2);
1048 message.add_repeated_double(1.23);
1049 message.add_repeated_double(1.234);
1050 message.add_repeated_double(1.2345);
1051 message.add_repeated_double(1.23456);
1052 message.add_repeated_double(1.234567);
1053 message.add_repeated_double(1.2345678);
1054 message.add_repeated_double(1.23456789);
1055 message.add_repeated_double(1.234567898);
1056 message.add_repeated_double(1.2345678987);
1057 message.add_repeated_double(1.23456789876);
1058 message.add_repeated_double(1.234567898765);
1059 message.add_repeated_double(1.2345678987654);
1060 message.add_repeated_double(1.23456789876543);
1061 message.add_repeated_double(1.2e100);
1062 message.add_repeated_double(1.23e100);
1063 message.add_repeated_double(1.234e100);
1064 message.add_repeated_double(1.2345e100);
1065 message.add_repeated_double(1.23456e100);
1066 message.add_repeated_double(1.234567e100);
1067 message.add_repeated_double(1.2345678e100);
1068 message.add_repeated_double(1.23456789e100);
1069 message.add_repeated_double(1.234567898e100);
1070 message.add_repeated_double(1.2345678987e100);
1071 message.add_repeated_double(1.23456789876e100);
1072 message.add_repeated_double(1.234567898765e100);
1073 message.add_repeated_double(1.2345678987654e100);
1074 message.add_repeated_double(1.23456789876543e100);
1077 "repeated_float: 1\n"
1078 "repeated_float: 1.2\n"
1079 "repeated_float: 1.23\n"
1080 "repeated_float: 1.234\n"
1081 "repeated_float: 1.2345\n"
1082 "repeated_float: 1.23456\n"
1083 "repeated_float: 1.2e+10\n"
1084 "repeated_float: 1.23e+10\n"
1085 "repeated_float: 1.234e+10\n"
1086 "repeated_float: 1.2345e+10\n"
1087 "repeated_float: 1.23456e+10\n"
1088 "repeated_double: 1.2\n"
1089 "repeated_double: 1.23\n"
1090 "repeated_double: 1.234\n"
1091 "repeated_double: 1.2345\n"
1092 "repeated_double: 1.23456\n"
1093 "repeated_double: 1.234567\n"
1094 "repeated_double: 1.2345678\n"
1095 "repeated_double: 1.23456789\n"
1096 "repeated_double: 1.234567898\n"
1097 "repeated_double: 1.2345678987\n"
1098 "repeated_double: 1.23456789876\n"
1099 "repeated_double: 1.234567898765\n"
1100 "repeated_double: 1.2345678987654\n"
1101 "repeated_double: 1.23456789876543\n"
1102 "repeated_double: 1.2e+100\n"
1103 "repeated_double: 1.23e+100\n"
1104 "repeated_double: 1.234e+100\n"
1105 "repeated_double: 1.2345e+100\n"
1106 "repeated_double: 1.23456e+100\n"
1107 "repeated_double: 1.234567e+100\n"
1108 "repeated_double: 1.2345678e+100\n"
1109 "repeated_double: 1.23456789e+100\n"
1110 "repeated_double: 1.234567898e+100\n"
1111 "repeated_double: 1.2345678987e+100\n"
1112 "repeated_double: 1.23456789876e+100\n"
1113 "repeated_double: 1.234567898765e+100\n"
1114 "repeated_double: 1.2345678987654e+100\n"
1115 "repeated_double: 1.23456789876543e+100\n",
1116 RemoveRedundantZeros(message.DebugString()));
1119 TEST_F(TextFormatTest, AllowPartial) {
1120 unittest::TestRequired message;
1121 TextFormat::Parser parser;
1122 parser.AllowPartialMessage(true);
1123 EXPECT_TRUE(parser.ParseFromString("a: 1", &message));
1124 EXPECT_EQ(1, message.a());
1125 EXPECT_FALSE(message.has_b());
1126 EXPECT_FALSE(message.has_c());
1129 TEST_F(TextFormatTest, ParseExotic) {
1130 unittest::TestAllTypes message;
1131 ASSERT_TRUE(TextFormat::ParseFromString(
1132 "repeated_int32: -1\n"
1133 "repeated_int32: -2147483648\n"
1134 "repeated_int64: -1\n"
1135 "repeated_int64: -9223372036854775808\n"
1136 "repeated_uint32: 4294967295\n"
1137 "repeated_uint32: 2147483648\n"
1138 "repeated_uint64: 18446744073709551615\n"
1139 "repeated_uint64: 9223372036854775808\n"
1140 "repeated_double: 123.0\n"
1141 "repeated_double: 123.5\n"
1142 "repeated_double: 0.125\n"
1143 "repeated_double: 1.23E17\n"
1144 "repeated_double: 1.235E+22\n"
1145 "repeated_double: 1.235e-18\n"
1146 "repeated_double: 123.456789\n"
1147 "repeated_double: inf\n"
1148 "repeated_double: Infinity\n"
1149 "repeated_double: -inf\n"
1150 "repeated_double: -Infinity\n"
1151 "repeated_double: nan\n"
1152 "repeated_double: NaN\n"
1153 "repeated_string: \"\\000\\001\\a\\b\\f\\n\\r\\t\\v\\\\\\'\\\"\"\n",
1156 ASSERT_EQ(2, message.repeated_int32_size());
1157 EXPECT_EQ(-1, message.repeated_int32(0));
1158 // Note: In C, a negative integer literal is actually the unary negation
1159 // operator being applied to a positive integer literal, and 2147483648 is
1160 // outside the range of int32. However, it is not outside the range of
1161 // uint32. Confusingly, this means that everything works if we make the
1162 // literal unsigned, even though we are negating it.
1163 EXPECT_EQ(-2147483648u, message.repeated_int32(1));
1165 ASSERT_EQ(2, message.repeated_int64_size());
1166 EXPECT_EQ(-1, message.repeated_int64(0));
1167 // Note: In C, a negative integer literal is actually the unary negation
1168 // operator being applied to a positive integer literal, and
1169 // 9223372036854775808 is outside the range of int64. However, it is not
1170 // outside the range of uint64. Confusingly, this means that everything
1171 // works if we make the literal unsigned, even though we are negating it.
1172 EXPECT_EQ(-PROTOBUF_ULONGLONG(9223372036854775808),
1173 message.repeated_int64(1));
1175 ASSERT_EQ(2, message.repeated_uint32_size());
1176 EXPECT_EQ(4294967295u, message.repeated_uint32(0));
1177 EXPECT_EQ(2147483648u, message.repeated_uint32(1));
1179 ASSERT_EQ(2, message.repeated_uint64_size());
1180 EXPECT_EQ(PROTOBUF_ULONGLONG(18446744073709551615),
1181 message.repeated_uint64(0));
1182 EXPECT_EQ(PROTOBUF_ULONGLONG(9223372036854775808),
1183 message.repeated_uint64(1));
1185 ASSERT_EQ(13, message.repeated_double_size());
1186 EXPECT_EQ(123.0, message.repeated_double(0));
1187 EXPECT_EQ(123.5, message.repeated_double(1));
1188 EXPECT_EQ(0.125, message.repeated_double(2));
1189 EXPECT_EQ(1.23E17, message.repeated_double(3));
1190 EXPECT_EQ(1.235E22, message.repeated_double(4));
1191 EXPECT_EQ(1.235E-18, message.repeated_double(5));
1192 EXPECT_EQ(123.456789, message.repeated_double(6));
1193 EXPECT_EQ(message.repeated_double(7),
1194 std::numeric_limits<double>::infinity());
1195 EXPECT_EQ(message.repeated_double(8),
1196 std::numeric_limits<double>::infinity());
1197 EXPECT_EQ(message.repeated_double(9),
1198 -std::numeric_limits<double>::infinity());
1199 EXPECT_EQ(message.repeated_double(10),
1200 -std::numeric_limits<double>::infinity());
1201 EXPECT_TRUE(MathLimits<double>::IsNaN(message.repeated_double(11)));
1202 EXPECT_TRUE(MathLimits<double>::IsNaN(message.repeated_double(12)));
1204 // Note: Since these string literals have \0's in them, we must explicitly
1205 // pass their sizes to string's constructor.
1206 ASSERT_EQ(1, message.repeated_string_size());
1207 EXPECT_EQ(std::string("\000\001\a\b\f\n\r\t\v\\\'\"", 12),
1208 message.repeated_string(0));
1211 TextFormat::ParseFromString("repeated_float: 3.4028235e+38\n"
1212 "repeated_float: -3.4028235e+38\n"
1213 "repeated_float: 3.402823567797337e+38\n"
1214 "repeated_float: -3.402823567797337e+38\n",
1216 EXPECT_EQ(message.repeated_float(0), std::numeric_limits<float>::max());
1217 EXPECT_EQ(message.repeated_float(1), -std::numeric_limits<float>::max());
1218 EXPECT_EQ(message.repeated_float(2), std::numeric_limits<float>::infinity());
1219 EXPECT_EQ(message.repeated_float(3), -std::numeric_limits<float>::infinity());
1223 TEST_F(TextFormatTest, PrintFieldsInIndexOrder) {
1224 protobuf_unittest::TestFieldOrderings message;
1225 // Fields are listed in index order instead of field number.
1226 message.set_my_string("str"); // Field number 11
1227 message.set_my_int(12345); // Field number 1
1228 message.set_my_float(0.999); // Field number 101
1229 // Extensions are listed based on the order of extension number.
1230 // Extension number 12.
1233 protobuf_unittest::TestExtensionOrderings2::test_ext_orderings2)
1234 ->set_my_string("ext_str2");
1235 // Extension number 13.
1238 protobuf_unittest::TestExtensionOrderings1::test_ext_orderings1)
1239 ->set_my_string("ext_str1");
1240 // Extension number 14.
1242 .MutableExtension(protobuf_unittest::TestExtensionOrderings2::
1243 TestExtensionOrderings3::test_ext_orderings3)
1244 ->set_my_string("ext_str3");
1245 // Extension number 50.
1246 *message.MutableExtension(protobuf_unittest::my_extension_string) = "ext_str0";
1248 TextFormat::Printer printer;
1251 // By default, print in field number order.
1254 // [protobuf_unittest.TestExtensionOrderings2.test_ext_orderings2] {
1255 // my_string: "ext_str2"
1257 // [protobuf_unittest.TestExtensionOrderings1.test_ext_orderings1] {
1258 // my_string: "ext_str1"
1260 // [protobuf_unittest.TestExtensionOrderings2.TestExtensionOrderings3.test_ext_orderings3]
1262 // my_string: "ext_str3"
1264 // [protobuf_unittest.my_extension_string]: "ext_str0"
1266 printer.PrintToString(message, &text);
1268 "my_int: 12345\nmy_string: "
1269 "\"str\"\n[protobuf_unittest.TestExtensionOrderings2.test_ext_orderings2] "
1271 "\"ext_str2\"\n}\n[protobuf_unittest.TestExtensionOrderings1.test_ext_"
1272 "orderings1] {\n my_string: "
1273 "\"ext_str1\"\n}\n[protobuf_unittest.TestExtensionOrderings2."
1274 "TestExtensionOrderings3.test_ext_orderings3] {\n my_string: "
1275 "\"ext_str3\"\n}\n[protobuf_unittest.my_extension_string]: "
1276 "\"ext_str0\"\nmy_float: 0.999\n",
1279 // Print in index order.
1283 // [protobuf_unittest.TestExtensionOrderings2.test_ext_orderings2] {
1284 // my_string: "ext_str2"
1286 // [protobuf_unittest.TestExtensionOrderings1.test_ext_orderings1] {
1287 // my_string: "ext_str1"
1289 // [protobuf_unittest.TestExtensionOrderings2.TestExtensionOrderings3.test_ext_orderings3]
1291 // my_string: "ext_str3"
1293 // [protobuf_unittest.my_extension_string]: "ext_str0"
1294 printer.SetPrintMessageFieldsInIndexOrder(true);
1295 printer.PrintToString(message, &text);
1297 "my_string: \"str\"\nmy_int: 12345\nmy_float: "
1298 "0.999\n[protobuf_unittest.TestExtensionOrderings2.test_ext_orderings2] "
1300 "\"ext_str2\"\n}\n[protobuf_unittest.TestExtensionOrderings1.test_ext_"
1301 "orderings1] {\n my_string: "
1302 "\"ext_str1\"\n}\n[protobuf_unittest.TestExtensionOrderings2."
1303 "TestExtensionOrderings3.test_ext_orderings3] {\n my_string: "
1304 "\"ext_str3\"\n}\n[protobuf_unittest.my_extension_string]: \"ext_str0\"\n",
1308 class TextFormatParserTest : public testing::Test {
1310 void ExpectFailure(const std::string& input, const std::string& message,
1311 int line, int col) {
1312 std::unique_ptr<unittest::TestAllTypes> proto(new unittest::TestAllTypes);
1313 ExpectFailure(input, message, line, col, proto.get());
1316 void ExpectFailure(const std::string& input, const std::string& message,
1317 int line, int col, Message* proto) {
1318 ExpectMessage(input, message, line, col, proto, false);
1321 void ExpectMessage(const std::string& input, const std::string& message,
1322 int line, int col, Message* proto, bool expected_result) {
1323 MockErrorCollector error_collector;
1324 parser_.RecordErrorsTo(&error_collector);
1325 EXPECT_EQ(expected_result, parser_.ParseFromString(input, proto))
1326 << input << " -> " << proto->DebugString();
1328 StrCat(line) + ":" + StrCat(col) + ": " + message + "\n",
1329 error_collector.text_);
1330 parser_.RecordErrorsTo(nullptr);
1333 void ExpectSuccessAndTree(const std::string& input, Message* proto,
1334 TextFormat::ParseInfoTree* info_tree) {
1335 MockErrorCollector error_collector;
1336 parser_.RecordErrorsTo(&error_collector);
1337 parser_.WriteLocationsTo(info_tree);
1338 EXPECT_TRUE(parser_.ParseFromString(input, proto));
1339 parser_.WriteLocationsTo(nullptr);
1340 parser_.RecordErrorsTo(nullptr);
1343 void ExpectLocation(TextFormat::ParseInfoTree* tree, const Descriptor* d,
1344 const std::string& field_name, int index, int line,
1346 TextFormat::ParseLocation location =
1347 tree->GetLocation(d->FindFieldByName(field_name), index);
1348 EXPECT_EQ(line, location.line);
1349 EXPECT_EQ(column, location.column);
1352 // An error collector which simply concatenates all its errors into a big
1353 // block of text which can be checked.
1354 class MockErrorCollector : public io::ErrorCollector {
1356 MockErrorCollector() {}
1357 ~MockErrorCollector() {}
1361 // implements ErrorCollector -------------------------------------
1362 void AddError(int line, int column, const std::string& message) {
1363 strings::SubstituteAndAppend(&text_, "$0:$1: $2\n", line + 1, column + 1,
1367 void AddWarning(int line, int column, const std::string& message) {
1368 AddError(line, column, "WARNING:" + message);
1372 TextFormat::Parser parser_;
1375 TEST_F(TextFormatParserTest, ParseInfoTreeBuilding) {
1376 std::unique_ptr<unittest::TestAllTypes> message(new unittest::TestAllTypes);
1377 const Descriptor* d = message->GetDescriptor();
1379 std::string stringData =
1380 "optional_int32: 1\n"
1381 "optional_int64: 2\n"
1382 " optional_double: 2.4\n"
1383 "repeated_int32: 5\n"
1384 "repeated_int32: 10\n"
1385 "optional_nested_message <\n"
1388 "repeated_nested_message <\n"
1391 "repeated_nested_message <\n"
1395 TextFormat::ParseInfoTree tree;
1396 ExpectSuccessAndTree(stringData, message.get(), &tree);
1398 // Verify that the tree has the correct positions.
1399 ExpectLocation(&tree, d, "optional_int32", -1, 0, 0);
1400 ExpectLocation(&tree, d, "optional_int64", -1, 1, 0);
1401 ExpectLocation(&tree, d, "optional_double", -1, 2, 2);
1403 ExpectLocation(&tree, d, "repeated_int32", 0, 3, 0);
1404 ExpectLocation(&tree, d, "repeated_int32", 1, 4, 0);
1406 ExpectLocation(&tree, d, "optional_nested_message", -1, 5, 0);
1407 ExpectLocation(&tree, d, "repeated_nested_message", 0, 8, 0);
1408 ExpectLocation(&tree, d, "repeated_nested_message", 1, 11, 0);
1410 // Check for fields not set. For an invalid field, the location returned
1411 // should be -1, -1.
1412 ExpectLocation(&tree, d, "repeated_int64", 0, -1, -1);
1413 ExpectLocation(&tree, d, "repeated_int32", 6, -1, -1);
1414 ExpectLocation(&tree, d, "some_unknown_field", -1, -1, -1);
1416 // Verify inside the nested message.
1417 const FieldDescriptor* nested_field =
1418 d->FindFieldByName("optional_nested_message");
1420 TextFormat::ParseInfoTree* nested_tree =
1421 tree.GetTreeForNested(nested_field, -1);
1422 ExpectLocation(nested_tree, nested_field->message_type(), "bb", -1, 6, 2);
1424 // Verify inside another nested message.
1425 nested_field = d->FindFieldByName("repeated_nested_message");
1426 nested_tree = tree.GetTreeForNested(nested_field, 0);
1427 ExpectLocation(nested_tree, nested_field->message_type(), "bb", -1, 9, 2);
1429 nested_tree = tree.GetTreeForNested(nested_field, 1);
1430 ExpectLocation(nested_tree, nested_field->message_type(), "bb", -1, 12, 2);
1432 // Verify a NULL tree for an unknown nested field.
1433 TextFormat::ParseInfoTree* unknown_nested_tree =
1434 tree.GetTreeForNested(nested_field, 2);
1436 EXPECT_EQ(NULL, unknown_nested_tree);
1439 TEST_F(TextFormatParserTest, ParseFieldValueFromString) {
1440 std::unique_ptr<unittest::TestAllTypes> message(new unittest::TestAllTypes);
1441 const Descriptor* d = message->GetDescriptor();
1443 #define EXPECT_FIELD(name, value, valuestring) \
1444 EXPECT_TRUE(TextFormat::ParseFieldValueFromString( \
1445 valuestring, d->FindFieldByName("optional_" #name), message.get())); \
1446 EXPECT_EQ(value, message->optional_##name()); \
1447 EXPECT_TRUE(message->has_optional_##name());
1449 #define EXPECT_BOOL_FIELD(name, value, valuestring) \
1450 EXPECT_TRUE(TextFormat::ParseFieldValueFromString( \
1451 valuestring, d->FindFieldByName("optional_" #name), message.get())); \
1452 EXPECT_TRUE(message->optional_##name() == value); \
1453 EXPECT_TRUE(message->has_optional_##name());
1455 #define EXPECT_FLOAT_FIELD(name, value, valuestring) \
1456 EXPECT_TRUE(TextFormat::ParseFieldValueFromString( \
1457 valuestring, d->FindFieldByName("optional_" #name), message.get())); \
1458 EXPECT_FLOAT_EQ(value, message->optional_##name()); \
1459 EXPECT_TRUE(message->has_optional_##name());
1461 #define EXPECT_DOUBLE_FIELD(name, value, valuestring) \
1462 EXPECT_TRUE(TextFormat::ParseFieldValueFromString( \
1463 valuestring, d->FindFieldByName("optional_" #name), message.get())); \
1464 EXPECT_DOUBLE_EQ(value, message->optional_##name()); \
1465 EXPECT_TRUE(message->has_optional_##name());
1467 #define EXPECT_INVALID(name, valuestring) \
1468 EXPECT_FALSE(TextFormat::ParseFieldValueFromString( \
1469 valuestring, d->FindFieldByName("optional_" #name), message.get()));
1472 EXPECT_FIELD(int32, 1, "1");
1473 EXPECT_FIELD(int32, -1, "-1");
1474 EXPECT_FIELD(int32, 0x1234, "0x1234");
1475 EXPECT_INVALID(int32, "a");
1476 EXPECT_INVALID(int32, "999999999999999999999999999999999999");
1477 EXPECT_INVALID(int32, "1,2");
1480 EXPECT_FIELD(int64, 1, "1");
1481 EXPECT_FIELD(int64, -1, "-1");
1482 EXPECT_FIELD(int64, 0x1234567812345678LL, "0x1234567812345678");
1483 EXPECT_INVALID(int64, "a");
1484 EXPECT_INVALID(int64, "999999999999999999999999999999999999");
1485 EXPECT_INVALID(int64, "1,2");
1488 EXPECT_FIELD(uint64, 1, "1");
1489 EXPECT_FIELD(uint64, 0xf234567812345678ULL, "0xf234567812345678");
1490 EXPECT_INVALID(uint64, "-1");
1491 EXPECT_INVALID(uint64, "a");
1492 EXPECT_INVALID(uint64, "999999999999999999999999999999999999");
1493 EXPECT_INVALID(uint64, "1,2");
1496 EXPECT_FIELD(fixed32, 1, "1");
1497 EXPECT_FIELD(fixed32, 0x12345678, "0x12345678");
1498 EXPECT_INVALID(fixed32, "-1");
1499 EXPECT_INVALID(fixed32, "a");
1500 EXPECT_INVALID(fixed32, "999999999999999999999999999999999999");
1501 EXPECT_INVALID(fixed32, "1,2");
1504 EXPECT_FIELD(fixed64, 1, "1");
1505 EXPECT_FIELD(fixed64, 0x1234567812345678ULL, "0x1234567812345678");
1506 EXPECT_INVALID(fixed64, "-1");
1507 EXPECT_INVALID(fixed64, "a");
1508 EXPECT_INVALID(fixed64, "999999999999999999999999999999999999");
1509 EXPECT_INVALID(fixed64, "1,2");
1512 EXPECT_BOOL_FIELD(bool, true, "true");
1513 EXPECT_BOOL_FIELD(bool, false, "false");
1514 EXPECT_BOOL_FIELD(bool, true, "1");
1515 EXPECT_BOOL_FIELD(bool, true, "t");
1516 EXPECT_BOOL_FIELD(bool, false, "0");
1517 EXPECT_BOOL_FIELD(bool, false, "f");
1518 EXPECT_FIELD(bool, true, "True");
1519 EXPECT_FIELD(bool, false, "False");
1520 EXPECT_INVALID(bool, "tRue");
1521 EXPECT_INVALID(bool, "faLse");
1522 EXPECT_INVALID(bool, "2");
1523 EXPECT_INVALID(bool, "-0");
1524 EXPECT_INVALID(bool, "on");
1525 EXPECT_INVALID(bool, "a");
1528 EXPECT_FIELD(float, 1, "1");
1529 EXPECT_FLOAT_FIELD(float, 1.5, "1.5");
1530 EXPECT_FLOAT_FIELD(float, 1.5e3, "1.5e3");
1531 EXPECT_FLOAT_FIELD(float, -4.55, "-4.55");
1532 EXPECT_INVALID(float, "a");
1533 EXPECT_INVALID(float, "1,2");
1536 EXPECT_FIELD(double, 1, "1");
1537 EXPECT_FIELD(double, -1, "-1");
1538 EXPECT_DOUBLE_FIELD(double, 2.3, "2.3");
1539 EXPECT_DOUBLE_FIELD(double, 3e5, "3e5");
1540 EXPECT_INVALID(double, "a");
1541 EXPECT_INVALID(double, "1,2");
1542 // Rejects hex and oct numbers for a double field.
1543 EXPECT_INVALID(double, "0xf");
1544 EXPECT_INVALID(double, "012");
1547 EXPECT_FIELD(string, "hello", "\"hello\"");
1548 EXPECT_FIELD(string, "-1.87", "'-1.87'");
1549 EXPECT_INVALID(string, "hello"); // without quote for value
1552 EXPECT_FIELD(nested_enum, unittest::TestAllTypes::BAR, "BAR");
1553 EXPECT_FIELD(nested_enum, unittest::TestAllTypes::BAZ,
1554 StrCat(unittest::TestAllTypes::BAZ));
1555 EXPECT_INVALID(nested_enum, "FOOBAR");
1558 EXPECT_TRUE(TextFormat::ParseFieldValueFromString(
1559 "<bb:12>", d->FindFieldByName("optional_nested_message"), message.get()));
1560 EXPECT_EQ(12, message->optional_nested_message().bb());
1561 EXPECT_TRUE(message->has_optional_nested_message());
1562 EXPECT_INVALID(nested_message, "any");
1565 #undef EXPECT_BOOL_FIELD
1566 #undef EXPECT_FLOAT_FIELD
1567 #undef EXPECT_DOUBLE_FIELD
1568 #undef EXPECT_INVALID
1571 TEST_F(TextFormatParserTest, InvalidToken) {
1572 ExpectFailure("optional_bool: true\n-5\n", "Expected identifier, got: -", 2,
1575 ExpectFailure("optional_bool: true!\n", "Expected identifier, got: !", 1, 20);
1576 ExpectFailure("\"some string\"", "Expected identifier, got: \"some string\"",
1580 TEST_F(TextFormatParserTest, InvalidFieldName) {
1582 "invalid_field: somevalue\n",
1583 "Message type \"protobuf_unittest.TestAllTypes\" has no field named "
1584 "\"invalid_field\".",
1588 TEST_F(TextFormatParserTest, InvalidCapitalization) {
1589 // We require that group names be exactly as they appear in the .proto.
1591 "optionalgroup {\na: 15\n}\n",
1592 "Message type \"protobuf_unittest.TestAllTypes\" has no field named "
1593 "\"optionalgroup\".",
1596 "OPTIONALgroup {\na: 15\n}\n",
1597 "Message type \"protobuf_unittest.TestAllTypes\" has no field named "
1598 "\"OPTIONALgroup\".",
1601 "Optional_Double: 10.0\n",
1602 "Message type \"protobuf_unittest.TestAllTypes\" has no field named "
1603 "\"Optional_Double\".",
1607 TEST_F(TextFormatParserTest, AllowIgnoreCapitalizationError) {
1608 TextFormat::Parser parser;
1609 protobuf_unittest::TestAllTypes proto;
1611 // These fields have a mismatching case.
1612 EXPECT_FALSE(parser.ParseFromString("Optional_Double: 10.0", &proto));
1613 EXPECT_FALSE(parser.ParseFromString("oPtIoNaLgRoUp { a: 15 }", &proto));
1615 // ... but are parsed correctly if we match case insensitive.
1616 parser.AllowCaseInsensitiveField(true);
1617 EXPECT_TRUE(parser.ParseFromString("Optional_Double: 10.0", &proto));
1618 EXPECT_EQ(10.0, proto.optional_double());
1619 EXPECT_TRUE(parser.ParseFromString("oPtIoNaLgRoUp { a: 15 }", &proto));
1620 EXPECT_EQ(15, proto.optionalgroup().a());
1623 TEST_F(TextFormatParserTest, InvalidFieldValues) {
1624 // Invalid values for a double/float field.
1625 ExpectFailure("optional_double: \"hello\"\n",
1626 "Expected double, got: \"hello\"", 1, 18);
1627 ExpectFailure("optional_double: true\n", "Expected double, got: true", 1, 18);
1628 ExpectFailure("optional_double: !\n", "Expected double, got: !", 1, 18);
1629 ExpectFailure("optional_double {\n \n}\n", "Expected \":\", found \"{\".", 1,
1632 // Invalid values for a signed integer field.
1633 ExpectFailure("optional_int32: \"hello\"\n",
1634 "Expected integer, got: \"hello\"", 1, 17);
1635 ExpectFailure("optional_int32: true\n", "Expected integer, got: true", 1, 17);
1636 ExpectFailure("optional_int32: 4.5\n", "Expected integer, got: 4.5", 1, 17);
1637 ExpectFailure("optional_int32: !\n", "Expected integer, got: !", 1, 17);
1638 ExpectFailure("optional_int32 {\n \n}\n", "Expected \":\", found \"{\".", 1,
1640 ExpectFailure("optional_int32: 0x80000000\n",
1641 "Integer out of range (0x80000000)", 1, 17);
1642 ExpectFailure("optional_int64: 0x8000000000000000\n",
1643 "Integer out of range (0x8000000000000000)", 1, 17);
1644 ExpectFailure("optional_int32: -0x80000001\n",
1645 "Integer out of range (0x80000001)", 1, 18);
1646 ExpectFailure("optional_int64: -0x8000000000000001\n",
1647 "Integer out of range (0x8000000000000001)", 1, 18);
1649 // Invalid values for an unsigned integer field.
1650 ExpectFailure("optional_uint64: \"hello\"\n",
1651 "Expected integer, got: \"hello\"", 1, 18);
1652 ExpectFailure("optional_uint64: true\n", "Expected integer, got: true", 1,
1654 ExpectFailure("optional_uint64: 4.5\n", "Expected integer, got: 4.5", 1, 18);
1655 ExpectFailure("optional_uint64: -5\n", "Expected integer, got: -", 1, 18);
1656 ExpectFailure("optional_uint64: !\n", "Expected integer, got: !", 1, 18);
1657 ExpectFailure("optional_uint64 {\n \n}\n", "Expected \":\", found \"{\".", 1,
1659 ExpectFailure("optional_uint32: 0x100000000\n",
1660 "Integer out of range (0x100000000)", 1, 18);
1661 ExpectFailure("optional_uint64: 0x10000000000000000\n",
1662 "Integer out of range (0x10000000000000000)", 1, 18);
1664 // Invalid values for a boolean field.
1665 ExpectFailure("optional_bool: \"hello\"\n",
1666 "Expected identifier, got: \"hello\"", 1, 16);
1667 ExpectFailure("optional_bool: 5\n", "Integer out of range (5)", 1, 16);
1668 ExpectFailure("optional_bool: -7.5\n", "Expected identifier, got: -", 1, 16);
1669 ExpectFailure("optional_bool: !\n", "Expected identifier, got: !", 1, 16);
1672 "optional_bool: meh\n",
1673 "Invalid value for boolean field \"optional_bool\". Value: \"meh\".", 2,
1676 ExpectFailure("optional_bool {\n \n}\n", "Expected \":\", found \"{\".", 1,
1679 // Invalid values for a string field.
1680 ExpectFailure("optional_string: true\n", "Expected string, got: true", 1, 18);
1681 ExpectFailure("optional_string: 5\n", "Expected string, got: 5", 1, 18);
1682 ExpectFailure("optional_string: -7.5\n", "Expected string, got: -", 1, 18);
1683 ExpectFailure("optional_string: !\n", "Expected string, got: !", 1, 18);
1684 ExpectFailure("optional_string {\n \n}\n", "Expected \":\", found \"{\".", 1,
1687 // Invalid values for an enumeration field.
1688 ExpectFailure("optional_nested_enum: \"hello\"\n",
1689 "Expected integer or identifier, got: \"hello\"", 1, 23);
1691 // Valid token, but enum value is not defined.
1692 ExpectFailure("optional_nested_enum: 5\n",
1693 "Unknown enumeration value of \"5\" for field "
1694 "\"optional_nested_enum\".",
1696 // We consume the negative sign, so the error position starts one character
1698 ExpectFailure("optional_nested_enum: -7.5\n", "Expected integer, got: 7.5", 1,
1700 ExpectFailure("optional_nested_enum: !\n",
1701 "Expected integer or identifier, got: !", 1, 23);
1703 ExpectFailure("optional_nested_enum: grah\n",
1704 "Unknown enumeration value of \"grah\" for field "
1705 "\"optional_nested_enum\".",
1708 ExpectFailure("optional_nested_enum {\n \n}\n",
1709 "Expected \":\", found \"{\".", 1, 22);
1712 TEST_F(TextFormatParserTest, MessageDelimiters) {
1713 // Non-matching delimiters.
1714 ExpectFailure("OptionalGroup <\n \n}\n", "Expected \">\", found \"}\".", 3,
1717 // Invalid delimiters.
1718 ExpectFailure("OptionalGroup [\n \n]\n", "Expected \"{\", found \"[\".", 1,
1721 // Unending message.
1722 ExpectFailure("optional_nested_message {\n \nbb: 118\n",
1723 "Expected identifier, got: ", 4, 1);
1726 TEST_F(TextFormatParserTest, UnknownExtension) {
1727 // Non-matching delimiters.
1728 ExpectFailure("[blahblah]: 123",
1729 "Extension \"blahblah\" is not defined or is not an "
1730 "extension of \"protobuf_unittest.TestAllTypes\".",
1734 TEST_F(TextFormatParserTest, MissingRequired) {
1735 unittest::TestRequired message;
1736 ExpectFailure("a: 1", "Message missing required fields: b, c", 0, 1,
1740 TEST_F(TextFormatParserTest, ParseDuplicateRequired) {
1741 unittest::TestRequired message;
1742 ExpectFailure("a: 1 b: 2 c: 3 a: 1",
1743 "Non-repeated field \"a\" is specified multiple times.", 1, 17,
1747 TEST_F(TextFormatParserTest, ParseDuplicateOptional) {
1748 unittest::ForeignMessage message;
1749 ExpectFailure("c: 1 c: 2",
1750 "Non-repeated field \"c\" is specified multiple times.", 1, 7,
1754 TEST_F(TextFormatParserTest, MergeDuplicateRequired) {
1755 unittest::TestRequired message;
1756 TextFormat::Parser parser;
1757 EXPECT_TRUE(parser.MergeFromString("a: 1 b: 2 c: 3 a: 4", &message));
1758 EXPECT_EQ(4, message.a());
1761 TEST_F(TextFormatParserTest, MergeDuplicateOptional) {
1762 unittest::ForeignMessage message;
1763 TextFormat::Parser parser;
1764 EXPECT_TRUE(parser.MergeFromString("c: 1 c: 2", &message));
1765 EXPECT_EQ(2, message.c());
1768 TEST_F(TextFormatParserTest, ExplicitDelimiters) {
1769 unittest::TestRequired message;
1770 EXPECT_TRUE(TextFormat::ParseFromString("a:1,b:2;c:3", &message));
1771 EXPECT_EQ(1, message.a());
1772 EXPECT_EQ(2, message.b());
1773 EXPECT_EQ(3, message.c());
1776 TEST_F(TextFormatParserTest, PrintErrorsToStderr) {
1777 std::vector<std::string> errors;
1780 ScopedMemoryLog log;
1781 unittest::TestAllTypes proto;
1782 EXPECT_FALSE(TextFormat::ParseFromString("no_such_field: 1", &proto));
1783 errors = log.GetMessages(ERROR);
1786 ASSERT_EQ(1, errors.size());
1788 "Error parsing text-format protobuf_unittest.TestAllTypes: "
1789 "1:14: Message type \"protobuf_unittest.TestAllTypes\" has no field "
1790 "named \"no_such_field\".",
1794 TEST_F(TextFormatParserTest, FailsOnTokenizationError) {
1795 std::vector<std::string> errors;
1798 ScopedMemoryLog log;
1799 unittest::TestAllTypes proto;
1800 EXPECT_FALSE(TextFormat::ParseFromString("\020", &proto));
1801 errors = log.GetMessages(ERROR);
1804 ASSERT_EQ(1, errors.size());
1806 "Error parsing text-format protobuf_unittest.TestAllTypes: "
1807 "1:1: Invalid control characters encountered in text.",
1811 TEST_F(TextFormatParserTest, ParseDeprecatedField) {
1812 unittest::TestDeprecatedFields message;
1813 ExpectMessage("deprecated_int32: 42",
1814 "WARNING:text format contains deprecated field "
1815 "\"deprecated_int32\"",
1816 1, 21, &message, true);
1819 TEST_F(TextFormatParserTest, SetRecursionLimit) {
1820 const char* format = "child: { $0 }";
1822 for (int i = 0; i < 100; ++i) input = strings::Substitute(format, input);
1824 unittest::NestedTestAllTypes message;
1825 ExpectSuccessAndTree(input, &message, nullptr);
1827 input = strings::Substitute(format, input);
1828 parser_.SetRecursionLimit(100);
1829 ExpectMessage(input, "Message is too deep", 1, 908, &message, false);
1831 parser_.SetRecursionLimit(101);
1832 ExpectSuccessAndTree(input, &message, nullptr);
1835 class TextFormatMessageSetTest : public testing::Test {
1837 static const char proto_debug_string_[];
1839 const char TextFormatMessageSetTest::proto_debug_string_[] =
1841 " [protobuf_unittest.TestMessageSetExtension1] {\n"
1844 " [protobuf_unittest.TestMessageSetExtension2] {\n"
1849 TEST_F(TextFormatMessageSetTest, Serialize) {
1850 protobuf_unittest::TestMessageSetContainer proto;
1851 protobuf_unittest::TestMessageSetExtension1* item_a =
1852 proto.mutable_message_set()->MutableExtension(
1853 protobuf_unittest::TestMessageSetExtension1::message_set_extension);
1855 protobuf_unittest::TestMessageSetExtension2* item_b =
1856 proto.mutable_message_set()->MutableExtension(
1857 protobuf_unittest::TestMessageSetExtension2::message_set_extension);
1858 item_b->set_str("foo");
1859 EXPECT_EQ(proto_debug_string_, proto.DebugString());
1862 TEST_F(TextFormatMessageSetTest, Deserialize) {
1863 protobuf_unittest::TestMessageSetContainer proto;
1864 ASSERT_TRUE(TextFormat::ParseFromString(proto_debug_string_, &proto));
1869 protobuf_unittest::TestMessageSetExtension1::message_set_extension)
1875 protobuf_unittest::TestMessageSetExtension2::message_set_extension)
1878 // Ensure that these are the only entries present.
1879 std::vector<const FieldDescriptor*> descriptors;
1880 proto.message_set().GetReflection()->ListFields(proto.message_set(),
1882 EXPECT_EQ(2, descriptors.size());
1885 TEST(TextFormatUnknownFieldTest, TestUnknownField) {
1886 protobuf_unittest::TestAllTypes proto;
1887 TextFormat::Parser parser;
1888 // Unknown field is not permitted by default.
1889 EXPECT_FALSE(parser.ParseFromString("unknown_field: 12345", &proto));
1890 EXPECT_FALSE(parser.ParseFromString("12345678: 12345", &proto));
1892 parser.AllowUnknownField(true);
1893 EXPECT_TRUE(parser.ParseFromString("unknown_field: 12345", &proto));
1894 EXPECT_TRUE(parser.ParseFromString("unknown_field: -12345", &proto));
1895 EXPECT_TRUE(parser.ParseFromString("unknown_field: 1.2345", &proto));
1896 EXPECT_TRUE(parser.ParseFromString("unknown_field: -1.2345", &proto));
1897 EXPECT_TRUE(parser.ParseFromString("unknown_field: 1.2345f", &proto));
1898 EXPECT_TRUE(parser.ParseFromString("unknown_field: -1.2345f", &proto));
1899 EXPECT_TRUE(parser.ParseFromString("unknown_field: inf", &proto));
1900 EXPECT_TRUE(parser.ParseFromString("unknown_field: -inf", &proto));
1901 EXPECT_TRUE(parser.ParseFromString("unknown_field: TYPE_STRING", &proto));
1903 parser.ParseFromString("unknown_field: \"string value\"", &proto));
1904 // Invalid field value
1905 EXPECT_FALSE(parser.ParseFromString("unknown_field: -TYPE_STRING", &proto));
1906 // Two or more unknown fields
1908 parser.ParseFromString("unknown_field1: TYPE_STRING\n"
1909 "unknown_field2: 12345",
1911 // Unknown nested message
1913 parser.ParseFromString("unknown_message1: {}\n"
1914 "unknown_message2 {\n"
1915 " unknown_field: 12345\n"
1917 "unknown_message3 <\n"
1918 " unknown_nested_message {\n"
1919 " unknown_field: 12345\n"
1923 // Unmatched delimeters for message body
1924 EXPECT_FALSE(parser.ParseFromString("unknown_message: {>", &proto));
1925 // Unknown extension
1927 parser.ParseFromString("[somewhere.unknown_extension1]: 12345\n"
1928 "[somewhere.unknown_extension2] {\n"
1929 " unknown_field: 12345\n"
1932 // Unknown fields between known fields
1934 parser.ParseFromString("optional_int32: 1\n"
1935 "unknown_field: 12345\n"
1936 "optional_string: \"string\"\n"
1937 "unknown_message { unknown: 0 }\n"
1938 "optional_nested_message { bb: 2 }",
1940 EXPECT_EQ(1, proto.optional_int32());
1941 EXPECT_EQ("string", proto.optional_string());
1942 EXPECT_EQ(2, proto.optional_nested_message().bb());
1944 // Unknown field with numeric tag number instead of identifier.
1945 EXPECT_TRUE(parser.ParseFromString("12345678: 12345", &proto));
1947 // Nested unknown extensions.
1949 parser.ParseFromString("[test.extension1] <\n"
1950 " unknown_nested_message <\n"
1951 " [test.extension2] <\n"
1952 " unknown_field: 12345\n"
1958 parser.ParseFromString("[test.extension1] {\n"
1959 " unknown_nested_message {\n"
1960 " [test.extension2] {\n"
1961 " unknown_field: 12345\n"
1967 parser.ParseFromString("[test.extension1] <\n"
1968 " some_unknown_fields: <\n"
1969 " unknown_field: 12345\n"
1974 parser.ParseFromString("[test.extension1] {\n"
1975 " some_unknown_fields: {\n"
1976 " unknown_field: 12345\n"
1981 // Unknown field with compact repetition.
1982 EXPECT_TRUE(parser.ParseFromString("unknown_field: [1, 2]", &proto));
1983 // Unknown field with compact repetition of some unknown enum.
1984 EXPECT_TRUE(parser.ParseFromString("unknown_field: [VAL1, VAL2]", &proto));
1985 // Unknown field with compact repetition with sub-message.
1986 EXPECT_TRUE(parser.ParseFromString("unknown_field: [{a:1}, <b:2>]", &proto));
1989 TEST(TextFormatUnknownFieldTest, TestAnyInUnknownField) {
1990 protobuf_unittest::TestAllTypes proto;
1991 TextFormat::Parser parser;
1992 parser.AllowUnknownField(true);
1994 parser.ParseFromString("unknown {\n"
1995 " [type.googleapis.com/foo.bar] {\n"
2001 TEST(TextFormatUnknownFieldTest, TestUnknownExtension) {
2002 protobuf_unittest::TestAllTypes proto;
2003 TextFormat::Parser parser;
2004 std::string message_with_ext =
2005 "[test.extension1] {\n"
2006 " some_unknown_fields: {\n"
2007 " unknown_field: 12345\n"
2010 // Unknown extensions are not permitted by default.
2011 EXPECT_FALSE(parser.ParseFromString(message_with_ext, &proto));
2012 // AllowUnknownField implies AllowUnknownExtension.
2013 parser.AllowUnknownField(true);
2014 EXPECT_TRUE(parser.ParseFromString(message_with_ext, &proto));
2016 parser.AllowUnknownField(false);
2017 EXPECT_FALSE(parser.ParseFromString(message_with_ext, &proto));
2018 parser.AllowUnknownExtension(true);
2019 EXPECT_TRUE(parser.ParseFromString(message_with_ext, &proto));
2020 // Unknown fields are still not accepted.
2021 EXPECT_FALSE(parser.ParseFromString("unknown_field: 1", &proto));
2025 } // namespace text_format_unittest
2026 } // namespace protobuf
2027 } // namespace google