Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(154)

Side by Side Diff: third_party/protobuf/src/google/protobuf/descriptor_unittest.cc

Issue 2495533002: third_party/protobuf: Update to HEAD (83d681ee2c) (Closed)
Patch Set: Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Protocol Buffers - Google's data interchange format 1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved. 2 // Copyright 2008 Google Inc. All rights reserved.
3 // https://developers.google.com/protocol-buffers/ 3 // https://developers.google.com/protocol-buffers/
4 // 4 //
5 // Redistribution and use in source and binary forms, with or without 5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are 6 // modification, are permitted provided that the following conditions are
7 // met: 7 // met:
8 // 8 //
9 // * Redistributions of source code must retain the above copyright 9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer. 10 // notice, this list of conditions and the following disclaimer.
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
48 #include <google/protobuf/descriptor.h> 48 #include <google/protobuf/descriptor.h>
49 #include <google/protobuf/descriptor_database.h> 49 #include <google/protobuf/descriptor_database.h>
50 #include <google/protobuf/dynamic_message.h> 50 #include <google/protobuf/dynamic_message.h>
51 #include <google/protobuf/text_format.h> 51 #include <google/protobuf/text_format.h>
52 #include <google/protobuf/stubs/strutil.h> 52 #include <google/protobuf/stubs/strutil.h>
53 #include <google/protobuf/stubs/substitute.h> 53 #include <google/protobuf/stubs/substitute.h>
54 54
55 #include <google/protobuf/stubs/common.h> 55 #include <google/protobuf/stubs/common.h>
56 #include <google/protobuf/stubs/logging.h> 56 #include <google/protobuf/stubs/logging.h>
57 #include <google/protobuf/stubs/logging.h> 57 #include <google/protobuf/stubs/logging.h>
58 #include <google/protobuf/stubs/scoped_ptr.h>
59 #include <google/protobuf/testing/googletest.h> 58 #include <google/protobuf/testing/googletest.h>
60 #include <gtest/gtest.h> 59 #include <gtest/gtest.h>
61 60
62 namespace google { 61 namespace google {
63 namespace protobuf { 62 namespace protobuf {
64 63
65 // Can't use an anonymous namespace here due to brokenness of Tru64 compiler. 64 // Can't use an anonymous namespace here due to brokenness of Tru64 compiler.
66 namespace descriptor_unittest { 65 namespace descriptor_unittest {
67 66
68 // Some helpers to make assembling descriptors faster. 67 // Some helpers to make assembling descriptors faster.
(...skipping 698 matching lines...) Expand 10 before | Expand all | Expand 10 after
767 EXPECT_EQ("TestMessage.qux", qux_->full_name()); 766 EXPECT_EQ("TestMessage.qux", qux_->full_name());
768 767
769 EXPECT_EQ("corge.grault.TestMessage2.foo", foo2_->full_name()); 768 EXPECT_EQ("corge.grault.TestMessage2.foo", foo2_->full_name());
770 EXPECT_EQ("corge.grault.TestMessage2.bar", bar2_->full_name()); 769 EXPECT_EQ("corge.grault.TestMessage2.bar", bar2_->full_name());
771 EXPECT_EQ("corge.grault.TestMessage2.quux", quux2_->full_name()); 770 EXPECT_EQ("corge.grault.TestMessage2.quux", quux2_->full_name());
772 } 771 }
773 772
774 TEST_F(DescriptorTest, FieldJsonName) { 773 TEST_F(DescriptorTest, FieldJsonName) {
775 EXPECT_EQ("fieldName1", message4_->field(0)->json_name()); 774 EXPECT_EQ("fieldName1", message4_->field(0)->json_name());
776 EXPECT_EQ("fieldName2", message4_->field(1)->json_name()); 775 EXPECT_EQ("fieldName2", message4_->field(1)->json_name());
777 EXPECT_EQ("fieldName3", message4_->field(2)->json_name()); 776 EXPECT_EQ("FieldName3", message4_->field(2)->json_name());
778 EXPECT_EQ("fieldName4", message4_->field(3)->json_name()); 777 EXPECT_EQ("FieldName4", message4_->field(3)->json_name());
779 EXPECT_EQ("fIELDNAME5", message4_->field(4)->json_name()); 778 EXPECT_EQ("FIELDNAME5", message4_->field(4)->json_name());
780 EXPECT_EQ("@type", message4_->field(5)->json_name()); 779 EXPECT_EQ("@type", message4_->field(5)->json_name());
781 780
782 DescriptorProto proto; 781 DescriptorProto proto;
783 message4_->CopyTo(&proto); 782 message4_->CopyTo(&proto);
784 ASSERT_EQ(6, proto.field_size()); 783 ASSERT_EQ(6, proto.field_size());
785 EXPECT_FALSE(proto.field(0).has_json_name()); 784 EXPECT_FALSE(proto.field(0).has_json_name());
786 EXPECT_FALSE(proto.field(1).has_json_name()); 785 EXPECT_FALSE(proto.field(1).has_json_name());
787 EXPECT_FALSE(proto.field(2).has_json_name()); 786 EXPECT_FALSE(proto.field(2).has_json_name());
788 EXPECT_FALSE(proto.field(3).has_json_name()); 787 EXPECT_FALSE(proto.field(3).has_json_name());
789 EXPECT_FALSE(proto.field(4).has_json_name()); 788 EXPECT_FALSE(proto.field(4).has_json_name());
790 EXPECT_EQ("@type", proto.field(5).json_name()); 789 EXPECT_EQ("@type", proto.field(5).json_name());
791 790
792 proto.Clear(); 791 proto.Clear();
793 CopyWithJsonName(message4_, &proto); 792 CopyWithJsonName(message4_, &proto);
794 ASSERT_EQ(6, proto.field_size()); 793 ASSERT_EQ(6, proto.field_size());
795 EXPECT_EQ("fieldName1", proto.field(0).json_name()); 794 EXPECT_EQ("fieldName1", proto.field(0).json_name());
796 EXPECT_EQ("fieldName2", proto.field(1).json_name()); 795 EXPECT_EQ("fieldName2", proto.field(1).json_name());
797 EXPECT_EQ("fieldName3", proto.field(2).json_name()); 796 EXPECT_EQ("FieldName3", proto.field(2).json_name());
798 EXPECT_EQ("fieldName4", proto.field(3).json_name()); 797 EXPECT_EQ("FieldName4", proto.field(3).json_name());
799 EXPECT_EQ("fIELDNAME5", proto.field(4).json_name()); 798 EXPECT_EQ("FIELDNAME5", proto.field(4).json_name());
800 EXPECT_EQ("@type", proto.field(5).json_name()); 799 EXPECT_EQ("@type", proto.field(5).json_name());
800
801 // Test generated descriptor.
802 const Descriptor* generated = protobuf_unittest::TestJsonName::descriptor();
803 ASSERT_EQ(6, generated->field_count());
804 EXPECT_EQ("fieldName1", generated->field(0)->json_name());
805 EXPECT_EQ("fieldName2", generated->field(1)->json_name());
806 EXPECT_EQ("FieldName3", generated->field(2)->json_name());
807 EXPECT_EQ("FieldName4", generated->field(3)->json_name());
808 EXPECT_EQ("FIELDNAME5", generated->field(4)->json_name());
809 EXPECT_EQ("@type", generated->field(5)->json_name());
801 } 810 }
802 811
803 TEST_F(DescriptorTest, FieldFile) { 812 TEST_F(DescriptorTest, FieldFile) {
804 EXPECT_EQ(foo_file_, foo_->file()); 813 EXPECT_EQ(foo_file_, foo_->file());
805 EXPECT_EQ(foo_file_, bar_->file()); 814 EXPECT_EQ(foo_file_, bar_->file());
806 EXPECT_EQ(foo_file_, baz_->file()); 815 EXPECT_EQ(foo_file_, baz_->file());
807 EXPECT_EQ(foo_file_, qux_->file()); 816 EXPECT_EQ(foo_file_, qux_->file());
808 817
809 EXPECT_EQ(bar_file_, foo2_->file()); 818 EXPECT_EQ(bar_file_, foo2_->file());
810 EXPECT_EQ(bar_file_, bar2_->file()); 819 EXPECT_EQ(bar_file_, bar2_->file());
(...skipping 1887 matching lines...) Expand 10 before | Expand all | Expand 10 after
2698 AllowUnknownDependenciesTest, 2707 AllowUnknownDependenciesTest,
2699 testing::Values(NO_DATABASE, FALLBACK_DATABASE)); 2708 testing::Values(NO_DATABASE, FALLBACK_DATABASE));
2700 2709
2701 // =================================================================== 2710 // ===================================================================
2702 2711
2703 TEST(CustomOptions, OptionLocations) { 2712 TEST(CustomOptions, OptionLocations) {
2704 const Descriptor* message = 2713 const Descriptor* message =
2705 protobuf_unittest::TestMessageWithCustomOptions::descriptor(); 2714 protobuf_unittest::TestMessageWithCustomOptions::descriptor();
2706 const FileDescriptor* file = message->file(); 2715 const FileDescriptor* file = message->file();
2707 const FieldDescriptor* field = message->FindFieldByName("field1"); 2716 const FieldDescriptor* field = message->FindFieldByName("field1");
2717 const OneofDescriptor* oneof = message->FindOneofByName("AnOneof");
2708 const EnumDescriptor* enm = message->FindEnumTypeByName("AnEnum"); 2718 const EnumDescriptor* enm = message->FindEnumTypeByName("AnEnum");
2709 // TODO(benjy): Support EnumValue options, once the compiler does. 2719 // TODO(benjy): Support EnumValue options, once the compiler does.
2710 const ServiceDescriptor* service = 2720 const ServiceDescriptor* service =
2711 file->FindServiceByName("TestServiceWithCustomOptions"); 2721 file->FindServiceByName("TestServiceWithCustomOptions");
2712 const MethodDescriptor* method = service->FindMethodByName("Foo"); 2722 const MethodDescriptor* method = service->FindMethodByName("Foo");
2713 2723
2714 EXPECT_EQ(GOOGLE_LONGLONG(9876543210), 2724 EXPECT_EQ(GOOGLE_LONGLONG(9876543210),
2715 file->options().GetExtension(protobuf_unittest::file_opt1)); 2725 file->options().GetExtension(protobuf_unittest::file_opt1));
2716 EXPECT_EQ(-56, 2726 EXPECT_EQ(-56,
2717 message->options().GetExtension(protobuf_unittest::message_opt1)); 2727 message->options().GetExtension(protobuf_unittest::message_opt1));
2718 EXPECT_EQ(GOOGLE_LONGLONG(8765432109), 2728 EXPECT_EQ(GOOGLE_LONGLONG(8765432109),
2719 field->options().GetExtension(protobuf_unittest::field_opt1)); 2729 field->options().GetExtension(protobuf_unittest::field_opt1));
2720 EXPECT_EQ(42, // Check that we get the default for an option we don't set. 2730 EXPECT_EQ(42, // Check that we get the default for an option we don't set.
2721 field->options().GetExtension(protobuf_unittest::field_opt2)); 2731 field->options().GetExtension(protobuf_unittest::field_opt2));
2732 EXPECT_EQ(-99,
2733 oneof->options().GetExtension(protobuf_unittest::oneof_opt1));
2722 EXPECT_EQ(-789, 2734 EXPECT_EQ(-789,
2723 enm->options().GetExtension(protobuf_unittest::enum_opt1)); 2735 enm->options().GetExtension(protobuf_unittest::enum_opt1));
2724 EXPECT_EQ(123, 2736 EXPECT_EQ(123,
2725 enm->value(1)->options().GetExtension( 2737 enm->value(1)->options().GetExtension(
2726 protobuf_unittest::enum_value_opt1)); 2738 protobuf_unittest::enum_value_opt1));
2727 EXPECT_EQ(GOOGLE_LONGLONG(-9876543210), 2739 EXPECT_EQ(GOOGLE_LONGLONG(-9876543210),
2728 service->options().GetExtension(protobuf_unittest::service_opt1)); 2740 service->options().GetExtension(protobuf_unittest::service_opt1));
2729 EXPECT_EQ(protobuf_unittest::METHODOPT1_VAL2, 2741 EXPECT_EQ(protobuf_unittest::METHODOPT1_VAL2,
2730 method->options().GetExtension(protobuf_unittest::method_opt1)); 2742 method->options().GetExtension(protobuf_unittest::method_opt1));
2731 2743
(...skipping 2288 matching lines...) Expand 10 before | Expand all | Expand 10 after
5020 "\"foo = { <proto text format> }\". To set fields within it, use " 5032 "\"foo = { <proto text format> }\". To set fields within it, use "
5021 "syntax like \"foo.foo = value\".\n"); 5033 "syntax like \"foo.foo = value\".\n");
5022 } 5034 }
5023 5035
5024 TEST_F(ValidationErrorTest, AggregateValueParseError) { 5036 TEST_F(ValidationErrorTest, AggregateValueParseError) {
5025 BuildDescriptorMessagesInTestPool(); 5037 BuildDescriptorMessagesInTestPool();
5026 5038
5027 BuildFileWithErrors( 5039 BuildFileWithErrors(
5028 EmbedAggregateValue("aggregate_value: \"1+2\""), 5040 EmbedAggregateValue("aggregate_value: \"1+2\""),
5029 "foo.proto: foo.proto: OPTION_VALUE: Error while parsing option " 5041 "foo.proto: foo.proto: OPTION_VALUE: Error while parsing option "
5030 "value for \"foo\": Expected identifier.\n"); 5042 "value for \"foo\": Expected identifier, got: 1\n");
5031 } 5043 }
5032 5044
5033 TEST_F(ValidationErrorTest, AggregateValueUnknownFields) { 5045 TEST_F(ValidationErrorTest, AggregateValueUnknownFields) {
5034 BuildDescriptorMessagesInTestPool(); 5046 BuildDescriptorMessagesInTestPool();
5035 5047
5036 BuildFileWithErrors( 5048 BuildFileWithErrors(
5037 EmbedAggregateValue("aggregate_value: \"x:100\""), 5049 EmbedAggregateValue("aggregate_value: \"x:100\""),
5038 "foo.proto: foo.proto: OPTION_VALUE: Error while parsing option " 5050 "foo.proto: foo.proto: OPTION_VALUE: Error while parsing option "
5039 "value for \"foo\": Message type \"Foo\" has no field named \"x\".\n"); 5051 "value for \"foo\": Message type \"Foo\" has no field named \"x\".\n");
5040 } 5052 }
(...skipping 503 matching lines...) Expand 10 before | Expand all | Expand 10 after
5544 "}", 5556 "}",
5545 file_proto.mutable_message_type(0)); 5557 file_proto.mutable_message_type(0));
5546 BuildFileWithErrors( 5558 BuildFileWithErrors(
5547 file_proto.DebugString(), 5559 file_proto.DebugString(),
5548 "foo.proto: Foo.FooMapEntry: NAME: \"FooMapEntry\" is already defined in " 5560 "foo.proto: Foo.FooMapEntry: NAME: \"FooMapEntry\" is already defined in "
5549 "\"Foo\".\n" 5561 "\"Foo\".\n"
5550 "foo.proto: Foo: NAME: Expanded map entry type FooMapEntry conflicts " 5562 "foo.proto: Foo: NAME: Expanded map entry type FooMapEntry conflicts "
5551 "with an existing enum type.\n"); 5563 "with an existing enum type.\n");
5552 } 5564 }
5553 5565
5566 TEST_F(ValidationErrorTest, EnumValuesConflictWhenPrefixesStripped) {
5567 BuildFileWithErrors(
5568 "syntax: 'proto3'"
5569 "name: 'foo.proto' "
5570 "enum_type {"
5571 " name: 'FooEnum' "
5572 " value { name: 'FOO_ENUM_BAZ' number: 0 }"
5573 " value { name: 'BAZ' number: 1 }"
5574 "}",
5575 "foo.proto: BAZ: NAME: When enum name is stripped and label is "
5576 "PascalCased (Baz), this value label conflicts with FOO_ENUM_BAZ. This "
5577 "will make the proto fail to compile for some languages, such as C#.\n");
5578
5579 BuildFileWithErrors(
5580 "syntax: 'proto3'"
5581 "name: 'foo.proto' "
5582 "enum_type {"
5583 " name: 'FooEnum' "
5584 " value { name: 'FOOENUM_BAZ' number: 0 }"
5585 " value { name: 'BAZ' number: 1 }"
5586 "}",
5587 "foo.proto: BAZ: NAME: When enum name is stripped and label is "
5588 "PascalCased (Baz), this value label conflicts with FOOENUM_BAZ. This "
5589 "will make the proto fail to compile for some languages, such as C#.\n");
5590
5591 BuildFileWithErrors(
5592 "syntax: 'proto3'"
5593 "name: 'foo.proto' "
5594 "enum_type {"
5595 " name: 'FooEnum' "
5596 " value { name: 'FOO_ENUM_BAR_BAZ' number: 0 }"
5597 " value { name: 'BAR__BAZ' number: 1 }"
5598 "}",
5599 "foo.proto: BAR__BAZ: NAME: When enum name is stripped and label is "
5600 "PascalCased (BarBaz), this value label conflicts with "
5601 "FOO_ENUM_BAR_BAZ. This "
5602 "will make the proto fail to compile for some languages, such as C#.\n");
5603
5604 BuildFileWithErrors(
5605 "syntax: 'proto3'"
5606 "name: 'foo.proto' "
5607 "enum_type {"
5608 " name: 'FooEnum' "
5609 " value { name: 'FOO_ENUM__BAR_BAZ' number: 0 }"
5610 " value { name: 'BAR_BAZ' number: 1 }"
5611 "}",
5612 "foo.proto: BAR_BAZ: NAME: When enum name is stripped and label is "
5613 "PascalCased (BarBaz), this value label conflicts with "
5614 "FOO_ENUM__BAR_BAZ. This "
5615 "will make the proto fail to compile for some languages, such as C#.\n");
5616
5617 // This isn't an error because the underscore will cause the PascalCase to
5618 // differ by case (BarBaz vs. Barbaz).
5619 BuildFile(
5620 "syntax: 'proto3'"
5621 "name: 'foo.proto' "
5622 "enum_type {"
5623 " name: 'FooEnum' "
5624 " value { name: 'BAR_BAZ' number: 0 }"
5625 " value { name: 'BARBAZ' number: 1 }"
5626 "}");
5627 }
5628
5554 TEST_F(ValidationErrorTest, MapEntryConflictsWithOneof) { 5629 TEST_F(ValidationErrorTest, MapEntryConflictsWithOneof) {
5555 FileDescriptorProto file_proto; 5630 FileDescriptorProto file_proto;
5556 FillValidMapEntry(&file_proto); 5631 FillValidMapEntry(&file_proto);
5557 TextFormat::MergeFromString( 5632 TextFormat::MergeFromString(
5558 "oneof_decl { " 5633 "oneof_decl { "
5559 " name: 'FooMapEntry' " 5634 " name: 'FooMapEntry' "
5560 "}" 5635 "}"
5561 "field { " 5636 "field { "
5562 " name: 'int_field' " 5637 " name: 'int_field' "
5563 " type: TYPE_INT32 " 5638 " type: TYPE_INT32 "
(...skipping 263 matching lines...) Expand 10 before | Expand all | Expand 10 after
5827 TEST_F(ValidationErrorTest, ValidateProto3JsonName) { 5902 TEST_F(ValidationErrorTest, ValidateProto3JsonName) {
5828 // The comparison is case-insensitive. 5903 // The comparison is case-insensitive.
5829 BuildFileWithErrors( 5904 BuildFileWithErrors(
5830 "name: 'foo.proto' " 5905 "name: 'foo.proto' "
5831 "syntax: 'proto3' " 5906 "syntax: 'proto3' "
5832 "message_type {" 5907 "message_type {"
5833 " name: 'Foo'" 5908 " name: 'Foo'"
5834 " field { name:'name' number:1 label:LABEL_OPTIONAL type:TYPE_INT32 }" 5909 " field { name:'name' number:1 label:LABEL_OPTIONAL type:TYPE_INT32 }"
5835 " field { name:'Name' number:2 label:LABEL_OPTIONAL type:TYPE_INT32 }" 5910 " field { name:'Name' number:2 label:LABEL_OPTIONAL type:TYPE_INT32 }"
5836 "}", 5911 "}",
5837 "foo.proto: Foo: OTHER: The JSON camcel-case name of field \"Name\" " 5912 "foo.proto: Foo: OTHER: The JSON camel-case name of field \"Name\" "
5838 "conflicts with field \"name\". This is not allowed in proto3.\n"); 5913 "conflicts with field \"name\". This is not allowed in proto3.\n");
5839 // Underscores are ignored. 5914 // Underscores are ignored.
5840 BuildFileWithErrors( 5915 BuildFileWithErrors(
5841 "name: 'foo.proto' " 5916 "name: 'foo.proto' "
5842 "syntax: 'proto3' " 5917 "syntax: 'proto3' "
5843 "message_type {" 5918 "message_type {"
5844 " name: 'Foo'" 5919 " name: 'Foo'"
5845 " field { name:'ab' number:1 label:LABEL_OPTIONAL type:TYPE_INT32 }" 5920 " field { name:'ab' number:1 label:LABEL_OPTIONAL type:TYPE_INT32 }"
5846 " field { name:'_a__b_' number:2 label:LABEL_OPTIONAL type:TYPE_INT32 }" 5921 " field { name:'_a__b_' number:2 label:LABEL_OPTIONAL type:TYPE_INT32 }"
5847 "}", 5922 "}",
5848 "foo.proto: Foo: OTHER: The JSON camcel-case name of field \"_a__b_\" " 5923 "foo.proto: Foo: OTHER: The JSON camel-case name of field \"_a__b_\" "
5849 "conflicts with field \"ab\". This is not allowed in proto3.\n"); 5924 "conflicts with field \"ab\". This is not allowed in proto3.\n");
5850 } 5925 }
5851 5926
5852 // =================================================================== 5927 // ===================================================================
5853 // DescriptorDatabase 5928 // DescriptorDatabase
5854 5929
5855 static void AddToDatabase(SimpleDescriptorDatabase* database, 5930 static void AddToDatabase(SimpleDescriptorDatabase* database,
5856 const char* file_text) { 5931 const char* file_text) {
5857 FileDescriptorProto file_proto; 5932 FileDescriptorProto file_proto;
5858 EXPECT_TRUE(TextFormat::ParseFromString(file_text, &file_proto)); 5933 EXPECT_TRUE(TextFormat::ParseFromString(file_text, &file_proto));
(...skipping 745 matching lines...) Expand 10 before | Expand all | Expand 10 after
6604 EXPECT_EQ(0, foo_location.span(1)); // Foo starts at column 0 6679 EXPECT_EQ(0, foo_location.span(1)); // Foo starts at column 0
6605 EXPECT_EQ(14, foo_location.span(2)); // Foo ends on column 14 6680 EXPECT_EQ(14, foo_location.span(2)); // Foo ends on column 14
6606 } 6681 }
6607 6682
6608 // =================================================================== 6683 // ===================================================================
6609 6684
6610 6685
6611 } // namespace descriptor_unittest 6686 } // namespace descriptor_unittest
6612 } // namespace protobuf 6687 } // namespace protobuf
6613 } // namespace google 6688 } // namespace google
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698