1ffe3c632Sopenharmony_ci#region Copyright notice and license
2ffe3c632Sopenharmony_ci// Protocol Buffers - Google's data interchange format
3ffe3c632Sopenharmony_ci// Copyright 2018 Google Inc.  All rights reserved.
4ffe3c632Sopenharmony_ci// https://developers.google.com/protocol-buffers/
5ffe3c632Sopenharmony_ci//
6ffe3c632Sopenharmony_ci// Redistribution and use in source and binary forms, with or without
7ffe3c632Sopenharmony_ci// modification, are permitted provided that the following conditions are
8ffe3c632Sopenharmony_ci// met:
9ffe3c632Sopenharmony_ci//
10ffe3c632Sopenharmony_ci//     * Redistributions of source code must retain the above copyright
11ffe3c632Sopenharmony_ci// notice, this list of conditions and the following disclaimer.
12ffe3c632Sopenharmony_ci//     * Redistributions in binary form must reproduce the above
13ffe3c632Sopenharmony_ci// copyright notice, this list of conditions and the following disclaimer
14ffe3c632Sopenharmony_ci// in the documentation and/or other materials provided with the
15ffe3c632Sopenharmony_ci// distribution.
16ffe3c632Sopenharmony_ci//     * Neither the name of Google Inc. nor the names of its
17ffe3c632Sopenharmony_ci// contributors may be used to endorse or promote products derived from
18ffe3c632Sopenharmony_ci// this software without specific prior written permission.
19ffe3c632Sopenharmony_ci//
20ffe3c632Sopenharmony_ci// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21ffe3c632Sopenharmony_ci// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22ffe3c632Sopenharmony_ci// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23ffe3c632Sopenharmony_ci// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24ffe3c632Sopenharmony_ci// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25ffe3c632Sopenharmony_ci// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26ffe3c632Sopenharmony_ci// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27ffe3c632Sopenharmony_ci// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28ffe3c632Sopenharmony_ci// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29ffe3c632Sopenharmony_ci// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30ffe3c632Sopenharmony_ci// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31ffe3c632Sopenharmony_ci#endregion
32ffe3c632Sopenharmony_ci
33ffe3c632Sopenharmony_ciusing Google.Protobuf.Reflection;
34ffe3c632Sopenharmony_ciusing NUnit.Framework;
35ffe3c632Sopenharmony_ciusing System.Linq;
36ffe3c632Sopenharmony_ciusing System.Reflection;
37ffe3c632Sopenharmony_ci
38ffe3c632Sopenharmony_cinamespace Google.Protobuf.Test.Reflection
39ffe3c632Sopenharmony_ci{
40ffe3c632Sopenharmony_ci    // In reality this isn't a test for DescriptorDeclaration so much as the way they're loaded.
41ffe3c632Sopenharmony_ci    public class DescriptorDeclarationTest
42ffe3c632Sopenharmony_ci    {
43ffe3c632Sopenharmony_ci        static readonly FileDescriptor unitTestProto3Descriptor = LoadProtos();
44ffe3c632Sopenharmony_ci
45ffe3c632Sopenharmony_ci        // Note: we don't expose a declaration for FileDescriptor as it doesn't have comments
46ffe3c632Sopenharmony_ci        // at the moment and the locations aren't terribly useful.
47ffe3c632Sopenharmony_ci
48ffe3c632Sopenharmony_ci        // The tests for most elements are quite basic: we don't test every aspect of every element.
49ffe3c632Sopenharmony_ci        // The code within the library falls into two categories:
50ffe3c632Sopenharmony_ci        // - Exposing the properties for *any* declaration
51ffe3c632Sopenharmony_ci        // - Finding the right declaration for an element from the descriptor data
52ffe3c632Sopenharmony_ci        // We have a per-element check to make sure we *are* finding the right declaration, and we
53ffe3c632Sopenharmony_ci        // check every property of declarations in at least one test, but we don't have a cross-product.
54ffe3c632Sopenharmony_ci        // That would effectively be testing protoc, which seems redundant here.
55ffe3c632Sopenharmony_ci
56ffe3c632Sopenharmony_ci        [Test]
57ffe3c632Sopenharmony_ci        public void ServiceComments()
58ffe3c632Sopenharmony_ci        {
59ffe3c632Sopenharmony_ci            var service = unitTestProto3Descriptor.FindTypeByName<ServiceDescriptor>("TestService");
60ffe3c632Sopenharmony_ci            Assert.NotNull(service.Declaration);
61ffe3c632Sopenharmony_ci            Assert.AreEqual(" This is a test service\n", service.Declaration.LeadingComments);
62ffe3c632Sopenharmony_ci        }
63ffe3c632Sopenharmony_ci
64ffe3c632Sopenharmony_ci        [Test]
65ffe3c632Sopenharmony_ci        public void MethodComments()
66ffe3c632Sopenharmony_ci        {
67ffe3c632Sopenharmony_ci            var service = unitTestProto3Descriptor.FindTypeByName<ServiceDescriptor>("TestService");
68ffe3c632Sopenharmony_ci            var method = service.FindMethodByName("Foo");
69ffe3c632Sopenharmony_ci            Assert.NotNull(method.Declaration);
70ffe3c632Sopenharmony_ci            Assert.AreEqual(" This is a test method\n", method.Declaration.LeadingComments);
71ffe3c632Sopenharmony_ci        }
72ffe3c632Sopenharmony_ci
73ffe3c632Sopenharmony_ci        [Test]
74ffe3c632Sopenharmony_ci        public void MessageComments()
75ffe3c632Sopenharmony_ci        {
76ffe3c632Sopenharmony_ci            var message = unitTestProto3Descriptor.FindTypeByName<MessageDescriptor>("CommentMessage");
77ffe3c632Sopenharmony_ci            Assert.NotNull(message.Declaration);
78ffe3c632Sopenharmony_ci            Assert.AreEqual(" This is a leading comment\n", message.Declaration.LeadingComments);
79ffe3c632Sopenharmony_ci            Assert.AreEqual(new[] { " This is leading detached comment 1\n", " This is leading detached comment 2\n" },
80ffe3c632Sopenharmony_ci                message.Declaration.LeadingDetachedComments);
81ffe3c632Sopenharmony_ci        }
82ffe3c632Sopenharmony_ci
83ffe3c632Sopenharmony_ci        // Note: this test is somewhat brittle; a change earlier in the proto will break it.
84ffe3c632Sopenharmony_ci        [Test]
85ffe3c632Sopenharmony_ci        public void MessageLocations()
86ffe3c632Sopenharmony_ci        {
87ffe3c632Sopenharmony_ci            var message = unitTestProto3Descriptor.FindTypeByName<MessageDescriptor>("CommentMessage");
88ffe3c632Sopenharmony_ci            Assert.NotNull(message.Declaration);
89ffe3c632Sopenharmony_ci            Assert.AreEqual(389, message.Declaration.StartLine);
90ffe3c632Sopenharmony_ci            Assert.AreEqual(1, message.Declaration.StartColumn);
91ffe3c632Sopenharmony_ci
92ffe3c632Sopenharmony_ci            Assert.AreEqual(404, message.Declaration.EndLine);
93ffe3c632Sopenharmony_ci            Assert.AreEqual(2, message.Declaration.EndColumn);
94ffe3c632Sopenharmony_ci        }
95ffe3c632Sopenharmony_ci
96ffe3c632Sopenharmony_ci        [Test]
97ffe3c632Sopenharmony_ci        public void EnumComments()
98ffe3c632Sopenharmony_ci        {
99ffe3c632Sopenharmony_ci            var descriptor = unitTestProto3Descriptor.FindTypeByName<EnumDescriptor>("CommentEnum");
100ffe3c632Sopenharmony_ci            Assert.NotNull(descriptor.Declaration);
101ffe3c632Sopenharmony_ci            Assert.AreEqual(" Leading enum comment\n", descriptor.Declaration.LeadingComments);
102ffe3c632Sopenharmony_ci        }
103ffe3c632Sopenharmony_ci
104ffe3c632Sopenharmony_ci        [Test]
105ffe3c632Sopenharmony_ci        public void NestedMessageComments()
106ffe3c632Sopenharmony_ci        {
107ffe3c632Sopenharmony_ci            var outer = unitTestProto3Descriptor.FindTypeByName<MessageDescriptor>("CommentMessage");
108ffe3c632Sopenharmony_ci            var nested = outer.FindDescriptor<MessageDescriptor>("NestedCommentMessage");
109ffe3c632Sopenharmony_ci            Assert.NotNull(nested.Declaration);
110ffe3c632Sopenharmony_ci            Assert.AreEqual(" Leading nested message comment\n", nested.Declaration.LeadingComments);
111ffe3c632Sopenharmony_ci        }
112ffe3c632Sopenharmony_ci
113ffe3c632Sopenharmony_ci        [Test]
114ffe3c632Sopenharmony_ci        public void NestedEnumComments()
115ffe3c632Sopenharmony_ci        {
116ffe3c632Sopenharmony_ci            var outer = unitTestProto3Descriptor.FindTypeByName<MessageDescriptor>("CommentMessage");
117ffe3c632Sopenharmony_ci            var nested = outer.FindDescriptor<EnumDescriptor>("NestedCommentEnum");
118ffe3c632Sopenharmony_ci            Assert.NotNull(nested.Declaration);
119ffe3c632Sopenharmony_ci            Assert.AreEqual(" Leading nested enum comment\n", nested.Declaration.LeadingComments);
120ffe3c632Sopenharmony_ci        }
121ffe3c632Sopenharmony_ci
122ffe3c632Sopenharmony_ci        [Test]
123ffe3c632Sopenharmony_ci        public void FieldComments()
124ffe3c632Sopenharmony_ci        {
125ffe3c632Sopenharmony_ci            var message = unitTestProto3Descriptor.FindTypeByName<MessageDescriptor>("CommentMessage");
126ffe3c632Sopenharmony_ci            var field = message.FindFieldByName("text");
127ffe3c632Sopenharmony_ci            Assert.NotNull(field.Declaration);
128ffe3c632Sopenharmony_ci            Assert.AreEqual(" Leading field comment\n", field.Declaration.LeadingComments);
129ffe3c632Sopenharmony_ci            Assert.AreEqual(" Trailing field comment\n", field.Declaration.TrailingComments);
130ffe3c632Sopenharmony_ci        }
131ffe3c632Sopenharmony_ci
132ffe3c632Sopenharmony_ci        [Test]
133ffe3c632Sopenharmony_ci        public void NestedMessageFieldComments()
134ffe3c632Sopenharmony_ci        {
135ffe3c632Sopenharmony_ci            var outer = unitTestProto3Descriptor.FindTypeByName<MessageDescriptor>("CommentMessage");
136ffe3c632Sopenharmony_ci            var nested = outer.FindDescriptor<MessageDescriptor>("NestedCommentMessage");
137ffe3c632Sopenharmony_ci            var field = nested.FindFieldByName("nested_text");
138ffe3c632Sopenharmony_ci            Assert.NotNull(field.Declaration);
139ffe3c632Sopenharmony_ci            Assert.AreEqual(" Leading nested message field comment\n", field.Declaration.LeadingComments);
140ffe3c632Sopenharmony_ci        }
141ffe3c632Sopenharmony_ci
142ffe3c632Sopenharmony_ci        [Test]
143ffe3c632Sopenharmony_ci        public void EnumValueComments()
144ffe3c632Sopenharmony_ci        {
145ffe3c632Sopenharmony_ci            var enumDescriptor = unitTestProto3Descriptor.FindTypeByName<EnumDescriptor>("CommentEnum");
146ffe3c632Sopenharmony_ci            var value = enumDescriptor.FindValueByName("ZERO_VALUE");
147ffe3c632Sopenharmony_ci            Assert.NotNull(value.Declaration);
148ffe3c632Sopenharmony_ci            Assert.AreEqual(" Zero value comment\n", value.Declaration.LeadingComments);
149ffe3c632Sopenharmony_ci        }
150ffe3c632Sopenharmony_ci
151ffe3c632Sopenharmony_ci        [Test]
152ffe3c632Sopenharmony_ci        public void NestedEnumValueComments()
153ffe3c632Sopenharmony_ci        {
154ffe3c632Sopenharmony_ci            var outer = unitTestProto3Descriptor.FindTypeByName<MessageDescriptor>("CommentMessage");
155ffe3c632Sopenharmony_ci            var nested = outer.FindDescriptor<EnumDescriptor>("NestedCommentEnum");
156ffe3c632Sopenharmony_ci            var value = nested.FindValueByName("ZERO_VALUE");
157ffe3c632Sopenharmony_ci            Assert.NotNull(value.Declaration);
158ffe3c632Sopenharmony_ci            Assert.AreEqual(" Zero value comment\n", value.Declaration.LeadingComments);
159ffe3c632Sopenharmony_ci        }
160ffe3c632Sopenharmony_ci
161ffe3c632Sopenharmony_ci        private static FileDescriptor LoadProtos()
162ffe3c632Sopenharmony_ci        {
163ffe3c632Sopenharmony_ci            var type = typeof(DescriptorDeclarationTest);
164ffe3c632Sopenharmony_ci            // TODO: Make this simpler :)
165ffe3c632Sopenharmony_ci            FileDescriptorSet descriptorSet;
166ffe3c632Sopenharmony_ci            using (var stream = type.GetTypeInfo().Assembly.GetManifestResourceStream($"Google.Protobuf.Test.testprotos.pb"))
167ffe3c632Sopenharmony_ci            {
168ffe3c632Sopenharmony_ci                descriptorSet = FileDescriptorSet.Parser.ParseFrom(stream);
169ffe3c632Sopenharmony_ci            }
170ffe3c632Sopenharmony_ci            var byteStrings = descriptorSet.File.Select(f => f.ToByteString()).ToList();
171ffe3c632Sopenharmony_ci            var descriptors = FileDescriptor.BuildFromByteStrings(byteStrings);
172ffe3c632Sopenharmony_ci            return descriptors.Single(d => d.Name == "unittest_proto3.proto");
173ffe3c632Sopenharmony_ci        }
174ffe3c632Sopenharmony_ci    }
175ffe3c632Sopenharmony_ci}
176