text_format_unittest.cc 64 KB

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