| OLD | NEW |
| (Empty) | |
| 1 #region Copyright notice and license |
| 2 // Protocol Buffers - Google's data interchange format |
| 3 // Copyright 2008 Google Inc. All rights reserved. |
| 4 // https://developers.google.com/protocol-buffers/ |
| 5 // |
| 6 // Redistribution and use in source and binary forms, with or without |
| 7 // modification, are permitted provided that the following conditions are |
| 8 // met: |
| 9 // |
| 10 // * Redistributions of source code must retain the above copyright |
| 11 // notice, this list of conditions and the following disclaimer. |
| 12 // * Redistributions in binary form must reproduce the above |
| 13 // copyright notice, this list of conditions and the following disclaimer |
| 14 // in the documentation and/or other materials provided with the |
| 15 // distribution. |
| 16 // * Neither the name of Google Inc. nor the names of its |
| 17 // contributors may be used to endorse or promote products derived from |
| 18 // this software without specific prior written permission. |
| 19 // |
| 20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 #endregion |
| 32 |
| 33 using System.Linq; |
| 34 using Google.Protobuf.TestProtos; |
| 35 using NUnit.Framework; |
| 36 using UnitTest.Issues.TestProtos; |
| 37 |
| 38 namespace Google.Protobuf.Reflection |
| 39 { |
| 40 /// <summary> |
| 41 /// Tests for descriptors. (Not in its own namespace or broken up into indiv
idual classes as the |
| 42 /// size doesn't warrant it. On the other hand, this makes me feel a bit dir
ty...) |
| 43 /// </summary> |
| 44 public class DescriptorsTest |
| 45 { |
| 46 [Test] |
| 47 public void FileDescriptor() |
| 48 { |
| 49 FileDescriptor file = UnittestProto3.Descriptor; |
| 50 |
| 51 Assert.AreEqual("google/protobuf/unittest_proto3.proto", file.Name); |
| 52 Assert.AreEqual("protobuf_unittest", file.Package); |
| 53 |
| 54 Assert.AreEqual("UnittestProto", file.Proto.Options.JavaOuterClassna
me); |
| 55 Assert.AreEqual("google/protobuf/unittest_proto3.proto", file.Proto.
Name); |
| 56 |
| 57 // unittest.proto doesn't have any public imports, but unittest_impo
rt.proto does. |
| 58 Assert.AreEqual(0, file.PublicDependencies.Count); |
| 59 Assert.AreEqual(1, UnittestImportProto3.Descriptor.PublicDependencie
s.Count); |
| 60 Assert.AreEqual(UnittestImportPublicProto3.Descriptor, UnittestImpor
tProto3.Descriptor.PublicDependencies[0]); |
| 61 |
| 62 Assert.AreEqual(1, file.Dependencies.Count); |
| 63 Assert.AreEqual(UnittestImportProto3.Descriptor, file.Dependencies[0
]); |
| 64 |
| 65 MessageDescriptor messageType = TestAllTypes.Descriptor; |
| 66 Assert.AreSame(typeof(TestAllTypes), messageType.GeneratedType); |
| 67 Assert.AreEqual(messageType, file.MessageTypes[0]); |
| 68 Assert.AreEqual(messageType, file.FindTypeByName<MessageDescriptor>(
"TestAllTypes")); |
| 69 Assert.Null(file.FindTypeByName<MessageDescriptor>("NoSuchType")); |
| 70 Assert.Null(file.FindTypeByName<MessageDescriptor>("protobuf_unittes
t.TestAllTypes")); |
| 71 for (int i = 0; i < file.MessageTypes.Count; i++) |
| 72 { |
| 73 Assert.AreEqual(i, file.MessageTypes[i].Index); |
| 74 } |
| 75 |
| 76 Assert.AreEqual(file.EnumTypes[0], file.FindTypeByName<EnumDescripto
r>("ForeignEnum")); |
| 77 Assert.Null(file.FindTypeByName<EnumDescriptor>("NoSuchType")); |
| 78 Assert.Null(file.FindTypeByName<EnumDescriptor>("protobuf_unittest.F
oreignEnum")); |
| 79 Assert.AreEqual(1, UnittestImportProto3.Descriptor.EnumTypes.Count); |
| 80 Assert.AreEqual("ImportEnum", UnittestImportProto3.Descriptor.EnumTy
pes[0].Name); |
| 81 for (int i = 0; i < file.EnumTypes.Count; i++) |
| 82 { |
| 83 Assert.AreEqual(i, file.EnumTypes[i].Index); |
| 84 } |
| 85 |
| 86 Assert.AreEqual(10, file.SerializedData[0]); |
| 87 } |
| 88 |
| 89 [Test] |
| 90 public void MessageDescriptor() |
| 91 { |
| 92 MessageDescriptor messageType = TestAllTypes.Descriptor; |
| 93 MessageDescriptor nestedType = TestAllTypes.Types.NestedMessage.Desc
riptor; |
| 94 |
| 95 Assert.AreEqual("TestAllTypes", messageType.Name); |
| 96 Assert.AreEqual("protobuf_unittest.TestAllTypes", messageType.FullNa
me); |
| 97 Assert.AreEqual(UnittestProto3.Descriptor, messageType.File); |
| 98 Assert.IsNull(messageType.ContainingType); |
| 99 Assert.IsNull(messageType.Proto.Options); |
| 100 |
| 101 Assert.AreEqual("TestAllTypes", messageType.Name); |
| 102 |
| 103 Assert.AreEqual("NestedMessage", nestedType.Name); |
| 104 Assert.AreEqual("protobuf_unittest.TestAllTypes.NestedMessage", nest
edType.FullName); |
| 105 Assert.AreEqual(UnittestProto3.Descriptor, nestedType.File); |
| 106 Assert.AreEqual(messageType, nestedType.ContainingType); |
| 107 |
| 108 FieldDescriptor field = messageType.Fields.InDeclarationOrder()[0]; |
| 109 Assert.AreEqual("single_int32", field.Name); |
| 110 Assert.AreEqual(field, messageType.FindDescriptor<FieldDescriptor>("
single_int32")); |
| 111 Assert.Null(messageType.FindDescriptor<FieldDescriptor>("no_such_fie
ld")); |
| 112 Assert.AreEqual(field, messageType.FindFieldByNumber(1)); |
| 113 Assert.Null(messageType.FindFieldByNumber(571283)); |
| 114 var fieldsInDeclarationOrder = messageType.Fields.InDeclarationOrder
(); |
| 115 for (int i = 0; i < fieldsInDeclarationOrder.Count; i++) |
| 116 { |
| 117 Assert.AreEqual(i, fieldsInDeclarationOrder[i].Index); |
| 118 } |
| 119 |
| 120 Assert.AreEqual(nestedType, messageType.NestedTypes[0]); |
| 121 Assert.AreEqual(nestedType, messageType.FindDescriptor<MessageDescri
ptor>("NestedMessage")); |
| 122 Assert.Null(messageType.FindDescriptor<MessageDescriptor>("NoSuchTyp
e")); |
| 123 for (int i = 0; i < messageType.NestedTypes.Count; i++) |
| 124 { |
| 125 Assert.AreEqual(i, messageType.NestedTypes[i].Index); |
| 126 } |
| 127 |
| 128 Assert.AreEqual(messageType.EnumTypes[0], messageType.FindDescriptor
<EnumDescriptor>("NestedEnum")); |
| 129 Assert.Null(messageType.FindDescriptor<EnumDescriptor>("NoSuchType")
); |
| 130 for (int i = 0; i < messageType.EnumTypes.Count; i++) |
| 131 { |
| 132 Assert.AreEqual(i, messageType.EnumTypes[i].Index); |
| 133 } |
| 134 } |
| 135 |
| 136 [Test] |
| 137 public void FieldDescriptor() |
| 138 { |
| 139 MessageDescriptor messageType = TestAllTypes.Descriptor; |
| 140 FieldDescriptor primitiveField = messageType.FindDescriptor<FieldDes
criptor>("single_int32"); |
| 141 FieldDescriptor enumField = messageType.FindDescriptor<FieldDescript
or>("single_nested_enum"); |
| 142 FieldDescriptor messageField = messageType.FindDescriptor<FieldDescr
iptor>("single_foreign_message"); |
| 143 |
| 144 Assert.AreEqual("single_int32", primitiveField.Name); |
| 145 Assert.AreEqual("protobuf_unittest.TestAllTypes.single_int32", |
| 146 primitiveField.FullName); |
| 147 Assert.AreEqual(1, primitiveField.FieldNumber); |
| 148 Assert.AreEqual(messageType, primitiveField.ContainingType); |
| 149 Assert.AreEqual(UnittestProto3.Descriptor, primitiveField.File); |
| 150 Assert.AreEqual(FieldType.Int32, primitiveField.FieldType); |
| 151 Assert.IsNull(primitiveField.Proto.Options); |
| 152 |
| 153 Assert.AreEqual("single_nested_enum", enumField.Name); |
| 154 Assert.AreEqual(FieldType.Enum, enumField.FieldType); |
| 155 // Assert.AreEqual(TestAllTypes.Types.NestedEnum.DescriptorProtoFile
, enumField.EnumType); |
| 156 |
| 157 Assert.AreEqual("single_foreign_message", messageField.Name); |
| 158 Assert.AreEqual(FieldType.Message, messageField.FieldType); |
| 159 Assert.AreEqual(ForeignMessage.Descriptor, messageField.MessageType)
; |
| 160 } |
| 161 |
| 162 [Test] |
| 163 public void FieldDescriptorLabel() |
| 164 { |
| 165 FieldDescriptor singleField = |
| 166 TestAllTypes.Descriptor.FindDescriptor<FieldDescriptor>("single_
int32"); |
| 167 FieldDescriptor repeatedField = |
| 168 TestAllTypes.Descriptor.FindDescriptor<FieldDescriptor>("repeate
d_int32"); |
| 169 |
| 170 Assert.IsFalse(singleField.IsRepeated); |
| 171 Assert.IsTrue(repeatedField.IsRepeated); |
| 172 } |
| 173 |
| 174 [Test] |
| 175 public void EnumDescriptor() |
| 176 { |
| 177 // Note: this test is a bit different to the Java version because th
ere's no static way of getting to the descriptor |
| 178 EnumDescriptor enumType = UnittestProto3.Descriptor.FindTypeByName<E
numDescriptor>("ForeignEnum"); |
| 179 EnumDescriptor nestedType = TestAllTypes.Descriptor.FindDescriptor<E
numDescriptor>("NestedEnum"); |
| 180 |
| 181 Assert.AreEqual("ForeignEnum", enumType.Name); |
| 182 Assert.AreEqual("protobuf_unittest.ForeignEnum", enumType.FullName); |
| 183 Assert.AreEqual(UnittestProto3.Descriptor, enumType.File); |
| 184 Assert.Null(enumType.ContainingType); |
| 185 Assert.Null(enumType.Proto.Options); |
| 186 |
| 187 Assert.AreEqual("NestedEnum", nestedType.Name); |
| 188 Assert.AreEqual("protobuf_unittest.TestAllTypes.NestedEnum", |
| 189 nestedType.FullName); |
| 190 Assert.AreEqual(UnittestProto3.Descriptor, nestedType.File); |
| 191 Assert.AreEqual(TestAllTypes.Descriptor, nestedType.ContainingType); |
| 192 |
| 193 EnumValueDescriptor value = enumType.FindValueByName("FOREIGN_FOO"); |
| 194 Assert.AreEqual(value, enumType.Values[1]); |
| 195 Assert.AreEqual("FOREIGN_FOO", value.Name); |
| 196 Assert.AreEqual(4, value.Number); |
| 197 Assert.AreEqual((int) ForeignEnum.FOREIGN_FOO, value.Number); |
| 198 Assert.AreEqual(value, enumType.FindValueByNumber(4)); |
| 199 Assert.Null(enumType.FindValueByName("NO_SUCH_VALUE")); |
| 200 for (int i = 0; i < enumType.Values.Count; i++) |
| 201 { |
| 202 Assert.AreEqual(i, enumType.Values[i].Index); |
| 203 } |
| 204 } |
| 205 |
| 206 [Test] |
| 207 public void OneofDescriptor() |
| 208 { |
| 209 OneofDescriptor descriptor = TestAllTypes.Descriptor.FindDescriptor<
OneofDescriptor>("oneof_field"); |
| 210 Assert.AreEqual("oneof_field", descriptor.Name); |
| 211 Assert.AreEqual("protobuf_unittest.TestAllTypes.oneof_field", descri
ptor.FullName); |
| 212 |
| 213 var expectedFields = new[] { |
| 214 TestAllTypes.OneofBytesFieldNumber, |
| 215 TestAllTypes.OneofNestedMessageFieldNumber, |
| 216 TestAllTypes.OneofStringFieldNumber, |
| 217 TestAllTypes.OneofUint32FieldNumber } |
| 218 .Select(fieldNumber => TestAllTypes.Descriptor.FindFieldByNumber
(fieldNumber)) |
| 219 .ToList(); |
| 220 foreach (var field in expectedFields) |
| 221 { |
| 222 Assert.AreSame(descriptor, field.ContainingOneof); |
| 223 } |
| 224 |
| 225 CollectionAssert.AreEquivalent(expectedFields, descriptor.Fields); |
| 226 } |
| 227 |
| 228 [Test] |
| 229 public void ConstructionWithoutGeneratedCodeInfo() |
| 230 { |
| 231 var data = UnittestIssues.Descriptor.Proto.ToByteArray(); |
| 232 var newDescriptor = Google.Protobuf.Reflection.FileDescriptor.Intern
alBuildGeneratedFileFrom(data, new Reflection.FileDescriptor[] { }, null); |
| 233 |
| 234 // We should still be able to get at a field... |
| 235 var messageDescriptor = newDescriptor.FindTypeByName<MessageDescript
or>("ItemField"); |
| 236 var fieldDescriptor = messageDescriptor.FindFieldByName("item"); |
| 237 // But there shouldn't be an accessor (or a generated type for the m
essage) |
| 238 Assert.IsNull(fieldDescriptor.Accessor); |
| 239 Assert.IsNull(messageDescriptor.GeneratedType); |
| 240 } |
| 241 |
| 242 // From TestFieldOrdering: |
| 243 // string my_string = 11; |
| 244 // int64 my_int = 1; |
| 245 // float my_float = 101; |
| 246 // NestedMessage single_nested_message = 200; |
| 247 [Test] |
| 248 public void FieldListOrderings() |
| 249 { |
| 250 var fields = TestFieldOrderings.Descriptor.Fields; |
| 251 Assert.AreEqual(new[] { 11, 1, 101, 200 }, fields.InDeclarationOrder
().Select(x => x.FieldNumber)); |
| 252 Assert.AreEqual(new[] { 1, 11, 101, 200 }, fields.InFieldNumberOrder
().Select(x => x.FieldNumber)); |
| 253 } |
| 254 } |
| 255 } |
| OLD | NEW |