35 #include <google/protobuf/text_format.h>
44 #include <google/protobuf/stubs/logging.h>
45 #include <google/protobuf/stubs/common.h>
46 #include <google/protobuf/testing/file.h>
47 #include <google/protobuf/testing/file.h>
48 #include <google/protobuf/any.pb.h>
49 #include <google/protobuf/map_unittest.pb.h>
50 #include <google/protobuf/test_util.h>
51 #include <google/protobuf/test_util2.h>
52 #include <google/protobuf/unittest.pb.h>
53 #include <google/protobuf/unittest_mset.pb.h>
54 #include <google/protobuf/unittest_mset_wire_format.pb.h>
55 #include <google/protobuf/unittest_proto3.pb.h>
56 #include <google/protobuf/io/tokenizer.h>
57 #include <google/protobuf/io/zero_copy_stream_impl.h>
58 #include <google/protobuf/stubs/strutil.h>
59 #include <gmock/gmock.h>
60 #include <google/protobuf/testing/googletest.h>
61 #include <gtest/gtest.h>
62 #include <google/protobuf/stubs/logging.h>
63 #include <google/protobuf/stubs/substitute.h>
66 #include <google/protobuf/port_def.inc>
77 namespace text_format_unittest {
81 "\"A string with ' characters \n and \r newlines and \t tabs and \001 "
82 "slashes \\ and multiple spaces";
86 "\"\\\"A string with \\' characters \\n and \\r newlines "
87 "and \\t tabs and \\001 slashes \\\\ and multiple spaces\"";
94 "net/proto2/internal/"
95 "testdata/text_format_unittest_data_oneof_implemented.txt"),
105 unittest::TestAllTypes
proto_;
117 "text_format_unittest_extensions_data.txt"),
128 unittest::TestAllExtensions
proto_;
135 TEST_F(TextFormatTest, Basic) {
137 EXPECT_EQ(proto_debug_string_, proto_.DebugString());
142 EXPECT_EQ(proto_debug_string_, proto_.DebugString());
145 TEST_F(TextFormatTest, ShortDebugString) {
146 proto_.set_optional_int32(1);
147 proto_.set_optional_string(
"hello");
148 proto_.mutable_optional_nested_message()->set_bb(2);
149 proto_.mutable_optional_foreign_message();
152 "optional_int32: 1 optional_string: \"hello\" "
153 "optional_nested_message { bb: 2 } "
154 "optional_foreign_message { }",
155 proto_.ShortDebugString());
158 TEST_F(TextFormatTest, ShortPrimitiveRepeateds) {
159 proto_.set_optional_int32(123);
160 proto_.add_repeated_int32(456);
161 proto_.add_repeated_int32(789);
162 proto_.add_repeated_string(
"foo");
163 proto_.add_repeated_string(
"bar");
164 proto_.add_repeated_nested_message()->set_bb(2);
165 proto_.add_repeated_nested_message()->set_bb(3);
166 proto_.add_repeated_nested_enum(unittest::TestAllTypes::FOO);
167 proto_.add_repeated_nested_enum(unittest::TestAllTypes::BAR);
170 printer.SetUseShortRepeatedPrimitives(
true);
175 "optional_int32: 123\n"
176 "repeated_int32: [456, 789]\n"
177 "repeated_string: \"foo\"\n"
178 "repeated_string: \"bar\"\n"
179 "repeated_nested_message {\n bb: 2\n}\n"
180 "repeated_nested_message {\n bb: 3\n}\n"
181 "repeated_nested_enum: [FOO, BAR]\n",
186 text =
"just some data here...\n\nblah blah";
190 "optional_int32: 123\n"
191 "repeated_int32: [456, 789]\n"
192 "repeated_string: \"foo\"\n"
193 "repeated_string: \"bar\"\n"
194 "repeated_nested_message {\n bb: 2\n}\n"
195 "repeated_nested_message {\n bb: 3\n}\n"
196 "repeated_nested_enum: [FOO, BAR]\n",
200 printer.SetSingleLineMode(
true);
204 "optional_int32: 123 "
205 "repeated_int32: [456, 789] "
206 "repeated_string: \"foo\" "
207 "repeated_string: \"bar\" "
208 "repeated_nested_message { bb: 2 } "
209 "repeated_nested_message { bb: 3 } "
210 "repeated_nested_enum: [FOO, BAR] ",
215 TEST_F(TextFormatTest, StringEscape) {
221 std::string utf8_debug_string = proto_.Utf8DebugString();
231 EXPECT_EQ(correct_string, utf8_debug_string);
235 EXPECT_EQ(expected_short_debug_string, proto_.ShortDebugString());
238 TEST_F(TextFormatTest, Utf8DebugString) {
240 proto_.set_optional_string(
"\350\260\267\346\255\214");
241 proto_.set_optional_bytes(
"\350\260\267\346\255\214");
245 std::string utf8_debug_string = proto_.Utf8DebugString();
250 "\"\350\260\267\346\255\214\""
253 "\"\\350\\260\\267\\346\\255\\214\""
257 "\"\\350\\260\\267\\346\\255\\214\""
260 "\"\\350\\260\\267\\346\\255\\214\""
264 EXPECT_EQ(correct_utf8_string, utf8_debug_string);
268 TEST_F(TextFormatTest, PrintUnknownFields) {
271 unittest::TestEmptyMessage
message;
274 unknown_fields->AddVarint(5, 1);
275 unknown_fields->AddFixed32(5, 2);
276 unknown_fields->AddFixed64(5, 3);
277 unknown_fields->AddLengthDelimited(5,
"4");
278 unknown_fields->AddGroup(5)->AddVarint(10, 5);
280 unknown_fields->AddVarint(8, 1);
281 unknown_fields->AddVarint(8, 2);
282 unknown_fields->AddVarint(8, 3);
287 "5: 0x0000000000000003\n"
298 TEST_F(TextFormatTest, PrintUnknownFieldsHidden) {
305 unknown_fields->AddVarint(5, 1);
306 unknown_fields->AddFixed32(5, 2);
307 unknown_fields->AddFixed64(5, 3);
308 unknown_fields->AddLengthDelimited(5,
"4");
309 unknown_fields->AddGroup(5)->AddVarint(10, 5);
311 unknown_fields->AddVarint(8, 1);
312 unknown_fields->AddVarint(8, 2);
313 unknown_fields->AddVarint(8, 3);
316 printer.SetHideUnknownFields(
true);
323 TEST_F(TextFormatTest, PrintUnknownMessage) {
326 protobuf_unittest::TestAllTypes
message;
333 message.add_repeated_string(
"abc");
338 message.add_repeated_string(
"def");
342 message.add_repeated_string(
"");
348 message.add_repeated_nested_message()->set_bb(123);
368 static constexpr
int kNestingDepth = 25000;
369 static constexpr
int kUnknownFieldNumber = 1;
370 std::vector<int> lengths;
371 lengths.reserve(kNestingDepth);
372 lengths.push_back(0);
373 for (
int i = 0;
i < kNestingDepth - 1; ++
i) {
383 for (
int i = kNestingDepth - 1;
i >= 0; --
i) {
393 unittest::TestEmptyMessage
message;
399 TEST_F(TextFormatTest, PrintMessageWithIndent) {
402 protobuf_unittest::TestAllTypes
message;
404 message.add_repeated_string(
"abc");
405 message.add_repeated_string(
"def");
406 message.add_repeated_nested_message()->set_bb(123);
413 " repeated_string: \"abc\"\n"
414 " repeated_string: \"def\"\n"
415 " repeated_nested_message {\n"
421 TEST_F(TextFormatTest, PrintMessageSingleLine) {
424 protobuf_unittest::TestAllTypes
message;
426 message.add_repeated_string(
"abc");
427 message.add_repeated_string(
"def");
428 message.add_repeated_nested_message()->set_bb(123);
433 printer.SetSingleLineMode(
true);
436 " repeated_string: \"abc\" repeated_string: \"def\" "
437 "repeated_nested_message { bb: 123 } ",
441 TEST_F(TextFormatTest, PrintBufferTooSmall) {
444 protobuf_unittest::TestAllTypes
message;
446 message.add_repeated_string(
"abc");
447 message.add_repeated_string(
"def");
450 io::ArrayOutputStream output_stream(
buffer, 1);
457 class CustomUInt32FieldValuePrinter :
public TextFormat::FieldValuePrinter {
460 return StrCat(FieldValuePrinter::PrintUInt32(val),
"u");
464 TEST_F(TextFormatTest, DefaultCustomFieldPrinter) {
465 protobuf_unittest::TestAllTypes
message;
467 message.set_optional_uint32(42);
468 message.add_repeated_uint32(1);
469 message.add_repeated_uint32(2);
470 message.add_repeated_uint32(3);
478 EXPECT_EQ(
"optional_uint32: 42u\nrepeated_uint32: [1u, 2u, 3u]\n",
text);
481 class CustomInt32FieldValuePrinter :
public TextFormat::FieldValuePrinter {
484 return StrCat(
"value-is(", FieldValuePrinter::PrintInt32(val),
")");
488 TEST_F(TextFormatTest, FieldSpecificCustomPrinter) {
489 protobuf_unittest::TestAllTypes
message;
491 message.set_optional_int32(42);
492 message.add_repeated_int32(42);
496 message.GetDescriptor()->FindFieldByName(
"optional_int32"),
497 new CustomInt32FieldValuePrinter()));
500 EXPECT_EQ(
"optional_int32: value-is(42)\nrepeated_int32: 42\n",
text);
503 TEST_F(TextFormatTest, FieldSpecificCustomPrinterRegisterSameFieldTwice) {
504 protobuf_unittest::TestAllTypes
message;
507 message.GetDescriptor()->FindFieldByName(
"optional_int32");
509 field,
new CustomInt32FieldValuePrinter()));
510 const TextFormat::FieldValuePrinter*
const rejected =
511 new CustomInt32FieldValuePrinter();
516 TEST_F(TextFormatTest, ErrorCasesRegisteringFieldValuePrinterShouldFail) {
517 protobuf_unittest::TestAllTypes
message;
521 message.GetDescriptor()->FindFieldByName(
"optional_int32"),
522 static_cast<const TextFormat::FieldValuePrinter*
>(
nullptr)));
524 message.GetDescriptor()->FindFieldByName(
"optional_int32"),
525 static_cast<const TextFormat::FastFieldValuePrinter*
>(
nullptr)));
527 TextFormat::FieldValuePrinter my_field_printer;
529 EXPECT_FALSE(printer.RegisterFieldValuePrinter(
nullptr, &my_field_printer));
532 class CustomMessageFieldValuePrinter :
public TextFormat::FieldValuePrinter {
535 return StrCat(FieldValuePrinter::PrintInt32(
v),
" # x",
541 bool single_line_mode)
const {
542 if (single_line_mode) {
545 return StrCat(
" { # ",
message.GetDescriptor()->name(),
": ",
550 TEST_F(TextFormatTest, CustomPrinterForComments) {
551 protobuf_unittest::TestAllTypes
message;
552 message.mutable_optional_nested_message();
553 message.mutable_optional_import_message()->set_d(42);
554 message.add_repeated_nested_message();
555 message.add_repeated_nested_message();
556 message.add_repeated_import_message()->set_d(43);
557 message.add_repeated_import_message()->set_d(44);
559 CustomMessageFieldValuePrinter my_field_printer;
564 "optional_nested_message { # NestedMessage: -1\n"
566 "optional_import_message { # ImportMessage: -1\n"
569 "repeated_nested_message { # NestedMessage: 0\n"
571 "repeated_nested_message { # NestedMessage: 1\n"
573 "repeated_import_message { # ImportMessage: 0\n"
576 "repeated_import_message { # ImportMessage: 1\n"
587 bool single_line_mode,
589 if (
message.ByteSizeLong() > 0) {
598 protobuf_unittest::TestAllTypes
message;
599 message.mutable_optional_nested_message();
600 message.mutable_optional_import_message()->set_d(42);
601 message.add_repeated_nested_message();
602 message.add_repeated_nested_message();
603 message.add_repeated_import_message()->set_d(43);
604 message.add_repeated_import_message()->set_d(44);
612 "optional_nested_message {\n"
614 "optional_import_message {\n"
615 " # REDACTED, 2 bytes\n"
617 "repeated_nested_message {\n"
619 "repeated_nested_message {\n"
621 "repeated_import_message {\n"
622 " # REDACTED, 2 bytes\n"
624 "repeated_import_message {\n"
625 " # REDACTED, 2 bytes\n"
634 bool single_line_comment)
const {
635 return StrCat(
" { # 1\n",
" # 2\n");
639 TEST_F(TextFormatTest, CustomPrinterForMultilineComments) {
640 protobuf_unittest::TestAllTypes
message;
641 message.mutable_optional_nested_message();
642 message.mutable_optional_import_message()->set_d(42);
644 CustomMessageFieldValuePrinter my_field_printer;
649 "optional_nested_message { # 1\n"
652 "optional_import_message { # 1\n"
662 class CompactRepeatedFieldPrinter :
public TextFormat::FastFieldValuePrinter {
668 if (field_index == 0 || field_index == -1) {
679 bool single_line_mode,
681 if (field_index == 0 || field_index == -1) {
682 if (single_line_mode) {
691 bool single_line_mode,
693 if (field_index == field_count - 1 || field_index == -1) {
694 if (single_line_mode) {
703 TEST_F(TextFormatTest, CompactRepeatedFieldPrinter) {
706 unittest::TestAllTypes::default_instance()
709 unittest::TestAllTypes::kRepeatedNestedMessageFieldNumber),
710 new CompactRepeatedFieldPrinter));
712 protobuf_unittest::TestAllTypes
message;
713 message.add_repeated_nested_message()->set_bb(1);
714 message.add_repeated_nested_message()->set_bb(2);
715 message.add_repeated_nested_message()->set_bb(3);
720 "repeated_nested_message {\n"
730 class MultilineStringPrinter :
public TextFormat::FastFieldValuePrinter {
736 int newline_pos = val.find(
'\n');
737 while (newline_pos != std::string::npos) {
740 val.substr(last_pos, newline_pos + 1 - last_pos), generator);
741 last_pos = newline_pos + 1;
742 newline_pos = val.find(
'\n', last_pos);
744 if (last_pos < val.size()) {
753 TEST_F(TextFormatTest, MultilineStringPrinter) {
756 unittest::TestAllTypes::default_instance()
759 unittest::TestAllTypes::kOptionalStringFieldNumber),
760 new MultilineStringPrinter));
762 protobuf_unittest::TestAllTypes
message;
763 message.set_optional_string(
"first line\nsecond line\nthird line");
768 "optional_string: \n"
769 " \"first line\\n\"\n"
770 " \"second line\\n\"\n"
775 class CustomNestedMessagePrinter :
public TextFormat::MessagePrinter {
785 TEST_F(TextFormatTest, CustomMessagePrinter) {
788 unittest::TestAllTypes::NestedMessage::default_instance().
descriptor(),
789 new CustomNestedMessagePrinter);
791 unittest::TestAllTypes
message;
796 message.mutable_optional_nested_message()->set_bb(1);
801 TEST_F(TextFormatTest, ParseBasic) {
802 io::ArrayInputStream input_stream(proto_debug_string_.data(),
803 proto_debug_string_.size());
808 TEST_F(TextFormatExtensionsTest, ParseExtensions) {
809 io::ArrayInputStream input_stream(proto_debug_string_.data(),
810 proto_debug_string_.size());
812 TestUtil::ExpectAllExtensionsSet(proto_);
815 TEST_F(TextFormatTest, ParseEnumFieldFromNumber) {
821 EXPECT_EQ(unittest::TestAllTypes::BAZ, proto_.optional_nested_enum());
824 TEST_F(TextFormatTest, ParseEnumFieldFromNegativeNumber) {
828 unittest::SparseEnumMessage proto;
831 EXPECT_EQ(unittest::SPARSE_E, proto.sparse_enum());
834 TEST_F(TextFormatTest, PrintUnknownEnumFieldProto3) {
835 proto3_unittest::TestAllTypes proto;
837 proto.add_repeated_nested_enum(
838 static_cast<proto3_unittest::TestAllTypes::NestedEnum
>(10));
839 proto.add_repeated_nested_enum(
840 static_cast<proto3_unittest::TestAllTypes::NestedEnum
>(-10));
841 proto.add_repeated_nested_enum(
842 static_cast<proto3_unittest::TestAllTypes::NestedEnum
>(2147483647));
843 proto.add_repeated_nested_enum(
844 static_cast<proto3_unittest::TestAllTypes::NestedEnum
>(-2147483648));
847 "repeated_nested_enum: 10\n"
848 "repeated_nested_enum: -10\n"
849 "repeated_nested_enum: 2147483647\n"
850 "repeated_nested_enum: -2147483648\n",
851 proto.DebugString());
854 TEST_F(TextFormatTest, ParseUnknownEnumFieldProto3) {
855 proto3_unittest::TestAllTypes proto;
857 "repeated_nested_enum: [10, -10, 2147483647, -2147483648]";
859 ASSERT_EQ(4, proto.repeated_nested_enum_size());
860 EXPECT_EQ(10, proto.repeated_nested_enum(0));
861 EXPECT_EQ(-10, proto.repeated_nested_enum(1));
862 EXPECT_EQ(2147483647, proto.repeated_nested_enum(2));
863 EXPECT_EQ(-2147483648, proto.repeated_nested_enum(3));
866 TEST_F(TextFormatTest, ParseStringEscape) {
871 io::ArrayInputStream input_stream(parse_string.data(), parse_string.size());
878 TEST_F(TextFormatTest, ParseConcatenatedString) {
880 std::string parse_string =
"optional_string: \"foo\" \"bar\"\n";
882 io::ArrayInputStream input_stream1(parse_string.data(), parse_string.size());
886 EXPECT_EQ(
"foobar", proto_.optional_string());
890 "optional_string: \"foo\"\n"
893 io::ArrayInputStream input_stream2(parse_string.data(), parse_string.size());
897 EXPECT_EQ(
"foobar", proto_.optional_string());
900 TEST_F(TextFormatTest, ParseFloatWithSuffix) {
905 std::string parse_string =
"optional_float: 1.0f\n";
907 io::ArrayInputStream input_stream(parse_string.data(), parse_string.size());
915 TEST_F(TextFormatTest, ParseShortRepeatedForm) {
918 "repeated_int32: 1\n"
919 "repeated_int32: [456, 789]\n"
920 "repeated_nested_enum: [ FOO ,BAR, # comment\n"
924 "repeated_string: [ \"foo\", 'bar' ]\n"
926 "repeated_nested_message: [ { bb: 1 }, { bb : 2 }]\n"
928 "RepeatedGroup [{ a: 3 },{ a: 4 }]\n";
932 ASSERT_EQ(3, proto_.repeated_int32_size());
934 EXPECT_EQ(456, proto_.repeated_int32(1));
935 EXPECT_EQ(789, proto_.repeated_int32(2));
937 ASSERT_EQ(3, proto_.repeated_nested_enum_size());
938 EXPECT_EQ(unittest::TestAllTypes::FOO, proto_.repeated_nested_enum(0));
939 EXPECT_EQ(unittest::TestAllTypes::BAR, proto_.repeated_nested_enum(1));
940 EXPECT_EQ(unittest::TestAllTypes::BAZ, proto_.repeated_nested_enum(2));
942 ASSERT_EQ(2, proto_.repeated_string_size());
943 EXPECT_EQ(
"foo", proto_.repeated_string(0));
944 EXPECT_EQ(
"bar", proto_.repeated_string(1));
946 ASSERT_EQ(2, proto_.repeated_nested_message_size());
947 EXPECT_EQ(1, proto_.repeated_nested_message(0).bb());
948 EXPECT_EQ(2, proto_.repeated_nested_message(1).bb());
950 ASSERT_EQ(2, proto_.repeatedgroup_size());
951 EXPECT_EQ(3, proto_.repeatedgroup(0).a());
952 EXPECT_EQ(4, proto_.repeatedgroup(1).a());
955 TEST_F(TextFormatTest, ParseShortRepeatedWithTrailingComma) {
956 std::string parse_string =
"repeated_int32: [456,]\n";
958 parse_string =
"repeated_nested_enum: [ FOO , ]";
960 parse_string =
"repeated_string: [ \"foo\", ]";
962 parse_string =
"repeated_nested_message: [ { bb: 1 }, ]";
964 parse_string =
"RepeatedGroup [{ a: 3 },]\n";
967 TEST_F(TextFormatTest, ParseShortRepeatedEmpty) {
969 "repeated_int32: []\n"
970 "repeated_nested_enum: []\n"
971 "repeated_string: []\n"
972 "repeated_nested_message: []\n"
973 "RepeatedGroup []\n";
977 EXPECT_EQ(0, proto_.repeated_int32_size());
978 EXPECT_EQ(0, proto_.repeated_nested_enum_size());
979 EXPECT_EQ(0, proto_.repeated_string_size());
980 EXPECT_EQ(0, proto_.repeated_nested_message_size());
981 EXPECT_EQ(0, proto_.repeatedgroup_size());
984 TEST_F(TextFormatTest, ParseShortRepeatedConcatenatedWithEmpty) {
987 "repeated_int32: []\n"
988 "repeated_nested_enum: []\n"
989 "repeated_string: []\n"
990 "repeated_nested_message: []\n"
993 "repeated_int32: 1\n"
994 "repeated_int32: [456, 789]\n"
995 "repeated_nested_enum: [ FOO ,BAR, # comment\n"
999 "repeated_string: [ \"foo\", 'bar' ]\n"
1001 "repeated_nested_message: [ { bb: 1 }, { bb : 2 }]\n"
1003 "RepeatedGroup [{ a: 3 },{ a: 4 }]\n"
1005 "repeated_int32: []\n"
1006 "repeated_nested_enum: []\n"
1007 "repeated_string: []\n"
1008 "repeated_nested_message: []\n"
1009 "RepeatedGroup []\n";
1013 ASSERT_EQ(3, proto_.repeated_int32_size());
1015 EXPECT_EQ(456, proto_.repeated_int32(1));
1016 EXPECT_EQ(789, proto_.repeated_int32(2));
1018 ASSERT_EQ(3, proto_.repeated_nested_enum_size());
1019 EXPECT_EQ(unittest::TestAllTypes::FOO, proto_.repeated_nested_enum(0));
1020 EXPECT_EQ(unittest::TestAllTypes::BAR, proto_.repeated_nested_enum(1));
1021 EXPECT_EQ(unittest::TestAllTypes::BAZ, proto_.repeated_nested_enum(2));
1023 ASSERT_EQ(2, proto_.repeated_string_size());
1024 EXPECT_EQ(
"foo", proto_.repeated_string(0));
1025 EXPECT_EQ(
"bar", proto_.repeated_string(1));
1027 ASSERT_EQ(2, proto_.repeated_nested_message_size());
1028 EXPECT_EQ(1, proto_.repeated_nested_message(0).bb());
1029 EXPECT_EQ(2, proto_.repeated_nested_message(1).bb());
1031 ASSERT_EQ(2, proto_.repeatedgroup_size());
1032 EXPECT_EQ(3, proto_.repeatedgroup(0).a());
1033 EXPECT_EQ(4, proto_.repeatedgroup(1).a());
1037 TEST_F(TextFormatTest, Comments) {
1041 "optional_int32: 1 # a comment\n"
1042 "optional_int64: 2 # another comment";
1044 io::ArrayInputStream input_stream(parse_string.data(), parse_string.size());
1053 TEST_F(TextFormatTest, OptionalColon) {
1057 std::string parse_string =
"optional_nested_message: { bb: 1}\n";
1059 io::ArrayInputStream input_stream(parse_string.data(), parse_string.size());
1064 EXPECT_TRUE(proto_.has_optional_nested_message());
1065 EXPECT_EQ(1, proto_.optional_nested_message().bb());
1076 TEST_F(TextFormatTest, PrintExotic) {
1077 unittest::TestAllTypes
message;
1081 message.add_repeated_double(123.456);
1082 message.add_repeated_double(1.23e21);
1083 message.add_repeated_double(1.23e-18);
1084 message.add_repeated_double(std::numeric_limits<double>::infinity());
1085 message.add_repeated_double(-std::numeric_limits<double>::infinity());
1086 message.add_repeated_double(std::numeric_limits<double>::quiet_NaN());
1087 message.add_repeated_double(-std::numeric_limits<double>::quiet_NaN());
1088 message.add_repeated_double(std::numeric_limits<double>::signaling_NaN());
1089 message.add_repeated_double(-std::numeric_limits<double>::signaling_NaN());
1104 "repeated_int64: -9223372036854775808\n"
1105 "repeated_uint64: 18446744073709551615\n"
1106 "repeated_double: 123.456\n"
1107 "repeated_double: 1.23e+21\n"
1108 "repeated_double: 1.23e-18\n"
1109 "repeated_double: inf\n"
1110 "repeated_double: -inf\n"
1111 "repeated_double: nan\n"
1112 "repeated_double: nan\n"
1113 "repeated_double: nan\n"
1114 "repeated_double: nan\n"
1116 "\"\\000\\001\\007\\010\\014\\n\\r\\t\\013\\\\\\'\\\"\"\n",
1120 TEST_F(TextFormatTest, PrintFloatPrecision) {
1121 unittest::TestAllTypes
message;
1123 message.add_repeated_float(1.0);
1124 message.add_repeated_float(1.2);
1125 message.add_repeated_float(1.23);
1126 message.add_repeated_float(1.234);
1127 message.add_repeated_float(1.2345);
1128 message.add_repeated_float(1.23456);
1129 message.add_repeated_float(1.2e10);
1130 message.add_repeated_float(1.23e10);
1131 message.add_repeated_float(1.234e10);
1132 message.add_repeated_float(1.2345e10);
1133 message.add_repeated_float(1.23456e10);
1134 message.add_repeated_double(1.2);
1135 message.add_repeated_double(1.23);
1136 message.add_repeated_double(1.234);
1137 message.add_repeated_double(1.2345);
1138 message.add_repeated_double(1.23456);
1139 message.add_repeated_double(1.234567);
1140 message.add_repeated_double(1.2345678);
1141 message.add_repeated_double(1.23456789);
1142 message.add_repeated_double(1.234567898);
1143 message.add_repeated_double(1.2345678987);
1144 message.add_repeated_double(1.23456789876);
1145 message.add_repeated_double(1.234567898765);
1146 message.add_repeated_double(1.2345678987654);
1147 message.add_repeated_double(1.23456789876543);
1148 message.add_repeated_double(1.2e100);
1149 message.add_repeated_double(1.23e100);
1150 message.add_repeated_double(1.234e100);
1151 message.add_repeated_double(1.2345e100);
1152 message.add_repeated_double(1.23456e100);
1153 message.add_repeated_double(1.234567e100);
1154 message.add_repeated_double(1.2345678e100);
1155 message.add_repeated_double(1.23456789e100);
1156 message.add_repeated_double(1.234567898e100);
1157 message.add_repeated_double(1.2345678987e100);
1158 message.add_repeated_double(1.23456789876e100);
1159 message.add_repeated_double(1.234567898765e100);
1160 message.add_repeated_double(1.2345678987654e100);
1161 message.add_repeated_double(1.23456789876543e100);
1164 "repeated_float: 1\n"
1165 "repeated_float: 1.2\n"
1166 "repeated_float: 1.23\n"
1167 "repeated_float: 1.234\n"
1168 "repeated_float: 1.2345\n"
1169 "repeated_float: 1.23456\n"
1170 "repeated_float: 1.2e+10\n"
1171 "repeated_float: 1.23e+10\n"
1172 "repeated_float: 1.234e+10\n"
1173 "repeated_float: 1.2345e+10\n"
1174 "repeated_float: 1.23456e+10\n"
1175 "repeated_double: 1.2\n"
1176 "repeated_double: 1.23\n"
1177 "repeated_double: 1.234\n"
1178 "repeated_double: 1.2345\n"
1179 "repeated_double: 1.23456\n"
1180 "repeated_double: 1.234567\n"
1181 "repeated_double: 1.2345678\n"
1182 "repeated_double: 1.23456789\n"
1183 "repeated_double: 1.234567898\n"
1184 "repeated_double: 1.2345678987\n"
1185 "repeated_double: 1.23456789876\n"
1186 "repeated_double: 1.234567898765\n"
1187 "repeated_double: 1.2345678987654\n"
1188 "repeated_double: 1.23456789876543\n"
1189 "repeated_double: 1.2e+100\n"
1190 "repeated_double: 1.23e+100\n"
1191 "repeated_double: 1.234e+100\n"
1192 "repeated_double: 1.2345e+100\n"
1193 "repeated_double: 1.23456e+100\n"
1194 "repeated_double: 1.234567e+100\n"
1195 "repeated_double: 1.2345678e+100\n"
1196 "repeated_double: 1.23456789e+100\n"
1197 "repeated_double: 1.234567898e+100\n"
1198 "repeated_double: 1.2345678987e+100\n"
1199 "repeated_double: 1.23456789876e+100\n"
1200 "repeated_double: 1.234567898765e+100\n"
1201 "repeated_double: 1.2345678987654e+100\n"
1202 "repeated_double: 1.23456789876543e+100\n",
1206 TEST_F(TextFormatTest, AllowPartial) {
1207 unittest::TestRequired
message;
1208 TextFormat::Parser
parser;
1209 parser.AllowPartialMessage(
true);
1216 TEST_F(TextFormatTest, ParseExotic) {
1217 unittest::TestAllTypes
message;
1219 "repeated_int32: -1\n"
1220 "repeated_int32: -2147483648\n"
1221 "repeated_int64: -1\n"
1222 "repeated_int64: -9223372036854775808\n"
1223 "repeated_uint32: 4294967295\n"
1224 "repeated_uint32: 2147483648\n"
1225 "repeated_uint64: 18446744073709551615\n"
1226 "repeated_uint64: 9223372036854775808\n"
1227 "repeated_double: 123.0\n"
1228 "repeated_double: 123.5\n"
1229 "repeated_double: 0.125\n"
1230 "repeated_double: 1.23E17\n"
1231 "repeated_double: 1.235E+22\n"
1232 "repeated_double: 1.235e-18\n"
1233 "repeated_double: 123.456789\n"
1234 "repeated_double: inf\n"
1235 "repeated_double: Infinity\n"
1236 "repeated_double: -inf\n"
1237 "repeated_double: -Infinity\n"
1238 "repeated_double: nan\n"
1239 "repeated_double: NaN\n"
1240 "repeated_string: \"\\000\\001\\a\\b\\f\\n\\r\\t\\v\\\\\\'\\\"\"\n",
1268 std::numeric_limits<double>::infinity());
1270 std::numeric_limits<double>::infinity());
1272 -std::numeric_limits<double>::infinity());
1274 -std::numeric_limits<double>::infinity());
1286 "repeated_float: -3.4028235e+38\n"
1287 "repeated_float: 3.402823567797337e+38\n"
1288 "repeated_float: -3.402823567797337e+38\n",
1292 EXPECT_EQ(
message.repeated_float(2), std::numeric_limits<float>::infinity());
1293 EXPECT_EQ(
message.repeated_float(3), -std::numeric_limits<float>::infinity());
1297 TEST_F(TextFormatTest, PrintFieldsInIndexOrder) {
1298 protobuf_unittest::TestFieldOrderings
message;
1307 protobuf_unittest::TestExtensionOrderings2::test_ext_orderings2)
1308 ->set_my_string(
"ext_str2");
1312 protobuf_unittest::TestExtensionOrderings1::test_ext_orderings1)
1313 ->set_my_string(
"ext_str1");
1316 .MutableExtension(protobuf_unittest::TestExtensionOrderings2::
1317 TestExtensionOrderings3::test_ext_orderings3)
1318 ->set_my_string(
"ext_str3");
1320 *
message.MutableExtension(protobuf_unittest::my_extension_string) =
"ext_str0";
1342 "my_int: 12345\nmy_string: "
1343 "\"str\"\n[protobuf_unittest.TestExtensionOrderings2.test_ext_orderings2] "
1345 "\"ext_str2\"\n}\n[protobuf_unittest.TestExtensionOrderings1.test_ext_"
1346 "orderings1] {\n my_string: "
1347 "\"ext_str1\"\n}\n[protobuf_unittest.TestExtensionOrderings2."
1348 "TestExtensionOrderings3.test_ext_orderings3] {\n my_string: "
1349 "\"ext_str3\"\n}\n[protobuf_unittest.my_extension_string]: "
1350 "\"ext_str0\"\nmy_float: 0.999\n",
1368 printer.SetPrintMessageFieldsInIndexOrder(
true);
1371 "my_string: \"str\"\nmy_int: 12345\nmy_float: "
1372 "0.999\n[protobuf_unittest.TestExtensionOrderings2.test_ext_orderings2] "
1374 "\"ext_str2\"\n}\n[protobuf_unittest.TestExtensionOrderings1.test_ext_"
1375 "orderings1] {\n my_string: "
1376 "\"ext_str1\"\n}\n[protobuf_unittest.TestExtensionOrderings2."
1377 "TestExtensionOrderings3.test_ext_orderings3] {\n my_string: "
1378 "\"ext_str3\"\n}\n[protobuf_unittest.my_extension_string]: \"ext_str0\"\n",
1385 int line,
int col) {
1386 std::unique_ptr<unittest::TestAllTypes> proto(
new unittest::TestAllTypes);
1396 int line,
int col,
Message* proto,
bool expected_result) {
1402 error_collector.
text_);
1418 int start_column,
int end_line,
int end_column) {
1454 TEST_F(TextFormatParserTest, ParseInfoTreeBuilding) {
1455 std::unique_ptr<unittest::TestAllTypes>
message(
new unittest::TestAllTypes);
1459 "optional_int32: 1\n"
1460 "optional_int64: 2\n"
1461 " optional_double: 2.4\n"
1462 "repeated_int32: 5\n"
1463 "repeated_int32: 10\n"
1464 "optional_nested_message <\n"
1467 "repeated_nested_message <\n"
1470 "repeated_nested_message <\n"
1475 ExpectSuccessAndTree(stringData,
message.get(), &tree);
1478 ExpectLocation(&tree, d,
"optional_int32", -1, 0, 0, 0, 17);
1479 ExpectLocation(&tree, d,
"optional_int64", -1, 1, 0, 1, 17);
1480 ExpectLocation(&tree, d,
"optional_double", -1, 2, 2, 2, 22);
1482 ExpectLocation(&tree, d,
"repeated_int32", 0, 3, 0, 3, 17);
1483 ExpectLocation(&tree, d,
"repeated_int32", 1, 4, 0, 4, 18);
1485 ExpectLocation(&tree, d,
"optional_nested_message", -1, 5, 0, 7, 1);
1486 ExpectLocation(&tree, d,
"repeated_nested_message", 0, 8, 0, 10, 1);
1487 ExpectLocation(&tree, d,
"repeated_nested_message", 1, 11, 0, 13, 1);
1491 ExpectLocation(&tree, d,
"repeated_int64", 0, -1, -1, -1, -1);
1492 ExpectLocation(&tree, d,
"repeated_int32", 6, -1, -1, -1, -1);
1493 ExpectLocation(&tree, d,
"some_unknown_field", -1, -1, -1, -1, -1);
1497 d->FindFieldByName(
"optional_nested_message");
1501 ExpectLocation(nested_tree, nested_field->
message_type(),
"bb", -1, 6, 2, 6,
1505 nested_field = d->FindFieldByName(
"repeated_nested_message");
1507 ExpectLocation(nested_tree, nested_field->
message_type(),
"bb", -1, 9, 2, 9,
1511 ExpectLocation(nested_tree, nested_field->
message_type(),
"bb", -1, 12, 2, 12,
1518 EXPECT_EQ(
nullptr, unknown_nested_tree);
1521 TEST_F(TextFormatParserTest, ParseFieldValueFromString) {
1522 std::unique_ptr<unittest::TestAllTypes>
message(
new unittest::TestAllTypes);
1525 #define EXPECT_FIELD(name, value, valuestring) \
1526 EXPECT_TRUE(TextFormat::ParseFieldValueFromString( \
1527 valuestring, d->FindFieldByName("optional_" #name), message.get())); \
1528 EXPECT_EQ(value, message->optional_##name()); \
1529 EXPECT_TRUE(message->has_optional_##name());
1531 #define EXPECT_BOOL_FIELD(name, value, valuestring) \
1532 EXPECT_TRUE(TextFormat::ParseFieldValueFromString( \
1533 valuestring, d->FindFieldByName("optional_" #name), message.get())); \
1534 EXPECT_TRUE(message->optional_##name() == value); \
1535 EXPECT_TRUE(message->has_optional_##name());
1537 #define EXPECT_FLOAT_FIELD(name, value, valuestring) \
1538 EXPECT_TRUE(TextFormat::ParseFieldValueFromString( \
1539 valuestring, d->FindFieldByName("optional_" #name), message.get())); \
1540 EXPECT_FLOAT_EQ(value, message->optional_##name()); \
1541 EXPECT_TRUE(message->has_optional_##name());
1543 #define EXPECT_DOUBLE_FIELD(name, value, valuestring) \
1544 EXPECT_TRUE(TextFormat::ParseFieldValueFromString( \
1545 valuestring, d->FindFieldByName("optional_" #name), message.get())); \
1546 EXPECT_DOUBLE_EQ(value, message->optional_##name()); \
1547 EXPECT_TRUE(message->has_optional_##name());
1549 #define EXPECT_INVALID(name, valuestring) \
1550 EXPECT_FALSE(TextFormat::ParseFieldValueFromString( \
1551 valuestring, d->FindFieldByName("optional_" #name), message.get()));
1587 EXPECT_FIELD(fixed64, 0x1234567812345678ULL,
"0x1234567812345678");
1634 EXPECT_FIELD(nested_enum, unittest::TestAllTypes::BAR,
"BAR");
1636 StrCat(unittest::TestAllTypes::BAZ));
1641 "<bb:12>", d->FindFieldByName(
"optional_nested_message"),
message.get()));
1647 #undef EXPECT_BOOL_FIELD
1648 #undef EXPECT_FLOAT_FIELD
1649 #undef EXPECT_DOUBLE_FIELD
1650 #undef EXPECT_INVALID
1653 TEST_F(TextFormatParserTest, InvalidToken) {
1654 ExpectFailure(
"optional_bool: true\n-5\n",
"Expected identifier, got: -", 2,
1657 ExpectFailure(
"optional_bool: true!\n",
"Expected identifier, got: !", 1, 20);
1658 ExpectFailure(
"\"some string\"",
"Expected identifier, got: \"some string\"",
1662 TEST_F(TextFormatParserTest, InvalidFieldName) {
1664 "invalid_field: somevalue\n",
1665 "Message type \"protobuf_unittest.TestAllTypes\" has no field named "
1666 "\"invalid_field\".",
1670 TEST_F(TextFormatParserTest, InvalidCapitalization) {
1673 "optionalgroup {\na: 15\n}\n",
1674 "Message type \"protobuf_unittest.TestAllTypes\" has no field named "
1675 "\"optionalgroup\".",
1678 "OPTIONALgroup {\na: 15\n}\n",
1679 "Message type \"protobuf_unittest.TestAllTypes\" has no field named "
1680 "\"OPTIONALgroup\".",
1683 "Optional_Double: 10.0\n",
1684 "Message type \"protobuf_unittest.TestAllTypes\" has no field named "
1685 "\"Optional_Double\".",
1689 TEST_F(TextFormatParserTest, AllowIgnoreCapitalizationError) {
1690 TextFormat::Parser
parser;
1691 protobuf_unittest::TestAllTypes proto;
1698 parser.AllowCaseInsensitiveField(
true);
1700 EXPECT_EQ(10.0, proto.optional_double());
1702 EXPECT_EQ(15, proto.optionalgroup().a());
1705 TEST_F(TextFormatParserTest, InvalidFieldValues) {
1707 ExpectFailure(
"optional_double: \"hello\"\n",
1708 "Expected double, got: \"hello\"", 1, 18);
1709 ExpectFailure(
"optional_double: true\n",
"Expected double, got: true", 1, 18);
1710 ExpectFailure(
"optional_double: !\n",
"Expected double, got: !", 1, 18);
1711 ExpectFailure(
"optional_double {\n \n}\n",
"Expected \":\", found \"{\".", 1,
1715 ExpectFailure(
"optional_int32: \"hello\"\n",
1716 "Expected integer, got: \"hello\"", 1, 17);
1717 ExpectFailure(
"optional_int32: true\n",
"Expected integer, got: true", 1, 17);
1718 ExpectFailure(
"optional_int32: 4.5\n",
"Expected integer, got: 4.5", 1, 17);
1719 ExpectFailure(
"optional_int32: !\n",
"Expected integer, got: !", 1, 17);
1720 ExpectFailure(
"optional_int32 {\n \n}\n",
"Expected \":\", found \"{\".", 1,
1722 ExpectFailure(
"optional_int32: 0x80000000\n",
1723 "Integer out of range (0x80000000)", 1, 17);
1724 ExpectFailure(
"optional_int64: 0x8000000000000000\n",
1725 "Integer out of range (0x8000000000000000)", 1, 17);
1726 ExpectFailure(
"optional_int32: -0x80000001\n",
1727 "Integer out of range (0x80000001)", 1, 18);
1728 ExpectFailure(
"optional_int64: -0x8000000000000001\n",
1729 "Integer out of range (0x8000000000000001)", 1, 18);
1732 ExpectFailure(
"optional_uint64: \"hello\"\n",
1733 "Expected integer, got: \"hello\"", 1, 18);
1734 ExpectFailure(
"optional_uint64: true\n",
"Expected integer, got: true", 1,
1736 ExpectFailure(
"optional_uint64: 4.5\n",
"Expected integer, got: 4.5", 1, 18);
1737 ExpectFailure(
"optional_uint64: -5\n",
"Expected integer, got: -", 1, 18);
1738 ExpectFailure(
"optional_uint64: !\n",
"Expected integer, got: !", 1, 18);
1739 ExpectFailure(
"optional_uint64 {\n \n}\n",
"Expected \":\", found \"{\".", 1,
1741 ExpectFailure(
"optional_uint32: 0x100000000\n",
1742 "Integer out of range (0x100000000)", 1, 18);
1743 ExpectFailure(
"optional_uint64: 0x10000000000000000\n",
1744 "Integer out of range (0x10000000000000000)", 1, 18);
1747 ExpectFailure(
"optional_bool: \"hello\"\n",
1748 "Expected identifier, got: \"hello\"", 1, 16);
1749 ExpectFailure(
"optional_bool: 5\n",
"Integer out of range (5)", 1, 16);
1750 ExpectFailure(
"optional_bool: -7.5\n",
"Expected identifier, got: -", 1, 16);
1751 ExpectFailure(
"optional_bool: !\n",
"Expected identifier, got: !", 1, 16);
1754 "optional_bool: meh\n",
1755 "Invalid value for boolean field \"optional_bool\". Value: \"meh\".", 2,
1758 ExpectFailure(
"optional_bool {\n \n}\n",
"Expected \":\", found \"{\".", 1,
1762 ExpectFailure(
"optional_string: true\n",
"Expected string, got: true", 1, 18);
1763 ExpectFailure(
"optional_string: 5\n",
"Expected string, got: 5", 1, 18);
1764 ExpectFailure(
"optional_string: -7.5\n",
"Expected string, got: -", 1, 18);
1765 ExpectFailure(
"optional_string: !\n",
"Expected string, got: !", 1, 18);
1766 ExpectFailure(
"optional_string {\n \n}\n",
"Expected \":\", found \"{\".", 1,
1770 ExpectFailure(
"optional_nested_enum: \"hello\"\n",
1771 "Expected integer or identifier, got: \"hello\"", 1, 23);
1774 ExpectFailure(
"optional_nested_enum: 5\n",
1775 "Unknown enumeration value of \"5\" for field "
1776 "\"optional_nested_enum\".",
1780 ExpectFailure(
"optional_nested_enum: -7.5\n",
"Expected integer, got: 7.5", 1,
1782 ExpectFailure(
"optional_nested_enum: !\n",
1783 "Expected integer or identifier, got: !", 1, 23);
1785 ExpectFailure(
"optional_nested_enum: grah\n",
1786 "Unknown enumeration value of \"grah\" for field "
1787 "\"optional_nested_enum\".",
1790 ExpectFailure(
"optional_nested_enum {\n \n}\n",
1791 "Expected \":\", found \"{\".", 1, 22);
1794 TEST_F(TextFormatParserTest, MessageDelimiters) {
1796 ExpectFailure(
"OptionalGroup <\n \n}\n",
"Expected \">\", found \"}\".", 3,
1800 ExpectFailure(
"OptionalGroup [\n \n]\n",
"Expected \"{\", found \"[\".", 1,
1804 ExpectFailure(
"optional_nested_message {\n \nbb: 118\n",
1805 "Expected identifier, got: ", 4, 1);
1808 TEST_F(TextFormatParserTest, UnknownExtension) {
1810 ExpectFailure(
"[blahblah]: 123",
1811 "Extension \"blahblah\" is not defined or is not an "
1812 "extension of \"protobuf_unittest.TestAllTypes\".",
1816 TEST_F(TextFormatParserTest, MissingRequired) {
1817 unittest::TestRequired
message;
1818 ExpectFailure(
"a: 1",
"Message missing required fields: b, c", 0, 1,
1822 TEST_F(TextFormatParserTest, ParseDuplicateRequired) {
1823 unittest::TestRequired
message;
1824 ExpectFailure(
"a: 1 b: 2 c: 3 a: 1",
1825 "Non-repeated field \"a\" is specified multiple times.", 1, 17,
1829 TEST_F(TextFormatParserTest, ParseDuplicateOptional) {
1830 unittest::ForeignMessage
message;
1831 ExpectFailure(
"c: 1 c: 2",
1832 "Non-repeated field \"c\" is specified multiple times.", 1, 7,
1836 TEST_F(TextFormatParserTest, MergeDuplicateRequired) {
1837 unittest::TestRequired
message;
1838 TextFormat::Parser
parser;
1843 TEST_F(TextFormatParserTest, MergeDuplicateOptional) {
1844 unittest::ForeignMessage
message;
1845 TextFormat::Parser
parser;
1850 TEST_F(TextFormatParserTest, ExplicitDelimiters) {
1851 unittest::TestRequired
message;
1858 TEST_F(TextFormatParserTest, PrintErrorsToStderr) {
1859 std::vector<std::string>
errors;
1862 ScopedMemoryLog
log;
1863 unittest::TestAllTypes proto;
1870 "Error parsing text-format protobuf_unittest.TestAllTypes: "
1871 "1:14: Message type \"protobuf_unittest.TestAllTypes\" has no field "
1872 "named \"no_such_field\".",
1876 TEST_F(TextFormatParserTest, FailsOnTokenizationError) {
1877 std::vector<std::string>
errors;
1880 ScopedMemoryLog
log;
1881 unittest::TestAllTypes proto;
1888 "Error parsing text-format protobuf_unittest.TestAllTypes: "
1889 "1:1: Invalid control characters encountered in text.",
1893 TEST_F(TextFormatParserTest, ParseDeprecatedField) {
1894 unittest::TestDeprecatedFields
message;
1895 ExpectMessage(
"deprecated_int32: 42",
1896 "WARNING:text format contains deprecated field "
1897 "\"deprecated_int32\"",
1901 TEST_F(TextFormatParserTest, SetRecursionLimit) {
1902 const char*
format =
"child: { $0 }";
1906 unittest::NestedTestAllTypes
message;
1910 parser_.SetRecursionLimit(100);
1911 ExpectMessage(
input,
1912 "Message is too deep, the parser exceeded the configured "
1913 "recursion limit of 100.",
1916 parser_.SetRecursionLimit(101);
1921 const char*
format =
"[$0]";
1926 parser_.AllowUnknownField(
true);
1927 parser_.SetRecursionLimit(100);
1929 unittest::NestedTestAllTypes
message;
1930 ExpectSuccessAndTree(not_deep_input, &
message,
nullptr);
1936 "WARNING:Message type \"protobuf_unittest.NestedTestAllTypes\" has no "
1937 "field named \"unknown_nested_array\".\n1:123: Message is too deep, the "
1938 "parser exceeded the configured recursion limit of 100.",
1941 parser_.SetRecursionLimit(101);
1942 ExpectSuccessAndTree(deep_input, &
message,
nullptr);
1946 const char*
format =
"unknown_child: { $0 }";
1950 parser_.AllowUnknownField(
true);
1951 parser_.SetRecursionLimit(100);
1953 unittest::NestedTestAllTypes
message;
1959 "WARNING:Message type \"protobuf_unittest.NestedTestAllTypes\" has no "
1960 "field named \"unknown_child\".\n1:1716: Message is too deep, the parser "
1961 "exceeded the configured recursion limit of 100.",
1964 parser_.SetRecursionLimit(101);
1971 "[type.googleapis.com/protobuf_unittest.TestAllTypes] \t : \t {\n"
1972 " optional_int32: 321\n"
1973 " optional_string: \"teststr0\"\n"
1983 "[type.googleapis.com/protobuf_unittest.TestAllTypes] {\n"
1984 " optional_int32: 321\n"
1985 " optional_string: \"teststr0\"\n"
1990 unittest::TestAllExtensions proto;
1992 "[protobuf_unittest.optional_int32_extension] : \t 101\n"
1993 "[protobuf_unittest.optional_int64_extension] \t : 102\n";
2001 "[protobuf_unittest.optional_int32_extension]: 101\n"
2002 "[protobuf_unittest.optional_int64_extension]: 102\n");
2006 unittest::TestAllTypes proto;
2008 "repeated_int32 : \t 1\n"
2009 "repeated_int32: 2\n"
2010 "repeated_nested_message: {\n"
2013 "repeated_nested_message : \t {\n"
2016 "repeated_nested_message {\n"
2026 "repeated_int32: 1\n"
2027 "repeated_int32: 2\n"
2028 "repeated_nested_message {\n"
2031 "repeated_nested_message {\n"
2034 "repeated_nested_message {\n"
2040 protobuf_unittest::TestAllTypes proto;
2042 parser.AllowUnknownField(
true);
2044 parser.ParseFromString(
"optional_int32: 321\n"
2045 "unknown_field1 : \t 12345\n"
2046 "[somewhere.unknown_extension1] {\n"
2047 " unknown_field2 \t : 12345\n"
2049 "[somewhere.unknown_extension2] : \t {\n"
2050 " unknown_field3 \t : 12345\n"
2051 " [somewhere.unknown_extension3] \t : {\n"
2052 " unknown_field4: 10\n"
2054 " [somewhere.unknown_extension4] \t {\n"
2070 " [protobuf_unittest.TestMessageSetExtension1] {\n"
2073 " [protobuf_unittest.TestMessageSetExtension2] {\n"
2078 TEST_F(TextFormatMessageSetTest, Serialize) {
2079 protobuf_unittest::TestMessageSetContainer proto;
2080 protobuf_unittest::TestMessageSetExtension1* item_a =
2081 proto.mutable_message_set()->MutableExtension(
2082 protobuf_unittest::TestMessageSetExtension1::message_set_extension);
2084 protobuf_unittest::TestMessageSetExtension2* item_b =
2085 proto.mutable_message_set()->MutableExtension(
2086 protobuf_unittest::TestMessageSetExtension2::message_set_extension);
2087 item_b->set_str(
"foo");
2088 EXPECT_EQ(proto_debug_string_, proto.DebugString());
2091 TEST_F(TextFormatMessageSetTest, Deserialize) {
2092 protobuf_unittest::TestMessageSetContainer proto;
2098 protobuf_unittest::TestMessageSetExtension1::message_set_extension)
2104 protobuf_unittest::TestMessageSetExtension2::message_set_extension)
2109 proto.message_set().GetReflection()->ListFields(proto.message_set(),
2114 TEST(TextFormatUnknownFieldTest, TestUnknownField) {
2115 protobuf_unittest::TestAllTypes proto;
2116 TextFormat::Parser
parser;
2121 parser.AllowUnknownField(
true);
2132 parser.ParseFromString(
"unknown_field: \"string value\"", &proto));
2137 parser.ParseFromString(
"unknown_field1: TYPE_STRING\n"
2138 "unknown_field2: 12345",
2142 parser.ParseFromString(
"unknown_message1: {}\n"
2143 "unknown_message2 {\n"
2144 " unknown_field: 12345\n"
2146 "unknown_message3 <\n"
2147 " unknown_nested_message {\n"
2148 " unknown_field: 12345\n"
2156 parser.ParseFromString(
"[somewhere.unknown_extension1]: 12345\n"
2157 "[somewhere.unknown_extension2] {\n"
2158 " unknown_field: 12345\n"
2163 parser.ParseFromString(
"optional_int32: 1\n"
2164 "unknown_field: 12345\n"
2165 "optional_string: \"string\"\n"
2166 "unknown_message { unknown: 0 }\n"
2167 "optional_nested_message { bb: 2 }",
2170 EXPECT_EQ(
"string", proto.optional_string());
2171 EXPECT_EQ(2, proto.optional_nested_message().bb());
2178 parser.ParseFromString(
"[test.extension1] <\n"
2179 " unknown_nested_message <\n"
2180 " [test.extension2] <\n"
2181 " unknown_field: 12345\n"
2187 parser.ParseFromString(
"[test.extension1] {\n"
2188 " unknown_nested_message {\n"
2189 " [test.extension2] {\n"
2190 " unknown_field: 12345\n"
2196 parser.ParseFromString(
"[test.extension1] <\n"
2197 " some_unknown_fields: <\n"
2198 " unknown_field: 12345\n"
2203 parser.ParseFromString(
"[test.extension1] {\n"
2204 " some_unknown_fields: {\n"
2205 " unknown_field: 12345\n"
2218 TEST(TextFormatUnknownFieldTest, TestAnyInUnknownField) {
2219 protobuf_unittest::TestAllTypes proto;
2220 TextFormat::Parser
parser;
2221 parser.AllowUnknownField(
true);
2223 parser.ParseFromString(
"unknown {\n"
2224 " [type.googleapis.com/foo.bar] {\n"
2230 TEST(TextFormatUnknownFieldTest, TestUnknownExtension) {
2231 protobuf_unittest::TestAllTypes proto;
2232 TextFormat::Parser
parser;
2234 "[test.extension1] {\n"
2235 " some_unknown_fields: {\n"
2236 " unknown_field: 12345\n"
2242 parser.AllowUnknownField(
true);
2245 parser.AllowUnknownField(
false);
2247 parser.AllowUnknownExtension(
true);
2264 protobuf_unittest::TestAllTypes proto;
2265 proto.set_optional_int32(1);
2266 proto.mutable_optional_nested_message()->set_bb(2);
2269 "optional_int32: \t 1\n"
2270 "optional_nested_message {\n"
2273 proto.DebugString());
2276 "optional_int32: \t 1 "
2277 "optional_nested_message { bb: 2 }",
2278 proto.ShortDebugString());
2282 protobuf_unittest::TestAllTypes proto;
2283 proto.mutable_optional_nested_message()->set_bb(2);
2284 proto.add_repeated_int32(3);
2287 "optional_nested_message \t {\n"
2290 "repeated_int32: 3\n",
2291 proto.DebugString());
2294 "optional_nested_message \t { bb: 2 } "
2295 "repeated_int32: 3",
2296 proto.ShortDebugString());
2300 unittest::TestEmptyMessage
message;
2318 unknown_fields->
Clear();
2336 protobuf_unittest::TestAllTypes proto;
2337 proto.set_optional_string(
"teststr");
2338 proto.set_optional_int32(432);
2343 "[type.googleapis.com/protobuf_unittest.TestAllTypes] \t {\n"
2344 " optional_int32: 432\n"
2345 " optional_string: \"teststr\"\n"
2350 "[type.googleapis.com/protobuf_unittest.TestAllTypes]"
2351 " \t { optional_int32: 432 optional_string: \"teststr\" }",
2352 any.ShortDebugString());
2356 unittest::TestAllExtensions proto;
2357 proto.SetExtension(protobuf_unittest::optional_int32_extension, 101);
2358 proto.SetExtension(protobuf_unittest::optional_int64_extension, 102);
2361 "[protobuf_unittest.optional_int32_extension]: \t 101\n"
2362 "[protobuf_unittest.optional_int64_extension]: 102\n",
2363 proto.DebugString());
2367 unittest::TestMap proto;
2368 (*proto.mutable_map_int32_int32())[0] = 1;
2369 (*proto.mutable_map_int64_int64())[2] = 3;
2372 "map_int32_int32 \t {\n key: 0\n value: 1\n}\n"
2373 "map_int64_int64 {\n key: 2\n value: 3\n}\n",
2374 proto.DebugString());
2378 TEST(TextFormatFloatingPointTest, PreservesNegative0) {
2379 proto3_unittest::TestAllTypes in_message;
2380 in_message.set_optional_float(-0.0f);
2381 in_message.set_optional_double(-0.0);
2385 proto3_unittest::TestAllTypes out_message;
2388 EXPECT_EQ(in_message.optional_float(), out_message.optional_float());
2389 EXPECT_EQ(std::signbit(in_message.optional_float()),
2390 std::signbit(out_message.optional_float()));
2391 EXPECT_EQ(in_message.optional_double(), out_message.optional_double());
2392 EXPECT_EQ(std::signbit(in_message.optional_double()),
2393 std::signbit(out_message.optional_double()));
2400 #include <google/protobuf/port_undef.inc>