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