diff options
Diffstat (limited to 'src/google/protobuf/text_format_unittest.cc')
-rw-r--r-- | src/google/protobuf/text_format_unittest.cc | 459 |
1 files changed, 432 insertions, 27 deletions
diff --git a/src/google/protobuf/text_format_unittest.cc b/src/google/protobuf/text_format_unittest.cc index ddf8ff7..b3cf7b8 100644 --- a/src/google/protobuf/text_format_unittest.cc +++ b/src/google/protobuf/text_format_unittest.cc @@ -74,10 +74,11 @@ const string kEscapeTestStringEscaped = class TextFormatTest : public testing::Test { public: static void SetUpTestCase() { - File::ReadFileToStringOrDie( - TestSourceDir() - + "/google/protobuf/testdata/text_format_unittest_data.txt", - &static_proto_debug_string_); + GOOGLE_CHECK_OK(File::GetContents( + TestSourceDir() + + "/google/protobuf/" + "testdata/text_format_unittest_data_oneof_implemented.txt", + &static_proto_debug_string_, true)); } TextFormatTest() : proto_debug_string_(static_proto_debug_string_) {} @@ -95,11 +96,10 @@ string TextFormatTest::static_proto_debug_string_; class TextFormatExtensionsTest : public testing::Test { public: static void SetUpTestCase() { - File::ReadFileToStringOrDie( - TestSourceDir() - + "/google/protobuf/testdata/" - "text_format_unittest_extensions_data.txt", - &static_proto_debug_string_); + GOOGLE_CHECK_OK(File::GetContents(TestSourceDir() + + "/google/protobuf/testdata/" + "text_format_unittest_extensions_data.txt", + &static_proto_debug_string_, true)); } TextFormatExtensionsTest() @@ -205,16 +205,25 @@ TEST_F(TextFormatTest, StringEscape) { TEST_F(TextFormatTest, Utf8DebugString) { // Set the string value to test. proto_.set_optional_string("\350\260\267\346\255\214"); + proto_.set_optional_bytes("\350\260\267\346\255\214"); // Get the DebugString from the proto. string debug_string = proto_.DebugString(); string utf8_debug_string = proto_.Utf8DebugString(); // Hardcode a correct value to test against. - string correct_utf8_string = "optional_string: " + string correct_utf8_string = + "optional_string: " "\"\350\260\267\346\255\214\"" + "\n" + "optional_bytes: " + "\"\\350\\260\\267\\346\\255\\214\"" "\n"; - string correct_string = "optional_string: " + string correct_string = + "optional_string: " + "\"\\350\\260\\267\\346\\255\\214\"" + "\n" + "optional_bytes: " "\"\\350\\260\\267\\346\\255\\214\"" "\n"; @@ -253,6 +262,31 @@ TEST_F(TextFormatTest, PrintUnknownFields) { message.DebugString()); } +TEST_F(TextFormatTest, PrintUnknownFieldsHidden) { + // Test printing of unknown fields in a message when supressed. + + unittest::OneString message; + message.set_data("data"); + UnknownFieldSet* unknown_fields = message.mutable_unknown_fields(); + + unknown_fields->AddVarint(5, 1); + unknown_fields->AddFixed32(5, 2); + unknown_fields->AddFixed64(5, 3); + unknown_fields->AddLengthDelimited(5, "4"); + unknown_fields->AddGroup(5)->AddVarint(10, 5); + + unknown_fields->AddVarint(8, 1); + unknown_fields->AddVarint(8, 2); + unknown_fields->AddVarint(8, 3); + + TextFormat::Printer printer; + printer.SetHideUnknownFields(true); + string output; + printer.PrintToString(message, &output); + + EXPECT_EQ("data: \"data\"\n", output); +} + TEST_F(TextFormatTest, PrintUnknownMessage) { // Test heuristic printing of messages in an UnknownFieldSet. @@ -339,6 +373,161 @@ TEST_F(TextFormatTest, PrintMessageSingleLine) { text); } +TEST_F(TextFormatTest, PrintBufferTooSmall) { + // Test printing a message to a buffer that is too small. + + protobuf_unittest::TestAllTypes message; + + message.add_repeated_string("abc"); + message.add_repeated_string("def"); + + char buffer[1] = ""; + io::ArrayOutputStream output_stream(buffer, 1); + EXPECT_FALSE(TextFormat::Print(message, &output_stream)); + EXPECT_EQ(buffer[0], 'r'); + EXPECT_EQ(output_stream.ByteCount(), 1); +} + +// A printer that appends 'u' to all unsigned int32. +class CustomUInt32FieldValuePrinter : public TextFormat::FieldValuePrinter { + public: + virtual string PrintUInt32(uint32 val) const { + return StrCat(FieldValuePrinter::PrintUInt32(val), "u"); + } +}; + +TEST_F(TextFormatTest, DefaultCustomFieldPrinter) { + protobuf_unittest::TestAllTypes message; + + message.set_optional_uint32(42); + message.add_repeated_uint32(1); + message.add_repeated_uint32(2); + message.add_repeated_uint32(3); + + TextFormat::Printer printer; + printer.SetDefaultFieldValuePrinter(new CustomUInt32FieldValuePrinter()); + // Let's see if that works well together with the repeated primitives: + printer.SetUseShortRepeatedPrimitives(true); + string text; + printer.PrintToString(message, &text); + EXPECT_EQ("optional_uint32: 42u\nrepeated_uint32: [1u, 2u, 3u]\n", text); +} + +class CustomInt32FieldValuePrinter : public TextFormat::FieldValuePrinter { + public: + virtual string PrintInt32(int32 val) const { + return StrCat("value-is(", FieldValuePrinter::PrintInt32(val), ")"); + } +}; + +TEST_F(TextFormatTest, FieldSpecificCustomPrinter) { + protobuf_unittest::TestAllTypes message; + + message.set_optional_int32(42); // This will be handled by our Printer. + message.add_repeated_int32(42); // This will be printed as number. + + TextFormat::Printer printer; + EXPECT_TRUE(printer.RegisterFieldValuePrinter( + message.GetDescriptor()->FindFieldByName("optional_int32"), + new CustomInt32FieldValuePrinter())); + string text; + printer.PrintToString(message, &text); + EXPECT_EQ("optional_int32: value-is(42)\nrepeated_int32: 42\n", text); +} + +TEST_F(TextFormatTest, ErrorCasesRegisteringFieldValuePrinterShouldFail) { + protobuf_unittest::TestAllTypes message; + TextFormat::Printer printer; + // NULL printer. + EXPECT_FALSE(printer.RegisterFieldValuePrinter( + message.GetDescriptor()->FindFieldByName("optional_int32"), + NULL)); + // Because registration fails, the ownership of this printer is never taken. + TextFormat::FieldValuePrinter my_field_printer; + // NULL field + EXPECT_FALSE(printer.RegisterFieldValuePrinter(NULL, &my_field_printer)); +} + +class CustomMessageFieldValuePrinter : public TextFormat::FieldValuePrinter { + public: + virtual string PrintInt32(int32 v) const { + return StrCat(FieldValuePrinter::PrintInt32(v), " # x", ToHex(v)); + } + + virtual string PrintMessageStart(const Message& message, + int field_index, + int field_count, + bool single_line_mode) const { + if (single_line_mode) { + return " { "; + } + return StrCat( + " { # ", message.GetDescriptor()->name(), ": ", field_index, "\n"); + } +}; + +TEST_F(TextFormatTest, CustomPrinterForComments) { + protobuf_unittest::TestAllTypes message; + message.mutable_optional_nested_message(); + message.mutable_optional_import_message()->set_d(42); + message.add_repeated_nested_message(); + message.add_repeated_nested_message(); + message.add_repeated_import_message()->set_d(43); + message.add_repeated_import_message()->set_d(44); + TextFormat::Printer printer; + CustomMessageFieldValuePrinter my_field_printer; + printer.SetDefaultFieldValuePrinter(new CustomMessageFieldValuePrinter()); + string text; + printer.PrintToString(message, &text); + EXPECT_EQ( + "optional_nested_message { # NestedMessage: -1\n" + "}\n" + "optional_import_message { # ImportMessage: -1\n" + " d: 42 # x2a\n" + "}\n" + "repeated_nested_message { # NestedMessage: 0\n" + "}\n" + "repeated_nested_message { # NestedMessage: 1\n" + "}\n" + "repeated_import_message { # ImportMessage: 0\n" + " d: 43 # x2b\n" + "}\n" + "repeated_import_message { # ImportMessage: 1\n" + " d: 44 # x2c\n" + "}\n", + text); +} + +class CustomMultilineCommentPrinter : public TextFormat::FieldValuePrinter { + public: + virtual string PrintMessageStart(const Message& message, + int field_index, + int field_count, + bool single_line_comment) const { + return StrCat(" { # 1\n", " # 2\n"); + } +}; + +TEST_F(TextFormatTest, CustomPrinterForMultilineComments) { + protobuf_unittest::TestAllTypes message; + message.mutable_optional_nested_message(); + message.mutable_optional_import_message()->set_d(42); + TextFormat::Printer printer; + CustomMessageFieldValuePrinter my_field_printer; + printer.SetDefaultFieldValuePrinter(new CustomMultilineCommentPrinter()); + string text; + printer.PrintToString(message, &text); + EXPECT_EQ( + "optional_nested_message { # 1\n" + " # 2\n" + "}\n" + "optional_import_message { # 1\n" + " # 2\n" + " d: 42\n" + "}\n", + text); +} + TEST_F(TextFormatTest, ParseBasic) { io::ArrayInputStream input_stream(proto_debug_string_.data(), proto_debug_string_.size()); @@ -353,6 +542,25 @@ TEST_F(TextFormatExtensionsTest, ParseExtensions) { TestUtil::ExpectAllExtensionsSet(proto_); } +TEST_F(TextFormatTest, ParseEnumFieldFromNumber) { + // Create a parse string with a numerical value for an enum field. + string parse_string = strings::Substitute("optional_nested_enum: $0", + unittest::TestAllTypes::BAZ); + EXPECT_TRUE(TextFormat::ParseFromString(parse_string, &proto_)); + EXPECT_TRUE(proto_.has_optional_nested_enum()); + EXPECT_EQ(unittest::TestAllTypes::BAZ, proto_.optional_nested_enum()); +} + +TEST_F(TextFormatTest, ParseEnumFieldFromNegativeNumber) { + ASSERT_LT(unittest::SPARSE_E, 0); + string parse_string = strings::Substitute("sparse_enum: $0", + unittest::SPARSE_E); + unittest::SparseEnumMessage proto; + EXPECT_TRUE(TextFormat::ParseFromString(parse_string, &proto)); + EXPECT_TRUE(proto.has_sparse_enum()); + EXPECT_EQ(unittest::SPARSE_E, proto.sparse_enum()); +} + TEST_F(TextFormatTest, ParseStringEscape) { // Create a parse string with escpaed characters in it. string parse_string = "optional_string: " @@ -406,6 +614,47 @@ TEST_F(TextFormatTest, ParseFloatWithSuffix) { EXPECT_EQ(1.0, proto_.optional_float()); } +TEST_F(TextFormatTest, ParseShortRepeatedForm) { + string parse_string = + // Mixed short-form and long-form are simply concatenated. + "repeated_int32: 1\n" + "repeated_int32: [456, 789]\n" + "repeated_nested_enum: [ FOO ,BAR, # comment\n" + " 3]\n" + // Note that while the printer won't print repeated strings in short-form, + // the parser will accept them. + "repeated_string: [ \"foo\", 'bar' ]\n" + // Repeated message + "repeated_nested_message: [ { bb: 1 }, { bb : 2 }]\n" + // Repeated group + "RepeatedGroup [{ a: 3 },{ a: 4 }]\n"; + + ASSERT_TRUE(TextFormat::ParseFromString(parse_string, &proto_)); + + ASSERT_EQ(3, proto_.repeated_int32_size()); + EXPECT_EQ(1, proto_.repeated_int32(0)); + EXPECT_EQ(456, proto_.repeated_int32(1)); + EXPECT_EQ(789, proto_.repeated_int32(2)); + + ASSERT_EQ(3, proto_.repeated_nested_enum_size()); + EXPECT_EQ(unittest::TestAllTypes::FOO, proto_.repeated_nested_enum(0)); + EXPECT_EQ(unittest::TestAllTypes::BAR, proto_.repeated_nested_enum(1)); + EXPECT_EQ(unittest::TestAllTypes::BAZ, proto_.repeated_nested_enum(2)); + + ASSERT_EQ(2, proto_.repeated_string_size()); + EXPECT_EQ("foo", proto_.repeated_string(0)); + EXPECT_EQ("bar", proto_.repeated_string(1)); + + ASSERT_EQ(2, proto_.repeated_nested_message_size()); + EXPECT_EQ(1, proto_.repeated_nested_message(0).bb()); + EXPECT_EQ(2, proto_.repeated_nested_message(1).bb()); + + ASSERT_EQ(2, proto_.repeatedgroup_size()); + EXPECT_EQ(3, proto_.repeatedgroup(0).a()); + EXPECT_EQ(4, proto_.repeatedgroup(1).a()); +} + + TEST_F(TextFormatTest, Comments) { // Test that comments are ignored. @@ -658,6 +907,27 @@ TEST_F(TextFormatTest, ParseExotic) { message.repeated_string(0)); } +TEST_F(TextFormatTest, PrintFieldsInIndexOrder) { + protobuf_unittest::TestFieldOrderings message; + // Fields are listed in index order instead of field number. + message.set_my_string("Test String"); // Field number 11 + message.set_my_int(12345); // Field number 1 + message.set_my_float(0.999); // Field number 101 + TextFormat::Printer printer; + string text; + + // By default, print in field number order. + printer.PrintToString(message, &text); + EXPECT_EQ("my_int: 12345\nmy_string: \"Test String\"\nmy_float: 0.999\n", + text); + + // Print in index order. + printer.SetPrintMessageFieldsInIndexOrder(true); + printer.PrintToString(message, &text); + EXPECT_EQ("my_string: \"Test String\"\nmy_int: 12345\nmy_float: 0.999\n", + text); +} + class TextFormatParserTest : public testing::Test { protected: void ExpectFailure(const string& input, const string& message, int line, @@ -676,11 +946,31 @@ class TextFormatParserTest : public testing::Test { TextFormat::Parser parser; MockErrorCollector error_collector; parser.RecordErrorsTo(&error_collector); - EXPECT_EQ(parser.ParseFromString(input, proto), expected_result); + EXPECT_EQ(expected_result, parser.ParseFromString(input, proto)) + << input << " -> " << proto->DebugString(); EXPECT_EQ(SimpleItoa(line) + ":" + SimpleItoa(col) + ": " + message + "\n", error_collector.text_); } + void ExpectSuccessAndTree(const string& input, Message* proto, + TextFormat::ParseInfoTree* info_tree) { + TextFormat::Parser parser; + MockErrorCollector error_collector; + parser.RecordErrorsTo(&error_collector); + parser.WriteLocationsTo(info_tree); + + EXPECT_TRUE(parser.ParseFromString(input, proto)); + } + + void ExpectLocation(TextFormat::ParseInfoTree* tree, + const Descriptor* d, const string& field_name, + int index, int line, int column) { + TextFormat::ParseLocation location = tree->GetLocation( + d->FindFieldByName(field_name), index); + EXPECT_EQ(line, location.line); + EXPECT_EQ(column, location.column); + } + // An error collector which simply concatenates all its errors into a big // block of text which can be checked. class MockErrorCollector : public io::ErrorCollector { @@ -702,6 +992,71 @@ class TextFormatParserTest : public testing::Test { }; }; +TEST_F(TextFormatParserTest, ParseInfoTreeBuilding) { + scoped_ptr<unittest::TestAllTypes> message(new unittest::TestAllTypes); + const Descriptor* d = message->GetDescriptor(); + + string stringData = + "optional_int32: 1\n" + "optional_int64: 2\n" + " optional_double: 2.4\n" + "repeated_int32: 5\n" + "repeated_int32: 10\n" + "optional_nested_message <\n" + " bb: 78\n" + ">\n" + "repeated_nested_message <\n" + " bb: 79\n" + ">\n" + "repeated_nested_message <\n" + " bb: 80\n" + ">"; + + + TextFormat::ParseInfoTree tree; + ExpectSuccessAndTree(stringData, message.get(), &tree); + + // Verify that the tree has the correct positions. + ExpectLocation(&tree, d, "optional_int32", -1, 0, 0); + ExpectLocation(&tree, d, "optional_int64", -1, 1, 0); + ExpectLocation(&tree, d, "optional_double", -1, 2, 2); + + ExpectLocation(&tree, d, "repeated_int32", 0, 3, 0); + ExpectLocation(&tree, d, "repeated_int32", 1, 4, 0); + + ExpectLocation(&tree, d, "optional_nested_message", -1, 5, 0); + ExpectLocation(&tree, d, "repeated_nested_message", 0, 8, 0); + ExpectLocation(&tree, d, "repeated_nested_message", 1, 11, 0); + + // Check for fields not set. For an invalid field, the location returned + // should be -1, -1. + ExpectLocation(&tree, d, "repeated_int64", 0, -1, -1); + ExpectLocation(&tree, d, "repeated_int32", 6, -1, -1); + ExpectLocation(&tree, d, "some_unknown_field", -1, -1, -1); + + // Verify inside the nested message. + const FieldDescriptor* nested_field = + d->FindFieldByName("optional_nested_message"); + + TextFormat::ParseInfoTree* nested_tree = + tree.GetTreeForNested(nested_field, -1); + ExpectLocation(nested_tree, nested_field->message_type(), "bb", -1, 6, 2); + + // Verify inside another nested message. + nested_field = d->FindFieldByName("repeated_nested_message"); + nested_tree = tree.GetTreeForNested(nested_field, 0); + ExpectLocation(nested_tree, nested_field->message_type(), "bb", -1, 9, 2); + + nested_tree = tree.GetTreeForNested(nested_field, 1); + ExpectLocation(nested_tree, nested_field->message_type(), "bb", -1, 12, 2); + + // Verify a NULL tree for an unknown nested field. + TextFormat::ParseInfoTree* unknown_nested_tree = + tree.GetTreeForNested(nested_field, 2); + + EXPECT_EQ(NULL, unknown_nested_tree); +} + TEST_F(TextFormatParserTest, ParseFieldValueFromString) { scoped_ptr<unittest::TestAllTypes> message(new unittest::TestAllTypes); const Descriptor* d = message->GetDescriptor(); @@ -712,6 +1067,12 @@ TEST_F(TextFormatParserTest, ParseFieldValueFromString) { EXPECT_EQ(value, message->optional_##name()); \ EXPECT_TRUE(message->has_optional_##name()); +#define EXPECT_BOOL_FIELD(name, value, valuestring) \ + EXPECT_TRUE(TextFormat::ParseFieldValueFromString( \ + valuestring, d->FindFieldByName("optional_" #name), message.get())); \ + EXPECT_TRUE(message->optional_##name() == value); \ + EXPECT_TRUE(message->has_optional_##name()); + #define EXPECT_FLOAT_FIELD(name, value, valuestring) \ EXPECT_TRUE(TextFormat::ParseFieldValueFromString( \ valuestring, d->FindFieldByName("optional_" #name), message.get())); \ @@ -769,12 +1130,20 @@ TEST_F(TextFormatParserTest, ParseFieldValueFromString) { EXPECT_INVALID(fixed64, "1,2"); // bool - EXPECT_FIELD(bool, true, "true"); - EXPECT_FIELD(bool, false, "false"); - EXPECT_INVALID(bool, "1"); + EXPECT_BOOL_FIELD(bool, true, "true"); + EXPECT_BOOL_FIELD(bool, false, "false"); + EXPECT_BOOL_FIELD(bool, true, "1"); + EXPECT_BOOL_FIELD(bool, true, "t"); + EXPECT_BOOL_FIELD(bool, false, "0"); + EXPECT_BOOL_FIELD(bool, false, "f"); + EXPECT_FIELD(bool, true, "True"); + EXPECT_FIELD(bool, false, "False"); + EXPECT_INVALID(bool, "tRue"); + EXPECT_INVALID(bool, "faLse"); + EXPECT_INVALID(bool, "2"); + EXPECT_INVALID(bool, "-0"); EXPECT_INVALID(bool, "on"); EXPECT_INVALID(bool, "a"); - EXPECT_INVALID(bool, "True"); // float EXPECT_FIELD(float, 1, "1"); @@ -791,6 +1160,9 @@ TEST_F(TextFormatParserTest, ParseFieldValueFromString) { EXPECT_DOUBLE_FIELD(double, 3e5, "3e5"); EXPECT_INVALID(double, "a"); EXPECT_INVALID(double, "1,2"); + // Rejects hex and oct numbers for a double field. + EXPECT_INVALID(double, "0xf"); + EXPECT_INVALID(double, "012"); // string EXPECT_FIELD(string, "hello", "\"hello\""); @@ -799,7 +1171,8 @@ TEST_F(TextFormatParserTest, ParseFieldValueFromString) { // enum EXPECT_FIELD(nested_enum, unittest::TestAllTypes::BAR, "BAR"); - EXPECT_INVALID(nested_enum, "1"); // number not supported + EXPECT_FIELD(nested_enum, unittest::TestAllTypes::BAZ, + SimpleItoa(unittest::TestAllTypes::BAZ)); EXPECT_INVALID(nested_enum, "FOOBAR"); // message @@ -810,6 +1183,7 @@ TEST_F(TextFormatParserTest, ParseFieldValueFromString) { EXPECT_INVALID(nested_message, "any"); #undef EXPECT_FIELD +#undef EXPECT_BOOL_FIELD #undef EXPECT_FLOAT_FIELD #undef EXPECT_DOUBLE_FIELD #undef EXPECT_INVALID @@ -820,7 +1194,7 @@ TEST_F(TextFormatParserTest, InvalidToken) { ExpectFailure("optional_bool: true\n-5\n", "Expected identifier.", 2, 1); - ExpectFailure("optional_bool: true;\n", "Expected identifier.", 1, 20); + ExpectFailure("optional_bool: true!\n", "Expected identifier.", 1, 20); ExpectFailure("\"some string\"", "Expected identifier.", 1, 1); } @@ -851,6 +1225,22 @@ TEST_F(TextFormatParserTest, InvalidCapitalization) { 1, 16); } +TEST_F(TextFormatParserTest, AllowIgnoreCapitalizationError) { + TextFormat::Parser parser; + protobuf_unittest::TestAllTypes proto; + + // These fields have a mismatching case. + EXPECT_FALSE(parser.ParseFromString("Optional_Double: 10.0", &proto)); + EXPECT_FALSE(parser.ParseFromString("oPtIoNaLgRoUp { a: 15 }", &proto)); + + // ... but are parsed correctly if we match case insensitive. + parser.AllowCaseInsensitiveField(true); + EXPECT_TRUE(parser.ParseFromString("Optional_Double: 10.0", &proto)); + EXPECT_EQ(10.0, proto.optional_double()); + EXPECT_TRUE(parser.ParseFromString("oPtIoNaLgRoUp { a: 15 }", &proto)); + EXPECT_EQ(15, proto.optionalgroup().a()); +} + TEST_F(TextFormatParserTest, InvalidFieldValues) { // Invalid values for a double/float field. ExpectFailure("optional_double: \"hello\"\n", "Expected double.", 1, 18); @@ -868,10 +1258,10 @@ TEST_F(TextFormatParserTest, InvalidFieldValues) { 1, 16); ExpectFailure("optional_int32: 0x80000000\n", "Integer out of range.", 1, 17); - ExpectFailure("optional_int32: -0x80000001\n", - "Integer out of range.", 1, 18); ExpectFailure("optional_int64: 0x8000000000000000\n", "Integer out of range.", 1, 17); + ExpectFailure("optional_int32: -0x80000001\n", + "Integer out of range.", 1, 18); ExpectFailure("optional_int64: -0x8000000000000001\n", "Integer out of range.", 1, 18); @@ -890,7 +1280,7 @@ TEST_F(TextFormatParserTest, InvalidFieldValues) { // Invalid values for a boolean field. ExpectFailure("optional_bool: \"hello\"\n", "Expected identifier.", 1, 16); - ExpectFailure("optional_bool: 5\n", "Expected identifier.", 1, 16); + ExpectFailure("optional_bool: 5\n", "Integer out of range.", 1, 16); ExpectFailure("optional_bool: -7.5\n", "Expected identifier.", 1, 16); ExpectFailure("optional_bool: !\n", "Expected identifier.", 1, 16); @@ -911,12 +1301,18 @@ TEST_F(TextFormatParserTest, InvalidFieldValues) { 1, 17); // Invalid values for an enumeration field. - ExpectFailure("optional_nested_enum: \"hello\"\n", "Expected identifier.", - 1, 23); - - ExpectFailure("optional_nested_enum: 5\n", "Expected identifier.", 1, 23); - ExpectFailure("optional_nested_enum: -7.5\n", "Expected identifier.", 1, 23); - ExpectFailure("optional_nested_enum: !\n", "Expected identifier.", 1, 23); + ExpectFailure("optional_nested_enum: \"hello\"\n", + "Expected integer or identifier.", 1, 23); + + // Valid token, but enum value is not defined. + ExpectFailure("optional_nested_enum: 5\n", + "Unknown enumeration value of \"5\" for field " + "\"optional_nested_enum\".", 2, 1); + // We consume the negative sign, so the error position starts one character + // later. + ExpectFailure("optional_nested_enum: -7.5\n", "Expected integer.", 1, 24); + ExpectFailure("optional_nested_enum: !\n", + "Expected integer or identifier.", 1, 23); ExpectFailure( "optional_nested_enum: grah\n", @@ -986,6 +1382,14 @@ TEST_F(TextFormatParserTest, MergeDuplicateOptional) { EXPECT_EQ(2, message.c()); } +TEST_F(TextFormatParserTest, ExplicitDelimiters) { + unittest::TestRequired message; + EXPECT_TRUE(TextFormat::ParseFromString("a:1,b:2;c:3", &message)); + EXPECT_EQ(1, message.a()); + EXPECT_EQ(2, message.b()); + EXPECT_EQ(3, message.c()); +} + TEST_F(TextFormatParserTest, PrintErrorsToStderr) { vector<string> errors; @@ -1069,6 +1473,7 @@ TEST_F(TextFormatMessageSetTest, Deserialize) { EXPECT_EQ(2, descriptors.size()); } + } // namespace text_format_unittest } // namespace protobuf } // namespace google |