SampleMessages.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. namespace Google.Protobuf
  36. {
  37. /// <summary>
  38. /// Helper methods to create sample instances of types generated from unit test messages.
  39. /// </summary>
  40. public class SampleMessages
  41. {
  42. /// <summary>
  43. /// Creates a new sample TestAllTypes message with all fields populated.
  44. /// The "oneof" field is populated with the string property (OneofString).
  45. /// </summary>
  46. public static TestAllTypes CreateFullTestAllTypes()
  47. {
  48. return new TestAllTypes
  49. {
  50. SingleBool = true,
  51. SingleBytes = ByteString.CopyFrom(1, 2, 3, 4),
  52. SingleDouble = 23.5,
  53. SingleFixed32 = 23,
  54. SingleFixed64 = 1234567890123,
  55. SingleFloat = 12.25f,
  56. SingleForeignEnum = ForeignEnum.ForeignBar,
  57. SingleForeignMessage = new ForeignMessage { C = 10 },
  58. SingleImportEnum = ImportEnum.ImportBaz,
  59. SingleImportMessage = new ImportMessage { D = 20 },
  60. SingleInt32 = 100,
  61. SingleInt64 = 3210987654321,
  62. SingleNestedEnum = TestAllTypes.Types.NestedEnum.Foo,
  63. SingleNestedMessage = new TestAllTypes.Types.NestedMessage { Bb = 35 },
  64. SinglePublicImportMessage = new PublicImportMessage { E = 54 },
  65. SingleSfixed32 = -123,
  66. SingleSfixed64 = -12345678901234,
  67. SingleSint32 = -456,
  68. SingleSint64 = -12345678901235,
  69. SingleString = "test",
  70. SingleUint32 = UInt32.MaxValue,
  71. SingleUint64 = UInt64.MaxValue,
  72. RepeatedBool = { true, false },
  73. RepeatedBytes = { ByteString.CopyFrom(1, 2, 3, 4), ByteString.CopyFrom(5, 6), ByteString.CopyFrom(new byte[1000]) },
  74. RepeatedDouble = { -12.25, 23.5 },
  75. RepeatedFixed32 = { UInt32.MaxValue, 23 },
  76. RepeatedFixed64 = { UInt64.MaxValue, 1234567890123 },
  77. RepeatedFloat = { 100f, 12.25f },
  78. RepeatedForeignEnum = { ForeignEnum.ForeignFoo, ForeignEnum.ForeignBar },
  79. RepeatedForeignMessage = { new ForeignMessage(), new ForeignMessage { C = 10 } },
  80. RepeatedImportEnum = { ImportEnum.ImportBaz, ImportEnum.Unspecified },
  81. RepeatedImportMessage = { new ImportMessage { D = 20 }, new ImportMessage { D = 25 } },
  82. RepeatedInt32 = { 100, 200 },
  83. RepeatedInt64 = { 3210987654321, Int64.MaxValue },
  84. RepeatedNestedEnum = { TestAllTypes.Types.NestedEnum.Foo, TestAllTypes.Types.NestedEnum.Neg },
  85. RepeatedNestedMessage = { new TestAllTypes.Types.NestedMessage { Bb = 35 }, new TestAllTypes.Types.NestedMessage { Bb = 10 } },
  86. RepeatedPublicImportMessage = { new PublicImportMessage { E = 54 }, new PublicImportMessage { E = -1 } },
  87. RepeatedSfixed32 = { -123, 123 },
  88. RepeatedSfixed64 = { -12345678901234, 12345678901234 },
  89. RepeatedSint32 = { -456, 100 },
  90. RepeatedSint64 = { -12345678901235, 123 },
  91. RepeatedString = { "foo", "bar" },
  92. RepeatedUint32 = { UInt32.MaxValue, UInt32.MinValue },
  93. RepeatedUint64 = { UInt64.MaxValue, UInt32.MinValue },
  94. OneofString = "Oneof string"
  95. };
  96. }
  97. public static Proto2.TestAllTypes CreateFullTestAllTypesProto2()
  98. {
  99. return new Proto2.TestAllTypes
  100. {
  101. OptionalBool = true,
  102. OptionalBytes = ByteString.CopyFrom(1, 2, 3, 4),
  103. OptionalDouble = 23.5,
  104. OptionalFixed32 = 23,
  105. OptionalFixed64 = 1234567890123,
  106. OptionalFloat = 12.25f,
  107. OptionalForeignEnum = Proto2.ForeignEnum.ForeignBar,
  108. OptionalForeignMessage = new Proto2.ForeignMessage { C = 10 },
  109. OptionalImportEnum = Proto2.ImportEnum.ImportBaz,
  110. OptionalImportMessage = new Proto2.ImportMessage { D = 20 },
  111. OptionalInt32 = 100,
  112. OptionalInt64 = 3210987654321,
  113. OptionalNestedEnum = Proto2.TestAllTypes.Types.NestedEnum.Foo,
  114. OptionalNestedMessage = new Proto2.TestAllTypes.Types.NestedMessage { Bb = 35 },
  115. OptionalPublicImportMessage = new Proto2.PublicImportMessage { E = 54 },
  116. OptionalSfixed32 = -123,
  117. OptionalSfixed64 = -12345678901234,
  118. OptionalSint32 = -456,
  119. OptionalSint64 = -12345678901235,
  120. OptionalString = "test",
  121. OptionalUint32 = UInt32.MaxValue,
  122. OptionalUint64 = UInt64.MaxValue,
  123. RepeatedBool = { true, false },
  124. RepeatedBytes = { ByteString.CopyFrom(1, 2, 3, 4), ByteString.CopyFrom(5, 6), ByteString.CopyFrom(new byte[1000]) },
  125. RepeatedDouble = { -12.25, 23.5 },
  126. RepeatedFixed32 = { UInt32.MaxValue, 23 },
  127. RepeatedFixed64 = { UInt64.MaxValue, 1234567890123 },
  128. RepeatedFloat = { 100f, 12.25f },
  129. RepeatedForeignEnum = { Proto2.ForeignEnum.ForeignFoo, Proto2.ForeignEnum.ForeignBar },
  130. RepeatedForeignMessage = { new Proto2.ForeignMessage(), new Proto2.ForeignMessage { C = 10 } },
  131. RepeatedImportEnum = { Proto2.ImportEnum.ImportBaz, Proto2.ImportEnum.ImportFoo },
  132. RepeatedImportMessage = { new Proto2.ImportMessage { D = 20 }, new Proto2.ImportMessage { D = 25 } },
  133. RepeatedInt32 = { 100, 200 },
  134. RepeatedInt64 = { 3210987654321, Int64.MaxValue },
  135. RepeatedNestedEnum = { Proto2.TestAllTypes.Types.NestedEnum.Foo, Proto2.TestAllTypes.Types.NestedEnum.Neg },
  136. RepeatedNestedMessage = { new Proto2.TestAllTypes.Types.NestedMessage { Bb = 35 }, new Proto2.TestAllTypes.Types.NestedMessage { Bb = 10 } },
  137. RepeatedSfixed32 = { -123, 123 },
  138. RepeatedSfixed64 = { -12345678901234, 12345678901234 },
  139. RepeatedSint32 = { -456, 100 },
  140. RepeatedSint64 = { -12345678901235, 123 },
  141. RepeatedString = { "foo", "bar" },
  142. RepeatedUint32 = { UInt32.MaxValue, UInt32.MinValue },
  143. RepeatedUint64 = { UInt64.MaxValue, UInt32.MinValue },
  144. OneofString = "Oneof string"
  145. };
  146. }
  147. }
  148. }