Jon Skeet | ad74853 | 2009-06-25 16:55:58 +0100 | [diff] [blame^] | 1 | // Protocol Buffers - Google's data interchange format |
| 2 | // Copyright 2008 Google Inc. All rights reserved. |
| 3 | // http://github.com/jskeet/dotnet-protobufs/ |
| 4 | // Original C++/Java/Python code: |
| 5 | // http://code.google.com/p/protobuf/ |
| 6 | // |
| 7 | // Redistribution and use in source and binary forms, with or without |
| 8 | // modification, are permitted provided that the following conditions are |
| 9 | // met: |
| 10 | // |
| 11 | // * Redistributions of source code must retain the above copyright |
| 12 | // notice, this list of conditions and the following disclaimer. |
| 13 | // * Redistributions in binary form must reproduce the above |
| 14 | // copyright notice, this list of conditions and the following disclaimer |
| 15 | // in the documentation and/or other materials provided with the |
| 16 | // distribution. |
| 17 | // * Neither the name of Google Inc. nor the names of its |
| 18 | // contributors may be used to endorse or promote products derived from |
| 19 | // this software without specific prior written permission. |
| 20 | // |
| 21 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 22 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 23 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 24 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 25 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 26 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 27 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 28 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 29 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 30 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 31 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
Jon Skeet | 6803686 | 2008-10-22 13:30:34 +0100 | [diff] [blame] | 32 | using System.Collections.Generic; |
Jon Skeet | 8f8186a | 2009-01-16 10:57:40 +0000 | [diff] [blame] | 33 | using Google.ProtocolBuffers.DescriptorProtos; |
Jon Skeet | 6803686 | 2008-10-22 13:30:34 +0100 | [diff] [blame] | 34 | using Google.ProtocolBuffers.Descriptors; |
| 35 | using NUnit.Framework; |
Jon Skeet | 6803686 | 2008-10-22 13:30:34 +0100 | [diff] [blame] | 36 | |
| 37 | namespace Google.ProtocolBuffers.ProtoGen { |
| 38 | /// <summary> |
| 39 | /// Tests for the dependency resolution in Generator. |
| 40 | /// </summary> |
| 41 | [TestFixture] |
| 42 | public class DependencyResolutionTest { |
| 43 | |
| 44 | [Test] |
| 45 | public void TwoDistinctFiles() { |
| 46 | FileDescriptorProto first = new FileDescriptorProto.Builder { Name="First" }.Build(); |
| 47 | FileDescriptorProto second = new FileDescriptorProto.Builder { Name="Second" }.Build(); |
| 48 | FileDescriptorSet set = new FileDescriptorSet { FileList = { first, second } }; |
| 49 | |
| 50 | IList<FileDescriptor> converted = Generator.ConvertDescriptors(set); |
| 51 | Assert.AreEqual(2, converted.Count); |
| 52 | Assert.AreEqual("First", converted[0].Name); |
| 53 | Assert.AreEqual(0, converted[0].Dependencies.Count); |
| 54 | Assert.AreEqual("Second", converted[1].Name); |
| 55 | Assert.AreEqual(0, converted[1].Dependencies.Count); |
| 56 | } |
| 57 | |
| 58 | [Test] |
| 59 | public void FirstDependsOnSecond() { |
| 60 | FileDescriptorProto first = new FileDescriptorProto.Builder { Name = "First", DependencyList = {"Second"} }.Build(); |
| 61 | FileDescriptorProto second = new FileDescriptorProto.Builder { Name = "Second" }.Build(); |
| 62 | FileDescriptorSet set = new FileDescriptorSet { FileList = { first, second } }; |
| 63 | IList<FileDescriptor> converted = Generator.ConvertDescriptors(set); |
| 64 | Assert.AreEqual(2, converted.Count); |
| 65 | Assert.AreEqual("First", converted[0].Name); |
| 66 | Assert.AreEqual(1, converted[0].Dependencies.Count); |
| 67 | Assert.AreEqual(converted[1], converted[0].Dependencies[0]); |
| 68 | Assert.AreEqual("Second", converted[1].Name); |
| 69 | Assert.AreEqual(0, converted[1].Dependencies.Count); |
| 70 | } |
| 71 | |
| 72 | [Test] |
| 73 | public void SecondDependsOnFirst() { |
| 74 | FileDescriptorProto first = new FileDescriptorProto.Builder { Name = "First" }.Build(); |
| 75 | FileDescriptorProto second = new FileDescriptorProto.Builder { Name = "Second", DependencyList = {"First"} }.Build(); |
| 76 | FileDescriptorSet set = new FileDescriptorSet { FileList = { first, second } }; |
| 77 | IList<FileDescriptor> converted = Generator.ConvertDescriptors(set); |
| 78 | Assert.AreEqual(2, converted.Count); |
| 79 | Assert.AreEqual("First", converted[0].Name); |
| 80 | Assert.AreEqual(0, converted[0].Dependencies.Count); |
| 81 | Assert.AreEqual("Second", converted[1].Name); |
| 82 | Assert.AreEqual(1, converted[1].Dependencies.Count); |
| 83 | Assert.AreEqual(converted[0], converted[1].Dependencies[0]); |
| 84 | } |
| 85 | |
| 86 | [Test] |
| 87 | public void CircularDependency() { |
| 88 | FileDescriptorProto first = new FileDescriptorProto.Builder { Name = "First", DependencyList = { "Second" } }.Build(); |
| 89 | FileDescriptorProto second = new FileDescriptorProto.Builder { Name = "Second", DependencyList = { "First" } }.Build(); |
| 90 | FileDescriptorSet set = new FileDescriptorSet { FileList = { first, second } }; |
| 91 | try { |
| 92 | Generator.ConvertDescriptors(set); |
| 93 | Assert.Fail("Expected exception"); |
| 94 | } catch (DependencyResolutionException) { |
| 95 | // Expected |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | [Test] |
| 100 | public void MissingDependency() { |
| 101 | FileDescriptorProto first = new FileDescriptorProto.Builder { Name = "First", DependencyList = { "Second" } }.Build(); |
| 102 | FileDescriptorSet set = new FileDescriptorSet { FileList = { first } }; |
| 103 | try { |
| 104 | Generator.ConvertDescriptors(set); |
| 105 | Assert.Fail("Expected exception"); |
| 106 | } catch (DependencyResolutionException) { |
| 107 | // Expected |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | [Test] |
| 112 | public void SelfDependency() { |
| 113 | FileDescriptorProto first = new FileDescriptorProto.Builder { Name = "First", DependencyList = { "First" } }.Build(); |
| 114 | FileDescriptorSet set = new FileDescriptorSet { FileList = { first } }; |
| 115 | try { |
| 116 | Generator.ConvertDescriptors(set); |
| 117 | Assert.Fail("Expected exception"); |
| 118 | } catch (DependencyResolutionException) { |
| 119 | // Expected |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | } |