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/
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 Google.Protobuf.TestProtos;
34 using Google.Protobuf.WellKnownTypes;
35 using NUnit.Framework;
36 
37 namespace Google.Protobuf.Reflection
38 {
39     public class TypeRegistryTest
40     {
41         // Most of our tests use messages. Simple test that we really can use files...
42         [Test]
CreateWithFileDescriptor()43         public void CreateWithFileDescriptor()
44         {
45             var registry = TypeRegistry.FromFiles(DurationReflection.Descriptor, StructReflection.Descriptor);
46             AssertDescriptorPresent(registry, Duration.Descriptor);
47             AssertDescriptorPresent(registry, ListValue.Descriptor);
48             AssertDescriptorAbsent(registry, Timestamp.Descriptor);
49         }
50 
51         [Test]
TypesFromSameFile()52         public void TypesFromSameFile()
53         {
54             // Just for kicks, let's start with a nested type
55             var registry = TypeRegistry.FromMessages(TestAllTypes.Types.NestedMessage.Descriptor);
56             // Top-level...
57             AssertDescriptorPresent(registry, TestFieldOrderings.Descriptor);
58             // ... and nested (not the same as the original NestedMessage!)
59             AssertDescriptorPresent(registry, TestFieldOrderings.Types.NestedMessage.Descriptor);
60         }
61 
62         [Test]
DependenciesAreIncluded()63         public void DependenciesAreIncluded()
64         {
65             var registry = TypeRegistry.FromMessages(TestAllTypes.Descriptor);
66             // Direct dependencies
67             AssertDescriptorPresent(registry, ImportMessage.Descriptor);
68             // Public dependencies
69             AssertDescriptorPresent(registry, PublicImportMessage.Descriptor);
70         }
71 
72         [Test]
DuplicateFiles()73         public void DuplicateFiles()
74         {
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);
82         }
83 
AssertDescriptorPresent(TypeRegistry registry, MessageDescriptor descriptor)84         private static void AssertDescriptorPresent(TypeRegistry registry, MessageDescriptor descriptor)
85         {
86             Assert.AreSame(descriptor, registry.Find(descriptor.FullName));
87         }
88 
AssertDescriptorAbsent(TypeRegistry registry, MessageDescriptor descriptor)89         private static void AssertDescriptorAbsent(TypeRegistry registry, MessageDescriptor descriptor)
90         {
91             Assert.IsNull(registry.Find(descriptor.FullName));
92         }
93     }
94 }
95