blob: 2e5b11e804d56e8bf2d9da4cc622de037c970583 [file] [log] [blame]
csharptest71f662c2011-05-20 15:15:34 -05001#region Copyright notice and license
2
csharptestd965c662011-05-20 13:57:07 -05003// 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.
csharptest71f662c2011-05-20 15:15:34 -050034
csharptestd965c662011-05-20 13:57:07 -050035#endregion
36
37using System;
38using System.Collections.Generic;
39using System.IO;
40using Google.ProtocolBuffers;
41using Google.ProtocolBuffers.TestProtos;
csharptesteac64a52011-10-04 13:43:26 -050042using Microsoft.VisualStudio.TestTools.UnitTesting;
csharptestd965c662011-05-20 13:57:07 -050043
csharptest71f662c2011-05-20 15:15:34 -050044namespace Google.ProtocolBuffers
45{
csharptesteac64a52011-10-04 13:43:26 -050046 [TestClass]
csharptest71f662c2011-05-20 15:15:34 -050047 public class AbstractBuilderLiteTest
48 {
csharptesteac64a52011-10-04 13:43:26 -050049 [TestMethod]
csharptest71f662c2011-05-20 15:15:34 -050050 public void TestMergeFromCodedInputStream()
51 {
52 TestAllTypesLite copy,
53 msg = TestAllTypesLite.CreateBuilder()
54 .SetOptionalUint32(uint.MaxValue).Build();
csharptestd965c662011-05-20 13:57:07 -050055
csharptest71f662c2011-05-20 15:15:34 -050056 copy = TestAllTypesLite.DefaultInstance;
57 Assert.AreNotEqual(msg.ToByteArray(), copy.ToByteArray());
csharptestd965c662011-05-20 13:57:07 -050058
csharptest71f662c2011-05-20 15:15:34 -050059 using (MemoryStream ms = new MemoryStream(msg.ToByteArray()))
60 {
61 CodedInputStream ci = CodedInputStream.CreateInstance(ms);
62 copy = copy.ToBuilder().MergeFrom(ci).Build();
63 }
csharptestd965c662011-05-20 13:57:07 -050064
csharptesteac64a52011-10-04 13:43:26 -050065 TestUtil.AssertBytesEqual(msg.ToByteArray(), copy.ToByteArray());
csharptest71f662c2011-05-20 15:15:34 -050066 }
csharptestd965c662011-05-20 13:57:07 -050067
csharptesteac64a52011-10-04 13:43:26 -050068 [TestMethod]
csharptest71f662c2011-05-20 15:15:34 -050069 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();
csharptesteac64a52011-10-04 13:43:26 -050077 TestUtil.AssertBytesEqual(msg.ToByteArray(), copy.ToByteArray());
csharptest71f662c2011-05-20 15:15:34 -050078 }
79
csharptesteac64a52011-10-04 13:43:26 -050080 [TestMethod]
csharptest71f662c2011-05-20 15:15:34 -050081 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();
csharptesteac64a52011-10-04 13:43:26 -050092 TestUtil.AssertBytesEqual(msg.ToByteArray(), copy.ToByteArray());
csharptest71f662c2011-05-20 15:15:34 -050093 }
94
csharptesteac64a52011-10-04 13:43:26 -050095 [TestMethod]
csharptest71f662c2011-05-20 15:15:34 -050096 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();
csharptesteac64a52011-10-04 13:43:26 -0500108 TestUtil.AssertBytesEqual(msg.ToByteArray(), copy.ToByteArray());
csharptest71f662c2011-05-20 15:15:34 -0500109 }
110
csharptesteac64a52011-10-04 13:43:26 -0500111 [TestMethod]
csharptest71f662c2011-05-20 15:15:34 -0500112 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();
csharptesteac64a52011-10-04 13:43:26 -0500130 TestUtil.AssertBytesEqual(msg.ToByteArray(), copy.ToByteArray());
csharptest71f662c2011-05-20 15:15:34 -0500131 Assert.AreEqual("Should be merged.", copy.GetExtension(UnitTestLiteProtoFile.OptionalStringExtensionLite));
132 }
133
csharptesteac64a52011-10-04 13:43:26 -0500134 [TestMethod]
csharptest71f662c2011-05-20 15:15:34 -0500135 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();
csharptesteac64a52011-10-04 13:43:26 -0500147 TestUtil.AssertBytesEqual(msg.ToByteArray(), copy.ToByteArray());
csharptest71f662c2011-05-20 15:15:34 -0500148 }
149
csharptesteac64a52011-10-04 13:43:26 -0500150 [TestMethod]
csharptest71f662c2011-05-20 15:15:34 -0500151 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();
csharptesteac64a52011-10-04 13:43:26 -0500169 TestUtil.AssertBytesEqual(msg.ToByteArray(), copy.ToByteArray());
csharptest71f662c2011-05-20 15:15:34 -0500170 Assert.AreEqual("Should be merged.", copy.GetExtension(UnitTestLiteProtoFile.OptionalStringExtensionLite));
171 }
172
csharptesteac64a52011-10-04 13:43:26 -0500173 [TestMethod]
csharptest71f662c2011-05-20 15:15:34 -0500174 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();
csharptesteac64a52011-10-04 13:43:26 -0500184 TestUtil.AssertBytesEqual(msg.ToByteArray(), copy.ToByteArray());
csharptest71f662c2011-05-20 15:15:34 -0500185 }
186
csharptesteac64a52011-10-04 13:43:26 -0500187 [TestMethod]
csharptest71f662c2011-05-20 15:15:34 -0500188 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();
csharptesteac64a52011-10-04 13:43:26 -0500198 TestUtil.AssertBytesEqual(msg.ToByteArray(), copy.ToByteArray());
csharptest71f662c2011-05-20 15:15:34 -0500199 }
200
csharptesteac64a52011-10-04 13:43:26 -0500201 [TestMethod]
csharptest71f662c2011-05-20 15:15:34 -0500202 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();
csharptesteac64a52011-10-04 13:43:26 -0500223 TestUtil.AssertBytesEqual(msg.ToByteArray(), copy.ToByteArray());
csharptest71f662c2011-05-20 15:15:34 -0500224 Assert.AreEqual("Should be merged.", copy.GetExtension(UnitTestLiteProtoFile.OptionalStringExtensionLite));
225 }
226
csharptesteac64a52011-10-04 13:43:26 -0500227 [TestMethod]
csharptest71f662c2011-05-20 15:15:34 -0500228 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
csharptesteac64a52011-10-04 13:43:26 -0500243 TestUtil.AssertBytesEqual(msg.ToByteArray(), copy.ToByteArray());
csharptest71f662c2011-05-20 15:15:34 -0500244 }
245
csharptesteac64a52011-10-04 13:43:26 -0500246 [TestMethod]
csharptest71f662c2011-05-20 15:15:34 -0500247 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
csharptesteac64a52011-10-04 13:43:26 -0500255 TestUtil.AssertBytesEqual(msg.ToByteArray(), TestRequiredLite.DefaultInstance.ToByteArray());
csharptest71f662c2011-05-20 15:15:34 -0500256 }
257
csharptesteac64a52011-10-04 13:43:26 -0500258 [TestMethod, ExpectedException(typeof (UninitializedMessageException))]
csharptest71f662c2011-05-20 15:15:34 -0500259 public void TestIBuilderLiteWeakBuildUninitialized()
260 {
261 IBuilderLite builder = TestRequiredLite.CreateBuilder();
262 Assert.IsFalse(builder.IsInitialized);
263 builder.WeakBuild();
264 }
265
csharptesteac64a52011-10-04 13:43:26 -0500266 [TestMethod]
csharptest71f662c2011-05-20 15:15:34 -0500267 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
csharptesteac64a52011-10-04 13:43:26 -0500276 [TestMethod]
csharptest71f662c2011-05-20 15:15:34 -0500277 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();
csharptesteac64a52011-10-04 13:43:26 -0500284 TestUtil.AssertBytesEqual(msg.ToByteArray(), copy.ToByteArray());
csharptest71f662c2011-05-20 15:15:34 -0500285 }
286
csharptesteac64a52011-10-04 13:43:26 -0500287 [TestMethod]
csharptest71f662c2011-05-20 15:15:34 -0500288 public void TestIBuilderLiteWeakDefaultInstance()
289 {
290 Assert.IsTrue(ReferenceEquals(TestRequiredLite.DefaultInstance,
291 ((IBuilderLite) TestRequiredLite.CreateBuilder()).WeakDefaultInstanceForType));
292 }
293
csharptesteac64a52011-10-04 13:43:26 -0500294 [TestMethod]
csharptest71f662c2011-05-20 15:15:34 -0500295 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();
csharptesteac64a52011-10-04 13:43:26 -0500306 TestUtil.AssertBytesEqual(msg.ToByteArray(), copy.ToByteArray());
csharptest71f662c2011-05-20 15:15:34 -0500307 }
csharptestd965c662011-05-20 13:57:07 -0500308 }
csharptest71f662c2011-05-20 15:15:34 -0500309}