blob: f937b9eac253c4b56426b0ac441fa60ecccc946e [file] [log] [blame]
// Protocol Buffers - Google's data interchange format
// Copyright 2008 Google Inc. All rights reserved.
// https://developers.google.com/protocol-buffers/
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// Author: kenton@google.com (Kenton Varda)
// Based on original Protocol Buffers design by
// Sanjay Ghemawat, Jeff Dean, and others.
//
// This file makes extensive use of RFC 3092. :)
#include <memory>
#ifndef _SHARED_PTR_H
#include <google/protobuf/stubs/shared_ptr.h>
#endif
#include <vector>
#include <google/protobuf/compiler/importer.h>
#include <google/protobuf/unittest.pb.h>
#include <google/protobuf/unittest_custom_options.pb.h>
#include <google/protobuf/io/zero_copy_stream_impl.h>
#include <google/protobuf/descriptor.pb.h>
#include <google/protobuf/descriptor.h>
#include <google/protobuf/descriptor_database.h>
#include <google/protobuf/dynamic_message.h>
#include <google/protobuf/text_format.h>
#include <google/protobuf/stubs/strutil.h>
#include <google/protobuf/stubs/substitute.h>
#include <google/protobuf/stubs/common.h>
#include <google/protobuf/stubs/logging.h>
#include <google/protobuf/stubs/logging.h>
#include <google/protobuf/stubs/scoped_ptr.h>
#include <google/protobuf/testing/googletest.h>
#include <gtest/gtest.h>
namespace google {
namespace protobuf {
// Can't use an anonymous namespace here due to brokenness of Tru64 compiler.
namespace descriptor_unittest {
// Some helpers to make assembling descriptors faster.
DescriptorProto* AddMessage(FileDescriptorProto* file, const string& name) {
DescriptorProto* result = file->add_message_type();
result->set_name(name);
return result;
}
DescriptorProto* AddNestedMessage(DescriptorProto* parent, const string& name) {
DescriptorProto* result = parent->add_nested_type();
result->set_name(name);
return result;
}
EnumDescriptorProto* AddEnum(FileDescriptorProto* file, const string& name) {
EnumDescriptorProto* result = file->add_enum_type();
result->set_name(name);
return result;
}
EnumDescriptorProto* AddNestedEnum(DescriptorProto* parent,
const string& name) {
EnumDescriptorProto* result = parent->add_enum_type();
result->set_name(name);
return result;
}
ServiceDescriptorProto* AddService(FileDescriptorProto* file,
const string& name) {
ServiceDescriptorProto* result = file->add_service();
result->set_name(name);
return result;
}
FieldDescriptorProto* AddField(DescriptorProto* parent,
const string& name, int number,
FieldDescriptorProto::Label label,
FieldDescriptorProto::Type type) {
FieldDescriptorProto* result = parent->add_field();
result->set_name(name);
result->set_number(number);
result->set_label(label);
result->set_type(type);
return result;
}
FieldDescriptorProto* AddExtension(FileDescriptorProto* file,
const string& extendee,
const string& name, int number,
FieldDescriptorProto::Label label,
FieldDescriptorProto::Type type) {
FieldDescriptorProto* result = file->add_extension();
result->set_name(name);
result->set_number(number);
result->set_label(label);
result->set_type(type);
result->set_extendee(extendee);
return result;
}
FieldDescriptorProto* AddNestedExtension(DescriptorProto* parent,
const string& extendee,
const string& name, int number,
FieldDescriptorProto::Label label,
FieldDescriptorProto::Type type) {
FieldDescriptorProto* result = parent->add_extension();
result->set_name(name);
result->set_number(number);
result->set_label(label);
result->set_type(type);
result->set_extendee(extendee);
return result;
}
DescriptorProto::ExtensionRange* AddExtensionRange(DescriptorProto* parent,
int start, int end) {
DescriptorProto::ExtensionRange* result = parent->add_extension_range();
result->set_start(start);
result->set_end(end);
return result;
}
DescriptorProto::ReservedRange* AddReservedRange(DescriptorProto* parent,
int start, int end) {
DescriptorProto::ReservedRange* result = parent->add_reserved_range();
result->set_start(start);
result->set_end(end);
return result;
}
EnumValueDescriptorProto* AddEnumValue(EnumDescriptorProto* enum_proto,
const string& name, int number) {
EnumValueDescriptorProto* result = enum_proto->add_value();
result->set_name(name);
result->set_number(number);
return result;
}
MethodDescriptorProto* AddMethod(ServiceDescriptorProto* service,
const string& name,
const string& input_type,
const string& output_type) {
MethodDescriptorProto* result = service->add_method();
result->set_name(name);
result->set_input_type(input_type);
result->set_output_type(output_type);
return result;
}
// Empty enums technically aren't allowed. We need to insert a dummy value
// into them.
void AddEmptyEnum(FileDescriptorProto* file, const string& name) {
AddEnumValue(AddEnum(file, name), name + "_DUMMY", 1);
}
class MockErrorCollector : public DescriptorPool::ErrorCollector {
public:
MockErrorCollector() {}
~MockErrorCollector() {}
string text_;
string warning_text_;
// implements ErrorCollector ---------------------------------------
void AddError(const string& filename,
const string& element_name, const Message* descriptor,
ErrorLocation location, const string& message) {
const char* location_name = NULL;
switch (location) {
case NAME : location_name = "NAME" ; break;
case NUMBER : location_name = "NUMBER" ; break;
case TYPE : location_name = "TYPE" ; break;
case EXTENDEE : location_name = "EXTENDEE" ; break;
case DEFAULT_VALUE: location_name = "DEFAULT_VALUE"; break;
case OPTION_NAME : location_name = "OPTION_NAME" ; break;
case OPTION_VALUE : location_name = "OPTION_VALUE" ; break;
case INPUT_TYPE : location_name = "INPUT_TYPE" ; break;
case OUTPUT_TYPE : location_name = "OUTPUT_TYPE" ; break;
case OTHER : location_name = "OTHER" ; break;
}
strings::SubstituteAndAppend(
&text_, "$0: $1: $2: $3\n",
filename, element_name, location_name, message);
}
// implements ErrorCollector ---------------------------------------
void AddWarning(const string& filename, const string& element_name,
const Message* descriptor, ErrorLocation location,
const string& message) {
const char* location_name = NULL;
switch (location) {
case NAME : location_name = "NAME" ; break;
case NUMBER : location_name = "NUMBER" ; break;
case TYPE : location_name = "TYPE" ; break;
case EXTENDEE : location_name = "EXTENDEE" ; break;
case DEFAULT_VALUE: location_name = "DEFAULT_VALUE"; break;
case OPTION_NAME : location_name = "OPTION_NAME" ; break;
case OPTION_VALUE : location_name = "OPTION_VALUE" ; break;
case INPUT_TYPE : location_name = "INPUT_TYPE" ; break;
case OUTPUT_TYPE : location_name = "OUTPUT_TYPE" ; break;
case OTHER : location_name = "OTHER" ; break;
}
strings::SubstituteAndAppend(
&warning_text_, "$0: $1: $2: $3\n",
filename, element_name, location_name, message);
}
};
// ===================================================================
// Test simple files.
class FileDescriptorTest : public testing::Test {
protected:
virtual void SetUp() {
// Build descriptors for the following definitions:
//
// // in "foo.proto"
// message FooMessage { extensions 1; }
// enum FooEnum {FOO_ENUM_VALUE = 1;}
// service FooService {}
// extend FooMessage { optional int32 foo_extension = 1; }
//
// // in "bar.proto"
// package bar_package;
// message BarMessage { extensions 1; }
// enum BarEnum {BAR_ENUM_VALUE = 1;}
// service BarService {}
// extend BarMessage { optional int32 bar_extension = 1; }
//
// Also, we have an empty file "baz.proto". This file's purpose is to
// make sure that even though it has the same package as foo.proto,
// searching it for members of foo.proto won't work.
FileDescriptorProto foo_file;
foo_file.set_name("foo.proto");
AddExtensionRange(AddMessage(&foo_file, "FooMessage"), 1, 2);
AddEnumValue(AddEnum(&foo_file, "FooEnum"), "FOO_ENUM_VALUE", 1);
AddService(&foo_file, "FooService");
AddExtension(&foo_file, "FooMessage", "foo_extension", 1,
FieldDescriptorProto::LABEL_OPTIONAL,
FieldDescriptorProto::TYPE_INT32);
FileDescriptorProto bar_file;
bar_file.set_name("bar.proto");
bar_file.set_package("bar_package");
bar_file.add_dependency("foo.proto");
AddExtensionRange(AddMessage(&bar_file, "BarMessage"), 1, 2);
AddEnumValue(AddEnum(&bar_file, "BarEnum"), "BAR_ENUM_VALUE", 1);
AddService(&bar_file, "BarService");
AddExtension(&bar_file, "bar_package.BarMessage", "bar_extension", 1,
FieldDescriptorProto::LABEL_OPTIONAL,
FieldDescriptorProto::TYPE_INT32);
FileDescriptorProto baz_file;
baz_file.set_name("baz.proto");
// Build the descriptors and get the pointers.
foo_file_ = pool_.BuildFile(foo_file);
ASSERT_TRUE(foo_file_ != NULL);
bar_file_ = pool_.BuildFile(bar_file);
ASSERT_TRUE(bar_file_ != NULL);
baz_file_ = pool_.BuildFile(baz_file);
ASSERT_TRUE(baz_file_ != NULL);
ASSERT_EQ(1, foo_file_->message_type_count());
foo_message_ = foo_file_->message_type(0);
ASSERT_EQ(1, foo_file_->enum_type_count());
foo_enum_ = foo_file_->enum_type(0);
ASSERT_EQ(1, foo_enum_->value_count());
foo_enum_value_ = foo_enum_->value(0);
ASSERT_EQ(1, foo_file_->service_count());
foo_service_ = foo_file_->service(0);
ASSERT_EQ(1, foo_file_->extension_count());
foo_extension_ = foo_file_->extension(0);
ASSERT_EQ(1, bar_file_->message_type_count());
bar_message_ = bar_file_->message_type(0);
ASSERT_EQ(1, bar_file_->enum_type_count());
bar_enum_ = bar_file_->enum_type(0);
ASSERT_EQ(1, bar_enum_->value_count());
bar_enum_value_ = bar_enum_->value(0);
ASSERT_EQ(1, bar_file_->service_count());
bar_service_ = bar_file_->service(0);
ASSERT_EQ(1, bar_file_->extension_count());
bar_extension_ = bar_file_->extension(0);
}
DescriptorPool pool_;
const FileDescriptor* foo_file_;
const FileDescriptor* bar_file_;
const FileDescriptor* baz_file_;
const Descriptor* foo_message_;
const EnumDescriptor* foo_enum_;
const EnumValueDescriptor* foo_enum_value_;
const ServiceDescriptor* foo_service_;
const FieldDescriptor* foo_extension_;
const Descriptor* bar_message_;
const EnumDescriptor* bar_enum_;
const EnumValueDescriptor* bar_enum_value_;
const ServiceDescriptor* bar_service_;
const FieldDescriptor* bar_extension_;
};
TEST_F(FileDescriptorTest, Name) {
EXPECT_EQ("foo.proto", foo_file_->name());
EXPECT_EQ("bar.proto", bar_file_->name());
EXPECT_EQ("baz.proto", baz_file_->name());
}
TEST_F(FileDescriptorTest, Package) {
EXPECT_EQ("", foo_file_->package());
EXPECT_EQ("bar_package", bar_file_->package());
}
TEST_F(FileDescriptorTest, Dependencies) {
EXPECT_EQ(0, foo_file_->dependency_count());
EXPECT_EQ(1, bar_file_->dependency_count());
EXPECT_EQ(foo_file_, bar_file_->dependency(0));
}
TEST_F(FileDescriptorTest, FindMessageTypeByName) {
EXPECT_EQ(foo_message_, foo_file_->FindMessageTypeByName("FooMessage"));
EXPECT_EQ(bar_message_, bar_file_->FindMessageTypeByName("BarMessage"));
EXPECT_TRUE(foo_file_->FindMessageTypeByName("BarMessage") == NULL);
EXPECT_TRUE(bar_file_->FindMessageTypeByName("FooMessage") == NULL);
EXPECT_TRUE(baz_file_->FindMessageTypeByName("FooMessage") == NULL);
EXPECT_TRUE(foo_file_->FindMessageTypeByName("NoSuchMessage") == NULL);
EXPECT_TRUE(foo_file_->FindMessageTypeByName("FooEnum") == NULL);
}
TEST_F(FileDescriptorTest, FindEnumTypeByName) {
EXPECT_EQ(foo_enum_, foo_file_->FindEnumTypeByName("FooEnum"));
EXPECT_EQ(bar_enum_, bar_file_->FindEnumTypeByName("BarEnum"));
EXPECT_TRUE(foo_file_->FindEnumTypeByName("BarEnum") == NULL);
EXPECT_TRUE(bar_file_->FindEnumTypeByName("FooEnum") == NULL);
EXPECT_TRUE(baz_file_->FindEnumTypeByName("FooEnum") == NULL);
EXPECT_TRUE(foo_file_->FindEnumTypeByName("NoSuchEnum") == NULL);
EXPECT_TRUE(foo_file_->FindEnumTypeByName("FooMessage") == NULL);
}
TEST_F(FileDescriptorTest, FindEnumValueByName) {
EXPECT_EQ(foo_enum_value_, foo_file_->FindEnumValueByName("FOO_ENUM_VALUE"));
EXPECT_EQ(bar_enum_value_, bar_file_->FindEnumValueByName("BAR_ENUM_VALUE"));
EXPECT_TRUE(foo_file_->FindEnumValueByName("BAR_ENUM_VALUE") == NULL);
EXPECT_TRUE(bar_file_->FindEnumValueByName("FOO_ENUM_VALUE") == NULL);
EXPECT_TRUE(baz_file_->FindEnumValueByName("FOO_ENUM_VALUE") == NULL);
EXPECT_TRUE(foo_file_->FindEnumValueByName("NO_SUCH_VALUE") == NULL);
EXPECT_TRUE(foo_file_->FindEnumValueByName("FooMessage") == NULL);
}
TEST_F(FileDescriptorTest, FindServiceByName) {
EXPECT_EQ(foo_service_, foo_file_->FindServiceByName("FooService"));
EXPECT_EQ(bar_service_, bar_file_->FindServiceByName("BarService"));
EXPECT_TRUE(foo_file_->FindServiceByName("BarService") == NULL);
EXPECT_TRUE(bar_file_->FindServiceByName("FooService") == NULL);
EXPECT_TRUE(baz_file_->FindServiceByName("FooService") == NULL);
EXPECT_TRUE(foo_file_->FindServiceByName("NoSuchService") == NULL);
EXPECT_TRUE(foo_file_->FindServiceByName("FooMessage") == NULL);
}
TEST_F(FileDescriptorTest, FindExtensionByName) {
EXPECT_EQ(foo_extension_, foo_file_->FindExtensionByName("foo_extension"));
EXPECT_EQ(bar_extension_, bar_file_->FindExtensionByName("bar_extension"));
EXPECT_TRUE(foo_file_->FindExtensionByName("bar_extension") == NULL);
EXPECT_TRUE(bar_file_->FindExtensionByName("foo_extension") == NULL);
EXPECT_TRUE(baz_file_->FindExtensionByName("foo_extension") == NULL);
EXPECT_TRUE(foo_file_->FindExtensionByName("no_such_extension") == NULL);
EXPECT_TRUE(foo_file_->FindExtensionByName("FooMessage") == NULL);
}
TEST_F(FileDescriptorTest, FindExtensionByNumber) {
EXPECT_EQ(foo_extension_, pool_.FindExtensionByNumber(foo_message_, 1));
EXPECT_EQ(bar_extension_, pool_.FindExtensionByNumber(bar_message_, 1));
EXPECT_TRUE(pool_.FindExtensionByNumber(foo_message_, 2) == NULL);
}
TEST_F(FileDescriptorTest, BuildAgain) {
// Test that if te call BuildFile again on the same input we get the same
// FileDescriptor back.
FileDescriptorProto file;
foo_file_->CopyTo(&file);
EXPECT_EQ(foo_file_, pool_.BuildFile(file));
// But if we change the file then it won't work.
file.set_package("some.other.package");
EXPECT_TRUE(pool_.BuildFile(file) == NULL);
}
TEST_F(FileDescriptorTest, BuildAgainWithSyntax) {
// Test that if te call BuildFile again on the same input we get the same
// FileDescriptor back even if syntax param is specified.
FileDescriptorProto proto_syntax2;
proto_syntax2.set_name("foo_syntax2");
proto_syntax2.set_syntax("proto2");
const FileDescriptor* proto2_descriptor = pool_.BuildFile(proto_syntax2);
EXPECT_TRUE(proto2_descriptor != NULL);
EXPECT_EQ(proto2_descriptor, pool_.BuildFile(proto_syntax2));
FileDescriptorProto implicit_proto2;
implicit_proto2.set_name("foo_implicit_syntax2");
const FileDescriptor* implicit_proto2_descriptor =
pool_.BuildFile(implicit_proto2);
EXPECT_TRUE(implicit_proto2_descriptor != NULL);
// We get the same FileDescriptor back if syntax param is explicitly
// specified.
implicit_proto2.set_syntax("proto2");
EXPECT_EQ(implicit_proto2_descriptor, pool_.BuildFile(implicit_proto2));
FileDescriptorProto proto_syntax3;
proto_syntax3.set_name("foo_syntax3");
proto_syntax3.set_syntax("proto3");
const FileDescriptor* proto3_descriptor = pool_.BuildFile(proto_syntax3);
EXPECT_TRUE(proto3_descriptor != NULL);
EXPECT_EQ(proto3_descriptor, pool_.BuildFile(proto_syntax3));
}
TEST_F(FileDescriptorTest, Syntax) {
FileDescriptorProto proto;
proto.set_name("foo");
// Enable the test when we also populate the syntax for proto2.
#if 0
{
proto.set_syntax("proto2");
DescriptorPool pool;
const FileDescriptor* file = pool.BuildFile(proto);
EXPECT_TRUE(file != NULL);
EXPECT_EQ(FileDescriptor::SYNTAX_PROTO2, file->syntax());
FileDescriptorProto other;
file->CopyTo(&other);
EXPECT_EQ("proto2", other.syntax());
}
#endif
{
proto.set_syntax("proto3");
DescriptorPool pool;
const FileDescriptor* file = pool.BuildFile(proto);
EXPECT_TRUE(file != NULL);
EXPECT_EQ(FileDescriptor::SYNTAX_PROTO3, file->syntax());
FileDescriptorProto other;
file->CopyTo(&other);
EXPECT_EQ("proto3", other.syntax());
}
}
// ===================================================================
// Test simple flat messages and fields.
class DescriptorTest : public testing::Test {
protected:
virtual void SetUp() {
// Build descriptors for the following definitions:
//
// // in "foo.proto"
// message TestForeign {}
// enum TestEnum {}
//
// message TestMessage {
// required string foo = 1;
// optional TestEnum bar = 6;
// repeated TestForeign baz = 500000000;
// optional group qux = 15 {}
// }
//
// // in "bar.proto"
// package corge.grault;
// message TestMessage2 {
// required string foo = 1;
// required string bar = 2;
// required string quux = 6;
// }
//
// // in "map.proto"
// message TestMessage3 {
// map<int32, int32> map_int32_int32 = 1;
// }
//
// // in "json.proto"
// message TestMessage4 {
// optional int32 field_name1 = 1;
// optional int32 fieldName2 = 2;
// optional int32 FieldName3 = 3;
// optional int32 _field_name4 = 4;
// optional int32 FIELD_NAME5 = 5;
// optional int32 field_name6 = 6 [json_name = "@type"];
// }
//
// We cheat and use TestForeign as the type for qux rather than create
// an actual nested type.
//
// Since all primitive types (including string) use the same building
// code, there's no need to test each one individually.
//
// TestMessage2 is primarily here to test FindFieldByName and friends.
// All messages created from the same DescriptorPool share the same lookup
// table, so we need to insure that they don't interfere.
FileDescriptorProto foo_file;
foo_file.set_name("foo.proto");
AddMessage(&foo_file, "TestForeign");
AddEmptyEnum(&foo_file, "TestEnum");
DescriptorProto* message = AddMessage(&foo_file, "TestMessage");
AddField(message, "foo", 1,
FieldDescriptorProto::LABEL_REQUIRED,
FieldDescriptorProto::TYPE_STRING);
AddField(message, "bar", 6,
FieldDescriptorProto::LABEL_OPTIONAL,
FieldDescriptorProto::TYPE_ENUM)
->set_type_name("TestEnum");
AddField(message, "baz", 500000000,
FieldDescriptorProto::LABEL_REPEATED,
FieldDescriptorProto::TYPE_MESSAGE)
->set_type_name("TestForeign");
AddField(message, "qux", 15,
FieldDescriptorProto::LABEL_OPTIONAL,
FieldDescriptorProto::TYPE_GROUP)
->set_type_name("TestForeign");
FileDescriptorProto bar_file;
bar_file.set_name("bar.proto");
bar_file.set_package("corge.grault");
DescriptorProto* message2 = AddMessage(&bar_file, "TestMessage2");
AddField(message2, "foo", 1,
FieldDescriptorProto::LABEL_REQUIRED,
FieldDescriptorProto::TYPE_STRING);
AddField(message2, "bar", 2,
FieldDescriptorProto::LABEL_REQUIRED,
FieldDescriptorProto::TYPE_STRING);
AddField(message2, "quux", 6,
FieldDescriptorProto::LABEL_REQUIRED,
FieldDescriptorProto::TYPE_STRING);
FileDescriptorProto map_file;
map_file.set_name("map.proto");
DescriptorProto* message3 = AddMessage(&map_file, "TestMessage3");
DescriptorProto* entry = AddNestedMessage(message3, "MapInt32Int32Entry");
AddField(entry, "key", 1,
FieldDescriptorProto::LABEL_OPTIONAL,
FieldDescriptorProto::TYPE_INT32);
AddField(entry, "value", 2,
FieldDescriptorProto::LABEL_OPTIONAL,
FieldDescriptorProto::TYPE_INT32);
entry->mutable_options()->set_map_entry(true);
AddField(message3, "map_int32_int32", 1,
FieldDescriptorProto::LABEL_REPEATED,
FieldDescriptorProto::TYPE_MESSAGE)
->set_type_name("MapInt32Int32Entry");
FileDescriptorProto json_file;
json_file.set_name("json.proto");
json_file.set_syntax("proto3");
DescriptorProto* message4 = AddMessage(&json_file, "TestMessage4");
AddField(message4, "field_name1", 1,
FieldDescriptorProto::LABEL_OPTIONAL,
FieldDescriptorProto::TYPE_INT32);
AddField(message4, "fieldName2", 2,
FieldDescriptorProto::LABEL_OPTIONAL,
FieldDescriptorProto::TYPE_INT32);
AddField(message4, "FieldName3", 3,
FieldDescriptorProto::LABEL_OPTIONAL,
FieldDescriptorProto::TYPE_INT32);
AddField(message4, "_field_name4", 4,
FieldDescriptorProto::LABEL_OPTIONAL,
FieldDescriptorProto::TYPE_INT32);
AddField(message4, "FIELD_NAME5", 5,
FieldDescriptorProto::LABEL_OPTIONAL,
FieldDescriptorProto::TYPE_INT32);
AddField(message4, "field_name6", 6,
FieldDescriptorProto::LABEL_OPTIONAL,
FieldDescriptorProto::TYPE_INT32)
->set_json_name("@type");
// Build the descriptors and get the pointers.
foo_file_ = pool_.BuildFile(foo_file);
ASSERT_TRUE(foo_file_ != NULL);
bar_file_ = pool_.BuildFile(bar_file);
ASSERT_TRUE(bar_file_ != NULL);
map_file_ = pool_.BuildFile(map_file);
ASSERT_TRUE(map_file_ != NULL);
json_file_ = pool_.BuildFile(json_file);
ASSERT_TRUE(json_file_ != NULL);
ASSERT_EQ(1, foo_file_->enum_type_count());
enum_ = foo_file_->enum_type(0);
ASSERT_EQ(2, foo_file_->message_type_count());
foreign_ = foo_file_->message_type(0);
message_ = foo_file_->message_type(1);
ASSERT_EQ(4, message_->field_count());
foo_ = message_->field(0);
bar_ = message_->field(1);
baz_ = message_->field(2);
qux_ = message_->field(3);
ASSERT_EQ(1, bar_file_->message_type_count());
message2_ = bar_file_->message_type(0);
ASSERT_EQ(3, message2_->field_count());
foo2_ = message2_->field(0);
bar2_ = message2_->field(1);
quux2_ = message2_->field(2);
ASSERT_EQ(1, map_file_->message_type_count());
message3_ = map_file_->message_type(0);
ASSERT_EQ(1, message3_->field_count());
map_ = message3_->field(0);
ASSERT_EQ(1, json_file_->message_type_count());
message4_ = json_file_->message_type(0);
}
void CopyWithJsonName(const Descriptor* message, DescriptorProto* proto) {
message->CopyTo(proto);
message->CopyJsonNameTo(proto);
}
DescriptorPool pool_;
const FileDescriptor* foo_file_;
const FileDescriptor* bar_file_;
const FileDescriptor* map_file_;
const FileDescriptor* json_file_;
const Descriptor* message_;
const Descriptor* message2_;
const Descriptor* message3_;
const Descriptor* message4_;
const Descriptor* foreign_;
const EnumDescriptor* enum_;
const FieldDescriptor* foo_;
const FieldDescriptor* bar_;
const FieldDescriptor* baz_;
const FieldDescriptor* qux_;
const FieldDescriptor* foo2_;
const FieldDescriptor* bar2_;
const FieldDescriptor* quux2_;
const FieldDescriptor* map_;
};
TEST_F(DescriptorTest, Name) {
EXPECT_EQ("TestMessage", message_->name());
EXPECT_EQ("TestMessage", message_->full_name());
EXPECT_EQ(foo_file_, message_->file());
EXPECT_EQ("TestMessage2", message2_->name());
EXPECT_EQ("corge.grault.TestMessage2", message2_->full_name());
EXPECT_EQ(bar_file_, message2_->file());
}
TEST_F(DescriptorTest, ContainingType) {
EXPECT_TRUE(message_->containing_type() == NULL);
EXPECT_TRUE(message2_->containing_type() == NULL);
}
TEST_F(DescriptorTest, FieldsByIndex) {
ASSERT_EQ(4, message_->field_count());
EXPECT_EQ(foo_, message_->field(0));
EXPECT_EQ(bar_, message_->field(1));
EXPECT_EQ(baz_, message_->field(2));
EXPECT_EQ(qux_, message_->field(3));
}
TEST_F(DescriptorTest, FindFieldByName) {
// All messages in the same DescriptorPool share a single lookup table for
// fields. So, in addition to testing that FindFieldByName finds the fields
// of the message, we need to test that it does *not* find the fields of
// *other* messages.
EXPECT_EQ(foo_, message_->FindFieldByName("foo"));
EXPECT_EQ(bar_, message_->FindFieldByName("bar"));
EXPECT_EQ(baz_, message_->FindFieldByName("baz"));
EXPECT_EQ(qux_, message_->FindFieldByName("qux"));
EXPECT_TRUE(message_->FindFieldByName("no_such_field") == NULL);
EXPECT_TRUE(message_->FindFieldByName("quux") == NULL);
EXPECT_EQ(foo2_ , message2_->FindFieldByName("foo" ));
EXPECT_EQ(bar2_ , message2_->FindFieldByName("bar" ));
EXPECT_EQ(quux2_, message2_->FindFieldByName("quux"));
EXPECT_TRUE(message2_->FindFieldByName("baz") == NULL);
EXPECT_TRUE(message2_->FindFieldByName("qux") == NULL);
}
TEST_F(DescriptorTest, FindFieldByNumber) {
EXPECT_EQ(foo_, message_->FindFieldByNumber(1));
EXPECT_EQ(bar_, message_->FindFieldByNumber(6));
EXPECT_EQ(baz_, message_->FindFieldByNumber(500000000));
EXPECT_EQ(qux_, message_->FindFieldByNumber(15));
EXPECT_TRUE(message_->FindFieldByNumber(837592) == NULL);
EXPECT_TRUE(message_->FindFieldByNumber(2) == NULL);
EXPECT_EQ(foo2_ , message2_->FindFieldByNumber(1));
EXPECT_EQ(bar2_ , message2_->FindFieldByNumber(2));
EXPECT_EQ(quux2_, message2_->FindFieldByNumber(6));
EXPECT_TRUE(message2_->FindFieldByNumber(15) == NULL);
EXPECT_TRUE(message2_->FindFieldByNumber(500000000) == NULL);
}
TEST_F(DescriptorTest, FieldName) {
EXPECT_EQ("foo", foo_->name());
EXPECT_EQ("bar", bar_->name());
EXPECT_EQ("baz", baz_->name());
EXPECT_EQ("qux", qux_->name());
}
TEST_F(DescriptorTest, FieldFullName) {
EXPECT_EQ("TestMessage.foo", foo_->full_name());
EXPECT_EQ("TestMessage.bar", bar_->full_name());
EXPECT_EQ("TestMessage.baz", baz_->full_name());
EXPECT_EQ("TestMessage.qux", qux_->full_name());
EXPECT_EQ("corge.grault.TestMessage2.foo", foo2_->full_name());
EXPECT_EQ("corge.grault.TestMessage2.bar", bar2_->full_name());
EXPECT_EQ("corge.grault.TestMessage2.quux", quux2_->full_name());
}
TEST_F(DescriptorTest, FieldJsonName) {
EXPECT_EQ("fieldName1", message4_->field(0)->json_name());
EXPECT_EQ("fieldName2", message4_->field(1)->json_name());
EXPECT_EQ("fieldName3", message4_->field(2)->json_name());
EXPECT_EQ("fieldName4", message4_->field(3)->json_name());
EXPECT_EQ("fIELDNAME5", message4_->field(4)->json_name());
EXPECT_EQ("@type", message4_->field(5)->json_name());
DescriptorProto proto;
message4_->CopyTo(&proto);
ASSERT_EQ(6, proto.field_size());
EXPECT_FALSE(proto.field(0).has_json_name());
EXPECT_FALSE(proto.field(1).has_json_name());
EXPECT_FALSE(proto.field(2).has_json_name());
EXPECT_FALSE(proto.field(3).has_json_name());
EXPECT_FALSE(proto.field(4).has_json_name());
EXPECT_EQ("@type", proto.field(5).json_name());
proto.Clear();
CopyWithJsonName(message4_, &proto);
ASSERT_EQ(6, proto.field_size());
EXPECT_EQ("fieldName1", proto.field(0).json_name());
EXPECT_EQ("fieldName2", proto.field(1).json_name());
EXPECT_EQ("fieldName3", proto.field(2).json_name());
EXPECT_EQ("fieldName4", proto.field(3).json_name());
EXPECT_EQ("fIELDNAME5", proto.field(4).json_name());
EXPECT_EQ("@type", proto.field(5).json_name());
}
TEST_F(DescriptorTest, FieldFile) {
EXPECT_EQ(foo_file_, foo_->file());
EXPECT_EQ(foo_file_, bar_->file());
EXPECT_EQ(foo_file_, baz_->file());
EXPECT_EQ(foo_file_, qux_->file());
EXPECT_EQ(bar_file_, foo2_->file());
EXPECT_EQ(bar_file_, bar2_->file());
EXPECT_EQ(bar_file_, quux2_->file());
}
TEST_F(DescriptorTest, FieldIndex) {
EXPECT_EQ(0, foo_->index());
EXPECT_EQ(1, bar_->index());
EXPECT_EQ(2, baz_->index());
EXPECT_EQ(3, qux_->index());
}
TEST_F(DescriptorTest, FieldNumber) {
EXPECT_EQ( 1, foo_->number());
EXPECT_EQ( 6, bar_->number());
EXPECT_EQ(500000000, baz_->number());
EXPECT_EQ( 15, qux_->number());
}
TEST_F(DescriptorTest, FieldType) {
EXPECT_EQ(FieldDescriptor::TYPE_STRING , foo_->type());
EXPECT_EQ(FieldDescriptor::TYPE_ENUM , bar_->type());
EXPECT_EQ(FieldDescriptor::TYPE_MESSAGE, baz_->type());
EXPECT_EQ(FieldDescriptor::TYPE_GROUP , qux_->type());
}
TEST_F(DescriptorTest, FieldLabel) {
EXPECT_EQ(FieldDescriptor::LABEL_REQUIRED, foo_->label());
EXPECT_EQ(FieldDescriptor::LABEL_OPTIONAL, bar_->label());
EXPECT_EQ(FieldDescriptor::LABEL_REPEATED, baz_->label());
EXPECT_EQ(FieldDescriptor::LABEL_OPTIONAL, qux_->label());
EXPECT_TRUE (foo_->is_required());
EXPECT_FALSE(foo_->is_optional());
EXPECT_FALSE(foo_->is_repeated());
EXPECT_FALSE(bar_->is_required());
EXPECT_TRUE (bar_->is_optional());
EXPECT_FALSE(bar_->is_repeated());
EXPECT_FALSE(baz_->is_required());
EXPECT_FALSE(baz_->is_optional());
EXPECT_TRUE (baz_->is_repeated());
}
TEST_F(DescriptorTest, IsMap) {
EXPECT_TRUE(map_->is_map());
EXPECT_FALSE(baz_->is_map());
EXPECT_TRUE(map_->message_type()->options().map_entry());
}
TEST_F(DescriptorTest, FieldHasDefault) {
EXPECT_FALSE(foo_->has_default_value());
EXPECT_FALSE(bar_->has_default_value());
EXPECT_FALSE(baz_->has_default_value());
EXPECT_FALSE(qux_->has_default_value());
}
TEST_F(DescriptorTest, FieldContainingType) {
EXPECT_EQ(message_, foo_->containing_type());
EXPECT_EQ(message_, bar_->containing_type());
EXPECT_EQ(message_, baz_->containing_type());
EXPECT_EQ(message_, qux_->containing_type());
EXPECT_EQ(message2_, foo2_ ->containing_type());
EXPECT_EQ(message2_, bar2_ ->containing_type());
EXPECT_EQ(message2_, quux2_->containing_type());
}
TEST_F(DescriptorTest, FieldMessageType) {
EXPECT_TRUE(foo_->message_type() == NULL);
EXPECT_TRUE(bar_->message_type() == NULL);
EXPECT_EQ(foreign_, baz_->message_type());
EXPECT_EQ(foreign_, qux_->message_type());
}
TEST_F(DescriptorTest, FieldEnumType) {
EXPECT_TRUE(foo_->enum_type() == NULL);
EXPECT_TRUE(baz_->enum_type() == NULL);
EXPECT_TRUE(qux_->enum_type() == NULL);
EXPECT_EQ(enum_, bar_->enum_type());
}
// ===================================================================
// Test simple flat messages and fields.
class OneofDescriptorTest : public testing::Test {
protected:
virtual void SetUp() {
// Build descriptors for the following definitions:
//
// package garply;
// message TestOneof {
// optional int32 a = 1;
// oneof foo {
// string b = 2;
// TestOneof c = 3;
// }
// oneof bar {
// float d = 4;
// }
// }
FileDescriptorProto baz_file;
baz_file.set_name("baz.proto");
baz_file.set_package("garply");
DescriptorProto* oneof_message = AddMessage(&baz_file, "TestOneof");
oneof_message->add_oneof_decl()->set_name("foo");
oneof_message->add_oneof_decl()->set_name("bar");
AddField(oneof_message, "a", 1,
FieldDescriptorProto::LABEL_OPTIONAL,
FieldDescriptorProto::TYPE_INT32);
AddField(oneof_message, "b", 2,
FieldDescriptorProto::LABEL_OPTIONAL,
FieldDescriptorProto::TYPE_STRING);
oneof_message->mutable_field(1)->set_oneof_index(0);
AddField(oneof_message, "c", 3,
FieldDescriptorProto::LABEL_OPTIONAL,
FieldDescriptorProto::TYPE_MESSAGE);
oneof_message->mutable_field(2)->set_oneof_index(0);
oneof_message->mutable_field(2)->set_type_name("TestOneof");
AddField(oneof_message, "d", 4,
FieldDescriptorProto::LABEL_OPTIONAL,
FieldDescriptorProto::TYPE_FLOAT);
oneof_message->mutable_field(3)->set_oneof_index(1);
// Build the descriptors and get the pointers.
baz_file_ = pool_.BuildFile(baz_file);
ASSERT_TRUE(baz_file_ != NULL);
ASSERT_EQ(1, baz_file_->message_type_count());
oneof_message_ = baz_file_->message_type(0);
ASSERT_EQ(2, oneof_message_->oneof_decl_count());
oneof_ = oneof_message_->oneof_decl(0);
oneof2_ = oneof_message_->oneof_decl(1);
ASSERT_EQ(4, oneof_message_->field_count());
a_ = oneof_message_->field(0);
b_ = oneof_message_->field(1);
c_ = oneof_message_->field(2);
d_ = oneof_message_->field(3);
}
DescriptorPool pool_;
const FileDescriptor* baz_file_;
const Descriptor* oneof_message_;
const OneofDescriptor* oneof_;
const OneofDescriptor* oneof2_;
const FieldDescriptor* a_;
const FieldDescriptor* b_;
const FieldDescriptor* c_;
const FieldDescriptor* d_;
const FieldDescriptor* e_;
const FieldDescriptor* f_;
};
TEST_F(OneofDescriptorTest, Normal) {
EXPECT_EQ("foo", oneof_->name());
EXPECT_EQ("garply.TestOneof.foo", oneof_->full_name());
EXPECT_EQ(0, oneof_->index());
ASSERT_EQ(2, oneof_->field_count());
EXPECT_EQ(b_, oneof_->field(0));
EXPECT_EQ(c_, oneof_->field(1));
EXPECT_TRUE(a_->containing_oneof() == NULL);
EXPECT_EQ(oneof_, b_->containing_oneof());
EXPECT_EQ(oneof_, c_->containing_oneof());
}
TEST_F(OneofDescriptorTest, FindByName) {
EXPECT_EQ(oneof_, oneof_message_->FindOneofByName("foo"));
EXPECT_EQ(oneof2_, oneof_message_->FindOneofByName("bar"));
EXPECT_TRUE(oneof_message_->FindOneofByName("no_such_oneof") == NULL);
}
// ===================================================================
class StylizedFieldNamesTest : public testing::Test {
protected:
void SetUp() {
FileDescriptorProto file;
file.set_name("foo.proto");
AddExtensionRange(AddMessage(&file, "ExtendableMessage"), 1, 1000);
DescriptorProto* message = AddMessage(&file, "TestMessage");
AddField(message, "foo_foo", 1,
FieldDescriptorProto::LABEL_OPTIONAL,
FieldDescriptorProto::TYPE_INT32);
AddField(message, "FooBar", 2,
FieldDescriptorProto::LABEL_OPTIONAL,
FieldDescriptorProto::TYPE_INT32);
AddField(message, "fooBaz", 3,
FieldDescriptorProto::LABEL_OPTIONAL,
FieldDescriptorProto::TYPE_INT32);
AddField(message, "fooFoo", 4, // Camel-case conflict with foo_foo.
FieldDescriptorProto::LABEL_OPTIONAL,
FieldDescriptorProto::TYPE_INT32);
AddField(message, "foobar", 5, // Lower-case conflict with FooBar.
FieldDescriptorProto::LABEL_OPTIONAL,
FieldDescriptorProto::TYPE_INT32);
AddNestedExtension(message, "ExtendableMessage", "bar_foo", 1,
FieldDescriptorProto::LABEL_OPTIONAL,
FieldDescriptorProto::TYPE_INT32);
AddNestedExtension(message, "ExtendableMessage", "BarBar", 2,
FieldDescriptorProto::LABEL_OPTIONAL,
FieldDescriptorProto::TYPE_INT32);
AddNestedExtension(message, "ExtendableMessage", "BarBaz", 3,
FieldDescriptorProto::LABEL_OPTIONAL,
FieldDescriptorProto::TYPE_INT32);
AddNestedExtension(message, "ExtendableMessage", "barFoo", 4, // Conflict
FieldDescriptorProto::LABEL_OPTIONAL,
FieldDescriptorProto::TYPE_INT32);
AddNestedExtension(message, "ExtendableMessage", "barbar", 5, // Conflict
FieldDescriptorProto::LABEL_OPTIONAL,
FieldDescriptorProto::TYPE_INT32);
AddExtension(&file, "ExtendableMessage", "baz_foo", 11,
FieldDescriptorProto::LABEL_OPTIONAL,
FieldDescriptorProto::TYPE_INT32);
AddExtension(&file, "ExtendableMessage", "BazBar", 12,
FieldDescriptorProto::LABEL_OPTIONAL,
FieldDescriptorProto::TYPE_INT32);
AddExtension(&file, "ExtendableMessage", "BazBaz", 13,
FieldDescriptorProto::LABEL_OPTIONAL,
FieldDescriptorProto::TYPE_INT32);
AddExtension(&file, "ExtendableMessage", "bazFoo", 14, // Conflict
FieldDescriptorProto::LABEL_OPTIONAL,
FieldDescriptorProto::TYPE_INT32);
AddExtension(&file, "ExtendableMessage", "bazbar", 15, // Conflict
FieldDescriptorProto::LABEL_OPTIONAL,
FieldDescriptorProto::TYPE_INT32);
file_ = pool_.BuildFile(file);
ASSERT_TRUE(file_ != NULL);
ASSERT_EQ(2, file_->message_type_count());
message_ = file_->message_type(1);
ASSERT_EQ("TestMessage", message_->name());
ASSERT_EQ(5, message_->field_count());
ASSERT_EQ(5, message_->extension_count());
ASSERT_EQ(5, file_->extension_count());
}
DescriptorPool pool_;
const FileDescriptor* file_;
const Descriptor* message_;
};
TEST_F(StylizedFieldNamesTest, LowercaseName) {
EXPECT_EQ("foo_foo", message_->field(0)->lowercase_name());
EXPECT_EQ("foobar" , message_->field(1)->lowercase_name());
EXPECT_EQ("foobaz" , message_->field(2)->lowercase_name());
EXPECT_EQ("foofoo" , message_->field(3)->lowercase_name());
EXPECT_EQ("foobar" , message_->field(4)->lowercase_name());
EXPECT_EQ("bar_foo", message_->extension(0)->lowercase_name());
EXPECT_EQ("barbar" , message_->extension(1)->lowercase_name());
EXPECT_EQ("barbaz" , message_->extension(2)->lowercase_name());
EXPECT_EQ("barfoo" , message_->extension(3)->lowercase_name());
EXPECT_EQ("barbar" , message_->extension(4)->lowercase_name());
EXPECT_EQ("baz_foo", file_->extension(0)->lowercase_name());
EXPECT_EQ("bazbar" , file_->extension(1)->lowercase_name());
EXPECT_EQ("bazbaz" , file_->extension(2)->lowercase_name());
EXPECT_EQ("bazfoo" , file_->extension(3)->lowercase_name());
EXPECT_EQ("bazbar" , file_->extension(4)->lowercase_name());
}
TEST_F(StylizedFieldNamesTest, CamelcaseName) {
EXPECT_EQ("fooFoo", message_->field(0)->camelcase_name());
EXPECT_EQ("fooBar", message_->field(1)->camelcase_name());
EXPECT_EQ("fooBaz", message_->field(2)->camelcase_name());
EXPECT_EQ("fooFoo", message_->field(3)->camelcase_name());
EXPECT_EQ("foobar", message_->field(4)->camelcase_name());
EXPECT_EQ("barFoo", message_->extension(0)->camelcase_name());
EXPECT_EQ("barBar", message_->extension(1)->camelcase_name());
EXPECT_EQ("barBaz", message_->extension(2)->camelcase_name());
EXPECT_EQ("barFoo", message_->extension(3)->camelcase_name());
EXPECT_EQ("barbar", message_->extension(4)->camelcase_name());
EXPECT_EQ("bazFoo", file_->extension(0)->camelcase_name());
EXPECT_EQ("bazBar", file_->extension(1)->camelcase_name());
EXPECT_EQ("bazBaz", file_->extension(2)->camelcase_name());
EXPECT_EQ("bazFoo", file_->extension(3)->camelcase_name());
EXPECT_EQ("bazbar", file_->extension(4)->camelcase_name());
}
TEST_F(StylizedFieldNamesTest, FindByLowercaseName) {
EXPECT_EQ(message_->field(0),
message_->FindFieldByLowercaseName("foo_foo"));
EXPECT_EQ(message_->field(1),
message_->FindFieldByLowercaseName("foobar"));
EXPECT_EQ(message_->field(2),
message_->FindFieldByLowercaseName("foobaz"));
EXPECT_TRUE(message_->FindFieldByLowercaseName("FooBar") == NULL);
EXPECT_TRUE(message_->FindFieldByLowercaseName("fooBaz") == NULL);
EXPECT_TRUE(message_->FindFieldByLowercaseName("bar_foo") == NULL);
EXPECT_TRUE(message_->FindFieldByLowercaseName("nosuchfield") == NULL);
EXPECT_EQ(message_->extension(0),
message_->FindExtensionByLowercaseName("bar_foo"));
EXPECT_EQ(message_->extension(1),
message_->FindExtensionByLowercaseName("barbar"));
EXPECT_EQ(message_->extension(2),
message_->FindExtensionByLowercaseName("barbaz"));
EXPECT_TRUE(message_->FindExtensionByLowercaseName("BarBar") == NULL);
EXPECT_TRUE(message_->FindExtensionByLowercaseName("barBaz") == NULL);
EXPECT_TRUE(message_->FindExtensionByLowercaseName("foo_foo") == NULL);
EXPECT_TRUE(message_->FindExtensionByLowercaseName("nosuchfield") == NULL);
EXPECT_EQ(file_->extension(0),
file_->FindExtensionByLowercaseName("baz_foo"));
EXPECT_EQ(file_->extension(1),
file_->FindExtensionByLowercaseName("bazbar"));
EXPECT_EQ(file_->extension(2),
file_->FindExtensionByLowercaseName("bazbaz"));
EXPECT_TRUE(file_->FindExtensionByLowercaseName("BazBar") == NULL);
EXPECT_TRUE(file_->FindExtensionByLowercaseName("bazBaz") == NULL);
EXPECT_TRUE(file_->FindExtensionByLowercaseName("nosuchfield") == NULL);
}
TEST_F(StylizedFieldNamesTest, FindByCamelcaseName) {
EXPECT_EQ(message_->field(0),
message_->FindFieldByCamelcaseName("fooFoo"));
EXPECT_EQ(message_->field(1),
message_->FindFieldByCamelcaseName("fooBar"));
EXPECT_EQ(message_->field(2),
message_->FindFieldByCamelcaseName("fooBaz"));
EXPECT_TRUE(message_->FindFieldByCamelcaseName("foo_foo") == NULL);
EXPECT_TRUE(message_->FindFieldByCamelcaseName("FooBar") == NULL);
EXPECT_TRUE(message_->FindFieldByCamelcaseName("barFoo") == NULL);
EXPECT_TRUE(message_->FindFieldByCamelcaseName("nosuchfield") == NULL);
EXPECT_EQ(message_->extension(0),
message_->FindExtensionByCamelcaseName("barFoo"));
EXPECT_EQ(message_->extension(1),
message_->FindExtensionByCamelcaseName("barBar"));
EXPECT_EQ(message_->extension(2),
message_->FindExtensionByCamelcaseName("barBaz"));
EXPECT_TRUE(message_->FindExtensionByCamelcaseName("bar_foo") == NULL);
EXPECT_TRUE(message_->FindExtensionByCamelcaseName("BarBar") == NULL);
EXPECT_TRUE(message_->FindExtensionByCamelcaseName("fooFoo") == NULL);
EXPECT_TRUE(message_->FindExtensionByCamelcaseName("nosuchfield") == NULL);
EXPECT_EQ(file_->extension(0),
file_->FindExtensionByCamelcaseName("bazFoo"));
EXPECT_EQ(file_->extension(1),
file_->FindExtensionByCamelcaseName("bazBar"));
EXPECT_EQ(file_->extension(2),
file_->FindExtensionByCamelcaseName("bazBaz"));
EXPECT_TRUE(file_->FindExtensionByCamelcaseName("baz_foo") == NULL);
EXPECT_TRUE(file_->FindExtensionByCamelcaseName("BazBar") == NULL);
EXPECT_TRUE(file_->FindExtensionByCamelcaseName("nosuchfield") == NULL);
}
// ===================================================================
// Test enum descriptors.
class EnumDescriptorTest : public testing::Test {
protected:
virtual void SetUp() {
// Build descriptors for the following definitions:
//
// // in "foo.proto"
// enum TestEnum {
// FOO = 1;
// BAR = 2;
// }
//
// // in "bar.proto"
// package corge.grault;
// enum TestEnum2 {
// FOO = 1;
// BAZ = 3;
// }
//
// TestEnum2 is primarily here to test FindValueByName and friends.
// All enums created from the same DescriptorPool share the same lookup
// table, so we need to insure that they don't interfere.
// TestEnum
FileDescriptorProto foo_file;
foo_file.set_name("foo.proto");
EnumDescriptorProto* enum_proto = AddEnum(&foo_file, "TestEnum");
AddEnumValue(enum_proto, "FOO", 1);
AddEnumValue(enum_proto, "BAR", 2);
// TestEnum2
FileDescriptorProto bar_file;
bar_file.set_name("bar.proto");
bar_file.set_package("corge.grault");
EnumDescriptorProto* enum2_proto = AddEnum(&bar_file, "TestEnum2");
AddEnumValue(enum2_proto, "FOO", 1);
AddEnumValue(enum2_proto, "BAZ", 3);
// Build the descriptors and get the pointers.
foo_file_ = pool_.BuildFile(foo_file);
ASSERT_TRUE(foo_file_ != NULL);
bar_file_ = pool_.BuildFile(bar_file);
ASSERT_TRUE(bar_file_ != NULL);
ASSERT_EQ(1, foo_file_->enum_type_count());
enum_ = foo_file_->enum_type(0);
ASSERT_EQ(2, enum_->value_count());
foo_ = enum_->value(0);
bar_ = enum_->value(1);
ASSERT_EQ(1, bar_file_->enum_type_count());
enum2_ = bar_file_->enum_type(0);
ASSERT_EQ(2, enum2_->value_count());
foo2_ = enum2_->value(0);
baz2_ = enum2_->value(1);
}
DescriptorPool pool_;
const FileDescriptor* foo_file_;
const FileDescriptor* bar_file_;
const EnumDescriptor* enum_;
const EnumDescriptor* enum2_;
const EnumValueDescriptor* foo_;
const EnumValueDescriptor* bar_;
const EnumValueDescriptor* foo2_;
const EnumValueDescriptor* baz2_;
};
TEST_F(EnumDescriptorTest, Name) {
EXPECT_EQ("TestEnum", enum_->name());
EXPECT_EQ("TestEnum", enum_->full_name());
EXPECT_EQ(foo_file_, enum_->file());
EXPECT_EQ("TestEnum2", enum2_->name());
EXPECT_EQ("corge.grault.TestEnum2", enum2_->full_name());
EXPECT_EQ(bar_file_, enum2_->file());
}
TEST_F(EnumDescriptorTest, ContainingType) {
EXPECT_TRUE(enum_->containing_type() == NULL);
EXPECT_TRUE(enum2_->containing_type() == NULL);
}
TEST_F(EnumDescriptorTest, ValuesByIndex) {
ASSERT_EQ(2, enum_->value_count());
EXPECT_EQ(foo_, enum_->value(0));
EXPECT_EQ(bar_, enum_->value(1));
}
TEST_F(EnumDescriptorTest, FindValueByName) {
EXPECT_EQ(foo_ , enum_ ->FindValueByName("FOO"));
EXPECT_EQ(bar_ , enum_ ->FindValueByName("BAR"));
EXPECT_EQ(foo2_, enum2_->FindValueByName("FOO"));
EXPECT_EQ(baz2_, enum2_->FindValueByName("BAZ"));
EXPECT_TRUE(enum_ ->FindValueByName("NO_SUCH_VALUE") == NULL);
EXPECT_TRUE(enum_ ->FindValueByName("BAZ" ) == NULL);
EXPECT_TRUE(enum2_->FindValueByName("BAR" ) == NULL);
}
TEST_F(EnumDescriptorTest, FindValueByNumber) {
EXPECT_EQ(foo_ , enum_ ->FindValueByNumber(1));
EXPECT_EQ(bar_ , enum_ ->FindValueByNumber(2));
EXPECT_EQ(foo2_, enum2_->FindValueByNumber(1));
EXPECT_EQ(baz2_, enum2_->FindValueByNumber(3));
EXPECT_TRUE(enum_ ->FindValueByNumber(416) == NULL);
EXPECT_TRUE(enum_ ->FindValueByNumber(3) == NULL);
EXPECT_TRUE(enum2_->FindValueByNumber(2) == NULL);
}
TEST_F(EnumDescriptorTest, ValueName) {
EXPECT_EQ("FOO", foo_->name());
EXPECT_EQ("BAR", bar_->name());
}
TEST_F(EnumDescriptorTest, ValueFullName) {
EXPECT_EQ("FOO", foo_->full_name());
EXPECT_EQ("BAR", bar_->full_name());
EXPECT_EQ("corge.grault.FOO", foo2_->full_name());
EXPECT_EQ("corge.grault.BAZ", baz2_->full_name());
}
TEST_F(EnumDescriptorTest, ValueIndex) {
EXPECT_EQ(0, foo_->index());
EXPECT_EQ(1, bar_->index());
}
TEST_F(EnumDescriptorTest, ValueNumber) {
EXPECT_EQ(1, foo_->number());
EXPECT_EQ(2, bar_->number());
}
TEST_F(EnumDescriptorTest, ValueType) {
EXPECT_EQ(enum_ , foo_ ->type());
EXPECT_EQ(enum_ , bar_ ->type());
EXPECT_EQ(enum2_, foo2_->type());
EXPECT_EQ(enum2_, baz2_->type());
}
// ===================================================================
// Test service descriptors.
class ServiceDescriptorTest : public testing::Test {
protected:
virtual void SetUp() {
// Build descriptors for the following messages and service:
// // in "foo.proto"
// message FooRequest {}
// message FooResponse {}
// message BarRequest {}
// message BarResponse {}
// message BazRequest {}
// message BazResponse {}
//
// service TestService {
// rpc Foo(FooRequest) returns (FooResponse);
// rpc Bar(BarRequest) returns (BarResponse);
// }
//
// // in "bar.proto"
// package corge.grault
// service TestService2 {
// rpc Foo(FooRequest) returns (FooResponse);
// rpc Baz(BazRequest) returns (BazResponse);
// }
FileDescriptorProto foo_file;
foo_file.set_name("foo.proto");
AddMessage(&foo_file, "FooRequest");
AddMessage(&foo_file, "FooResponse");
AddMessage(&foo_file, "BarRequest");
AddMessage(&foo_file, "BarResponse");
AddMessage(&foo_file, "BazRequest");
AddMessage(&foo_file, "BazResponse");
ServiceDescriptorProto* service = AddService(&foo_file, "TestService");
AddMethod(service, "Foo", "FooRequest", "FooResponse");
AddMethod(service, "Bar", "BarRequest", "BarResponse");
FileDescriptorProto bar_file;
bar_file.set_name("bar.proto");
bar_file.set_package("corge.grault");
bar_file.add_dependency("foo.proto");
ServiceDescriptorProto* service2 = AddService(&bar_file, "TestService2");
AddMethod(service2, "Foo", "FooRequest", "FooResponse");
AddMethod(service2, "Baz", "BazRequest", "BazResponse");
// Build the descriptors and get the pointers.
foo_file_ = pool_.BuildFile(foo_file);
ASSERT_TRUE(foo_file_ != NULL);
bar_file_ = pool_.BuildFile(bar_file);
ASSERT_TRUE(bar_file_ != NULL);
ASSERT_EQ(6, foo_file_->message_type_count());
foo_request_ = foo_file_->message_type(0);
foo_response_ = foo_file_->message_type(1);
bar_request_ = foo_file_->message_type(2);
bar_response_ = foo_file_->message_type(3);
baz_request_ = foo_file_->message_type(4);
baz_response_ = foo_file_->message_type(5);
ASSERT_EQ(1, foo_file_->service_count());
service_ = foo_file_->service(0);
ASSERT_EQ(2, service_->method_count());
foo_ = service_->method(0);
bar_ = service_->method(1);
ASSERT_EQ(1, bar_file_->service_count());
service2_ = bar_file_->service(0);
ASSERT_EQ(2, service2_->method_count());
foo2_ = service2_->method(0);
baz2_ = service2_->method(1);
}
DescriptorPool pool_;
const FileDescriptor* foo_file_;
const FileDescriptor* bar_file_;
const Descriptor* foo_request_;
const Descriptor* foo_response_;
const Descriptor* bar_request_;
const Descriptor* bar_response_;
const Descriptor* baz_request_;
const Descriptor* baz_response_;
const ServiceDescriptor* service_;
const ServiceDescriptor* service2_;
const MethodDescriptor* foo_;
const MethodDescriptor* bar_;
const MethodDescriptor* foo2_;
const MethodDescriptor* baz2_;
};
TEST_F(ServiceDescriptorTest, Name) {
EXPECT_EQ("TestService", service_->name());
EXPECT_EQ("TestService", service_->full_name());
EXPECT_EQ(foo_file_, service_->file());
EXPECT_EQ("TestService2", service2_->name());
EXPECT_EQ("corge.grault.TestService2", service2_->full_name());
EXPECT_EQ(bar_file_, service2_->file());
}
TEST_F(ServiceDescriptorTest, MethodsByIndex) {
ASSERT_EQ(2, service_->method_count());
EXPECT_EQ(foo_, service_->method(0));
EXPECT_EQ(bar_, service_->method(1));
}
TEST_F(ServiceDescriptorTest, FindMethodByName) {
EXPECT_EQ(foo_ , service_ ->FindMethodByName("Foo"));
EXPECT_EQ(bar_ , service_ ->FindMethodByName("Bar"));
EXPECT_EQ(foo2_, service2_->FindMethodByName("Foo"));
EXPECT_EQ(baz2_, service2_->FindMethodByName("Baz"));
EXPECT_TRUE(service_ ->FindMethodByName("NoSuchMethod") == NULL);
EXPECT_TRUE(service_ ->FindMethodByName("Baz" ) == NULL);
EXPECT_TRUE(service2_->FindMethodByName("Bar" ) == NULL);
}
TEST_F(ServiceDescriptorTest, MethodName) {
EXPECT_EQ("Foo", foo_->name());
EXPECT_EQ("Bar", bar_->name());
}
TEST_F(ServiceDescriptorTest, MethodFullName) {
EXPECT_EQ("TestService.Foo", foo_->full_name());
EXPECT_EQ("TestService.Bar", bar_->full_name());
EXPECT_EQ("corge.grault.TestService2.Foo", foo2_->full_name());
EXPECT_EQ("corge.grault.TestService2.Baz", baz2_->full_name());
}
TEST_F(ServiceDescriptorTest, MethodIndex) {
EXPECT_EQ(0, foo_->index());
EXPECT_EQ(1, bar_->index());
}
TEST_F(ServiceDescriptorTest, MethodParent) {
EXPECT_EQ(service_, foo_->service());
EXPECT_EQ(service_, bar_->service());
}
TEST_F(ServiceDescriptorTest, MethodInputType) {
EXPECT_EQ(foo_request_, foo_->input_type());
EXPECT_EQ(bar_request_, bar_->input_type());
}
TEST_F(ServiceDescriptorTest, MethodOutputType) {
EXPECT_EQ(foo_response_, foo_->output_type());
EXPECT_EQ(bar_response_, bar_->output_type());
}
// ===================================================================
// Test nested types.
class NestedDescriptorTest : public testing::Test {
protected:
virtual void SetUp() {
// Build descriptors for the following definitions:
//
// // in "foo.proto"
// message TestMessage {
// message Foo {}
// message Bar {}
// enum Baz { A = 1; }
// enum Qux { B = 1; }
// }
//
// // in "bar.proto"
// package corge.grault;
// message TestMessage2 {
// message Foo {}
// message Baz {}
// enum Qux { A = 1; }
// enum Quux { C = 1; }
// }
//
// TestMessage2 is primarily here to test FindNestedTypeByName and friends.
// All messages created from the same DescriptorPool share the same lookup
// table, so we need to insure that they don't interfere.
//
// We add enum values to the enums in order to test searching for enum
// values across a message's scope.
FileDescriptorProto foo_file;
foo_file.set_name("foo.proto");
DescriptorProto* message = AddMessage(&foo_file, "TestMessage");
AddNestedMessage(message, "Foo");
AddNestedMessage(message, "Bar");
EnumDescriptorProto* baz = AddNestedEnum(message, "Baz");
AddEnumValue(baz, "A", 1);
EnumDescriptorProto* qux = AddNestedEnum(message, "Qux");
AddEnumValue(qux, "B", 1);
FileDescriptorProto bar_file;
bar_file.set_name("bar.proto");
bar_file.set_package("corge.grault");
DescriptorProto* message2 = AddMessage(&bar_file, "TestMessage2");
AddNestedMessage(message2, "Foo");
AddNestedMessage(message2, "Baz");
EnumDescriptorProto* qux2 = AddNestedEnum(message2, "Qux");
AddEnumValue(qux2, "A", 1);
EnumDescriptorProto* quux2 = AddNestedEnum(message2, "Quux");
AddEnumValue(quux2, "C", 1);
// Build the descriptors and get the pointers.
foo_file_ = pool_.BuildFile(foo_file);
ASSERT_TRUE(foo_file_ != NULL);
bar_file_ = pool_.BuildFile(bar_file);
ASSERT_TRUE(bar_file_ != NULL);
ASSERT_EQ(1, foo_file_->message_type_count());
message_ = foo_file_->message_type(0);
ASSERT_EQ(2, message_->nested_type_count());
foo_ = message_->nested_type(0);
bar_ = message_->nested_type(1);
ASSERT_EQ(2, message_->enum_type_count());
baz_ = message_->enum_type(0);
qux_ = message_->enum_type(1);
ASSERT_EQ(1, baz_->value_count());
a_ = baz_->value(0);
ASSERT_EQ(1, qux_->value_count());
b_ = qux_->value(0);
ASSERT_EQ(1, bar_file_->message_type_count());
message2_ = bar_file_->message_type(0);
ASSERT_EQ(2, message2_->nested_type_count());
foo2_ = message2_->nested_type(0);
baz2_ = message2_->nested_type(1);
ASSERT_EQ(2, message2_->enum_type_count());
qux2_ = message2_->enum_type(0);
quux2_ = message2_->enum_type(1);
ASSERT_EQ(1, qux2_->value_count());
a2_ = qux2_->value(0);
ASSERT_EQ(1, quux2_->value_count());
c2_ = quux2_->value(0);
}
DescriptorPool pool_;
const FileDescriptor* foo_file_;
const FileDescriptor* bar_file_;
const Descriptor* message_;
const Descriptor* message2_;
const Descriptor* foo_;
const Descriptor* bar_;
const EnumDescriptor* baz_;
const EnumDescriptor* qux_;
const EnumValueDescriptor* a_;
const EnumValueDescriptor* b_;
const Descriptor* foo2_;
const Descriptor* baz2_;
const EnumDescriptor* qux2_;
const EnumDescriptor* quux2_;
const EnumValueDescriptor* a2_;
const EnumValueDescriptor* c2_;
};
TEST_F(NestedDescriptorTest, MessageName) {
EXPECT_EQ("Foo", foo_ ->name());
EXPECT_EQ("Bar", bar_ ->name());
EXPECT_EQ("Foo", foo2_->name());
EXPECT_EQ("Baz", baz2_->name());
EXPECT_EQ("TestMessage.Foo", foo_->full_name());
EXPECT_EQ("TestMessage.Bar", bar_->full_name());
EXPECT_EQ("corge.grault.TestMessage2.Foo", foo2_->full_name());
EXPECT_EQ("corge.grault.TestMessage2.Baz", baz2_->full_name());
}
TEST_F(NestedDescriptorTest, MessageContainingType) {
EXPECT_EQ(message_ , foo_ ->containing_type());
EXPECT_EQ(message_ , bar_ ->containing_type());
EXPECT_EQ(message2_, foo2_->containing_type());
EXPECT_EQ(message2_, baz2_->containing_type());
}
TEST_F(NestedDescriptorTest, NestedMessagesByIndex) {
ASSERT_EQ(2, message_->nested_type_count());
EXPECT_EQ(foo_, message_->nested_type(0));
EXPECT_EQ(bar_, message_->nested_type(1));
}
TEST_F(NestedDescriptorTest, FindFieldByNameDoesntFindNestedTypes) {
EXPECT_TRUE(message_->FindFieldByName("Foo") == NULL);
EXPECT_TRUE(message_->FindFieldByName("Qux") == NULL);
EXPECT_TRUE(message_->FindExtensionByName("Foo") == NULL);
EXPECT_TRUE(message_->FindExtensionByName("Qux") == NULL);
}
TEST_F(NestedDescriptorTest, FindNestedTypeByName) {
EXPECT_EQ(foo_ , message_ ->FindNestedTypeByName("Foo"));
EXPECT_EQ(bar_ , message_ ->FindNestedTypeByName("Bar"));
EXPECT_EQ(foo2_, message2_->FindNestedTypeByName("Foo"));
EXPECT_EQ(baz2_, message2_->FindNestedTypeByName("Baz"));
EXPECT_TRUE(message_ ->FindNestedTypeByName("NoSuchType") == NULL);
EXPECT_TRUE(message_ ->FindNestedTypeByName("Baz" ) == NULL);
EXPECT_TRUE(message2_->FindNestedTypeByName("Bar" ) == NULL);
EXPECT_TRUE(message_->FindNestedTypeByName("Qux") == NULL);
}
TEST_F(NestedDescriptorTest, EnumName) {
EXPECT_EQ("Baz" , baz_ ->name());
EXPECT_EQ("Qux" , qux_ ->name());
EXPECT_EQ("Qux" , qux2_->name());
EXPECT_EQ("Quux", quux2_->name());
EXPECT_EQ("TestMessage.Baz", baz_->full_name());
EXPECT_EQ("TestMessage.Qux", qux_->full_name());
EXPECT_EQ("corge.grault.TestMessage2.Qux" , qux2_ ->full_name());
EXPECT_EQ("corge.grault.TestMessage2.Quux", quux2_->full_name());
}
TEST_F(NestedDescriptorTest, EnumContainingType) {
EXPECT_EQ(message_ , baz_ ->containing_type());
EXPECT_EQ(message_ , qux_ ->containing_type());
EXPECT_EQ(message2_, qux2_ ->containing_type());
EXPECT_EQ(message2_, quux2_->containing_type());
}
TEST_F(NestedDescriptorTest, NestedEnumsByIndex) {
ASSERT_EQ(2, message_->nested_type_count());
EXPECT_EQ(foo_, message_->nested_type(0));
EXPECT_EQ(bar_, message_->nested_type(1));
}
TEST_F(NestedDescriptorTest, FindEnumTypeByName) {
EXPECT_EQ(baz_ , message_ ->FindEnumTypeByName("Baz" ));
EXPECT_EQ(qux_ , message_ ->FindEnumTypeByName("Qux" ));
EXPECT_EQ(qux2_ , message2_->FindEnumTypeByName("Qux" ));
EXPECT_EQ(quux2_, message2_->FindEnumTypeByName("Quux"));
EXPECT_TRUE(message_ ->FindEnumTypeByName("NoSuchType") == NULL);
EXPECT_TRUE(message_ ->FindEnumTypeByName("Quux" ) == NULL);
EXPECT_TRUE(message2_->FindEnumTypeByName("Baz" ) == NULL);
EXPECT_TRUE(message_->FindEnumTypeByName("Foo") == NULL);
}
TEST_F(NestedDescriptorTest, FindEnumValueByName) {
EXPECT_EQ(a_ , message_ ->FindEnumValueByName("A"));
EXPECT_EQ(b_ , message_ ->FindEnumValueByName("B"));
EXPECT_EQ(a2_, message2_->FindEnumValueByName("A"));
EXPECT_EQ(c2_, message2_->FindEnumValueByName("C"));
EXPECT_TRUE(message_ ->FindEnumValueByName("NO_SUCH_VALUE") == NULL);
EXPECT_TRUE(message_ ->FindEnumValueByName("C" ) == NULL);
EXPECT_TRUE(message2_->FindEnumValueByName("B" ) == NULL);
EXPECT_TRUE(message_->FindEnumValueByName("Foo") == NULL);
}
// ===================================================================
// Test extensions.
class ExtensionDescriptorTest : public testing::Test {
protected:
virtual void SetUp() {
// Build descriptors for the following definitions:
//
// enum Baz {}
// message Qux {}
//
// message Foo {
// extensions 10 to 19;
// extensions 30 to 39;
// }
// extends Foo with optional int32 foo_int32 = 10;
// extends Foo with repeated TestEnum foo_enum = 19;
// message Bar {
// extends Foo with optional Qux foo_message = 30;
// // (using Qux as the group type)
// extends Foo with repeated group foo_group = 39;
// }
FileDescriptorProto foo_file;
foo_file.set_name("foo.proto");
AddEmptyEnum(&foo_file, "Baz");
AddMessage(&foo_file, "Qux");
DescriptorProto* foo = AddMessage(&foo_file, "Foo");
AddExtensionRange(foo, 10, 20);
AddExtensionRange(foo, 30, 40);
AddExtension(&foo_file, "Foo", "foo_int32", 10,
FieldDescriptorProto::LABEL_OPTIONAL,
FieldDescriptorProto::TYPE_INT32);
AddExtension(&foo_file, "Foo", "foo_enum", 19,
FieldDescriptorProto::LABEL_REPEATED,
FieldDescriptorProto::TYPE_ENUM)
->set_type_name("Baz");
DescriptorProto* bar = AddMessage(&foo_file, "Bar");
AddNestedExtension(bar, "Foo", "foo_message", 30,
FieldDescriptorProto::LABEL_OPTIONAL,
FieldDescriptorProto::TYPE_MESSAGE)
->set_type_name("Qux");
AddNestedExtension(bar, "Foo", "foo_group", 39,
FieldDescriptorProto::LABEL_REPEATED,
FieldDescriptorProto::TYPE_GROUP)
->set_type_name("Qux");
// Build the descriptors and get the pointers.
foo_file_ = pool_.BuildFile(foo_file);
ASSERT_TRUE(foo_file_ != NULL);
ASSERT_EQ(1, foo_file_->enum_type_count());
baz_ = foo_file_->enum_type(0);
ASSERT_EQ(3, foo_file_->message_type_count());
qux_ = foo_file_->message_type(0);
foo_ = foo_file_->message_type(1);
bar_ = foo_file_->message_type(2);
}
DescriptorPool pool_;
const FileDescriptor* foo_file_;
const Descriptor* foo_;
const Descriptor* bar_;
const EnumDescriptor* baz_;
const Descriptor* qux_;
};
TEST_F(ExtensionDescriptorTest, ExtensionRanges) {
EXPECT_EQ(0, bar_->extension_range_count());
ASSERT_EQ(2, foo_->extension_range_count());
EXPECT_EQ(10, foo_->extension_range(0)->start);
EXPECT_EQ(30, foo_->extension_range(1)->start);
EXPECT_EQ(20, foo_->extension_range(0)->end);
EXPECT_EQ(40, foo_->extension_range(1)->end);
};
TEST_F(ExtensionDescriptorTest, Extensions) {
EXPECT_EQ(0, foo_->extension_count());
ASSERT_EQ(2, foo_file_->extension_count());
ASSERT_EQ(2, bar_->extension_count());
EXPECT_TRUE(foo_file_->extension(0)->is_extension());
EXPECT_TRUE(foo_file_->extension(1)->is_extension());
EXPECT_TRUE(bar_->extension(0)->is_extension());
EXPECT_TRUE(bar_->extension(1)->is_extension());
EXPECT_EQ("foo_int32" , foo_file_->extension(0)->name());
EXPECT_EQ("foo_enum" , foo_file_->extension(1)->name());
EXPECT_EQ("foo_message", bar_->extension(0)->name());
EXPECT_EQ("foo_group" , bar_->extension(1)->name());
EXPECT_EQ(10, foo_file_->extension(0)->number());
EXPECT_EQ(19, foo_file_->extension(1)->number());
EXPECT_EQ(30, bar_->extension(0)->number());
EXPECT_EQ(39, bar_->extension(1)->number());
EXPECT_EQ(FieldDescriptor::TYPE_INT32 , foo_file_->extension(0)->type());
EXPECT_EQ(FieldDescriptor::TYPE_ENUM , foo_file_->extension(1)->type());
EXPECT_EQ(FieldDescriptor::TYPE_MESSAGE, bar_->extension(0)->type());
EXPECT_EQ(FieldDescriptor::TYPE_GROUP , bar_->extension(1)->type());
EXPECT_EQ(baz_, foo_file_->extension(1)->enum_type());
EXPECT_EQ(qux_, bar_->extension(0)->message_type());
EXPECT_EQ(qux_, bar_->extension(1)->message_type());
EXPECT_EQ(FieldDescriptor::LABEL_OPTIONAL, foo_file_->extension(0)->label());
EXPECT_EQ(FieldDescriptor::LABEL_REPEATED, foo_file_->extension(1)->label());
EXPECT_EQ(FieldDescriptor::LABEL_OPTIONAL, bar_->extension(0)->label());
EXPECT_EQ(FieldDescriptor::LABEL_REPEATED, bar_->extension(1)->label());
EXPECT_EQ(foo_, foo_file_->extension(0)->containing_type());
EXPECT_EQ(foo_, foo_file_->extension(1)->containing_type());
EXPECT_EQ(foo_, bar_->extension(0)->containing_type());
EXPECT_EQ(foo_, bar_->extension(1)->containing_type());
EXPECT_TRUE(foo_file_->extension(0)->extension_scope() == NULL);
EXPECT_TRUE(foo_file_->extension(1)->extension_scope() == NULL);
EXPECT_EQ(bar_, bar_->extension(0)->extension_scope());
EXPECT_EQ(bar_, bar_->extension(1)->extension_scope());
};
TEST_F(ExtensionDescriptorTest, IsExtensionNumber) {
EXPECT_FALSE(foo_->IsExtensionNumber( 9));
EXPECT_TRUE (foo_->IsExtensionNumber(10));
EXPECT_TRUE (foo_->IsExtensionNumber(19));
EXPECT_FALSE(foo_->IsExtensionNumber(20));
EXPECT_FALSE(foo_->IsExtensionNumber(29));
EXPECT_TRUE (foo_->IsExtensionNumber(30));
EXPECT_TRUE (foo_->IsExtensionNumber(39));
EXPECT_FALSE(foo_->IsExtensionNumber(40));
}
TEST_F(ExtensionDescriptorTest, FindExtensionByName) {
// Note that FileDescriptor::FindExtensionByName() is tested by
// FileDescriptorTest.
ASSERT_EQ(2, bar_->extension_count());
EXPECT_EQ(bar_->extension(0), bar_->FindExtensionByName("foo_message"));
EXPECT_EQ(bar_->extension(1), bar_->FindExtensionByName("foo_group" ));
EXPECT_TRUE(bar_->FindExtensionByName("no_such_extension") == NULL);
EXPECT_TRUE(foo_->FindExtensionByName("foo_int32") == NULL);
EXPECT_TRUE(foo_->FindExtensionByName("foo_message") == NULL);
}
TEST_F(ExtensionDescriptorTest, FindAllExtensions) {
vector<const FieldDescriptor*> extensions;
pool_.FindAllExtensions(foo_, &extensions);
ASSERT_EQ(4, extensions.size());
EXPECT_EQ(10, extensions[0]->number());
EXPECT_EQ(19, extensions[1]->number());
EXPECT_EQ(30, extensions[2]->number());
EXPECT_EQ(39, extensions[3]->number());
}
TEST_F(ExtensionDescriptorTest, DuplicateFieldNumber) {
DescriptorPool pool;
FileDescriptorProto file_proto;
// Add "google/protobuf/descriptor.proto".
FileDescriptorProto::descriptor()->file()->CopyTo(&file_proto);
ASSERT_TRUE(pool.BuildFile(file_proto) != NULL);
// Add "foo.proto":
// import "google/protobuf/descriptor.proto";
// extend google.protobuf.FieldOptions {
// optional int32 option1 = 1000;
// }
file_proto.Clear();
file_proto.set_name("foo.proto");
file_proto.add_dependency("google/protobuf/descriptor.proto");
AddExtension(&file_proto, "google.protobuf.FieldOptions", "option1", 1000,
FieldDescriptorProto::LABEL_OPTIONAL,
FieldDescriptorProto::TYPE_INT32);
ASSERT_TRUE(pool.BuildFile(file_proto) != NULL);
// Add "bar.proto":
// import "google/protobuf/descriptor.proto";
// extend google.protobuf.FieldOptions {
// optional int32 option2 = 1000;
// }
file_proto.Clear();
file_proto.set_name("bar.proto");
file_proto.add_dependency("google/protobuf/descriptor.proto");
AddExtension(&file_proto, "google.protobuf.FieldOptions", "option2", 1000,
FieldDescriptorProto::LABEL_OPTIONAL,
FieldDescriptorProto::TYPE_INT32);
// Currently we only generate a warning for conflicting extension numbers.
// TODO(xiaofeng): Change it to an error.
ASSERT_TRUE(pool.BuildFile(file_proto) != NULL);
}
// ===================================================================
// Test reserved fields.
class ReservedDescriptorTest : public testing::Test {
protected:
virtual void SetUp() {
// Build descriptors for the following definitions:
//
// message Foo {
// reserved 2, 9 to 11, 15;
// reserved "foo", "bar";
// }
FileDescriptorProto foo_file;
foo_file.set_name("foo.proto");
DescriptorProto* foo = AddMessage(&foo_file, "Foo");
AddReservedRange(foo, 2, 3);
AddReservedRange(foo, 9, 12);
AddReservedRange(foo, 15, 16);
foo->add_reserved_name("foo");
foo->add_reserved_name("bar");
// Build the descriptors and get the pointers.
foo_file_ = pool_.BuildFile(foo_file);
ASSERT_TRUE(foo_file_ != NULL);
ASSERT_EQ(1, foo_file_->message_type_count());
foo_ = foo_file_->message_type(0);
}
DescriptorPool pool_;
const FileDescriptor* foo_file_;
const Descriptor* foo_;
};
TEST_F(ReservedDescriptorTest, ReservedRanges) {
ASSERT_EQ(3, foo_->reserved_range_count());
EXPECT_EQ(2, foo_->reserved_range(0)->start);
EXPECT_EQ(3, foo_->reserved_range(0)->end);
EXPECT_EQ(9, foo_->reserved_range(1)->start);
EXPECT_EQ(12, foo_->reserved_range(1)->end);
EXPECT_EQ(15, foo_->reserved_range(2)->start);
EXPECT_EQ(16, foo_->reserved_range(2)->end);
};
TEST_F(ReservedDescriptorTest, IsReservedNumber) {
EXPECT_FALSE(foo_->IsReservedNumber(1));
EXPECT_TRUE (foo_->IsReservedNumber(2));
EXPECT_FALSE(foo_->IsReservedNumber(3));
EXPECT_FALSE(foo_->IsReservedNumber(8));
EXPECT_TRUE (foo_->IsReservedNumber(9));
EXPECT_TRUE (foo_->IsReservedNumber(10));
EXPECT_TRUE (foo_->IsReservedNumber(11));
EXPECT_FALSE(foo_->IsReservedNumber(12));
EXPECT_FALSE(foo_->IsReservedNumber(13));
EXPECT_FALSE(foo_->IsReservedNumber(14));
EXPECT_TRUE (foo_->IsReservedNumber(15));
EXPECT_FALSE(foo_->IsReservedNumber(16));
};
TEST_F(ReservedDescriptorTest, ReservedNames) {
ASSERT_EQ(2, foo_->reserved_name_count());
EXPECT_EQ("foo", foo_->reserved_name(0));
EXPECT_EQ("bar", foo_->reserved_name(1));
};
TEST_F(ReservedDescriptorTest, IsReservedName) {
EXPECT_TRUE (foo_->IsReservedName("foo"));
EXPECT_TRUE (foo_->IsReservedName("bar"));
EXPECT_FALSE(foo_->IsReservedName("baz"));
};
// ===================================================================
class MiscTest : public testing::Test {
protected:
// Function which makes a field descriptor of the given type.
const FieldDescriptor* GetFieldDescriptorOfType(FieldDescriptor::Type type) {
FileDescriptorProto file_proto;
file_proto.set_name("foo.proto");
AddEmptyEnum(&file_proto, "DummyEnum");
DescriptorProto* message = AddMessage(&file_proto, "TestMessage");
FieldDescriptorProto* field =
AddField(message, "foo", 1, FieldDescriptorProto::LABEL_OPTIONAL,
static_cast<FieldDescriptorProto::Type>(static_cast<int>(type)));
if (type == FieldDescriptor::TYPE_MESSAGE ||
type == FieldDescriptor::TYPE_GROUP) {
field->set_type_name("TestMessage");
} else if (type == FieldDescriptor::TYPE_ENUM) {
field->set_type_name("DummyEnum");
}
// Build the descriptors and get the pointers.
pool_.reset(new DescriptorPool());
const FileDescriptor* file = pool_->BuildFile(file_proto);
if (file != NULL &&
file->message_type_count() == 1 &&
file->message_type(0)->field_count() == 1) {
return file->message_type(0)->field(0);
} else {
return NULL;
}
}
const char* GetTypeNameForFieldType(FieldDescriptor::Type type) {
const FieldDescriptor* field = GetFieldDescriptorOfType(type);
return field != NULL ? field->type_name() : "";
}
FieldDescriptor::CppType GetCppTypeForFieldType(FieldDescriptor::Type type) {
const FieldDescriptor* field = GetFieldDescriptorOfType(type);
return field != NULL ? field->cpp_type() :
static_cast<FieldDescriptor::CppType>(0);
}
const char* GetCppTypeNameForFieldType(FieldDescriptor::Type type) {
const FieldDescriptor* field = GetFieldDescriptorOfType(type);
return field != NULL ? field->cpp_type_name() : "";
}
const Descriptor* GetMessageDescriptorForFieldType(
FieldDescriptor::Type type) {
const FieldDescriptor* field = GetFieldDescriptorOfType(type);
return field != NULL ? field->message_type() : NULL;
}
const EnumDescriptor* GetEnumDescriptorForFieldType(
FieldDescriptor::Type type) {
const FieldDescriptor* field = GetFieldDescriptorOfType(type);
return field != NULL ? field->enum_type() : NULL;
}
google::protobuf::scoped_ptr<DescriptorPool> pool_;
};
TEST_F(MiscTest, TypeNames) {
// Test that correct type names are returned.
typedef FieldDescriptor FD; // avoid ugly line wrapping
EXPECT_STREQ("double" , GetTypeNameForFieldType(FD::TYPE_DOUBLE ));
EXPECT_STREQ("float" , GetTypeNameForFieldType(FD::TYPE_FLOAT ));
EXPECT_STREQ("int64" , GetTypeNameForFieldType(FD::TYPE_INT64 ));
EXPECT_STREQ("uint64" , GetTypeNameForFieldType(FD::TYPE_UINT64 ));
EXPECT_STREQ("int32" , GetTypeNameForFieldType(FD::TYPE_INT32 ));
EXPECT_STREQ("fixed64" , GetTypeNameForFieldType(FD::TYPE_FIXED64 ));
EXPECT_STREQ("fixed32" , GetTypeNameForFieldType(FD::TYPE_FIXED32 ));
EXPECT_STREQ("bool" , GetTypeNameForFieldType(FD::TYPE_BOOL ));
EXPECT_STREQ("string" , GetTypeNameForFieldType(FD::TYPE_STRING ));
EXPECT_STREQ("group" , GetTypeNameForFieldType(FD::TYPE_GROUP ));
EXPECT_STREQ("message" , GetTypeNameForFieldType(FD::TYPE_MESSAGE ));
EXPECT_STREQ("bytes" , GetTypeNameForFieldType(FD::TYPE_BYTES ));
EXPECT_STREQ("uint32" , GetTypeNameForFieldType(FD::TYPE_UINT32 ));
EXPECT_STREQ("enum" , GetTypeNameForFieldType(FD::TYPE_ENUM ));
EXPECT_STREQ("sfixed32", GetTypeNameForFieldType(FD::TYPE_SFIXED32));
EXPECT_STREQ("sfixed64", GetTypeNameForFieldType(FD::TYPE_SFIXED64));
EXPECT_STREQ("sint32" , GetTypeNameForFieldType(FD::TYPE_SINT32 ));
EXPECT_STREQ("sint64" , GetTypeNameForFieldType(FD::TYPE_SINT64 ));
}
TEST_F(MiscTest, StaticTypeNames) {
// Test that correct type names are returned.
typedef FieldDescriptor FD; // avoid ugly line wrapping
EXPECT_STREQ("double" , FD::TypeName(FD::TYPE_DOUBLE ));
EXPECT_STREQ("float" , FD::TypeName(FD::TYPE_FLOAT ));
EXPECT_STREQ("int64" , FD::TypeName(FD::TYPE_INT64 ));
EXPECT_STREQ("uint64" , FD::TypeName(FD::TYPE_UINT64 ));
EXPECT_STREQ("int32" , FD::TypeName(FD::TYPE_INT32 ));
EXPECT_STREQ("fixed64" , FD::TypeName(FD::TYPE_FIXED64 ));
EXPECT_STREQ("fixed32" , FD::TypeName(FD::TYPE_FIXED32 ));
EXPECT_STREQ("bool" , FD::TypeName(FD::TYPE_BOOL ));
EXPECT_STREQ("string" , FD::TypeName(FD::TYPE_STRING ));
EXPECT_STREQ("group" , FD::TypeName(FD::TYPE_GROUP ));
EXPECT_STREQ("message" , FD::TypeName(FD::TYPE_MESSAGE ));
EXPECT_STREQ("bytes" , FD::TypeName(FD::TYPE_BYTES ));
EXPECT_STREQ("uint32" , FD::TypeName(FD::TYPE_UINT32 ));
EXPECT_STREQ("enum" , FD::TypeName(FD::TYPE_ENUM ));
EXPECT_STREQ("sfixed32", FD::TypeName(FD::TYPE_SFIXED32));
EXPECT_STREQ("sfixed64", FD::TypeName(FD::TYPE_SFIXED64));
EXPECT_STREQ("sint32" , FD::TypeName(FD::TYPE_SINT32 ));
EXPECT_STREQ("sint64" , FD::TypeName(FD::TYPE_SINT64 ));
}
TEST_F(MiscTest, CppTypes) {
// Test that CPP types are assigned correctly.
typedef FieldDescriptor FD; // avoid ugly line wrapping
EXPECT_EQ(FD::CPPTYPE_DOUBLE , GetCppTypeForFieldType(FD::TYPE_DOUBLE ));
EXPECT_EQ(FD::CPPTYPE_FLOAT , GetCppTypeForFieldType(FD::TYPE_FLOAT ));
EXPECT_EQ(FD::CPPTYPE_INT64 , GetCppTypeForFieldType(FD::TYPE_INT64 ));
EXPECT_EQ(FD::CPPTYPE_UINT64 , GetCppTypeForFieldType(FD::TYPE_UINT64 ));
EXPECT_EQ(FD::CPPTYPE_INT32 , GetCppTypeForFieldType(FD::TYPE_INT32 ));
EXPECT_EQ(FD::CPPTYPE_UINT64 , GetCppTypeForFieldType(FD::TYPE_FIXED64 ));
EXPECT_EQ(FD::CPPTYPE_UINT32 , GetCppTypeForFieldType(FD::TYPE_FIXED32 ));
EXPECT_EQ(FD::CPPTYPE_BOOL , GetCppTypeForFieldType(FD::TYPE_BOOL ));
EXPECT_EQ(FD::CPPTYPE_STRING , GetCppTypeForFieldType(FD::TYPE_STRING ));
EXPECT_EQ(FD::CPPTYPE_MESSAGE, GetCppTypeForFieldType(FD::TYPE_GROUP ));
EXPECT_EQ(FD::CPPTYPE_MESSAGE, GetCppTypeForFieldType(FD::TYPE_MESSAGE ));
EXPECT_EQ(FD::CPPTYPE_STRING , GetCppTypeForFieldType(FD::TYPE_BYTES ));
EXPECT_EQ(FD::CPPTYPE_UINT32 , GetCppTypeForFieldType(FD::TYPE_UINT32 ));
EXPECT_EQ(FD::CPPTYPE_ENUM , GetCppTypeForFieldType(FD::TYPE_ENUM ));
EXPECT_EQ(FD::CPPTYPE_INT32 , GetCppTypeForFieldType(FD::TYPE_SFIXED32));
EXPECT_EQ(FD::CPPTYPE_INT64 , GetCppTypeForFieldType(FD::TYPE_SFIXED64));
EXPECT_EQ(FD::CPPTYPE_INT32 , GetCppTypeForFieldType(FD::TYPE_SINT32 ));
EXPECT_EQ(FD::CPPTYPE_INT64 , GetCppTypeForFieldType(FD::TYPE_SINT64 ));
}
TEST_F(MiscTest, CppTypeNames) {
// Test that correct CPP type names are returned.
typedef FieldDescriptor FD; // avoid ugly line wrapping
EXPECT_STREQ("double" , GetCppTypeNameForFieldType(FD::TYPE_DOUBLE ));
EXPECT_STREQ("float" , GetCppTypeNameForFieldType(FD::TYPE_FLOAT ));
EXPECT_STREQ("int64" , GetCppTypeNameForFieldType(FD::TYPE_INT64 ));
EXPECT_STREQ("uint64" , GetCppTypeNameForFieldType(FD::TYPE_UINT64 ));
EXPECT_STREQ("int32" , GetCppTypeNameForFieldType(FD::TYPE_INT32 ));
EXPECT_STREQ("uint64" , GetCppTypeNameForFieldType(FD::TYPE_FIXED64 ));
EXPECT_STREQ("uint32" , GetCppTypeNameForFieldType(FD::TYPE_FIXED32 ));
EXPECT_STREQ("bool" , GetCppTypeNameForFieldType(FD::TYPE_BOOL ));
EXPECT_STREQ("string" , GetCppTypeNameForFieldType(FD::TYPE_STRING ));
EXPECT_STREQ("message", GetCppTypeNameForFieldType(FD::TYPE_GROUP ));
EXPECT_STREQ("message", GetCppTypeNameForFieldType(FD::TYPE_MESSAGE ));
EXPECT_STREQ("string" , GetCppTypeNameForFieldType(FD::TYPE_BYTES ));
EXPECT_STREQ("uint32" , GetCppTypeNameForFieldType(FD::TYPE_UINT32 ));
EXPECT_STREQ("enum" , GetCppTypeNameForFieldType(FD::TYPE_ENUM ));
EXPECT_STREQ("int32" , GetCppTypeNameForFieldType(FD::TYPE_SFIXED32));
EXPECT_STREQ("int64" , GetCppTypeNameForFieldType(FD::TYPE_SFIXED64));
EXPECT_STREQ("int32" , GetCppTypeNameForFieldType(FD::TYPE_SINT32 ));
EXPECT_STREQ("int64" , GetCppTypeNameForFieldType(FD::TYPE_SINT64 ));
}
TEST_F(MiscTest, StaticCppTypeNames) {
// Test that correct CPP type names are returned.
typedef FieldDescriptor FD; // avoid ugly line wrapping
EXPECT_STREQ("int32" , FD::CppTypeName(FD::CPPTYPE_INT32 ));
EXPECT_STREQ("int64" , FD::CppTypeName(FD::CPPTYPE_INT64 ));
EXPECT_STREQ("uint32" , FD::CppTypeName(FD::CPPTYPE_UINT32 ));
EXPECT_STREQ("uint64" , FD::CppTypeName(FD::CPPTYPE_UINT64 ));
EXPECT_STREQ("double" , FD::CppTypeName(FD::CPPTYPE_DOUBLE ));
EXPECT_STREQ("float" , FD::CppTypeName(FD::CPPTYPE_FLOAT ));
EXPECT_STREQ("bool" , FD::CppTypeName(FD::CPPTYPE_BOOL ));
EXPECT_STREQ("enum" , FD::CppTypeName(FD::CPPTYPE_ENUM ));
EXPECT_STREQ("string" , FD::CppTypeName(FD::CPPTYPE_STRING ));
EXPECT_STREQ("message", FD::CppTypeName(FD::CPPTYPE_MESSAGE));
}
TEST_F(MiscTest, MessageType) {
// Test that message_type() is NULL for non-aggregate fields
typedef FieldDescriptor FD; // avoid ugly line wrapping
EXPECT_TRUE(NULL == GetMessageDescriptorForFieldType(FD::TYPE_DOUBLE ));
EXPECT_TRUE(NULL == GetMessageDescriptorForFieldType(FD::TYPE_FLOAT ));
EXPECT_TRUE(NULL == GetMessageDescriptorForFieldType(FD::TYPE_INT64 ));
EXPECT_TRUE(NULL == GetMessageDescriptorForFieldType(FD::TYPE_UINT64 ));
EXPECT_TRUE(NULL == GetMessageDescriptorForFieldType(FD::TYPE_INT32 ));
EXPECT_TRUE(NULL == GetMessageDescriptorForFieldType(FD::TYPE_FIXED64 ));
EXPECT_TRUE(NULL == GetMessageDescriptorForFieldType(FD::TYPE_FIXED32 ));
EXPECT_TRUE(NULL == GetMessageDescriptorForFieldType(FD::TYPE_BOOL ));
EXPECT_TRUE(NULL == GetMessageDescriptorForFieldType(FD::TYPE_STRING ));
EXPECT_TRUE(NULL != GetMessageDescriptorForFieldType(FD::TYPE_GROUP ));
EXPECT_TRUE(NULL != GetMessageDescriptorForFieldType(FD::TYPE_MESSAGE ));
EXPECT_TRUE(NULL == GetMessageDescriptorForFieldType(FD::TYPE_BYTES ));
EXPECT_TRUE(NULL == GetMessageDescriptorForFieldType(FD::TYPE_UINT32 ));
EXPECT_TRUE(NULL == GetMessageDescriptorForFieldType(FD::TYPE_ENUM ));
EXPECT_TRUE(NULL == GetMessageDescriptorForFieldType(FD::TYPE_SFIXED32));
EXPECT_TRUE(NULL == GetMessageDescriptorForFieldType(FD::TYPE_SFIXED64));
EXPECT_TRUE(NULL == GetMessageDescriptorForFieldType(FD::TYPE_SINT32 ));
EXPECT_TRUE(NULL == GetMessageDescriptorForFieldType(FD::TYPE_SINT64 ));
}
TEST_F(MiscTest, EnumType) {
// Test that enum_type() is NULL for non-enum fields
typedef FieldDescriptor FD; // avoid ugly line wrapping
EXPECT_TRUE(NULL == GetEnumDescriptorForFieldType(FD::TYPE_DOUBLE ));
EXPECT_TRUE(NULL == GetEnumDescriptorForFieldType(FD::TYPE_FLOAT ));
EXPECT_TRUE(NULL == GetEnumDescriptorForFieldType(FD::TYPE_INT64 ));
EXPECT_TRUE(NULL == GetEnumDescriptorForFieldType(FD::TYPE_UINT64 ));
EXPECT_TRUE(NULL == GetEnumDescriptorForFieldType(FD::TYPE_INT32 ));
EXPECT_TRUE(NULL == GetEnumDescriptorForFieldType(FD::TYPE_FIXED64 ));
EXPECT_TRUE(NULL == GetEnumDescriptorForFieldType(FD::TYPE_FIXED32 ));
EXPECT_TRUE(NULL == GetEnumDescriptorForFieldType(FD::TYPE_BOOL ));
EXPECT_TRUE(NULL == GetEnumDescriptorForFieldType(FD::TYPE_STRING ));
EXPECT_TRUE(NULL == GetEnumDescriptorForFieldType(FD::TYPE_GROUP ));
EXPECT_TRUE(NULL == GetEnumDescriptorForFieldType(FD::TYPE_MESSAGE ));
EXPECT_TRUE(NULL == GetEnumDescriptorForFieldType(FD::TYPE_BYTES ));
EXPECT_TRUE(NULL == GetEnumDescriptorForFieldType(FD::TYPE_UINT32 ));
EXPECT_TRUE(NULL != GetEnumDescriptorForFieldType(FD::TYPE_ENUM ));
EXPECT_TRUE(NULL == GetEnumDescriptorForFieldType(FD::TYPE_SFIXED32));
EXPECT_TRUE(NULL == GetEnumDescriptorForFieldType(FD::TYPE_SFIXED64));
EXPECT_TRUE(NULL == GetEnumDescriptorForFieldType(FD::TYPE_SINT32 ));
EXPECT_TRUE(NULL == GetEnumDescriptorForFieldType(FD::TYPE_SINT64 ));
}
TEST_F(MiscTest, DefaultValues) {
// Test that setting default values works.
FileDescriptorProto file_proto;
file_proto.set_name("foo.proto");
EnumDescriptorProto* enum_type_proto = AddEnum(&file_proto, "DummyEnum");
AddEnumValue(enum_type_proto, "A", 1);
AddEnumValue(enum_type_proto, "B", 2);
DescriptorProto* message_proto = AddMessage(&file_proto, "TestMessage");
typedef FieldDescriptorProto FD; // avoid ugly line wrapping
const FD::Label label = FD::LABEL_OPTIONAL;
// Create fields of every CPP type with default values.
AddField(message_proto, "int32" , 1, label, FD::TYPE_INT32 )
->set_default_value("-1");
AddField(message_proto, "int64" , 2, label, FD::TYPE_INT64 )
->set_default_value("-1000000000000");
AddField(message_proto, "uint32", 3, label, FD::TYPE_UINT32)
->set_default_value("42");
AddField(message_proto, "uint64", 4, label, FD::TYPE_UINT64)
->set_default_value("2000000000000");
AddField(message_proto, "float" , 5, label, FD::TYPE_FLOAT )
->set_default_value("4.5");
AddField(message_proto, "double", 6, label, FD::TYPE_DOUBLE)
->set_default_value("10e100");
AddField(message_proto, "bool" , 7, label, FD::TYPE_BOOL )
->set_default_value("true");
AddField(message_proto, "string", 8, label, FD::TYPE_STRING)
->set_default_value("hello");
AddField(message_proto, "data" , 9, label, FD::TYPE_BYTES )
->set_default_value("\\001\\002\\003");
FieldDescriptorProto* enum_field =
AddField(message_proto, "enum", 10, label, FD::TYPE_ENUM);
enum_field->set_type_name("DummyEnum");
enum_field->set_default_value("B");
// Strings are allowed to have empty defaults. (At one point, due to
// a bug, empty defaults for strings were rejected. Oops.)
AddField(message_proto, "empty_string", 11, label, FD::TYPE_STRING)
->set_default_value("");
// Add a second set of fields with implicit defalut values.
AddField(message_proto, "implicit_int32" , 21, label, FD::TYPE_INT32 );
AddField(message_proto, "implicit_int64" , 22, label, FD::TYPE_INT64 );
AddField(message_proto, "implicit_uint32", 23, label, FD::TYPE_UINT32);
AddField(message_proto, "implicit_uint64", 24, label, FD::TYPE_UINT64);
AddField(message_proto, "implicit_float" , 25, label, FD::TYPE_FLOAT );
AddField(message_proto, "implicit_double", 26, label, FD::TYPE_DOUBLE);
AddField(message_proto, "implicit_bool" , 27, label, FD::TYPE_BOOL );
AddField(message_proto, "implicit_string", 28, label, FD::TYPE_STRING);
AddField(message_proto, "implicit_data" , 29, label, FD::TYPE_BYTES );
AddField(message_proto, "implicit_enum" , 30, label, FD::TYPE_ENUM)
->set_type_name("DummyEnum");
// Build it.
DescriptorPool pool;
const FileDescriptor* file = pool.BuildFile(file_proto);
ASSERT_TRUE(file != NULL);
ASSERT_EQ(1, file->enum_type_count());
const EnumDescriptor* enum_type = file->enum_type(0);
ASSERT_EQ(2, enum_type->value_count());
const EnumValueDescriptor* enum_value_a = enum_type->value(0);
const EnumValueDescriptor* enum_value_b = enum_type->value(1);
ASSERT_EQ(1, file->message_type_count());
const Descriptor* message = file->message_type(0);
ASSERT_EQ(21, message->field_count());
// Check the default values.
ASSERT_TRUE(message->field(0)->has_default_value());
ASSERT_TRUE(message->field(1)->has_default_value());
ASSERT_TRUE(message->field(2)->has_default_value());
ASSERT_TRUE(message->field(3)->has_default_value());
ASSERT_TRUE(message->field(4)->has_default_value());
ASSERT_TRUE(message->field(5)->has_default_value());
ASSERT_TRUE(message->field(6)->has_default_value());
ASSERT_TRUE(message->field(7)->has_default_value());
ASSERT_TRUE(message->field(8)->has_default_value());
ASSERT_TRUE(message->field(9)->has_default_value());
ASSERT_TRUE(message->field(10)->has_default_value());
EXPECT_EQ(-1 , message->field(0)->default_value_int32 ());
EXPECT_EQ(-GOOGLE_ULONGLONG(1000000000000),
message->field(1)->default_value_int64 ());
EXPECT_EQ(42 , message->field(2)->default_value_uint32());
EXPECT_EQ(GOOGLE_ULONGLONG(2000000000000),
message->field(3)->default_value_uint64());
EXPECT_EQ(4.5 , message->field(4)->default_value_float ());
EXPECT_EQ(10e100 , message->field(5)->default_value_double());
EXPECT_TRUE( message->field(6)->default_value_bool ());
EXPECT_EQ("hello" , message->field(7)->default_value_string());
EXPECT_EQ("\001\002\003" , message->field(8)->default_value_string());
EXPECT_EQ(enum_value_b , message->field(9)->default_value_enum ());
EXPECT_EQ("" , message->field(10)->default_value_string());
ASSERT_FALSE(message->field(11)->has_default_value());
ASSERT_FALSE(message->field(12)->has_default_value());
ASSERT_FALSE(message->field(13)->has_default_value());
ASSERT_FALSE(message->field(14)->has_default_value());
ASSERT_FALSE(message->field(15)->has_default_value());
ASSERT_FALSE(message->field(16)->has_default_value());
ASSERT_FALSE(message->field(17)->has_default_value());
ASSERT_FALSE(message->field(18)->has_default_value());
ASSERT_FALSE(message->field(19)->has_default_value());
ASSERT_FALSE(message->field(20)->has_default_value());
EXPECT_EQ(0 , message->field(11)->default_value_int32 ());
EXPECT_EQ(0 , message->field(12)->default_value_int64 ());
EXPECT_EQ(0 , message->field(13)->default_value_uint32());
EXPECT_EQ(0 , message->field(14)->default_value_uint64());
EXPECT_EQ(0.0f , message->field(15)->default_value_float ());
EXPECT_EQ(0.0 , message->field(16)->default_value_double());
EXPECT_FALSE( message->field(17)->default_value_bool ());
EXPECT_EQ("" , message->field(18)->default_value_string());
EXPECT_EQ("" , message->field(19)->default_value_string());
EXPECT_EQ(enum_value_a, message->field(20)->default_value_enum());
}
TEST_F(MiscTest, FieldOptions) {
// Try setting field options.
FileDescriptorProto file_proto;
file_proto.set_name("foo.proto");
DescriptorProto* message_proto = AddMessage(&file_proto, "TestMessage");
AddField(message_proto, "foo", 1,
FieldDescriptorProto::LABEL_OPTIONAL,
FieldDescriptorProto::TYPE_INT32);
FieldDescriptorProto* bar_proto =
AddField(message_proto, "bar", 2,
FieldDescriptorProto::LABEL_OPTIONAL,
FieldDescriptorProto::TYPE_INT32);
FieldOptions* options = bar_proto->mutable_options();
options->set_ctype(FieldOptions::CORD);
// Build the descriptors and get the pointers.
DescriptorPool pool;
const FileDescriptor* file = pool.BuildFile(file_proto);
ASSERT_TRUE(file != NULL);
ASSERT_EQ(1, file->message_type_count());
const Descriptor* message = file->message_type(0);
ASSERT_EQ(2, message->field_count());
const FieldDescriptor* foo = message->field(0);
const FieldDescriptor* bar = message->field(1);
// "foo" had no options set, so it should return the default options.
EXPECT_EQ(&FieldOptions::default_instance(), &foo->options());
// "bar" had options set.
EXPECT_NE(&FieldOptions::default_instance(), options);
EXPECT_TRUE(bar->options().has_ctype());
EXPECT_EQ(FieldOptions::CORD, bar->options().ctype());
}
// ===================================================================
enum DescriptorPoolMode {
NO_DATABASE,
FALLBACK_DATABASE
};
class AllowUnknownDependenciesTest
: public testing::TestWithParam<DescriptorPoolMode> {
protected:
DescriptorPoolMode mode() {
return GetParam();
}
virtual void SetUp() {
FileDescriptorProto foo_proto, bar_proto;
switch (mode()) {
case NO_DATABASE:
pool_.reset(new DescriptorPool);
break;
case FALLBACK_DATABASE:
pool_.reset(new DescriptorPool(&db_));
break;
}
pool_->AllowUnknownDependencies();
ASSERT_TRUE(TextFormat::ParseFromString(
"name: 'foo.proto'"
"dependency: 'bar.proto'"
"dependency: 'baz.proto'"
"message_type {"
" name: 'Foo'"
" field { name:'bar' number:1 label:LABEL_OPTIONAL type_name:'Bar' }"
" field { name:'baz' number:2 label:LABEL_OPTIONAL type_name:'Baz' }"
" field { name:'qux' number:3 label:LABEL_OPTIONAL"
" type_name: '.corge.Qux'"
" type: TYPE_ENUM"
" options {"
" uninterpreted_option {"
" name {"
" name_part: 'grault'"
" is_extension: true"
" }"
" positive_int_value: 1234"
" }"
" }"
" }"
"}",
&foo_proto));
ASSERT_TRUE(TextFormat::ParseFromString(
"name: 'bar.proto'"
"message_type { name: 'Bar' }",
&bar_proto));
// Collect pointers to stuff.
bar_file_ = BuildFile(bar_proto);
ASSERT_TRUE(bar_file_ != NULL);
ASSERT_EQ(1, bar_file_->message_type_count());
bar_type_ = bar_file_->message_type(0);
foo_file_ = BuildFile(foo_proto);
ASSERT_TRUE(foo_file_ != NULL);
ASSERT_EQ(1, foo_file_->message_type_count());
foo_type_ = foo_file_->message_type(0);
ASSERT_EQ(3, foo_type_->field_count());
bar_field_ = foo_type_->field(0);
baz_field_ = foo_type_->field(1);
qux_field_ = foo_type_->field(2);
}
const FileDescriptor* BuildFile(const FileDescriptorProto& proto) {
switch (mode()) {
case NO_DATABASE:
return pool_->BuildFile(proto);
break;
case FALLBACK_DATABASE: {
EXPECT_TRUE(db_.Add(proto));
return pool_->FindFileByName(proto.name());
}
}
GOOGLE_LOG(FATAL) << "Can't get here.";
return NULL;
}
const FileDescriptor* bar_file_;
const Descriptor* bar_type_;
const FileDescriptor* foo_file_;
const Descriptor* foo_type_;
const FieldDescriptor* bar_field_;
const FieldDescriptor* baz_field_;
const FieldDescriptor* qux_field_;
SimpleDescriptorDatabase db_; // used if in FALLBACK_DATABASE mode.
google::protobuf::scoped_ptr<DescriptorPool> pool_;
};
TEST_P(AllowUnknownDependenciesTest, PlaceholderFile) {
ASSERT_EQ(2, foo_file_->dependency_count());
EXPECT_EQ(bar_file_, foo_file_->dependency(0));
EXPECT_FALSE(bar_file_->is_placeholder());
const FileDescriptor* baz_file = foo_file_->dependency(1);
EXPECT_EQ("baz.proto", baz_file->name());
EXPECT_EQ(0, baz_file->message_type_count());
EXPECT_TRUE(baz_file->is_placeholder());
// Placeholder files should not be findable.
EXPECT_EQ(bar_file_, pool_->FindFileByName(bar_file_->name()));
EXPECT_TRUE(pool_->FindFileByName(baz_file->name()) == NULL);
// Copy*To should not crash for placeholder files.
FileDescriptorProto baz_file_proto;
baz_file->CopyTo(&baz_file_proto);
baz_file->CopySourceCodeInfoTo(&baz_file_proto);
EXPECT_FALSE(baz_file_proto.has_source_code_info());
}
TEST_P(AllowUnknownDependenciesTest, PlaceholderTypes) {
ASSERT_EQ(FieldDescriptor::TYPE_MESSAGE, bar_field_->type());
EXPECT_EQ(bar_type_, bar_field_->message_type());
EXPECT_FALSE(bar_type_->is_placeholder());
ASSERT_EQ(FieldDescriptor::TYPE_MESSAGE, baz_field_->type());
const Descriptor* baz_type = baz_field_->message_type();
EXPECT_EQ("Baz", baz_type->name());
EXPECT_EQ("Baz", baz_type->full_name());
EXPECT_EQ(0, baz_type->extension_range_count());
EXPECT_TRUE(baz_type->is_placeholder());
ASSERT_EQ(FieldDescriptor::TYPE_ENUM, qux_field_->type());
const EnumDescriptor* qux_type = qux_field_->enum_type();
EXPECT_EQ("Qux", qux_type->name());
EXPECT_EQ("corge.Qux", qux_type->full_name());
EXPECT_TRUE(qux_type->is_placeholder());
// Placeholder types should not be findable.
EXPECT_EQ(bar_type_, pool_->FindMessageTypeByName(bar_type_->full_name()));
EXPECT_TRUE(pool_->FindMessageTypeByName(baz_type->full_name()) == NULL);
EXPECT_TRUE(pool_->FindEnumTypeByName(qux_type->full_name()) == NULL);
}
TEST_P(AllowUnknownDependenciesTest, CopyTo) {
// FieldDescriptor::CopyTo() should write non-fully-qualified type names
// for placeholder types which were not originally fully-qualified.
FieldDescriptorProto proto;
// Bar is not a placeholder, so it is fully-qualified.
bar_field_->CopyTo(&proto);
EXPECT_EQ(".Bar", proto.type_name());
EXPECT_EQ(FieldDescriptorProto::TYPE_MESSAGE, proto.type());
// Baz is an unqualified placeholder.
proto.Clear();
baz_field_->CopyTo(&proto);
EXPECT_EQ("Baz", proto.type_name());
EXPECT_FALSE(proto.has_type());
// Qux is a fully-qualified placeholder.
proto.Clear();
qux_field_->CopyTo(&proto);
EXPECT_EQ(".corge.Qux", proto.type_name());
EXPECT_EQ(FieldDescriptorProto::TYPE_ENUM, proto.type());
}
TEST_P(AllowUnknownDependenciesTest, CustomOptions) {
// Qux should still have the uninterpreted option attached.
ASSERT_EQ(1, qux_field_->options().uninterpreted_option_size());
const UninterpretedOption& option =
qux_field_->options().uninterpreted_option(0);
ASSERT_EQ(1, option.name_size());
EXPECT_EQ("grault", option.name(0).name_part());
}
TEST_P(AllowUnknownDependenciesTest, UnknownExtendee) {
// Test that we can extend an unknown type. This is slightly tricky because
// it means that the placeholder type must have an extension range.
FileDescriptorProto extension_proto;
ASSERT_TRUE(TextFormat::ParseFromString(
"name: 'extension.proto'"
"extension { extendee: 'UnknownType' name:'some_extension' number:123"
" label:LABEL_OPTIONAL type:TYPE_INT32 }",
&extension_proto));
const FileDescriptor* file = BuildFile(extension_proto);
ASSERT_TRUE(file != NULL);
ASSERT_EQ(1, file->extension_count());
const Descriptor* extendee = file->extension(0)->containing_type();
EXPECT_EQ("UnknownType", extendee->name());
EXPECT_TRUE(extendee->is_placeholder());
ASSERT_EQ(1, extendee->extension_range_count());
EXPECT_EQ(1, extendee->extension_range(0)->start);
EXPECT_EQ(FieldDescriptor::kMaxNumber + 1, extendee->extension_range(0)->end);
}
TEST_P(AllowUnknownDependenciesTest, CustomOption) {
// Test that we can use a custom option without having parsed
// descriptor.proto.
FileDescriptorProto option_proto;
ASSERT_TRUE(TextFormat::ParseFromString(
"name: \"unknown_custom_options.proto\" "
"dependency: \"google/protobuf/descriptor.proto\" "
"extension { "
" extendee: \"google.protobuf.FileOptions\" "
" name: \"some_option\" "
" number: 123456 "
" label: LABEL_OPTIONAL "
" type: TYPE_INT32 "
"} "
"options { "
" uninterpreted_option { "
" name { "
" name_part: \"some_option\" "
" is_extension: true "
" } "
" positive_int_value: 1234 "
" } "
" uninterpreted_option { "
" name { "
" name_part: \"unknown_option\" "
" is_extension: true "
" } "
" positive_int_value: 1234 "
" } "
" uninterpreted_option { "
" name { "
" name_part: \"optimize_for\" "
" is_extension: false "
" } "
" identifier_value: \"SPEED\" "
" } "
"}",
&option_proto));
const FileDescriptor* file = BuildFile(option_proto);
ASSERT_TRUE(file != NULL);
// Verify that no extension options were set, but they were left as
// uninterpreted_options.
vector<const FieldDescriptor*> fields;
file->options().GetReflection()->ListFields(file->options(), &fields);
ASSERT_EQ(2, fields.size());
EXPECT_TRUE(file->options().has_optimize_for());
EXPECT_EQ(2, file->options().uninterpreted_option_size());
}
TEST_P(AllowUnknownDependenciesTest,
UndeclaredDependencyTriggersBuildOfDependency) {
// Crazy case: suppose foo.proto refers to a symbol without declaring the
// dependency that finds it. In the event that the pool is backed by a
// DescriptorDatabase, the pool will attempt to find the symbol in the
// database. If successful, it will build the undeclared dependency to verify
// that the file does indeed contain the symbol. If that file fails to build,
// then its descriptors must be rolled back. However, we still want foo.proto
// to build successfully, since we are allowing unknown dependencies.
FileDescriptorProto undeclared_dep_proto;
// We make this file fail to build by giving it two fields with tag 1.
ASSERT_TRUE(TextFormat::ParseFromString(
"name: \"invalid_file_as_undeclared_dep.proto\" "
"package: \"undeclared\" "
"message_type: { "
" name: \"Quux\" "
" field { "
" name:'qux' number:1 label:LABEL_OPTIONAL type: TYPE_INT32 "
" }"
" field { "
" name:'quux' number:1 label:LABEL_OPTIONAL type: TYPE_INT64 "
" }"
"}",
&undeclared_dep_proto));
// We can't use the BuildFile() helper because we don't actually want to build
// it into the descriptor pool in the fallback database case: it just needs to
// be sitting in the database so that it gets built during the building of
// test.proto below.
switch (mode()) {
case NO_DATABASE: {
ASSERT_TRUE(pool_->BuildFile(undeclared_dep_proto) == NULL);
break;
}
case FALLBACK_DATABASE: {
ASSERT_TRUE(db_.Add(undeclared_dep_proto));
}
}
FileDescriptorProto test_proto;
ASSERT_TRUE(TextFormat::ParseFromString(
"name: \"test.proto\" "
"message_type: { "
" name: \"Corge\" "
" field { "
" name:'quux' number:1 label: LABEL_OPTIONAL "
" type_name:'undeclared.Quux' type: TYPE_MESSAGE "
" }"
"}",
&test_proto));
const FileDescriptor* file = BuildFile(test_proto);
ASSERT_TRUE(file != NULL);
GOOGLE_LOG(INFO) << file->DebugString();
EXPECT_EQ(0, file->dependency_count());
ASSERT_EQ(1, file->message_type_count());
const Descriptor* corge_desc = file->message_type(0);
ASSERT_EQ("Corge", corge_desc->name());
ASSERT_EQ(1, corge_desc->field_count());
EXPECT_FALSE(corge_desc->is_placeholder());
const FieldDescriptor* quux_field = corge_desc->field(0);
ASSERT_EQ(FieldDescriptor::TYPE_MESSAGE, quux_field->type());
ASSERT_EQ("Quux", quux_field->message_type()->name());
ASSERT_EQ("undeclared.Quux", quux_field->message_type()->full_name());
EXPECT_TRUE(quux_field->message_type()->is_placeholder());
// The place holder type should not be findable.
ASSERT_TRUE(pool_->FindMessageTypeByName("undeclared.Quux") == NULL);
}
INSTANTIATE_TEST_CASE_P(DatabaseSource,
AllowUnknownDependenciesTest,
testing::Values(NO_DATABASE, FALLBACK_DATABASE));
// ===================================================================
TEST(CustomOptions, OptionLocations) {
const Descriptor* message =
protobuf_unittest::TestMessageWithCustomOptions::descriptor();
const FileDescriptor* file = message->file();
const FieldDescriptor* field = message->FindFieldByName("field1");
const EnumDescriptor* enm = message->FindEnumTypeByName("AnEnum");
// TODO(benjy): Support EnumValue options, once the compiler does.
const ServiceDescriptor* service =
file->FindServiceByName("TestServiceWithCustomOptions");
const MethodDescriptor* method = service->FindMethodByName("Foo");
EXPECT_EQ(GOOGLE_LONGLONG(9876543210),
file->options().GetExtension(protobuf_unittest::file_opt1));
EXPECT_EQ(-56,
message->options().GetExtension(protobuf_unittest::message_opt1));
EXPECT_EQ(GOOGLE_LONGLONG(8765432109),
field->options().GetExtension(protobuf_unittest::field_opt1));
EXPECT_EQ(42, // Check that we get the default for an option we don't set.
field->options().GetExtension(protobuf_unittest::field_opt2));
EXPECT_EQ(-789,
enm->options().GetExtension(protobuf_unittest::enum_opt1));
EXPECT_EQ(123,
enm->value(1)->options().GetExtension(
protobuf_unittest::enum_value_opt1));
EXPECT_EQ(GOOGLE_LONGLONG(-9876543210),
service->options().GetExtension(protobuf_unittest::service_opt1));
EXPECT_EQ(protobuf_unittest::METHODOPT1_VAL2,
method->options().GetExtension(protobuf_unittest::method_opt1));
// See that the regular options went through unscathed.
EXPECT_TRUE(message->options().has_message_set_wire_format());
EXPECT_EQ(FieldOptions::CORD, field->options().ctype());
}
TEST(CustomOptions, OptionTypes) {
const MessageOptions* options = NULL;
options =
&protobuf_unittest::