PageRenderTime 146ms CodeModel.GetById 14ms app.highlight 114ms RepoModel.GetById 1ms app.codeStats 1ms

/thirdparty/breakpad/third_party/protobuf/protobuf/src/google/protobuf/descriptor_unittest.cc

http://github.com/tomahawk-player/tomahawk
C++ | 1748 lines | 1249 code | 287 blank | 212 comment | 122 complexity | 7c1ca12f3da94d978fb5097cf54ef868 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

   1// Protocol Buffers - Google's data interchange format
   2// Copyright 2008 Google Inc.  All rights reserved.
   3// http://code.google.com/p/protobuf/
   4//
   5// Redistribution and use in source and binary forms, with or without
   6// modification, are permitted provided that the following conditions are
   7// met:
   8//
   9//     * Redistributions of source code must retain the above copyright
  10// notice, this list of conditions and the following disclaimer.
  11//     * Redistributions in binary form must reproduce the above
  12// copyright notice, this list of conditions and the following disclaimer
  13// in the documentation and/or other materials provided with the
  14// distribution.
  15//     * Neither the name of Google Inc. nor the names of its
  16// contributors may be used to endorse or promote products derived from
  17// this software without specific prior written permission.
  18//
  19// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30
  31// Author: kenton@google.com (Kenton Varda)
  32//  Based on original Protocol Buffers design by
  33//  Sanjay Ghemawat, Jeff Dean, and others.
  34//
  35// This file makes extensive use of RFC 3092.  :)
  36
  37#include <vector>
  38
  39#include <google/protobuf/descriptor.h>
  40#include <google/protobuf/descriptor_database.h>
  41#include <google/protobuf/dynamic_message.h>
  42#include <google/protobuf/descriptor.pb.h>
  43#include <google/protobuf/text_format.h>
  44#include <google/protobuf/unittest.pb.h>
  45#include <google/protobuf/unittest_custom_options.pb.h>
  46#include <google/protobuf/stubs/strutil.h>
  47#include <google/protobuf/stubs/substitute.h>
  48
  49#include <google/protobuf/stubs/common.h>
  50#include <google/protobuf/testing/googletest.h>
  51#include <gtest/gtest.h>
  52
  53namespace google {
  54namespace protobuf {
  55
  56// Can't use an anonymous namespace here due to brokenness of Tru64 compiler.
  57namespace descriptor_unittest {
  58
  59// Some helpers to make assembling descriptors faster.
  60DescriptorProto* AddMessage(FileDescriptorProto* file, const string& name) {
  61  DescriptorProto* result = file->add_message_type();
  62  result->set_name(name);
  63  return result;
  64}
  65
  66DescriptorProto* AddNestedMessage(DescriptorProto* parent, const string& name) {
  67  DescriptorProto* result = parent->add_nested_type();
  68  result->set_name(name);
  69  return result;
  70}
  71
  72EnumDescriptorProto* AddEnum(FileDescriptorProto* file, const string& name) {
  73  EnumDescriptorProto* result = file->add_enum_type();
  74  result->set_name(name);
  75  return result;
  76}
  77
  78EnumDescriptorProto* AddNestedEnum(DescriptorProto* parent,
  79                                   const string& name) {
  80  EnumDescriptorProto* result = parent->add_enum_type();
  81  result->set_name(name);
  82  return result;
  83}
  84
  85ServiceDescriptorProto* AddService(FileDescriptorProto* file,
  86                                   const string& name) {
  87  ServiceDescriptorProto* result = file->add_service();
  88  result->set_name(name);
  89  return result;
  90}
  91
  92FieldDescriptorProto* AddField(DescriptorProto* parent,
  93                               const string& name, int number,
  94                               FieldDescriptorProto::Label label,
  95                               FieldDescriptorProto::Type type) {
  96  FieldDescriptorProto* result = parent->add_field();
  97  result->set_name(name);
  98  result->set_number(number);
  99  result->set_label(label);
 100  result->set_type(type);
 101  return result;
 102}
 103
 104FieldDescriptorProto* AddExtension(FileDescriptorProto* file,
 105                                   const string& extendee,
 106                                   const string& name, int number,
 107                                   FieldDescriptorProto::Label label,
 108                                   FieldDescriptorProto::Type type) {
 109  FieldDescriptorProto* result = file->add_extension();
 110  result->set_name(name);
 111  result->set_number(number);
 112  result->set_label(label);
 113  result->set_type(type);
 114  result->set_extendee(extendee);
 115  return result;
 116}
 117
 118FieldDescriptorProto* AddNestedExtension(DescriptorProto* parent,
 119                                         const string& extendee,
 120                                         const string& name, int number,
 121                                         FieldDescriptorProto::Label label,
 122                                         FieldDescriptorProto::Type type) {
 123  FieldDescriptorProto* result = parent->add_extension();
 124  result->set_name(name);
 125  result->set_number(number);
 126  result->set_label(label);
 127  result->set_type(type);
 128  result->set_extendee(extendee);
 129  return result;
 130}
 131
 132DescriptorProto::ExtensionRange* AddExtensionRange(DescriptorProto* parent,
 133                                                   int start, int end) {
 134  DescriptorProto::ExtensionRange* result = parent->add_extension_range();
 135  result->set_start(start);
 136  result->set_end(end);
 137  return result;
 138}
 139
 140EnumValueDescriptorProto* AddEnumValue(EnumDescriptorProto* enum_proto,
 141                                       const string& name, int number) {
 142  EnumValueDescriptorProto* result = enum_proto->add_value();
 143  result->set_name(name);
 144  result->set_number(number);
 145  return result;
 146}
 147
 148MethodDescriptorProto* AddMethod(ServiceDescriptorProto* service,
 149                                 const string& name,
 150                                 const string& input_type,
 151                                 const string& output_type) {
 152  MethodDescriptorProto* result = service->add_method();
 153  result->set_name(name);
 154  result->set_input_type(input_type);
 155  result->set_output_type(output_type);
 156  return result;
 157}
 158
 159// Empty enums technically aren't allowed.  We need to insert a dummy value
 160// into them.
 161void AddEmptyEnum(FileDescriptorProto* file, const string& name) {
 162  AddEnumValue(AddEnum(file, name), name + "_DUMMY", 1);
 163}
 164
 165// ===================================================================
 166
 167// Test simple files.
 168class FileDescriptorTest : public testing::Test {
 169 protected:
 170  virtual void SetUp() {
 171    // Build descriptors for the following definitions:
 172    //
 173    //   // in "foo.proto"
 174    //   message FooMessage { extensions 1; }
 175    //   enum FooEnum {FOO_ENUM_VALUE = 1;}
 176    //   service FooService {}
 177    //   extend FooMessage { optional int32 foo_extension = 1; }
 178    //
 179    //   // in "bar.proto"
 180    //   package bar_package;
 181    //   message BarMessage { extensions 1; }
 182    //   enum BarEnum {BAR_ENUM_VALUE = 1;}
 183    //   service BarService {}
 184    //   extend BarMessage { optional int32 bar_extension = 1; }
 185    //
 186    // Also, we have an empty file "baz.proto".  This file's purpose is to
 187    // make sure that even though it has the same package as foo.proto,
 188    // searching it for members of foo.proto won't work.
 189
 190    FileDescriptorProto foo_file;
 191    foo_file.set_name("foo.proto");
 192    AddExtensionRange(AddMessage(&foo_file, "FooMessage"), 1, 2);
 193    AddEnumValue(AddEnum(&foo_file, "FooEnum"), "FOO_ENUM_VALUE", 1);
 194    AddService(&foo_file, "FooService");
 195    AddExtension(&foo_file, "FooMessage", "foo_extension", 1,
 196                 FieldDescriptorProto::LABEL_OPTIONAL,
 197                 FieldDescriptorProto::TYPE_INT32);
 198
 199    FileDescriptorProto bar_file;
 200    bar_file.set_name("bar.proto");
 201    bar_file.set_package("bar_package");
 202    bar_file.add_dependency("foo.proto");
 203    AddExtensionRange(AddMessage(&bar_file, "BarMessage"), 1, 2);
 204    AddEnumValue(AddEnum(&bar_file, "BarEnum"), "BAR_ENUM_VALUE", 1);
 205    AddService(&bar_file, "BarService");
 206    AddExtension(&bar_file, "bar_package.BarMessage", "bar_extension", 1,
 207                 FieldDescriptorProto::LABEL_OPTIONAL,
 208                 FieldDescriptorProto::TYPE_INT32);
 209
 210    FileDescriptorProto baz_file;
 211    baz_file.set_name("baz.proto");
 212
 213    // Build the descriptors and get the pointers.
 214    foo_file_ = pool_.BuildFile(foo_file);
 215    ASSERT_TRUE(foo_file_ != NULL);
 216
 217    bar_file_ = pool_.BuildFile(bar_file);
 218    ASSERT_TRUE(bar_file_ != NULL);
 219
 220    baz_file_ = pool_.BuildFile(baz_file);
 221    ASSERT_TRUE(baz_file_ != NULL);
 222
 223    ASSERT_EQ(1, foo_file_->message_type_count());
 224    foo_message_ = foo_file_->message_type(0);
 225    ASSERT_EQ(1, foo_file_->enum_type_count());
 226    foo_enum_ = foo_file_->enum_type(0);
 227    ASSERT_EQ(1, foo_enum_->value_count());
 228    foo_enum_value_ = foo_enum_->value(0);
 229    ASSERT_EQ(1, foo_file_->service_count());
 230    foo_service_ = foo_file_->service(0);
 231    ASSERT_EQ(1, foo_file_->extension_count());
 232    foo_extension_ = foo_file_->extension(0);
 233
 234    ASSERT_EQ(1, bar_file_->message_type_count());
 235    bar_message_ = bar_file_->message_type(0);
 236    ASSERT_EQ(1, bar_file_->enum_type_count());
 237    bar_enum_ = bar_file_->enum_type(0);
 238    ASSERT_EQ(1, bar_enum_->value_count());
 239    bar_enum_value_ = bar_enum_->value(0);
 240    ASSERT_EQ(1, bar_file_->service_count());
 241    bar_service_ = bar_file_->service(0);
 242    ASSERT_EQ(1, bar_file_->extension_count());
 243    bar_extension_ = bar_file_->extension(0);
 244  }
 245
 246  DescriptorPool pool_;
 247
 248  const FileDescriptor* foo_file_;
 249  const FileDescriptor* bar_file_;
 250  const FileDescriptor* baz_file_;
 251
 252  const Descriptor*          foo_message_;
 253  const EnumDescriptor*      foo_enum_;
 254  const EnumValueDescriptor* foo_enum_value_;
 255  const ServiceDescriptor*   foo_service_;
 256  const FieldDescriptor*     foo_extension_;
 257
 258  const Descriptor*          bar_message_;
 259  const EnumDescriptor*      bar_enum_;
 260  const EnumValueDescriptor* bar_enum_value_;
 261  const ServiceDescriptor*   bar_service_;
 262  const FieldDescriptor*     bar_extension_;
 263};
 264
 265TEST_F(FileDescriptorTest, Name) {
 266  EXPECT_EQ("foo.proto", foo_file_->name());
 267  EXPECT_EQ("bar.proto", bar_file_->name());
 268  EXPECT_EQ("baz.proto", baz_file_->name());
 269}
 270
 271TEST_F(FileDescriptorTest, Package) {
 272  EXPECT_EQ("", foo_file_->package());
 273  EXPECT_EQ("bar_package", bar_file_->package());
 274}
 275
 276TEST_F(FileDescriptorTest, Dependencies) {
 277  EXPECT_EQ(0, foo_file_->dependency_count());
 278  EXPECT_EQ(1, bar_file_->dependency_count());
 279  EXPECT_EQ(foo_file_, bar_file_->dependency(0));
 280}
 281
 282TEST_F(FileDescriptorTest, FindMessageTypeByName) {
 283  EXPECT_EQ(foo_message_, foo_file_->FindMessageTypeByName("FooMessage"));
 284  EXPECT_EQ(bar_message_, bar_file_->FindMessageTypeByName("BarMessage"));
 285
 286  EXPECT_TRUE(foo_file_->FindMessageTypeByName("BarMessage") == NULL);
 287  EXPECT_TRUE(bar_file_->FindMessageTypeByName("FooMessage") == NULL);
 288  EXPECT_TRUE(baz_file_->FindMessageTypeByName("FooMessage") == NULL);
 289
 290  EXPECT_TRUE(foo_file_->FindMessageTypeByName("NoSuchMessage") == NULL);
 291  EXPECT_TRUE(foo_file_->FindMessageTypeByName("FooEnum") == NULL);
 292}
 293
 294TEST_F(FileDescriptorTest, FindEnumTypeByName) {
 295  EXPECT_EQ(foo_enum_, foo_file_->FindEnumTypeByName("FooEnum"));
 296  EXPECT_EQ(bar_enum_, bar_file_->FindEnumTypeByName("BarEnum"));
 297
 298  EXPECT_TRUE(foo_file_->FindEnumTypeByName("BarEnum") == NULL);
 299  EXPECT_TRUE(bar_file_->FindEnumTypeByName("FooEnum") == NULL);
 300  EXPECT_TRUE(baz_file_->FindEnumTypeByName("FooEnum") == NULL);
 301
 302  EXPECT_TRUE(foo_file_->FindEnumTypeByName("NoSuchEnum") == NULL);
 303  EXPECT_TRUE(foo_file_->FindEnumTypeByName("FooMessage") == NULL);
 304}
 305
 306TEST_F(FileDescriptorTest, FindEnumValueByName) {
 307  EXPECT_EQ(foo_enum_value_, foo_file_->FindEnumValueByName("FOO_ENUM_VALUE"));
 308  EXPECT_EQ(bar_enum_value_, bar_file_->FindEnumValueByName("BAR_ENUM_VALUE"));
 309
 310  EXPECT_TRUE(foo_file_->FindEnumValueByName("BAR_ENUM_VALUE") == NULL);
 311  EXPECT_TRUE(bar_file_->FindEnumValueByName("FOO_ENUM_VALUE") == NULL);
 312  EXPECT_TRUE(baz_file_->FindEnumValueByName("FOO_ENUM_VALUE") == NULL);
 313
 314  EXPECT_TRUE(foo_file_->FindEnumValueByName("NO_SUCH_VALUE") == NULL);
 315  EXPECT_TRUE(foo_file_->FindEnumValueByName("FooMessage") == NULL);
 316}
 317
 318TEST_F(FileDescriptorTest, FindServiceByName) {
 319  EXPECT_EQ(foo_service_, foo_file_->FindServiceByName("FooService"));
 320  EXPECT_EQ(bar_service_, bar_file_->FindServiceByName("BarService"));
 321
 322  EXPECT_TRUE(foo_file_->FindServiceByName("BarService") == NULL);
 323  EXPECT_TRUE(bar_file_->FindServiceByName("FooService") == NULL);
 324  EXPECT_TRUE(baz_file_->FindServiceByName("FooService") == NULL);
 325
 326  EXPECT_TRUE(foo_file_->FindServiceByName("NoSuchService") == NULL);
 327  EXPECT_TRUE(foo_file_->FindServiceByName("FooMessage") == NULL);
 328}
 329
 330TEST_F(FileDescriptorTest, FindExtensionByName) {
 331  EXPECT_EQ(foo_extension_, foo_file_->FindExtensionByName("foo_extension"));
 332  EXPECT_EQ(bar_extension_, bar_file_->FindExtensionByName("bar_extension"));
 333
 334  EXPECT_TRUE(foo_file_->FindExtensionByName("bar_extension") == NULL);
 335  EXPECT_TRUE(bar_file_->FindExtensionByName("foo_extension") == NULL);
 336  EXPECT_TRUE(baz_file_->FindExtensionByName("foo_extension") == NULL);
 337
 338  EXPECT_TRUE(foo_file_->FindExtensionByName("no_such_extension") == NULL);
 339  EXPECT_TRUE(foo_file_->FindExtensionByName("FooMessage") == NULL);
 340}
 341
 342TEST_F(FileDescriptorTest, FindExtensionByNumber) {
 343  EXPECT_EQ(foo_extension_, pool_.FindExtensionByNumber(foo_message_, 1));
 344  EXPECT_EQ(bar_extension_, pool_.FindExtensionByNumber(bar_message_, 1));
 345
 346  EXPECT_TRUE(pool_.FindExtensionByNumber(foo_message_, 2) == NULL);
 347}
 348
 349TEST_F(FileDescriptorTest, BuildAgain) {
 350  // Test that if te call BuildFile again on the same input we get the same
 351  // FileDescriptor back.
 352  FileDescriptorProto file;
 353  foo_file_->CopyTo(&file);
 354  EXPECT_EQ(foo_file_, pool_.BuildFile(file));
 355
 356  // But if we change the file then it won't work.
 357  file.set_package("some.other.package");
 358  EXPECT_TRUE(pool_.BuildFile(file) == NULL);
 359}
 360
 361// ===================================================================
 362
 363// Test simple flat messages and fields.
 364class DescriptorTest : public testing::Test {
 365 protected:
 366  virtual void SetUp() {
 367    // Build descriptors for the following definitions:
 368    //
 369    //   // in "foo.proto"
 370    //   message TestForeign {}
 371    //   enum TestEnum {}
 372    //
 373    //   message TestMessage {
 374    //     required string      foo = 1;
 375    //     optional TestEnum    bar = 6;
 376    //     repeated TestForeign baz = 500000000;
 377    //     optional group       qux = 15 {}
 378    //   }
 379    //
 380    //   // in "bar.proto"
 381    //   package corge.grault;
 382    //   message TestMessage2 {
 383    //     required string foo = 1;
 384    //     required string bar = 2;
 385    //     required string quux = 6;
 386    //   }
 387    //
 388    // We cheat and use TestForeign as the type for qux rather than create
 389    // an actual nested type.
 390    //
 391    // Since all primitive types (including string) use the same building
 392    // code, there's no need to test each one individually.
 393    //
 394    // TestMessage2 is primarily here to test FindFieldByName and friends.
 395    // All messages created from the same DescriptorPool share the same lookup
 396    // table, so we need to insure that they don't interfere.
 397
 398    FileDescriptorProto foo_file;
 399    foo_file.set_name("foo.proto");
 400    AddMessage(&foo_file, "TestForeign");
 401    AddEmptyEnum(&foo_file, "TestEnum");
 402
 403    DescriptorProto* message = AddMessage(&foo_file, "TestMessage");
 404    AddField(message, "foo", 1,
 405             FieldDescriptorProto::LABEL_REQUIRED,
 406             FieldDescriptorProto::TYPE_STRING);
 407    AddField(message, "bar", 6,
 408             FieldDescriptorProto::LABEL_OPTIONAL,
 409             FieldDescriptorProto::TYPE_ENUM)
 410      ->set_type_name("TestEnum");
 411    AddField(message, "baz", 500000000,
 412             FieldDescriptorProto::LABEL_REPEATED,
 413             FieldDescriptorProto::TYPE_MESSAGE)
 414      ->set_type_name("TestForeign");
 415    AddField(message, "qux", 15,
 416             FieldDescriptorProto::LABEL_OPTIONAL,
 417             FieldDescriptorProto::TYPE_GROUP)
 418      ->set_type_name("TestForeign");
 419
 420    FileDescriptorProto bar_file;
 421    bar_file.set_name("bar.proto");
 422    bar_file.set_package("corge.grault");
 423
 424    DescriptorProto* message2 = AddMessage(&bar_file, "TestMessage2");
 425    AddField(message2, "foo", 1,
 426             FieldDescriptorProto::LABEL_REQUIRED,
 427             FieldDescriptorProto::TYPE_STRING);
 428    AddField(message2, "bar", 2,
 429             FieldDescriptorProto::LABEL_REQUIRED,
 430             FieldDescriptorProto::TYPE_STRING);
 431    AddField(message2, "quux", 6,
 432             FieldDescriptorProto::LABEL_REQUIRED,
 433             FieldDescriptorProto::TYPE_STRING);
 434
 435    // Build the descriptors and get the pointers.
 436    foo_file_ = pool_.BuildFile(foo_file);
 437    ASSERT_TRUE(foo_file_ != NULL);
 438
 439    bar_file_ = pool_.BuildFile(bar_file);
 440    ASSERT_TRUE(bar_file_ != NULL);
 441
 442    ASSERT_EQ(1, foo_file_->enum_type_count());
 443    enum_ = foo_file_->enum_type(0);
 444
 445    ASSERT_EQ(2, foo_file_->message_type_count());
 446    foreign_ = foo_file_->message_type(0);
 447    message_ = foo_file_->message_type(1);
 448
 449    ASSERT_EQ(4, message_->field_count());
 450    foo_ = message_->field(0);
 451    bar_ = message_->field(1);
 452    baz_ = message_->field(2);
 453    qux_ = message_->field(3);
 454
 455    ASSERT_EQ(1, bar_file_->message_type_count());
 456    message2_ = bar_file_->message_type(0);
 457
 458    ASSERT_EQ(3, message2_->field_count());
 459    foo2_  = message2_->field(0);
 460    bar2_  = message2_->field(1);
 461    quux2_ = message2_->field(2);
 462  }
 463
 464  DescriptorPool pool_;
 465
 466  const FileDescriptor* foo_file_;
 467  const FileDescriptor* bar_file_;
 468
 469  const Descriptor* message_;
 470  const Descriptor* message2_;
 471  const Descriptor* foreign_;
 472  const EnumDescriptor* enum_;
 473
 474  const FieldDescriptor* foo_;
 475  const FieldDescriptor* bar_;
 476  const FieldDescriptor* baz_;
 477  const FieldDescriptor* qux_;
 478
 479  const FieldDescriptor* foo2_;
 480  const FieldDescriptor* bar2_;
 481  const FieldDescriptor* quux2_;
 482};
 483
 484TEST_F(DescriptorTest, Name) {
 485  EXPECT_EQ("TestMessage", message_->name());
 486  EXPECT_EQ("TestMessage", message_->full_name());
 487  EXPECT_EQ(foo_file_, message_->file());
 488
 489  EXPECT_EQ("TestMessage2", message2_->name());
 490  EXPECT_EQ("corge.grault.TestMessage2", message2_->full_name());
 491  EXPECT_EQ(bar_file_, message2_->file());
 492}
 493
 494TEST_F(DescriptorTest, ContainingType) {
 495  EXPECT_TRUE(message_->containing_type() == NULL);
 496  EXPECT_TRUE(message2_->containing_type() == NULL);
 497}
 498
 499TEST_F(DescriptorTest, FieldsByIndex) {
 500  ASSERT_EQ(4, message_->field_count());
 501  EXPECT_EQ(foo_, message_->field(0));
 502  EXPECT_EQ(bar_, message_->field(1));
 503  EXPECT_EQ(baz_, message_->field(2));
 504  EXPECT_EQ(qux_, message_->field(3));
 505}
 506
 507TEST_F(DescriptorTest, FindFieldByName) {
 508  // All messages in the same DescriptorPool share a single lookup table for
 509  // fields.  So, in addition to testing that FindFieldByName finds the fields
 510  // of the message, we need to test that it does *not* find the fields of
 511  // *other* messages.
 512
 513  EXPECT_EQ(foo_, message_->FindFieldByName("foo"));
 514  EXPECT_EQ(bar_, message_->FindFieldByName("bar"));
 515  EXPECT_EQ(baz_, message_->FindFieldByName("baz"));
 516  EXPECT_EQ(qux_, message_->FindFieldByName("qux"));
 517  EXPECT_TRUE(message_->FindFieldByName("no_such_field") == NULL);
 518  EXPECT_TRUE(message_->FindFieldByName("quux") == NULL);
 519
 520  EXPECT_EQ(foo2_ , message2_->FindFieldByName("foo" ));
 521  EXPECT_EQ(bar2_ , message2_->FindFieldByName("bar" ));
 522  EXPECT_EQ(quux2_, message2_->FindFieldByName("quux"));
 523  EXPECT_TRUE(message2_->FindFieldByName("baz") == NULL);
 524  EXPECT_TRUE(message2_->FindFieldByName("qux") == NULL);
 525}
 526
 527TEST_F(DescriptorTest, FindFieldByNumber) {
 528  EXPECT_EQ(foo_, message_->FindFieldByNumber(1));
 529  EXPECT_EQ(bar_, message_->FindFieldByNumber(6));
 530  EXPECT_EQ(baz_, message_->FindFieldByNumber(500000000));
 531  EXPECT_EQ(qux_, message_->FindFieldByNumber(15));
 532  EXPECT_TRUE(message_->FindFieldByNumber(837592) == NULL);
 533  EXPECT_TRUE(message_->FindFieldByNumber(2) == NULL);
 534
 535  EXPECT_EQ(foo2_ , message2_->FindFieldByNumber(1));
 536  EXPECT_EQ(bar2_ , message2_->FindFieldByNumber(2));
 537  EXPECT_EQ(quux2_, message2_->FindFieldByNumber(6));
 538  EXPECT_TRUE(message2_->FindFieldByNumber(15) == NULL);
 539  EXPECT_TRUE(message2_->FindFieldByNumber(500000000) == NULL);
 540}
 541
 542TEST_F(DescriptorTest, FieldName) {
 543  EXPECT_EQ("foo", foo_->name());
 544  EXPECT_EQ("bar", bar_->name());
 545  EXPECT_EQ("baz", baz_->name());
 546  EXPECT_EQ("qux", qux_->name());
 547}
 548
 549TEST_F(DescriptorTest, FieldFullName) {
 550  EXPECT_EQ("TestMessage.foo", foo_->full_name());
 551  EXPECT_EQ("TestMessage.bar", bar_->full_name());
 552  EXPECT_EQ("TestMessage.baz", baz_->full_name());
 553  EXPECT_EQ("TestMessage.qux", qux_->full_name());
 554
 555  EXPECT_EQ("corge.grault.TestMessage2.foo", foo2_->full_name());
 556  EXPECT_EQ("corge.grault.TestMessage2.bar", bar2_->full_name());
 557  EXPECT_EQ("corge.grault.TestMessage2.quux", quux2_->full_name());
 558}
 559
 560TEST_F(DescriptorTest, FieldFile) {
 561  EXPECT_EQ(foo_file_, foo_->file());
 562  EXPECT_EQ(foo_file_, bar_->file());
 563  EXPECT_EQ(foo_file_, baz_->file());
 564  EXPECT_EQ(foo_file_, qux_->file());
 565
 566  EXPECT_EQ(bar_file_, foo2_->file());
 567  EXPECT_EQ(bar_file_, bar2_->file());
 568  EXPECT_EQ(bar_file_, quux2_->file());
 569}
 570
 571TEST_F(DescriptorTest, FieldIndex) {
 572  EXPECT_EQ(0, foo_->index());
 573  EXPECT_EQ(1, bar_->index());
 574  EXPECT_EQ(2, baz_->index());
 575  EXPECT_EQ(3, qux_->index());
 576}
 577
 578TEST_F(DescriptorTest, FieldNumber) {
 579  EXPECT_EQ(        1, foo_->number());
 580  EXPECT_EQ(        6, bar_->number());
 581  EXPECT_EQ(500000000, baz_->number());
 582  EXPECT_EQ(       15, qux_->number());
 583}
 584
 585TEST_F(DescriptorTest, FieldType) {
 586  EXPECT_EQ(FieldDescriptor::TYPE_STRING , foo_->type());
 587  EXPECT_EQ(FieldDescriptor::TYPE_ENUM   , bar_->type());
 588  EXPECT_EQ(FieldDescriptor::TYPE_MESSAGE, baz_->type());
 589  EXPECT_EQ(FieldDescriptor::TYPE_GROUP  , qux_->type());
 590}
 591
 592TEST_F(DescriptorTest, FieldLabel) {
 593  EXPECT_EQ(FieldDescriptor::LABEL_REQUIRED, foo_->label());
 594  EXPECT_EQ(FieldDescriptor::LABEL_OPTIONAL, bar_->label());
 595  EXPECT_EQ(FieldDescriptor::LABEL_REPEATED, baz_->label());
 596  EXPECT_EQ(FieldDescriptor::LABEL_OPTIONAL, qux_->label());
 597
 598  EXPECT_TRUE (foo_->is_required());
 599  EXPECT_FALSE(foo_->is_optional());
 600  EXPECT_FALSE(foo_->is_repeated());
 601
 602  EXPECT_FALSE(bar_->is_required());
 603  EXPECT_TRUE (bar_->is_optional());
 604  EXPECT_FALSE(bar_->is_repeated());
 605
 606  EXPECT_FALSE(baz_->is_required());
 607  EXPECT_FALSE(baz_->is_optional());
 608  EXPECT_TRUE (baz_->is_repeated());
 609}
 610
 611TEST_F(DescriptorTest, FieldHasDefault) {
 612  EXPECT_FALSE(foo_->has_default_value());
 613  EXPECT_FALSE(bar_->has_default_value());
 614  EXPECT_FALSE(baz_->has_default_value());
 615  EXPECT_FALSE(qux_->has_default_value());
 616}
 617
 618TEST_F(DescriptorTest, FieldContainingType) {
 619  EXPECT_EQ(message_, foo_->containing_type());
 620  EXPECT_EQ(message_, bar_->containing_type());
 621  EXPECT_EQ(message_, baz_->containing_type());
 622  EXPECT_EQ(message_, qux_->containing_type());
 623
 624  EXPECT_EQ(message2_, foo2_ ->containing_type());
 625  EXPECT_EQ(message2_, bar2_ ->containing_type());
 626  EXPECT_EQ(message2_, quux2_->containing_type());
 627}
 628
 629TEST_F(DescriptorTest, FieldMessageType) {
 630  EXPECT_TRUE(foo_->message_type() == NULL);
 631  EXPECT_TRUE(bar_->message_type() == NULL);
 632
 633  EXPECT_EQ(foreign_, baz_->message_type());
 634  EXPECT_EQ(foreign_, qux_->message_type());
 635}
 636
 637TEST_F(DescriptorTest, FieldEnumType) {
 638  EXPECT_TRUE(foo_->enum_type() == NULL);
 639  EXPECT_TRUE(baz_->enum_type() == NULL);
 640  EXPECT_TRUE(qux_->enum_type() == NULL);
 641
 642  EXPECT_EQ(enum_, bar_->enum_type());
 643}
 644
 645// ===================================================================
 646
 647class StylizedFieldNamesTest : public testing::Test {
 648 protected:
 649  void SetUp() {
 650    FileDescriptorProto file;
 651    file.set_name("foo.proto");
 652
 653    AddExtensionRange(AddMessage(&file, "ExtendableMessage"), 1, 1000);
 654
 655    DescriptorProto* message = AddMessage(&file, "TestMessage");
 656    AddField(message, "foo_foo", 1,
 657             FieldDescriptorProto::LABEL_OPTIONAL,
 658             FieldDescriptorProto::TYPE_INT32);
 659    AddField(message, "FooBar", 2,
 660             FieldDescriptorProto::LABEL_OPTIONAL,
 661             FieldDescriptorProto::TYPE_INT32);
 662    AddField(message, "fooBaz", 3,
 663             FieldDescriptorProto::LABEL_OPTIONAL,
 664             FieldDescriptorProto::TYPE_INT32);
 665    AddField(message, "fooFoo", 4,  // Camel-case conflict with foo_foo.
 666             FieldDescriptorProto::LABEL_OPTIONAL,
 667             FieldDescriptorProto::TYPE_INT32);
 668    AddField(message, "foobar", 5,  // Lower-case conflict with FooBar.
 669             FieldDescriptorProto::LABEL_OPTIONAL,
 670             FieldDescriptorProto::TYPE_INT32);
 671
 672    AddNestedExtension(message, "ExtendableMessage", "bar_foo", 1,
 673                       FieldDescriptorProto::LABEL_OPTIONAL,
 674                       FieldDescriptorProto::TYPE_INT32);
 675    AddNestedExtension(message, "ExtendableMessage", "BarBar", 2,
 676                       FieldDescriptorProto::LABEL_OPTIONAL,
 677                       FieldDescriptorProto::TYPE_INT32);
 678    AddNestedExtension(message, "ExtendableMessage", "BarBaz", 3,
 679                       FieldDescriptorProto::LABEL_OPTIONAL,
 680                       FieldDescriptorProto::TYPE_INT32);
 681    AddNestedExtension(message, "ExtendableMessage", "barFoo", 4,  // Conflict
 682                       FieldDescriptorProto::LABEL_OPTIONAL,
 683                       FieldDescriptorProto::TYPE_INT32);
 684    AddNestedExtension(message, "ExtendableMessage", "barbar", 5,  // Conflict
 685                       FieldDescriptorProto::LABEL_OPTIONAL,
 686                       FieldDescriptorProto::TYPE_INT32);
 687
 688    AddExtension(&file, "ExtendableMessage", "baz_foo", 11,
 689                 FieldDescriptorProto::LABEL_OPTIONAL,
 690                 FieldDescriptorProto::TYPE_INT32);
 691    AddExtension(&file, "ExtendableMessage", "BazBar", 12,
 692                 FieldDescriptorProto::LABEL_OPTIONAL,
 693                 FieldDescriptorProto::TYPE_INT32);
 694    AddExtension(&file, "ExtendableMessage", "BazBaz", 13,
 695                 FieldDescriptorProto::LABEL_OPTIONAL,
 696                 FieldDescriptorProto::TYPE_INT32);
 697    AddExtension(&file, "ExtendableMessage", "bazFoo", 14,  // Conflict
 698                 FieldDescriptorProto::LABEL_OPTIONAL,
 699                 FieldDescriptorProto::TYPE_INT32);
 700    AddExtension(&file, "ExtendableMessage", "bazbar", 15,  // Conflict
 701                 FieldDescriptorProto::LABEL_OPTIONAL,
 702                 FieldDescriptorProto::TYPE_INT32);
 703
 704    file_ = pool_.BuildFile(file);
 705    ASSERT_TRUE(file_ != NULL);
 706    ASSERT_EQ(2, file_->message_type_count());
 707    message_ = file_->message_type(1);
 708    ASSERT_EQ("TestMessage", message_->name());
 709    ASSERT_EQ(5, message_->field_count());
 710    ASSERT_EQ(5, message_->extension_count());
 711    ASSERT_EQ(5, file_->extension_count());
 712  }
 713
 714  DescriptorPool pool_;
 715  const FileDescriptor* file_;
 716  const Descriptor* message_;
 717};
 718
 719TEST_F(StylizedFieldNamesTest, LowercaseName) {
 720  EXPECT_EQ("foo_foo", message_->field(0)->lowercase_name());
 721  EXPECT_EQ("foobar" , message_->field(1)->lowercase_name());
 722  EXPECT_EQ("foobaz" , message_->field(2)->lowercase_name());
 723  EXPECT_EQ("foofoo" , message_->field(3)->lowercase_name());
 724  EXPECT_EQ("foobar" , message_->field(4)->lowercase_name());
 725
 726  EXPECT_EQ("bar_foo", message_->extension(0)->lowercase_name());
 727  EXPECT_EQ("barbar" , message_->extension(1)->lowercase_name());
 728  EXPECT_EQ("barbaz" , message_->extension(2)->lowercase_name());
 729  EXPECT_EQ("barfoo" , message_->extension(3)->lowercase_name());
 730  EXPECT_EQ("barbar" , message_->extension(4)->lowercase_name());
 731
 732  EXPECT_EQ("baz_foo", file_->extension(0)->lowercase_name());
 733  EXPECT_EQ("bazbar" , file_->extension(1)->lowercase_name());
 734  EXPECT_EQ("bazbaz" , file_->extension(2)->lowercase_name());
 735  EXPECT_EQ("bazfoo" , file_->extension(3)->lowercase_name());
 736  EXPECT_EQ("bazbar" , file_->extension(4)->lowercase_name());
 737}
 738
 739TEST_F(StylizedFieldNamesTest, CamelcaseName) {
 740  EXPECT_EQ("fooFoo", message_->field(0)->camelcase_name());
 741  EXPECT_EQ("fooBar", message_->field(1)->camelcase_name());
 742  EXPECT_EQ("fooBaz", message_->field(2)->camelcase_name());
 743  EXPECT_EQ("fooFoo", message_->field(3)->camelcase_name());
 744  EXPECT_EQ("foobar", message_->field(4)->camelcase_name());
 745
 746  EXPECT_EQ("barFoo", message_->extension(0)->camelcase_name());
 747  EXPECT_EQ("barBar", message_->extension(1)->camelcase_name());
 748  EXPECT_EQ("barBaz", message_->extension(2)->camelcase_name());
 749  EXPECT_EQ("barFoo", message_->extension(3)->camelcase_name());
 750  EXPECT_EQ("barbar", message_->extension(4)->camelcase_name());
 751
 752  EXPECT_EQ("bazFoo", file_->extension(0)->camelcase_name());
 753  EXPECT_EQ("bazBar", file_->extension(1)->camelcase_name());
 754  EXPECT_EQ("bazBaz", file_->extension(2)->camelcase_name());
 755  EXPECT_EQ("bazFoo", file_->extension(3)->camelcase_name());
 756  EXPECT_EQ("bazbar", file_->extension(4)->camelcase_name());
 757}
 758
 759TEST_F(StylizedFieldNamesTest, FindByLowercaseName) {
 760  EXPECT_EQ(message_->field(0),
 761            message_->FindFieldByLowercaseName("foo_foo"));
 762  EXPECT_EQ(message_->field(1),
 763            message_->FindFieldByLowercaseName("foobar"));
 764  EXPECT_EQ(message_->field(2),
 765            message_->FindFieldByLowercaseName("foobaz"));
 766  EXPECT_TRUE(message_->FindFieldByLowercaseName("FooBar") == NULL);
 767  EXPECT_TRUE(message_->FindFieldByLowercaseName("fooBaz") == NULL);
 768  EXPECT_TRUE(message_->FindFieldByLowercaseName("bar_foo") == NULL);
 769  EXPECT_TRUE(message_->FindFieldByLowercaseName("nosuchfield") == NULL);
 770
 771  EXPECT_EQ(message_->extension(0),
 772            message_->FindExtensionByLowercaseName("bar_foo"));
 773  EXPECT_EQ(message_->extension(1),
 774            message_->FindExtensionByLowercaseName("barbar"));
 775  EXPECT_EQ(message_->extension(2),
 776            message_->FindExtensionByLowercaseName("barbaz"));
 777  EXPECT_TRUE(message_->FindExtensionByLowercaseName("BarBar") == NULL);
 778  EXPECT_TRUE(message_->FindExtensionByLowercaseName("barBaz") == NULL);
 779  EXPECT_TRUE(message_->FindExtensionByLowercaseName("foo_foo") == NULL);
 780  EXPECT_TRUE(message_->FindExtensionByLowercaseName("nosuchfield") == NULL);
 781
 782  EXPECT_EQ(file_->extension(0),
 783            file_->FindExtensionByLowercaseName("baz_foo"));
 784  EXPECT_EQ(file_->extension(1),
 785            file_->FindExtensionByLowercaseName("bazbar"));
 786  EXPECT_EQ(file_->extension(2),
 787            file_->FindExtensionByLowercaseName("bazbaz"));
 788  EXPECT_TRUE(file_->FindExtensionByLowercaseName("BazBar") == NULL);
 789  EXPECT_TRUE(file_->FindExtensionByLowercaseName("bazBaz") == NULL);
 790  EXPECT_TRUE(file_->FindExtensionByLowercaseName("nosuchfield") == NULL);
 791}
 792
 793TEST_F(StylizedFieldNamesTest, FindByCamelcaseName) {
 794  EXPECT_EQ(message_->field(0),
 795            message_->FindFieldByCamelcaseName("fooFoo"));
 796  EXPECT_EQ(message_->field(1),
 797            message_->FindFieldByCamelcaseName("fooBar"));
 798  EXPECT_EQ(message_->field(2),
 799            message_->FindFieldByCamelcaseName("fooBaz"));
 800  EXPECT_TRUE(message_->FindFieldByCamelcaseName("foo_foo") == NULL);
 801  EXPECT_TRUE(message_->FindFieldByCamelcaseName("FooBar") == NULL);
 802  EXPECT_TRUE(message_->FindFieldByCamelcaseName("barFoo") == NULL);
 803  EXPECT_TRUE(message_->FindFieldByCamelcaseName("nosuchfield") == NULL);
 804
 805  EXPECT_EQ(message_->extension(0),
 806            message_->FindExtensionByCamelcaseName("barFoo"));
 807  EXPECT_EQ(message_->extension(1),
 808            message_->FindExtensionByCamelcaseName("barBar"));
 809  EXPECT_EQ(message_->extension(2),
 810            message_->FindExtensionByCamelcaseName("barBaz"));
 811  EXPECT_TRUE(message_->FindExtensionByCamelcaseName("bar_foo") == NULL);
 812  EXPECT_TRUE(message_->FindExtensionByCamelcaseName("BarBar") == NULL);
 813  EXPECT_TRUE(message_->FindExtensionByCamelcaseName("fooFoo") == NULL);
 814  EXPECT_TRUE(message_->FindExtensionByCamelcaseName("nosuchfield") == NULL);
 815
 816  EXPECT_EQ(file_->extension(0),
 817            file_->FindExtensionByCamelcaseName("bazFoo"));
 818  EXPECT_EQ(file_->extension(1),
 819            file_->FindExtensionByCamelcaseName("bazBar"));
 820  EXPECT_EQ(file_->extension(2),
 821            file_->FindExtensionByCamelcaseName("bazBaz"));
 822  EXPECT_TRUE(file_->FindExtensionByCamelcaseName("baz_foo") == NULL);
 823  EXPECT_TRUE(file_->FindExtensionByCamelcaseName("BazBar") == NULL);
 824  EXPECT_TRUE(file_->FindExtensionByCamelcaseName("nosuchfield") == NULL);
 825}
 826
 827// ===================================================================
 828
 829// Test enum descriptors.
 830class EnumDescriptorTest : public testing::Test {
 831 protected:
 832  virtual void SetUp() {
 833    // Build descriptors for the following definitions:
 834    //
 835    //   // in "foo.proto"
 836    //   enum TestEnum {
 837    //     FOO = 1;
 838    //     BAR = 2;
 839    //   }
 840    //
 841    //   // in "bar.proto"
 842    //   package corge.grault;
 843    //   enum TestEnum2 {
 844    //     FOO = 1;
 845    //     BAZ = 3;
 846    //   }
 847    //
 848    // TestEnum2 is primarily here to test FindValueByName and friends.
 849    // All enums created from the same DescriptorPool share the same lookup
 850    // table, so we need to insure that they don't interfere.
 851
 852    // TestEnum
 853    FileDescriptorProto foo_file;
 854    foo_file.set_name("foo.proto");
 855
 856    EnumDescriptorProto* enum_proto = AddEnum(&foo_file, "TestEnum");
 857    AddEnumValue(enum_proto, "FOO", 1);
 858    AddEnumValue(enum_proto, "BAR", 2);
 859
 860    // TestEnum2
 861    FileDescriptorProto bar_file;
 862    bar_file.set_name("bar.proto");
 863    bar_file.set_package("corge.grault");
 864
 865    EnumDescriptorProto* enum2_proto = AddEnum(&bar_file, "TestEnum2");
 866    AddEnumValue(enum2_proto, "FOO", 1);
 867    AddEnumValue(enum2_proto, "BAZ", 3);
 868
 869    // Build the descriptors and get the pointers.
 870    foo_file_ = pool_.BuildFile(foo_file);
 871    ASSERT_TRUE(foo_file_ != NULL);
 872
 873    bar_file_ = pool_.BuildFile(bar_file);
 874    ASSERT_TRUE(bar_file_ != NULL);
 875
 876    ASSERT_EQ(1, foo_file_->enum_type_count());
 877    enum_ = foo_file_->enum_type(0);
 878
 879    ASSERT_EQ(2, enum_->value_count());
 880    foo_ = enum_->value(0);
 881    bar_ = enum_->value(1);
 882
 883    ASSERT_EQ(1, bar_file_->enum_type_count());
 884    enum2_ = bar_file_->enum_type(0);
 885
 886    ASSERT_EQ(2, enum2_->value_count());
 887    foo2_ = enum2_->value(0);
 888    baz2_ = enum2_->value(1);
 889  }
 890
 891  DescriptorPool pool_;
 892
 893  const FileDescriptor* foo_file_;
 894  const FileDescriptor* bar_file_;
 895
 896  const EnumDescriptor* enum_;
 897  const EnumDescriptor* enum2_;
 898
 899  const EnumValueDescriptor* foo_;
 900  const EnumValueDescriptor* bar_;
 901
 902  const EnumValueDescriptor* foo2_;
 903  const EnumValueDescriptor* baz2_;
 904};
 905
 906TEST_F(EnumDescriptorTest, Name) {
 907  EXPECT_EQ("TestEnum", enum_->name());
 908  EXPECT_EQ("TestEnum", enum_->full_name());
 909  EXPECT_EQ(foo_file_, enum_->file());
 910
 911  EXPECT_EQ("TestEnum2", enum2_->name());
 912  EXPECT_EQ("corge.grault.TestEnum2", enum2_->full_name());
 913  EXPECT_EQ(bar_file_, enum2_->file());
 914}
 915
 916TEST_F(EnumDescriptorTest, ContainingType) {
 917  EXPECT_TRUE(enum_->containing_type() == NULL);
 918  EXPECT_TRUE(enum2_->containing_type() == NULL);
 919}
 920
 921TEST_F(EnumDescriptorTest, ValuesByIndex) {
 922  ASSERT_EQ(2, enum_->value_count());
 923  EXPECT_EQ(foo_, enum_->value(0));
 924  EXPECT_EQ(bar_, enum_->value(1));
 925}
 926
 927TEST_F(EnumDescriptorTest, FindValueByName) {
 928  EXPECT_EQ(foo_ , enum_ ->FindValueByName("FOO"));
 929  EXPECT_EQ(bar_ , enum_ ->FindValueByName("BAR"));
 930  EXPECT_EQ(foo2_, enum2_->FindValueByName("FOO"));
 931  EXPECT_EQ(baz2_, enum2_->FindValueByName("BAZ"));
 932
 933  EXPECT_TRUE(enum_ ->FindValueByName("NO_SUCH_VALUE") == NULL);
 934  EXPECT_TRUE(enum_ ->FindValueByName("BAZ"          ) == NULL);
 935  EXPECT_TRUE(enum2_->FindValueByName("BAR"          ) == NULL);
 936}
 937
 938TEST_F(EnumDescriptorTest, FindValueByNumber) {
 939  EXPECT_EQ(foo_ , enum_ ->FindValueByNumber(1));
 940  EXPECT_EQ(bar_ , enum_ ->FindValueByNumber(2));
 941  EXPECT_EQ(foo2_, enum2_->FindValueByNumber(1));
 942  EXPECT_EQ(baz2_, enum2_->FindValueByNumber(3));
 943
 944  EXPECT_TRUE(enum_ ->FindValueByNumber(416) == NULL);
 945  EXPECT_TRUE(enum_ ->FindValueByNumber(3) == NULL);
 946  EXPECT_TRUE(enum2_->FindValueByNumber(2) == NULL);
 947}
 948
 949TEST_F(EnumDescriptorTest, ValueName) {
 950  EXPECT_EQ("FOO", foo_->name());
 951  EXPECT_EQ("BAR", bar_->name());
 952}
 953
 954TEST_F(EnumDescriptorTest, ValueFullName) {
 955  EXPECT_EQ("FOO", foo_->full_name());
 956  EXPECT_EQ("BAR", bar_->full_name());
 957  EXPECT_EQ("corge.grault.FOO", foo2_->full_name());
 958  EXPECT_EQ("corge.grault.BAZ", baz2_->full_name());
 959}
 960
 961TEST_F(EnumDescriptorTest, ValueIndex) {
 962  EXPECT_EQ(0, foo_->index());
 963  EXPECT_EQ(1, bar_->index());
 964}
 965
 966TEST_F(EnumDescriptorTest, ValueNumber) {
 967  EXPECT_EQ(1, foo_->number());
 968  EXPECT_EQ(2, bar_->number());
 969}
 970
 971TEST_F(EnumDescriptorTest, ValueType) {
 972  EXPECT_EQ(enum_ , foo_ ->type());
 973  EXPECT_EQ(enum_ , bar_ ->type());
 974  EXPECT_EQ(enum2_, foo2_->type());
 975  EXPECT_EQ(enum2_, baz2_->type());
 976}
 977
 978// ===================================================================
 979
 980// Test service descriptors.
 981class ServiceDescriptorTest : public testing::Test {
 982 protected:
 983  virtual void SetUp() {
 984    // Build descriptors for the following messages and service:
 985    //    // in "foo.proto"
 986    //    message FooRequest  {}
 987    //    message FooResponse {}
 988    //    message BarRequest  {}
 989    //    message BarResponse {}
 990    //    message BazRequest  {}
 991    //    message BazResponse {}
 992    //
 993    //    service TestService {
 994    //      rpc Foo(FooRequest) returns (FooResponse);
 995    //      rpc Bar(BarRequest) returns (BarResponse);
 996    //    }
 997    //
 998    //    // in "bar.proto"
 999    //    package corge.grault
1000    //    service TestService2 {
1001    //      rpc Foo(FooRequest) returns (FooResponse);
1002    //      rpc Baz(BazRequest) returns (BazResponse);
1003    //    }
1004
1005    FileDescriptorProto foo_file;
1006    foo_file.set_name("foo.proto");
1007
1008    AddMessage(&foo_file, "FooRequest");
1009    AddMessage(&foo_file, "FooResponse");
1010    AddMessage(&foo_file, "BarRequest");
1011    AddMessage(&foo_file, "BarResponse");
1012    AddMessage(&foo_file, "BazRequest");
1013    AddMessage(&foo_file, "BazResponse");
1014
1015    ServiceDescriptorProto* service = AddService(&foo_file, "TestService");
1016    AddMethod(service, "Foo", "FooRequest", "FooResponse");
1017    AddMethod(service, "Bar", "BarRequest", "BarResponse");
1018
1019    FileDescriptorProto bar_file;
1020    bar_file.set_name("bar.proto");
1021    bar_file.set_package("corge.grault");
1022    bar_file.add_dependency("foo.proto");
1023
1024    ServiceDescriptorProto* service2 = AddService(&bar_file, "TestService2");
1025    AddMethod(service2, "Foo", "FooRequest", "FooResponse");
1026    AddMethod(service2, "Baz", "BazRequest", "BazResponse");
1027
1028    // Build the descriptors and get the pointers.
1029    foo_file_ = pool_.BuildFile(foo_file);
1030    ASSERT_TRUE(foo_file_ != NULL);
1031
1032    bar_file_ = pool_.BuildFile(bar_file);
1033    ASSERT_TRUE(bar_file_ != NULL);
1034
1035    ASSERT_EQ(6, foo_file_->message_type_count());
1036    foo_request_  = foo_file_->message_type(0);
1037    foo_response_ = foo_file_->message_type(1);
1038    bar_request_  = foo_file_->message_type(2);
1039    bar_response_ = foo_file_->message_type(3);
1040    baz_request_  = foo_file_->message_type(4);
1041    baz_response_ = foo_file_->message_type(5);
1042
1043    ASSERT_EQ(1, foo_file_->service_count());
1044    service_ = foo_file_->service(0);
1045
1046    ASSERT_EQ(2, service_->method_count());
1047    foo_ = service_->method(0);
1048    bar_ = service_->method(1);
1049
1050    ASSERT_EQ(1, bar_file_->service_count());
1051    service2_ = bar_file_->service(0);
1052
1053    ASSERT_EQ(2, service2_->method_count());
1054    foo2_ = service2_->method(0);
1055    baz2_ = service2_->method(1);
1056  }
1057
1058  DescriptorPool pool_;
1059
1060  const FileDescriptor* foo_file_;
1061  const FileDescriptor* bar_file_;
1062
1063  const Descriptor* foo_request_;
1064  const Descriptor* foo_response_;
1065  const Descriptor* bar_request_;
1066  const Descriptor* bar_response_;
1067  const Descriptor* baz_request_;
1068  const Descriptor* baz_response_;
1069
1070  const ServiceDescriptor* service_;
1071  const ServiceDescriptor* service2_;
1072
1073  const MethodDescriptor* foo_;
1074  const MethodDescriptor* bar_;
1075
1076  const MethodDescriptor* foo2_;
1077  const MethodDescriptor* baz2_;
1078};
1079
1080TEST_F(ServiceDescriptorTest, Name) {
1081  EXPECT_EQ("TestService", service_->name());
1082  EXPECT_EQ("TestService", service_->full_name());
1083  EXPECT_EQ(foo_file_, service_->file());
1084
1085  EXPECT_EQ("TestService2", service2_->name());
1086  EXPECT_EQ("corge.grault.TestService2", service2_->full_name());
1087  EXPECT_EQ(bar_file_, service2_->file());
1088}
1089
1090TEST_F(ServiceDescriptorTest, MethodsByIndex) {
1091  ASSERT_EQ(2, service_->method_count());
1092  EXPECT_EQ(foo_, service_->method(0));
1093  EXPECT_EQ(bar_, service_->method(1));
1094}
1095
1096TEST_F(ServiceDescriptorTest, FindMethodByName) {
1097  EXPECT_EQ(foo_ , service_ ->FindMethodByName("Foo"));
1098  EXPECT_EQ(bar_ , service_ ->FindMethodByName("Bar"));
1099  EXPECT_EQ(foo2_, service2_->FindMethodByName("Foo"));
1100  EXPECT_EQ(baz2_, service2_->FindMethodByName("Baz"));
1101
1102  EXPECT_TRUE(service_ ->FindMethodByName("NoSuchMethod") == NULL);
1103  EXPECT_TRUE(service_ ->FindMethodByName("Baz"         ) == NULL);
1104  EXPECT_TRUE(service2_->FindMethodByName("Bar"         ) == NULL);
1105}
1106
1107TEST_F(ServiceDescriptorTest, MethodName) {
1108  EXPECT_EQ("Foo", foo_->name());
1109  EXPECT_EQ("Bar", bar_->name());
1110}
1111
1112TEST_F(ServiceDescriptorTest, MethodFullName) {
1113  EXPECT_EQ("TestService.Foo", foo_->full_name());
1114  EXPECT_EQ("TestService.Bar", bar_->full_name());
1115  EXPECT_EQ("corge.grault.TestService2.Foo", foo2_->full_name());
1116  EXPECT_EQ("corge.grault.TestService2.Baz", baz2_->full_name());
1117}
1118
1119TEST_F(ServiceDescriptorTest, MethodIndex) {
1120  EXPECT_EQ(0, foo_->index());
1121  EXPECT_EQ(1, bar_->index());
1122}
1123
1124TEST_F(ServiceDescriptorTest, MethodParent) {
1125  EXPECT_EQ(service_, foo_->service());
1126  EXPECT_EQ(service_, bar_->service());
1127}
1128
1129TEST_F(ServiceDescriptorTest, MethodInputType) {
1130  EXPECT_EQ(foo_request_, foo_->input_type());
1131  EXPECT_EQ(bar_request_, bar_->input_type());
1132}
1133
1134TEST_F(ServiceDescriptorTest, MethodOutputType) {
1135  EXPECT_EQ(foo_response_, foo_->output_type());
1136  EXPECT_EQ(bar_response_, bar_->output_type());
1137}
1138
1139// ===================================================================
1140
1141// Test nested types.
1142class NestedDescriptorTest : public testing::Test {
1143 protected:
1144  virtual void SetUp() {
1145    // Build descriptors for the following definitions:
1146    //
1147    //   // in "foo.proto"
1148    //   message TestMessage {
1149    //     message Foo {}
1150    //     message Bar {}
1151    //     enum Baz { A = 1; }
1152    //     enum Qux { B = 1; }
1153    //   }
1154    //
1155    //   // in "bar.proto"
1156    //   package corge.grault;
1157    //   message TestMessage2 {
1158    //     message Foo {}
1159    //     message Baz {}
1160    //     enum Qux  { A = 1; }
1161    //     enum Quux { C = 1; }
1162    //   }
1163    //
1164    // TestMessage2 is primarily here to test FindNestedTypeByName and friends.
1165    // All messages created from the same DescriptorPool share the same lookup
1166    // table, so we need to insure that they don't interfere.
1167    //
1168    // We add enum values to the enums in order to test searching for enum
1169    // values across a message's scope.
1170
1171    FileDescriptorProto foo_file;
1172    foo_file.set_name("foo.proto");
1173
1174    DescriptorProto* message = AddMessage(&foo_file, "TestMessage");
1175    AddNestedMessage(message, "Foo");
1176    AddNestedMessage(message, "Bar");
1177    EnumDescriptorProto* baz = AddNestedEnum(message, "Baz");
1178    AddEnumValue(baz, "A", 1);
1179    EnumDescriptorProto* qux = AddNestedEnum(message, "Qux");
1180    AddEnumValue(qux, "B", 1);
1181
1182    FileDescriptorProto bar_file;
1183    bar_file.set_name("bar.proto");
1184    bar_file.set_package("corge.grault");
1185
1186    DescriptorProto* message2 = AddMessage(&bar_file, "TestMessage2");
1187    AddNestedMessage(message2, "Foo");
1188    AddNestedMessage(message2, "Baz");
1189    EnumDescriptorProto* qux2 = AddNestedEnum(message2, "Qux");
1190    AddEnumValue(qux2, "A", 1);
1191    EnumDescriptorProto* quux2 = AddNestedEnum(message2, "Quux");
1192    AddEnumValue(quux2, "C", 1);
1193
1194    // Build the descriptors and get the pointers.
1195    foo_file_ = pool_.BuildFile(foo_file);
1196    ASSERT_TRUE(foo_file_ != NULL);
1197
1198    bar_file_ = pool_.BuildFile(bar_file);
1199    ASSERT_TRUE(bar_file_ != NULL);
1200
1201    ASSERT_EQ(1, foo_file_->message_type_count());
1202    message_ = foo_file_->message_type(0);
1203
1204    ASSERT_EQ(2, message_->nested_type_count());
1205    foo_ = message_->nested_type(0);
1206    bar_ = message_->nested_type(1);
1207
1208    ASSERT_EQ(2, message_->enum_type_count());
1209    baz_ = message_->enum_type(0);
1210    qux_ = message_->enum_type(1);
1211
1212    ASSERT_EQ(1, baz_->value_count());
1213    a_ = baz_->value(0);
1214    ASSERT_EQ(1, qux_->value_count());
1215    b_ = qux_->value(0);
1216
1217    ASSERT_EQ(1, bar_file_->message_type_count());
1218    message2_ = bar_file_->message_type(0);
1219
1220    ASSERT_EQ(2, message2_->nested_type_count());
1221    foo2_ = message2_->nested_type(0);
1222    baz2_ = message2_->nested_type(1);
1223
1224    ASSERT_EQ(2, message2_->enum_type_count());
1225    qux2_ = message2_->enum_type(0);
1226    quux2_ = message2_->enum_type(1);
1227
1228    ASSERT_EQ(1, qux2_->value_count());
1229    a2_ = qux2_->value(0);
1230    ASSERT_EQ(1, quux2_->value_count());
1231    c2_ = quux2_->value(0);
1232  }
1233
1234  DescriptorPool pool_;
1235
1236  const FileDescriptor* foo_file_;
1237  const FileDescriptor* bar_file_;
1238
1239  const Descriptor* message_;
1240  const Descriptor* message2_;
1241
1242  const Descriptor* foo_;
1243  const Descriptor* bar_;
1244  const EnumDescriptor* baz_;
1245  const EnumDescriptor* qux_;
1246  const EnumValueDescriptor* a_;
1247  const EnumValueDescriptor* b_;
1248
1249  const Descriptor* foo2_;
1250  const Descriptor* baz2_;
1251  const EnumDescriptor* qux2_;
1252  const EnumDescriptor* quux2_;
1253  const EnumValueDescriptor* a2_;
1254  const EnumValueDescriptor* c2_;
1255};
1256
1257TEST_F(NestedDescriptorTest, MessageName) {
1258  EXPECT_EQ("Foo", foo_ ->name());
1259  EXPECT_EQ("Bar", bar_ ->name());
1260  EXPECT_EQ("Foo", foo2_->name());
1261  EXPECT_EQ("Baz", baz2_->name());
1262
1263  EXPECT_EQ("TestMessage.Foo", foo_->full_name());
1264  EXPECT_EQ("TestMessage.Bar", bar_->full_name());
1265  EXPECT_EQ("corge.grault.TestMessage2.Foo", foo2_->full_name());
1266  EXPECT_EQ("corge.grault.TestMessage2.Baz", baz2_->full_name());
1267}
1268
1269TEST_F(NestedDescriptorTest, MessageContainingType) {
1270  EXPECT_EQ(message_ , foo_ ->containing_type());
1271  EXPECT_EQ(message_ , bar_ ->containing_type());
1272  EXPECT_EQ(message2_, foo2_->containing_type());
1273  EXPECT_EQ(message2_, baz2_->containing_type());
1274}
1275
1276TEST_F(NestedDescriptorTest, NestedMessagesByIndex) {
1277  ASSERT_EQ(2, message_->nested_type_count());
1278  EXPECT_EQ(foo_, message_->nested_type(0));
1279  EXPECT_EQ(bar_, message_->nested_type(1));
1280}
1281
1282TEST_F(NestedDescriptorTest, FindFieldByNameDoesntFindNestedTypes) {
1283  EXPECT_TRUE(message_->FindFieldByName("Foo") == NULL);
1284  EXPECT_TRUE(message_->FindFieldByName("Qux") == NULL);
1285  EXPECT_TRUE(message_->FindExtensionByName("Foo") == NULL);
1286  EXPECT_TRUE(message_->FindExtensionByName("Qux") == NULL);
1287}
1288
1289TEST_F(NestedDescriptorTest, FindNestedTypeByName) {
1290  EXPECT_EQ(foo_ , message_ ->FindNestedTypeByName("Foo"));
1291  EXPECT_EQ(bar_ , message_ ->FindNestedTypeByName("Bar"));
1292  EXPECT_EQ(foo2_, message2_->FindNestedTypeByName("Foo"));
1293  EXPECT_EQ(baz2_, message2_->FindNestedTypeByName("Baz"));
1294
1295  EXPECT_TRUE(message_ ->FindNestedTypeByName("NoSuchType") == NULL);
1296  EXPECT_TRUE(message_ ->FindNestedTypeByName("Baz"       ) == NULL);
1297  EXPECT_TRUE(message2_->FindNestedTypeByName("Bar"       ) == NULL);
1298
1299  EXPECT_TRUE(message_->FindNestedTypeByName("Qux") == NULL);
1300}
1301
1302TEST_F(NestedDescriptorTest, EnumName) {
1303  EXPECT_EQ("Baz" , baz_ ->name());
1304  EXPECT_EQ("Qux" , qux_ ->name());
1305  EXPECT_EQ("Qux" , qux2_->name());
1306  EXPECT_EQ("Quux", quux2_->name());
1307
1308  EXPECT_EQ("TestMessage.Baz", baz_->full_name());
1309  EXPECT_EQ("TestMessage.Qux", qux_->full_name());
1310  EXPECT_EQ("corge.grault.TestMessage2.Qux" , qux2_ ->full_name());
1311  EXPECT_EQ("corge.grault.TestMessage2.Quux", quux2_->full_name());
1312}
1313
1314TEST_F(NestedDescriptorTest, EnumContainingType) {
1315  EXPECT_EQ(message_ , baz_  ->containing_type());
1316  EXPECT_EQ(message_ , qux_  ->containing_type());
1317  EXPECT_EQ(message2_, qux2_ ->containing_type());
1318  EXPECT_EQ(message2_, quux2_->containing_type());
1319}
1320
1321TEST_F(NestedDescriptorTest, NestedEnumsByIndex) {
1322  ASSERT_EQ(2, message_->nested_type_count());
1323  EXPECT_EQ(foo_, message_->nested_type(0));
1324  EXPECT_EQ(bar_, message_->nested_type(1));
1325}
1326
1327TEST_F(NestedDescriptorTest, FindEnumTypeByName) {
1328  EXPECT_EQ(baz_  , message_ ->FindEnumTypeByName("Baz" ));
1329  EXPECT_EQ(qux_  , message_ ->FindEnumTypeByName("Qux" ));
1330  EXPECT_EQ(qux2_ , message2_->FindEnumTypeByName("Qux" ));
1331  EXPECT_EQ(quux2_, message2_->FindEnumTypeByName("Quux"));
1332
1333  EXPECT_TRUE(message_ ->FindEnumTypeByName("NoSuchType") == NULL);
1334  EXPECT_TRUE(message_ ->FindEnumTypeByName("Quux"      ) == NULL);
1335  EXPECT_TRUE(message2_->FindEnumTypeByName("Baz"       ) == NULL);
1336
1337  EXPECT_TRUE(message_->FindEnumTypeByName("Foo") == NULL);
1338}
1339
1340TEST_F(NestedDescriptorTest, FindEnumValueByName) {
1341  EXPECT_EQ(a_ , message_ ->FindEnumValueByName("A"));
1342  EXPECT_EQ(b_ , message_ ->FindEnumValueByName("B"));
1343  EXPECT_EQ(a2_, message2_->FindEnumValueByName("A"));
1344  EXPECT_EQ(c2_, message2_->FindEnumValueByName("C"));
1345
1346  EXPECT_TRUE(message_ ->FindEnumValueByName("NO_SUCH_VALUE") == NULL);
1347  EXPECT_TRUE(message_ ->FindEnumValueByName("C"            ) == NULL);
1348  EXPECT_TRUE(message2_->FindEnumValueByName("B"            ) == NULL);
1349
1350  EXPECT_TRUE(message_->FindEnumValueByName("Foo") == NULL);
1351}
1352
1353// ===================================================================
1354
1355// Test extensions.
1356class ExtensionDescriptorTest : public testing::Test {
1357 protected:
1358  virtual void SetUp() {
1359    // Build descriptors for the following definitions:
1360    //
1361    //   enum Baz {}
1362    //   message Qux {}
1363    //
1364    //   message Foo {
1365    //     extensions 10 to 19;
1366    //     extensions 30 to 39;
1367    //   }
1368    //   extends Foo with optional int32 foo_int32 = 10;
1369    //   extends Foo with repeated TestEnum foo_enum = 19;
1370    //   message Bar {
1371    //     extends Foo with optional Qux foo_message = 30;
1372    //     // (using Qux as the group type)
1373    //     extends Foo with repeated group foo_group = 39;
1374    //   }
1375
1376    FileDescriptorProto foo_file;
1377    foo_file.set_name("foo.proto");
1378
1379    AddEmptyEnum(&foo_file, "Baz");
1380    AddMessage(&foo_file, "Qux");
1381
1382    DescriptorProto* foo = AddMessage(&foo_file, "Foo");
1383    AddExtensionRange(foo, 10, 20);
1384    AddExtension

Large files files are truncated, but you can click here to view the full file