/thirdparty/breakpad/third_party/protobuf/protobuf/src/google/protobuf/text_format_unittest.cc

http://github.com/tomahawk-player/tomahawk · C++ · 1141 lines · 826 code · 173 blank · 142 comment · 1 complexity · dd983dd2fb2bd33d6203ad596bb7eaab MD5 · raw file

  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // http://code.google.com/p/protobuf/
  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 <math.h>
  34. #include <stdlib.h>
  35. #include <limits>
  36. #include <google/protobuf/text_format.h>
  37. #include <google/protobuf/io/zero_copy_stream_impl.h>
  38. #include <google/protobuf/io/tokenizer.h>
  39. #include <google/protobuf/unittest.pb.h>
  40. #include <google/protobuf/unittest_mset.pb.h>
  41. #include <google/protobuf/test_util.h>
  42. #include <google/protobuf/stubs/common.h>
  43. #include <google/protobuf/testing/file.h>
  44. #include <google/protobuf/testing/googletest.h>
  45. #include <gtest/gtest.h>
  46. #include <google/protobuf/stubs/strutil.h>
  47. #include <google/protobuf/stubs/substitute.h>
  48. namespace google {
  49. namespace protobuf {
  50. // Can't use an anonymous namespace here due to brokenness of Tru64 compiler.
  51. namespace text_format_unittest {
  52. inline bool IsNaN(double value) {
  53. // NaN is never equal to anything, even itself.
  54. return value != value;
  55. }
  56. // A basic string with different escapable characters for testing.
  57. const string kEscapeTestString =
  58. "\"A string with ' characters \n and \r newlines and \t tabs and \001 "
  59. "slashes \\ and multiple spaces";
  60. // A representation of the above string with all the characters escaped.
  61. const string kEscapeTestStringEscaped =
  62. "\"\\\"A string with \\' characters \\n and \\r newlines "
  63. "and \\t tabs and \\001 slashes \\\\ and multiple spaces\"";
  64. class TextFormatTest : public testing::Test {
  65. public:
  66. static void SetUpTestCase() {
  67. File::ReadFileToStringOrDie(
  68. TestSourceDir()
  69. + "/google/protobuf/testdata/text_format_unittest_data.txt",
  70. &static_proto_debug_string_);
  71. }
  72. TextFormatTest() : proto_debug_string_(static_proto_debug_string_) {}
  73. protected:
  74. // Debug string read from text_format_unittest_data.txt.
  75. const string proto_debug_string_;
  76. unittest::TestAllTypes proto_;
  77. private:
  78. static string static_proto_debug_string_;
  79. };
  80. string TextFormatTest::static_proto_debug_string_;
  81. class TextFormatExtensionsTest : public testing::Test {
  82. public:
  83. static void SetUpTestCase() {
  84. File::ReadFileToStringOrDie(
  85. TestSourceDir()
  86. + "/google/protobuf/testdata/"
  87. "text_format_unittest_extensions_data.txt",
  88. &static_proto_debug_string_);
  89. }
  90. TextFormatExtensionsTest()
  91. : proto_debug_string_(static_proto_debug_string_) {}
  92. protected:
  93. // Debug string read from text_format_unittest_data.txt.
  94. const string proto_debug_string_;
  95. unittest::TestAllExtensions proto_;
  96. private:
  97. static string static_proto_debug_string_;
  98. };
  99. string TextFormatExtensionsTest::static_proto_debug_string_;
  100. TEST_F(TextFormatTest, Basic) {
  101. TestUtil::SetAllFields(&proto_);
  102. EXPECT_EQ(proto_debug_string_, proto_.DebugString());
  103. }
  104. TEST_F(TextFormatExtensionsTest, Extensions) {
  105. TestUtil::SetAllExtensions(&proto_);
  106. EXPECT_EQ(proto_debug_string_, proto_.DebugString());
  107. }
  108. TEST_F(TextFormatTest, ShortDebugString) {
  109. proto_.set_optional_int32(1);
  110. proto_.set_optional_string("hello");
  111. proto_.mutable_optional_nested_message()->set_bb(2);
  112. proto_.mutable_optional_foreign_message();
  113. EXPECT_EQ("optional_int32: 1 optional_string: \"hello\" "
  114. "optional_nested_message { bb: 2 } "
  115. "optional_foreign_message { }",
  116. proto_.ShortDebugString());
  117. }
  118. TEST_F(TextFormatTest, ShortPrimitiveRepeateds) {
  119. proto_.set_optional_int32(123);
  120. proto_.add_repeated_int32(456);
  121. proto_.add_repeated_int32(789);
  122. proto_.add_repeated_string("foo");
  123. proto_.add_repeated_string("bar");
  124. proto_.add_repeated_nested_message()->set_bb(2);
  125. proto_.add_repeated_nested_message()->set_bb(3);
  126. proto_.add_repeated_nested_enum(unittest::TestAllTypes::FOO);
  127. proto_.add_repeated_nested_enum(unittest::TestAllTypes::BAR);
  128. TextFormat::Printer printer;
  129. printer.SetUseShortRepeatedPrimitives(true);
  130. string text;
  131. printer.PrintToString(proto_, &text);
  132. EXPECT_EQ("optional_int32: 123\n"
  133. "repeated_int32: [456, 789]\n"
  134. "repeated_string: \"foo\"\n"
  135. "repeated_string: \"bar\"\n"
  136. "repeated_nested_message {\n bb: 2\n}\n"
  137. "repeated_nested_message {\n bb: 3\n}\n"
  138. "repeated_nested_enum: [FOO, BAR]\n",
  139. text);
  140. // Try in single-line mode.
  141. printer.SetSingleLineMode(true);
  142. printer.PrintToString(proto_, &text);
  143. EXPECT_EQ("optional_int32: 123 "
  144. "repeated_int32: [456, 789] "
  145. "repeated_string: \"foo\" "
  146. "repeated_string: \"bar\" "
  147. "repeated_nested_message { bb: 2 } "
  148. "repeated_nested_message { bb: 3 } "
  149. "repeated_nested_enum: [FOO, BAR] ",
  150. text);
  151. }
  152. TEST_F(TextFormatTest, StringEscape) {
  153. // Set the string value to test.
  154. proto_.set_optional_string(kEscapeTestString);
  155. // Get the DebugString from the proto.
  156. string debug_string = proto_.DebugString();
  157. string utf8_debug_string = proto_.Utf8DebugString();
  158. // Hardcode a correct value to test against.
  159. string correct_string = "optional_string: "
  160. + kEscapeTestStringEscaped
  161. + "\n";
  162. // Compare.
  163. EXPECT_EQ(correct_string, debug_string);
  164. // UTF-8 string is the same as non-UTF-8 because
  165. // the protocol buffer contains no UTF-8 text.
  166. EXPECT_EQ(correct_string, utf8_debug_string);
  167. string expected_short_debug_string = "optional_string: "
  168. + kEscapeTestStringEscaped;
  169. EXPECT_EQ(expected_short_debug_string, proto_.ShortDebugString());
  170. }
  171. TEST_F(TextFormatTest, Utf8DebugString) {
  172. // Set the string value to test.
  173. proto_.set_optional_string("\350\260\267\346\255\214");
  174. // Get the DebugString from the proto.
  175. string debug_string = proto_.DebugString();
  176. string utf8_debug_string = proto_.Utf8DebugString();
  177. // Hardcode a correct value to test against.
  178. string correct_utf8_string = "optional_string: "
  179. "\"\350\260\267\346\255\214\""
  180. "\n";
  181. string correct_string = "optional_string: "
  182. "\"\\350\\260\\267\\346\\255\\214\""
  183. "\n";
  184. // Compare.
  185. EXPECT_EQ(correct_utf8_string, utf8_debug_string);
  186. EXPECT_EQ(correct_string, debug_string);
  187. }
  188. TEST_F(TextFormatTest, PrintUnknownFields) {
  189. // Test printing of unknown fields in a message.
  190. unittest::TestEmptyMessage message;
  191. UnknownFieldSet* unknown_fields = message.mutable_unknown_fields();
  192. unknown_fields->AddVarint(5, 1);
  193. unknown_fields->AddFixed32(5, 2);
  194. unknown_fields->AddFixed64(5, 3);
  195. unknown_fields->AddLengthDelimited(5, "4");
  196. unknown_fields->AddGroup(5)->AddVarint(10, 5);
  197. unknown_fields->AddVarint(8, 1);
  198. unknown_fields->AddVarint(8, 2);
  199. unknown_fields->AddVarint(8, 3);
  200. EXPECT_EQ(
  201. "5: 1\n"
  202. "5: 0x00000002\n"
  203. "5: 0x0000000000000003\n"
  204. "5: \"4\"\n"
  205. "5 {\n"
  206. " 10: 5\n"
  207. "}\n"
  208. "8: 1\n"
  209. "8: 2\n"
  210. "8: 3\n",
  211. message.DebugString());
  212. }
  213. TEST_F(TextFormatTest, PrintUnknownMessage) {
  214. // Test heuristic printing of messages in an UnknownFieldSet.
  215. protobuf_unittest::TestAllTypes message;
  216. // Cases which should not be interpreted as sub-messages.
  217. // 'a' is a valid FIXED64 tag, so for the string to be parseable as a message
  218. // it should be followed by 8 bytes. Since this string only has two
  219. // subsequent bytes, it should be treated as a string.
  220. message.add_repeated_string("abc");
  221. // 'd' happens to be a valid ENDGROUP tag. So,
  222. // UnknownFieldSet::MergeFromCodedStream() will successfully parse "def", but
  223. // the ConsumedEntireMessage() check should fail.
  224. message.add_repeated_string("def");
  225. // A zero-length string should never be interpreted as a message even though
  226. // it is technically valid as one.
  227. message.add_repeated_string("");
  228. // Case which should be interpreted as a sub-message.
  229. // An actual nested message with content should always be interpreted as a
  230. // nested message.
  231. message.add_repeated_nested_message()->set_bb(123);
  232. string data;
  233. message.SerializeToString(&data);
  234. string text;
  235. UnknownFieldSet unknown_fields;
  236. EXPECT_TRUE(unknown_fields.ParseFromString(data));
  237. EXPECT_TRUE(TextFormat::PrintUnknownFieldsToString(unknown_fields, &text));
  238. EXPECT_EQ(
  239. "44: \"abc\"\n"
  240. "44: \"def\"\n"
  241. "44: \"\"\n"
  242. "48 {\n"
  243. " 1: 123\n"
  244. "}\n",
  245. text);
  246. }
  247. TEST_F(TextFormatTest, PrintMessageWithIndent) {
  248. // Test adding an initial indent to printing.
  249. protobuf_unittest::TestAllTypes message;
  250. message.add_repeated_string("abc");
  251. message.add_repeated_string("def");
  252. message.add_repeated_nested_message()->set_bb(123);
  253. string text;
  254. TextFormat::Printer printer;
  255. printer.SetInitialIndentLevel(1);
  256. EXPECT_TRUE(printer.PrintToString(message, &text));
  257. EXPECT_EQ(
  258. " repeated_string: \"abc\"\n"
  259. " repeated_string: \"def\"\n"
  260. " repeated_nested_message {\n"
  261. " bb: 123\n"
  262. " }\n",
  263. text);
  264. }
  265. TEST_F(TextFormatTest, PrintMessageSingleLine) {
  266. // Test printing a message on a single line.
  267. protobuf_unittest::TestAllTypes message;
  268. message.add_repeated_string("abc");
  269. message.add_repeated_string("def");
  270. message.add_repeated_nested_message()->set_bb(123);
  271. string text;
  272. TextFormat::Printer printer;
  273. printer.SetInitialIndentLevel(1);
  274. printer.SetSingleLineMode(true);
  275. EXPECT_TRUE(printer.PrintToString(message, &text));
  276. EXPECT_EQ(
  277. " repeated_string: \"abc\" repeated_string: \"def\" "
  278. "repeated_nested_message { bb: 123 } ",
  279. text);
  280. }
  281. TEST_F(TextFormatTest, ParseBasic) {
  282. io::ArrayInputStream input_stream(proto_debug_string_.data(),
  283. proto_debug_string_.size());
  284. TextFormat::Parse(&input_stream, &proto_);
  285. TestUtil::ExpectAllFieldsSet(proto_);
  286. }
  287. TEST_F(TextFormatExtensionsTest, ParseExtensions) {
  288. io::ArrayInputStream input_stream(proto_debug_string_.data(),
  289. proto_debug_string_.size());
  290. TextFormat::Parse(&input_stream, &proto_);
  291. TestUtil::ExpectAllExtensionsSet(proto_);
  292. }
  293. TEST_F(TextFormatTest, ParseEnumFieldFromNumber) {
  294. // Create a parse string with a numerical value for an enum field.
  295. string parse_string = strings::Substitute("optional_nested_enum: $0",
  296. unittest::TestAllTypes::BAZ);
  297. EXPECT_TRUE(TextFormat::ParseFromString(parse_string, &proto_));
  298. EXPECT_TRUE(proto_.has_optional_nested_enum());
  299. EXPECT_EQ(unittest::TestAllTypes::BAZ, proto_.optional_nested_enum());
  300. }
  301. TEST_F(TextFormatTest, ParseEnumFieldFromNegativeNumber) {
  302. ASSERT_LT(unittest::SPARSE_E, 0);
  303. string parse_string = strings::Substitute("sparse_enum: $0",
  304. unittest::SPARSE_E);
  305. unittest::SparseEnumMessage proto;
  306. EXPECT_TRUE(TextFormat::ParseFromString(parse_string, &proto));
  307. EXPECT_TRUE(proto.has_sparse_enum());
  308. EXPECT_EQ(unittest::SPARSE_E, proto.sparse_enum());
  309. }
  310. TEST_F(TextFormatTest, ParseStringEscape) {
  311. // Create a parse string with escpaed characters in it.
  312. string parse_string = "optional_string: "
  313. + kEscapeTestStringEscaped
  314. + "\n";
  315. io::ArrayInputStream input_stream(parse_string.data(),
  316. parse_string.size());
  317. TextFormat::Parse(&input_stream, &proto_);
  318. // Compare.
  319. EXPECT_EQ(kEscapeTestString, proto_.optional_string());
  320. }
  321. TEST_F(TextFormatTest, ParseConcatenatedString) {
  322. // Create a parse string with multiple parts on one line.
  323. string parse_string = "optional_string: \"foo\" \"bar\"\n";
  324. io::ArrayInputStream input_stream1(parse_string.data(),
  325. parse_string.size());
  326. TextFormat::Parse(&input_stream1, &proto_);
  327. // Compare.
  328. EXPECT_EQ("foobar", proto_.optional_string());
  329. // Create a parse string with multiple parts on seperate lines.
  330. parse_string = "optional_string: \"foo\"\n"
  331. "\"bar\"\n";
  332. io::ArrayInputStream input_stream2(parse_string.data(),
  333. parse_string.size());
  334. TextFormat::Parse(&input_stream2, &proto_);
  335. // Compare.
  336. EXPECT_EQ("foobar", proto_.optional_string());
  337. }
  338. TEST_F(TextFormatTest, ParseFloatWithSuffix) {
  339. // Test that we can parse a floating-point value with 'f' appended to the
  340. // end. This is needed for backwards-compatibility with proto1.
  341. // Have it parse a float with the 'f' suffix.
  342. string parse_string = "optional_float: 1.0f\n";
  343. io::ArrayInputStream input_stream(parse_string.data(),
  344. parse_string.size());
  345. TextFormat::Parse(&input_stream, &proto_);
  346. // Compare.
  347. EXPECT_EQ(1.0, proto_.optional_float());
  348. }
  349. TEST_F(TextFormatTest, ParseShortRepeatedForm) {
  350. string parse_string =
  351. // Mixed short-form and long-form are simply concatenated.
  352. "repeated_int32: 1\n"
  353. "repeated_int32: [456, 789]\n"
  354. "repeated_nested_enum: [ FOO ,BAR, # comment\n"
  355. " 3]\n"
  356. // Note that while the printer won't print repeated strings in short-form,
  357. // the parser will accept them.
  358. "repeated_string: [ \"foo\", 'bar' ]\n";
  359. ASSERT_TRUE(TextFormat::ParseFromString(parse_string, &proto_));
  360. ASSERT_EQ(3, proto_.repeated_int32_size());
  361. EXPECT_EQ(1, proto_.repeated_int32(0));
  362. EXPECT_EQ(456, proto_.repeated_int32(1));
  363. EXPECT_EQ(789, proto_.repeated_int32(2));
  364. ASSERT_EQ(3, proto_.repeated_nested_enum_size());
  365. EXPECT_EQ(unittest::TestAllTypes::FOO, proto_.repeated_nested_enum(0));
  366. EXPECT_EQ(unittest::TestAllTypes::BAR, proto_.repeated_nested_enum(1));
  367. EXPECT_EQ(unittest::TestAllTypes::BAZ, proto_.repeated_nested_enum(2));
  368. ASSERT_EQ(2, proto_.repeated_string_size());
  369. EXPECT_EQ("foo", proto_.repeated_string(0));
  370. EXPECT_EQ("bar", proto_.repeated_string(1));
  371. }
  372. TEST_F(TextFormatTest, Comments) {
  373. // Test that comments are ignored.
  374. string parse_string = "optional_int32: 1 # a comment\n"
  375. "optional_int64: 2 # another comment";
  376. io::ArrayInputStream input_stream(parse_string.data(),
  377. parse_string.size());
  378. TextFormat::Parse(&input_stream, &proto_);
  379. // Compare.
  380. EXPECT_EQ(1, proto_.optional_int32());
  381. EXPECT_EQ(2, proto_.optional_int64());
  382. }
  383. TEST_F(TextFormatTest, OptionalColon) {
  384. // Test that we can place a ':' after the field name of a nested message,
  385. // even though we don't have to.
  386. string parse_string = "optional_nested_message: { bb: 1}\n";
  387. io::ArrayInputStream input_stream(parse_string.data(),
  388. parse_string.size());
  389. TextFormat::Parse(&input_stream, &proto_);
  390. // Compare.
  391. EXPECT_TRUE(proto_.has_optional_nested_message());
  392. EXPECT_EQ(1, proto_.optional_nested_message().bb());
  393. }
  394. // Some platforms (e.g. Windows) insist on padding the exponent to three
  395. // digits when one or two would be just fine.
  396. static string RemoveRedundantZeros(string text) {
  397. text = StringReplace(text, "e+0", "e+", true);
  398. text = StringReplace(text, "e-0", "e-", true);
  399. return text;
  400. }
  401. TEST_F(TextFormatTest, PrintExotic) {
  402. unittest::TestAllTypes message;
  403. // Note: In C, a negative integer literal is actually the unary negation
  404. // operator being applied to a positive integer literal, and
  405. // 9223372036854775808 is outside the range of int64. However, it is not
  406. // outside the range of uint64. Confusingly, this means that everything
  407. // works if we make the literal unsigned, even though we are negating it.
  408. message.add_repeated_int64(-GOOGLE_ULONGLONG(9223372036854775808));
  409. message.add_repeated_uint64(GOOGLE_ULONGLONG(18446744073709551615));
  410. message.add_repeated_double(123.456);
  411. message.add_repeated_double(1.23e21);
  412. message.add_repeated_double(1.23e-18);
  413. message.add_repeated_double(std::numeric_limits<double>::infinity());
  414. message.add_repeated_double(-std::numeric_limits<double>::infinity());
  415. message.add_repeated_double(std::numeric_limits<double>::quiet_NaN());
  416. message.add_repeated_string(string("\000\001\a\b\f\n\r\t\v\\\'\"", 12));
  417. // Fun story: We used to use 1.23e22 instead of 1.23e21 above, but this
  418. // seemed to trigger an odd case on MinGW/GCC 3.4.5 where GCC's parsing of
  419. // the value differed from strtod()'s parsing. That is to say, the
  420. // following assertion fails on MinGW:
  421. // assert(1.23e22 == strtod("1.23e22", NULL));
  422. // As a result, SimpleDtoa() would print the value as
  423. // "1.2300000000000001e+22" to make sure strtod() produce the exact same
  424. // result. Our goal is to test runtime parsing, not compile-time parsing,
  425. // so this wasn't our problem. It was found that using 1.23e21 did not
  426. // have this problem, so we switched to that instead.
  427. EXPECT_EQ(
  428. "repeated_int64: -9223372036854775808\n"
  429. "repeated_uint64: 18446744073709551615\n"
  430. "repeated_double: 123.456\n"
  431. "repeated_double: 1.23e+21\n"
  432. "repeated_double: 1.23e-18\n"
  433. "repeated_double: inf\n"
  434. "repeated_double: -inf\n"
  435. "repeated_double: nan\n"
  436. "repeated_string: \"\\000\\001\\007\\010\\014\\n\\r\\t\\013\\\\\\'\\\"\"\n",
  437. RemoveRedundantZeros(message.DebugString()));
  438. }
  439. TEST_F(TextFormatTest, PrintFloatPrecision) {
  440. unittest::TestAllTypes message;
  441. message.add_repeated_float(1.2);
  442. message.add_repeated_float(1.23);
  443. message.add_repeated_float(1.234);
  444. message.add_repeated_float(1.2345);
  445. message.add_repeated_float(1.23456);
  446. message.add_repeated_float(1.2e10);
  447. message.add_repeated_float(1.23e10);
  448. message.add_repeated_float(1.234e10);
  449. message.add_repeated_float(1.2345e10);
  450. message.add_repeated_float(1.23456e10);
  451. message.add_repeated_double(1.2);
  452. message.add_repeated_double(1.23);
  453. message.add_repeated_double(1.234);
  454. message.add_repeated_double(1.2345);
  455. message.add_repeated_double(1.23456);
  456. message.add_repeated_double(1.234567);
  457. message.add_repeated_double(1.2345678);
  458. message.add_repeated_double(1.23456789);
  459. message.add_repeated_double(1.234567898);
  460. message.add_repeated_double(1.2345678987);
  461. message.add_repeated_double(1.23456789876);
  462. message.add_repeated_double(1.234567898765);
  463. message.add_repeated_double(1.2345678987654);
  464. message.add_repeated_double(1.23456789876543);
  465. message.add_repeated_double(1.2e100);
  466. message.add_repeated_double(1.23e100);
  467. message.add_repeated_double(1.234e100);
  468. message.add_repeated_double(1.2345e100);
  469. message.add_repeated_double(1.23456e100);
  470. message.add_repeated_double(1.234567e100);
  471. message.add_repeated_double(1.2345678e100);
  472. message.add_repeated_double(1.23456789e100);
  473. message.add_repeated_double(1.234567898e100);
  474. message.add_repeated_double(1.2345678987e100);
  475. message.add_repeated_double(1.23456789876e100);
  476. message.add_repeated_double(1.234567898765e100);
  477. message.add_repeated_double(1.2345678987654e100);
  478. message.add_repeated_double(1.23456789876543e100);
  479. EXPECT_EQ(
  480. "repeated_float: 1.2\n"
  481. "repeated_float: 1.23\n"
  482. "repeated_float: 1.234\n"
  483. "repeated_float: 1.2345\n"
  484. "repeated_float: 1.23456\n"
  485. "repeated_float: 1.2e+10\n"
  486. "repeated_float: 1.23e+10\n"
  487. "repeated_float: 1.234e+10\n"
  488. "repeated_float: 1.2345e+10\n"
  489. "repeated_float: 1.23456e+10\n"
  490. "repeated_double: 1.2\n"
  491. "repeated_double: 1.23\n"
  492. "repeated_double: 1.234\n"
  493. "repeated_double: 1.2345\n"
  494. "repeated_double: 1.23456\n"
  495. "repeated_double: 1.234567\n"
  496. "repeated_double: 1.2345678\n"
  497. "repeated_double: 1.23456789\n"
  498. "repeated_double: 1.234567898\n"
  499. "repeated_double: 1.2345678987\n"
  500. "repeated_double: 1.23456789876\n"
  501. "repeated_double: 1.234567898765\n"
  502. "repeated_double: 1.2345678987654\n"
  503. "repeated_double: 1.23456789876543\n"
  504. "repeated_double: 1.2e+100\n"
  505. "repeated_double: 1.23e+100\n"
  506. "repeated_double: 1.234e+100\n"
  507. "repeated_double: 1.2345e+100\n"
  508. "repeated_double: 1.23456e+100\n"
  509. "repeated_double: 1.234567e+100\n"
  510. "repeated_double: 1.2345678e+100\n"
  511. "repeated_double: 1.23456789e+100\n"
  512. "repeated_double: 1.234567898e+100\n"
  513. "repeated_double: 1.2345678987e+100\n"
  514. "repeated_double: 1.23456789876e+100\n"
  515. "repeated_double: 1.234567898765e+100\n"
  516. "repeated_double: 1.2345678987654e+100\n"
  517. "repeated_double: 1.23456789876543e+100\n",
  518. RemoveRedundantZeros(message.DebugString()));
  519. }
  520. TEST_F(TextFormatTest, AllowPartial) {
  521. unittest::TestRequired message;
  522. TextFormat::Parser parser;
  523. parser.AllowPartialMessage(true);
  524. EXPECT_TRUE(parser.ParseFromString("a: 1", &message));
  525. EXPECT_EQ(1, message.a());
  526. EXPECT_FALSE(message.has_b());
  527. EXPECT_FALSE(message.has_c());
  528. }
  529. TEST_F(TextFormatTest, ParseExotic) {
  530. unittest::TestAllTypes message;
  531. ASSERT_TRUE(TextFormat::ParseFromString(
  532. "repeated_int32: -1\n"
  533. "repeated_int32: -2147483648\n"
  534. "repeated_int64: -1\n"
  535. "repeated_int64: -9223372036854775808\n"
  536. "repeated_uint32: 4294967295\n"
  537. "repeated_uint32: 2147483648\n"
  538. "repeated_uint64: 18446744073709551615\n"
  539. "repeated_uint64: 9223372036854775808\n"
  540. "repeated_double: 123.0\n"
  541. "repeated_double: 123.5\n"
  542. "repeated_double: 0.125\n"
  543. "repeated_double: 1.23E17\n"
  544. "repeated_double: 1.235E+22\n"
  545. "repeated_double: 1.235e-18\n"
  546. "repeated_double: 123.456789\n"
  547. "repeated_double: inf\n"
  548. "repeated_double: Infinity\n"
  549. "repeated_double: -inf\n"
  550. "repeated_double: -Infinity\n"
  551. "repeated_double: nan\n"
  552. "repeated_double: NaN\n"
  553. "repeated_string: \"\\000\\001\\a\\b\\f\\n\\r\\t\\v\\\\\\'\\\"\"\n",
  554. &message));
  555. ASSERT_EQ(2, message.repeated_int32_size());
  556. EXPECT_EQ(-1, message.repeated_int32(0));
  557. // Note: In C, a negative integer literal is actually the unary negation
  558. // operator being applied to a positive integer literal, and 2147483648 is
  559. // outside the range of int32. However, it is not outside the range of
  560. // uint32. Confusingly, this means that everything works if we make the
  561. // literal unsigned, even though we are negating it.
  562. EXPECT_EQ(-2147483648u, message.repeated_int32(1));
  563. ASSERT_EQ(2, message.repeated_int64_size());
  564. EXPECT_EQ(-1, message.repeated_int64(0));
  565. // Note: In C, a negative integer literal is actually the unary negation
  566. // operator being applied to a positive integer literal, and
  567. // 9223372036854775808 is outside the range of int64. However, it is not
  568. // outside the range of uint64. Confusingly, this means that everything
  569. // works if we make the literal unsigned, even though we are negating it.
  570. EXPECT_EQ(-GOOGLE_ULONGLONG(9223372036854775808), message.repeated_int64(1));
  571. ASSERT_EQ(2, message.repeated_uint32_size());
  572. EXPECT_EQ(4294967295u, message.repeated_uint32(0));
  573. EXPECT_EQ(2147483648u, message.repeated_uint32(1));
  574. ASSERT_EQ(2, message.repeated_uint64_size());
  575. EXPECT_EQ(GOOGLE_ULONGLONG(18446744073709551615), message.repeated_uint64(0));
  576. EXPECT_EQ(GOOGLE_ULONGLONG(9223372036854775808), message.repeated_uint64(1));
  577. ASSERT_EQ(13, message.repeated_double_size());
  578. EXPECT_EQ(123.0 , message.repeated_double(0));
  579. EXPECT_EQ(123.5 , message.repeated_double(1));
  580. EXPECT_EQ(0.125 , message.repeated_double(2));
  581. EXPECT_EQ(1.23E17 , message.repeated_double(3));
  582. EXPECT_EQ(1.235E22 , message.repeated_double(4));
  583. EXPECT_EQ(1.235E-18 , message.repeated_double(5));
  584. EXPECT_EQ(123.456789, message.repeated_double(6));
  585. EXPECT_EQ(message.repeated_double(7), numeric_limits<double>::infinity());
  586. EXPECT_EQ(message.repeated_double(8), numeric_limits<double>::infinity());
  587. EXPECT_EQ(message.repeated_double(9), -numeric_limits<double>::infinity());
  588. EXPECT_EQ(message.repeated_double(10), -numeric_limits<double>::infinity());
  589. EXPECT_TRUE(IsNaN(message.repeated_double(11)));
  590. EXPECT_TRUE(IsNaN(message.repeated_double(12)));
  591. // Note: Since these string literals have \0's in them, we must explicitly
  592. // pass their sizes to string's constructor.
  593. ASSERT_EQ(1, message.repeated_string_size());
  594. EXPECT_EQ(string("\000\001\a\b\f\n\r\t\v\\\'\"", 12),
  595. message.repeated_string(0));
  596. }
  597. class TextFormatParserTest : public testing::Test {
  598. protected:
  599. void ExpectFailure(const string& input, const string& message, int line,
  600. int col) {
  601. scoped_ptr<unittest::TestAllTypes> proto(new unittest::TestAllTypes);
  602. ExpectFailure(input, message, line, col, proto.get());
  603. }
  604. void ExpectFailure(const string& input, const string& message, int line,
  605. int col, Message* proto) {
  606. ExpectMessage(input, message, line, col, proto, false);
  607. }
  608. void ExpectMessage(const string& input, const string& message, int line,
  609. int col, Message* proto, bool expected_result) {
  610. TextFormat::Parser parser;
  611. MockErrorCollector error_collector;
  612. parser.RecordErrorsTo(&error_collector);
  613. EXPECT_EQ(parser.ParseFromString(input, proto), expected_result);
  614. EXPECT_EQ(SimpleItoa(line) + ":" + SimpleItoa(col) + ": " + message + "\n",
  615. error_collector.text_);
  616. }
  617. // An error collector which simply concatenates all its errors into a big
  618. // block of text which can be checked.
  619. class MockErrorCollector : public io::ErrorCollector {
  620. public:
  621. MockErrorCollector() {}
  622. ~MockErrorCollector() {}
  623. string text_;
  624. // implements ErrorCollector -------------------------------------
  625. void AddError(int line, int column, const string& message) {
  626. strings::SubstituteAndAppend(&text_, "$0:$1: $2\n",
  627. line + 1, column + 1, message);
  628. }
  629. void AddWarning(int line, int column, const string& message) {
  630. AddError(line, column, "WARNING:" + message);
  631. }
  632. };
  633. };
  634. TEST_F(TextFormatParserTest, ParseFieldValueFromString) {
  635. scoped_ptr<unittest::TestAllTypes> message(new unittest::TestAllTypes);
  636. const Descriptor* d = message->GetDescriptor();
  637. #define EXPECT_FIELD(name, value, valuestring) \
  638. EXPECT_TRUE(TextFormat::ParseFieldValueFromString( \
  639. valuestring, d->FindFieldByName("optional_" #name), message.get())); \
  640. EXPECT_EQ(value, message->optional_##name()); \
  641. EXPECT_TRUE(message->has_optional_##name());
  642. #define EXPECT_FLOAT_FIELD(name, value, valuestring) \
  643. EXPECT_TRUE(TextFormat::ParseFieldValueFromString( \
  644. valuestring, d->FindFieldByName("optional_" #name), message.get())); \
  645. EXPECT_FLOAT_EQ(value, message->optional_##name()); \
  646. EXPECT_TRUE(message->has_optional_##name());
  647. #define EXPECT_DOUBLE_FIELD(name, value, valuestring) \
  648. EXPECT_TRUE(TextFormat::ParseFieldValueFromString( \
  649. valuestring, d->FindFieldByName("optional_" #name), message.get())); \
  650. EXPECT_DOUBLE_EQ(value, message->optional_##name()); \
  651. EXPECT_TRUE(message->has_optional_##name());
  652. #define EXPECT_INVALID(name, valuestring) \
  653. EXPECT_FALSE(TextFormat::ParseFieldValueFromString( \
  654. valuestring, d->FindFieldByName("optional_" #name), message.get()));
  655. // int32
  656. EXPECT_FIELD(int32, 1, "1");
  657. EXPECT_FIELD(int32, -1, "-1");
  658. EXPECT_FIELD(int32, 0x1234, "0x1234");
  659. EXPECT_INVALID(int32, "a");
  660. EXPECT_INVALID(int32, "999999999999999999999999999999999999");
  661. EXPECT_INVALID(int32, "1,2");
  662. // int64
  663. EXPECT_FIELD(int64, 1, "1");
  664. EXPECT_FIELD(int64, -1, "-1");
  665. EXPECT_FIELD(int64, 0x1234567812345678LL, "0x1234567812345678");
  666. EXPECT_INVALID(int64, "a");
  667. EXPECT_INVALID(int64, "999999999999999999999999999999999999");
  668. EXPECT_INVALID(int64, "1,2");
  669. // uint64
  670. EXPECT_FIELD(uint64, 1, "1");
  671. EXPECT_FIELD(uint64, 0xf234567812345678ULL, "0xf234567812345678");
  672. EXPECT_INVALID(uint64, "-1");
  673. EXPECT_INVALID(uint64, "a");
  674. EXPECT_INVALID(uint64, "999999999999999999999999999999999999");
  675. EXPECT_INVALID(uint64, "1,2");
  676. // fixed32
  677. EXPECT_FIELD(fixed32, 1, "1");
  678. EXPECT_FIELD(fixed32, 0x12345678, "0x12345678");
  679. EXPECT_INVALID(fixed32, "-1");
  680. EXPECT_INVALID(fixed32, "a");
  681. EXPECT_INVALID(fixed32, "999999999999999999999999999999999999");
  682. EXPECT_INVALID(fixed32, "1,2");
  683. // fixed64
  684. EXPECT_FIELD(fixed64, 1, "1");
  685. EXPECT_FIELD(fixed64, 0x1234567812345678ULL, "0x1234567812345678");
  686. EXPECT_INVALID(fixed64, "-1");
  687. EXPECT_INVALID(fixed64, "a");
  688. EXPECT_INVALID(fixed64, "999999999999999999999999999999999999");
  689. EXPECT_INVALID(fixed64, "1,2");
  690. // bool
  691. EXPECT_FIELD(bool, true, "true");
  692. EXPECT_FIELD(bool, false, "false");
  693. EXPECT_FIELD(bool, true, "1");
  694. EXPECT_FIELD(bool, true, "t");
  695. EXPECT_FIELD(bool, false, "0");
  696. EXPECT_FIELD(bool, false, "f");
  697. EXPECT_INVALID(bool, "2");
  698. EXPECT_INVALID(bool, "-0");
  699. EXPECT_INVALID(bool, "on");
  700. EXPECT_INVALID(bool, "a");
  701. EXPECT_INVALID(bool, "True");
  702. // float
  703. EXPECT_FIELD(float, 1, "1");
  704. EXPECT_FLOAT_FIELD(float, 1.5, "1.5");
  705. EXPECT_FLOAT_FIELD(float, 1.5e3, "1.5e3");
  706. EXPECT_FLOAT_FIELD(float, -4.55, "-4.55");
  707. EXPECT_INVALID(float, "a");
  708. EXPECT_INVALID(float, "1,2");
  709. // double
  710. EXPECT_FIELD(double, 1, "1");
  711. EXPECT_FIELD(double, -1, "-1");
  712. EXPECT_DOUBLE_FIELD(double, 2.3, "2.3");
  713. EXPECT_DOUBLE_FIELD(double, 3e5, "3e5");
  714. EXPECT_INVALID(double, "a");
  715. EXPECT_INVALID(double, "1,2");
  716. // string
  717. EXPECT_FIELD(string, "hello", "\"hello\"");
  718. EXPECT_FIELD(string, "-1.87", "'-1.87'");
  719. EXPECT_INVALID(string, "hello"); // without quote for value
  720. // enum
  721. EXPECT_FIELD(nested_enum, unittest::TestAllTypes::BAR, "BAR");
  722. EXPECT_FIELD(nested_enum, unittest::TestAllTypes::BAZ,
  723. SimpleItoa(unittest::TestAllTypes::BAZ));
  724. EXPECT_INVALID(nested_enum, "FOOBAR");
  725. // message
  726. EXPECT_TRUE(TextFormat::ParseFieldValueFromString(
  727. "<bb:12>", d->FindFieldByName("optional_nested_message"), message.get()));
  728. EXPECT_EQ(12, message->optional_nested_message().bb()); \
  729. EXPECT_TRUE(message->has_optional_nested_message());
  730. EXPECT_INVALID(nested_message, "any");
  731. #undef EXPECT_FIELD
  732. #undef EXPECT_FLOAT_FIELD
  733. #undef EXPECT_DOUBLE_FIELD
  734. #undef EXPECT_INVALID
  735. }
  736. TEST_F(TextFormatParserTest, InvalidToken) {
  737. ExpectFailure("optional_bool: true\n-5\n", "Expected identifier.",
  738. 2, 1);
  739. ExpectFailure("optional_bool: true!\n", "Expected identifier.", 1, 20);
  740. ExpectFailure("\"some string\"", "Expected identifier.", 1, 1);
  741. }
  742. TEST_F(TextFormatParserTest, InvalidFieldName) {
  743. ExpectFailure(
  744. "invalid_field: somevalue\n",
  745. "Message type \"protobuf_unittest.TestAllTypes\" has no field named "
  746. "\"invalid_field\".",
  747. 1, 14);
  748. }
  749. TEST_F(TextFormatParserTest, InvalidCapitalization) {
  750. // We require that group names be exactly as they appear in the .proto.
  751. ExpectFailure(
  752. "optionalgroup {\na: 15\n}\n",
  753. "Message type \"protobuf_unittest.TestAllTypes\" has no field named "
  754. "\"optionalgroup\".",
  755. 1, 15);
  756. ExpectFailure(
  757. "OPTIONALgroup {\na: 15\n}\n",
  758. "Message type \"protobuf_unittest.TestAllTypes\" has no field named "
  759. "\"OPTIONALgroup\".",
  760. 1, 15);
  761. ExpectFailure(
  762. "Optional_Double: 10.0\n",
  763. "Message type \"protobuf_unittest.TestAllTypes\" has no field named "
  764. "\"Optional_Double\".",
  765. 1, 16);
  766. }
  767. TEST_F(TextFormatParserTest, InvalidFieldValues) {
  768. // Invalid values for a double/float field.
  769. ExpectFailure("optional_double: \"hello\"\n", "Expected double.", 1, 18);
  770. ExpectFailure("optional_double: true\n", "Expected double.", 1, 18);
  771. ExpectFailure("optional_double: !\n", "Expected double.", 1, 18);
  772. ExpectFailure("optional_double {\n \n}\n", "Expected \":\", found \"{\".",
  773. 1, 17);
  774. // Invalid values for a signed integer field.
  775. ExpectFailure("optional_int32: \"hello\"\n", "Expected integer.", 1, 17);
  776. ExpectFailure("optional_int32: true\n", "Expected integer.", 1, 17);
  777. ExpectFailure("optional_int32: 4.5\n", "Expected integer.", 1, 17);
  778. ExpectFailure("optional_int32: !\n", "Expected integer.", 1, 17);
  779. ExpectFailure("optional_int32 {\n \n}\n", "Expected \":\", found \"{\".",
  780. 1, 16);
  781. ExpectFailure("optional_int32: 0x80000000\n",
  782. "Integer out of range.", 1, 17);
  783. ExpectFailure("optional_int32: -0x80000001\n",
  784. "Integer out of range.", 1, 18);
  785. ExpectFailure("optional_int64: 0x8000000000000000\n",
  786. "Integer out of range.", 1, 17);
  787. ExpectFailure("optional_int64: -0x8000000000000001\n",
  788. "Integer out of range.", 1, 18);
  789. // Invalid values for an unsigned integer field.
  790. ExpectFailure("optional_uint64: \"hello\"\n", "Expected integer.", 1, 18);
  791. ExpectFailure("optional_uint64: true\n", "Expected integer.", 1, 18);
  792. ExpectFailure("optional_uint64: 4.5\n", "Expected integer.", 1, 18);
  793. ExpectFailure("optional_uint64: -5\n", "Expected integer.", 1, 18);
  794. ExpectFailure("optional_uint64: !\n", "Expected integer.", 1, 18);
  795. ExpectFailure("optional_uint64 {\n \n}\n", "Expected \":\", found \"{\".",
  796. 1, 17);
  797. ExpectFailure("optional_uint32: 0x100000000\n",
  798. "Integer out of range.", 1, 18);
  799. ExpectFailure("optional_uint64: 0x10000000000000000\n",
  800. "Integer out of range.", 1, 18);
  801. // Invalid values for a boolean field.
  802. ExpectFailure("optional_bool: \"hello\"\n", "Expected identifier.", 1, 16);
  803. ExpectFailure("optional_bool: 5\n", "Integer out of range.", 1, 16);
  804. ExpectFailure("optional_bool: -7.5\n", "Expected identifier.", 1, 16);
  805. ExpectFailure("optional_bool: !\n", "Expected identifier.", 1, 16);
  806. ExpectFailure(
  807. "optional_bool: meh\n",
  808. "Invalid value for boolean field \"optional_bool\". Value: \"meh\".",
  809. 2, 1);
  810. ExpectFailure("optional_bool {\n \n}\n", "Expected \":\", found \"{\".",
  811. 1, 15);
  812. // Invalid values for a string field.
  813. ExpectFailure("optional_string: true\n", "Expected string.", 1, 18);
  814. ExpectFailure("optional_string: 5\n", "Expected string.", 1, 18);
  815. ExpectFailure("optional_string: -7.5\n", "Expected string.", 1, 18);
  816. ExpectFailure("optional_string: !\n", "Expected string.", 1, 18);
  817. ExpectFailure("optional_string {\n \n}\n", "Expected \":\", found \"{\".",
  818. 1, 17);
  819. // Invalid values for an enumeration field.
  820. ExpectFailure("optional_nested_enum: \"hello\"\n",
  821. "Expected integer or identifier.", 1, 23);
  822. // Valid token, but enum value is not defined.
  823. ExpectFailure("optional_nested_enum: 5\n",
  824. "Unknown enumeration value of \"5\" for field "
  825. "\"optional_nested_enum\".", 2, 1);
  826. // We consume the negative sign, so the error position starts one character
  827. // later.
  828. ExpectFailure("optional_nested_enum: -7.5\n", "Expected integer.", 1, 24);
  829. ExpectFailure("optional_nested_enum: !\n",
  830. "Expected integer or identifier.", 1, 23);
  831. ExpectFailure(
  832. "optional_nested_enum: grah\n",
  833. "Unknown enumeration value of \"grah\" for field "
  834. "\"optional_nested_enum\".", 2, 1);
  835. ExpectFailure(
  836. "optional_nested_enum {\n \n}\n",
  837. "Expected \":\", found \"{\".", 1, 22);
  838. }
  839. TEST_F(TextFormatParserTest, MessageDelimeters) {
  840. // Non-matching delimeters.
  841. ExpectFailure("OptionalGroup <\n \n}\n", "Expected \">\", found \"}\".",
  842. 3, 1);
  843. // Invalid delimeters.
  844. ExpectFailure("OptionalGroup [\n \n]\n", "Expected \"{\", found \"[\".",
  845. 1, 15);
  846. // Unending message.
  847. ExpectFailure("optional_nested_message {\n \nbb: 118\n",
  848. "Expected identifier.",
  849. 4, 1);
  850. }
  851. TEST_F(TextFormatParserTest, UnknownExtension) {
  852. // Non-matching delimeters.
  853. ExpectFailure("[blahblah]: 123",
  854. "Extension \"blahblah\" is not defined or is not an "
  855. "extension of \"protobuf_unittest.TestAllTypes\".",
  856. 1, 11);
  857. }
  858. TEST_F(TextFormatParserTest, MissingRequired) {
  859. unittest::TestRequired message;
  860. ExpectFailure("a: 1",
  861. "Message missing required fields: b, c",
  862. 0, 1, &message);
  863. }
  864. TEST_F(TextFormatParserTest, ParseDuplicateRequired) {
  865. unittest::TestRequired message;
  866. ExpectFailure("a: 1 b: 2 c: 3 a: 1",
  867. "Non-repeated field \"a\" is specified multiple times.",
  868. 1, 17, &message);
  869. }
  870. TEST_F(TextFormatParserTest, ParseDuplicateOptional) {
  871. unittest::ForeignMessage message;
  872. ExpectFailure("c: 1 c: 2",
  873. "Non-repeated field \"c\" is specified multiple times.",
  874. 1, 7, &message);
  875. }
  876. TEST_F(TextFormatParserTest, MergeDuplicateRequired) {
  877. unittest::TestRequired message;
  878. TextFormat::Parser parser;
  879. EXPECT_TRUE(parser.MergeFromString("a: 1 b: 2 c: 3 a: 4", &message));
  880. EXPECT_EQ(4, message.a());
  881. }
  882. TEST_F(TextFormatParserTest, MergeDuplicateOptional) {
  883. unittest::ForeignMessage message;
  884. TextFormat::Parser parser;
  885. EXPECT_TRUE(parser.MergeFromString("c: 1 c: 2", &message));
  886. EXPECT_EQ(2, message.c());
  887. }
  888. TEST_F(TextFormatParserTest, ExplicitDelimiters) {
  889. unittest::TestRequired message;
  890. EXPECT_TRUE(TextFormat::ParseFromString("a:1,b:2;c:3", &message));
  891. EXPECT_EQ(1, message.a());
  892. EXPECT_EQ(2, message.b());
  893. EXPECT_EQ(3, message.c());
  894. }
  895. TEST_F(TextFormatParserTest, PrintErrorsToStderr) {
  896. vector<string> errors;
  897. {
  898. ScopedMemoryLog log;
  899. unittest::TestAllTypes proto;
  900. EXPECT_FALSE(TextFormat::ParseFromString("no_such_field: 1", &proto));
  901. errors = log.GetMessages(ERROR);
  902. }
  903. ASSERT_EQ(1, errors.size());
  904. EXPECT_EQ("Error parsing text-format protobuf_unittest.TestAllTypes: "
  905. "1:14: Message type \"protobuf_unittest.TestAllTypes\" has no field "
  906. "named \"no_such_field\".",
  907. errors[0]);
  908. }
  909. TEST_F(TextFormatParserTest, FailsOnTokenizationError) {
  910. vector<string> errors;
  911. {
  912. ScopedMemoryLog log;
  913. unittest::TestAllTypes proto;
  914. EXPECT_FALSE(TextFormat::ParseFromString("\020", &proto));
  915. errors = log.GetMessages(ERROR);
  916. }
  917. ASSERT_EQ(1, errors.size());
  918. EXPECT_EQ("Error parsing text-format protobuf_unittest.TestAllTypes: "
  919. "1:1: Invalid control characters encountered in text.",
  920. errors[0]);
  921. }
  922. TEST_F(TextFormatParserTest, ParseDeprecatedField) {
  923. unittest::TestDeprecatedFields message;
  924. ExpectMessage("deprecated_int32: 42",
  925. "WARNING:text format contains deprecated field "
  926. "\"deprecated_int32\"", 1, 21, &message, true);
  927. }
  928. class TextFormatMessageSetTest : public testing::Test {
  929. protected:
  930. static const char proto_debug_string_[];
  931. };
  932. const char TextFormatMessageSetTest::proto_debug_string_[] =
  933. "message_set {\n"
  934. " [protobuf_unittest.TestMessageSetExtension1] {\n"
  935. " i: 23\n"
  936. " }\n"
  937. " [protobuf_unittest.TestMessageSetExtension2] {\n"
  938. " str: \"foo\"\n"
  939. " }\n"
  940. "}\n";
  941. TEST_F(TextFormatMessageSetTest, Serialize) {
  942. protobuf_unittest::TestMessageSetContainer proto;
  943. protobuf_unittest::TestMessageSetExtension1* item_a =
  944. proto.mutable_message_set()->MutableExtension(
  945. protobuf_unittest::TestMessageSetExtension1::message_set_extension);
  946. item_a->set_i(23);
  947. protobuf_unittest::TestMessageSetExtension2* item_b =
  948. proto.mutable_message_set()->MutableExtension(
  949. protobuf_unittest::TestMessageSetExtension2::message_set_extension);
  950. item_b->set_str("foo");
  951. EXPECT_EQ(proto_debug_string_, proto.DebugString());
  952. }
  953. TEST_F(TextFormatMessageSetTest, Deserialize) {
  954. protobuf_unittest::TestMessageSetContainer proto;
  955. ASSERT_TRUE(TextFormat::ParseFromString(proto_debug_string_, &proto));
  956. EXPECT_EQ(23, proto.message_set().GetExtension(
  957. protobuf_unittest::TestMessageSetExtension1::message_set_extension).i());
  958. EXPECT_EQ("foo", proto.message_set().GetExtension(
  959. protobuf_unittest::TestMessageSetExtension2::message_set_extension).str());
  960. // Ensure that these are the only entries present.
  961. vector<const FieldDescriptor*> descriptors;
  962. proto.message_set().GetReflection()->ListFields(
  963. proto.message_set(), &descriptors);
  964. EXPECT_EQ(2, descriptors.size());
  965. }
  966. } // namespace text_format_unittest
  967. } // namespace protobuf
  968. } // namespace google