csharptest | 71f662c | 2011-05-20 15:15:34 -0500 | [diff] [blame^] | 1 | #region Copyright notice and license
|
| 2 |
|
csharptest | d965c66 | 2011-05-20 13:57:07 -0500 | [diff] [blame] | 3 | // Protocol Buffers - Google's data interchange format
|
| 4 | // Copyright 2008 Google Inc. All rights reserved.
|
| 5 | // http://github.com/jskeet/dotnet-protobufs/
|
| 6 | // Original C++/Java/Python code:
|
| 7 | // http://code.google.com/p/protobuf/
|
| 8 | //
|
| 9 | // Redistribution and use in source and binary forms, with or without
|
| 10 | // modification, are permitted provided that the following conditions are
|
| 11 | // met:
|
| 12 | //
|
| 13 | // * Redistributions of source code must retain the above copyright
|
| 14 | // notice, this list of conditions and the following disclaimer.
|
| 15 | // * Redistributions in binary form must reproduce the above
|
| 16 | // copyright notice, this list of conditions and the following disclaimer
|
| 17 | // in the documentation and/or other materials provided with the
|
| 18 | // distribution.
|
| 19 | // * Neither the name of Google Inc. nor the names of its
|
| 20 | // contributors may be used to endorse or promote products derived from
|
| 21 | // this software without specific prior written permission.
|
| 22 | //
|
| 23 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
| 24 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
| 25 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
| 26 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
| 27 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
| 28 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
| 29 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
| 30 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
| 31 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
| 32 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
| 33 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
csharptest | 71f662c | 2011-05-20 15:15:34 -0500 | [diff] [blame^] | 34 |
|
csharptest | d965c66 | 2011-05-20 13:57:07 -0500 | [diff] [blame] | 35 | #endregion
|
| 36 |
|
| 37 | using System;
|
| 38 | using System.Collections.Generic;
|
| 39 | using System.IO;
|
| 40 | using Google.ProtocolBuffers;
|
| 41 | using Google.ProtocolBuffers.TestProtos;
|
| 42 | using NUnit.Framework;
|
| 43 |
|
csharptest | 71f662c | 2011-05-20 15:15:34 -0500 | [diff] [blame^] | 44 | namespace Google.ProtocolBuffers
|
| 45 | {
|
| 46 | [TestFixture]
|
| 47 | public class AbstractBuilderLiteTest
|
| 48 | {
|
| 49 | [Test]
|
| 50 | public void TestMergeFromCodedInputStream()
|
| 51 | {
|
| 52 | TestAllTypesLite copy,
|
| 53 | msg = TestAllTypesLite.CreateBuilder()
|
| 54 | .SetOptionalUint32(uint.MaxValue).Build();
|
csharptest | d965c66 | 2011-05-20 13:57:07 -0500 | [diff] [blame] | 55 |
|
csharptest | 71f662c | 2011-05-20 15:15:34 -0500 | [diff] [blame^] | 56 | copy = TestAllTypesLite.DefaultInstance;
|
| 57 | Assert.AreNotEqual(msg.ToByteArray(), copy.ToByteArray());
|
csharptest | d965c66 | 2011-05-20 13:57:07 -0500 | [diff] [blame] | 58 |
|
csharptest | 71f662c | 2011-05-20 15:15:34 -0500 | [diff] [blame^] | 59 | using (MemoryStream ms = new MemoryStream(msg.ToByteArray()))
|
| 60 | {
|
| 61 | CodedInputStream ci = CodedInputStream.CreateInstance(ms);
|
| 62 | copy = copy.ToBuilder().MergeFrom(ci).Build();
|
| 63 | }
|
csharptest | d965c66 | 2011-05-20 13:57:07 -0500 | [diff] [blame] | 64 |
|
csharptest | 71f662c | 2011-05-20 15:15:34 -0500 | [diff] [blame^] | 65 | Assert.AreEqual(msg.ToByteArray(), copy.ToByteArray());
|
| 66 | }
|
csharptest | d965c66 | 2011-05-20 13:57:07 -0500 | [diff] [blame] | 67 |
|
csharptest | 71f662c | 2011-05-20 15:15:34 -0500 | [diff] [blame^] | 68 | [Test]
|
| 69 | public void TestIBuilderLiteWeakClear()
|
| 70 | {
|
| 71 | TestAllTypesLite copy, msg = TestAllTypesLite.DefaultInstance;
|
| 72 |
|
| 73 | copy = msg.ToBuilder().SetOptionalString("Should be removed.").Build();
|
| 74 | Assert.AreNotEqual(msg.ToByteArray(), copy.ToByteArray());
|
| 75 |
|
| 76 | copy = (TestAllTypesLite) ((IBuilderLite) copy.ToBuilder()).WeakClear().WeakBuild();
|
| 77 | Assert.AreEqual(msg.ToByteArray(), copy.ToByteArray());
|
| 78 | }
|
| 79 |
|
| 80 | [Test]
|
| 81 | public void TestBuilderLiteMergeFromCodedInputStream()
|
| 82 | {
|
| 83 | TestAllTypesLite copy,
|
| 84 | msg = TestAllTypesLite.CreateBuilder()
|
| 85 | .SetOptionalString("Should be merged.").Build();
|
| 86 |
|
| 87 | copy = TestAllTypesLite.DefaultInstance;
|
| 88 | Assert.AreNotEqual(msg.ToByteArray(), copy.ToByteArray());
|
| 89 |
|
| 90 | copy =
|
| 91 | copy.ToBuilder().MergeFrom(CodedInputStream.CreateInstance(new MemoryStream(msg.ToByteArray()))).Build();
|
| 92 | Assert.AreEqual(msg.ToByteArray(), copy.ToByteArray());
|
| 93 | }
|
| 94 |
|
| 95 | [Test]
|
| 96 | public void TestBuilderLiteMergeDelimitedFrom()
|
| 97 | {
|
| 98 | TestAllTypesLite copy,
|
| 99 | msg = TestAllTypesLite.CreateBuilder()
|
| 100 | .SetOptionalString("Should be merged.").Build();
|
| 101 |
|
| 102 | copy = TestAllTypesLite.DefaultInstance;
|
| 103 | Assert.AreNotEqual(msg.ToByteArray(), copy.ToByteArray());
|
| 104 | Stream s = new MemoryStream();
|
| 105 | msg.WriteDelimitedTo(s);
|
| 106 | s.Position = 0;
|
| 107 | copy = copy.ToBuilder().MergeDelimitedFrom(s).Build();
|
| 108 | Assert.AreEqual(msg.ToByteArray(), copy.ToByteArray());
|
| 109 | }
|
| 110 |
|
| 111 | [Test]
|
| 112 | public void TestBuilderLiteMergeDelimitedFromExtensions()
|
| 113 | {
|
| 114 | TestAllExtensionsLite copy,
|
| 115 | msg = TestAllExtensionsLite.CreateBuilder()
|
| 116 | .SetExtension(UnitTestLiteProtoFile.OptionalStringExtensionLite,
|
| 117 | "Should be merged.").Build();
|
| 118 |
|
| 119 | copy = TestAllExtensionsLite.DefaultInstance;
|
| 120 | Assert.AreNotEqual(msg.ToByteArray(), copy.ToByteArray());
|
| 121 |
|
| 122 | Stream s = new MemoryStream();
|
| 123 | msg.WriteDelimitedTo(s);
|
| 124 | s.Position = 0;
|
| 125 |
|
| 126 | ExtensionRegistry registry = ExtensionRegistry.CreateInstance();
|
| 127 | UnitTestLiteProtoFile.RegisterAllExtensions(registry);
|
| 128 |
|
| 129 | copy = copy.ToBuilder().MergeDelimitedFrom(s, registry).Build();
|
| 130 | Assert.AreEqual(msg.ToByteArray(), copy.ToByteArray());
|
| 131 | Assert.AreEqual("Should be merged.", copy.GetExtension(UnitTestLiteProtoFile.OptionalStringExtensionLite));
|
| 132 | }
|
| 133 |
|
| 134 | [Test]
|
| 135 | public void TestBuilderLiteMergeFromStream()
|
| 136 | {
|
| 137 | TestAllTypesLite copy,
|
| 138 | msg = TestAllTypesLite.CreateBuilder()
|
| 139 | .SetOptionalString("Should be merged.").Build();
|
| 140 |
|
| 141 | copy = TestAllTypesLite.DefaultInstance;
|
| 142 | Assert.AreNotEqual(msg.ToByteArray(), copy.ToByteArray());
|
| 143 | Stream s = new MemoryStream();
|
| 144 | msg.WriteTo(s);
|
| 145 | s.Position = 0;
|
| 146 | copy = copy.ToBuilder().MergeFrom(s).Build();
|
| 147 | Assert.AreEqual(msg.ToByteArray(), copy.ToByteArray());
|
| 148 | }
|
| 149 |
|
| 150 | [Test]
|
| 151 | public void TestBuilderLiteMergeFromStreamExtensions()
|
| 152 | {
|
| 153 | TestAllExtensionsLite copy,
|
| 154 | msg = TestAllExtensionsLite.CreateBuilder()
|
| 155 | .SetExtension(UnitTestLiteProtoFile.OptionalStringExtensionLite,
|
| 156 | "Should be merged.").Build();
|
| 157 |
|
| 158 | copy = TestAllExtensionsLite.DefaultInstance;
|
| 159 | Assert.AreNotEqual(msg.ToByteArray(), copy.ToByteArray());
|
| 160 |
|
| 161 | Stream s = new MemoryStream();
|
| 162 | msg.WriteTo(s);
|
| 163 | s.Position = 0;
|
| 164 |
|
| 165 | ExtensionRegistry registry = ExtensionRegistry.CreateInstance();
|
| 166 | UnitTestLiteProtoFile.RegisterAllExtensions(registry);
|
| 167 |
|
| 168 | copy = copy.ToBuilder().MergeFrom(s, registry).Build();
|
| 169 | Assert.AreEqual(msg.ToByteArray(), copy.ToByteArray());
|
| 170 | Assert.AreEqual("Should be merged.", copy.GetExtension(UnitTestLiteProtoFile.OptionalStringExtensionLite));
|
| 171 | }
|
| 172 |
|
| 173 | [Test]
|
| 174 | public void TestIBuilderLiteWeakMergeFromIMessageLite()
|
| 175 | {
|
| 176 | TestAllTypesLite copy,
|
| 177 | msg = TestAllTypesLite.CreateBuilder()
|
| 178 | .SetOptionalString("Should be merged.").Build();
|
| 179 |
|
| 180 | copy = TestAllTypesLite.DefaultInstance;
|
| 181 | Assert.AreNotEqual(msg.ToByteArray(), copy.ToByteArray());
|
| 182 |
|
| 183 | copy = (TestAllTypesLite) ((IBuilderLite) copy.ToBuilder()).WeakMergeFrom((IMessageLite) msg).WeakBuild();
|
| 184 | Assert.AreEqual(msg.ToByteArray(), copy.ToByteArray());
|
| 185 | }
|
| 186 |
|
| 187 | [Test]
|
| 188 | public void TestIBuilderLiteWeakMergeFromByteString()
|
| 189 | {
|
| 190 | TestAllTypesLite copy,
|
| 191 | msg = TestAllTypesLite.CreateBuilder()
|
| 192 | .SetOptionalString("Should be merged.").Build();
|
| 193 |
|
| 194 | copy = TestAllTypesLite.DefaultInstance;
|
| 195 | Assert.AreNotEqual(msg.ToByteArray(), copy.ToByteArray());
|
| 196 |
|
| 197 | copy = (TestAllTypesLite) ((IBuilderLite) copy.ToBuilder()).WeakMergeFrom(msg.ToByteString()).WeakBuild();
|
| 198 | Assert.AreEqual(msg.ToByteArray(), copy.ToByteArray());
|
| 199 | }
|
| 200 |
|
| 201 | [Test]
|
| 202 | public void TestIBuilderLiteWeakMergeFromByteStringExtensions()
|
| 203 | {
|
| 204 | TestAllExtensionsLite copy,
|
| 205 | msg = TestAllExtensionsLite.CreateBuilder()
|
| 206 | .SetExtension(UnitTestLiteProtoFile.OptionalStringExtensionLite,
|
| 207 | "Should be merged.").Build();
|
| 208 |
|
| 209 | copy = TestAllExtensionsLite.DefaultInstance;
|
| 210 | Assert.AreNotEqual(msg.ToByteArray(), copy.ToByteArray());
|
| 211 |
|
| 212 | copy =
|
| 213 | (TestAllExtensionsLite)
|
| 214 | ((IBuilderLite) copy.ToBuilder()).WeakMergeFrom(msg.ToByteString(), ExtensionRegistry.Empty).WeakBuild();
|
| 215 | Assert.AreNotEqual(msg.ToByteArray(), copy.ToByteArray());
|
| 216 |
|
| 217 | ExtensionRegistry registry = ExtensionRegistry.CreateInstance();
|
| 218 | UnitTestLiteProtoFile.RegisterAllExtensions(registry);
|
| 219 |
|
| 220 | copy =
|
| 221 | (TestAllExtensionsLite)
|
| 222 | ((IBuilderLite) copy.ToBuilder()).WeakMergeFrom(msg.ToByteString(), registry).WeakBuild();
|
| 223 | Assert.AreEqual(msg.ToByteArray(), copy.ToByteArray());
|
| 224 | Assert.AreEqual("Should be merged.", copy.GetExtension(UnitTestLiteProtoFile.OptionalStringExtensionLite));
|
| 225 | }
|
| 226 |
|
| 227 | [Test]
|
| 228 | public void TestIBuilderLiteWeakMergeFromCodedInputStream()
|
| 229 | {
|
| 230 | TestAllTypesLite copy,
|
| 231 | msg = TestAllTypesLite.CreateBuilder()
|
| 232 | .SetOptionalUint32(uint.MaxValue).Build();
|
| 233 |
|
| 234 | copy = TestAllTypesLite.DefaultInstance;
|
| 235 | Assert.AreNotEqual(msg.ToByteArray(), copy.ToByteArray());
|
| 236 |
|
| 237 | using (MemoryStream ms = new MemoryStream(msg.ToByteArray()))
|
| 238 | {
|
| 239 | CodedInputStream ci = CodedInputStream.CreateInstance(ms);
|
| 240 | copy = (TestAllTypesLite) ((IBuilderLite) copy.ToBuilder()).WeakMergeFrom(ci).WeakBuild();
|
| 241 | }
|
| 242 |
|
| 243 | Assert.AreEqual(msg.ToByteArray(), copy.ToByteArray());
|
| 244 | }
|
| 245 |
|
| 246 | [Test]
|
| 247 | public void TestIBuilderLiteWeakBuildPartial()
|
| 248 | {
|
| 249 | IBuilderLite builder = TestRequiredLite.CreateBuilder();
|
| 250 | Assert.IsFalse(builder.IsInitialized);
|
| 251 |
|
| 252 | IMessageLite msg = builder.WeakBuildPartial();
|
| 253 | Assert.IsFalse(msg.IsInitialized);
|
| 254 |
|
| 255 | Assert.AreEqual(msg.ToByteArray(), TestRequiredLite.DefaultInstance.ToByteArray());
|
| 256 | }
|
| 257 |
|
| 258 | [Test, ExpectedException(typeof (UninitializedMessageException))]
|
| 259 | public void TestIBuilderLiteWeakBuildUninitialized()
|
| 260 | {
|
| 261 | IBuilderLite builder = TestRequiredLite.CreateBuilder();
|
| 262 | Assert.IsFalse(builder.IsInitialized);
|
| 263 | builder.WeakBuild();
|
| 264 | }
|
| 265 |
|
| 266 | [Test]
|
| 267 | public void TestIBuilderLiteWeakBuild()
|
| 268 | {
|
| 269 | IBuilderLite builder = TestRequiredLite.CreateBuilder()
|
| 270 | .SetD(0)
|
| 271 | .SetEn(ExtraEnum.EXLITE_BAZ);
|
| 272 | Assert.IsTrue(builder.IsInitialized);
|
| 273 | builder.WeakBuild();
|
| 274 | }
|
| 275 |
|
| 276 | [Test]
|
| 277 | public void TestIBuilderLiteWeakClone()
|
| 278 | {
|
| 279 | TestRequiredLite msg = TestRequiredLite.CreateBuilder()
|
| 280 | .SetD(1).SetEn(ExtraEnum.EXLITE_BAR).Build();
|
| 281 | Assert.IsTrue(msg.IsInitialized);
|
| 282 |
|
| 283 | IMessageLite copy = ((IBuilderLite) msg.ToBuilder()).WeakClone().WeakBuild();
|
| 284 | Assert.AreEqual(msg.ToByteArray(), copy.ToByteArray());
|
| 285 | }
|
| 286 |
|
| 287 | [Test]
|
| 288 | public void TestIBuilderLiteWeakDefaultInstance()
|
| 289 | {
|
| 290 | Assert.IsTrue(ReferenceEquals(TestRequiredLite.DefaultInstance,
|
| 291 | ((IBuilderLite) TestRequiredLite.CreateBuilder()).WeakDefaultInstanceForType));
|
| 292 | }
|
| 293 |
|
| 294 | [Test]
|
| 295 | public void TestGeneratedBuilderLiteAddRange()
|
| 296 | {
|
| 297 | TestAllTypesLite copy,
|
| 298 | msg = TestAllTypesLite.CreateBuilder()
|
| 299 | .SetOptionalUint32(123)
|
| 300 | .AddRepeatedInt32(1)
|
| 301 | .AddRepeatedInt32(2)
|
| 302 | .AddRepeatedInt32(3)
|
| 303 | .Build();
|
| 304 |
|
| 305 | copy = msg.DefaultInstanceForType.ToBuilder().MergeFrom(msg).Build();
|
| 306 | Assert.AreEqual(msg.ToByteArray(), copy.ToByteArray());
|
| 307 | }
|
csharptest | d965c66 | 2011-05-20 13:57:07 -0500 | [diff] [blame] | 308 | }
|
csharptest | 71f662c | 2011-05-20 15:15:34 -0500 | [diff] [blame^] | 309 | } |