Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.ProtobufCS/0.9.1/ProtobufCS/src/ProtocolBuffers.Test/DescriptorsTest.cs @ 4095

Last change on this file since 4095 was 3857, checked in by abeham, 14 years ago

#866

  • Added protobuf-csharp-port project source to ExtLibs
File size: 16.4 KB
Line 
1#region Copyright notice and license
2// Protocol Buffers - Google's data interchange format
3// Copyright 2008 Google Inc.  All rights reserved.
4// http://github.com/jskeet/dotnet-protobufs/
5// Original C++/Java/Python code:
6// http://code.google.com/p/protobuf/
7//
8// Redistribution and use in source and binary forms, with or without
9// modification, are permitted provided that the following conditions are
10// met:
11//
12//     * Redistributions of source code must retain the above copyright
13// notice, this list of conditions and the following disclaimer.
14//     * Redistributions in binary form must reproduce the above
15// copyright notice, this list of conditions and the following disclaimer
16// in the documentation and/or other materials provided with the
17// distribution.
18//     * Neither the name of Google Inc. nor the names of its
19// contributors may be used to endorse or promote products derived from
20// this software without specific prior written permission.
21//
22// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33#endregion
34
35using System.Text;
36using Google.ProtocolBuffers.Descriptors;
37using Google.ProtocolBuffers.TestProtos;
38using NUnit.Framework;
39
40namespace Google.ProtocolBuffers {
41  /// <summary>
42  /// Tests for descriptors. (Not in its own namespace or broken up into individual classes as the
43  /// size doesn't warrant it. On the other hand, this makes me feel a bit dirty...)
44  /// </summary>
45  [TestFixture]
46  public class DescriptorsTest {
47   
48    [Test]
49    public void FileDescriptor()  {
50      FileDescriptor file = UnitTestProtoFile.Descriptor;
51
52      Assert.AreEqual("google/protobuf/unittest.proto", file.Name);
53      Assert.AreEqual("protobuf_unittest", file.Package);
54
55      Assert.AreEqual("UnittestProto", file.Options.JavaOuterClassname);
56      Assert.AreEqual("google/protobuf/unittest.proto", file.Proto.Name);
57
58// TODO(jonskeet): Either change to expect 2 dependencies, or don't emit them.
59//      Assert.AreEqual(2, file.Dependencies.Count);
60      Assert.AreEqual(UnitTestImportProtoFile.Descriptor, file.Dependencies[1]);
61
62      MessageDescriptor messageType = TestAllTypes.Descriptor;
63      Assert.AreEqual(messageType, file.MessageTypes[0]);
64      Assert.AreEqual(messageType, file.FindTypeByName<MessageDescriptor>("TestAllTypes"));
65      Assert.IsNull(file.FindTypeByName<MessageDescriptor>("NoSuchType"));
66      Assert.IsNull(file.FindTypeByName<MessageDescriptor>("protobuf_unittest.TestAllTypes"));
67      for (int i = 0; i < file.MessageTypes.Count; i++) {
68        Assert.AreEqual(i, file.MessageTypes[i].Index);
69      }
70
71      Assert.AreEqual(file.EnumTypes[0], file.FindTypeByName<EnumDescriptor>("ForeignEnum"));
72      Assert.IsNull(file.FindTypeByName<EnumDescriptor>("NoSuchType"));
73      Assert.IsNull(file.FindTypeByName<EnumDescriptor>("protobuf_unittest.ForeignEnum"));
74      Assert.AreEqual(1, UnitTestImportProtoFile.Descriptor.EnumTypes.Count);
75      Assert.AreEqual("ImportEnum", UnitTestImportProtoFile.Descriptor.EnumTypes[0].Name);
76      for (int i = 0; i < file.EnumTypes.Count; i++) {
77        Assert.AreEqual(i, file.EnumTypes[i].Index);
78      }
79
80      ServiceDescriptor service = TestService.Descriptor;
81      Assert.AreEqual(service, file.Services[0]);
82      Assert.AreEqual(service, file.FindTypeByName<ServiceDescriptor>("TestService"));
83      Assert.IsNull(file.FindTypeByName<ServiceDescriptor>("NoSuchType"));
84      Assert.IsNull(file.FindTypeByName<ServiceDescriptor>("protobuf_unittest.TestService"));
85      Assert.AreEqual(0, UnitTestImportProtoFile.Descriptor.Services.Count);
86      for (int i = 0; i < file.Services.Count; i++) {
87        Assert.AreEqual(i, file.Services[i].Index);
88      }
89
90      FieldDescriptor extension = UnitTestProtoFile.OptionalInt32Extension.Descriptor;
91      Assert.AreEqual(extension, file.Extensions[0]);
92      Assert.AreEqual(extension, file.FindTypeByName<FieldDescriptor>("optional_int32_extension"));
93      Assert.IsNull(file.FindTypeByName<FieldDescriptor>("no_such_ext"));
94      Assert.IsNull(file.FindTypeByName<FieldDescriptor>("protobuf_unittest.optional_int32_extension"));
95      Assert.AreEqual(0, UnitTestImportProtoFile.Descriptor.Extensions.Count);
96      for (int i = 0; i < file.Extensions.Count; i++) {
97        Assert.AreEqual(i, file.Extensions[i].Index);
98      }
99    }
100
101    [Test]
102    public void MessageDescriptor() {
103      MessageDescriptor messageType = TestAllTypes.Descriptor;
104      MessageDescriptor nestedType = TestAllTypes.Types.NestedMessage.Descriptor;
105
106      Assert.AreEqual("TestAllTypes", messageType.Name);
107      Assert.AreEqual("protobuf_unittest.TestAllTypes", messageType.FullName);
108      Assert.AreEqual(UnitTestProtoFile.Descriptor, messageType.File);
109      Assert.IsNull(messageType.ContainingType);
110      Assert.AreEqual(DescriptorProtos.MessageOptions.DefaultInstance, messageType.Options);
111      Assert.AreEqual("TestAllTypes", messageType.Proto.Name);
112
113      Assert.AreEqual("NestedMessage", nestedType.Name);
114      Assert.AreEqual("protobuf_unittest.TestAllTypes.NestedMessage", nestedType.FullName);
115      Assert.AreEqual(UnitTestProtoFile.Descriptor, nestedType.File);
116      Assert.AreEqual(messageType, nestedType.ContainingType);
117
118      FieldDescriptor field = messageType.Fields[0];
119      Assert.AreEqual("optional_int32", field.Name);
120      Assert.AreEqual(field, messageType.FindDescriptor<FieldDescriptor>("optional_int32"));
121      Assert.IsNull(messageType.FindDescriptor<FieldDescriptor>("no_such_field"));
122      Assert.AreEqual(field, messageType.FindFieldByNumber(1));
123      Assert.IsNull(messageType.FindFieldByNumber(571283));
124      for (int i = 0; i < messageType.Fields.Count; i++) {
125        Assert.AreEqual(i, messageType.Fields[i].Index);
126      }
127
128      Assert.AreEqual(nestedType, messageType.NestedTypes[0]);
129      Assert.AreEqual(nestedType, messageType.FindDescriptor<MessageDescriptor>("NestedMessage"));
130      Assert.IsNull(messageType.FindDescriptor<MessageDescriptor>("NoSuchType"));
131      for (int i = 0; i < messageType.NestedTypes.Count; i++) {
132        Assert.AreEqual(i, messageType.NestedTypes[i].Index);
133      }
134
135      Assert.AreEqual(messageType.EnumTypes[0], messageType.FindDescriptor<EnumDescriptor>("NestedEnum"));
136      Assert.IsNull(messageType.FindDescriptor<EnumDescriptor>("NoSuchType"));
137      for (int i = 0; i < messageType.EnumTypes.Count; i++) {
138        Assert.AreEqual(i, messageType.EnumTypes[i].Index);
139      }
140    }
141
142    [Test]
143    public void FieldDescriptor() {
144      MessageDescriptor messageType = TestAllTypes.Descriptor;
145      FieldDescriptor primitiveField = messageType.FindDescriptor<FieldDescriptor>("optional_int32");
146      FieldDescriptor enumField = messageType.FindDescriptor<FieldDescriptor>("optional_nested_enum");
147      FieldDescriptor messageField = messageType.FindDescriptor<FieldDescriptor>("optional_foreign_message");
148      FieldDescriptor cordField = messageType.FindDescriptor<FieldDescriptor>("optional_cord");
149      FieldDescriptor extension = UnitTestProtoFile.OptionalInt32Extension.Descriptor;
150      FieldDescriptor nestedExtension = TestRequired.Single.Descriptor;
151
152      Assert.AreEqual("optional_int32", primitiveField.Name);
153      Assert.AreEqual("protobuf_unittest.TestAllTypes.optional_int32",
154                   primitiveField.FullName);
155      Assert.AreEqual(1, primitiveField.FieldNumber);
156      Assert.AreEqual(messageType, primitiveField.ContainingType);
157      Assert.AreEqual(UnitTestProtoFile.Descriptor, primitiveField.File);
158      Assert.AreEqual(FieldType.Int32, primitiveField.FieldType);
159      Assert.AreEqual(MappedType.Int32, primitiveField.MappedType);
160      Assert.AreEqual(DescriptorProtos.FieldOptions.DefaultInstance, primitiveField.Options);
161      Assert.IsFalse(primitiveField.IsExtension);
162      Assert.AreEqual("optional_int32", primitiveField.Proto.Name);
163
164      Assert.AreEqual("optional_nested_enum", enumField.Name);
165      Assert.AreEqual(FieldType.Enum, enumField.FieldType);
166      Assert.AreEqual(MappedType.Enum, enumField.MappedType);
167      // Assert.AreEqual(TestAllTypes.Types.NestedEnum.Descriptor, enumField.EnumType);
168
169      Assert.AreEqual("optional_foreign_message", messageField.Name);
170      Assert.AreEqual(FieldType.Message, messageField.FieldType);
171      Assert.AreEqual(MappedType.Message, messageField.MappedType);
172      Assert.AreEqual(ForeignMessage.Descriptor, messageField.MessageType);
173
174      Assert.AreEqual("optional_cord", cordField.Name);
175      Assert.AreEqual(FieldType.String, cordField.FieldType);
176      Assert.AreEqual(MappedType.String, cordField.MappedType);
177      Assert.AreEqual(DescriptorProtos.FieldOptions.Types.CType.CORD, cordField.Options.Ctype);
178
179      Assert.AreEqual("optional_int32_extension", extension.Name);
180      Assert.AreEqual("protobuf_unittest.optional_int32_extension", extension.FullName);
181      Assert.AreEqual(1, extension.FieldNumber);
182      Assert.AreEqual(TestAllExtensions.Descriptor, extension.ContainingType);
183      Assert.AreEqual(UnitTestProtoFile.Descriptor, extension.File);
184      Assert.AreEqual(FieldType.Int32, extension.FieldType);
185      Assert.AreEqual(MappedType.Int32, extension.MappedType);
186      Assert.AreEqual(DescriptorProtos.FieldOptions.DefaultInstance,
187                   extension.Options);
188      Assert.IsTrue(extension.IsExtension);
189      Assert.AreEqual(null, extension.ExtensionScope);
190      Assert.AreEqual("optional_int32_extension", extension.Proto.Name);
191
192      Assert.AreEqual("single", nestedExtension.Name);
193      Assert.AreEqual("protobuf_unittest.TestRequired.single",
194                   nestedExtension.FullName);
195      Assert.AreEqual(TestRequired.Descriptor,
196                   nestedExtension.ExtensionScope);
197    }
198
199    [Test]
200    public void FieldDescriptorLabel() {
201      FieldDescriptor requiredField =
202        TestRequired.Descriptor.FindDescriptor<FieldDescriptor>("a");
203      FieldDescriptor optionalField =
204        TestAllTypes.Descriptor.FindDescriptor<FieldDescriptor>("optional_int32");
205      FieldDescriptor repeatedField =
206        TestAllTypes.Descriptor.FindDescriptor<FieldDescriptor>("repeated_int32");
207
208      Assert.IsTrue(requiredField.IsRequired);
209      Assert.IsFalse(requiredField.IsRepeated);
210      Assert.IsFalse(optionalField.IsRequired);
211      Assert.IsFalse(optionalField.IsRepeated);
212      Assert.IsFalse(repeatedField.IsRequired);
213      Assert.IsTrue(repeatedField.IsRepeated);
214    }
215
216    [Test]
217    public void FieldDescriptorDefault() {
218      MessageDescriptor d = TestAllTypes.Descriptor;
219      Assert.IsFalse(d.FindDescriptor<FieldDescriptor>("optional_int32").HasDefaultValue);
220      Assert.AreEqual(0, d.FindDescriptor<FieldDescriptor>("optional_int32").DefaultValue);
221      Assert.IsTrue(d.FindDescriptor<FieldDescriptor>("default_int32").HasDefaultValue);
222      Assert.AreEqual(41, d.FindDescriptor<FieldDescriptor>("default_int32").DefaultValue);
223
224      d = TestExtremeDefaultValues.Descriptor;
225      Assert.AreEqual(ByteString.CopyFrom("\u0000\u0001\u0007\b\f\n\r\t\u000b\\\'\"\u00fe", Encoding.GetEncoding(28591)),
226        d.FindDescriptor<FieldDescriptor>("escaped_bytes").DefaultValue);
227      Assert.AreEqual(uint.MaxValue, d.FindDescriptor<FieldDescriptor>("large_uint32").DefaultValue);
228      Assert.AreEqual(ulong.MaxValue, d.FindDescriptor<FieldDescriptor>("large_uint64").DefaultValue);
229    }
230
231    [Test]
232    public void EnumDescriptor()  {
233      // Note: this test is a bit different to the Java version because there's no static way of getting to the descriptor
234      EnumDescriptor enumType = UnitTestProtoFile.Descriptor.FindTypeByName<EnumDescriptor>("ForeignEnum");
235      EnumDescriptor nestedType = TestAllTypes.Descriptor.FindDescriptor<EnumDescriptor>("NestedEnum");
236
237      Assert.AreEqual("ForeignEnum", enumType.Name);
238      Assert.AreEqual("protobuf_unittest.ForeignEnum", enumType.FullName);
239      Assert.AreEqual(UnitTestProtoFile.Descriptor, enumType.File);
240      Assert.IsNull(enumType.ContainingType);
241      Assert.AreEqual(DescriptorProtos.EnumOptions.DefaultInstance,
242                   enumType.Options);
243
244      Assert.AreEqual("NestedEnum", nestedType.Name);
245      Assert.AreEqual("protobuf_unittest.TestAllTypes.NestedEnum",
246                   nestedType.FullName);
247      Assert.AreEqual(UnitTestProtoFile.Descriptor, nestedType.File);
248      Assert.AreEqual(TestAllTypes.Descriptor, nestedType.ContainingType);
249
250      EnumValueDescriptor value = enumType.FindValueByName("FOREIGN_FOO");
251      Assert.AreEqual(value, enumType.Values[0]);
252      Assert.AreEqual("FOREIGN_FOO", value.Name);
253      Assert.AreEqual(4, value.Number);
254      Assert.AreEqual((int) ForeignEnum.FOREIGN_FOO, value.Number);
255      Assert.AreEqual(value, enumType.FindValueByNumber(4));
256      Assert.IsNull(enumType.FindValueByName("NO_SUCH_VALUE"));
257      for (int i = 0; i < enumType.Values.Count; i++) {
258        Assert.AreEqual(i, enumType.Values[i].Index);
259      }
260    }
261
262    [Test]
263    public void ServiceDescriptor() {
264      ServiceDescriptor service = TestService.Descriptor;
265
266      Assert.AreEqual("TestService", service.Name);
267      Assert.AreEqual("protobuf_unittest.TestService", service.FullName);
268      Assert.AreEqual(UnitTestProtoFile.Descriptor, service.File);
269
270      Assert.AreEqual(2, service.Methods.Count);
271
272      MethodDescriptor fooMethod = service.Methods[0];
273      Assert.AreEqual("Foo", fooMethod.Name);
274      Assert.AreEqual(FooRequest.Descriptor, fooMethod.InputType);
275      Assert.AreEqual(FooResponse.Descriptor, fooMethod.OutputType);
276      Assert.AreEqual(fooMethod, service.FindMethodByName("Foo"));
277
278      MethodDescriptor barMethod = service.Methods[1];
279      Assert.AreEqual("Bar", barMethod.Name);
280      Assert.AreEqual(BarRequest.Descriptor, barMethod.InputType);
281      Assert.AreEqual(BarResponse.Descriptor, barMethod.OutputType);
282      Assert.AreEqual(barMethod, service.FindMethodByName("Bar"));
283
284      Assert.IsNull(service.FindMethodByName("NoSuchMethod"));
285
286      for (int i = 0; i < service.Methods.Count; i++) {
287        Assert.AreEqual(i, service.Methods[i].Index);
288      }
289    }
290
291    [Test]
292    public void CustomOptions() {
293      MessageDescriptor descriptor = TestMessageWithCustomOptions.Descriptor;
294      Assert.IsTrue(descriptor.Options.HasExtension(UnitTestCustomOptionsProtoFile.MessageOpt1));
295      Assert.AreEqual(-56, descriptor.Options.GetExtension(UnitTestCustomOptionsProtoFile.MessageOpt1));
296
297
298      FieldDescriptor field = descriptor.FindFieldByName("field1");
299      Assert.IsNotNull(field);
300
301      Assert.IsTrue(field.Options.HasExtension(UnitTestCustomOptionsProtoFile.FieldOpt1));
302      Assert.AreEqual(8765432109L, field.Options.GetExtension(UnitTestCustomOptionsProtoFile.FieldOpt1));
303
304      // TODO: Write out enum descriptors
305      /*
306      EnumDescriptor enumType = TestMessageWithCustomOptions.Types.
307        UnittestCustomOptions.TestMessageWithCustomOptions.AnEnum.getDescriptor();
308
309      Assert.IsTrue(
310        enumType.getOptions().hasExtension(UnittestCustomOptions.enumOpt1));
311      Assert.AreEqual(Integer.valueOf(-789),
312        enumType.getOptions().getExtension(UnittestCustomOptions.enumOpt1));
313        */
314
315      ServiceDescriptor service = TestServiceWithCustomOptions.Descriptor;
316
317      Assert.IsTrue(service.Options.HasExtension(UnitTestCustomOptionsProtoFile.ServiceOpt1));
318      Assert.AreEqual(-9876543210L, service.Options.GetExtension(UnitTestCustomOptionsProtoFile.ServiceOpt1));
319
320      MethodDescriptor method = service.FindMethodByName("Foo");
321      Assert.IsNotNull(method);
322
323      Assert.IsTrue(method.Options.HasExtension(UnitTestCustomOptionsProtoFile.MethodOpt1));
324      Assert.AreEqual(MethodOpt1.METHODOPT1_VAL2, method.Options.GetExtension(UnitTestCustomOptionsProtoFile.MethodOpt1));
325    }
326  }
327}
Note: See TracBrowser for help on using the repository browser.