InteropLiteTest.cs 7.1 KB

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