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;
|
| 39 | using System.Collections.Generic;
|
| 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 ExtendableBuilderLiteTest
|
csharptest | d965c66 | 2011-05-20 13:57:07 -0500 | [diff] [blame] | 48 | {
|
csharptest | 71f662c | 2011-05-20 15:15:34 -0500 | [diff] [blame] | 49 | [Test]
|
| 50 | public void TestHasExtensionT()
|
| 51 | {
|
| 52 | TestAllExtensionsLite.Builder builder = TestAllExtensionsLite.CreateBuilder()
|
| 53 | .SetExtension(UnitTestLiteProtoFile.OptionalInt32ExtensionLite, 123);
|
csharptest | d965c66 | 2011-05-20 13:57:07 -0500 | [diff] [blame] | 54 |
|
csharptest | 71f662c | 2011-05-20 15:15:34 -0500 | [diff] [blame] | 55 | Assert.IsTrue(builder.HasExtension(UnitTestLiteProtoFile.OptionalInt32ExtensionLite));
|
| 56 | }
|
csharptest | d965c66 | 2011-05-20 13:57:07 -0500 | [diff] [blame] | 57 |
|
csharptest | 71f662c | 2011-05-20 15:15:34 -0500 | [diff] [blame] | 58 | [Test]
|
| 59 | public void TestHasExtensionTMissing()
|
| 60 | {
|
| 61 | TestAllExtensionsLite.Builder builder = TestAllExtensionsLite.CreateBuilder();
|
| 62 | Assert.IsFalse(builder.HasExtension(UnitTestLiteProtoFile.OptionalInt32ExtensionLite));
|
| 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 | [Test]
|
| 66 | public void TestGetExtensionCountT()
|
| 67 | {
|
| 68 | TestAllExtensionsLite.Builder builder = TestAllExtensionsLite.CreateBuilder()
|
| 69 | .AddExtension(UnitTestLiteProtoFile.RepeatedInt32ExtensionLite, 1)
|
| 70 | .AddExtension(UnitTestLiteProtoFile.RepeatedInt32ExtensionLite, 2)
|
| 71 | .AddExtension(UnitTestLiteProtoFile.RepeatedInt32ExtensionLite, 3);
|
| 72 |
|
| 73 | Assert.AreEqual(3, builder.GetExtensionCount(UnitTestLiteProtoFile.RepeatedInt32ExtensionLite));
|
| 74 | }
|
| 75 |
|
| 76 | [Test]
|
| 77 | public void TestGetExtensionCountTEmpty()
|
| 78 | {
|
| 79 | TestAllExtensionsLite.Builder builder = TestAllExtensionsLite.CreateBuilder();
|
| 80 | Assert.AreEqual(0, builder.GetExtensionCount(UnitTestLiteProtoFile.RepeatedInt32ExtensionLite));
|
| 81 | }
|
| 82 |
|
| 83 | [Test]
|
| 84 | public void TestGetExtensionTNull()
|
| 85 | {
|
| 86 | TestAllExtensionsLite.Builder builder = TestAllExtensionsLite.CreateBuilder();
|
| 87 | string value = builder.GetExtension(UnitTestLiteProtoFile.OptionalStringExtensionLite);
|
| 88 | Assert.IsNull(value);
|
| 89 | }
|
| 90 |
|
| 91 | [Test]
|
| 92 | public void TestGetExtensionTValue()
|
| 93 | {
|
| 94 | TestAllExtensionsLite.Builder builder = TestAllExtensionsLite.CreateBuilder()
|
| 95 | .SetExtension(UnitTestLiteProtoFile.OptionalInt32ExtensionLite, 3);
|
| 96 |
|
| 97 | Assert.AreEqual(3, builder.GetExtension(UnitTestLiteProtoFile.OptionalInt32ExtensionLite));
|
| 98 | }
|
| 99 |
|
| 100 | [Test]
|
| 101 | public void TestGetExtensionTEmpty()
|
| 102 | {
|
| 103 | TestAllExtensionsLite.Builder builder = TestAllExtensionsLite.CreateBuilder();
|
| 104 | Assert.AreEqual(0, builder.GetExtension(UnitTestLiteProtoFile.RepeatedInt32ExtensionLite).Count);
|
| 105 | }
|
| 106 |
|
| 107 | [Test]
|
| 108 | public void TestGetExtensionTList()
|
| 109 | {
|
| 110 | TestAllExtensionsLite.Builder builder = TestAllExtensionsLite.CreateBuilder()
|
| 111 | .AddExtension(UnitTestLiteProtoFile.RepeatedInt32ExtensionLite, 1)
|
| 112 | .AddExtension(UnitTestLiteProtoFile.RepeatedInt32ExtensionLite, 2)
|
| 113 | .AddExtension(UnitTestLiteProtoFile.RepeatedInt32ExtensionLite, 3);
|
| 114 |
|
| 115 | IList<int> values = builder.GetExtension(UnitTestLiteProtoFile.RepeatedInt32ExtensionLite);
|
| 116 | Assert.AreEqual(3, values.Count);
|
| 117 | }
|
| 118 |
|
| 119 | [Test]
|
| 120 | public void TestGetExtensionTIndex()
|
| 121 | {
|
| 122 | TestAllExtensionsLite.Builder builder = TestAllExtensionsLite.CreateBuilder()
|
| 123 | .AddExtension(UnitTestLiteProtoFile.RepeatedInt32ExtensionLite, 0)
|
| 124 | .AddExtension(UnitTestLiteProtoFile.RepeatedInt32ExtensionLite, 1)
|
| 125 | .AddExtension(UnitTestLiteProtoFile.RepeatedInt32ExtensionLite, 2);
|
| 126 |
|
| 127 | for (int i = 0; i < 3; i++)
|
| 128 | Assert.AreEqual(i, builder.GetExtension(UnitTestLiteProtoFile.RepeatedInt32ExtensionLite, i));
|
| 129 | }
|
| 130 |
|
| 131 | [Test, ExpectedException(typeof (ArgumentOutOfRangeException))]
|
| 132 | public void TestGetExtensionTIndexOutOfRange()
|
| 133 | {
|
| 134 | TestAllExtensionsLite.Builder builder = TestAllExtensionsLite.CreateBuilder();
|
| 135 | builder.GetExtension(UnitTestLiteProtoFile.RepeatedInt32ExtensionLite, 0);
|
| 136 | }
|
| 137 |
|
| 138 | [Test]
|
| 139 | public void TestSetExtensionTIndex()
|
| 140 | {
|
| 141 | TestAllExtensionsLite.Builder builder = TestAllExtensionsLite.CreateBuilder()
|
| 142 | .AddExtension(UnitTestLiteProtoFile.RepeatedInt32ExtensionLite, 0)
|
| 143 | .AddExtension(UnitTestLiteProtoFile.RepeatedInt32ExtensionLite, 1)
|
| 144 | .AddExtension(UnitTestLiteProtoFile.RepeatedInt32ExtensionLite, 2);
|
| 145 |
|
| 146 | for (int i = 0; i < 3; i++)
|
| 147 | Assert.AreEqual(i, builder.GetExtension(UnitTestLiteProtoFile.RepeatedInt32ExtensionLite, i));
|
| 148 |
|
| 149 | builder.SetExtension(UnitTestLiteProtoFile.RepeatedInt32ExtensionLite, 0, 5);
|
| 150 | builder.SetExtension(UnitTestLiteProtoFile.RepeatedInt32ExtensionLite, 1, 6);
|
| 151 | builder.SetExtension(UnitTestLiteProtoFile.RepeatedInt32ExtensionLite, 2, 7);
|
| 152 |
|
| 153 | for (int i = 0; i < 3; i++)
|
| 154 | Assert.AreEqual(5 + i, builder.GetExtension(UnitTestLiteProtoFile.RepeatedInt32ExtensionLite, i));
|
| 155 | }
|
| 156 |
|
| 157 | [Test, ExpectedException(typeof (ArgumentOutOfRangeException))]
|
| 158 | public void TestSetExtensionTIndexOutOfRange()
|
| 159 | {
|
| 160 | TestAllExtensionsLite.Builder builder = TestAllExtensionsLite.CreateBuilder();
|
| 161 | builder.SetExtension(UnitTestLiteProtoFile.RepeatedInt32ExtensionLite, 0, -1);
|
| 162 | }
|
| 163 |
|
| 164 | [Test]
|
| 165 | public void TestClearExtensionTList()
|
| 166 | {
|
| 167 | TestAllExtensionsLite.Builder builder = TestAllExtensionsLite.CreateBuilder()
|
| 168 | .AddExtension(UnitTestLiteProtoFile.RepeatedInt32ExtensionLite, 0);
|
| 169 | Assert.AreEqual(1, builder.GetExtensionCount(UnitTestLiteProtoFile.RepeatedInt32ExtensionLite));
|
| 170 |
|
| 171 | builder.ClearExtension(UnitTestLiteProtoFile.RepeatedInt32ExtensionLite);
|
| 172 | Assert.AreEqual(0, builder.GetExtensionCount(UnitTestLiteProtoFile.RepeatedInt32ExtensionLite));
|
| 173 | }
|
| 174 |
|
| 175 | [Test]
|
| 176 | public void TestClearExtensionTValue()
|
| 177 | {
|
| 178 | TestAllExtensionsLite.Builder builder = TestAllExtensionsLite.CreateBuilder()
|
| 179 | .SetExtension(UnitTestLiteProtoFile.OptionalInt32ExtensionLite, 0);
|
| 180 | Assert.IsTrue(builder.HasExtension(UnitTestLiteProtoFile.OptionalInt32ExtensionLite));
|
| 181 |
|
| 182 | builder.ClearExtension(UnitTestLiteProtoFile.OptionalInt32ExtensionLite);
|
| 183 | Assert.IsFalse(builder.HasExtension(UnitTestLiteProtoFile.OptionalInt32ExtensionLite));
|
| 184 | }
|
| 185 |
|
| 186 | [Test]
|
| 187 | public void TestIndexedByDescriptor()
|
| 188 | {
|
| 189 | TestAllExtensionsLite.Builder builder = TestAllExtensionsLite.CreateBuilder();
|
| 190 | Assert.IsFalse(builder.HasExtension(UnitTestLiteProtoFile.OptionalInt32ExtensionLite));
|
| 191 |
|
| 192 | builder[UnitTestLiteProtoFile.OptionalInt32ExtensionLite.Descriptor] = 123;
|
| 193 |
|
| 194 | Assert.IsTrue(builder.HasExtension(UnitTestLiteProtoFile.OptionalInt32ExtensionLite));
|
| 195 | Assert.AreEqual(123, builder.GetExtension(UnitTestLiteProtoFile.OptionalInt32ExtensionLite));
|
| 196 | }
|
| 197 |
|
| 198 | [Test]
|
| 199 | public void TestIndexedByDescriptorAndOrdinal()
|
| 200 | {
|
| 201 | TestAllExtensionsLite.Builder builder = TestAllExtensionsLite.CreateBuilder()
|
| 202 | .AddExtension(UnitTestLiteProtoFile.RepeatedInt32ExtensionLite, 0);
|
| 203 | Assert.AreEqual(1, builder.GetExtensionCount(UnitTestLiteProtoFile.RepeatedInt32ExtensionLite));
|
| 204 |
|
| 205 | IFieldDescriptorLite f = UnitTestLiteProtoFile.RepeatedInt32ExtensionLite.Descriptor;
|
| 206 | builder[f, 0] = 123;
|
| 207 |
|
| 208 | Assert.AreEqual(1, builder.GetExtensionCount(UnitTestLiteProtoFile.RepeatedInt32ExtensionLite));
|
| 209 | Assert.AreEqual(123, builder.GetExtension(UnitTestLiteProtoFile.RepeatedInt32ExtensionLite, 0));
|
| 210 | }
|
| 211 |
|
| 212 | [Test, ExpectedException(typeof (ArgumentOutOfRangeException))]
|
| 213 | public void TestIndexedByDescriptorAndOrdinalOutOfRange()
|
| 214 | {
|
| 215 | TestAllExtensionsLite.Builder builder = TestAllExtensionsLite.CreateBuilder();
|
| 216 | Assert.AreEqual(0, builder.GetExtensionCount(UnitTestLiteProtoFile.RepeatedInt32ExtensionLite));
|
| 217 |
|
| 218 | IFieldDescriptorLite f = UnitTestLiteProtoFile.RepeatedInt32ExtensionLite.Descriptor;
|
| 219 | builder[f, 0] = 123;
|
| 220 | }
|
| 221 |
|
| 222 | [Test]
|
| 223 | public void TestClearFieldByDescriptor()
|
| 224 | {
|
| 225 | TestAllExtensionsLite.Builder builder = TestAllExtensionsLite.CreateBuilder()
|
| 226 | .AddExtension(UnitTestLiteProtoFile.RepeatedInt32ExtensionLite, 0);
|
| 227 | Assert.AreEqual(1, builder.GetExtensionCount(UnitTestLiteProtoFile.RepeatedInt32ExtensionLite));
|
| 228 |
|
| 229 | IFieldDescriptorLite f = UnitTestLiteProtoFile.RepeatedInt32ExtensionLite.Descriptor;
|
| 230 | builder.ClearField(f);
|
| 231 | Assert.AreEqual(0, builder.GetExtensionCount(UnitTestLiteProtoFile.RepeatedInt32ExtensionLite));
|
| 232 | }
|
| 233 |
|
| 234 | [Test]
|
| 235 | public void TestAddRepeatedFieldByDescriptor()
|
| 236 | {
|
| 237 | TestAllExtensionsLite.Builder builder = TestAllExtensionsLite.CreateBuilder()
|
| 238 | .AddExtension(UnitTestLiteProtoFile.RepeatedInt32ExtensionLite, 0);
|
| 239 | Assert.AreEqual(1, builder.GetExtensionCount(UnitTestLiteProtoFile.RepeatedInt32ExtensionLite));
|
| 240 |
|
| 241 | IFieldDescriptorLite f = UnitTestLiteProtoFile.RepeatedInt32ExtensionLite.Descriptor;
|
| 242 | builder.AddRepeatedField(f, 123);
|
| 243 | Assert.AreEqual(2, builder.GetExtensionCount(UnitTestLiteProtoFile.RepeatedInt32ExtensionLite));
|
| 244 | Assert.AreEqual(123, builder.GetExtension(UnitTestLiteProtoFile.RepeatedInt32ExtensionLite, 1));
|
| 245 | }
|
| 246 |
|
| 247 | [Test]
|
| 248 | public void TestMissingExtensionsLite()
|
| 249 | {
|
| 250 | const int optionalInt32 = 12345678;
|
| 251 | TestAllExtensionsLite.Builder builder = TestAllExtensionsLite.CreateBuilder();
|
| 252 | builder.SetExtension(UnitTestLiteProtoFile.OptionalInt32ExtensionLite, optionalInt32);
|
| 253 | builder.AddExtension(UnitTestLiteProtoFile.RepeatedDoubleExtensionLite, 1.1);
|
| 254 | builder.AddExtension(UnitTestLiteProtoFile.RepeatedDoubleExtensionLite, 1.2);
|
| 255 | builder.AddExtension(UnitTestLiteProtoFile.RepeatedDoubleExtensionLite, 1.3);
|
| 256 | TestAllExtensionsLite msg = builder.Build();
|
| 257 |
|
| 258 | Assert.IsTrue(msg.HasExtension(UnitTestLiteProtoFile.OptionalInt32ExtensionLite));
|
| 259 | Assert.AreEqual(3, msg.GetExtensionCount(UnitTestLiteProtoFile.RepeatedDoubleExtensionLite));
|
| 260 |
|
| 261 | byte[] bits = msg.ToByteArray();
|
| 262 | TestAllExtensionsLite copy = TestAllExtensionsLite.ParseFrom(bits);
|
| 263 | Assert.IsFalse(copy.HasExtension(UnitTestLiteProtoFile.OptionalInt32ExtensionLite));
|
| 264 | Assert.AreEqual(0, copy.GetExtensionCount(UnitTestLiteProtoFile.RepeatedDoubleExtensionLite));
|
| 265 | Assert.AreNotEqual(msg, copy);
|
| 266 |
|
| 267 | //The lite runtime removes all unknown fields and extensions
|
| 268 | byte[] copybits = copy.ToByteArray();
|
| 269 | Assert.AreEqual(0, copybits.Length);
|
| 270 | }
|
| 271 |
|
| 272 | [Test]
|
| 273 | public void TestMissingFieldsLite()
|
| 274 | {
|
| 275 | TestAllTypesLite msg = TestAllTypesLite.CreateBuilder()
|
| 276 | .SetOptionalInt32(123)
|
| 277 | .SetOptionalString("123")
|
| 278 | .Build();
|
| 279 |
|
| 280 | byte[] bits = msg.ToByteArray();
|
| 281 | TestAllExtensionsLite copy = TestAllExtensionsLite.ParseFrom(bits);
|
| 282 | Assert.AreNotEqual(msg, copy);
|
| 283 |
|
| 284 | //The lite runtime removes all unknown fields and extensions
|
| 285 | byte[] copybits = copy.ToByteArray();
|
| 286 | Assert.AreEqual(0, copybits.Length);
|
| 287 | }
|
csharptest | d965c66 | 2011-05-20 13:57:07 -0500 | [diff] [blame] | 288 | }
|
csharptest | 71f662c | 2011-05-20 15:15:34 -0500 | [diff] [blame] | 289 | } |