Index: third_party/protobuf/src/google/protobuf/text_format_unittest.cc |
=================================================================== |
--- third_party/protobuf/src/google/protobuf/text_format_unittest.cc (revision 216642) |
+++ third_party/protobuf/src/google/protobuf/text_format_unittest.cc (working copy) |
@@ -339,6 +339,21 @@ |
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); |
+} |
+ |
TEST_F(TextFormatTest, ParseBasic) { |
io::ArrayInputStream input_stream(proto_debug_string_.data(), |
proto_debug_string_.size()); |
@@ -728,6 +743,25 @@ |
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 { |
@@ -749,6 +783,71 @@ |
}; |
}; |
+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(); |
@@ -921,10 +1020,10 @@ |
1, 16); |
ExpectFailure("optional_int32: 0x80000000\n", |
"Integer out of range.", 1, 17); |
+ 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: 0x8000000000000000\n", |
- "Integer out of range.", 1, 17); |
ExpectFailure("optional_int64: -0x8000000000000001\n", |
"Integer out of range.", 1, 18); |
@@ -1136,6 +1235,7 @@ |
EXPECT_EQ(2, descriptors.size()); |
} |
+ |
} // namespace text_format_unittest |
} // namespace protobuf |
} // namespace google |