FieldPResenceTest.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. #region Copyright notice and license
  2. // Protocol Buffers - Google's data interchange format
  3. // Copyright 2015 Google Inc. All rights reserved.
  4. // Author: jieluo@google.com (Jie Luo)
  5. //
  6. // Redistribution and use in source and binary forms, with or without
  7. // modification, are permitted provided that the following conditions are
  8. // met:
  9. //
  10. // * Redistributions of source code must retain the above copyright
  11. // notice, this list of conditions and the following disclaimer.
  12. // * Redistributions in binary form must reproduce the above
  13. // copyright notice, this list of conditions and the following disclaimer
  14. // in the documentation and/or other materials provided with the
  15. // distribution.
  16. // * Neither the name of Google Inc. nor the names of its
  17. // contributors may be used to endorse or promote products derived from
  18. // this software without specific prior written permission.
  19. //
  20. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. #endregion
  31. using System;
  32. using System.Reflection;
  33. using System.Collections.Generic;
  34. using Google.ProtocolBuffers.Descriptors;
  35. using Google.ProtocolBuffers.TestProtos;
  36. using Microsoft.VisualStudio.TestTools.UnitTesting;
  37. namespace Google.ProtocolBuffers
  38. {
  39. [TestClass]
  40. class FieldPresenceTest
  41. {
  42. private void CheckHasMethodRemoved(Type proto2Type, Type proto3Type, string name)
  43. {
  44. Assert.NotNull(proto2Type.GetProperty(name));
  45. Assert.NotNull(proto2Type.GetProperty("Has" + name));
  46. Assert.NotNull(proto3Type.GetProperty(name));
  47. Assert.Null(proto3Type.GetProperty("Has" + name));
  48. }
  49. [TestMethod]
  50. public void TestHasMethod()
  51. {
  52. // Optional non-message fields don't have HasFoo method generated
  53. Type proto2Type = typeof(TestAllTypes);
  54. Type proto3Type = typeof(field_presence_test.TestAllTypes);
  55. CheckHasMethodRemoved(proto2Type, proto3Type, "OptionalInt32");
  56. CheckHasMethodRemoved(proto2Type, proto3Type, "OptionalString");
  57. CheckHasMethodRemoved(proto2Type, proto3Type, "OptionalBytes");
  58. CheckHasMethodRemoved(proto2Type, proto3Type, "OptionalNestedEnum");
  59. proto2Type = typeof(TestAllTypes.Builder);
  60. proto3Type = typeof(field_presence_test.TestAllTypes.Builder);
  61. CheckHasMethodRemoved(proto2Type, proto3Type, "OptionalInt32");
  62. CheckHasMethodRemoved(proto2Type, proto3Type, "OptionalString");
  63. CheckHasMethodRemoved(proto2Type, proto3Type, "OptionalBytes");
  64. CheckHasMethodRemoved(proto2Type, proto3Type, "OptionalNestedEnum");
  65. // message fields still have the HasFoo method generated
  66. Assert.False(field_presence_test.TestAllTypes.CreateBuilder().Build().HasOptionalNestedMessage);
  67. Assert.False(field_presence_test.TestAllTypes.CreateBuilder().HasOptionalNestedMessage);
  68. }
  69. [TestMethod]
  70. public void TestFieldPresence()
  71. {
  72. // Optional non-message fields set to their default value are treated the same
  73. // way as not set.
  74. // Serialization will ignore such fields.
  75. field_presence_test.TestAllTypes.Builder builder = field_presence_test.TestAllTypes.CreateBuilder();
  76. builder.SetOptionalInt32(0);
  77. builder.SetOptionalString("");
  78. builder.SetOptionalBytes(ByteString.Empty);
  79. builder.SetOptionalNestedEnum(field_presence_test.TestAllTypes.Types.NestedEnum.FOO);
  80. field_presence_test.TestAllTypes message = builder.Build();
  81. Assert.AreEqual(0, message.SerializedSize);
  82. // Test merge
  83. field_presence_test.TestAllTypes.Builder a = field_presence_test.TestAllTypes.CreateBuilder();
  84. a.SetOptionalInt32(1);
  85. a.SetOptionalString("x");
  86. a.SetOptionalBytes(ByteString.CopyFromUtf8("y"));
  87. a.SetOptionalNestedEnum(field_presence_test.TestAllTypes.Types.NestedEnum.BAR);
  88. a.MergeFrom(message);
  89. field_presence_test.TestAllTypes messageA = a.Build();
  90. Assert.AreEqual(1, messageA.OptionalInt32);
  91. Assert.AreEqual("x", messageA.OptionalString);
  92. Assert.AreEqual(ByteString.CopyFromUtf8("y"), messageA.OptionalBytes);
  93. Assert.AreEqual(field_presence_test.TestAllTypes.Types.NestedEnum.BAR, messageA.OptionalNestedEnum);
  94. // equals/hashCode should produce the same results
  95. field_presence_test.TestAllTypes empty = field_presence_test.TestAllTypes.CreateBuilder().Build();
  96. Assert.True(empty.Equals(message));
  97. Assert.True(message.Equals(empty));
  98. Assert.AreEqual(empty.GetHashCode(), message.GetHashCode());
  99. }
  100. [TestMethod]
  101. public void TestFieldPresenceReflection()
  102. {
  103. MessageDescriptor descriptor = field_presence_test.TestAllTypes.Descriptor;
  104. FieldDescriptor optionalInt32Field = descriptor.FindFieldByName("optional_int32");
  105. FieldDescriptor optionalStringField = descriptor.FindFieldByName("optional_string");
  106. FieldDescriptor optionalBytesField = descriptor.FindFieldByName("optional_bytes");
  107. FieldDescriptor optionalNestedEnumField = descriptor.FindFieldByName("optional_nested_enum");
  108. field_presence_test.TestAllTypes message = field_presence_test.TestAllTypes.CreateBuilder().Build();
  109. Assert.False(message.HasField(optionalInt32Field));
  110. Assert.False(message.HasField(optionalStringField));
  111. Assert.False(message.HasField(optionalBytesField));
  112. Assert.False(message.HasField(optionalNestedEnumField));
  113. // Set to default value is seen as not present
  114. message = field_presence_test.TestAllTypes.CreateBuilder()
  115. .SetOptionalInt32(0)
  116. .SetOptionalString("")
  117. .SetOptionalBytes(ByteString.Empty)
  118. .SetOptionalNestedEnum(field_presence_test.TestAllTypes.Types.NestedEnum.FOO)
  119. .Build();
  120. Assert.False(message.HasField(optionalInt32Field));
  121. Assert.False(message.HasField(optionalStringField));
  122. Assert.False(message.HasField(optionalBytesField));
  123. Assert.False(message.HasField(optionalNestedEnumField));
  124. Assert.AreEqual(0, message.AllFields.Count);
  125. // Set t0 non-defalut value is seen as present
  126. message = field_presence_test.TestAllTypes.CreateBuilder()
  127. .SetOptionalInt32(1)
  128. .SetOptionalString("x")
  129. .SetOptionalBytes(ByteString.CopyFromUtf8("y"))
  130. .SetOptionalNestedEnum(field_presence_test.TestAllTypes.Types.NestedEnum.BAR)
  131. .Build();
  132. Assert.True(message.HasField(optionalInt32Field));
  133. Assert.True(message.HasField(optionalStringField));
  134. Assert.True(message.HasField(optionalBytesField));
  135. Assert.True(message.HasField(optionalNestedEnumField));
  136. Assert.AreEqual(4, message.AllFields.Count);
  137. }
  138. [TestMethod]
  139. public void TestMessageField()
  140. {
  141. field_presence_test.TestAllTypes.Builder builder = field_presence_test.TestAllTypes.CreateBuilder();
  142. Assert.False(builder.HasOptionalNestedMessage);
  143. Assert.False(builder.Build().HasOptionalNestedMessage);
  144. // Unlike non-message fields, if we set default value to message field, the field
  145. // shoule be seem as present.
  146. builder.SetOptionalNestedMessage(field_presence_test.TestAllTypes.Types.NestedMessage.DefaultInstance);
  147. Assert.True(builder.HasOptionalNestedMessage);
  148. Assert.True(builder.Build().HasOptionalNestedMessage);
  149. }
  150. [TestMethod]
  151. public void TestSeralizeAndParese()
  152. {
  153. field_presence_test.TestAllTypes.Builder builder = field_presence_test.TestAllTypes.CreateBuilder();
  154. builder.SetOptionalInt32(1234);
  155. builder.SetOptionalString("hello");
  156. builder.SetOptionalNestedMessage(field_presence_test.TestAllTypes.Types.NestedMessage.DefaultInstance);
  157. ByteString data = builder.Build().ToByteString();
  158. field_presence_test.TestAllTypes message = field_presence_test.TestAllTypes.ParseFrom(data);
  159. Assert.AreEqual(1234, message.OptionalInt32);
  160. Assert.AreEqual("hello", message.OptionalString);
  161. Assert.AreEqual(ByteString.Empty, message.OptionalBytes);
  162. Assert.AreEqual(field_presence_test.TestAllTypes.Types.NestedEnum.FOO, message.OptionalNestedEnum);
  163. Assert.True(message.HasOptionalNestedMessage);
  164. Assert.AreEqual(0, message.OptionalNestedMessage.Value);
  165. }
  166. }
  167. }