| OLD | NEW |
| 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 Loading... |
| 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> | |
| 57 #include <google/protobuf/stubs/strutil.h> | 56 #include <google/protobuf/stubs/strutil.h> |
| 58 #include <google/protobuf/stubs/substitute.h> | 57 #include <google/protobuf/stubs/substitute.h> |
| 59 #include <google/protobuf/testing/googletest.h> | 58 #include <google/protobuf/testing/googletest.h> |
| 60 #include <gtest/gtest.h> | 59 #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 Loading... |
| 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 EXPECT_TRUE(printer.PrintToString(proto_, &text)); | 160 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" | |
| 178 "repeated_string: \"foo\"\n" | 164 "repeated_string: \"foo\"\n" |
| 179 "repeated_string: \"bar\"\n" | 165 "repeated_string: \"bar\"\n" |
| 180 "repeated_nested_message {\n bb: 2\n}\n" | 166 "repeated_nested_message {\n bb: 2\n}\n" |
| 181 "repeated_nested_message {\n bb: 3\n}\n" | 167 "repeated_nested_message {\n bb: 3\n}\n" |
| 182 "repeated_nested_enum: [FOO, BAR]\n", | 168 "repeated_nested_enum: [FOO, BAR]\n", |
| 183 text); | 169 text); |
| 184 | 170 |
| 185 // Try in single-line mode. | 171 // Try in single-line mode. |
| 186 printer.SetSingleLineMode(true); | 172 printer.SetSingleLineMode(true); |
| 187 EXPECT_TRUE(printer.PrintToString(proto_, &text)); | 173 printer.PrintToString(proto_, &text); |
| 188 | 174 |
| 189 EXPECT_EQ("optional_int32: 123 " | 175 EXPECT_EQ("optional_int32: 123 " |
| 190 "repeated_int32: [456, 789] " | 176 "repeated_int32: [456, 789] " |
| 191 "repeated_string: \"foo\" " | 177 "repeated_string: \"foo\" " |
| 192 "repeated_string: \"bar\" " | 178 "repeated_string: \"bar\" " |
| 193 "repeated_nested_message { bb: 2 } " | 179 "repeated_nested_message { bb: 2 } " |
| 194 "repeated_nested_message { bb: 3 } " | 180 "repeated_nested_message { bb: 3 } " |
| 195 "repeated_nested_enum: [FOO, BAR] ", | 181 "repeated_nested_enum: [FOO, BAR] ", |
| 196 text); | 182 text); |
| 197 } | 183 } |
| (...skipping 468 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 666 | 652 |
| 667 ASSERT_EQ(2, proto_.repeated_nested_message_size()); | 653 ASSERT_EQ(2, proto_.repeated_nested_message_size()); |
| 668 EXPECT_EQ(1, proto_.repeated_nested_message(0).bb()); | 654 EXPECT_EQ(1, proto_.repeated_nested_message(0).bb()); |
| 669 EXPECT_EQ(2, proto_.repeated_nested_message(1).bb()); | 655 EXPECT_EQ(2, proto_.repeated_nested_message(1).bb()); |
| 670 | 656 |
| 671 ASSERT_EQ(2, proto_.repeatedgroup_size()); | 657 ASSERT_EQ(2, proto_.repeatedgroup_size()); |
| 672 EXPECT_EQ(3, proto_.repeatedgroup(0).a()); | 658 EXPECT_EQ(3, proto_.repeatedgroup(0).a()); |
| 673 EXPECT_EQ(4, proto_.repeatedgroup(1).a()); | 659 EXPECT_EQ(4, proto_.repeatedgroup(1).a()); |
| 674 } | 660 } |
| 675 | 661 |
| 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 | |
| 757 | 662 |
| 758 TEST_F(TextFormatTest, Comments) { | 663 TEST_F(TextFormatTest, Comments) { |
| 759 // Test that comments are ignored. | 664 // Test that comments are ignored. |
| 760 | 665 |
| 761 string parse_string = "optional_int32: 1 # a comment\n" | 666 string parse_string = "optional_int32: 1 # a comment\n" |
| 762 "optional_int64: 2 # another comment"; | 667 "optional_int64: 2 # another comment"; |
| 763 | 668 |
| 764 io::ArrayInputStream input_stream(parse_string.data(), | 669 io::ArrayInputStream input_stream(parse_string.data(), |
| 765 parse_string.size()); | 670 parse_string.size()); |
| 766 | 671 |
| (...skipping 219 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 986 EXPECT_EQ(GOOGLE_ULONGLONG(9223372036854775808), message.repeated_uint64(1)); | 891 EXPECT_EQ(GOOGLE_ULONGLONG(9223372036854775808), message.repeated_uint64(1)); |
| 987 | 892 |
| 988 ASSERT_EQ(13, message.repeated_double_size()); | 893 ASSERT_EQ(13, message.repeated_double_size()); |
| 989 EXPECT_EQ(123.0 , message.repeated_double(0)); | 894 EXPECT_EQ(123.0 , message.repeated_double(0)); |
| 990 EXPECT_EQ(123.5 , message.repeated_double(1)); | 895 EXPECT_EQ(123.5 , message.repeated_double(1)); |
| 991 EXPECT_EQ(0.125 , message.repeated_double(2)); | 896 EXPECT_EQ(0.125 , message.repeated_double(2)); |
| 992 EXPECT_EQ(1.23E17 , message.repeated_double(3)); | 897 EXPECT_EQ(1.23E17 , message.repeated_double(3)); |
| 993 EXPECT_EQ(1.235E22 , message.repeated_double(4)); | 898 EXPECT_EQ(1.235E22 , message.repeated_double(4)); |
| 994 EXPECT_EQ(1.235E-18 , message.repeated_double(5)); | 899 EXPECT_EQ(1.235E-18 , message.repeated_double(5)); |
| 995 EXPECT_EQ(123.456789, message.repeated_double(6)); | 900 EXPECT_EQ(123.456789, message.repeated_double(6)); |
| 996 EXPECT_EQ(message.repeated_double(7), | 901 EXPECT_EQ(message.repeated_double(7), numeric_limits<double>::infinity()); |
| 997 std::numeric_limits<double>::infinity()); | 902 EXPECT_EQ(message.repeated_double(8), numeric_limits<double>::infinity()); |
| 998 EXPECT_EQ(message.repeated_double(8), | 903 EXPECT_EQ(message.repeated_double(9), -numeric_limits<double>::infinity()); |
| 999 std::numeric_limits<double>::infinity()); | 904 EXPECT_EQ(message.repeated_double(10), -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()); | |
| 1004 EXPECT_TRUE(MathLimits<double>::IsNaN(message.repeated_double(11))); | 905 EXPECT_TRUE(MathLimits<double>::IsNaN(message.repeated_double(11))); |
| 1005 EXPECT_TRUE(MathLimits<double>::IsNaN(message.repeated_double(12))); | 906 EXPECT_TRUE(MathLimits<double>::IsNaN(message.repeated_double(12))); |
| 1006 | 907 |
| 1007 // Note: Since these string literals have \0's in them, we must explicitly | 908 // Note: Since these string literals have \0's in them, we must explicitly |
| 1008 // pass their sizes to string's constructor. | 909 // pass their sizes to string's constructor. |
| 1009 ASSERT_EQ(1, message.repeated_string_size()); | 910 ASSERT_EQ(1, message.repeated_string_size()); |
| 1010 EXPECT_EQ(string("\000\001\a\b\f\n\r\t\v\\\'\"", 12), | 911 EXPECT_EQ(string("\000\001\a\b\f\n\r\t\v\\\'\"", 12), |
| 1011 message.repeated_string(0)); | 912 message.repeated_string(0)); |
| 1012 } | 913 } |
| 1013 | 914 |
| (...skipping 274 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1288 | 1189 |
| 1289 #undef EXPECT_FIELD | 1190 #undef EXPECT_FIELD |
| 1290 #undef EXPECT_BOOL_FIELD | 1191 #undef EXPECT_BOOL_FIELD |
| 1291 #undef EXPECT_FLOAT_FIELD | 1192 #undef EXPECT_FLOAT_FIELD |
| 1292 #undef EXPECT_DOUBLE_FIELD | 1193 #undef EXPECT_DOUBLE_FIELD |
| 1293 #undef EXPECT_INVALID | 1194 #undef EXPECT_INVALID |
| 1294 } | 1195 } |
| 1295 | 1196 |
| 1296 | 1197 |
| 1297 TEST_F(TextFormatParserTest, InvalidToken) { | 1198 TEST_F(TextFormatParserTest, InvalidToken) { |
| 1298 ExpectFailure("optional_bool: true\n-5\n", "Expected identifier, got: -", | 1199 ExpectFailure("optional_bool: true\n-5\n", "Expected identifier.", |
| 1299 2, 1); | 1200 2, 1); |
| 1300 | 1201 |
| 1301 ExpectFailure("optional_bool: true!\n", "Expected identifier, got: !", 1, | 1202 ExpectFailure("optional_bool: true!\n", "Expected identifier.", 1, 20); |
| 1302 20); | 1203 ExpectFailure("\"some string\"", "Expected identifier.", 1, 1); |
| 1303 ExpectFailure("\"some string\"", | |
| 1304 "Expected identifier, got: \"some string\"", 1, 1); | |
| 1305 } | 1204 } |
| 1306 | 1205 |
| 1307 TEST_F(TextFormatParserTest, InvalidFieldName) { | 1206 TEST_F(TextFormatParserTest, InvalidFieldName) { |
| 1308 ExpectFailure( | 1207 ExpectFailure( |
| 1309 "invalid_field: somevalue\n", | 1208 "invalid_field: somevalue\n", |
| 1310 "Message type \"protobuf_unittest.TestAllTypes\" has no field named " | 1209 "Message type \"protobuf_unittest.TestAllTypes\" has no field named " |
| 1311 "\"invalid_field\".", | 1210 "\"invalid_field\".", |
| 1312 1, 14); | 1211 1, 14); |
| 1313 } | 1212 } |
| 1314 | 1213 |
| (...skipping 27 matching lines...) Expand all Loading... |
| 1342 // ... but are parsed correctly if we match case insensitive. | 1241 // ... but are parsed correctly if we match case insensitive. |
| 1343 parser.AllowCaseInsensitiveField(true); | 1242 parser.AllowCaseInsensitiveField(true); |
| 1344 EXPECT_TRUE(parser.ParseFromString("Optional_Double: 10.0", &proto)); | 1243 EXPECT_TRUE(parser.ParseFromString("Optional_Double: 10.0", &proto)); |
| 1345 EXPECT_EQ(10.0, proto.optional_double()); | 1244 EXPECT_EQ(10.0, proto.optional_double()); |
| 1346 EXPECT_TRUE(parser.ParseFromString("oPtIoNaLgRoUp { a: 15 }", &proto)); | 1245 EXPECT_TRUE(parser.ParseFromString("oPtIoNaLgRoUp { a: 15 }", &proto)); |
| 1347 EXPECT_EQ(15, proto.optionalgroup().a()); | 1246 EXPECT_EQ(15, proto.optionalgroup().a()); |
| 1348 } | 1247 } |
| 1349 | 1248 |
| 1350 TEST_F(TextFormatParserTest, InvalidFieldValues) { | 1249 TEST_F(TextFormatParserTest, InvalidFieldValues) { |
| 1351 // Invalid values for a double/float field. | 1250 // Invalid values for a double/float field. |
| 1352 ExpectFailure("optional_double: \"hello\"\n", | 1251 ExpectFailure("optional_double: \"hello\"\n", "Expected double.", 1, 18); |
| 1353 "Expected double, got: \"hello\"", 1, 18); | 1252 ExpectFailure("optional_double: true\n", "Expected double.", 1, 18); |
| 1354 ExpectFailure("optional_double: true\n", "Expected double, got: true", 1, | 1253 ExpectFailure("optional_double: !\n", "Expected double.", 1, 18); |
| 1355 18); | |
| 1356 ExpectFailure("optional_double: !\n", "Expected double, got: !", 1, 18); | |
| 1357 ExpectFailure("optional_double {\n \n}\n", "Expected \":\", found \"{\".", | 1254 ExpectFailure("optional_double {\n \n}\n", "Expected \":\", found \"{\".", |
| 1358 1, 17); | 1255 1, 17); |
| 1359 | 1256 |
| 1360 // Invalid values for a signed integer field. | 1257 // Invalid values for a signed integer field. |
| 1361 ExpectFailure("optional_int32: \"hello\"\n", | 1258 ExpectFailure("optional_int32: \"hello\"\n", "Expected integer.", 1, 17); |
| 1362 "Expected integer, got: \"hello\"", 1, 17); | 1259 ExpectFailure("optional_int32: true\n", "Expected integer.", 1, 17); |
| 1363 ExpectFailure("optional_int32: true\n", "Expected integer, got: true", 1, 17); | 1260 ExpectFailure("optional_int32: 4.5\n", "Expected integer.", 1, 17); |
| 1364 ExpectFailure("optional_int32: 4.5\n", "Expected integer, got: 4.5", 1, 17); | 1261 ExpectFailure("optional_int32: !\n", "Expected integer.", 1, 17); |
| 1365 ExpectFailure("optional_int32: !\n", "Expected integer, got: !", 1, 17); | |
| 1366 ExpectFailure("optional_int32 {\n \n}\n", "Expected \":\", found \"{\".", | 1262 ExpectFailure("optional_int32 {\n \n}\n", "Expected \":\", found \"{\".", |
| 1367 1, 16); | 1263 1, 16); |
| 1368 ExpectFailure("optional_int32: 0x80000000\n", | 1264 ExpectFailure("optional_int32: 0x80000000\n", |
| 1369 "Integer out of range (0x80000000)", 1, 17); | 1265 "Integer out of range.", 1, 17); |
| 1370 ExpectFailure("optional_int64: 0x8000000000000000\n", | 1266 ExpectFailure("optional_int64: 0x8000000000000000\n", |
| 1371 "Integer out of range (0x8000000000000000)", 1, 17); | 1267 "Integer out of range.", 1, 17); |
| 1372 ExpectFailure("optional_int32: -0x80000001\n", | 1268 ExpectFailure("optional_int32: -0x80000001\n", |
| 1373 "Integer out of range (0x80000001)", 1, 18); | 1269 "Integer out of range.", 1, 18); |
| 1374 ExpectFailure("optional_int64: -0x8000000000000001\n", | 1270 ExpectFailure("optional_int64: -0x8000000000000001\n", |
| 1375 "Integer out of range (0x8000000000000001)", 1, 18); | 1271 "Integer out of range.", 1, 18); |
| 1376 | 1272 |
| 1377 // Invalid values for an unsigned integer field. | 1273 // Invalid values for an unsigned integer field. |
| 1378 ExpectFailure("optional_uint64: \"hello\"\n", | 1274 ExpectFailure("optional_uint64: \"hello\"\n", "Expected integer.", 1, 18); |
| 1379 "Expected integer, got: \"hello\"", 1, 18); | 1275 ExpectFailure("optional_uint64: true\n", "Expected integer.", 1, 18); |
| 1380 ExpectFailure("optional_uint64: true\n", | 1276 ExpectFailure("optional_uint64: 4.5\n", "Expected integer.", 1, 18); |
| 1381 "Expected integer, got: true", 1, 18); | 1277 ExpectFailure("optional_uint64: -5\n", "Expected integer.", 1, 18); |
| 1382 ExpectFailure("optional_uint64: 4.5\n", "Expected integer, got: 4.5", 1, 18); | 1278 ExpectFailure("optional_uint64: !\n", "Expected integer.", 1, 18); |
| 1383 ExpectFailure("optional_uint64: -5\n", "Expected integer, got: -", 1, 18); | |
| 1384 ExpectFailure("optional_uint64: !\n", "Expected integer, got: !", 1, 18); | |
| 1385 ExpectFailure("optional_uint64 {\n \n}\n", "Expected \":\", found \"{\".", | 1279 ExpectFailure("optional_uint64 {\n \n}\n", "Expected \":\", found \"{\".", |
| 1386 1, 17); | 1280 1, 17); |
| 1387 ExpectFailure("optional_uint32: 0x100000000\n", | 1281 ExpectFailure("optional_uint32: 0x100000000\n", |
| 1388 "Integer out of range (0x100000000)", 1, 18); | 1282 "Integer out of range.", 1, 18); |
| 1389 ExpectFailure("optional_uint64: 0x10000000000000000\n", | 1283 ExpectFailure("optional_uint64: 0x10000000000000000\n", |
| 1390 "Integer out of range (0x10000000000000000)", 1, 18); | 1284 "Integer out of range.", 1, 18); |
| 1391 | 1285 |
| 1392 // Invalid values for a boolean field. | 1286 // Invalid values for a boolean field. |
| 1393 ExpectFailure("optional_bool: \"hello\"\n", | 1287 ExpectFailure("optional_bool: \"hello\"\n", "Expected identifier.", 1, 16); |
| 1394 "Expected identifier, got: \"hello\"", 1, 16); | 1288 ExpectFailure("optional_bool: 5\n", "Integer out of range.", 1, 16); |
| 1395 ExpectFailure("optional_bool: 5\n", "Integer out of range (5)", 1, 16); | 1289 ExpectFailure("optional_bool: -7.5\n", "Expected identifier.", 1, 16); |
| 1396 ExpectFailure("optional_bool: -7.5\n", "Expected identifier, got: -", 1, 16); | 1290 ExpectFailure("optional_bool: !\n", "Expected identifier.", 1, 16); |
| 1397 ExpectFailure("optional_bool: !\n", "Expected identifier, got: !", 1, 16); | |
| 1398 | 1291 |
| 1399 ExpectFailure( | 1292 ExpectFailure( |
| 1400 "optional_bool: meh\n", | 1293 "optional_bool: meh\n", |
| 1401 "Invalid value for boolean field \"optional_bool\". Value: \"meh\".", | 1294 "Invalid value for boolean field \"optional_bool\". Value: \"meh\".", |
| 1402 2, 1); | 1295 2, 1); |
| 1403 | 1296 |
| 1404 ExpectFailure("optional_bool {\n \n}\n", "Expected \":\", found \"{\".", | 1297 ExpectFailure("optional_bool {\n \n}\n", "Expected \":\", found \"{\".", |
| 1405 1, 15); | 1298 1, 15); |
| 1406 | 1299 |
| 1407 // Invalid values for a string field. | 1300 // Invalid values for a string field. |
| 1408 ExpectFailure("optional_string: true\n", "Expected string, got: true", 1, 18); | 1301 ExpectFailure("optional_string: true\n", "Expected string.", 1, 18); |
| 1409 ExpectFailure("optional_string: 5\n", "Expected string, got: 5", 1, 18); | 1302 ExpectFailure("optional_string: 5\n", "Expected string.", 1, 18); |
| 1410 ExpectFailure("optional_string: -7.5\n", "Expected string, got: -", 1, 18); | 1303 ExpectFailure("optional_string: -7.5\n", "Expected string.", 1, 18); |
| 1411 ExpectFailure("optional_string: !\n", "Expected string, got: !", 1, 18); | 1304 ExpectFailure("optional_string: !\n", "Expected string.", 1, 18); |
| 1412 ExpectFailure("optional_string {\n \n}\n", "Expected \":\", found \"{\".", | 1305 ExpectFailure("optional_string {\n \n}\n", "Expected \":\", found \"{\".", |
| 1413 1, 17); | 1306 1, 17); |
| 1414 | 1307 |
| 1415 // Invalid values for an enumeration field. | 1308 // Invalid values for an enumeration field. |
| 1416 ExpectFailure("optional_nested_enum: \"hello\"\n", | 1309 ExpectFailure("optional_nested_enum: \"hello\"\n", |
| 1417 "Expected integer or identifier, got: \"hello\"", 1, 23); | 1310 "Expected integer or identifier.", 1, 23); |
| 1418 | 1311 |
| 1419 // Valid token, but enum value is not defined. | 1312 // Valid token, but enum value is not defined. |
| 1420 ExpectFailure("optional_nested_enum: 5\n", | 1313 ExpectFailure("optional_nested_enum: 5\n", |
| 1421 "Unknown enumeration value of \"5\" for field " | 1314 "Unknown enumeration value of \"5\" for field " |
| 1422 "\"optional_nested_enum\".", 2, 1); | 1315 "\"optional_nested_enum\".", 2, 1); |
| 1423 // We consume the negative sign, so the error position starts one character | 1316 // We consume the negative sign, so the error position starts one character |
| 1424 // later. | 1317 // later. |
| 1425 ExpectFailure("optional_nested_enum: -7.5\n", "Expected integer, got: 7.5", 1, | 1318 ExpectFailure("optional_nested_enum: -7.5\n", "Expected integer.", 1, 24); |
| 1426 24); | |
| 1427 ExpectFailure("optional_nested_enum: !\n", | 1319 ExpectFailure("optional_nested_enum: !\n", |
| 1428 "Expected integer or identifier, got: !", 1, 23); | 1320 "Expected integer or identifier.", 1, 23); |
| 1429 | 1321 |
| 1430 ExpectFailure( | 1322 ExpectFailure( |
| 1431 "optional_nested_enum: grah\n", | 1323 "optional_nested_enum: grah\n", |
| 1432 "Unknown enumeration value of \"grah\" for field " | 1324 "Unknown enumeration value of \"grah\" for field " |
| 1433 "\"optional_nested_enum\".", 2, 1); | 1325 "\"optional_nested_enum\".", 2, 1); |
| 1434 | 1326 |
| 1435 ExpectFailure( | 1327 ExpectFailure( |
| 1436 "optional_nested_enum {\n \n}\n", | 1328 "optional_nested_enum {\n \n}\n", |
| 1437 "Expected \":\", found \"{\".", 1, 22); | 1329 "Expected \":\", found \"{\".", 1, 22); |
| 1438 } | 1330 } |
| 1439 | 1331 |
| 1440 TEST_F(TextFormatParserTest, MessageDelimiters) { | 1332 TEST_F(TextFormatParserTest, MessageDelimiters) { |
| 1441 // Non-matching delimiters. | 1333 // Non-matching delimiters. |
| 1442 ExpectFailure("OptionalGroup <\n \n}\n", "Expected \">\", found \"}\".", | 1334 ExpectFailure("OptionalGroup <\n \n}\n", "Expected \">\", found \"}\".", |
| 1443 3, 1); | 1335 3, 1); |
| 1444 | 1336 |
| 1445 // Invalid delimiters. | 1337 // Invalid delimiters. |
| 1446 ExpectFailure("OptionalGroup [\n \n]\n", "Expected \"{\", found \"[\".", | 1338 ExpectFailure("OptionalGroup [\n \n]\n", "Expected \"{\", found \"[\".", |
| 1447 1, 15); | 1339 1, 15); |
| 1448 | 1340 |
| 1449 // Unending message. | 1341 // Unending message. |
| 1450 ExpectFailure("optional_nested_message {\n \nbb: 118\n", | 1342 ExpectFailure("optional_nested_message {\n \nbb: 118\n", |
| 1451 "Expected identifier, got: ", | 1343 "Expected identifier.", |
| 1452 4, 1); | 1344 4, 1); |
| 1453 } | 1345 } |
| 1454 | 1346 |
| 1455 TEST_F(TextFormatParserTest, UnknownExtension) { | 1347 TEST_F(TextFormatParserTest, UnknownExtension) { |
| 1456 // Non-matching delimiters. | 1348 // Non-matching delimiters. |
| 1457 ExpectFailure("[blahblah]: 123", | 1349 ExpectFailure("[blahblah]: 123", |
| 1458 "Extension \"blahblah\" is not defined or is not an " | 1350 "Extension \"blahblah\" is not defined or is not an " |
| 1459 "extension of \"protobuf_unittest.TestAllTypes\".", | 1351 "extension of \"protobuf_unittest.TestAllTypes\".", |
| 1460 1, 11); | 1352 1, 11); |
| 1461 } | 1353 } |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1497 | 1389 |
| 1498 TEST_F(TextFormatParserTest, ExplicitDelimiters) { | 1390 TEST_F(TextFormatParserTest, ExplicitDelimiters) { |
| 1499 unittest::TestRequired message; | 1391 unittest::TestRequired message; |
| 1500 EXPECT_TRUE(TextFormat::ParseFromString("a:1,b:2;c:3", &message)); | 1392 EXPECT_TRUE(TextFormat::ParseFromString("a:1,b:2;c:3", &message)); |
| 1501 EXPECT_EQ(1, message.a()); | 1393 EXPECT_EQ(1, message.a()); |
| 1502 EXPECT_EQ(2, message.b()); | 1394 EXPECT_EQ(2, message.b()); |
| 1503 EXPECT_EQ(3, message.c()); | 1395 EXPECT_EQ(3, message.c()); |
| 1504 } | 1396 } |
| 1505 | 1397 |
| 1506 TEST_F(TextFormatParserTest, PrintErrorsToStderr) { | 1398 TEST_F(TextFormatParserTest, PrintErrorsToStderr) { |
| 1507 std::vector<string> errors; | 1399 vector<string> errors; |
| 1508 | 1400 |
| 1509 { | 1401 { |
| 1510 ScopedMemoryLog log; | 1402 ScopedMemoryLog log; |
| 1511 unittest::TestAllTypes proto; | 1403 unittest::TestAllTypes proto; |
| 1512 EXPECT_FALSE(TextFormat::ParseFromString("no_such_field: 1", &proto)); | 1404 EXPECT_FALSE(TextFormat::ParseFromString("no_such_field: 1", &proto)); |
| 1513 errors = log.GetMessages(ERROR); | 1405 errors = log.GetMessages(ERROR); |
| 1514 } | 1406 } |
| 1515 | 1407 |
| 1516 ASSERT_EQ(1, errors.size()); | 1408 ASSERT_EQ(1, errors.size()); |
| 1517 EXPECT_EQ("Error parsing text-format protobuf_unittest.TestAllTypes: " | 1409 EXPECT_EQ("Error parsing text-format protobuf_unittest.TestAllTypes: " |
| 1518 "1:14: Message type \"protobuf_unittest.TestAllTypes\" has no field
" | 1410 "1:14: Message type \"protobuf_unittest.TestAllTypes\" has no field
" |
| 1519 "named \"no_such_field\".", | 1411 "named \"no_such_field\".", |
| 1520 errors[0]); | 1412 errors[0]); |
| 1521 } | 1413 } |
| 1522 | 1414 |
| 1523 TEST_F(TextFormatParserTest, FailsOnTokenizationError) { | 1415 TEST_F(TextFormatParserTest, FailsOnTokenizationError) { |
| 1524 std::vector<string> errors; | 1416 vector<string> errors; |
| 1525 | 1417 |
| 1526 { | 1418 { |
| 1527 ScopedMemoryLog log; | 1419 ScopedMemoryLog log; |
| 1528 unittest::TestAllTypes proto; | 1420 unittest::TestAllTypes proto; |
| 1529 EXPECT_FALSE(TextFormat::ParseFromString("\020", &proto)); | 1421 EXPECT_FALSE(TextFormat::ParseFromString("\020", &proto)); |
| 1530 errors = log.GetMessages(ERROR); | 1422 errors = log.GetMessages(ERROR); |
| 1531 } | 1423 } |
| 1532 | 1424 |
| 1533 ASSERT_EQ(1, errors.size()); | 1425 ASSERT_EQ(1, errors.size()); |
| 1534 EXPECT_EQ("Error parsing text-format protobuf_unittest.TestAllTypes: " | 1426 EXPECT_EQ("Error parsing text-format protobuf_unittest.TestAllTypes: " |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1573 | 1465 |
| 1574 TEST_F(TextFormatMessageSetTest, Deserialize) { | 1466 TEST_F(TextFormatMessageSetTest, Deserialize) { |
| 1575 protobuf_unittest::TestMessageSetContainer proto; | 1467 protobuf_unittest::TestMessageSetContainer proto; |
| 1576 ASSERT_TRUE(TextFormat::ParseFromString(proto_debug_string_, &proto)); | 1468 ASSERT_TRUE(TextFormat::ParseFromString(proto_debug_string_, &proto)); |
| 1577 EXPECT_EQ(23, proto.message_set().GetExtension( | 1469 EXPECT_EQ(23, proto.message_set().GetExtension( |
| 1578 protobuf_unittest::TestMessageSetExtension1::message_set_extension).i()); | 1470 protobuf_unittest::TestMessageSetExtension1::message_set_extension).i()); |
| 1579 EXPECT_EQ("foo", proto.message_set().GetExtension( | 1471 EXPECT_EQ("foo", proto.message_set().GetExtension( |
| 1580 protobuf_unittest::TestMessageSetExtension2::message_set_extension).str()); | 1472 protobuf_unittest::TestMessageSetExtension2::message_set_extension).str()); |
| 1581 | 1473 |
| 1582 // Ensure that these are the only entries present. | 1474 // Ensure that these are the only entries present. |
| 1583 std::vector<const FieldDescriptor*> descriptors; | 1475 vector<const FieldDescriptor*> descriptors; |
| 1584 proto.message_set().GetReflection()->ListFields( | 1476 proto.message_set().GetReflection()->ListFields( |
| 1585 proto.message_set(), &descriptors); | 1477 proto.message_set(), &descriptors); |
| 1586 EXPECT_EQ(2, descriptors.size()); | 1478 EXPECT_EQ(2, descriptors.size()); |
| 1587 } | 1479 } |
| 1588 | 1480 |
| 1589 | 1481 |
| 1590 } // namespace text_format_unittest | 1482 } // namespace text_format_unittest |
| 1591 } // namespace protobuf | 1483 } // namespace protobuf |
| 1592 } // namespace google | 1484 } // namespace google |
| OLD | NEW |