InteropLiteTest.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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;
  35. using System.Collections.Generic;
  36. using Google.ProtocolBuffers;
  37. using Google.ProtocolBuffers.TestProtos;
  38. using NUnit.Framework;
  39. namespace Google.ProtocolBuffers
  40. {
  41. [TestFixture]
  42. public class InteropLiteTest
  43. {
  44. [Test]
  45. public void TestConvertFromFullMinimal()
  46. {
  47. TestInteropPerson person = TestInteropPerson.CreateBuilder()
  48. .SetId(123)
  49. .SetName("abc")
  50. .Build();
  51. Assert.IsTrue(person.IsInitialized);
  52. TestInteropPersonLite copy = TestInteropPersonLite.ParseFrom(person.ToByteArray());
  53. Assert.AreEqual(person.ToByteArray(), copy.ToByteArray());
  54. }
  55. [Test]
  56. public void TestConvertFromFullComplete()
  57. {
  58. TestInteropPerson person = TestInteropPerson.CreateBuilder()
  59. .SetId(123)
  60. .SetName("abc")
  61. .SetEmail("abc@123.com")
  62. .AddRangeCodes(new[] {1, 2, 3})
  63. .AddPhone(TestInteropPerson.Types.PhoneNumber.CreateBuilder().SetNumber("555-1234").Build())
  64. .AddPhone(TestInteropPerson.Types.PhoneNumber.CreateBuilder().SetNumber("555-5678").Build())
  65. .AddAddresses(
  66. TestInteropPerson.Types.Addresses.CreateBuilder().SetAddress("123 Seseme").SetCity("Wonderland").
  67. SetState("NA").SetZip(12345).Build())
  68. .SetExtension(UnitTestExtrasFullProtoFile.EmployeeId,
  69. TestInteropEmployeeId.CreateBuilder().SetNumber("123").Build())
  70. .Build();
  71. Assert.IsTrue(person.IsInitialized);
  72. ExtensionRegistry registry = ExtensionRegistry.CreateInstance();
  73. UnitTestExtrasLiteProtoFile.RegisterAllExtensions(registry);
  74. TestInteropPersonLite copy = TestInteropPersonLite.ParseFrom(person.ToByteArray(), registry);
  75. Assert.AreEqual(person.ToByteArray(), copy.ToByteArray());
  76. }
  77. [Test]
  78. public void TestConvertFromLiteMinimal()
  79. {
  80. TestInteropPersonLite person = TestInteropPersonLite.CreateBuilder()
  81. .SetId(123)
  82. .SetName("abc")
  83. .Build();
  84. Assert.IsTrue(person.IsInitialized);
  85. TestInteropPerson copy = TestInteropPerson.ParseFrom(person.ToByteArray());
  86. Assert.AreEqual(person.ToByteArray(), copy.ToByteArray());
  87. }
  88. [Test]
  89. public void TestConvertFromLiteComplete()
  90. {
  91. TestInteropPersonLite person = TestInteropPersonLite.CreateBuilder()
  92. .SetId(123)
  93. .SetName("abc")
  94. .SetEmail("abc@123.com")
  95. .AddRangeCodes(new[] {1, 2, 3})
  96. .AddPhone(TestInteropPersonLite.Types.PhoneNumber.CreateBuilder().SetNumber("555-1234").Build())
  97. .AddPhone(TestInteropPersonLite.Types.PhoneNumber.CreateBuilder().SetNumber("555-5678").Build())
  98. .AddAddresses(
  99. TestInteropPersonLite.Types.Addresses.CreateBuilder().SetAddress("123 Seseme").SetCity("Wonderland")
  100. .SetState("NA").SetZip(12345).Build())
  101. .SetExtension(UnitTestExtrasLiteProtoFile.EmployeeIdLite,
  102. TestInteropEmployeeIdLite.CreateBuilder().SetNumber("123").Build())
  103. .Build();
  104. Assert.IsTrue(person.IsInitialized);
  105. ExtensionRegistry registry = ExtensionRegistry.CreateInstance();
  106. UnitTestExtrasFullProtoFile.RegisterAllExtensions(registry);
  107. TestInteropPerson copy = TestInteropPerson.ParseFrom(person.ToByteArray(), registry);
  108. Assert.AreEqual(person.ToByteArray(), copy.ToByteArray());
  109. }
  110. public ByteString AllBytes
  111. {
  112. get
  113. {
  114. byte[] bytes = new byte[256];
  115. for (int i = 0; i < bytes.Length; i++)
  116. bytes[i] = (byte) i;
  117. return ByteString.CopyFrom(bytes);
  118. }
  119. }
  120. [Test]
  121. public void TestCompareStringValues()
  122. {
  123. TestInteropPersonLite person = TestInteropPersonLite.CreateBuilder()
  124. .SetId(123)
  125. .SetName("abc")
  126. .SetEmail("abc@123.com")
  127. .AddRangeCodes(new[] {1, 2, 3})
  128. .AddPhone(TestInteropPersonLite.Types.PhoneNumber.CreateBuilder().SetNumber("555-1234").Build())
  129. .AddPhone(
  130. TestInteropPersonLite.Types.PhoneNumber.CreateBuilder().SetNumber(
  131. System.Text.Encoding.ASCII.GetString(AllBytes.ToByteArray())).Build())
  132. .AddAddresses(
  133. TestInteropPersonLite.Types.Addresses.CreateBuilder().SetAddress("123 Seseme").SetCity("Wonderland")
  134. .SetState("NA").SetZip(12345).Build())
  135. .SetExtension(UnitTestExtrasLiteProtoFile.EmployeeIdLite,
  136. TestInteropEmployeeIdLite.CreateBuilder().SetNumber("123").Build())
  137. .Build();
  138. Assert.IsTrue(person.IsInitialized);
  139. ExtensionRegistry registry = ExtensionRegistry.CreateInstance();
  140. UnitTestExtrasFullProtoFile.RegisterAllExtensions(registry);
  141. TestInteropPerson copy = TestInteropPerson.ParseFrom(person.ToByteArray(), registry);
  142. Assert.AreEqual(person.ToByteArray(), copy.ToByteArray());
  143. TestInteropPerson.Builder copyBuilder = TestInteropPerson.CreateBuilder();
  144. TextFormat.Merge(
  145. person.ToString().Replace("[protobuf_unittest_extra.employee_id_lite]",
  146. "[protobuf_unittest_extra.employee_id]"), registry, copyBuilder);
  147. copy = copyBuilder.Build();
  148. Assert.AreEqual(person.ToByteArray(), copy.ToByteArray());
  149. string liteText = person.ToString().TrimEnd().Replace("\r", "");
  150. string fullText = copy.ToString().TrimEnd().Replace("\r", "");
  151. //map the extension type
  152. liteText = liteText.Replace("[protobuf_unittest_extra.employee_id_lite]",
  153. "[protobuf_unittest_extra.employee_id]");
  154. //lite version does not indent
  155. while (fullText.IndexOf("\n ") >= 0)
  156. fullText = fullText.Replace("\n ", "\n");
  157. Assert.AreEqual(fullText, liteText);
  158. }
  159. }
  160. }