DependencyResolutionTest.cs 6.5 KB

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