Jon Skeet | 6803686 | 2008-10-22 13:30:34 +0100 | [diff] [blame^] | 1 | using System; |
| 2 | using System.Collections.Generic; |
| 3 | using System.Text; |
| 4 | using Google.ProtocolBuffers.Descriptors; |
| 5 | using NUnit.Framework; |
| 6 | using Google.ProtocolBuffers.DescriptorProtos; |
| 7 | using Google.ProtocolBuffers.ProtoGen; |
| 8 | |
| 9 | namespace Google.ProtocolBuffers.ProtoGen { |
| 10 | /// <summary> |
| 11 | /// Tests for the dependency resolution in Generator. |
| 12 | /// </summary> |
| 13 | [TestFixture] |
| 14 | public class DependencyResolutionTest { |
| 15 | |
| 16 | [Test] |
| 17 | public void TwoDistinctFiles() { |
| 18 | FileDescriptorProto first = new FileDescriptorProto.Builder { Name="First" }.Build(); |
| 19 | FileDescriptorProto second = new FileDescriptorProto.Builder { Name="Second" }.Build(); |
| 20 | FileDescriptorSet set = new FileDescriptorSet { FileList = { first, second } }; |
| 21 | |
| 22 | IList<FileDescriptor> converted = Generator.ConvertDescriptors(set); |
| 23 | Assert.AreEqual(2, converted.Count); |
| 24 | Assert.AreEqual("First", converted[0].Name); |
| 25 | Assert.AreEqual(0, converted[0].Dependencies.Count); |
| 26 | Assert.AreEqual("Second", converted[1].Name); |
| 27 | Assert.AreEqual(0, converted[1].Dependencies.Count); |
| 28 | } |
| 29 | |
| 30 | [Test] |
| 31 | public void FirstDependsOnSecond() { |
| 32 | FileDescriptorProto first = new FileDescriptorProto.Builder { Name = "First", DependencyList = {"Second"} }.Build(); |
| 33 | FileDescriptorProto second = new FileDescriptorProto.Builder { Name = "Second" }.Build(); |
| 34 | FileDescriptorSet set = new FileDescriptorSet { FileList = { first, second } }; |
| 35 | IList<FileDescriptor> converted = Generator.ConvertDescriptors(set); |
| 36 | Assert.AreEqual(2, converted.Count); |
| 37 | Assert.AreEqual("First", converted[0].Name); |
| 38 | Assert.AreEqual(1, converted[0].Dependencies.Count); |
| 39 | Assert.AreEqual(converted[1], converted[0].Dependencies[0]); |
| 40 | Assert.AreEqual("Second", converted[1].Name); |
| 41 | Assert.AreEqual(0, converted[1].Dependencies.Count); |
| 42 | } |
| 43 | |
| 44 | [Test] |
| 45 | public void SecondDependsOnFirst() { |
| 46 | FileDescriptorProto first = new FileDescriptorProto.Builder { Name = "First" }.Build(); |
| 47 | FileDescriptorProto second = new FileDescriptorProto.Builder { Name = "Second", DependencyList = {"First"} }.Build(); |
| 48 | FileDescriptorSet set = new FileDescriptorSet { FileList = { first, second } }; |
| 49 | IList<FileDescriptor> converted = Generator.ConvertDescriptors(set); |
| 50 | Assert.AreEqual(2, converted.Count); |
| 51 | Assert.AreEqual("First", converted[0].Name); |
| 52 | Assert.AreEqual(0, converted[0].Dependencies.Count); |
| 53 | Assert.AreEqual("Second", converted[1].Name); |
| 54 | Assert.AreEqual(1, converted[1].Dependencies.Count); |
| 55 | Assert.AreEqual(converted[0], converted[1].Dependencies[0]); |
| 56 | } |
| 57 | |
| 58 | [Test] |
| 59 | public void CircularDependency() { |
| 60 | FileDescriptorProto first = new FileDescriptorProto.Builder { Name = "First", DependencyList = { "Second" } }.Build(); |
| 61 | FileDescriptorProto second = new FileDescriptorProto.Builder { Name = "Second", DependencyList = { "First" } }.Build(); |
| 62 | FileDescriptorSet set = new FileDescriptorSet { FileList = { first, second } }; |
| 63 | try { |
| 64 | Generator.ConvertDescriptors(set); |
| 65 | Assert.Fail("Expected exception"); |
| 66 | } catch (DependencyResolutionException) { |
| 67 | // Expected |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | [Test] |
| 72 | public void MissingDependency() { |
| 73 | FileDescriptorProto first = new FileDescriptorProto.Builder { Name = "First", DependencyList = { "Second" } }.Build(); |
| 74 | FileDescriptorSet set = new FileDescriptorSet { FileList = { first } }; |
| 75 | try { |
| 76 | Generator.ConvertDescriptors(set); |
| 77 | Assert.Fail("Expected exception"); |
| 78 | } catch (DependencyResolutionException) { |
| 79 | // Expected |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | [Test] |
| 84 | public void SelfDependency() { |
| 85 | FileDescriptorProto first = new FileDescriptorProto.Builder { Name = "First", DependencyList = { "First" } }.Build(); |
| 86 | FileDescriptorSet set = new FileDescriptorSet { FileList = { first } }; |
| 87 | try { |
| 88 | Generator.ConvertDescriptors(set); |
| 89 | Assert.Fail("Expected exception"); |
| 90 | } catch (DependencyResolutionException) { |
| 91 | // Expected |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | } |