SampleMessages.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 System;
  33. using Google.Protobuf.TestProtos;
  34. using Proto2 = Google.Protobuf.TestProtos.Proto2;
  35. using static Google.Protobuf.TestProtos.Proto2.UnittestExtensions;
  36. namespace Google.Protobuf
  37. {
  38. /// <summary>
  39. /// Helper methods to create sample instances of types generated from unit test messages.
  40. /// </summary>
  41. public class SampleMessages
  42. {
  43. /// <summary>
  44. /// Creates a new sample TestAllTypes message with all fields populated.
  45. /// The "oneof" field is populated with the string property (OneofString).
  46. /// </summary>
  47. public static TestAllTypes CreateFullTestAllTypes()
  48. {
  49. return new TestAllTypes
  50. {
  51. SingleBool = true,
  52. SingleBytes = ByteString.CopyFrom(1, 2, 3, 4),
  53. SingleDouble = 23.5,
  54. SingleFixed32 = 23,
  55. SingleFixed64 = 1234567890123,
  56. SingleFloat = 12.25f,
  57. SingleForeignEnum = ForeignEnum.ForeignBar,
  58. SingleForeignMessage = new ForeignMessage { C = 10 },
  59. SingleImportEnum = ImportEnum.ImportBaz,
  60. SingleImportMessage = new ImportMessage { D = 20 },
  61. SingleInt32 = 100,
  62. SingleInt64 = 3210987654321,
  63. SingleNestedEnum = TestProtos.TestAllTypes.Types.NestedEnum.Foo,
  64. SingleNestedMessage = new TestAllTypes.Types.NestedMessage { Bb = 35 },
  65. SinglePublicImportMessage = new PublicImportMessage { E = 54 },
  66. SingleSfixed32 = -123,
  67. SingleSfixed64 = -12345678901234,
  68. SingleSint32 = -456,
  69. SingleSint64 = -12345678901235,
  70. SingleString = "test",
  71. SingleUint32 = UInt32.MaxValue,
  72. SingleUint64 = UInt64.MaxValue,
  73. RepeatedBool = { true, false },
  74. RepeatedBytes = { ByteString.CopyFrom(1, 2, 3, 4), ByteString.CopyFrom(5, 6), ByteString.CopyFrom(new byte[1000]) },
  75. RepeatedDouble = { -12.25, 23.5 },
  76. RepeatedFixed32 = { UInt32.MaxValue, 23 },
  77. RepeatedFixed64 = { UInt64.MaxValue, 1234567890123 },
  78. RepeatedFloat = { 100f, 12.25f },
  79. RepeatedForeignEnum = { ForeignEnum.ForeignFoo, ForeignEnum.ForeignBar },
  80. RepeatedForeignMessage = { new ForeignMessage(), new ForeignMessage { C = 10 } },
  81. RepeatedImportEnum = { ImportEnum.ImportBaz, ImportEnum.Unspecified },
  82. RepeatedImportMessage = { new ImportMessage { D = 20 }, new ImportMessage { D = 25 } },
  83. RepeatedInt32 = { 100, 200 },
  84. RepeatedInt64 = { 3210987654321, Int64.MaxValue },
  85. RepeatedNestedEnum = { TestProtos.TestAllTypes.Types.NestedEnum.Foo, TestProtos.TestAllTypes.Types.NestedEnum.Neg },
  86. RepeatedNestedMessage = { new TestAllTypes.Types.NestedMessage { Bb = 35 }, new TestAllTypes.Types.NestedMessage { Bb = 10 } },
  87. RepeatedPublicImportMessage = { new PublicImportMessage { E = 54 }, new PublicImportMessage { E = -1 } },
  88. RepeatedSfixed32 = { -123, 123 },
  89. RepeatedSfixed64 = { -12345678901234, 12345678901234 },
  90. RepeatedSint32 = { -456, 100 },
  91. RepeatedSint64 = { -12345678901235, 123 },
  92. RepeatedString = { "foo", "bar" },
  93. RepeatedUint32 = { UInt32.MaxValue, UInt32.MinValue },
  94. RepeatedUint64 = { UInt64.MaxValue, UInt32.MinValue },
  95. OneofString = "Oneof string"
  96. };
  97. }
  98. public static Proto2.TestAllTypes CreateFullTestAllTypesProto2()
  99. {
  100. return new Proto2.TestAllTypes
  101. {
  102. OptionalBool = true,
  103. OptionalBytes = ByteString.CopyFrom(1, 2, 3, 4),
  104. OptionalDouble = 23.5,
  105. OptionalFixed32 = 23,
  106. OptionalFixed64 = 1234567890123,
  107. OptionalFloat = 12.25f,
  108. OptionalForeignEnum = Proto2.ForeignEnum.ForeignBar,
  109. OptionalForeignMessage = new Proto2.ForeignMessage { C = 10 },
  110. OptionalImportEnum = Proto2.ImportEnum.ImportBaz,
  111. OptionalImportMessage = new Proto2.ImportMessage { D = 20 },
  112. OptionalInt32 = 100,
  113. OptionalInt64 = 3210987654321,
  114. OptionalNestedEnum = Proto2.TestAllTypes.Types.NestedEnum.Foo,
  115. OptionalNestedMessage = new Proto2.TestAllTypes.Types.NestedMessage { Bb = 35 },
  116. OptionalPublicImportMessage = new Proto2.PublicImportMessage { E = 54 },
  117. OptionalSfixed32 = -123,
  118. OptionalSfixed64 = -12345678901234,
  119. OptionalSint32 = -456,
  120. OptionalSint64 = -12345678901235,
  121. OptionalString = "test",
  122. OptionalUint32 = UInt32.MaxValue,
  123. OptionalUint64 = UInt64.MaxValue,
  124. OptionalGroup = new Proto2.TestAllTypes.Types.OptionalGroup { A = 10 },
  125. RepeatedBool = { true, false },
  126. RepeatedBytes = { ByteString.CopyFrom(1, 2, 3, 4), ByteString.CopyFrom(5, 6), ByteString.CopyFrom(new byte[1000]) },
  127. RepeatedDouble = { -12.25, 23.5 },
  128. RepeatedFixed32 = { UInt32.MaxValue, 23 },
  129. RepeatedFixed64 = { UInt64.MaxValue, 1234567890123 },
  130. RepeatedFloat = { 100f, 12.25f },
  131. RepeatedForeignEnum = { Proto2.ForeignEnum.ForeignFoo, Proto2.ForeignEnum.ForeignBar },
  132. RepeatedForeignMessage = { new Proto2.ForeignMessage(), new Proto2.ForeignMessage { C = 10 } },
  133. RepeatedImportEnum = { Proto2.ImportEnum.ImportBaz, Proto2.ImportEnum.ImportFoo },
  134. RepeatedImportMessage = { new Proto2.ImportMessage { D = 20 }, new Proto2.ImportMessage { D = 25 } },
  135. RepeatedInt32 = { 100, 200 },
  136. RepeatedInt64 = { 3210987654321, Int64.MaxValue },
  137. RepeatedNestedEnum = { Proto2.TestAllTypes.Types.NestedEnum.Foo, Proto2.TestAllTypes.Types.NestedEnum.Neg },
  138. RepeatedNestedMessage = { new Proto2.TestAllTypes.Types.NestedMessage { Bb = 35 }, new Proto2.TestAllTypes.Types.NestedMessage { Bb = 10 } },
  139. RepeatedSfixed32 = { -123, 123 },
  140. RepeatedSfixed64 = { -12345678901234, 12345678901234 },
  141. RepeatedSint32 = { -456, 100 },
  142. RepeatedSint64 = { -12345678901235, 123 },
  143. RepeatedString = { "foo", "bar" },
  144. RepeatedUint32 = { UInt32.MaxValue, UInt32.MinValue },
  145. RepeatedUint64 = { UInt64.MaxValue, UInt32.MinValue },
  146. RepeatedGroup = { new Proto2.TestAllTypes.Types.RepeatedGroup { A = 10 }, new Proto2.TestAllTypes.Types.RepeatedGroup { A = 20 } },
  147. OneofString = "Oneof string"
  148. };
  149. }
  150. public static Proto2.TestAllExtensions CreateFullTestAllExtensions()
  151. {
  152. var message = new Proto2.TestAllExtensions();
  153. message.SetExtension(OptionalBoolExtension, true);
  154. message.SetExtension(OptionalBytesExtension, ByteString.CopyFrom(1, 2, 3, 4));
  155. message.SetExtension(OptionalDoubleExtension, 23.5);
  156. message.SetExtension(OptionalFixed32Extension, 23u);
  157. message.SetExtension(OptionalFixed64Extension, 1234567890123u);
  158. message.SetExtension(OptionalFloatExtension, 12.25f);
  159. message.SetExtension(OptionalForeignEnumExtension, Proto2.ForeignEnum.ForeignBar);
  160. message.SetExtension(OptionalForeignMessageExtension, new Proto2.ForeignMessage { C = 10 });
  161. message.SetExtension(OptionalImportEnumExtension, Proto2.ImportEnum.ImportBaz);
  162. message.SetExtension(OptionalImportMessageExtension, new Proto2.ImportMessage { D = 20 });
  163. message.SetExtension(OptionalInt32Extension, 100);
  164. message.SetExtension(OptionalInt64Extension, 3210987654321);
  165. message.SetExtension(OptionalNestedEnumExtension, Proto2.TestAllTypes.Types.NestedEnum.Foo);
  166. message.SetExtension(OptionalNestedMessageExtension, new Proto2.TestAllTypes.Types.NestedMessage { Bb = 35 });
  167. message.SetExtension(OptionalPublicImportMessageExtension, new Proto2.PublicImportMessage { E = 54 });
  168. message.SetExtension(OptionalSfixed32Extension, -123);
  169. message.SetExtension(OptionalSfixed64Extension, -12345678901234);
  170. message.SetExtension(OptionalSint32Extension, -456);
  171. message.SetExtension(OptionalSint64Extension, -12345678901235);
  172. message.SetExtension(OptionalStringExtension, "test");
  173. message.SetExtension(OptionalUint32Extension, UInt32.MaxValue);
  174. message.SetExtension(OptionalUint64Extension, UInt64.MaxValue);
  175. message.SetExtension(OptionalGroupExtension, new Proto2.OptionalGroup_extension { A = 10 });
  176. message.GetOrInitializeExtension(RepeatedBoolExtension).AddRange(new[] { true, false });
  177. message.GetOrInitializeExtension(RepeatedBytesExtension).AddRange(new[] { ByteString.CopyFrom(1, 2, 3, 4), ByteString.CopyFrom(5, 6), ByteString.CopyFrom(new byte[1000]) });
  178. message.GetOrInitializeExtension(RepeatedDoubleExtension).AddRange(new[] { -12.25, 23.5 });
  179. message.GetOrInitializeExtension(RepeatedFixed32Extension).AddRange(new[] { UInt32.MaxValue, 23u });
  180. message.GetOrInitializeExtension(RepeatedFixed64Extension).AddRange(new[] { UInt64.MaxValue, 1234567890123ul });
  181. message.GetOrInitializeExtension(RepeatedFloatExtension).AddRange(new[] { 100f, 12.25f });
  182. message.GetOrInitializeExtension(RepeatedForeignEnumExtension).AddRange(new[] { Proto2.ForeignEnum.ForeignFoo, Proto2.ForeignEnum.ForeignBar });
  183. message.GetOrInitializeExtension(RepeatedForeignMessageExtension).AddRange(new[] { new Proto2.ForeignMessage(), new Proto2.ForeignMessage { C = 10 } });
  184. message.GetOrInitializeExtension(RepeatedImportEnumExtension).AddRange(new[] { Proto2.ImportEnum.ImportBaz, Proto2.ImportEnum.ImportFoo });
  185. message.GetOrInitializeExtension(RepeatedImportMessageExtension).AddRange(new[] { new Proto2.ImportMessage { D = 20 }, new Proto2.ImportMessage { D = 25 } });
  186. message.GetOrInitializeExtension(RepeatedInt32Extension).AddRange(new[] { 100, 200 });
  187. message.GetOrInitializeExtension(RepeatedInt64Extension).AddRange(new[] { 3210987654321, Int64.MaxValue });
  188. message.GetOrInitializeExtension(RepeatedNestedEnumExtension).AddRange(new[] { Proto2.TestAllTypes.Types.NestedEnum.Foo, Proto2.TestAllTypes.Types.NestedEnum.Neg });
  189. message.GetOrInitializeExtension(RepeatedNestedMessageExtension).AddRange(new[] { new Proto2.TestAllTypes.Types.NestedMessage { Bb = 35 }, new Proto2.TestAllTypes.Types.NestedMessage { Bb = 10 } });
  190. message.GetOrInitializeExtension(RepeatedSfixed32Extension).AddRange(new[] { -123, 123 });
  191. message.GetOrInitializeExtension(RepeatedSfixed64Extension).AddRange(new[] { -12345678901234, 12345678901234 });
  192. message.GetOrInitializeExtension(RepeatedSint32Extension).AddRange(new[] { -456, 100 });
  193. message.GetOrInitializeExtension(RepeatedSint64Extension).AddRange(new[] { -12345678901235, 123 });
  194. message.GetOrInitializeExtension(RepeatedStringExtension).AddRange(new[] { "foo", "bar" });
  195. message.GetOrInitializeExtension(RepeatedUint32Extension).AddRange(new[] { UInt32.MaxValue, UInt32.MinValue });
  196. message.GetOrInitializeExtension(RepeatedUint64Extension).AddRange(new[] { UInt64.MaxValue, UInt32.MinValue });
  197. message.GetOrInitializeExtension(RepeatedGroupExtension).AddRange(new[] { new Proto2.RepeatedGroup_extension { A = 10 }, new Proto2.RepeatedGroup_extension { A = 20 } });
  198. message.SetExtension(OneofStringExtension, "Oneof string");
  199. return message;
  200. }
  201. }
  202. }