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