Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(390)

Side by Side Diff: third_party/protobuf/src/google/protobuf/text_format_unittest.cc

Issue 2495533002: third_party/protobuf: Update to HEAD (83d681ee2c) (Closed)
Patch Set: Make chrome settings proto generated file a component Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Protocol Buffers - Google's data interchange format 1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved. 2 // Copyright 2008 Google Inc. All rights reserved.
3 // https://developers.google.com/protocol-buffers/ 3 // https://developers.google.com/protocol-buffers/
4 // 4 //
5 // Redistribution and use in source and binary forms, with or without 5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are 6 // modification, are permitted provided that the following conditions are
7 // met: 7 // met:
8 // 8 //
9 // * Redistributions of source code must retain the above copyright 9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer. 10 // notice, this list of conditions and the following disclaimer.
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
46 #include <google/protobuf/stubs/common.h> 46 #include <google/protobuf/stubs/common.h>
47 #include <google/protobuf/stubs/logging.h> 47 #include <google/protobuf/stubs/logging.h>
48 #include <google/protobuf/testing/file.h> 48 #include <google/protobuf/testing/file.h>
49 #include <google/protobuf/testing/file.h> 49 #include <google/protobuf/testing/file.h>
50 #include <google/protobuf/test_util.h> 50 #include <google/protobuf/test_util.h>
51 #include <google/protobuf/unittest.pb.h> 51 #include <google/protobuf/unittest.pb.h>
52 #include <google/protobuf/unittest_mset.pb.h> 52 #include <google/protobuf/unittest_mset.pb.h>
53 #include <google/protobuf/unittest_mset_wire_format.pb.h> 53 #include <google/protobuf/unittest_mset_wire_format.pb.h>
54 #include <google/protobuf/io/tokenizer.h> 54 #include <google/protobuf/io/tokenizer.h>
55 #include <google/protobuf/io/zero_copy_stream_impl.h> 55 #include <google/protobuf/io/zero_copy_stream_impl.h>
56 #include <google/protobuf/stubs/mathlimits.h>
56 #include <google/protobuf/stubs/strutil.h> 57 #include <google/protobuf/stubs/strutil.h>
57 #include <google/protobuf/stubs/substitute.h> 58 #include <google/protobuf/stubs/substitute.h>
58 #include <google/protobuf/testing/googletest.h> 59 #include <google/protobuf/testing/googletest.h>
59 #include <gtest/gtest.h> 60 #include <gtest/gtest.h>
60 #include <google/protobuf/stubs/mathlimits.h>
61 61
62 62
63 namespace google { 63 namespace google {
64 namespace protobuf { 64 namespace protobuf {
65 65
66 // Can't use an anonymous namespace here due to brokenness of Tru64 compiler. 66 // Can't use an anonymous namespace here due to brokenness of Tru64 compiler.
67 namespace text_format_unittest { 67 namespace text_format_unittest {
68 68
69 // A basic string with different escapable characters for testing. 69 // A basic string with different escapable characters for testing.
70 const string kEscapeTestString = 70 const string kEscapeTestString =
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
150 proto_.add_repeated_string("foo"); 150 proto_.add_repeated_string("foo");
151 proto_.add_repeated_string("bar"); 151 proto_.add_repeated_string("bar");
152 proto_.add_repeated_nested_message()->set_bb(2); 152 proto_.add_repeated_nested_message()->set_bb(2);
153 proto_.add_repeated_nested_message()->set_bb(3); 153 proto_.add_repeated_nested_message()->set_bb(3);
154 proto_.add_repeated_nested_enum(unittest::TestAllTypes::FOO); 154 proto_.add_repeated_nested_enum(unittest::TestAllTypes::FOO);
155 proto_.add_repeated_nested_enum(unittest::TestAllTypes::BAR); 155 proto_.add_repeated_nested_enum(unittest::TestAllTypes::BAR);
156 156
157 TextFormat::Printer printer; 157 TextFormat::Printer printer;
158 printer.SetUseShortRepeatedPrimitives(true); 158 printer.SetUseShortRepeatedPrimitives(true);
159 string text; 159 string text;
160 printer.PrintToString(proto_, &text); 160 EXPECT_TRUE(printer.PrintToString(proto_, &text));
161 161
162 EXPECT_EQ("optional_int32: 123\n" 162 EXPECT_EQ("optional_int32: 123\n"
163 "repeated_int32: [456, 789]\n" 163 "repeated_int32: [456, 789]\n"
164 "repeated_string: \"foo\"\n"
165 "repeated_string: \"bar\"\n"
166 "repeated_nested_message {\n bb: 2\n}\n"
167 "repeated_nested_message {\n bb: 3\n}\n"
168 "repeated_nested_enum: [FOO, BAR]\n",
169 text);
170
171 // Verify that any existing data in the string is cleared when
172 // PrintToString() is called.
173 text = "just some data here...\n\nblah blah";
174 EXPECT_TRUE(printer.PrintToString(proto_, &text));
175
176 EXPECT_EQ("optional_int32: 123\n"
177 "repeated_int32: [456, 789]\n"
164 "repeated_string: \"foo\"\n" 178 "repeated_string: \"foo\"\n"
165 "repeated_string: \"bar\"\n" 179 "repeated_string: \"bar\"\n"
166 "repeated_nested_message {\n bb: 2\n}\n" 180 "repeated_nested_message {\n bb: 2\n}\n"
167 "repeated_nested_message {\n bb: 3\n}\n" 181 "repeated_nested_message {\n bb: 3\n}\n"
168 "repeated_nested_enum: [FOO, BAR]\n", 182 "repeated_nested_enum: [FOO, BAR]\n",
169 text); 183 text);
170 184
171 // Try in single-line mode. 185 // Try in single-line mode.
172 printer.SetSingleLineMode(true); 186 printer.SetSingleLineMode(true);
173 printer.PrintToString(proto_, &text); 187 EXPECT_TRUE(printer.PrintToString(proto_, &text));
174 188
175 EXPECT_EQ("optional_int32: 123 " 189 EXPECT_EQ("optional_int32: 123 "
176 "repeated_int32: [456, 789] " 190 "repeated_int32: [456, 789] "
177 "repeated_string: \"foo\" " 191 "repeated_string: \"foo\" "
178 "repeated_string: \"bar\" " 192 "repeated_string: \"bar\" "
179 "repeated_nested_message { bb: 2 } " 193 "repeated_nested_message { bb: 2 } "
180 "repeated_nested_message { bb: 3 } " 194 "repeated_nested_message { bb: 3 } "
181 "repeated_nested_enum: [FOO, BAR] ", 195 "repeated_nested_enum: [FOO, BAR] ",
182 text); 196 text);
183 } 197 }
(...skipping 468 matching lines...) Expand 10 before | Expand all | Expand 10 after
652 666
653 ASSERT_EQ(2, proto_.repeated_nested_message_size()); 667 ASSERT_EQ(2, proto_.repeated_nested_message_size());
654 EXPECT_EQ(1, proto_.repeated_nested_message(0).bb()); 668 EXPECT_EQ(1, proto_.repeated_nested_message(0).bb());
655 EXPECT_EQ(2, proto_.repeated_nested_message(1).bb()); 669 EXPECT_EQ(2, proto_.repeated_nested_message(1).bb());
656 670
657 ASSERT_EQ(2, proto_.repeatedgroup_size()); 671 ASSERT_EQ(2, proto_.repeatedgroup_size());
658 EXPECT_EQ(3, proto_.repeatedgroup(0).a()); 672 EXPECT_EQ(3, proto_.repeatedgroup(0).a());
659 EXPECT_EQ(4, proto_.repeatedgroup(1).a()); 673 EXPECT_EQ(4, proto_.repeatedgroup(1).a());
660 } 674 }
661 675
676 TEST_F(TextFormatTest, ParseShortRepeatedWithTrailingComma) {
677 string parse_string = "repeated_int32: [456,]\n";
678 ASSERT_FALSE(TextFormat::ParseFromString(parse_string, &proto_));
679 parse_string = "repeated_nested_enum: [ FOO , ]";
680 ASSERT_FALSE(TextFormat::ParseFromString(parse_string, &proto_));
681 parse_string = "repeated_string: [ \"foo\", ]";
682 ASSERT_FALSE(TextFormat::ParseFromString(parse_string, &proto_));
683 parse_string = "repeated_nested_message: [ { bb: 1 }, ]";
684 ASSERT_FALSE(TextFormat::ParseFromString(parse_string, &proto_));
685 parse_string = "RepeatedGroup [{ a: 3 },]\n";
686 }
687
688 TEST_F(TextFormatTest, ParseShortRepeatedEmpty) {
689 string parse_string =
690 "repeated_int32: []\n"
691 "repeated_nested_enum: []\n"
692 "repeated_string: []\n"
693 "repeated_nested_message: []\n"
694 "RepeatedGroup []\n";
695
696 ASSERT_TRUE(TextFormat::ParseFromString(parse_string, &proto_));
697
698 EXPECT_EQ(0, proto_.repeated_int32_size());
699 EXPECT_EQ(0, proto_.repeated_nested_enum_size());
700 EXPECT_EQ(0, proto_.repeated_string_size());
701 EXPECT_EQ(0, proto_.repeated_nested_message_size());
702 EXPECT_EQ(0, proto_.repeatedgroup_size());
703 }
704
705 TEST_F(TextFormatTest, ParseShortRepeatedConcatenatedWithEmpty) {
706 string parse_string =
707 // Starting with empty [] should have no impact.
708 "repeated_int32: []\n"
709 "repeated_nested_enum: []\n"
710 "repeated_string: []\n"
711 "repeated_nested_message: []\n"
712 "RepeatedGroup []\n"
713 // Mixed short-form and long-form are simply concatenated.
714 "repeated_int32: 1\n"
715 "repeated_int32: [456, 789]\n"
716 "repeated_nested_enum: [ FOO ,BAR, # comment\n"
717 " 3]\n"
718 // Note that while the printer won't print repeated strings in short-form,
719 // the parser will accept them.
720 "repeated_string: [ \"foo\", 'bar' ]\n"
721 // Repeated message
722 "repeated_nested_message: [ { bb: 1 }, { bb : 2 }]\n"
723 // Repeated group
724 "RepeatedGroup [{ a: 3 },{ a: 4 }]\n"
725 // Adding empty [] should have no impact.
726 "repeated_int32: []\n"
727 "repeated_nested_enum: []\n"
728 "repeated_string: []\n"
729 "repeated_nested_message: []\n"
730 "RepeatedGroup []\n";
731
732 ASSERT_TRUE(TextFormat::ParseFromString(parse_string, &proto_));
733
734 ASSERT_EQ(3, proto_.repeated_int32_size());
735 EXPECT_EQ(1, proto_.repeated_int32(0));
736 EXPECT_EQ(456, proto_.repeated_int32(1));
737 EXPECT_EQ(789, proto_.repeated_int32(2));
738
739 ASSERT_EQ(3, proto_.repeated_nested_enum_size());
740 EXPECT_EQ(unittest::TestAllTypes::FOO, proto_.repeated_nested_enum(0));
741 EXPECT_EQ(unittest::TestAllTypes::BAR, proto_.repeated_nested_enum(1));
742 EXPECT_EQ(unittest::TestAllTypes::BAZ, proto_.repeated_nested_enum(2));
743
744 ASSERT_EQ(2, proto_.repeated_string_size());
745 EXPECT_EQ("foo", proto_.repeated_string(0));
746 EXPECT_EQ("bar", proto_.repeated_string(1));
747
748 ASSERT_EQ(2, proto_.repeated_nested_message_size());
749 EXPECT_EQ(1, proto_.repeated_nested_message(0).bb());
750 EXPECT_EQ(2, proto_.repeated_nested_message(1).bb());
751
752 ASSERT_EQ(2, proto_.repeatedgroup_size());
753 EXPECT_EQ(3, proto_.repeatedgroup(0).a());
754 EXPECT_EQ(4, proto_.repeatedgroup(1).a());
755 }
756
662 757
663 TEST_F(TextFormatTest, Comments) { 758 TEST_F(TextFormatTest, Comments) {
664 // Test that comments are ignored. 759 // Test that comments are ignored.
665 760
666 string parse_string = "optional_int32: 1 # a comment\n" 761 string parse_string = "optional_int32: 1 # a comment\n"
667 "optional_int64: 2 # another comment"; 762 "optional_int64: 2 # another comment";
668 763
669 io::ArrayInputStream input_stream(parse_string.data(), 764 io::ArrayInputStream input_stream(parse_string.data(),
670 parse_string.size()); 765 parse_string.size());
671 766
(...skipping 219 matching lines...) Expand 10 before | Expand all | Expand 10 after
891 EXPECT_EQ(GOOGLE_ULONGLONG(9223372036854775808), message.repeated_uint64(1)); 986 EXPECT_EQ(GOOGLE_ULONGLONG(9223372036854775808), message.repeated_uint64(1));
892 987
893 ASSERT_EQ(13, message.repeated_double_size()); 988 ASSERT_EQ(13, message.repeated_double_size());
894 EXPECT_EQ(123.0 , message.repeated_double(0)); 989 EXPECT_EQ(123.0 , message.repeated_double(0));
895 EXPECT_EQ(123.5 , message.repeated_double(1)); 990 EXPECT_EQ(123.5 , message.repeated_double(1));
896 EXPECT_EQ(0.125 , message.repeated_double(2)); 991 EXPECT_EQ(0.125 , message.repeated_double(2));
897 EXPECT_EQ(1.23E17 , message.repeated_double(3)); 992 EXPECT_EQ(1.23E17 , message.repeated_double(3));
898 EXPECT_EQ(1.235E22 , message.repeated_double(4)); 993 EXPECT_EQ(1.235E22 , message.repeated_double(4));
899 EXPECT_EQ(1.235E-18 , message.repeated_double(5)); 994 EXPECT_EQ(1.235E-18 , message.repeated_double(5));
900 EXPECT_EQ(123.456789, message.repeated_double(6)); 995 EXPECT_EQ(123.456789, message.repeated_double(6));
901 EXPECT_EQ(message.repeated_double(7), numeric_limits<double>::infinity()); 996 EXPECT_EQ(message.repeated_double(7),
902 EXPECT_EQ(message.repeated_double(8), numeric_limits<double>::infinity()); 997 std::numeric_limits<double>::infinity());
903 EXPECT_EQ(message.repeated_double(9), -numeric_limits<double>::infinity()); 998 EXPECT_EQ(message.repeated_double(8),
904 EXPECT_EQ(message.repeated_double(10), -numeric_limits<double>::infinity()); 999 std::numeric_limits<double>::infinity());
1000 EXPECT_EQ(message.repeated_double(9),
1001 -std::numeric_limits<double>::infinity());
1002 EXPECT_EQ(message.repeated_double(10),
1003 -std::numeric_limits<double>::infinity());
905 EXPECT_TRUE(MathLimits<double>::IsNaN(message.repeated_double(11))); 1004 EXPECT_TRUE(MathLimits<double>::IsNaN(message.repeated_double(11)));
906 EXPECT_TRUE(MathLimits<double>::IsNaN(message.repeated_double(12))); 1005 EXPECT_TRUE(MathLimits<double>::IsNaN(message.repeated_double(12)));
907 1006
908 // Note: Since these string literals have \0's in them, we must explicitly 1007 // Note: Since these string literals have \0's in them, we must explicitly
909 // pass their sizes to string's constructor. 1008 // pass their sizes to string's constructor.
910 ASSERT_EQ(1, message.repeated_string_size()); 1009 ASSERT_EQ(1, message.repeated_string_size());
911 EXPECT_EQ(string("\000\001\a\b\f\n\r\t\v\\\'\"", 12), 1010 EXPECT_EQ(string("\000\001\a\b\f\n\r\t\v\\\'\"", 12),
912 message.repeated_string(0)); 1011 message.repeated_string(0));
913 } 1012 }
914 1013
(...skipping 274 matching lines...) Expand 10 before | Expand all | Expand 10 after
1189 1288
1190 #undef EXPECT_FIELD 1289 #undef EXPECT_FIELD
1191 #undef EXPECT_BOOL_FIELD 1290 #undef EXPECT_BOOL_FIELD
1192 #undef EXPECT_FLOAT_FIELD 1291 #undef EXPECT_FLOAT_FIELD
1193 #undef EXPECT_DOUBLE_FIELD 1292 #undef EXPECT_DOUBLE_FIELD
1194 #undef EXPECT_INVALID 1293 #undef EXPECT_INVALID
1195 } 1294 }
1196 1295
1197 1296
1198 TEST_F(TextFormatParserTest, InvalidToken) { 1297 TEST_F(TextFormatParserTest, InvalidToken) {
1199 ExpectFailure("optional_bool: true\n-5\n", "Expected identifier.", 1298 ExpectFailure("optional_bool: true\n-5\n", "Expected identifier, got: -",
1200 2, 1); 1299 2, 1);
1201 1300
1202 ExpectFailure("optional_bool: true!\n", "Expected identifier.", 1, 20); 1301 ExpectFailure("optional_bool: true!\n", "Expected identifier, got: !", 1,
1203 ExpectFailure("\"some string\"", "Expected identifier.", 1, 1); 1302 20);
1303 ExpectFailure("\"some string\"",
1304 "Expected identifier, got: \"some string\"", 1, 1);
1204 } 1305 }
1205 1306
1206 TEST_F(TextFormatParserTest, InvalidFieldName) { 1307 TEST_F(TextFormatParserTest, InvalidFieldName) {
1207 ExpectFailure( 1308 ExpectFailure(
1208 "invalid_field: somevalue\n", 1309 "invalid_field: somevalue\n",
1209 "Message type \"protobuf_unittest.TestAllTypes\" has no field named " 1310 "Message type \"protobuf_unittest.TestAllTypes\" has no field named "
1210 "\"invalid_field\".", 1311 "\"invalid_field\".",
1211 1, 14); 1312 1, 14);
1212 } 1313 }
1213 1314
(...skipping 27 matching lines...) Expand all
1241 // ... but are parsed correctly if we match case insensitive. 1342 // ... but are parsed correctly if we match case insensitive.
1242 parser.AllowCaseInsensitiveField(true); 1343 parser.AllowCaseInsensitiveField(true);
1243 EXPECT_TRUE(parser.ParseFromString("Optional_Double: 10.0", &proto)); 1344 EXPECT_TRUE(parser.ParseFromString("Optional_Double: 10.0", &proto));
1244 EXPECT_EQ(10.0, proto.optional_double()); 1345 EXPECT_EQ(10.0, proto.optional_double());
1245 EXPECT_TRUE(parser.ParseFromString("oPtIoNaLgRoUp { a: 15 }", &proto)); 1346 EXPECT_TRUE(parser.ParseFromString("oPtIoNaLgRoUp { a: 15 }", &proto));
1246 EXPECT_EQ(15, proto.optionalgroup().a()); 1347 EXPECT_EQ(15, proto.optionalgroup().a());
1247 } 1348 }
1248 1349
1249 TEST_F(TextFormatParserTest, InvalidFieldValues) { 1350 TEST_F(TextFormatParserTest, InvalidFieldValues) {
1250 // Invalid values for a double/float field. 1351 // Invalid values for a double/float field.
1251 ExpectFailure("optional_double: \"hello\"\n", "Expected double.", 1, 18); 1352 ExpectFailure("optional_double: \"hello\"\n",
1252 ExpectFailure("optional_double: true\n", "Expected double.", 1, 18); 1353 "Expected double, got: \"hello\"", 1, 18);
1253 ExpectFailure("optional_double: !\n", "Expected double.", 1, 18); 1354 ExpectFailure("optional_double: true\n", "Expected double, got: true", 1,
1355 18);
1356 ExpectFailure("optional_double: !\n", "Expected double, got: !", 1, 18);
1254 ExpectFailure("optional_double {\n \n}\n", "Expected \":\", found \"{\".", 1357 ExpectFailure("optional_double {\n \n}\n", "Expected \":\", found \"{\".",
1255 1, 17); 1358 1, 17);
1256 1359
1257 // Invalid values for a signed integer field. 1360 // Invalid values for a signed integer field.
1258 ExpectFailure("optional_int32: \"hello\"\n", "Expected integer.", 1, 17); 1361 ExpectFailure("optional_int32: \"hello\"\n",
1259 ExpectFailure("optional_int32: true\n", "Expected integer.", 1, 17); 1362 "Expected integer, got: \"hello\"", 1, 17);
1260 ExpectFailure("optional_int32: 4.5\n", "Expected integer.", 1, 17); 1363 ExpectFailure("optional_int32: true\n", "Expected integer, got: true", 1, 17);
1261 ExpectFailure("optional_int32: !\n", "Expected integer.", 1, 17); 1364 ExpectFailure("optional_int32: 4.5\n", "Expected integer, got: 4.5", 1, 17);
1365 ExpectFailure("optional_int32: !\n", "Expected integer, got: !", 1, 17);
1262 ExpectFailure("optional_int32 {\n \n}\n", "Expected \":\", found \"{\".", 1366 ExpectFailure("optional_int32 {\n \n}\n", "Expected \":\", found \"{\".",
1263 1, 16); 1367 1, 16);
1264 ExpectFailure("optional_int32: 0x80000000\n", 1368 ExpectFailure("optional_int32: 0x80000000\n",
1265 "Integer out of range.", 1, 17); 1369 "Integer out of range (0x80000000)", 1, 17);
1266 ExpectFailure("optional_int64: 0x8000000000000000\n", 1370 ExpectFailure("optional_int64: 0x8000000000000000\n",
1267 "Integer out of range.", 1, 17); 1371 "Integer out of range (0x8000000000000000)", 1, 17);
1268 ExpectFailure("optional_int32: -0x80000001\n", 1372 ExpectFailure("optional_int32: -0x80000001\n",
1269 "Integer out of range.", 1, 18); 1373 "Integer out of range (0x80000001)", 1, 18);
1270 ExpectFailure("optional_int64: -0x8000000000000001\n", 1374 ExpectFailure("optional_int64: -0x8000000000000001\n",
1271 "Integer out of range.", 1, 18); 1375 "Integer out of range (0x8000000000000001)", 1, 18);
1272 1376
1273 // Invalid values for an unsigned integer field. 1377 // Invalid values for an unsigned integer field.
1274 ExpectFailure("optional_uint64: \"hello\"\n", "Expected integer.", 1, 18); 1378 ExpectFailure("optional_uint64: \"hello\"\n",
1275 ExpectFailure("optional_uint64: true\n", "Expected integer.", 1, 18); 1379 "Expected integer, got: \"hello\"", 1, 18);
1276 ExpectFailure("optional_uint64: 4.5\n", "Expected integer.", 1, 18); 1380 ExpectFailure("optional_uint64: true\n",
1277 ExpectFailure("optional_uint64: -5\n", "Expected integer.", 1, 18); 1381 "Expected integer, got: true", 1, 18);
1278 ExpectFailure("optional_uint64: !\n", "Expected integer.", 1, 18); 1382 ExpectFailure("optional_uint64: 4.5\n", "Expected integer, got: 4.5", 1, 18);
1383 ExpectFailure("optional_uint64: -5\n", "Expected integer, got: -", 1, 18);
1384 ExpectFailure("optional_uint64: !\n", "Expected integer, got: !", 1, 18);
1279 ExpectFailure("optional_uint64 {\n \n}\n", "Expected \":\", found \"{\".", 1385 ExpectFailure("optional_uint64 {\n \n}\n", "Expected \":\", found \"{\".",
1280 1, 17); 1386 1, 17);
1281 ExpectFailure("optional_uint32: 0x100000000\n", 1387 ExpectFailure("optional_uint32: 0x100000000\n",
1282 "Integer out of range.", 1, 18); 1388 "Integer out of range (0x100000000)", 1, 18);
1283 ExpectFailure("optional_uint64: 0x10000000000000000\n", 1389 ExpectFailure("optional_uint64: 0x10000000000000000\n",
1284 "Integer out of range.", 1, 18); 1390 "Integer out of range (0x10000000000000000)", 1, 18);
1285 1391
1286 // Invalid values for a boolean field. 1392 // Invalid values for a boolean field.
1287 ExpectFailure("optional_bool: \"hello\"\n", "Expected identifier.", 1, 16); 1393 ExpectFailure("optional_bool: \"hello\"\n",
1288 ExpectFailure("optional_bool: 5\n", "Integer out of range.", 1, 16); 1394 "Expected identifier, got: \"hello\"", 1, 16);
1289 ExpectFailure("optional_bool: -7.5\n", "Expected identifier.", 1, 16); 1395 ExpectFailure("optional_bool: 5\n", "Integer out of range (5)", 1, 16);
1290 ExpectFailure("optional_bool: !\n", "Expected identifier.", 1, 16); 1396 ExpectFailure("optional_bool: -7.5\n", "Expected identifier, got: -", 1, 16);
1397 ExpectFailure("optional_bool: !\n", "Expected identifier, got: !", 1, 16);
1291 1398
1292 ExpectFailure( 1399 ExpectFailure(
1293 "optional_bool: meh\n", 1400 "optional_bool: meh\n",
1294 "Invalid value for boolean field \"optional_bool\". Value: \"meh\".", 1401 "Invalid value for boolean field \"optional_bool\". Value: \"meh\".",
1295 2, 1); 1402 2, 1);
1296 1403
1297 ExpectFailure("optional_bool {\n \n}\n", "Expected \":\", found \"{\".", 1404 ExpectFailure("optional_bool {\n \n}\n", "Expected \":\", found \"{\".",
1298 1, 15); 1405 1, 15);
1299 1406
1300 // Invalid values for a string field. 1407 // Invalid values for a string field.
1301 ExpectFailure("optional_string: true\n", "Expected string.", 1, 18); 1408 ExpectFailure("optional_string: true\n", "Expected string, got: true", 1, 18);
1302 ExpectFailure("optional_string: 5\n", "Expected string.", 1, 18); 1409 ExpectFailure("optional_string: 5\n", "Expected string, got: 5", 1, 18);
1303 ExpectFailure("optional_string: -7.5\n", "Expected string.", 1, 18); 1410 ExpectFailure("optional_string: -7.5\n", "Expected string, got: -", 1, 18);
1304 ExpectFailure("optional_string: !\n", "Expected string.", 1, 18); 1411 ExpectFailure("optional_string: !\n", "Expected string, got: !", 1, 18);
1305 ExpectFailure("optional_string {\n \n}\n", "Expected \":\", found \"{\".", 1412 ExpectFailure("optional_string {\n \n}\n", "Expected \":\", found \"{\".",
1306 1, 17); 1413 1, 17);
1307 1414
1308 // Invalid values for an enumeration field. 1415 // Invalid values for an enumeration field.
1309 ExpectFailure("optional_nested_enum: \"hello\"\n", 1416 ExpectFailure("optional_nested_enum: \"hello\"\n",
1310 "Expected integer or identifier.", 1, 23); 1417 "Expected integer or identifier, got: \"hello\"", 1, 23);
1311 1418
1312 // Valid token, but enum value is not defined. 1419 // Valid token, but enum value is not defined.
1313 ExpectFailure("optional_nested_enum: 5\n", 1420 ExpectFailure("optional_nested_enum: 5\n",
1314 "Unknown enumeration value of \"5\" for field " 1421 "Unknown enumeration value of \"5\" for field "
1315 "\"optional_nested_enum\".", 2, 1); 1422 "\"optional_nested_enum\".", 2, 1);
1316 // We consume the negative sign, so the error position starts one character 1423 // We consume the negative sign, so the error position starts one character
1317 // later. 1424 // later.
1318 ExpectFailure("optional_nested_enum: -7.5\n", "Expected integer.", 1, 24); 1425 ExpectFailure("optional_nested_enum: -7.5\n", "Expected integer, got: 7.5", 1,
1426 24);
1319 ExpectFailure("optional_nested_enum: !\n", 1427 ExpectFailure("optional_nested_enum: !\n",
1320 "Expected integer or identifier.", 1, 23); 1428 "Expected integer or identifier, got: !", 1, 23);
1321 1429
1322 ExpectFailure( 1430 ExpectFailure(
1323 "optional_nested_enum: grah\n", 1431 "optional_nested_enum: grah\n",
1324 "Unknown enumeration value of \"grah\" for field " 1432 "Unknown enumeration value of \"grah\" for field "
1325 "\"optional_nested_enum\".", 2, 1); 1433 "\"optional_nested_enum\".", 2, 1);
1326 1434
1327 ExpectFailure( 1435 ExpectFailure(
1328 "optional_nested_enum {\n \n}\n", 1436 "optional_nested_enum {\n \n}\n",
1329 "Expected \":\", found \"{\".", 1, 22); 1437 "Expected \":\", found \"{\".", 1, 22);
1330 } 1438 }
1331 1439
1332 TEST_F(TextFormatParserTest, MessageDelimiters) { 1440 TEST_F(TextFormatParserTest, MessageDelimiters) {
1333 // Non-matching delimiters. 1441 // Non-matching delimiters.
1334 ExpectFailure("OptionalGroup <\n \n}\n", "Expected \">\", found \"}\".", 1442 ExpectFailure("OptionalGroup <\n \n}\n", "Expected \">\", found \"}\".",
1335 3, 1); 1443 3, 1);
1336 1444
1337 // Invalid delimiters. 1445 // Invalid delimiters.
1338 ExpectFailure("OptionalGroup [\n \n]\n", "Expected \"{\", found \"[\".", 1446 ExpectFailure("OptionalGroup [\n \n]\n", "Expected \"{\", found \"[\".",
1339 1, 15); 1447 1, 15);
1340 1448
1341 // Unending message. 1449 // Unending message.
1342 ExpectFailure("optional_nested_message {\n \nbb: 118\n", 1450 ExpectFailure("optional_nested_message {\n \nbb: 118\n",
1343 "Expected identifier.", 1451 "Expected identifier, got: ",
1344 4, 1); 1452 4, 1);
1345 } 1453 }
1346 1454
1347 TEST_F(TextFormatParserTest, UnknownExtension) { 1455 TEST_F(TextFormatParserTest, UnknownExtension) {
1348 // Non-matching delimiters. 1456 // Non-matching delimiters.
1349 ExpectFailure("[blahblah]: 123", 1457 ExpectFailure("[blahblah]: 123",
1350 "Extension \"blahblah\" is not defined or is not an " 1458 "Extension \"blahblah\" is not defined or is not an "
1351 "extension of \"protobuf_unittest.TestAllTypes\".", 1459 "extension of \"protobuf_unittest.TestAllTypes\".",
1352 1, 11); 1460 1, 11);
1353 } 1461 }
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
1389 1497
1390 TEST_F(TextFormatParserTest, ExplicitDelimiters) { 1498 TEST_F(TextFormatParserTest, ExplicitDelimiters) {
1391 unittest::TestRequired message; 1499 unittest::TestRequired message;
1392 EXPECT_TRUE(TextFormat::ParseFromString("a:1,b:2;c:3", &message)); 1500 EXPECT_TRUE(TextFormat::ParseFromString("a:1,b:2;c:3", &message));
1393 EXPECT_EQ(1, message.a()); 1501 EXPECT_EQ(1, message.a());
1394 EXPECT_EQ(2, message.b()); 1502 EXPECT_EQ(2, message.b());
1395 EXPECT_EQ(3, message.c()); 1503 EXPECT_EQ(3, message.c());
1396 } 1504 }
1397 1505
1398 TEST_F(TextFormatParserTest, PrintErrorsToStderr) { 1506 TEST_F(TextFormatParserTest, PrintErrorsToStderr) {
1399 vector<string> errors; 1507 std::vector<string> errors;
1400 1508
1401 { 1509 {
1402 ScopedMemoryLog log; 1510 ScopedMemoryLog log;
1403 unittest::TestAllTypes proto; 1511 unittest::TestAllTypes proto;
1404 EXPECT_FALSE(TextFormat::ParseFromString("no_such_field: 1", &proto)); 1512 EXPECT_FALSE(TextFormat::ParseFromString("no_such_field: 1", &proto));
1405 errors = log.GetMessages(ERROR); 1513 errors = log.GetMessages(ERROR);
1406 } 1514 }
1407 1515
1408 ASSERT_EQ(1, errors.size()); 1516 ASSERT_EQ(1, errors.size());
1409 EXPECT_EQ("Error parsing text-format protobuf_unittest.TestAllTypes: " 1517 EXPECT_EQ("Error parsing text-format protobuf_unittest.TestAllTypes: "
1410 "1:14: Message type \"protobuf_unittest.TestAllTypes\" has no field " 1518 "1:14: Message type \"protobuf_unittest.TestAllTypes\" has no field "
1411 "named \"no_such_field\".", 1519 "named \"no_such_field\".",
1412 errors[0]); 1520 errors[0]);
1413 } 1521 }
1414 1522
1415 TEST_F(TextFormatParserTest, FailsOnTokenizationError) { 1523 TEST_F(TextFormatParserTest, FailsOnTokenizationError) {
1416 vector<string> errors; 1524 std::vector<string> errors;
1417 1525
1418 { 1526 {
1419 ScopedMemoryLog log; 1527 ScopedMemoryLog log;
1420 unittest::TestAllTypes proto; 1528 unittest::TestAllTypes proto;
1421 EXPECT_FALSE(TextFormat::ParseFromString("\020", &proto)); 1529 EXPECT_FALSE(TextFormat::ParseFromString("\020", &proto));
1422 errors = log.GetMessages(ERROR); 1530 errors = log.GetMessages(ERROR);
1423 } 1531 }
1424 1532
1425 ASSERT_EQ(1, errors.size()); 1533 ASSERT_EQ(1, errors.size());
1426 EXPECT_EQ("Error parsing text-format protobuf_unittest.TestAllTypes: " 1534 EXPECT_EQ("Error parsing text-format protobuf_unittest.TestAllTypes: "
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
1465 1573
1466 TEST_F(TextFormatMessageSetTest, Deserialize) { 1574 TEST_F(TextFormatMessageSetTest, Deserialize) {
1467 protobuf_unittest::TestMessageSetContainer proto; 1575 protobuf_unittest::TestMessageSetContainer proto;
1468 ASSERT_TRUE(TextFormat::ParseFromString(proto_debug_string_, &proto)); 1576 ASSERT_TRUE(TextFormat::ParseFromString(proto_debug_string_, &proto));
1469 EXPECT_EQ(23, proto.message_set().GetExtension( 1577 EXPECT_EQ(23, proto.message_set().GetExtension(
1470 protobuf_unittest::TestMessageSetExtension1::message_set_extension).i()); 1578 protobuf_unittest::TestMessageSetExtension1::message_set_extension).i());
1471 EXPECT_EQ("foo", proto.message_set().GetExtension( 1579 EXPECT_EQ("foo", proto.message_set().GetExtension(
1472 protobuf_unittest::TestMessageSetExtension2::message_set_extension).str()); 1580 protobuf_unittest::TestMessageSetExtension2::message_set_extension).str());
1473 1581
1474 // Ensure that these are the only entries present. 1582 // Ensure that these are the only entries present.
1475 vector<const FieldDescriptor*> descriptors; 1583 std::vector<const FieldDescriptor*> descriptors;
1476 proto.message_set().GetReflection()->ListFields( 1584 proto.message_set().GetReflection()->ListFields(
1477 proto.message_set(), &descriptors); 1585 proto.message_set(), &descriptors);
1478 EXPECT_EQ(2, descriptors.size()); 1586 EXPECT_EQ(2, descriptors.size());
1479 } 1587 }
1480 1588
1481 1589
1482 } // namespace text_format_unittest 1590 } // namespace text_format_unittest
1483 } // namespace protobuf 1591 } // namespace protobuf
1484 } // namespace google 1592 } // namespace google
OLDNEW
« no previous file with comments | « third_party/protobuf/src/google/protobuf/text_format.cc ('k') | third_party/protobuf/src/google/protobuf/timestamp.proto » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698