csharptest | 7d396f9 | 2010-11-08 20:06:46 -0600 | [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; |
| 37 | using System.Collections.Generic; |
| 38 | using Google.ProtocolBuffers; |
| 39 | using Google.ProtocolBuffers.TestProtos; |
| 40 | using NUnit.Framework; |
| 41 | |
| 42 | namespace Google.ProtocolBuffers { |
| 43 | [TestFixture] |
| 44 | public class ExtendableBuilderLiteTest { |
| 45 | |
| 46 | [Test] |
| 47 | public void TestHasExtensionT() { |
| 48 | TestAllExtensionsLite.Builder builder = TestAllExtensionsLite.CreateBuilder() |
| 49 | .SetExtension(UnitTestLiteProtoFile.OptionalInt32ExtensionLite, 123); |
| 50 | |
| 51 | Assert.IsTrue(builder.HasExtension(UnitTestLiteProtoFile.OptionalInt32ExtensionLite)); |
| 52 | } |
| 53 | |
| 54 | [Test] |
| 55 | public void TestHasExtensionTMissing() { |
| 56 | TestAllExtensionsLite.Builder builder = TestAllExtensionsLite.CreateBuilder(); |
| 57 | Assert.IsFalse(builder.HasExtension(UnitTestLiteProtoFile.OptionalInt32ExtensionLite)); |
| 58 | } |
| 59 | |
| 60 | [Test] |
| 61 | public void TestGetExtensionCountT() { |
| 62 | TestAllExtensionsLite.Builder builder = TestAllExtensionsLite.CreateBuilder() |
| 63 | .AddExtension(UnitTestLiteProtoFile.RepeatedInt32ExtensionLite, 1) |
| 64 | .AddExtension(UnitTestLiteProtoFile.RepeatedInt32ExtensionLite, 2) |
| 65 | .AddExtension(UnitTestLiteProtoFile.RepeatedInt32ExtensionLite, 3); |
| 66 | |
| 67 | Assert.AreEqual(3, builder.GetExtensionCount(UnitTestLiteProtoFile.RepeatedInt32ExtensionLite)); |
| 68 | } |
| 69 | |
| 70 | [Test] |
| 71 | public void TestGetExtensionCountTEmpty() { |
| 72 | TestAllExtensionsLite.Builder builder = TestAllExtensionsLite.CreateBuilder(); |
| 73 | Assert.AreEqual(0, builder.GetExtensionCount(UnitTestLiteProtoFile.RepeatedInt32ExtensionLite)); |
| 74 | } |
| 75 | |
| 76 | [Test] |
| 77 | public void TestGetExtensionTNull() { |
| 78 | TestAllExtensionsLite.Builder builder = TestAllExtensionsLite.CreateBuilder(); |
| 79 | string value = builder.GetExtension(UnitTestLiteProtoFile.OptionalStringExtensionLite); |
| 80 | Assert.IsNull(value); |
| 81 | } |
| 82 | |
| 83 | [Test] |
| 84 | public void TestGetExtensionTValue() { |
| 85 | TestAllExtensionsLite.Builder builder = TestAllExtensionsLite.CreateBuilder() |
| 86 | .SetExtension(UnitTestLiteProtoFile.OptionalInt32ExtensionLite, 3); |
| 87 | |
| 88 | Assert.AreEqual(3, builder.GetExtension(UnitTestLiteProtoFile.OptionalInt32ExtensionLite)); |
| 89 | } |
| 90 | |
| 91 | [Test] |
| 92 | public void TestGetExtensionTEmpty() { |
| 93 | TestAllExtensionsLite.Builder builder = TestAllExtensionsLite.CreateBuilder(); |
| 94 | Assert.AreEqual(0, builder.GetExtension(UnitTestLiteProtoFile.RepeatedInt32ExtensionLite).Count); |
| 95 | } |
| 96 | |
| 97 | [Test] |
| 98 | public void TestGetExtensionTList() { |
| 99 | TestAllExtensionsLite.Builder builder = TestAllExtensionsLite.CreateBuilder() |
| 100 | .AddExtension(UnitTestLiteProtoFile.RepeatedInt32ExtensionLite, 1) |
| 101 | .AddExtension(UnitTestLiteProtoFile.RepeatedInt32ExtensionLite, 2) |
| 102 | .AddExtension(UnitTestLiteProtoFile.RepeatedInt32ExtensionLite, 3); |
| 103 | |
| 104 | IList<int> values = builder.GetExtension(UnitTestLiteProtoFile.RepeatedInt32ExtensionLite); |
| 105 | Assert.AreEqual(3, values.Count); |
| 106 | } |
| 107 | |
| 108 | [Test] |
| 109 | public void TestGetExtensionTIndex() { |
| 110 | TestAllExtensionsLite.Builder builder = TestAllExtensionsLite.CreateBuilder() |
| 111 | .AddExtension(UnitTestLiteProtoFile.RepeatedInt32ExtensionLite, 0) |
| 112 | .AddExtension(UnitTestLiteProtoFile.RepeatedInt32ExtensionLite, 1) |
| 113 | .AddExtension(UnitTestLiteProtoFile.RepeatedInt32ExtensionLite, 2); |
| 114 | |
| 115 | for(int i = 0; i < 3; i++ ) |
| 116 | Assert.AreEqual(i, builder.GetExtension(UnitTestLiteProtoFile.RepeatedInt32ExtensionLite, i)); |
| 117 | } |
| 118 | |
| 119 | [Test,ExpectedException(typeof(ArgumentOutOfRangeException))] |
| 120 | public void TestGetExtensionTIndexOutOfRange() { |
| 121 | TestAllExtensionsLite.Builder builder = TestAllExtensionsLite.CreateBuilder(); |
| 122 | builder.GetExtension(UnitTestLiteProtoFile.RepeatedInt32ExtensionLite, 0); |
| 123 | } |
| 124 | |
| 125 | [Test] |
| 126 | public void TestSetExtensionTIndex() { |
| 127 | TestAllExtensionsLite.Builder builder = TestAllExtensionsLite.CreateBuilder() |
| 128 | .AddExtension(UnitTestLiteProtoFile.RepeatedInt32ExtensionLite, 0) |
| 129 | .AddExtension(UnitTestLiteProtoFile.RepeatedInt32ExtensionLite, 1) |
| 130 | .AddExtension(UnitTestLiteProtoFile.RepeatedInt32ExtensionLite, 2); |
| 131 | |
| 132 | for (int i = 0; i < 3; i++) |
| 133 | Assert.AreEqual(i, builder.GetExtension(UnitTestLiteProtoFile.RepeatedInt32ExtensionLite, i)); |
| 134 | |
| 135 | builder.SetExtension(UnitTestLiteProtoFile.RepeatedInt32ExtensionLite, 0, 5); |
| 136 | builder.SetExtension(UnitTestLiteProtoFile.RepeatedInt32ExtensionLite, 1, 6); |
| 137 | builder.SetExtension(UnitTestLiteProtoFile.RepeatedInt32ExtensionLite, 2, 7); |
| 138 | |
| 139 | for (int i = 0; i < 3; i++) |
| 140 | Assert.AreEqual(5 + i, builder.GetExtension(UnitTestLiteProtoFile.RepeatedInt32ExtensionLite, i)); |
| 141 | } |
| 142 | |
| 143 | [Test, ExpectedException(typeof(ArgumentOutOfRangeException))] |
| 144 | public void TestSetExtensionTIndexOutOfRange() { |
| 145 | TestAllExtensionsLite.Builder builder = TestAllExtensionsLite.CreateBuilder(); |
| 146 | builder.SetExtension(UnitTestLiteProtoFile.RepeatedInt32ExtensionLite, 0, -1); |
| 147 | } |
| 148 | |
| 149 | [Test] |
| 150 | public void TestClearExtensionTList() { |
| 151 | TestAllExtensionsLite.Builder builder = TestAllExtensionsLite.CreateBuilder() |
| 152 | .AddExtension(UnitTestLiteProtoFile.RepeatedInt32ExtensionLite, 0); |
| 153 | Assert.AreEqual(1, builder.GetExtensionCount(UnitTestLiteProtoFile.RepeatedInt32ExtensionLite)); |
| 154 | |
| 155 | builder.ClearExtension(UnitTestLiteProtoFile.RepeatedInt32ExtensionLite); |
| 156 | Assert.AreEqual(0, builder.GetExtensionCount(UnitTestLiteProtoFile.RepeatedInt32ExtensionLite)); |
| 157 | } |
| 158 | |
| 159 | [Test] |
| 160 | public void TestClearExtensionTValue() { |
| 161 | TestAllExtensionsLite.Builder builder = TestAllExtensionsLite.CreateBuilder() |
| 162 | .SetExtension(UnitTestLiteProtoFile.OptionalInt32ExtensionLite, 0); |
| 163 | Assert.IsTrue(builder.HasExtension(UnitTestLiteProtoFile.OptionalInt32ExtensionLite)); |
| 164 | |
| 165 | builder.ClearExtension(UnitTestLiteProtoFile.OptionalInt32ExtensionLite); |
| 166 | Assert.IsFalse(builder.HasExtension(UnitTestLiteProtoFile.OptionalInt32ExtensionLite)); |
| 167 | } |
| 168 | |
| 169 | [Test] |
| 170 | public void TestIndexedByDescriptor() { |
| 171 | TestAllExtensionsLite.Builder builder = TestAllExtensionsLite.CreateBuilder(); |
| 172 | Assert.IsFalse(builder.HasExtension(UnitTestLiteProtoFile.OptionalInt32ExtensionLite)); |
| 173 | |
| 174 | builder[UnitTestLiteProtoFile.OptionalInt32ExtensionLite.Descriptor] = 123; |
| 175 | |
| 176 | Assert.IsTrue(builder.HasExtension(UnitTestLiteProtoFile.OptionalInt32ExtensionLite)); |
| 177 | Assert.AreEqual(123, builder.GetExtension(UnitTestLiteProtoFile.OptionalInt32ExtensionLite)); |
| 178 | } |
| 179 | |
| 180 | [Test] |
| 181 | public void TestIndexedByDescriptorAndOrdinal() { |
| 182 | TestAllExtensionsLite.Builder builder = TestAllExtensionsLite.CreateBuilder() |
| 183 | .AddExtension(UnitTestLiteProtoFile.RepeatedInt32ExtensionLite, 0); |
| 184 | Assert.AreEqual(1, builder.GetExtensionCount(UnitTestLiteProtoFile.RepeatedInt32ExtensionLite)); |
| 185 | |
| 186 | IFieldDescriptorLite f = UnitTestLiteProtoFile.RepeatedInt32ExtensionLite.Descriptor; |
| 187 | builder[f, 0] = 123; |
| 188 | |
| 189 | Assert.AreEqual(1, builder.GetExtensionCount(UnitTestLiteProtoFile.RepeatedInt32ExtensionLite)); |
| 190 | Assert.AreEqual(123, builder.GetExtension(UnitTestLiteProtoFile.RepeatedInt32ExtensionLite, 0)); |
| 191 | } |
| 192 | |
| 193 | [Test, ExpectedException(typeof(ArgumentOutOfRangeException))] |
| 194 | public void TestIndexedByDescriptorAndOrdinalOutOfRange() { |
| 195 | TestAllExtensionsLite.Builder builder = TestAllExtensionsLite.CreateBuilder(); |
| 196 | Assert.AreEqual(0, builder.GetExtensionCount(UnitTestLiteProtoFile.RepeatedInt32ExtensionLite)); |
| 197 | |
| 198 | IFieldDescriptorLite f = UnitTestLiteProtoFile.RepeatedInt32ExtensionLite.Descriptor; |
| 199 | builder[f, 0] = 123; |
| 200 | } |
| 201 | |
| 202 | [Test] |
| 203 | public void TestClearFieldByDescriptor() { |
| 204 | TestAllExtensionsLite.Builder builder = TestAllExtensionsLite.CreateBuilder() |
| 205 | .AddExtension(UnitTestLiteProtoFile.RepeatedInt32ExtensionLite, 0); |
| 206 | Assert.AreEqual(1, builder.GetExtensionCount(UnitTestLiteProtoFile.RepeatedInt32ExtensionLite)); |
| 207 | |
| 208 | IFieldDescriptorLite f = UnitTestLiteProtoFile.RepeatedInt32ExtensionLite.Descriptor; |
| 209 | builder.ClearField(f); |
| 210 | Assert.AreEqual(0, builder.GetExtensionCount(UnitTestLiteProtoFile.RepeatedInt32ExtensionLite)); |
| 211 | } |
| 212 | |
| 213 | [Test] |
| 214 | public void TestAddRepeatedFieldByDescriptor() { |
| 215 | TestAllExtensionsLite.Builder builder = TestAllExtensionsLite.CreateBuilder() |
| 216 | .AddExtension(UnitTestLiteProtoFile.RepeatedInt32ExtensionLite, 0); |
| 217 | Assert.AreEqual(1, builder.GetExtensionCount(UnitTestLiteProtoFile.RepeatedInt32ExtensionLite)); |
| 218 | |
| 219 | IFieldDescriptorLite f = UnitTestLiteProtoFile.RepeatedInt32ExtensionLite.Descriptor; |
| 220 | builder.AddRepeatedField(f, 123); |
| 221 | Assert.AreEqual(2, builder.GetExtensionCount(UnitTestLiteProtoFile.RepeatedInt32ExtensionLite)); |
| 222 | Assert.AreEqual(123, builder.GetExtension(UnitTestLiteProtoFile.RepeatedInt32ExtensionLite, 1)); |
| 223 | } |
| 224 | } |
| 225 | } |