DependencyResolutionTest.cs 4.0 KB

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