blob: c7836d4e1cb36c19a9220ac96bddcde99682242c [file] [log] [blame]
csharptest7d396f92010-11-08 20:06:46 -06001#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
35using System;
36using System.Collections.Generic;
37using System.IO;
38using Google.ProtocolBuffers;
39using Google.ProtocolBuffers.TestProtos;
40using NUnit.Framework;
41
42namespace 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 TestIBuilderLiteWeakMergeFromByteString() {
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 = (TestAllTypesLite)((IBuilderLite)copy.ToBuilder()).WeakMergeFrom(msg.ToByteString()).WeakBuild();
82 Assert.AreEqual(msg.ToByteArray(), copy.ToByteArray());
83
84 //again with extension registry
85 copy = TestAllTypesLite.DefaultInstance;
86 Assert.AreNotEqual(msg.ToByteArray(), copy.ToByteArray());
87
88 copy = (TestAllTypesLite)((IBuilderLite)copy.ToBuilder()).WeakMergeFrom(msg.ToByteString(), ExtensionRegistry.Empty).WeakBuild();
89 Assert.AreEqual(msg.ToByteArray(), copy.ToByteArray());
90 }
91
92 [Test]
93 public void TestIBuilderLiteWeakMergeFromCodedInputStream() {
94 TestAllTypesLite copy, msg = TestAllTypesLite.CreateBuilder()
95 .SetOptionalUint32(uint.MaxValue).Build();
96
97 copy = TestAllTypesLite.DefaultInstance;
98 Assert.AreNotEqual(msg.ToByteArray(), copy.ToByteArray());
99
100 using (MemoryStream ms = new MemoryStream(msg.ToByteArray())) {
101 CodedInputStream ci = CodedInputStream.CreateInstance(ms);
102 copy = (TestAllTypesLite)((IBuilderLite)copy.ToBuilder()).WeakMergeFrom(ci).WeakBuild();
103 }
104
105 Assert.AreEqual(msg.ToByteArray(), copy.ToByteArray());
106 }
107
108 [Test]
109 public void TestIBuilderLiteWeakBuildPartial() {
110 IBuilderLite builder = TestRequiredLite.CreateBuilder();
111 Assert.IsFalse(builder.IsInitialized);
112
113 IMessageLite msg = builder.WeakBuildPartial();
114 Assert.IsFalse(msg.IsInitialized);
115
116 Assert.AreEqual(msg.ToByteArray(), TestRequiredLite.DefaultInstance.ToByteArray());
117 }
118
119 [Test, ExpectedException(typeof(UninitializedMessageException ))]
120 public void TestIBuilderLiteWeakBuildUninitialized() {
121 IBuilderLite builder = TestRequiredLite.CreateBuilder();
122 Assert.IsFalse(builder.IsInitialized);
123 builder.WeakBuild();
124 }
125
126 [Test]
127 public void TestIBuilderLiteWeakBuild() {
128 IBuilderLite builder = TestRequiredLite.CreateBuilder()
129 .SetD(0)
130 .SetEn(ExtraEnum.EXLITE_BAZ);
131 Assert.IsTrue(builder.IsInitialized);
132 builder.WeakBuild();
133 }
134
135 [Test]
136 public void TestIBuilderLiteWeakClone() {
137 TestRequiredLite msg = TestRequiredLite.CreateBuilder()
138 .SetD(1).SetEn(ExtraEnum.EXLITE_BAR).Build();
139 Assert.IsTrue(msg.IsInitialized);
140
141 IMessageLite copy = ((IBuilderLite)msg.ToBuilder()).WeakClone().WeakBuild();
142 Assert.AreEqual(msg.ToByteArray(), copy.ToByteArray());
143 }
144
145 [Test]
146 public void TestIBuilderLiteWeakDefaultInstance() {
147 Assert.IsTrue(ReferenceEquals(TestRequiredLite.DefaultInstance,
148 ((IBuilderLite)TestRequiredLite.CreateBuilder()).WeakDefaultInstanceForType));
149 }
150 }
151}