FieldAccessTest.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. #region Copyright notice and license
  2. // Protocol Buffers - Google's data interchange format
  3. // Copyright 2015 Google Inc. All rights reserved.
  4. // https://developers.google.com/protocol-buffers/
  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. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. #endregion
  32. using Google.Protobuf.TestProtos;
  33. using Proto2 = Google.Protobuf.TestProtos.Proto2;
  34. using NUnit.Framework;
  35. using System;
  36. using System.Collections;
  37. using System.Collections.Generic;
  38. using static Google.Protobuf.TestProtos.Proto2.UnittestExtensions;
  39. namespace Google.Protobuf.Reflection
  40. {
  41. public class FieldAccessTest
  42. {
  43. [Test]
  44. public void GetValue()
  45. {
  46. var message = SampleMessages.CreateFullTestAllTypes();
  47. var fields = TestProtos.TestAllTypes.Descriptor.Fields;
  48. Assert.AreEqual(message.SingleBool, fields[TestProtos.TestAllTypes.SingleBoolFieldNumber].Accessor.GetValue(message));
  49. Assert.AreEqual(message.SingleBytes, fields[TestProtos.TestAllTypes.SingleBytesFieldNumber].Accessor.GetValue(message));
  50. Assert.AreEqual(message.SingleDouble, fields[TestProtos.TestAllTypes.SingleDoubleFieldNumber].Accessor.GetValue(message));
  51. Assert.AreEqual(message.SingleFixed32, fields[TestProtos.TestAllTypes.SingleFixed32FieldNumber].Accessor.GetValue(message));
  52. Assert.AreEqual(message.SingleFixed64, fields[TestProtos.TestAllTypes.SingleFixed64FieldNumber].Accessor.GetValue(message));
  53. Assert.AreEqual(message.SingleFloat, fields[TestProtos.TestAllTypes.SingleFloatFieldNumber].Accessor.GetValue(message));
  54. Assert.AreEqual(message.SingleForeignEnum, fields[TestProtos.TestAllTypes.SingleForeignEnumFieldNumber].Accessor.GetValue(message));
  55. Assert.AreEqual(message.SingleForeignMessage, fields[TestProtos.TestAllTypes.SingleForeignMessageFieldNumber].Accessor.GetValue(message));
  56. Assert.AreEqual(message.SingleImportEnum, fields[TestProtos.TestAllTypes.SingleImportEnumFieldNumber].Accessor.GetValue(message));
  57. Assert.AreEqual(message.SingleImportMessage, fields[TestProtos.TestAllTypes.SingleImportMessageFieldNumber].Accessor.GetValue(message));
  58. Assert.AreEqual(message.SingleInt32, fields[TestProtos.TestAllTypes.SingleInt32FieldNumber].Accessor.GetValue(message));
  59. Assert.AreEqual(message.SingleInt64, fields[TestProtos.TestAllTypes.SingleInt64FieldNumber].Accessor.GetValue(message));
  60. Assert.AreEqual(message.SingleNestedEnum, fields[TestProtos.TestAllTypes.SingleNestedEnumFieldNumber].Accessor.GetValue(message));
  61. Assert.AreEqual(message.SingleNestedMessage, fields[TestProtos.TestAllTypes.SingleNestedMessageFieldNumber].Accessor.GetValue(message));
  62. Assert.AreEqual(message.SinglePublicImportMessage, fields[TestProtos.TestAllTypes.SinglePublicImportMessageFieldNumber].Accessor.GetValue(message));
  63. Assert.AreEqual(message.SingleSint32, fields[TestProtos.TestAllTypes.SingleSint32FieldNumber].Accessor.GetValue(message));
  64. Assert.AreEqual(message.SingleSint64, fields[TestProtos.TestAllTypes.SingleSint64FieldNumber].Accessor.GetValue(message));
  65. Assert.AreEqual(message.SingleString, fields[TestProtos.TestAllTypes.SingleStringFieldNumber].Accessor.GetValue(message));
  66. Assert.AreEqual(message.SingleSfixed32, fields[TestProtos.TestAllTypes.SingleSfixed32FieldNumber].Accessor.GetValue(message));
  67. Assert.AreEqual(message.SingleSfixed64, fields[TestProtos.TestAllTypes.SingleSfixed64FieldNumber].Accessor.GetValue(message));
  68. Assert.AreEqual(message.SingleUint32, fields[TestProtos.TestAllTypes.SingleUint32FieldNumber].Accessor.GetValue(message));
  69. Assert.AreEqual(message.SingleUint64, fields[TestProtos.TestAllTypes.SingleUint64FieldNumber].Accessor.GetValue(message));
  70. Assert.AreEqual(message.OneofBytes, fields[TestProtos.TestAllTypes.OneofBytesFieldNumber].Accessor.GetValue(message));
  71. Assert.AreEqual(message.OneofString, fields[TestProtos.TestAllTypes.OneofStringFieldNumber].Accessor.GetValue(message));
  72. Assert.AreEqual(message.OneofNestedMessage, fields[TestProtos.TestAllTypes.OneofNestedMessageFieldNumber].Accessor.GetValue(message));
  73. Assert.AreEqual(message.OneofUint32, fields[TestProtos.TestAllTypes.OneofUint32FieldNumber].Accessor.GetValue(message));
  74. // Just one example for repeated fields - they're all just returning the list
  75. var list = (IList) fields[TestProtos.TestAllTypes.RepeatedInt32FieldNumber].Accessor.GetValue(message);
  76. Assert.AreEqual(message.RepeatedInt32, list);
  77. Assert.AreEqual(message.RepeatedInt32[0], list[0]); // Just in case there was any doubt...
  78. // Just a single map field, for the same reason
  79. var mapMessage = new TestMap { MapStringString = { { "key1", "value1" }, { "key2", "value2" } } };
  80. fields = TestMap.Descriptor.Fields;
  81. var dictionary = (IDictionary) fields[TestMap.MapStringStringFieldNumber].Accessor.GetValue(mapMessage);
  82. Assert.AreEqual(mapMessage.MapStringString, dictionary);
  83. Assert.AreEqual("value1", dictionary["key1"]);
  84. }
  85. [Test]
  86. public void GetValue_IncorrectType()
  87. {
  88. IMessage message = SampleMessages.CreateFullTestAllTypes();
  89. var fields = message.Descriptor.Fields;
  90. Assert.Throws<InvalidCastException>(() => fields[TestProtos.TestAllTypes.SingleBoolFieldNumber].Accessor.GetValue(new TestMap()));
  91. }
  92. [Test]
  93. public void HasValue_Proto3()
  94. {
  95. IMessage message = SampleMessages.CreateFullTestAllTypes();
  96. var fields = message.Descriptor.Fields;
  97. Assert.Throws<InvalidOperationException>(() => (fields[TestProtos.TestAllTypes.SingleBoolFieldNumber].Accessor as IFieldPresenceAccessor).HasValue(message));
  98. }
  99. [Test]
  100. public void HasValue()
  101. {
  102. IMessage message = new Proto2.TestAllTypes();
  103. var fields = message.Descriptor.Fields;
  104. var accessor = fields[Proto2.TestAllTypes.OptionalBoolFieldNumber].Accessor as IFieldPresenceAccessor;
  105. Assert.False(accessor.HasValue(message));
  106. accessor.SetValue(message, true);
  107. Assert.True(accessor.HasValue(message));
  108. accessor.Clear(message);
  109. Assert.False(accessor.HasValue(message));
  110. }
  111. [Test]
  112. public void SetValue_SingleFields()
  113. {
  114. // Just a sample (primitives, messages, enums, strings, byte strings)
  115. var message = SampleMessages.CreateFullTestAllTypes();
  116. var fields = TestProtos.TestAllTypes.Descriptor.Fields;
  117. fields[TestProtos.TestAllTypes.SingleBoolFieldNumber].Accessor.SetValue(message, false);
  118. fields[TestProtos.TestAllTypes.SingleInt32FieldNumber].Accessor.SetValue(message, 500);
  119. fields[TestProtos.TestAllTypes.SingleStringFieldNumber].Accessor.SetValue(message, "It's a string");
  120. fields[TestProtos.TestAllTypes.SingleBytesFieldNumber].Accessor.SetValue(message, ByteString.CopyFrom(99, 98, 97));
  121. fields[TestProtos.TestAllTypes.SingleForeignEnumFieldNumber].Accessor.SetValue(message, ForeignEnum.ForeignFoo);
  122. fields[TestProtos.TestAllTypes.SingleForeignMessageFieldNumber].Accessor.SetValue(message, new ForeignMessage { C = 12345 });
  123. fields[TestProtos.TestAllTypes.SingleDoubleFieldNumber].Accessor.SetValue(message, 20150701.5);
  124. var expected = new TestAllTypes(SampleMessages.CreateFullTestAllTypes())
  125. {
  126. SingleBool = false,
  127. SingleInt32 = 500,
  128. SingleString = "It's a string",
  129. SingleBytes = ByteString.CopyFrom(99, 98, 97),
  130. SingleForeignEnum = ForeignEnum.ForeignFoo,
  131. SingleForeignMessage = new ForeignMessage { C = 12345 },
  132. SingleDouble = 20150701.5
  133. };
  134. Assert.AreEqual(expected, message);
  135. }
  136. [Test]
  137. public void SetValue_SingleFields_WrongType()
  138. {
  139. IMessage message = SampleMessages.CreateFullTestAllTypes();
  140. var fields = message.Descriptor.Fields;
  141. Assert.Throws<InvalidCastException>(() => fields[TestProtos.TestAllTypes.SingleBoolFieldNumber].Accessor.SetValue(message, "This isn't a bool"));
  142. }
  143. [Test]
  144. public void SetValue_MapFields()
  145. {
  146. IMessage message = new TestMap();
  147. var fields = message.Descriptor.Fields;
  148. Assert.Throws<InvalidOperationException>(() => fields[TestMap.MapStringStringFieldNumber].Accessor.SetValue(message, new Dictionary<string, string>()));
  149. }
  150. [Test]
  151. public void SetValue_RepeatedFields()
  152. {
  153. IMessage message = SampleMessages.CreateFullTestAllTypes();
  154. var fields = message.Descriptor.Fields;
  155. Assert.Throws<InvalidOperationException>(() => fields[TestProtos.TestAllTypes.RepeatedDoubleFieldNumber].Accessor.SetValue(message, new double[10]));
  156. }
  157. [Test]
  158. public void Oneof()
  159. {
  160. var message = new TestAllTypes();
  161. var descriptor = TestProtos.TestAllTypes.Descriptor;
  162. Assert.AreEqual(1, descriptor.Oneofs.Count);
  163. var oneof = descriptor.Oneofs[0];
  164. Assert.AreEqual("oneof_field", oneof.Name);
  165. Assert.IsNull(oneof.Accessor.GetCaseFieldDescriptor(message));
  166. message.OneofString = "foo";
  167. Assert.AreSame(descriptor.Fields[TestProtos.TestAllTypes.OneofStringFieldNumber], oneof.Accessor.GetCaseFieldDescriptor(message));
  168. message.OneofUint32 = 10;
  169. Assert.AreSame(descriptor.Fields[TestProtos.TestAllTypes.OneofUint32FieldNumber], oneof.Accessor.GetCaseFieldDescriptor(message));
  170. oneof.Accessor.Clear(message);
  171. Assert.AreEqual(TestProtos.TestAllTypes.OneofFieldOneofCase.None, message.OneofFieldCase);
  172. }
  173. [Test]
  174. public void Clear()
  175. {
  176. var message = SampleMessages.CreateFullTestAllTypes();
  177. var fields = TestProtos.TestAllTypes.Descriptor.Fields;
  178. fields[TestProtos.TestAllTypes.SingleBoolFieldNumber].Accessor.Clear(message);
  179. fields[TestProtos.TestAllTypes.SingleInt32FieldNumber].Accessor.Clear(message);
  180. fields[TestProtos.TestAllTypes.SingleStringFieldNumber].Accessor.Clear(message);
  181. fields[TestProtos.TestAllTypes.SingleBytesFieldNumber].Accessor.Clear(message);
  182. fields[TestProtos.TestAllTypes.SingleForeignEnumFieldNumber].Accessor.Clear(message);
  183. fields[TestProtos.TestAllTypes.SingleForeignMessageFieldNumber].Accessor.Clear(message);
  184. fields[TestProtos.TestAllTypes.RepeatedDoubleFieldNumber].Accessor.Clear(message);
  185. var expected = new TestAllTypes(SampleMessages.CreateFullTestAllTypes())
  186. {
  187. SingleBool = false,
  188. SingleInt32 = 0,
  189. SingleString = "",
  190. SingleBytes = ByteString.Empty,
  191. SingleForeignEnum = 0,
  192. SingleForeignMessage = null,
  193. };
  194. expected.RepeatedDouble.Clear();
  195. Assert.AreEqual(expected, message);
  196. // Separately, maps.
  197. var mapMessage = new TestMap { MapStringString = { { "key1", "value1" }, { "key2", "value2" } } };
  198. fields = TestMap.Descriptor.Fields;
  199. fields[TestMap.MapStringStringFieldNumber].Accessor.Clear(mapMessage);
  200. Assert.AreEqual(0, mapMessage.MapStringString.Count);
  201. }
  202. [Test]
  203. public void FieldDescriptor_ByName()
  204. {
  205. var descriptor = TestProtos.TestAllTypes.Descriptor;
  206. Assert.AreSame(
  207. descriptor.Fields[TestProtos.TestAllTypes.SingleBoolFieldNumber],
  208. descriptor.Fields["single_bool"]);
  209. }
  210. [Test]
  211. public void FieldDescriptor_NotFound()
  212. {
  213. var descriptor = TestProtos.TestAllTypes.Descriptor;
  214. Assert.Throws<KeyNotFoundException>(() => descriptor.Fields[999999].ToString());
  215. Assert.Throws<KeyNotFoundException>(() => descriptor.Fields["not found"].ToString());
  216. }
  217. [Test]
  218. public void GetExtensionValue()
  219. {
  220. var message = SampleMessages.CreateFullTestAllExtensions();
  221. // test that the reflector works, since the reflector just runs through IExtendableMessage
  222. Assert.AreEqual(message.GetExtension(OptionalBoolExtension), Proto2.TestAllExtensions.Descriptor.FindFieldByNumber(OptionalBoolExtension.FieldNumber).Accessor.GetValue(message));
  223. }
  224. [Test]
  225. public void GetRepeatedExtensionValue()
  226. {
  227. // check to make sure repeated accessor uses GetOrRegister
  228. var message = new Proto2.TestAllExtensions();
  229. Assert.IsNull(message.GetExtension(RepeatedBoolExtension));
  230. Assert.IsNotNull(Proto2.TestAllExtensions.Descriptor.FindFieldByNumber(RepeatedBoolExtension.FieldNumber).Accessor.GetValue(message));
  231. Assert.IsNotNull(message.GetExtension(RepeatedBoolExtension));
  232. message.ClearExtension(RepeatedBoolExtension);
  233. Assert.IsNull(message.GetExtension(RepeatedBoolExtension));
  234. }
  235. }
  236. }