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