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 |
|
---|
35 | using System.Collections.Generic;
|
---|
36 | using Google.ProtocolBuffers.DescriptorProtos;
|
---|
37 | using Google.ProtocolBuffers.Descriptors;
|
---|
38 | using NUnit.Framework;
|
---|
39 |
|
---|
40 | namespace Google.ProtocolBuffers.ProtoGen {
|
---|
41 | /// <summary>
|
---|
42 | /// Tests for the dependency resolution in Generator.
|
---|
43 | /// </summary>
|
---|
44 | [TestFixture]
|
---|
45 | public class DependencyResolutionTest {
|
---|
46 |
|
---|
47 | [Test]
|
---|
48 | public void TwoDistinctFiles() {
|
---|
49 | FileDescriptorProto first = new FileDescriptorProto.Builder { Name="First" }.Build();
|
---|
50 | FileDescriptorProto second = new FileDescriptorProto.Builder { Name="Second" }.Build();
|
---|
51 | FileDescriptorSet set = new FileDescriptorSet { FileList = { first, second } };
|
---|
52 |
|
---|
53 | IList<FileDescriptor> converted = Generator.ConvertDescriptors(set);
|
---|
54 | Assert.AreEqual(2, converted.Count);
|
---|
55 | Assert.AreEqual("First", converted[0].Name);
|
---|
56 | Assert.AreEqual(0, converted[0].Dependencies.Count);
|
---|
57 | Assert.AreEqual("Second", converted[1].Name);
|
---|
58 | Assert.AreEqual(0, converted[1].Dependencies.Count);
|
---|
59 | }
|
---|
60 |
|
---|
61 | [Test]
|
---|
62 | public void FirstDependsOnSecond() {
|
---|
63 | FileDescriptorProto first = new FileDescriptorProto.Builder { Name = "First", DependencyList = {"Second"} }.Build();
|
---|
64 | FileDescriptorProto second = new FileDescriptorProto.Builder { Name = "Second" }.Build();
|
---|
65 | FileDescriptorSet set = new FileDescriptorSet { FileList = { first, second } };
|
---|
66 | IList<FileDescriptor> converted = Generator.ConvertDescriptors(set);
|
---|
67 | Assert.AreEqual(2, converted.Count);
|
---|
68 | Assert.AreEqual("First", converted[0].Name);
|
---|
69 | Assert.AreEqual(1, converted[0].Dependencies.Count);
|
---|
70 | Assert.AreEqual(converted[1], converted[0].Dependencies[0]);
|
---|
71 | Assert.AreEqual("Second", converted[1].Name);
|
---|
72 | Assert.AreEqual(0, converted[1].Dependencies.Count);
|
---|
73 | }
|
---|
74 |
|
---|
75 | [Test]
|
---|
76 | public void SecondDependsOnFirst() {
|
---|
77 | FileDescriptorProto first = new FileDescriptorProto.Builder { Name = "First" }.Build();
|
---|
78 | FileDescriptorProto second = new FileDescriptorProto.Builder { Name = "Second", DependencyList = {"First"} }.Build();
|
---|
79 | FileDescriptorSet set = new FileDescriptorSet { FileList = { first, second } };
|
---|
80 | IList<FileDescriptor> converted = Generator.ConvertDescriptors(set);
|
---|
81 | Assert.AreEqual(2, converted.Count);
|
---|
82 | Assert.AreEqual("First", converted[0].Name);
|
---|
83 | Assert.AreEqual(0, converted[0].Dependencies.Count);
|
---|
84 | Assert.AreEqual("Second", converted[1].Name);
|
---|
85 | Assert.AreEqual(1, converted[1].Dependencies.Count);
|
---|
86 | Assert.AreEqual(converted[0], converted[1].Dependencies[0]);
|
---|
87 | }
|
---|
88 |
|
---|
89 | [Test]
|
---|
90 | public void CircularDependency() {
|
---|
91 | FileDescriptorProto first = new FileDescriptorProto.Builder { Name = "First", DependencyList = { "Second" } }.Build();
|
---|
92 | FileDescriptorProto second = new FileDescriptorProto.Builder { Name = "Second", DependencyList = { "First" } }.Build();
|
---|
93 | FileDescriptorSet set = new FileDescriptorSet { FileList = { first, second } };
|
---|
94 | try {
|
---|
95 | Generator.ConvertDescriptors(set);
|
---|
96 | Assert.Fail("Expected exception");
|
---|
97 | } catch (DependencyResolutionException) {
|
---|
98 | // Expected
|
---|
99 | }
|
---|
100 | }
|
---|
101 |
|
---|
102 | [Test]
|
---|
103 | public void MissingDependency() {
|
---|
104 | FileDescriptorProto first = new FileDescriptorProto.Builder { Name = "First", DependencyList = { "Second" } }.Build();
|
---|
105 | FileDescriptorSet set = new FileDescriptorSet { FileList = { first } };
|
---|
106 | try {
|
---|
107 | Generator.ConvertDescriptors(set);
|
---|
108 | Assert.Fail("Expected exception");
|
---|
109 | } catch (DependencyResolutionException) {
|
---|
110 | // Expected
|
---|
111 | }
|
---|
112 | }
|
---|
113 |
|
---|
114 | [Test]
|
---|
115 | public void SelfDependency() {
|
---|
116 | FileDescriptorProto first = new FileDescriptorProto.Builder { Name = "First", DependencyList = { "First" } }.Build();
|
---|
117 | FileDescriptorSet set = new FileDescriptorSet { FileList = { first } };
|
---|
118 | try {
|
---|
119 | Generator.ConvertDescriptors(set);
|
---|
120 | Assert.Fail("Expected exception");
|
---|
121 | } catch (DependencyResolutionException) {
|
---|
122 | // Expected
|
---|
123 | }
|
---|
124 | }
|
---|
125 | }
|
---|
126 | } |
---|