56 const char* file_text) {
67 ADD_FAILURE() <<
"\"" << proto.
name() <<
"\" did not contain expected type \""
68 << type_name <<
"\".";
73 #if GTEST_HAS_PARAM_TEST
82 class DescriptorDatabaseTestCase {
84 virtual ~DescriptorDatabaseTestCase() {}
86 virtual DescriptorDatabase* GetDatabase() = 0;
91 typedef DescriptorDatabaseTestCase* DescriptorDatabaseTestCaseFactory();
94 class SimpleDescriptorDatabaseTestCase :
public DescriptorDatabaseTestCase {
96 static DescriptorDatabaseTestCase*
New() {
97 return new SimpleDescriptorDatabaseTestCase;
100 virtual ~SimpleDescriptorDatabaseTestCase() {}
102 virtual DescriptorDatabase* GetDatabase() {
return &database_; }
104 return database_.Add(file);
108 SimpleDescriptorDatabase database_;
112 class EncodedDescriptorDatabaseTestCase :
public DescriptorDatabaseTestCase {
114 static DescriptorDatabaseTestCase*
New() {
115 return new EncodedDescriptorDatabaseTestCase;
118 virtual ~EncodedDescriptorDatabaseTestCase() {}
120 virtual DescriptorDatabase* GetDatabase() {
return &database_; }
123 file.SerializeToString(&
data);
124 return database_.AddCopy(
data.data(),
data.size());
128 EncodedDescriptorDatabase database_;
132 class DescriptorPoolDatabaseTestCase :
public DescriptorDatabaseTestCase {
134 static DescriptorDatabaseTestCase*
New() {
135 return new EncodedDescriptorDatabaseTestCase;
138 DescriptorPoolDatabaseTestCase() : database_(
pool_) {}
139 virtual ~DescriptorPoolDatabaseTestCase() {}
141 virtual DescriptorDatabase* GetDatabase() {
return &database_; }
143 return pool_.BuildFile(file);
148 DescriptorPoolDatabase database_;
153 class DescriptorDatabaseTest
156 virtual void SetUp() {
157 test_case_.reset(GetParam()());
158 database_ = test_case_->GetDatabase();
164 EXPECT_TRUE(test_case_->AddToDatabase(file_proto));
167 void AddToDatabaseWithError(
const char* file_descriptor_text) {
173 std::unique_ptr<DescriptorDatabaseTestCase> test_case_;
174 DescriptorDatabase* database_;
179 "name: \"foo.proto\" "
180 "message_type { name:\"Foo\" }");
182 "name: \"bar.proto\" "
183 "message_type { name:\"Bar\" }");
187 EXPECT_TRUE(database_->FindFileByName(
"foo.proto", &file));
189 ExpectContainsType(file,
"Foo");
194 EXPECT_TRUE(database_->FindFileByName(
"bar.proto", &file));
196 ExpectContainsType(file,
"Bar");
202 EXPECT_FALSE(database_->FindFileByName(
"baz.proto", &file));
208 "name: \"foo.proto\" "
211 " field { name:\"qux\" }"
212 " nested_type { name: \"Grault\" } "
213 " enum_type { name: \"Garply\" } "
217 " value { name:\"FRED\" } "
219 "extension { name: \"plugh\" } "
222 " method { name: \"Thud\" } "
225 "name: \"bar.proto\" "
226 "package: \"corge\" "
227 "message_type { name: \"Bar\" }");
231 EXPECT_TRUE(database_->FindFileContainingSymbol(
"Foo", &file));
238 EXPECT_TRUE(database_->FindFileContainingSymbol(
"Foo.qux", &file));
243 database_->FindFileContainingSymbol(
"Foo.none_field.none", &file));
249 EXPECT_TRUE(database_->FindFileContainingSymbol(
"Foo.Grault", &file));
256 EXPECT_TRUE(database_->FindFileContainingSymbol(
"Foo.Garply", &file));
263 EXPECT_TRUE(database_->FindFileContainingSymbol(
"Waldo", &file));
270 EXPECT_TRUE(database_->FindFileContainingSymbol(
"Waldo.FRED", &file));
277 EXPECT_TRUE(database_->FindFileContainingSymbol(
"plugh", &file));
284 EXPECT_TRUE(database_->FindFileContainingSymbol(
"Xyzzy", &file));
291 EXPECT_TRUE(database_->FindFileContainingSymbol(
"Xyzzy.Thud", &file));
298 EXPECT_TRUE(database_->FindFileContainingSymbol(
"corge.Bar", &file));
305 EXPECT_FALSE(database_->FindFileContainingSymbol(
"Baz", &file));
311 EXPECT_FALSE(database_->FindFileContainingSymbol(
"Bar", &file));
315 TEST_P(DescriptorDatabaseTest, FindFileContainingExtension) {
317 "name: \"foo.proto\" "
320 " extension_range { start: 1 end: 1000 } "
321 " extension { name:\"qux\" label:LABEL_OPTIONAL type:TYPE_INT32 "
323 " extendee: \".Foo\" }"
326 "name: \"bar.proto\" "
327 "package: \"corge\" "
328 "dependency: \"foo.proto\" "
331 " extension_range { start: 1 end: 1000 } "
333 "extension { name:\"grault\" extendee: \".Foo\" number:32 } "
334 "extension { name:\"garply\" extendee: \".corge.Bar\" number:70 } "
335 "extension { name:\"waldo\" extendee: \"Bar\" number:56 } ");
339 EXPECT_TRUE(database_->FindFileContainingExtension(
"Foo", 5, &file));
345 EXPECT_TRUE(database_->FindFileContainingExtension(
"Foo", 32, &file));
352 EXPECT_TRUE(database_->FindFileContainingExtension(
"corge.Bar", 70, &file));
360 EXPECT_FALSE(database_->FindFileContainingExtension(
"Bar", 56, &file));
362 database_->FindFileContainingExtension(
"corge.Bar", 56, &file));
368 EXPECT_FALSE(database_->FindFileContainingExtension(
"Foo", 12, &file));
375 database_->FindFileContainingExtension(
"NoSuchType", 5, &file));
381 EXPECT_FALSE(database_->FindFileContainingExtension(
"Bar", 70, &file));
385 TEST_P(DescriptorDatabaseTest, FindAllExtensionNumbers) {
387 "name: \"foo.proto\" "
390 " extension_range { start: 1 end: 1000 } "
391 " extension { name:\"qux\" label:LABEL_OPTIONAL type:TYPE_INT32 "
393 " extendee: \".Foo\" }"
396 "name: \"bar.proto\" "
397 "package: \"corge\" "
398 "dependency: \"foo.proto\" "
401 " extension_range { start: 1 end: 1000 } "
403 "extension { name:\"grault\" extendee: \".Foo\" number:32 } "
404 "extension { name:\"garply\" extendee: \".corge.Bar\" number:70 } "
405 "extension { name:\"waldo\" extendee: \"Bar\" number:56 } ");
408 std::vector<int> numbers;
409 EXPECT_TRUE(database_->FindAllExtensionNumbers(
"Foo", &numbers));
411 std::sort(numbers.begin(), numbers.end());
417 std::vector<int> numbers;
418 EXPECT_TRUE(database_->FindAllExtensionNumbers(
"corge.Bar", &numbers));
426 std::vector<int> numbers;
427 EXPECT_FALSE(database_->FindAllExtensionNumbers(
"NoSuchType", &numbers));
432 std::vector<int> numbers;
433 EXPECT_FALSE(database_->FindAllExtensionNumbers(
"Bar", &numbers));
437 TEST_P(DescriptorDatabaseTest, ConflictingFileError) {
439 "name: \"foo.proto\" "
443 AddToDatabaseWithError(
444 "name: \"foo.proto\" "
450 TEST_P(DescriptorDatabaseTest, ConflictingTypeError) {
452 "name: \"foo.proto\" "
456 AddToDatabaseWithError(
457 "name: \"bar.proto\" "
463 TEST_P(DescriptorDatabaseTest, ConflictingExtensionError) {
465 "name: \"foo.proto\" "
466 "extension { name:\"foo\" label:LABEL_OPTIONAL type:TYPE_INT32 number:5 "
467 " extendee: \".Foo\" }");
468 AddToDatabaseWithError(
469 "name: \"bar.proto\" "
470 "extension { name:\"bar\" label:LABEL_OPTIONAL type:TYPE_INT32 number:5 "
471 " extendee: \".Foo\" }");
475 Simple, DescriptorDatabaseTest,
478 MemoryConserving, DescriptorDatabaseTest,
483 #endif // GTEST_HAS_PARAM_TEST
485 TEST(EncodedDescriptorDatabaseExtraTest, FindNameOfFileContainingSymbol) {
499 std::string data2 = file2b.SerializeAsString() + file2a.SerializeAsString();
502 EncodedDescriptorDatabase db;
503 db.Add(data1.data(), data1.size());
504 db.Add(data2.data(), data2.size());
508 EXPECT_TRUE(db.FindNameOfFileContainingSymbol(
"foo.Foo", &filename));
510 EXPECT_TRUE(db.FindNameOfFileContainingSymbol(
"foo.Foo.Blah", &filename));
512 EXPECT_TRUE(db.FindNameOfFileContainingSymbol(
"bar.Bar", &filename));
514 EXPECT_FALSE(db.FindNameOfFileContainingSymbol(
"foo", &filename));
515 EXPECT_FALSE(db.FindNameOfFileContainingSymbol(
"bar", &filename));
516 EXPECT_FALSE(db.FindNameOfFileContainingSymbol(
"baz.Baz", &filename));
519 TEST(SimpleDescriptorDatabaseExtraTest, FindAllFileNames) {
521 f.set_name(
"foo.proto");
522 f.set_package(
"foo");
523 f.add_message_type()->set_name(
"Foo");
525 SimpleDescriptorDatabase db;
529 std::vector<std::string> all_files;
530 db.FindAllFileNames(&all_files);
531 EXPECT_THAT(all_files, testing::ElementsAre(
"foo.proto"));
534 TEST(SimpleDescriptorDatabaseExtraTest, FindAllPackageNames) {
536 f.set_name(
"foo.proto");
537 f.set_package(
"foo");
538 f.add_message_type()->set_name(
"Foo");
541 b.set_name(
"bar.proto");
543 b.add_message_type()->set_name(
"Bar");
545 SimpleDescriptorDatabase db;
554 TEST(SimpleDescriptorDatabaseExtraTest, FindAllMessageNames) {
556 f.set_name(
"foo.proto");
557 f.set_package(
"foo");
558 f.add_message_type()->set_name(
"Foo");
561 b.set_name(
"bar.proto");
563 b.add_message_type()->set_name(
"Bar");
565 SimpleDescriptorDatabase db;
569 std::vector<string> messages;
571 EXPECT_THAT(messages, ::testing::UnorderedElementsAre(
"foo.Foo",
"Bar"));
578 MergedDescriptorDatabaseTest()
582 virtual void SetUp() {
585 "name: \"foo.proto\" "
586 "message_type { name:\"Foo\" extension_range { start: 1 end: 100 } } "
587 "extension { name:\"foo_ext\" extendee: \".Foo\" number:3 "
588 " label:LABEL_OPTIONAL type:TYPE_INT32 } ");
591 "name: \"bar.proto\" "
592 "message_type { name:\"Bar\" extension_range { start: 1 end: 100 } } "
593 "extension { name:\"bar_ext\" extendee: \".Bar\" number:5 "
594 " label:LABEL_OPTIONAL type:TYPE_INT32 } ");
599 "name: \"baz.proto\" "
600 "message_type { name:\"Baz\" extension_range { start: 1 end: 100 } } "
601 "message_type { name:\"FromPool1\" } "
602 "extension { name:\"baz_ext\" extendee: \".Baz\" number:12 "
603 " label:LABEL_OPTIONAL type:TYPE_INT32 } "
604 "extension { name:\"database1_only_ext\" extendee: \".Baz\" number:13 "
605 " label:LABEL_OPTIONAL type:TYPE_INT32 } ");
608 "name: \"baz.proto\" "
609 "message_type { name:\"Baz\" extension_range { start: 1 end: 100 } } "
610 "message_type { name:\"FromPool2\" } "
611 "extension { name:\"baz_ext\" extendee: \".Baz\" number:12 "
612 " label:LABEL_OPTIONAL type:TYPE_INT32 } ");
628 ExpectContainsType(file,
"Foo");
636 ExpectContainsType(file,
"Bar");
644 ExpectContainsType(file,
"FromPool1");
652 ExpectContainsType(file,
"FromPool2");
668 ExpectContainsType(file,
"Foo");
676 ExpectContainsType(file,
"Bar");
684 ExpectContainsType(file,
"FromPool1");
692 ExpectContainsType(file,
"FromPool2");
710 TEST_F(MergedDescriptorDatabaseTest, FindFileContainingExtension) {
716 ExpectContainsType(file,
"Foo");
724 ExpectContainsType(file,
"Bar");
732 ExpectContainsType(file,
"FromPool1");
740 ExpectContainsType(file,
"FromPool2");
758 TEST_F(MergedDescriptorDatabaseTest, FindAllExtensionNumbers) {
761 std::vector<int> numbers;
769 std::vector<int> numbers;
777 std::vector<int> numbers;
780 std::sort(numbers.begin(), numbers.end());
786 std::vector<int> numbers;
789 std::sort(numbers.begin(), numbers.end());
796 std::vector<int> numbers;