protobuf/src/google/protobuf/text_format_unittest.cc
Go to the documentation of this file.
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
3 // https://developers.google.com/protocol-buffers/
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the
14 // distribution.
15 // * Neither the name of Google Inc. nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
18 //
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.
30 
31 // Author: jschorr@google.com (Joseph Schorr)
32 // Based on original Protocol Buffers design by
33 // Sanjay Ghemawat, Jeff Dean, and others.
34 
35 #include <google/protobuf/text_format.h>
36 
37 #include <math.h>
38 #include <stdlib.h>
39 
40 #include <atomic>
41 #include <limits>
42 #include <memory>
43 
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>
64 
65 
66 #include <google/protobuf/port_def.inc>
67 
68 namespace google {
69 namespace protobuf {
70 
71 namespace internal {
72 // Controls insertion of DEBUG_STRING_SILENT_MARKER.
73 extern PROTOBUF_EXPORT std::atomic<bool> enable_debug_text_format_marker;
74 } // namespace internal
75 
76 // Can't use an anonymous namespace here due to brokenness of Tru64 compiler.
77 namespace text_format_unittest {
78 
79 // A basic string with different escapable characters for testing.
81  "\"A string with ' characters \n and \r newlines and \t tabs and \001 "
82  "slashes \\ and multiple spaces";
83 
84 // A representation of the above string with all the characters escaped.
86  "\"\\\"A string with \\' characters \\n and \\r newlines "
87  "and \\t tabs and \\001 slashes \\\\ and multiple spaces\"";
88 
89 class TextFormatTest : public testing::Test {
90  public:
91  static void SetUpTestSuite() {
94  "net/proto2/internal/"
95  "testdata/text_format_unittest_data_oneof_implemented.txt"),
98  }
99 
101 
102  protected:
103  // Debug string read from text_format_unittest_data.txt.
105  unittest::TestAllTypes proto_;
106 
107  private:
109 };
111 
112 class TextFormatExtensionsTest : public testing::Test {
113  public:
114  static void SetUpTestSuite() {
116  TestUtil::GetTestDataPath("net/proto2/internal/testdata/"
117  "text_format_unittest_extensions_data.txt"),
120  }
121 
124 
125  protected:
126  // Debug string read from text_format_unittest_data.txt.
128  unittest::TestAllExtensions proto_;
129 
130  private:
132 };
134 
135 TEST_F(TextFormatTest, Basic) {
136  TestUtil::SetAllFields(&proto_);
137  EXPECT_EQ(proto_debug_string_, proto_.DebugString());
138 }
139 
140 TEST_F(TextFormatExtensionsTest, Extensions) {
142  EXPECT_EQ(proto_debug_string_, proto_.DebugString());
143 }
144 
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();
150 
151  EXPECT_EQ(
152  "optional_int32: 1 optional_string: \"hello\" "
153  "optional_nested_message { bb: 2 } "
154  "optional_foreign_message { }",
155  proto_.ShortDebugString());
156 }
157 
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);
168 
169  TextFormat::Printer printer;
170  printer.SetUseShortRepeatedPrimitives(true);
172  EXPECT_TRUE(printer.PrintToString(proto_, &text));
173 
174  EXPECT_EQ(
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",
182  text);
183 
184  // Verify that any existing data in the string is cleared when PrintToString()
185  // is called.
186  text = "just some data here...\n\nblah blah";
187  EXPECT_TRUE(printer.PrintToString(proto_, &text));
188 
189  EXPECT_EQ(
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",
197  text);
198 
199  // Try in single-line mode.
200  printer.SetSingleLineMode(true);
201  EXPECT_TRUE(printer.PrintToString(proto_, &text));
202 
203  EXPECT_EQ(
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] ",
211  text);
212 }
213 
214 
215 TEST_F(TextFormatTest, StringEscape) {
216  // Set the string value to test.
217  proto_.set_optional_string(kEscapeTestString);
218 
219  // Get the DebugString from the proto.
220  std::string debug_string = proto_.DebugString();
221  std::string utf8_debug_string = proto_.Utf8DebugString();
222 
223  // Hardcode a correct value to test against.
224  std::string correct_string =
225  "optional_string: " + kEscapeTestStringEscaped + "\n";
226 
227  // Compare.
228  EXPECT_EQ(correct_string, debug_string);
229  // UTF-8 string is the same as non-UTF-8 because
230  // the protocol buffer contains no UTF-8 text.
231  EXPECT_EQ(correct_string, utf8_debug_string);
232 
233  std::string expected_short_debug_string =
234  "optional_string: " + kEscapeTestStringEscaped;
235  EXPECT_EQ(expected_short_debug_string, proto_.ShortDebugString());
236 }
237 
238 TEST_F(TextFormatTest, Utf8DebugString) {
239  // Set the string value to test.
240  proto_.set_optional_string("\350\260\267\346\255\214");
241  proto_.set_optional_bytes("\350\260\267\346\255\214");
242 
243  // Get the DebugString from the proto.
244  std::string debug_string = proto_.DebugString();
245  std::string utf8_debug_string = proto_.Utf8DebugString();
246 
247  // Hardcode a correct value to test against.
248  std::string correct_utf8_string =
249  "optional_string: "
250  "\"\350\260\267\346\255\214\""
251  "\n"
252  "optional_bytes: "
253  "\"\\350\\260\\267\\346\\255\\214\""
254  "\n";
255  std::string correct_string =
256  "optional_string: "
257  "\"\\350\\260\\267\\346\\255\\214\""
258  "\n"
259  "optional_bytes: "
260  "\"\\350\\260\\267\\346\\255\\214\""
261  "\n";
262 
263  // Compare.
264  EXPECT_EQ(correct_utf8_string, utf8_debug_string);
265  EXPECT_EQ(correct_string, debug_string);
266 }
267 
268 TEST_F(TextFormatTest, PrintUnknownFields) {
269  // Test printing of unknown fields in a message.
270 
271  unittest::TestEmptyMessage message;
272  UnknownFieldSet* unknown_fields = message.mutable_unknown_fields();
273 
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);
279 
280  unknown_fields->AddVarint(8, 1);
281  unknown_fields->AddVarint(8, 2);
282  unknown_fields->AddVarint(8, 3);
283 
284  EXPECT_EQ(
285  "5: 1\n"
286  "5: 0x00000002\n"
287  "5: 0x0000000000000003\n"
288  "5: \"4\"\n"
289  "5 {\n"
290  " 10: 5\n"
291  "}\n"
292  "8: 1\n"
293  "8: 2\n"
294  "8: 3\n",
295  message.DebugString());
296 }
297 
298 TEST_F(TextFormatTest, PrintUnknownFieldsHidden) {
299  // Test printing of unknown fields in a message when suppressed.
300 
301  unittest::OneString message;
302  message.set_data("data");
303  UnknownFieldSet* unknown_fields = message.mutable_unknown_fields();
304 
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);
310 
311  unknown_fields->AddVarint(8, 1);
312  unknown_fields->AddVarint(8, 2);
313  unknown_fields->AddVarint(8, 3);
314 
315  TextFormat::Printer printer;
316  printer.SetHideUnknownFields(true);
318  printer.PrintToString(message, &output);
319 
320  EXPECT_EQ("data: \"data\"\n", output);
321 }
322 
323 TEST_F(TextFormatTest, PrintUnknownMessage) {
324  // Test heuristic printing of messages in an UnknownFieldSet.
325 
326  protobuf_unittest::TestAllTypes message;
327 
328  // Cases which should not be interpreted as sub-messages.
329 
330  // 'a' is a valid FIXED64 tag, so for the string to be parseable as a message
331  // it should be followed by 8 bytes. Since this string only has two
332  // subsequent bytes, it should be treated as a string.
333  message.add_repeated_string("abc");
334 
335  // 'd' happens to be a valid ENDGROUP tag. So,
336  // UnknownFieldSet::MergeFromCodedStream() will successfully parse "def", but
337  // the ConsumedEntireMessage() check should fail.
338  message.add_repeated_string("def");
339 
340  // A zero-length string should never be interpreted as a message even though
341  // it is technically valid as one.
342  message.add_repeated_string("");
343 
344  // Case which should be interpreted as a sub-message.
345 
346  // An actual nested message with content should always be interpreted as a
347  // nested message.
348  message.add_repeated_nested_message()->set_bb(123);
349 
351  message.SerializeToString(&data);
352 
354  UnknownFieldSet unknown_fields;
355  EXPECT_TRUE(unknown_fields.ParseFromString(data));
357  // Field 44 and 48 can be printed in any order.
358  EXPECT_THAT(text, testing::HasSubstr("44: \"abc\"\n"
359  "44: \"def\"\n"
360  "44: \"\"\n"));
362  " 1: 123\n"
363  "}\n"));
364 }
365 
366 TEST_F(TextFormatTest, PrintDeeplyNestedUnknownMessage) {
367  // Create a deeply nested message.
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) {
374  lengths.push_back(
376  kUnknownFieldNumber, internal::WireFormatLite::TYPE_BYTES) +
378  }
379  std::string serialized;
380  {
381  io::StringOutputStream zero_copy_stream(&serialized);
382  io::CodedOutputStream coded_stream(&zero_copy_stream);
383  for (int i = kNestingDepth - 1; i >= 0; --i) {
385  kUnknownFieldNumber,
387  coded_stream.WriteVarint32(lengths[i]);
388  }
389  }
390 
391  // Parse the data and verify that we can print it without overflowing the
392  // stack.
393  unittest::TestEmptyMessage message;
394  ASSERT_TRUE(message.ParseFromString(serialized));
397 }
398 
399 TEST_F(TextFormatTest, PrintMessageWithIndent) {
400  // Test adding an initial indent to printing.
401 
402  protobuf_unittest::TestAllTypes message;
403 
404  message.add_repeated_string("abc");
405  message.add_repeated_string("def");
406  message.add_repeated_nested_message()->set_bb(123);
407 
409  TextFormat::Printer printer;
410  printer.SetInitialIndentLevel(1);
412  EXPECT_EQ(
413  " repeated_string: \"abc\"\n"
414  " repeated_string: \"def\"\n"
415  " repeated_nested_message {\n"
416  " bb: 123\n"
417  " }\n",
418  text);
419 }
420 
421 TEST_F(TextFormatTest, PrintMessageSingleLine) {
422  // Test printing a message on a single line.
423 
424  protobuf_unittest::TestAllTypes message;
425 
426  message.add_repeated_string("abc");
427  message.add_repeated_string("def");
428  message.add_repeated_nested_message()->set_bb(123);
429 
431  TextFormat::Printer printer;
432  printer.SetInitialIndentLevel(1);
433  printer.SetSingleLineMode(true);
434  EXPECT_TRUE(printer.PrintToString(message, &text));
435  EXPECT_EQ(
436  " repeated_string: \"abc\" repeated_string: \"def\" "
437  "repeated_nested_message { bb: 123 } ",
438  text);
439 }
440 
441 TEST_F(TextFormatTest, PrintBufferTooSmall) {
442  // Test printing a message to a buffer that is too small.
443 
444  protobuf_unittest::TestAllTypes message;
445 
446  message.add_repeated_string("abc");
447  message.add_repeated_string("def");
448 
449  char buffer[1] = "";
450  io::ArrayOutputStream output_stream(buffer, 1);
451  EXPECT_FALSE(TextFormat::Print(message, &output_stream));
452  EXPECT_EQ(buffer[0], 'r');
453  EXPECT_EQ(output_stream.ByteCount(), 1);
454 }
455 
456 // A printer that appends 'u' to all unsigned int32.
457 class CustomUInt32FieldValuePrinter : public TextFormat::FieldValuePrinter {
458  public:
459  virtual std::string PrintUInt32(uint32 val) const {
460  return StrCat(FieldValuePrinter::PrintUInt32(val), "u");
461  }
462 };
463 
464 TEST_F(TextFormatTest, DefaultCustomFieldPrinter) {
465  protobuf_unittest::TestAllTypes message;
466 
467  message.set_optional_uint32(42);
468  message.add_repeated_uint32(1);
469  message.add_repeated_uint32(2);
470  message.add_repeated_uint32(3);
471 
472  TextFormat::Printer printer;
473  printer.SetDefaultFieldValuePrinter(new CustomUInt32FieldValuePrinter());
474  // Let's see if that works well together with the repeated primitives:
475  printer.SetUseShortRepeatedPrimitives(true);
477  printer.PrintToString(message, &text);
478  EXPECT_EQ("optional_uint32: 42u\nrepeated_uint32: [1u, 2u, 3u]\n", text);
479 }
480 
481 class CustomInt32FieldValuePrinter : public TextFormat::FieldValuePrinter {
482  public:
483  virtual std::string PrintInt32(int32 val) const {
484  return StrCat("value-is(", FieldValuePrinter::PrintInt32(val), ")");
485  }
486 };
487 
488 TEST_F(TextFormatTest, FieldSpecificCustomPrinter) {
489  protobuf_unittest::TestAllTypes message;
490 
491  message.set_optional_int32(42); // This will be handled by our Printer.
492  message.add_repeated_int32(42); // This will be printed as number.
493 
494  TextFormat::Printer printer;
496  message.GetDescriptor()->FindFieldByName("optional_int32"),
497  new CustomInt32FieldValuePrinter()));
499  printer.PrintToString(message, &text);
500  EXPECT_EQ("optional_int32: value-is(42)\nrepeated_int32: 42\n", text);
501 }
502 
503 TEST_F(TextFormatTest, FieldSpecificCustomPrinterRegisterSameFieldTwice) {
504  protobuf_unittest::TestAllTypes message;
505  TextFormat::Printer printer;
506  const FieldDescriptor* const field =
507  message.GetDescriptor()->FindFieldByName("optional_int32");
508  ASSERT_TRUE(printer.RegisterFieldValuePrinter(
509  field, new CustomInt32FieldValuePrinter()));
510  const TextFormat::FieldValuePrinter* const rejected =
511  new CustomInt32FieldValuePrinter();
512  ASSERT_FALSE(printer.RegisterFieldValuePrinter(field, rejected));
513  delete rejected;
514 }
515 
516 TEST_F(TextFormatTest, ErrorCasesRegisteringFieldValuePrinterShouldFail) {
517  protobuf_unittest::TestAllTypes message;
518  TextFormat::Printer printer;
519  // nullptr printer.
520  EXPECT_FALSE(printer.RegisterFieldValuePrinter(
521  message.GetDescriptor()->FindFieldByName("optional_int32"),
522  static_cast<const TextFormat::FieldValuePrinter*>(nullptr)));
523  EXPECT_FALSE(printer.RegisterFieldValuePrinter(
524  message.GetDescriptor()->FindFieldByName("optional_int32"),
525  static_cast<const TextFormat::FastFieldValuePrinter*>(nullptr)));
526  // Because registration fails, the ownership of this printer is never taken.
527  TextFormat::FieldValuePrinter my_field_printer;
528  // nullptr field
529  EXPECT_FALSE(printer.RegisterFieldValuePrinter(nullptr, &my_field_printer));
530 }
531 
532 class CustomMessageFieldValuePrinter : public TextFormat::FieldValuePrinter {
533  public:
534  virtual std::string PrintInt32(int32 v) const {
535  return StrCat(FieldValuePrinter::PrintInt32(v), " # x",
536  strings::Hex(v));
537  }
538 
539  virtual std::string PrintMessageStart(const Message& message, int field_index,
540  int field_count,
541  bool single_line_mode) const {
542  if (single_line_mode) {
543  return " { ";
544  }
545  return StrCat(" { # ", message.GetDescriptor()->name(), ": ",
546  field_index, "\n");
547  }
548 };
549 
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);
558  TextFormat::Printer printer;
559  CustomMessageFieldValuePrinter my_field_printer;
560  printer.SetDefaultFieldValuePrinter(new CustomMessageFieldValuePrinter());
562  printer.PrintToString(message, &text);
563  EXPECT_EQ(
564  "optional_nested_message { # NestedMessage: -1\n"
565  "}\n"
566  "optional_import_message { # ImportMessage: -1\n"
567  " d: 42 # x2a\n"
568  "}\n"
569  "repeated_nested_message { # NestedMessage: 0\n"
570  "}\n"
571  "repeated_nested_message { # NestedMessage: 1\n"
572  "}\n"
573  "repeated_import_message { # ImportMessage: 0\n"
574  " d: 43 # x2b\n"
575  "}\n"
576  "repeated_import_message { # ImportMessage: 1\n"
577  " d: 44 # x2c\n"
578  "}\n",
579  text);
580 }
581 
584  public:
586  const Message& message, int field_index, int field_count,
587  bool single_line_mode,
588  TextFormat::BaseTextGenerator* generator) const override {
589  if (message.ByteSizeLong() > 0) {
590  generator->PrintString(
591  strings::Substitute("# REDACTED, $0 bytes\n", message.ByteSizeLong()));
592  }
593  return true;
594  }
595 };
596 
597 TEST_F(TextFormatTest, CustomPrinterForMessageContent) {
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);
605  TextFormat::Printer printer;
606  CustomMessageContentFieldValuePrinter my_field_printer;
610  printer.PrintToString(message, &text);
611  EXPECT_EQ(
612  "optional_nested_message {\n"
613  "}\n"
614  "optional_import_message {\n"
615  " # REDACTED, 2 bytes\n"
616  "}\n"
617  "repeated_nested_message {\n"
618  "}\n"
619  "repeated_nested_message {\n"
620  "}\n"
621  "repeated_import_message {\n"
622  " # REDACTED, 2 bytes\n"
623  "}\n"
624  "repeated_import_message {\n"
625  " # REDACTED, 2 bytes\n"
626  "}\n",
627  text);
628 }
629 
630 class CustomMultilineCommentPrinter : public TextFormat::FieldValuePrinter {
631  public:
632  virtual std::string PrintMessageStart(const Message& message, int field_index,
633  int field_count,
634  bool single_line_comment) const {
635  return StrCat(" { # 1\n", " # 2\n");
636  }
637 };
638 
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);
643  TextFormat::Printer printer;
644  CustomMessageFieldValuePrinter my_field_printer;
645  printer.SetDefaultFieldValuePrinter(new CustomMultilineCommentPrinter());
647  printer.PrintToString(message, &text);
648  EXPECT_EQ(
649  "optional_nested_message { # 1\n"
650  " # 2\n"
651  "}\n"
652  "optional_import_message { # 1\n"
653  " # 2\n"
654  " d: 42\n"
655  "}\n",
656  text);
657 }
658 
659 // Achieve effects similar to SetUseShortRepeatedPrimitives for messages, using
660 // RegisterFieldValuePrinter. Use this to test the version of PrintFieldName
661 // that accepts repeated field index and count.
662 class CompactRepeatedFieldPrinter : public TextFormat::FastFieldValuePrinter {
663  public:
664  void PrintFieldName(const Message& message, int field_index, int field_count,
665  const Reflection* reflection,
666  const FieldDescriptor* field,
667  TextFormat::BaseTextGenerator* generator) const override {
668  if (field_index == 0 || field_index == -1) {
669  generator->PrintString(field->name());
670  }
671  }
672  // To prevent compiler complaining about Woverloaded-virtual
673  void PrintFieldName(const Message& message, const Reflection* reflection,
674  const FieldDescriptor* field,
675  TextFormat::BaseTextGenerator* generator) const override {
676  }
678  const Message& message, int field_index, int field_count,
679  bool single_line_mode,
680  TextFormat::BaseTextGenerator* generator) const override {
681  if (field_index == 0 || field_index == -1) {
682  if (single_line_mode) {
683  generator->PrintLiteral(" { ");
684  } else {
685  generator->PrintLiteral(" {\n");
686  }
687  }
688  }
690  const Message& message, int field_index, int field_count,
691  bool single_line_mode,
692  TextFormat::BaseTextGenerator* generator) const override {
693  if (field_index == field_count - 1 || field_index == -1) {
694  if (single_line_mode) {
695  generator->PrintLiteral("} ");
696  } else {
697  generator->PrintLiteral("}\n");
698  }
699  }
700  }
701 };
702 
703 TEST_F(TextFormatTest, CompactRepeatedFieldPrinter) {
704  TextFormat::Printer printer;
706  unittest::TestAllTypes::default_instance()
707  .descriptor()
708  ->FindFieldByNumber(
709  unittest::TestAllTypes::kRepeatedNestedMessageFieldNumber),
710  new CompactRepeatedFieldPrinter));
711 
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);
716 
719  EXPECT_EQ(
720  "repeated_nested_message {\n"
721  " bb: 1\n"
722  " bb: 2\n"
723  " bb: 3\n"
724  "}\n",
725  text);
726 }
727 
728 // Print strings into multiple line, with indentation. Use this to test
729 // BaseTextGenerator::Indent and BaseTextGenerator::Outdent.
730 class MultilineStringPrinter : public TextFormat::FastFieldValuePrinter {
731  public:
732  void PrintString(const std::string& val,
733  TextFormat::BaseTextGenerator* generator) const override {
734  generator->Indent();
735  int last_pos = 0;
736  int newline_pos = val.find('\n');
737  while (newline_pos != std::string::npos) {
738  generator->PrintLiteral("\n");
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);
743  }
744  if (last_pos < val.size()) {
745  generator->PrintLiteral("\n");
747  generator);
748  }
749  generator->Outdent();
750  }
751 };
752 
753 TEST_F(TextFormatTest, MultilineStringPrinter) {
754  TextFormat::Printer printer;
756  unittest::TestAllTypes::default_instance()
757  .descriptor()
758  ->FindFieldByNumber(
759  unittest::TestAllTypes::kOptionalStringFieldNumber),
760  new MultilineStringPrinter));
761 
762  protobuf_unittest::TestAllTypes message;
763  message.set_optional_string("first line\nsecond line\nthird line");
764 
767  EXPECT_EQ(
768  "optional_string: \n"
769  " \"first line\\n\"\n"
770  " \"second line\\n\"\n"
771  " \"third line\"\n",
772  text);
773 }
774 
775 class CustomNestedMessagePrinter : public TextFormat::MessagePrinter {
776  public:
779  void Print(const Message& message, bool single_line_mode,
780  TextFormat::BaseTextGenerator* generator) const override {
781  generator->PrintLiteral("custom");
782  }
783 };
784 
785 TEST_F(TextFormatTest, CustomMessagePrinter) {
786  TextFormat::Printer printer;
787  printer.RegisterMessagePrinter(
788  unittest::TestAllTypes::NestedMessage::default_instance().descriptor(),
789  new CustomNestedMessagePrinter);
790 
791  unittest::TestAllTypes message;
794  EXPECT_EQ("", text);
795 
796  message.mutable_optional_nested_message()->set_bb(1);
798  EXPECT_EQ("optional_nested_message {\n custom}\n", text);
799 }
800 
801 TEST_F(TextFormatTest, ParseBasic) {
802  io::ArrayInputStream input_stream(proto_debug_string_.data(),
803  proto_debug_string_.size());
804  TextFormat::Parse(&input_stream, &proto_);
806 }
807 
808 TEST_F(TextFormatExtensionsTest, ParseExtensions) {
809  io::ArrayInputStream input_stream(proto_debug_string_.data(),
810  proto_debug_string_.size());
811  TextFormat::Parse(&input_stream, &proto_);
812  TestUtil::ExpectAllExtensionsSet(proto_);
813 }
814 
815 TEST_F(TextFormatTest, ParseEnumFieldFromNumber) {
816  // Create a parse string with a numerical value for an enum field.
817  std::string parse_string =
818  strings::Substitute("optional_nested_enum: $0", unittest::TestAllTypes::BAZ);
819  EXPECT_TRUE(TextFormat::ParseFromString(parse_string, &proto_));
820  EXPECT_TRUE(proto_.has_optional_nested_enum());
821  EXPECT_EQ(unittest::TestAllTypes::BAZ, proto_.optional_nested_enum());
822 }
823 
824 TEST_F(TextFormatTest, ParseEnumFieldFromNegativeNumber) {
825  ASSERT_LT(unittest::SPARSE_E, 0);
826  std::string parse_string =
827  strings::Substitute("sparse_enum: $0", unittest::SPARSE_E);
828  unittest::SparseEnumMessage proto;
829  EXPECT_TRUE(TextFormat::ParseFromString(parse_string, &proto));
830  EXPECT_TRUE(proto.has_sparse_enum());
831  EXPECT_EQ(unittest::SPARSE_E, proto.sparse_enum());
832 }
833 
834 TEST_F(TextFormatTest, PrintUnknownEnumFieldProto3) {
835  proto3_unittest::TestAllTypes proto;
836 
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));
845 
846  EXPECT_EQ(
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());
852 }
853 
854 TEST_F(TextFormatTest, ParseUnknownEnumFieldProto3) {
855  proto3_unittest::TestAllTypes proto;
856  std::string parse_string =
857  "repeated_nested_enum: [10, -10, 2147483647, -2147483648]";
858  EXPECT_TRUE(TextFormat::ParseFromString(parse_string, &proto));
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));
864 }
865 
866 TEST_F(TextFormatTest, ParseStringEscape) {
867  // Create a parse string with escaped characters in it.
868  std::string parse_string =
869  "optional_string: " + kEscapeTestStringEscaped + "\n";
870 
871  io::ArrayInputStream input_stream(parse_string.data(), parse_string.size());
872  TextFormat::Parse(&input_stream, &proto_);
873 
874  // Compare.
875  EXPECT_EQ(kEscapeTestString, proto_.optional_string());
876 }
877 
878 TEST_F(TextFormatTest, ParseConcatenatedString) {
879  // Create a parse string with multiple parts on one line.
880  std::string parse_string = "optional_string: \"foo\" \"bar\"\n";
881 
882  io::ArrayInputStream input_stream1(parse_string.data(), parse_string.size());
883  TextFormat::Parse(&input_stream1, &proto_);
884 
885  // Compare.
886  EXPECT_EQ("foobar", proto_.optional_string());
887 
888  // Create a parse string with multiple parts on separate lines.
889  parse_string =
890  "optional_string: \"foo\"\n"
891  "\"bar\"\n";
892 
893  io::ArrayInputStream input_stream2(parse_string.data(), parse_string.size());
894  TextFormat::Parse(&input_stream2, &proto_);
895 
896  // Compare.
897  EXPECT_EQ("foobar", proto_.optional_string());
898 }
899 
900 TEST_F(TextFormatTest, ParseFloatWithSuffix) {
901  // Test that we can parse a floating-point value with 'f' appended to the
902  // end. This is needed for backwards-compatibility with proto1.
903 
904  // Have it parse a float with the 'f' suffix.
905  std::string parse_string = "optional_float: 1.0f\n";
906 
907  io::ArrayInputStream input_stream(parse_string.data(), parse_string.size());
908 
909  TextFormat::Parse(&input_stream, &proto_);
910 
911  // Compare.
912  EXPECT_EQ(1.0, proto_.optional_float());
913 }
914 
915 TEST_F(TextFormatTest, ParseShortRepeatedForm) {
916  std::string parse_string =
917  // Mixed short-form and long-form are simply concatenated.
918  "repeated_int32: 1\n"
919  "repeated_int32: [456, 789]\n"
920  "repeated_nested_enum: [ FOO ,BAR, # comment\n"
921  " 3]\n"
922  // Note that while the printer won't print repeated strings in short-form,
923  // the parser will accept them.
924  "repeated_string: [ \"foo\", 'bar' ]\n"
925  // Repeated message
926  "repeated_nested_message: [ { bb: 1 }, { bb : 2 }]\n"
927  // Repeated group
928  "RepeatedGroup [{ a: 3 },{ a: 4 }]\n";
929 
930  ASSERT_TRUE(TextFormat::ParseFromString(parse_string, &proto_));
931 
932  ASSERT_EQ(3, proto_.repeated_int32_size());
933  EXPECT_EQ(1, proto_.repeated_int32(0));
934  EXPECT_EQ(456, proto_.repeated_int32(1));
935  EXPECT_EQ(789, proto_.repeated_int32(2));
936 
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));
941 
942  ASSERT_EQ(2, proto_.repeated_string_size());
943  EXPECT_EQ("foo", proto_.repeated_string(0));
944  EXPECT_EQ("bar", proto_.repeated_string(1));
945 
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());
949 
950  ASSERT_EQ(2, proto_.repeatedgroup_size());
951  EXPECT_EQ(3, proto_.repeatedgroup(0).a());
952  EXPECT_EQ(4, proto_.repeatedgroup(1).a());
953 }
954 
955 TEST_F(TextFormatTest, ParseShortRepeatedWithTrailingComma) {
956  std::string parse_string = "repeated_int32: [456,]\n";
957  ASSERT_FALSE(TextFormat::ParseFromString(parse_string, &proto_));
958  parse_string = "repeated_nested_enum: [ FOO , ]";
959  ASSERT_FALSE(TextFormat::ParseFromString(parse_string, &proto_));
960  parse_string = "repeated_string: [ \"foo\", ]";
961  ASSERT_FALSE(TextFormat::ParseFromString(parse_string, &proto_));
962  parse_string = "repeated_nested_message: [ { bb: 1 }, ]";
963  ASSERT_FALSE(TextFormat::ParseFromString(parse_string, &proto_));
964  parse_string = "RepeatedGroup [{ a: 3 },]\n";
965 }
966 
967 TEST_F(TextFormatTest, ParseShortRepeatedEmpty) {
968  std::string parse_string =
969  "repeated_int32: []\n"
970  "repeated_nested_enum: []\n"
971  "repeated_string: []\n"
972  "repeated_nested_message: []\n"
973  "RepeatedGroup []\n";
974 
975  ASSERT_TRUE(TextFormat::ParseFromString(parse_string, &proto_));
976 
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());
982 }
983 
984 TEST_F(TextFormatTest, ParseShortRepeatedConcatenatedWithEmpty) {
985  std::string parse_string =
986  // Starting with empty [] should have no impact.
987  "repeated_int32: []\n"
988  "repeated_nested_enum: []\n"
989  "repeated_string: []\n"
990  "repeated_nested_message: []\n"
991  "RepeatedGroup []\n"
992  // Mixed short-form and long-form are simply concatenated.
993  "repeated_int32: 1\n"
994  "repeated_int32: [456, 789]\n"
995  "repeated_nested_enum: [ FOO ,BAR, # comment\n"
996  " 3]\n"
997  // Note that while the printer won't print repeated strings in short-form,
998  // the parser will accept them.
999  "repeated_string: [ \"foo\", 'bar' ]\n"
1000  // Repeated message
1001  "repeated_nested_message: [ { bb: 1 }, { bb : 2 }]\n"
1002  // Repeated group
1003  "RepeatedGroup [{ a: 3 },{ a: 4 }]\n"
1004  // Adding empty [] should have no impact.
1005  "repeated_int32: []\n"
1006  "repeated_nested_enum: []\n"
1007  "repeated_string: []\n"
1008  "repeated_nested_message: []\n"
1009  "RepeatedGroup []\n";
1010 
1011  ASSERT_TRUE(TextFormat::ParseFromString(parse_string, &proto_));
1012 
1013  ASSERT_EQ(3, proto_.repeated_int32_size());
1014  EXPECT_EQ(1, proto_.repeated_int32(0));
1015  EXPECT_EQ(456, proto_.repeated_int32(1));
1016  EXPECT_EQ(789, proto_.repeated_int32(2));
1017 
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));
1022 
1023  ASSERT_EQ(2, proto_.repeated_string_size());
1024  EXPECT_EQ("foo", proto_.repeated_string(0));
1025  EXPECT_EQ("bar", proto_.repeated_string(1));
1026 
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());
1030 
1031  ASSERT_EQ(2, proto_.repeatedgroup_size());
1032  EXPECT_EQ(3, proto_.repeatedgroup(0).a());
1033  EXPECT_EQ(4, proto_.repeatedgroup(1).a());
1034 }
1035 
1036 
1037 TEST_F(TextFormatTest, Comments) {
1038  // Test that comments are ignored.
1039 
1040  std::string parse_string =
1041  "optional_int32: 1 # a comment\n"
1042  "optional_int64: 2 # another comment";
1043 
1044  io::ArrayInputStream input_stream(parse_string.data(), parse_string.size());
1045 
1046  TextFormat::Parse(&input_stream, &proto_);
1047 
1048  // Compare.
1049  EXPECT_EQ(1, proto_.optional_int32());
1050  EXPECT_EQ(2, proto_.optional_int64());
1051 }
1052 
1053 TEST_F(TextFormatTest, OptionalColon) {
1054  // Test that we can place a ':' after the field name of a nested message,
1055  // even though we don't have to.
1056 
1057  std::string parse_string = "optional_nested_message: { bb: 1}\n";
1058 
1059  io::ArrayInputStream input_stream(parse_string.data(), parse_string.size());
1060 
1061  TextFormat::Parse(&input_stream, &proto_);
1062 
1063  // Compare.
1064  EXPECT_TRUE(proto_.has_optional_nested_message());
1065  EXPECT_EQ(1, proto_.optional_nested_message().bb());
1066 }
1067 
1068 // Some platforms (e.g. Windows) insist on padding the exponent to three
1069 // digits when one or two would be just fine.
1071  text = StringReplace(text, "e+0", "e+", true);
1072  text = StringReplace(text, "e-0", "e-", true);
1073  return text;
1074 }
1075 
1076 TEST_F(TextFormatTest, PrintExotic) {
1077  unittest::TestAllTypes message;
1078 
1079  message.add_repeated_int64(int64_t{-9223372036854775807} - 1);
1080  message.add_repeated_uint64(uint64_t{18446744073709551615u});
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());
1090  message.add_repeated_string(std::string("\000\001\a\b\f\n\r\t\v\\\'\"", 12));
1091 
1092  // Fun story: We used to use 1.23e22 instead of 1.23e21 above, but this
1093  // seemed to trigger an odd case on MinGW/GCC 3.4.5 where GCC's parsing of
1094  // the value differed from strtod()'s parsing. That is to say, the
1095  // following assertion fails on MinGW:
1096  // assert(1.23e22 == strtod("1.23e22", nullptr));
1097  // As a result, SimpleDtoa() would print the value as
1098  // "1.2300000000000001e+22" to make sure strtod() produce the exact same
1099  // result. Our goal is to test runtime parsing, not compile-time parsing,
1100  // so this wasn't our problem. It was found that using 1.23e21 did not
1101  // have this problem, so we switched to that instead.
1102 
1103  EXPECT_EQ(
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"
1115  "repeated_string: "
1116  "\"\\000\\001\\007\\010\\014\\n\\r\\t\\013\\\\\\'\\\"\"\n",
1117  RemoveRedundantZeros(message.DebugString()));
1118 }
1119 
1120 TEST_F(TextFormatTest, PrintFloatPrecision) {
1121  unittest::TestAllTypes message;
1122 
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);
1162 
1163  EXPECT_EQ(
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",
1203  RemoveRedundantZeros(message.DebugString()));
1204 }
1205 
1206 TEST_F(TextFormatTest, AllowPartial) {
1207  unittest::TestRequired message;
1208  TextFormat::Parser parser;
1209  parser.AllowPartialMessage(true);
1210  EXPECT_TRUE(parser.ParseFromString("a: 1", &message));
1211  EXPECT_EQ(1, message.a());
1212  EXPECT_FALSE(message.has_b());
1213  EXPECT_FALSE(message.has_c());
1214 }
1215 
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",
1241  &message));
1242 
1243  ASSERT_EQ(2, message.repeated_int32_size());
1244  EXPECT_EQ(-1, message.repeated_int32(0));
1245  EXPECT_EQ(-2147483648, message.repeated_int32(1));
1246 
1247  ASSERT_EQ(2, message.repeated_int64_size());
1248  EXPECT_EQ(-1, message.repeated_int64(0));
1249  EXPECT_EQ(int64_t{-9223372036854775807} - 1, message.repeated_int64(1));
1250 
1251  ASSERT_EQ(2, message.repeated_uint32_size());
1252  EXPECT_EQ(4294967295u, message.repeated_uint32(0));
1253  EXPECT_EQ(2147483648u, message.repeated_uint32(1));
1254 
1255  ASSERT_EQ(2, message.repeated_uint64_size());
1256  EXPECT_EQ(uint64_t{18446744073709551615u}, message.repeated_uint64(0));
1257  EXPECT_EQ(uint64_t{9223372036854775808u}, message.repeated_uint64(1));
1258 
1259  ASSERT_EQ(13, message.repeated_double_size());
1260  EXPECT_EQ(123.0, message.repeated_double(0));
1261  EXPECT_EQ(123.5, message.repeated_double(1));
1262  EXPECT_EQ(0.125, message.repeated_double(2));
1263  EXPECT_EQ(1.23E17, message.repeated_double(3));
1264  EXPECT_EQ(1.235E22, message.repeated_double(4));
1265  EXPECT_EQ(1.235E-18, message.repeated_double(5));
1266  EXPECT_EQ(123.456789, message.repeated_double(6));
1267  EXPECT_EQ(message.repeated_double(7),
1268  std::numeric_limits<double>::infinity());
1269  EXPECT_EQ(message.repeated_double(8),
1270  std::numeric_limits<double>::infinity());
1271  EXPECT_EQ(message.repeated_double(9),
1272  -std::numeric_limits<double>::infinity());
1273  EXPECT_EQ(message.repeated_double(10),
1274  -std::numeric_limits<double>::infinity());
1275  EXPECT_TRUE(std::isnan(message.repeated_double(11)));
1276  EXPECT_TRUE(std::isnan(message.repeated_double(12)));
1277 
1278  // Note: Since these string literals have \0's in them, we must explicitly
1279  // pass their sizes to string's constructor.
1280  ASSERT_EQ(1, message.repeated_string_size());
1281  EXPECT_EQ(std::string("\000\001\a\b\f\n\r\t\v\\\'\"", 12),
1282  message.repeated_string(0));
1283 
1284  ASSERT_TRUE(
1285  TextFormat::ParseFromString("repeated_float: 3.4028235e+38\n"
1286  "repeated_float: -3.4028235e+38\n"
1287  "repeated_float: 3.402823567797337e+38\n"
1288  "repeated_float: -3.402823567797337e+38\n",
1289  &message));
1290  EXPECT_EQ(message.repeated_float(0), std::numeric_limits<float>::max());
1291  EXPECT_EQ(message.repeated_float(1), -std::numeric_limits<float>::max());
1292  EXPECT_EQ(message.repeated_float(2), std::numeric_limits<float>::infinity());
1293  EXPECT_EQ(message.repeated_float(3), -std::numeric_limits<float>::infinity());
1294 
1295 }
1296 
1297 TEST_F(TextFormatTest, PrintFieldsInIndexOrder) {
1298  protobuf_unittest::TestFieldOrderings message;
1299  // Fields are listed in index order instead of field number.
1300  message.set_my_string("str"); // Field number 11
1301  message.set_my_int(12345); // Field number 1
1302  message.set_my_float(0.999); // Field number 101
1303  // Extensions are listed based on the order of extension number.
1304  // Extension number 12.
1305  message
1306  .MutableExtension(
1307  protobuf_unittest::TestExtensionOrderings2::test_ext_orderings2)
1308  ->set_my_string("ext_str2");
1309  // Extension number 13.
1310  message
1311  .MutableExtension(
1312  protobuf_unittest::TestExtensionOrderings1::test_ext_orderings1)
1313  ->set_my_string("ext_str1");
1314  // Extension number 14.
1315  message
1316  .MutableExtension(protobuf_unittest::TestExtensionOrderings2::
1317  TestExtensionOrderings3::test_ext_orderings3)
1318  ->set_my_string("ext_str3");
1319  // Extension number 50.
1320  *message.MutableExtension(protobuf_unittest::my_extension_string) = "ext_str0";
1321 
1322  TextFormat::Printer printer;
1323  std::string text;
1324 
1325  // By default, print in field number order.
1326  // my_int: 12345
1327  // my_string: "str"
1328  // [protobuf_unittest.TestExtensionOrderings2.test_ext_orderings2] {
1329  // my_string: "ext_str2"
1330  // }
1331  // [protobuf_unittest.TestExtensionOrderings1.test_ext_orderings1] {
1332  // my_string: "ext_str1"
1333  // }
1334  // [protobuf_unittest.TestExtensionOrderings2.TestExtensionOrderings3.test_ext_orderings3]
1335  // {
1336  // my_string: "ext_str3"
1337  // }
1338  // [protobuf_unittest.my_extension_string]: "ext_str0"
1339  // my_float: 0.999
1340  printer.PrintToString(message, &text);
1341  EXPECT_EQ(
1342  "my_int: 12345\nmy_string: "
1343  "\"str\"\n[protobuf_unittest.TestExtensionOrderings2.test_ext_orderings2] "
1344  "{\n my_string: "
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",
1351  text);
1352 
1353  // Print in index order.
1354  // my_string: "str"
1355  // my_int: 12345
1356  // my_float: 0.999
1357  // [protobuf_unittest.TestExtensionOrderings2.test_ext_orderings2] {
1358  // my_string: "ext_str2"
1359  // }
1360  // [protobuf_unittest.TestExtensionOrderings1.test_ext_orderings1] {
1361  // my_string: "ext_str1"
1362  // }
1363  // [protobuf_unittest.TestExtensionOrderings2.TestExtensionOrderings3.test_ext_orderings3]
1364  // {
1365  // my_string: "ext_str3"
1366  // }
1367  // [protobuf_unittest.my_extension_string]: "ext_str0"
1368  printer.SetPrintMessageFieldsInIndexOrder(true);
1369  printer.PrintToString(message, &text);
1370  EXPECT_EQ(
1371  "my_string: \"str\"\nmy_int: 12345\nmy_float: "
1372  "0.999\n[protobuf_unittest.TestExtensionOrderings2.test_ext_orderings2] "
1373  "{\n my_string: "
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",
1379  text);
1380 }
1381 
1382 class TextFormatParserTest : public testing::Test {
1383  protected:
1385  int line, int col) {
1386  std::unique_ptr<unittest::TestAllTypes> proto(new unittest::TestAllTypes);
1387  ExpectFailure(input, message, line, col, proto.get());
1388  }
1389 
1391  int line, int col, Message* proto) {
1392  ExpectMessage(input, message, line, col, proto, false);
1393  }
1394 
1396  int line, int col, Message* proto, bool expected_result) {
1397  MockErrorCollector error_collector;
1398  parser_.RecordErrorsTo(&error_collector);
1399  EXPECT_EQ(expected_result, parser_.ParseFromString(input, proto))
1400  << input << " -> " << proto->DebugString();
1401  EXPECT_EQ(StrCat(line, ":", col, ": ", message, "\n"),
1402  error_collector.text_);
1403  parser_.RecordErrorsTo(nullptr);
1404  }
1405 
1407  TextFormat::ParseInfoTree* info_tree) {
1408  MockErrorCollector error_collector;
1409  parser_.RecordErrorsTo(&error_collector);
1410  parser_.WriteLocationsTo(info_tree);
1412  parser_.WriteLocationsTo(nullptr);
1413  parser_.RecordErrorsTo(nullptr);
1414  }
1415 
1417  const std::string& field_name, int index, int start_line,
1418  int start_column, int end_line, int end_column) {
1420  tree->GetLocationRange(d->FindFieldByName(field_name), index);
1421  EXPECT_EQ(start_line, range.start.line);
1422  EXPECT_EQ(start_column, range.start.column);
1423  EXPECT_EQ(end_line, range.end.line);
1424  EXPECT_EQ(end_column, range.end.column);
1425  TextFormat::ParseLocation start_location =
1426  tree->GetLocation(d->FindFieldByName(field_name), index);
1427  EXPECT_EQ(start_line, start_location.line);
1428  EXPECT_EQ(start_column, start_location.column);
1429  }
1430 
1431  // An error collector which simply concatenates all its errors into a big
1432  // block of text which can be checked.
1433  class MockErrorCollector : public io::ErrorCollector {
1434  public:
1437 
1439 
1440  // implements ErrorCollector -------------------------------------
1441  void AddError(int line, int column, const std::string& message) {
1442  strings::SubstituteAndAppend(&text_, "$0:$1: $2\n", line + 1, column + 1,
1443  message);
1444  }
1445 
1446  void AddWarning(int line, int column, const std::string& message) {
1447  AddError(line, column, "WARNING:" + message);
1448  }
1449  };
1450 
1452 };
1453 
1454 TEST_F(TextFormatParserTest, ParseInfoTreeBuilding) {
1455  std::unique_ptr<unittest::TestAllTypes> message(new unittest::TestAllTypes);
1456  const Descriptor* d = message->GetDescriptor();
1457 
1458  std::string stringData =
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"
1465  " bb: 78\n"
1466  ">\n"
1467  "repeated_nested_message <\n"
1468  " bb: 79\n"
1469  ">\n"
1470  "repeated_nested_message <\n"
1471  " bb: 80\n"
1472  ">";
1473 
1475  ExpectSuccessAndTree(stringData, message.get(), &tree);
1476 
1477  // Verify that the tree has the correct positions.
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);
1481 
1482  ExpectLocation(&tree, d, "repeated_int32", 0, 3, 0, 3, 17);
1483  ExpectLocation(&tree, d, "repeated_int32", 1, 4, 0, 4, 18);
1484 
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);
1488 
1489  // Check for fields not set. For an invalid field, the start and end locations
1490  // returned should be -1, -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);
1494 
1495  // Verify inside the nested message.
1496  const FieldDescriptor* nested_field =
1497  d->FindFieldByName("optional_nested_message");
1498 
1499  TextFormat::ParseInfoTree* nested_tree =
1500  tree.GetTreeForNested(nested_field, -1);
1501  ExpectLocation(nested_tree, nested_field->message_type(), "bb", -1, 6, 2, 6,
1502  8);
1503 
1504  // Verify inside another nested message.
1505  nested_field = d->FindFieldByName("repeated_nested_message");
1506  nested_tree = tree.GetTreeForNested(nested_field, 0);
1507  ExpectLocation(nested_tree, nested_field->message_type(), "bb", -1, 9, 2, 9,
1508  8);
1509 
1510  nested_tree = tree.GetTreeForNested(nested_field, 1);
1511  ExpectLocation(nested_tree, nested_field->message_type(), "bb", -1, 12, 2, 12,
1512  8);
1513 
1514  // Verify a nullptr tree for an unknown nested field.
1515  TextFormat::ParseInfoTree* unknown_nested_tree =
1516  tree.GetTreeForNested(nested_field, 2);
1517 
1518  EXPECT_EQ(nullptr, unknown_nested_tree);
1519 }
1520 
1521 TEST_F(TextFormatParserTest, ParseFieldValueFromString) {
1522  std::unique_ptr<unittest::TestAllTypes> message(new unittest::TestAllTypes);
1523  const Descriptor* d = message->GetDescriptor();
1524 
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());
1530 
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());
1536 
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());
1542 
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());
1548 
1549 #define EXPECT_INVALID(name, valuestring) \
1550  EXPECT_FALSE(TextFormat::ParseFieldValueFromString( \
1551  valuestring, d->FindFieldByName("optional_" #name), message.get()));
1552 
1553  // int32
1554  EXPECT_FIELD(int32, 1, "1");
1555  EXPECT_FIELD(int32, -1, "-1");
1556  EXPECT_FIELD(int32, 0x1234, "0x1234");
1557  EXPECT_INVALID(int32, "a");
1558  EXPECT_INVALID(int32, "999999999999999999999999999999999999");
1559  EXPECT_INVALID(int32, "1,2");
1560 
1561  // int64
1562  EXPECT_FIELD(int64, 1, "1");
1563  EXPECT_FIELD(int64, -1, "-1");
1564  EXPECT_FIELD(int64, 0x1234567812345678LL, "0x1234567812345678");
1565  EXPECT_INVALID(int64, "a");
1566  EXPECT_INVALID(int64, "999999999999999999999999999999999999");
1567  EXPECT_INVALID(int64, "1,2");
1568 
1569  // uint64
1570  EXPECT_FIELD(uint64, 1, "1");
1571  EXPECT_FIELD(uint64, 0xf234567812345678ULL, "0xf234567812345678");
1572  EXPECT_INVALID(uint64, "-1");
1573  EXPECT_INVALID(uint64, "a");
1574  EXPECT_INVALID(uint64, "999999999999999999999999999999999999");
1575  EXPECT_INVALID(uint64, "1,2");
1576 
1577  // fixed32
1578  EXPECT_FIELD(fixed32, 1, "1");
1579  EXPECT_FIELD(fixed32, 0x12345678, "0x12345678");
1580  EXPECT_INVALID(fixed32, "-1");
1581  EXPECT_INVALID(fixed32, "a");
1582  EXPECT_INVALID(fixed32, "999999999999999999999999999999999999");
1583  EXPECT_INVALID(fixed32, "1,2");
1584 
1585  // fixed64
1586  EXPECT_FIELD(fixed64, 1, "1");
1587  EXPECT_FIELD(fixed64, 0x1234567812345678ULL, "0x1234567812345678");
1588  EXPECT_INVALID(fixed64, "-1");
1589  EXPECT_INVALID(fixed64, "a");
1590  EXPECT_INVALID(fixed64, "999999999999999999999999999999999999");
1591  EXPECT_INVALID(fixed64, "1,2");
1592 
1593  // bool
1594  EXPECT_BOOL_FIELD(bool, true, "true");
1595  EXPECT_BOOL_FIELD(bool, false, "false");
1596  EXPECT_BOOL_FIELD(bool, true, "1");
1597  EXPECT_BOOL_FIELD(bool, true, "t");
1598  EXPECT_BOOL_FIELD(bool, false, "0");
1599  EXPECT_BOOL_FIELD(bool, false, "f");
1600  EXPECT_FIELD(bool, true, "True");
1601  EXPECT_FIELD(bool, false, "False");
1602  EXPECT_INVALID(bool, "tRue");
1603  EXPECT_INVALID(bool, "faLse");
1604  EXPECT_INVALID(bool, "2");
1605  EXPECT_INVALID(bool, "-0");
1606  EXPECT_INVALID(bool, "on");
1607  EXPECT_INVALID(bool, "a");
1608 
1609  // float
1610  EXPECT_FIELD(float, 1, "1");
1611  EXPECT_FLOAT_FIELD(float, 1.5, "1.5");
1612  EXPECT_FLOAT_FIELD(float, 1.5e3, "1.5e3");
1613  EXPECT_FLOAT_FIELD(float, -4.55, "-4.55");
1614  EXPECT_INVALID(float, "a");
1615  EXPECT_INVALID(float, "1,2");
1616 
1617  // double
1618  EXPECT_FIELD(double, 1, "1");
1619  EXPECT_FIELD(double, -1, "-1");
1620  EXPECT_DOUBLE_FIELD(double, 2.3, "2.3");
1621  EXPECT_DOUBLE_FIELD(double, 3e5, "3e5");
1622  EXPECT_INVALID(double, "a");
1623  EXPECT_INVALID(double, "1,2");
1624  // Rejects hex and oct numbers for a double field.
1625  EXPECT_INVALID(double, "0xf");
1626  EXPECT_INVALID(double, "012");
1627 
1628  // string
1629  EXPECT_FIELD(string, "hello", "\"hello\"");
1630  EXPECT_FIELD(string, "-1.87", "'-1.87'");
1631  EXPECT_INVALID(string, "hello"); // without quote for value
1632 
1633  // enum
1634  EXPECT_FIELD(nested_enum, unittest::TestAllTypes::BAR, "BAR");
1635  EXPECT_FIELD(nested_enum, unittest::TestAllTypes::BAZ,
1636  StrCat(unittest::TestAllTypes::BAZ));
1637  EXPECT_INVALID(nested_enum, "FOOBAR");
1638 
1639  // message
1641  "<bb:12>", d->FindFieldByName("optional_nested_message"), message.get()));
1642  EXPECT_EQ(12, message->optional_nested_message().bb());
1643  EXPECT_TRUE(message->has_optional_nested_message());
1644  EXPECT_INVALID(nested_message, "any");
1645 
1646 #undef EXPECT_FIELD
1647 #undef EXPECT_BOOL_FIELD
1648 #undef EXPECT_FLOAT_FIELD
1649 #undef EXPECT_DOUBLE_FIELD
1650 #undef EXPECT_INVALID
1651 }
1652 
1653 TEST_F(TextFormatParserTest, InvalidToken) {
1654  ExpectFailure("optional_bool: true\n-5\n", "Expected identifier, got: -", 2,
1655  1);
1656 
1657  ExpectFailure("optional_bool: true!\n", "Expected identifier, got: !", 1, 20);
1658  ExpectFailure("\"some string\"", "Expected identifier, got: \"some string\"",
1659  1, 1);
1660 }
1661 
1662 TEST_F(TextFormatParserTest, InvalidFieldName) {
1663  ExpectFailure(
1664  "invalid_field: somevalue\n",
1665  "Message type \"protobuf_unittest.TestAllTypes\" has no field named "
1666  "\"invalid_field\".",
1667  1, 14);
1668 }
1669 
1670 TEST_F(TextFormatParserTest, InvalidCapitalization) {
1671  // We require that group names be exactly as they appear in the .proto.
1672  ExpectFailure(
1673  "optionalgroup {\na: 15\n}\n",
1674  "Message type \"protobuf_unittest.TestAllTypes\" has no field named "
1675  "\"optionalgroup\".",
1676  1, 15);
1677  ExpectFailure(
1678  "OPTIONALgroup {\na: 15\n}\n",
1679  "Message type \"protobuf_unittest.TestAllTypes\" has no field named "
1680  "\"OPTIONALgroup\".",
1681  1, 15);
1682  ExpectFailure(
1683  "Optional_Double: 10.0\n",
1684  "Message type \"protobuf_unittest.TestAllTypes\" has no field named "
1685  "\"Optional_Double\".",
1686  1, 16);
1687 }
1688 
1689 TEST_F(TextFormatParserTest, AllowIgnoreCapitalizationError) {
1690  TextFormat::Parser parser;
1691  protobuf_unittest::TestAllTypes proto;
1692 
1693  // These fields have a mismatching case.
1694  EXPECT_FALSE(parser.ParseFromString("Optional_Double: 10.0", &proto));
1695  EXPECT_FALSE(parser.ParseFromString("oPtIoNaLgRoUp { a: 15 }", &proto));
1696 
1697  // ... but are parsed correctly if we match case insensitive.
1698  parser.AllowCaseInsensitiveField(true);
1699  EXPECT_TRUE(parser.ParseFromString("Optional_Double: 10.0", &proto));
1700  EXPECT_EQ(10.0, proto.optional_double());
1701  EXPECT_TRUE(parser.ParseFromString("oPtIoNaLgRoUp { a: 15 }", &proto));
1702  EXPECT_EQ(15, proto.optionalgroup().a());
1703 }
1704 
1705 TEST_F(TextFormatParserTest, InvalidFieldValues) {
1706  // Invalid values for a double/float field.
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,
1712  17);
1713 
1714  // Invalid values for a signed integer field.
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,
1721  16);
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);
1730 
1731  // Invalid values for an unsigned integer field.
1732  ExpectFailure("optional_uint64: \"hello\"\n",
1733  "Expected integer, got: \"hello\"", 1, 18);
1734  ExpectFailure("optional_uint64: true\n", "Expected integer, got: true", 1,
1735  18);
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,
1740  17);
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);
1745 
1746  // Invalid values for a boolean field.
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);
1752 
1753  ExpectFailure(
1754  "optional_bool: meh\n",
1755  "Invalid value for boolean field \"optional_bool\". Value: \"meh\".", 2,
1756  1);
1757 
1758  ExpectFailure("optional_bool {\n \n}\n", "Expected \":\", found \"{\".", 1,
1759  15);
1760 
1761  // Invalid values for a string field.
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,
1767  17);
1768 
1769  // Invalid values for an enumeration field.
1770  ExpectFailure("optional_nested_enum: \"hello\"\n",
1771  "Expected integer or identifier, got: \"hello\"", 1, 23);
1772 
1773  // Valid token, but enum value is not defined.
1774  ExpectFailure("optional_nested_enum: 5\n",
1775  "Unknown enumeration value of \"5\" for field "
1776  "\"optional_nested_enum\".",
1777  2, 1);
1778  // We consume the negative sign, so the error position starts one character
1779  // later.
1780  ExpectFailure("optional_nested_enum: -7.5\n", "Expected integer, got: 7.5", 1,
1781  24);
1782  ExpectFailure("optional_nested_enum: !\n",
1783  "Expected integer or identifier, got: !", 1, 23);
1784 
1785  ExpectFailure("optional_nested_enum: grah\n",
1786  "Unknown enumeration value of \"grah\" for field "
1787  "\"optional_nested_enum\".",
1788  2, 1);
1789 
1790  ExpectFailure("optional_nested_enum {\n \n}\n",
1791  "Expected \":\", found \"{\".", 1, 22);
1792 }
1793 
1794 TEST_F(TextFormatParserTest, MessageDelimiters) {
1795  // Non-matching delimiters.
1796  ExpectFailure("OptionalGroup <\n \n}\n", "Expected \">\", found \"}\".", 3,
1797  1);
1798 
1799  // Invalid delimiters.
1800  ExpectFailure("OptionalGroup [\n \n]\n", "Expected \"{\", found \"[\".", 1,
1801  15);
1802 
1803  // Unending message.
1804  ExpectFailure("optional_nested_message {\n \nbb: 118\n",
1805  "Expected identifier, got: ", 4, 1);
1806 }
1807 
1808 TEST_F(TextFormatParserTest, UnknownExtension) {
1809  // Non-matching delimiters.
1810  ExpectFailure("[blahblah]: 123",
1811  "Extension \"blahblah\" is not defined or is not an "
1812  "extension of \"protobuf_unittest.TestAllTypes\".",
1813  1, 11);
1814 }
1815 
1816 TEST_F(TextFormatParserTest, MissingRequired) {
1817  unittest::TestRequired message;
1818  ExpectFailure("a: 1", "Message missing required fields: b, c", 0, 1,
1819  &message);
1820 }
1821 
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,
1826  &message);
1827 }
1828 
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,
1833  &message);
1834 }
1835 
1836 TEST_F(TextFormatParserTest, MergeDuplicateRequired) {
1837  unittest::TestRequired message;
1838  TextFormat::Parser parser;
1839  EXPECT_TRUE(parser.MergeFromString("a: 1 b: 2 c: 3 a: 4", &message));
1840  EXPECT_EQ(4, message.a());
1841 }
1842 
1843 TEST_F(TextFormatParserTest, MergeDuplicateOptional) {
1844  unittest::ForeignMessage message;
1845  TextFormat::Parser parser;
1846  EXPECT_TRUE(parser.MergeFromString("c: 1 c: 2", &message));
1847  EXPECT_EQ(2, message.c());
1848 }
1849 
1850 TEST_F(TextFormatParserTest, ExplicitDelimiters) {
1851  unittest::TestRequired message;
1853  EXPECT_EQ(1, message.a());
1854  EXPECT_EQ(2, message.b());
1855  EXPECT_EQ(3, message.c());
1856 }
1857 
1858 TEST_F(TextFormatParserTest, PrintErrorsToStderr) {
1859  std::vector<std::string> errors;
1860 
1861  {
1862  ScopedMemoryLog log;
1863  unittest::TestAllTypes proto;
1864  EXPECT_FALSE(TextFormat::ParseFromString("no_such_field: 1", &proto));
1865  errors = log.GetMessages(ERROR);
1866  }
1867 
1868  ASSERT_EQ(1, errors.size());
1869  EXPECT_EQ(
1870  "Error parsing text-format protobuf_unittest.TestAllTypes: "
1871  "1:14: Message type \"protobuf_unittest.TestAllTypes\" has no field "
1872  "named \"no_such_field\".",
1873  errors[0]);
1874 }
1875 
1876 TEST_F(TextFormatParserTest, FailsOnTokenizationError) {
1877  std::vector<std::string> errors;
1878 
1879  {
1880  ScopedMemoryLog log;
1881  unittest::TestAllTypes proto;
1882  EXPECT_FALSE(TextFormat::ParseFromString("\020", &proto));
1883  errors = log.GetMessages(ERROR);
1884  }
1885 
1886  ASSERT_EQ(1, errors.size());
1887  EXPECT_EQ(
1888  "Error parsing text-format protobuf_unittest.TestAllTypes: "
1889  "1:1: Invalid control characters encountered in text.",
1890  errors[0]);
1891 }
1892 
1893 TEST_F(TextFormatParserTest, ParseDeprecatedField) {
1894  unittest::TestDeprecatedFields message;
1895  ExpectMessage("deprecated_int32: 42",
1896  "WARNING:text format contains deprecated field "
1897  "\"deprecated_int32\"",
1898  1, 21, &message, true);
1899 }
1900 
1901 TEST_F(TextFormatParserTest, SetRecursionLimit) {
1902  const char* format = "child: { $0 }";
1904  for (int i = 0; i < 100; ++i) input = strings::Substitute(format, input);
1905 
1906  unittest::NestedTestAllTypes message;
1907  ExpectSuccessAndTree(input, &message, nullptr);
1908 
1910  parser_.SetRecursionLimit(100);
1911  ExpectMessage(input,
1912  "Message is too deep, the parser exceeded the configured "
1913  "recursion limit of 100.",
1914  1, 908, &message, false);
1915 
1916  parser_.SetRecursionLimit(101);
1917  ExpectSuccessAndTree(input, &message, nullptr);
1918 }
1919 
1920 TEST_F(TextFormatParserTest, SetRecursionLimitUnknownFieldValue) {
1921  const char* format = "[$0]";
1922  std::string input = "\"test_value\"";
1923  for (int i = 0; i < 99; ++i) input = strings::Substitute(format, input);
1924  std::string not_deep_input = StrCat("unknown_nested_array: ", input);
1925 
1926  parser_.AllowUnknownField(true);
1927  parser_.SetRecursionLimit(100);
1928 
1929  unittest::NestedTestAllTypes message;
1930  ExpectSuccessAndTree(not_deep_input, &message, nullptr);
1931 
1933  std::string deep_input = StrCat("unknown_nested_array: ", input);
1934  ExpectMessage(
1935  deep_input,
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.",
1939  1, 21, &message, false);
1940 
1941  parser_.SetRecursionLimit(101);
1942  ExpectSuccessAndTree(deep_input, &message, nullptr);
1943 }
1944 
1945 TEST_F(TextFormatParserTest, SetRecursionLimitUnknownFieldMessage) {
1946  const char* format = "unknown_child: { $0 }";
1948  for (int i = 0; i < 100; ++i) input = strings::Substitute(format, input);
1949 
1950  parser_.AllowUnknownField(true);
1951  parser_.SetRecursionLimit(100);
1952 
1953  unittest::NestedTestAllTypes message;
1954  ExpectSuccessAndTree(input, &message, nullptr);
1955 
1957  ExpectMessage(
1958  input,
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.",
1962  1, 14, &message, false);
1963 
1964  parser_.SetRecursionLimit(101);
1965  ExpectSuccessAndTree(input, &message, nullptr);
1966 }
1967 
1968 TEST_F(TextFormatParserTest, ParseAnyFieldWithAdditionalWhiteSpaces) {
1969  Any any;
1970  std::string parse_string =
1971  "[type.googleapis.com/protobuf_unittest.TestAllTypes] \t : \t {\n"
1972  " optional_int32: 321\n"
1973  " optional_string: \"teststr0\"\n"
1974  "}\n";
1975 
1976  ASSERT_TRUE(TextFormat::ParseFromString(parse_string, &any));
1977 
1978  TextFormat::Printer printer;
1979  printer.SetExpandAny(true);
1980  std::string text;
1981  ASSERT_TRUE(printer.PrintToString(any, &text));
1982  EXPECT_EQ(text,
1983  "[type.googleapis.com/protobuf_unittest.TestAllTypes] {\n"
1984  " optional_int32: 321\n"
1985  " optional_string: \"teststr0\"\n"
1986  "}\n");
1987 }
1988 
1989 TEST_F(TextFormatParserTest, ParseExtensionFieldWithAdditionalWhiteSpaces) {
1990  unittest::TestAllExtensions proto;
1991  std::string parse_string =
1992  "[protobuf_unittest.optional_int32_extension] : \t 101\n"
1993  "[protobuf_unittest.optional_int64_extension] \t : 102\n";
1994 
1995  ASSERT_TRUE(TextFormat::ParseFromString(parse_string, &proto));
1996 
1997  TextFormat::Printer printer;
1998  std::string text;
1999  ASSERT_TRUE(printer.PrintToString(proto, &text));
2000  EXPECT_EQ(text,
2001  "[protobuf_unittest.optional_int32_extension]: 101\n"
2002  "[protobuf_unittest.optional_int64_extension]: 102\n");
2003 }
2004 
2005 TEST_F(TextFormatParserTest, ParseNormalFieldWithAdditionalWhiteSpaces) {
2006  unittest::TestAllTypes proto;
2007  std::string parse_string =
2008  "repeated_int32 : \t 1\n"
2009  "repeated_int32: 2\n"
2010  "repeated_nested_message: {\n"
2011  " bb: 3\n"
2012  "}\n"
2013  "repeated_nested_message : \t {\n"
2014  " bb: 4\n"
2015  "}\n"
2016  "repeated_nested_message {\n"
2017  " bb: 5\n"
2018  "}\n";
2019 
2020  ASSERT_TRUE(TextFormat::ParseFromString(parse_string, &proto));
2021 
2022  TextFormat::Printer printer;
2023  std::string text;
2024  ASSERT_TRUE(printer.PrintToString(proto, &text));
2025  EXPECT_EQ(text,
2026  "repeated_int32: 1\n"
2027  "repeated_int32: 2\n"
2028  "repeated_nested_message {\n"
2029  " bb: 3\n"
2030  "}\n"
2031  "repeated_nested_message {\n"
2032  " bb: 4\n"
2033  "}\n"
2034  "repeated_nested_message {\n"
2035  " bb: 5\n"
2036  "}\n");
2037 }
2038 
2039 TEST_F(TextFormatParserTest, ParseSkippedFieldWithAdditionalWhiteSpaces) {
2040  protobuf_unittest::TestAllTypes proto;
2042  parser.AllowUnknownField(true);
2043  EXPECT_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"
2048  "}\n"
2049  "[somewhere.unknown_extension2] : \t {\n"
2050  " unknown_field3 \t : 12345\n"
2051  " [somewhere.unknown_extension3] \t : {\n"
2052  " unknown_field4: 10\n"
2053  " }\n"
2054  " [somewhere.unknown_extension4] \t {\n"
2055  " }\n"
2056  "}\n",
2057  &proto));
2058  std::string text;
2059  TextFormat::Printer printer;
2060  ASSERT_TRUE(printer.PrintToString(proto, &text));
2061  EXPECT_EQ(text, "optional_int32: 321\n");
2062 }
2063 
2064 class TextFormatMessageSetTest : public testing::Test {
2065  protected:
2066  static const char proto_debug_string_[];
2067 };
2069  "message_set {\n"
2070  " [protobuf_unittest.TestMessageSetExtension1] {\n"
2071  " i: 23\n"
2072  " }\n"
2073  " [protobuf_unittest.TestMessageSetExtension2] {\n"
2074  " str: \"foo\"\n"
2075  " }\n"
2076  "}\n";
2077 
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);
2083  item_a->set_i(23);
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());
2089 }
2090 
2091 TEST_F(TextFormatMessageSetTest, Deserialize) {
2092  protobuf_unittest::TestMessageSetContainer proto;
2093  ASSERT_TRUE(TextFormat::ParseFromString(proto_debug_string_, &proto));
2094  EXPECT_EQ(
2095  23,
2096  proto.message_set()
2097  .GetExtension(
2098  protobuf_unittest::TestMessageSetExtension1::message_set_extension)
2099  .i());
2100  EXPECT_EQ(
2101  "foo",
2102  proto.message_set()
2103  .GetExtension(
2104  protobuf_unittest::TestMessageSetExtension2::message_set_extension)
2105  .str());
2106 
2107  // Ensure that these are the only entries present.
2108  std::vector<const FieldDescriptor*> descriptors;
2109  proto.message_set().GetReflection()->ListFields(proto.message_set(),
2110  &descriptors);
2111  EXPECT_EQ(2, descriptors.size());
2112 }
2113 
2114 TEST(TextFormatUnknownFieldTest, TestUnknownField) {
2115  protobuf_unittest::TestAllTypes proto;
2116  TextFormat::Parser parser;
2117  // Unknown field is not permitted by default.
2118  EXPECT_FALSE(parser.ParseFromString("unknown_field: 12345", &proto));
2119  EXPECT_FALSE(parser.ParseFromString("12345678: 12345", &proto));
2120 
2121  parser.AllowUnknownField(true);
2122  EXPECT_TRUE(parser.ParseFromString("unknown_field: 12345", &proto));
2123  EXPECT_TRUE(parser.ParseFromString("unknown_field: -12345", &proto));
2124  EXPECT_TRUE(parser.ParseFromString("unknown_field: 1.2345", &proto));
2125  EXPECT_TRUE(parser.ParseFromString("unknown_field: -1.2345", &proto));
2126  EXPECT_TRUE(parser.ParseFromString("unknown_field: 1.2345f", &proto));
2127  EXPECT_TRUE(parser.ParseFromString("unknown_field: -1.2345f", &proto));
2128  EXPECT_TRUE(parser.ParseFromString("unknown_field: inf", &proto));
2129  EXPECT_TRUE(parser.ParseFromString("unknown_field: -inf", &proto));
2130  EXPECT_TRUE(parser.ParseFromString("unknown_field: TYPE_STRING", &proto));
2131  EXPECT_TRUE(
2132  parser.ParseFromString("unknown_field: \"string value\"", &proto));
2133  // Invalid field value
2134  EXPECT_FALSE(parser.ParseFromString("unknown_field: -TYPE_STRING", &proto));
2135  // Two or more unknown fields
2136  EXPECT_TRUE(
2137  parser.ParseFromString("unknown_field1: TYPE_STRING\n"
2138  "unknown_field2: 12345",
2139  &proto));
2140  // Unknown nested message
2141  EXPECT_TRUE(
2142  parser.ParseFromString("unknown_message1: {}\n"
2143  "unknown_message2 {\n"
2144  " unknown_field: 12345\n"
2145  "}\n"
2146  "unknown_message3 <\n"
2147  " unknown_nested_message {\n"
2148  " unknown_field: 12345\n"
2149  " }\n"
2150  ">",
2151  &proto));
2152  // Unmatched delimiters for message body
2153  EXPECT_FALSE(parser.ParseFromString("unknown_message: {>", &proto));
2154  // Unknown extension
2155  EXPECT_TRUE(
2156  parser.ParseFromString("[somewhere.unknown_extension1]: 12345\n"
2157  "[somewhere.unknown_extension2] {\n"
2158  " unknown_field: 12345\n"
2159  "}",
2160  &proto));
2161  // Unknown fields between known fields
2162  ASSERT_TRUE(
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 }",
2168  &proto));
2169  EXPECT_EQ(1, proto.optional_int32());
2170  EXPECT_EQ("string", proto.optional_string());
2171  EXPECT_EQ(2, proto.optional_nested_message().bb());
2172 
2173  // Unknown field with numeric tag number instead of identifier.
2174  EXPECT_TRUE(parser.ParseFromString("12345678: 12345", &proto));
2175 
2176  // Nested unknown extensions.
2177  EXPECT_TRUE(
2178  parser.ParseFromString("[test.extension1] <\n"
2179  " unknown_nested_message <\n"
2180  " [test.extension2] <\n"
2181  " unknown_field: 12345\n"
2182  " >\n"
2183  " >\n"
2184  ">",
2185  &proto));
2186  EXPECT_TRUE(
2187  parser.ParseFromString("[test.extension1] {\n"
2188  " unknown_nested_message {\n"
2189  " [test.extension2] {\n"
2190  " unknown_field: 12345\n"
2191  " }\n"
2192  " }\n"
2193  "}",
2194  &proto));
2195  EXPECT_TRUE(
2196  parser.ParseFromString("[test.extension1] <\n"
2197  " some_unknown_fields: <\n"
2198  " unknown_field: 12345\n"
2199  " >\n"
2200  ">",
2201  &proto));
2202  EXPECT_TRUE(
2203  parser.ParseFromString("[test.extension1] {\n"
2204  " some_unknown_fields: {\n"
2205  " unknown_field: 12345\n"
2206  " }\n"
2207  "}",
2208  &proto));
2209 
2210  // Unknown field with compact repetition.
2211  EXPECT_TRUE(parser.ParseFromString("unknown_field: [1, 2]", &proto));
2212  // Unknown field with compact repetition of some unknown enum.
2213  EXPECT_TRUE(parser.ParseFromString("unknown_field: [VAL1, VAL2]", &proto));
2214  // Unknown field with compact repetition with sub-message.
2215  EXPECT_TRUE(parser.ParseFromString("unknown_field: [{a:1}, <b:2>]", &proto));
2216 }
2217 
2218 TEST(TextFormatUnknownFieldTest, TestAnyInUnknownField) {
2219  protobuf_unittest::TestAllTypes proto;
2220  TextFormat::Parser parser;
2221  parser.AllowUnknownField(true);
2222  EXPECT_TRUE(
2223  parser.ParseFromString("unknown {\n"
2224  " [type.googleapis.com/foo.bar] {\n"
2225  " }\n"
2226  "}",
2227  &proto));
2228 }
2229 
2230 TEST(TextFormatUnknownFieldTest, TestUnknownExtension) {
2231  protobuf_unittest::TestAllTypes proto;
2232  TextFormat::Parser parser;
2233  std::string message_with_ext =
2234  "[test.extension1] {\n"
2235  " some_unknown_fields: {\n"
2236  " unknown_field: 12345\n"
2237  " }\n"
2238  "}";
2239  // Unknown extensions are not permitted by default.
2240  EXPECT_FALSE(parser.ParseFromString(message_with_ext, &proto));
2241  // AllowUnknownField implies AllowUnknownExtension.
2242  parser.AllowUnknownField(true);
2243  EXPECT_TRUE(parser.ParseFromString(message_with_ext, &proto));
2244 
2245  parser.AllowUnknownField(false);
2246  EXPECT_FALSE(parser.ParseFromString(message_with_ext, &proto));
2247  parser.AllowUnknownExtension(true);
2248  EXPECT_TRUE(parser.ParseFromString(message_with_ext, &proto));
2249  // Unknown fields are still not accepted.
2250  EXPECT_FALSE(parser.ParseFromString("unknown_field: 1", &proto));
2251 }
2252 
2254  public:
2255  void SetUp() override {
2257  }
2258  void TearDown() override {
2260  }
2261 };
2262 
2263 TEST_F(TextFormatSilentMarkerTest, NonMessageFieldAsFirstField) {
2264  protobuf_unittest::TestAllTypes proto;
2265  proto.set_optional_int32(1);
2266  proto.mutable_optional_nested_message()->set_bb(2);
2267 
2268  EXPECT_EQ(
2269  "optional_int32: \t 1\n"
2270  "optional_nested_message {\n"
2271  " bb: 2\n"
2272  "}\n",
2273  proto.DebugString());
2274 
2275  EXPECT_EQ(
2276  "optional_int32: \t 1 "
2277  "optional_nested_message { bb: 2 }",
2278  proto.ShortDebugString());
2279 }
2280 
2281 TEST_F(TextFormatSilentMarkerTest, MessageFieldAsFirstField) {
2282  protobuf_unittest::TestAllTypes proto;
2283  proto.mutable_optional_nested_message()->set_bb(2);
2284  proto.add_repeated_int32(3);
2285 
2286  EXPECT_EQ(
2287  "optional_nested_message \t {\n"
2288  " bb: 2\n"
2289  "}\n"
2290  "repeated_int32: 3\n",
2291  proto.DebugString());
2292 
2293  EXPECT_EQ(
2294  "optional_nested_message \t { bb: 2 } "
2295  "repeated_int32: 3",
2296  proto.ShortDebugString());
2297 }
2298 
2299 TEST_F(TextFormatSilentMarkerTest, UnknownFieldAsFirstField) {
2300  unittest::TestEmptyMessage message;
2301  UnknownFieldSet* unknown_fields = message.mutable_unknown_fields();
2302 
2303  unknown_fields->AddVarint(5, 1);
2304  unknown_fields->AddGroup(5)->AddVarint(10, 5);
2305 
2306  EXPECT_EQ(
2307  "5: \t 1\n"
2308  "5 {\n"
2309  " 10: 5\n"
2310  "}\n",
2311  message.DebugString());
2312 
2313  EXPECT_EQ(
2314  "5: \t 1 "
2315  "5 { 10: 5 }",
2316  message.ShortDebugString());
2317 
2318  unknown_fields->Clear();
2319  unknown_fields->AddGroup(5)->AddVarint(10, 5);
2320  unknown_fields->AddVarint(5, 1);
2321 
2322  EXPECT_EQ(
2323  "5 \t {\n"
2324  " 10: 5\n"
2325  "}\n"
2326  "5: 1\n",
2327  message.DebugString());
2328 
2329  EXPECT_EQ(
2330  "5 \t { 10: 5 } "
2331  "5: 1",
2332  message.ShortDebugString());
2333 }
2334 
2335 TEST_F(TextFormatSilentMarkerTest, AnyFieldAsFirstField) {
2336  protobuf_unittest::TestAllTypes proto;
2337  proto.set_optional_string("teststr");
2338  proto.set_optional_int32(432);
2339  Any any;
2340  any.PackFrom(proto);
2341 
2342  EXPECT_EQ(
2343  "[type.googleapis.com/protobuf_unittest.TestAllTypes] \t {\n"
2344  " optional_int32: 432\n"
2345  " optional_string: \"teststr\"\n"
2346  "}\n",
2347  any.DebugString());
2348 
2349  EXPECT_EQ(
2350  "[type.googleapis.com/protobuf_unittest.TestAllTypes]"
2351  " \t { optional_int32: 432 optional_string: \"teststr\" }",
2352  any.ShortDebugString());
2353 }
2354 
2355 TEST_F(TextFormatSilentMarkerTest, ExtensionFieldAsFirstField) {
2356  unittest::TestAllExtensions proto;
2357  proto.SetExtension(protobuf_unittest::optional_int32_extension, 101);
2358  proto.SetExtension(protobuf_unittest::optional_int64_extension, 102);
2359 
2360  EXPECT_EQ(
2361  "[protobuf_unittest.optional_int32_extension]: \t 101\n"
2362  "[protobuf_unittest.optional_int64_extension]: 102\n",
2363  proto.DebugString());
2364 }
2365 
2366 TEST_F(TextFormatSilentMarkerTest, MapFieldAsFirstField) {
2367  unittest::TestMap proto;
2368  (*proto.mutable_map_int32_int32())[0] = 1;
2369  (*proto.mutable_map_int64_int64())[2] = 3;
2370 
2371  EXPECT_EQ(
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());
2375 }
2376 
2377 
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);
2382  TextFormat::Printer printer;
2383  std::string serialized;
2384  EXPECT_TRUE(printer.PrintToString(in_message, &serialized));
2385  proto3_unittest::TestAllTypes out_message;
2387  EXPECT_TRUE(parser.ParseFromString(serialized, &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()));
2394 }
2395 
2396 } // namespace text_format_unittest
2397 } // namespace protobuf
2398 } // namespace google
2399 
2400 #include <google/protobuf/port_undef.inc>
google::protobuf::text_format_unittest::CustomMessageContentFieldValuePrinter::PrintMessageContent
bool PrintMessageContent(const Message &message, int field_index, int field_count, bool single_line_mode, TextFormat::BaseTextGenerator *generator) const override
Definition: protobuf/src/google/protobuf/text_format_unittest.cc:585
EXPECT_FALSE
#define EXPECT_FALSE(condition)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1970
google::protobuf::text_format_unittest::TextFormatExtensionsTest::SetUpTestSuite
static void SetUpTestSuite()
Definition: protobuf/src/google/protobuf/text_format_unittest.cc:114
google::protobuf::text_format_unittest::TextFormatTest::proto_
unittest::TestAllTypes proto_
Definition: bloaty/third_party/protobuf/src/google/protobuf/text_format_unittest.cc:98
EXPECT_FLOAT_FIELD
#define EXPECT_FLOAT_FIELD(name, value, valuestring)
google::protobuf::text_format_unittest::CompactRepeatedFieldPrinter::PrintFieldName
void PrintFieldName(const Message &message, int field_index, int field_count, const Reflection *reflection, const FieldDescriptor *field, TextFormat::BaseTextGenerator *generator) const override
Definition: protobuf/src/google/protobuf/text_format_unittest.cc:664
google::protobuf::text_format_unittest::CustomNestedMessagePrinter::Print
void Print(const Message &message, bool single_line_mode, TextFormat::BaseTextGenerator *generator) const override
Definition: protobuf/src/google/protobuf/text_format_unittest.cc:779
http2_test_server.format
format
Definition: http2_test_server.py:118
google::protobuf::FieldDescriptor
Definition: bloaty/third_party/protobuf/src/google/protobuf/descriptor.h:515
google::protobuf::TextFormat::Print
static bool Print(const Message &message, io::ZeroCopyOutputStream *output)
Definition: bloaty/third_party/protobuf/src/google/protobuf/text_format.cc:2385
google::protobuf::text_format_unittest::TextFormatSilentMarkerTest::TearDown
void TearDown() override
Definition: protobuf/src/google/protobuf/text_format_unittest.cc:2258
google::protobuf::text_format_unittest::TextFormatExtensionsTest::proto_debug_string_
const std::string proto_debug_string_
Definition: bloaty/third_party/protobuf/src/google/protobuf/text_format_unittest.cc:120
google::protobuf::text_format_unittest::TextFormatParserTest::MockErrorCollector::AddWarning
void AddWarning(int line, int column, const std::string &message)
Definition: protobuf/src/google/protobuf/text_format_unittest.cc:1446
google::protobuf::text_format_unittest::CustomMessageFieldValuePrinter::PrintMessageStart
virtual std::string PrintMessageStart(const Message &message, int field_index, int field_count, bool single_line_mode) const
Definition: protobuf/src/google/protobuf/text_format_unittest.cc:539
google::protobuf::text_format_unittest::TextFormatExtensionsTest::TextFormatExtensionsTest
TextFormatExtensionsTest()
Definition: protobuf/src/google/protobuf/text_format_unittest.cc:122
google::protobuf::int64
int64_t int64
Definition: third_party/bloaty/third_party/protobuf/src/google/protobuf/stubs/port.h:151
EXPECT_THAT
#define EXPECT_THAT(value, matcher)
tests.google.protobuf.internal.message_test.isnan
def isnan(val)
Definition: bloaty/third_party/protobuf/python/compatibility_tests/v2.5.0/tests/google/protobuf/internal/message_test.py:65
google::protobuf::text_format_unittest::CompactRepeatedFieldPrinter::PrintMessageStart
void PrintMessageStart(const Message &message, int field_index, int field_count, bool single_line_mode, TextFormat::BaseTextGenerator *generator) const override
Definition: protobuf/src/google/protobuf/text_format_unittest.cc:677
capstone.range
range
Definition: third_party/bloaty/third_party/capstone/bindings/python/capstone/__init__.py:6
tests.google.protobuf.internal.test_util.SetAllExtensions
def SetAllExtensions(message)
Definition: bloaty/third_party/protobuf/python/compatibility_tests/v2.5.0/tests/google/protobuf/internal/test_util.py:187
google::protobuf.internal::WireFormatLite::TYPE_BYTES
@ TYPE_BYTES
Definition: bloaty/third_party/protobuf/src/google/protobuf/wire_format_lite.h:123
google::protobuf::text_format_unittest::TextFormatParserTest::ExpectLocation
void ExpectLocation(TextFormat::ParseInfoTree *tree, const Descriptor *d, const std::string &field_name, int index, int start_line, int start_column, int end_line, int end_column)
Definition: protobuf/src/google/protobuf/text_format_unittest.cc:1416
google::protobuf::TextFormat::BaseTextGenerator::PrintLiteral
void PrintLiteral(const char(&text)[n])
Definition: bloaty/third_party/protobuf/src/google/protobuf/text_format.h:115
google::protobuf::TextFormat::Parser
Definition: bloaty/third_party/protobuf/src/google/protobuf/text_format.h:511
google::protobuf::TextFormat::Printer::PrintToString
bool PrintToString(const Message &message, std::string *output) const
Definition: bloaty/third_party/protobuf/src/google/protobuf/text_format.cc:1860
google::protobuf.internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED
@ WIRETYPE_LENGTH_DELIMITED
Definition: bloaty/third_party/protobuf/src/google/protobuf/wire_format_lite.h:104
google::protobuf::FieldDescriptor::message_type
const Descriptor * message_type
Definition: protobuf/src/google/protobuf/descriptor.h:936
google::protobuf::text_format_unittest::CompactRepeatedFieldPrinter::PrintFieldName
void PrintFieldName(const Message &message, const Reflection *reflection, const FieldDescriptor *field, TextFormat::BaseTextGenerator *generator) const override
Definition: protobuf/src/google/protobuf/text_format_unittest.cc:673
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
google::protobuf::text_format_unittest::kEscapeTestString
const std::string kEscapeTestString
Definition: bloaty/third_party/protobuf/src/google/protobuf/text_format_unittest.cc:73
google::protobuf::text_format_unittest::TEST_F
TEST_F(TextFormatTest, Basic)
Definition: bloaty/third_party/protobuf/src/google/protobuf/text_format_unittest.cc:128
google::protobuf::uint32
uint32_t uint32
Definition: third_party/bloaty/third_party/protobuf/src/google/protobuf/stubs/port.h:155
u
OPENSSL_EXPORT pem_password_cb void * u
Definition: pem.h:351
grpc::protobuf::io::Printer
GRPC_CUSTOM_PRINTER Printer
Definition: src/compiler/config.h:54
google::protobuf
Definition: bloaty/third_party/protobuf/benchmarks/util/data_proto2_to_proto3_util.h:12
google::protobuf::text_format_unittest::TextFormatParserTest::MockErrorCollector::~MockErrorCollector
~MockErrorCollector()
Definition: protobuf/src/google/protobuf/text_format_unittest.cc:1436
google::protobuf::text_format_unittest::CustomMessageContentFieldValuePrinter
Definition: protobuf/src/google/protobuf/text_format_unittest.cc:582
google::protobuf::text_format_unittest::CustomMultilineCommentPrinter::PrintMessageStart
virtual std::string PrintMessageStart(const Message &message, int field_index, int field_count, bool single_line_comment) const
Definition: protobuf/src/google/protobuf/text_format_unittest.cc:632
google::protobuf::strings::Hex
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/strutil.h:594
google::protobuf::text_format_unittest::TextFormatParserTest::MockErrorCollector::AddError
void AddError(int line, int column, const std::string &message)
Definition: protobuf/src/google/protobuf/text_format_unittest.cc:1441
google::protobuf::TextFormat::ParseLocation
Definition: bloaty/third_party/protobuf/src/google/protobuf/text_format.h:458
google::protobuf::TextFormat::Printer::RegisterFieldValuePrinter
bool RegisterFieldValuePrinter(const FieldDescriptor *field, const FieldValuePrinter *printer)
Definition: bloaty/third_party/protobuf/src/google/protobuf/text_format.cc:1814
google::protobuf::TextFormat::PrintUnknownFieldsToString
static bool PrintUnknownFieldsToString(const UnknownFieldSet &unknown_fields, std::string *output)
Definition: bloaty/third_party/protobuf/src/google/protobuf/text_format.cc:2400
google::protobuf::Reflection
Definition: bloaty/third_party/protobuf/src/google/protobuf/message.h:397
google::protobuf::UnknownFieldSet::Clear
void Clear()
Definition: bloaty/third_party/protobuf/src/google/protobuf/unknown_field_set.h:296
google::protobuf::python::cmessage::UnknownFieldSet
static PyObject * UnknownFieldSet(CMessage *self)
Definition: bloaty/third_party/protobuf/python/google/protobuf/pyext/message.cc:2512
message
char * message
Definition: libuv/docs/code/tty-gravity/main.c:12
google::protobuf::text_format_unittest::TextFormatParserTest::parser_
TextFormat::Parser parser_
Definition: bloaty/third_party/protobuf/src/google/protobuf/text_format_unittest.cc:1377
google::protobuf::StrCat
string StrCat(const AlphaNum &a, const AlphaNum &b)
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/strutil.cc:1482
Descriptor
Definition: bloaty/third_party/protobuf/ruby/ext/google/protobuf_c/protobuf.h:121
testing::Test
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:402
tests.google.protobuf.internal.test_util.SetAllFields
def SetAllFields(message)
Definition: bloaty/third_party/protobuf/python/compatibility_tests/v2.5.0/tests/google/protobuf/internal/test_util.py:182
EXPECT_EQ
#define EXPECT_EQ(a, b)
Definition: iomgr/time_averaged_stats_test.cc:27
google::protobuf::text_format_unittest::TextFormatParserTest::MockErrorCollector::text_
std::string text_
Definition: bloaty/third_party/protobuf/src/google/protobuf/text_format_unittest.cc:1364
google::protobuf::TextFormat::Printer::RegisterMessagePrinter
bool RegisterMessagePrinter(const Descriptor *descriptor, const MessagePrinter *printer)
Definition: bloaty/third_party/protobuf/src/google/protobuf/text_format.cc:1845
google::protobuf::text_format_unittest::CustomNestedMessagePrinter::~CustomNestedMessagePrinter
~CustomNestedMessagePrinter() override
Definition: protobuf/src/google/protobuf/text_format_unittest.cc:778
google::protobuf::text_format_unittest::TextFormatTest::TextFormatTest
TextFormatTest()
Definition: protobuf/src/google/protobuf/text_format_unittest.cc:100
FieldDescriptor
Definition: bloaty/third_party/protobuf/ruby/ext/google/protobuf_c/protobuf.h:133
EXPECT_FIELD
#define EXPECT_FIELD(name, value, valuestring)
google::protobuf::text_format_unittest::TextFormatMessageSetTest::proto_debug_string_
static const char proto_debug_string_[]
Definition: bloaty/third_party/protobuf/src/google/protobuf/text_format_unittest.cc:1842
google::protobuf::TextFormat::Parser::ParseFromString
bool ParseFromString(const std::string &input, Message *output)
Definition: bloaty/third_party/protobuf/src/google/protobuf/text_format.cc:1420
google::protobuf.internal.python_message.Extensions
Extensions
Definition: bloaty/third_party/protobuf/python/google/protobuf/internal/python_message.py:584
google::protobuf::TextFormat::ParseFieldValueFromString
static bool ParseFieldValueFromString(const std::string &input, const FieldDescriptor *field, Message *message)
Definition: bloaty/third_party/protobuf/src/google/protobuf/text_format.cc:2411
google::protobuf::text_format_unittest::TextFormatTest::SetUpTestSuite
static void SetUpTestSuite()
Definition: protobuf/src/google/protobuf/text_format_unittest.cc:91
google::protobuf::int32
int32_t int32
Definition: third_party/bloaty/third_party/protobuf/src/google/protobuf/stubs/port.h:150
google::protobuf::TextFormat::Parser::WriteLocationsTo
void WriteLocationsTo(ParseInfoTree *tree)
Definition: bloaty/third_party/protobuf/src/google/protobuf/text_format.h:538
google::protobuf::TextFormat::ParseInfoTree::GetTreeForNested
ParseInfoTree * GetTreeForNested(const FieldDescriptor *field, int index) const
Definition: bloaty/third_party/protobuf/src/google/protobuf/text_format.cc:171
google::protobuf::UnknownFieldSet::AddGroup
UnknownFieldSet * AddGroup(int number)
Definition: bloaty/third_party/protobuf/src/google/protobuf/unknown_field_set.cc:169
asyncio_get_stats.parser
parser
Definition: asyncio_get_stats.py:34
gen_server_registered_method_bad_client_test_body.text
def text
Definition: gen_server_registered_method_bad_client_test_body.py:50
errors
const char * errors
Definition: bloaty/third_party/protobuf/src/google/protobuf/io/tokenizer_unittest.cc:841
ASSERT_LT
#define ASSERT_LT(val1, val2)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2068
int64_t
signed __int64 int64_t
Definition: stdint-msvc2008.h:89
google::protobuf::TextFormat::ParseInfoTree
Definition: bloaty/third_party/protobuf/src/google/protobuf/text_format.h:469
max
int max
Definition: bloaty/third_party/zlib/examples/enough.c:170
gmock_output_test.output
output
Definition: bloaty/third_party/googletest/googlemock/test/gmock_output_test.py:175
google::protobuf::text_format_unittest::TextFormatExtensionsTest::proto_
unittest::TestAllExtensions proto_
Definition: bloaty/third_party/protobuf/src/google/protobuf/text_format_unittest.cc:121
google::protobuf::File::GetContents
static bool GetContents(const string &name, string *output, bool)
Definition: bloaty/third_party/protobuf/src/google/protobuf/testing/file.h:83
google::protobuf::TextFormat::BaseTextGenerator
Definition: bloaty/third_party/protobuf/src/google/protobuf/text_format.h:100
google::protobuf::text_format_unittest::TextFormatParserTest::MockErrorCollector
Definition: bloaty/third_party/protobuf/src/google/protobuf/text_format_unittest.cc:1359
google::protobuf::TestUtil::GetTestDataPath
std::string GetTestDataPath(const std::string &google3_path)
Definition: bloaty/third_party/protobuf/src/google/protobuf/test_util2.h:65
setup.v
v
Definition: third_party/bloaty/third_party/capstone/bindings/python/setup.py:42
google::protobuf::Message::DebugString
std::string DebugString() const
Definition: bloaty/third_party/protobuf/src/google/protobuf/text_format.cc:84
google::protobuf::text_format_unittest::TextFormatTest::proto_debug_string_
const std::string proto_debug_string_
Definition: bloaty/third_party/protobuf/src/google/protobuf/text_format_unittest.cc:97
google::protobuf::TextFormat::ParseLocation::line
int line
Definition: bloaty/third_party/protobuf/src/google/protobuf/text_format.h:459
google::protobuf::text_format_unittest::CustomUInt32FieldValuePrinter::PrintUInt32
virtual std::string PrintUInt32(uint32 val) const
Definition: protobuf/src/google/protobuf/text_format_unittest.cc:459
google::protobuf::TextFormat::ParseFromString
static bool ParseFromString(const std::string &input, Message *output)
Definition: bloaty/third_party/protobuf/src/google/protobuf/text_format.cc:1485
google::protobuf::TextFormat::Printer::SetDefaultFieldValuePrinter
void SetDefaultFieldValuePrinter(const FastFieldValuePrinter *printer)
Definition: bloaty/third_party/protobuf/src/google/protobuf/text_format.cc:1809
uint64_t
unsigned __int64 uint64_t
Definition: stdint-msvc2008.h:90
google::protobuf::TextFormat::Printer
Definition: bloaty/third_party/protobuf/src/google/protobuf/text_format.h:236
google::protobuf::text_format_unittest::TextFormatParserTest::ExpectMessage
void ExpectMessage(const std::string &input, const std::string &message, int line, int col, Message *proto, bool expected_result)
Definition: bloaty/third_party/protobuf/src/google/protobuf/text_format_unittest.cc:1326
google::protobuf::text_format_unittest::CustomMessageFieldValuePrinter::PrintInt32
virtual std::string PrintInt32(int32 v) const
Definition: protobuf/src/google/protobuf/text_format_unittest.cc:534
google::protobuf::UnknownFieldSet::AddVarint
void AddVarint(int number, uint64 value)
Definition: bloaty/third_party/protobuf/src/google/protobuf/unknown_field_set.cc:135
google::protobuf.internal::WireFormatLite::LengthDelimitedSize
static size_t LengthDelimitedSize(size_t length)
Definition: bloaty/third_party/protobuf/src/google/protobuf/wire_format_lite.h:1798
google::protobuf::text_format_unittest::TextFormatSilentMarkerTest::SetUp
void SetUp() override
Definition: protobuf/src/google/protobuf/text_format_unittest.cc:2255
google::protobuf::TextFormat::Printer::SetExpandAny
void SetExpandAny(bool expand)
Definition: bloaty/third_party/protobuf/src/google/protobuf/text_format.h:321
google::protobuf::TextFormat::ParseLocationRange
Definition: protobuf/src/google/protobuf/text_format.h:500
google::protobuf::TextFormat::FastFieldValuePrinter::PrintString
virtual void PrintString(const std::string &val, BaseTextGenerator *generator) const
Definition: bloaty/third_party/protobuf/src/google/protobuf/text_format.cc:1634
google::protobuf::text_format_unittest::CompactRepeatedFieldPrinter::PrintMessageEnd
void PrintMessageEnd(const Message &message, int field_index, int field_count, bool single_line_mode, TextFormat::BaseTextGenerator *generator) const override
Definition: protobuf/src/google/protobuf/text_format_unittest.cc:689
google::protobuf::text_format_unittest::TEST
TEST(TextFormatUnknownFieldTest, TestUnknownField)
Definition: bloaty/third_party/protobuf/src/google/protobuf/text_format_unittest.cc:1890
google::protobuf::uint64
uint64_t uint64
Definition: third_party/bloaty/third_party/protobuf/src/google/protobuf/stubs/port.h:156
tests.google.protobuf.internal.test_util.ExpectAllFieldsSet
def ExpectAllFieldsSet(test_case, message)
Definition: bloaty/third_party/protobuf/python/compatibility_tests/v2.5.0/tests/google/protobuf/internal/test_util.py:367
data
char data[kBufferLength]
Definition: abseil-cpp/absl/strings/internal/str_format/float_conversion.cc:1006
google::protobuf::strings::Substitute
string Substitute(const char *format, const SubstituteArg &arg0, const SubstituteArg &arg1, const SubstituteArg &arg2, const SubstituteArg &arg3, const SubstituteArg &arg4, const SubstituteArg &arg5, const SubstituteArg &arg6, const SubstituteArg &arg7, const SubstituteArg &arg8, const SubstituteArg &arg9)
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/substitute.cc:55
buffer
char buffer[1024]
Definition: libuv/docs/code/idle-compute/main.c:8
google::protobuf::text_format_unittest::TextFormatParserTest
Definition: bloaty/third_party/protobuf/src/google/protobuf/text_format_unittest.cc:1313
google::protobuf::TextFormat::Parser::RecordErrorsTo
void RecordErrorsTo(io::ErrorCollector *error_collector)
Definition: bloaty/third_party/protobuf/src/google/protobuf/text_format.h:527
log
Definition: bloaty/third_party/zlib/examples/gzlog.c:289
google::protobuf::TextFormat::BaseTextGenerator::Indent
virtual void Indent()
Definition: bloaty/third_party/protobuf/src/google/protobuf/text_format.h:104
google::protobuf::TextFormat::ParseInfoTree::GetLocation
ParseLocation GetLocation(const FieldDescriptor *field, int index) const
Definition: bloaty/third_party/protobuf/src/google/protobuf/text_format.cc:155
google::protobuf::ERROR
static const LogLevel ERROR
Definition: bloaty/third_party/protobuf/src/google/protobuf/testing/googletest.h:70
google::protobuf::TextFormat::FastFieldValuePrinter
Definition: bloaty/third_party/protobuf/src/google/protobuf/text_format.h:124
google::protobuf::text_format_unittest::TextFormatParserTest::ExpectFailure
void ExpectFailure(const std::string &input, const std::string &message, int line, int col, Message *proto)
Definition: protobuf/src/google/protobuf/text_format_unittest.cc:1390
google::protobuf::io::CodedOutputStream
Definition: bloaty/third_party/protobuf/src/google/protobuf/io/coded_stream.h:1044
google::protobuf::text_format_unittest::TextFormatParserTest::ExpectSuccessAndTree
void ExpectSuccessAndTree(const std::string &input, Message *proto, TextFormat::ParseInfoTree *info_tree)
Definition: protobuf/src/google/protobuf/text_format_unittest.cc:1406
google::protobuf.internal::enable_debug_text_format_marker
PROTOBUF_EXPORT std::atomic< bool > enable_debug_text_format_marker
Definition: protobuf/src/google/protobuf/text_format.cc:90
google::protobuf::Message
Definition: bloaty/third_party/protobuf/src/google/protobuf/message.h:205
field
const FieldDescriptor * field
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/parser_unittest.cc:2692
google::protobuf::io::StringOutputStream
Definition: bloaty/third_party/protobuf/src/google/protobuf/io/zero_copy_stream_impl_lite.h:132
google::protobuf::text_format_unittest::TextFormatExtensionsTest::static_proto_debug_string_
static std::string static_proto_debug_string_
Definition: bloaty/third_party/protobuf/src/google/protobuf/text_format_unittest.cc:124
google::protobuf.internal::WireFormatLite::TagSize
static size_t TagSize(int field_number, WireFormatLite::FieldType type)
Definition: bloaty/third_party/protobuf/src/google/protobuf/wire_format_lite.h:796
google::protobuf::StringReplace
void StringReplace(const string &s, const string &oldsub, const string &newsub, bool replace_all, string *res)
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/strutil.cc:148
google::protobuf::text_format_unittest::kEscapeTestStringEscaped
const std::string kEscapeTestStringEscaped
Definition: bloaty/third_party/protobuf/src/google/protobuf/text_format_unittest.cc:78
google::protobuf::TextFormat::Printer::SetInitialIndentLevel
void SetInitialIndentLevel(int indent_level)
Definition: bloaty/third_party/protobuf/src/google/protobuf/text_format.h:257
google::protobuf::text_format_unittest::TextFormatParserTest::ExpectFailure
void ExpectFailure(const std::string &input, const std::string &message, int line, int col)
Definition: protobuf/src/google/protobuf/text_format_unittest.cc:1384
google::protobuf::TextFormat::BaseTextGenerator::PrintString
void PrintString(const std::string &str)
Definition: bloaty/third_party/protobuf/src/google/protobuf/text_format.h:112
google::protobuf.internal::WireFormatLite::WriteTag
static PROTOBUF_ALWAYS_INLINE void WriteTag(int field_number, WireType type, io::CodedOutputStream *output)
Definition: bloaty/third_party/protobuf/src/google/protobuf/wire_format_lite.h:1289
google::protobuf::text_format_unittest::MultilineStringPrinter::PrintString
void PrintString(const std::string &val, TextFormat::BaseTextGenerator *generator) const override
Definition: protobuf/src/google/protobuf/text_format_unittest.cc:732
index
int index
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/protobuf.h:1184
google::protobuf::TextFormat::PrintToString
static bool PrintToString(const Message &message, std::string *output)
Definition: bloaty/third_party/protobuf/src/google/protobuf/text_format.cc:2395
google::protobuf::io::ErrorCollector
Definition: bloaty/third_party/protobuf/src/google/protobuf/io/tokenizer.h:66
google::protobuf::UnknownFieldSet
Definition: bloaty/third_party/protobuf/src/google/protobuf/unknown_field_set.h:81
google::protobuf::TextFormat::Parse
static bool Parse(io::ZeroCopyInputStream *input, Message *output)
Definition: bloaty/third_party/protobuf/src/google/protobuf/text_format.cc:1475
regen-readme.line
line
Definition: regen-readme.py:30
ASSERT_TRUE
#define ASSERT_TRUE(condition)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1973
ASSERT_FALSE
#define ASSERT_FALSE(condition)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1976
google::protobuf::CleanStringLineEndings
void CleanStringLineEndings(const string &src, string *dst, bool auto_end_last_line)
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/strutil.cc:2343
google::protobuf::Descriptor
Definition: bloaty/third_party/protobuf/src/google/protobuf/descriptor.h:231
log
bool log
Definition: abseil-cpp/absl/synchronization/mutex.cc:310
Any::PackFrom
void PackFrom(const ::PROTOBUF_NAMESPACE_ID::Message &message)
Definition: bloaty/third_party/protobuf/src/google/protobuf/any.pb.cc:88
input
std::string input
Definition: bloaty/third_party/protobuf/src/google/protobuf/io/tokenizer_unittest.cc:197
EXPECT_BOOL_FIELD
#define EXPECT_BOOL_FIELD(name, value, valuestring)
EXPECT_TRUE
#define EXPECT_TRUE(condition)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1967
EXPECT_INVALID
#define EXPECT_INVALID(name, valuestring)
google::protobuf::text_format_unittest::TextFormatTest
Definition: bloaty/third_party/protobuf/src/google/protobuf/text_format_unittest.cc:82
EXPECT_DOUBLE_FIELD
#define EXPECT_DOUBLE_FIELD(name, value, valuestring)
google::protobuf::TextFormat::BaseTextGenerator::Outdent
virtual void Outdent()
Definition: bloaty/third_party/protobuf/src/google/protobuf/text_format.h:105
google::protobuf::text_format_unittest::TextFormatTest::static_proto_debug_string_
static std::string static_proto_debug_string_
Definition: bloaty/third_party/protobuf/src/google/protobuf/text_format_unittest.cc:101
google::protobuf::text_format_unittest::TextFormatParserTest::MockErrorCollector::MockErrorCollector
MockErrorCollector()
Definition: protobuf/src/google/protobuf/text_format_unittest.cc:1435
internal
Definition: benchmark/test/output_test_helper.cc:20
google::protobuf::text_format_unittest::CustomNestedMessagePrinter::CustomNestedMessagePrinter
CustomNestedMessagePrinter()
Definition: protobuf/src/google/protobuf/text_format_unittest.cc:777
google::protobuf::text_format_unittest::CustomInt32FieldValuePrinter::PrintInt32
virtual std::string PrintInt32(int32 val) const
Definition: protobuf/src/google/protobuf/text_format_unittest.cc:483
google::protobuf::TextFormat::ParseInfoTree::GetLocationRange
ParseLocationRange GetLocationRange(const FieldDescriptor *field, int index) const
Definition: protobuf/src/google/protobuf/text_format.cc:170
descriptors
HashTable descriptors
Definition: protobuf/php/ext/google/protobuf/protobuf.c:82
Any
Definition: bloaty/third_party/protobuf/src/google/protobuf/any.pb.h:70
google::protobuf::TextFormat::FieldValuePrinter
Definition: protobuf/src/google/protobuf/text_format.h:169
parser_
std::unique_ptr< Parser > parser_
Definition: bloaty/third_party/protobuf/src/google/protobuf/compiler/parser_unittest.cc:185
google::protobuf::text_format_unittest::TextFormatSilentMarkerTest
Definition: protobuf/src/google/protobuf/text_format_unittest.cc:2253
descriptor
static const char descriptor[1336]
Definition: certs.upbdefs.c:16
testing::HasSubstr
PolymorphicMatcher< internal::HasSubstrMatcher< internal::string > > HasSubstr(const internal::string &substring)
Definition: cares/cares/test/gmock-1.8.0/gmock/gmock.h:8803
google::protobuf::io::CodedOutputStream::WriteVarint32
void WriteVarint32(uint32 value)
Definition: bloaty/third_party/protobuf/src/google/protobuf/io/coded_stream.h:1638
google
Definition: bloaty/third_party/protobuf/benchmarks/util/data_proto2_to_proto3_util.h:11
google::protobuf::text_format_unittest::RemoveRedundantZeros
static std::string RemoveRedundantZeros(std::string text)
Definition: bloaty/third_party/protobuf/src/google/protobuf/text_format_unittest.cc:983
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
GOOGLE_CHECK_OK
#define GOOGLE_CHECK_OK(A)
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/logging.h:155
google::protobuf::TextFormat::Printer::SetUseShortRepeatedPrimitives
void SetUseShortRepeatedPrimitives(bool use_short_repeated_primitives)
Definition: bloaty/third_party/protobuf/src/google/protobuf/text_format.h:279
ASSERT_EQ
#define ASSERT_EQ(val1, val2)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2056
google::protobuf::strings::SubstituteAndAppend
void SubstituteAndAppend(string *output, const char *format, const SubstituteArg &arg0, const SubstituteArg &arg1, const SubstituteArg &arg2, const SubstituteArg &arg3, const SubstituteArg &arg4, const SubstituteArg &arg5, const SubstituteArg &arg6, const SubstituteArg &arg7, const SubstituteArg &arg8, const SubstituteArg &arg9)
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/substitute.cc:68
google::protobuf::TextFormat::ParseLocation::column
int column
Definition: bloaty/third_party/protobuf/src/google/protobuf/text_format.h:460


grpc
Author(s):
autogenerated on Fri May 16 2025 03:00:35