1 #region Copyright notice and license
2 // Protocol Buffers - Google's data interchange format
3 // Copyright 2015 Google Inc. All rights reserved.
4 // https://developers.google.com/protocol-buffers/
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions are
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
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.
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.
33 using Google.Protobuf.TestProtos;
34 using Google.Protobuf.WellKnownTypes;
35 using NUnit.Framework;
37 namespace Google.Protobuf.Reflection
39 public class TypeRegistryTest
41 // Most of our tests use messages. Simple test that we really can use files...
43 public void CreateWithFileDescriptor()
45 var registry = TypeRegistry.FromFiles(DurationReflection.Descriptor, StructReflection.Descriptor);
46 AssertDescriptorPresent(registry, Duration.Descriptor);
47 AssertDescriptorPresent(registry, ListValue.Descriptor);
48 AssertDescriptorAbsent(registry, Timestamp.Descriptor);
52 public void TypesFromSameFile()
54 // Just for kicks, let's start with a nested type
55 var registry = TypeRegistry.FromMessages(TestAllTypes.Types.NestedMessage.Descriptor);
57 AssertDescriptorPresent(registry, TestFieldOrderings.Descriptor);
58 // ... and nested (not the same as the original NestedMessage!)
59 AssertDescriptorPresent(registry, TestFieldOrderings.Types.NestedMessage.Descriptor);
63 public void DependenciesAreIncluded()
65 var registry = TypeRegistry.FromMessages(TestAllTypes.Descriptor);
66 // Direct dependencies
67 AssertDescriptorPresent(registry, ImportMessage.Descriptor);
68 // Public dependencies
69 AssertDescriptorPresent(registry, PublicImportMessage.Descriptor);
73 public void DuplicateFiles()
75 // Duplicates via dependencies and simply via repetition
76 var registry = TypeRegistry.FromFiles(
77 UnittestProto3Reflection.Descriptor, UnittestImportProto3Reflection.Descriptor,
78 TimestampReflection.Descriptor, TimestampReflection.Descriptor);
79 AssertDescriptorPresent(registry, TestAllTypes.Descriptor);
80 AssertDescriptorPresent(registry, ImportMessage.Descriptor);
81 AssertDescriptorPresent(registry, Timestamp.Descriptor);
84 private static void AssertDescriptorPresent(TypeRegistry registry, MessageDescriptor descriptor)
86 Assert.AreSame(descriptor, registry.Find(descriptor.FullName));
89 private static void AssertDescriptorAbsent(TypeRegistry registry, MessageDescriptor descriptor)
91 Assert.IsNull(registry.Find(descriptor.FullName));