diff options
author | Jan Tattermusch <jtattermusch@users.noreply.github.com> | 2015-04-17 14:15:52 -0700 |
---|---|---|
committer | Jan Tattermusch <jtattermusch@users.noreply.github.com> | 2015-04-17 14:15:52 -0700 |
commit | 01c8c35ca3d3424a9473dd22ea2456933b3e8d36 (patch) | |
tree | de582cb9ef3e449f24015dec09f80e998806b26a /csharp/src/ProtocolBuffers.Test/DynamicMessageTest.cs | |
parent | 813d6d652d8091e9365bfbd01efb5f2ee8bdfbce (diff) | |
parent | 1eab56aee8dddcb656db67d9fb87a8cc6a2ac112 (diff) | |
download | protobuf-01c8c35ca3d3424a9473dd22ea2456933b3e8d36.tar.gz protobuf-01c8c35ca3d3424a9473dd22ea2456933b3e8d36.tar.bz2 protobuf-01c8c35ca3d3424a9473dd22ea2456933b3e8d36.zip |
Merge pull request #2 from jtattermusch/csharp_protobuf_imported
Pulling protobuf-csharp-port into csharp/ directory
Diffstat (limited to 'csharp/src/ProtocolBuffers.Test/DynamicMessageTest.cs')
-rw-r--r-- | csharp/src/ProtocolBuffers.Test/DynamicMessageTest.cs | 237 |
1 files changed, 237 insertions, 0 deletions
diff --git a/csharp/src/ProtocolBuffers.Test/DynamicMessageTest.cs b/csharp/src/ProtocolBuffers.Test/DynamicMessageTest.cs new file mode 100644 index 00000000..92515999 --- /dev/null +++ b/csharp/src/ProtocolBuffers.Test/DynamicMessageTest.cs @@ -0,0 +1,237 @@ +#region Copyright notice and license
+
+// Protocol Buffers - Google's data interchange format
+// Copyright 2008 Google Inc. All rights reserved.
+// http://github.com/jskeet/dotnet-protobufs/
+// Original C++/Java/Python code:
+// http://code.google.com/p/protobuf/
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are
+// met:
+//
+// * Redistributions of source code must retain the above copyright
+// notice, this list of conditions and the following disclaimer.
+// * Redistributions in binary form must reproduce the above
+// copyright notice, this list of conditions and the following disclaimer
+// in the documentation and/or other materials provided with the
+// distribution.
+// * Neither the name of Google Inc. nor the names of its
+// contributors may be used to endorse or promote products derived from
+// this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+#endregion
+
+using System;
+using System.Collections.Generic;
+using Google.ProtocolBuffers.TestProtos;
+using Microsoft.VisualStudio.TestTools.UnitTesting;
+
+namespace Google.ProtocolBuffers
+{
+ [TestClass]
+ public class DynamicMessageTest
+ {
+ private ReflectionTester reflectionTester;
+ private ReflectionTester extensionsReflectionTester;
+ private ReflectionTester packedReflectionTester;
+
+ [TestInitialize]
+ public void SetUp()
+ {
+ reflectionTester = ReflectionTester.CreateTestAllTypesInstance();
+ extensionsReflectionTester = ReflectionTester.CreateTestAllExtensionsInstance();
+ packedReflectionTester = ReflectionTester.CreateTestPackedTypesInstance();
+ }
+
+ [TestMethod]
+ public void DynamicMessageAccessors()
+ {
+ IBuilder builder = DynamicMessage.CreateBuilder(TestAllTypes.Descriptor);
+ reflectionTester.SetAllFieldsViaReflection(builder);
+ IMessage message = builder.WeakBuild();
+ reflectionTester.AssertAllFieldsSetViaReflection(message);
+ }
+
+ [TestMethod]
+ public void DoubleBuildError()
+ {
+ DynamicMessage.Builder builder = DynamicMessage.CreateBuilder(TestAllTypes.Descriptor);
+ builder.Build();
+ try
+ {
+ builder.Build();
+ Assert.Fail("Should have thrown exception.");
+ }
+ catch (InvalidOperationException)
+ {
+ // Success.
+ }
+ }
+
+ [TestMethod]
+ public void DynamicMessageSettersRejectNull()
+ {
+ IBuilder builder = DynamicMessage.CreateBuilder(TestAllTypes.Descriptor);
+ reflectionTester.AssertReflectionSettersRejectNull(builder);
+ }
+
+ [TestMethod]
+ public void DynamicMessageExtensionAccessors()
+ {
+ // We don't need to extensively test DynamicMessage's handling of
+ // extensions because, frankly, it doesn't do anything special with them.
+ // It treats them just like any other fields.
+ IBuilder builder = DynamicMessage.CreateBuilder(TestAllExtensions.Descriptor);
+ extensionsReflectionTester.SetAllFieldsViaReflection(builder);
+ IMessage message = builder.WeakBuild();
+ extensionsReflectionTester.AssertAllFieldsSetViaReflection(message);
+ }
+
+ [TestMethod]
+ public void DynamicMessageExtensionSettersRejectNull()
+ {
+ IBuilder builder = DynamicMessage.CreateBuilder(TestAllExtensions.Descriptor);
+ extensionsReflectionTester.AssertReflectionSettersRejectNull(builder);
+ }
+
+ [TestMethod]
+ public void DynamicMessageRepeatedSetters()
+ {
+ IBuilder builder = DynamicMessage.CreateBuilder(TestAllTypes.Descriptor);
+ reflectionTester.SetAllFieldsViaReflection(builder);
+ reflectionTester.ModifyRepeatedFieldsViaReflection(builder);
+ IMessage message = builder.WeakBuild();
+ reflectionTester.AssertRepeatedFieldsModifiedViaReflection(message);
+ }
+
+ [TestMethod]
+ public void DynamicMessageRepeatedSettersRejectNull()
+ {
+ IBuilder builder = DynamicMessage.CreateBuilder(TestAllTypes.Descriptor);
+ reflectionTester.AssertReflectionRepeatedSettersRejectNull(builder);
+ }
+
+ [TestMethod]
+ public void DynamicMessageDefaults()
+ {
+ reflectionTester.AssertClearViaReflection(DynamicMessage.GetDefaultInstance(TestAllTypes.Descriptor));
+ reflectionTester.AssertClearViaReflection(DynamicMessage.CreateBuilder(TestAllTypes.Descriptor).Build());
+ }
+
+ [TestMethod]
+ public void DynamicMessageSerializedSize()
+ {
+ TestAllTypes message = TestUtil.GetAllSet();
+
+ IBuilder dynamicBuilder = DynamicMessage.CreateBuilder(TestAllTypes.Descriptor);
+ reflectionTester.SetAllFieldsViaReflection(dynamicBuilder);
+ IMessage dynamicMessage = dynamicBuilder.WeakBuild();
+
+ Assert.AreEqual(message.SerializedSize, dynamicMessage.SerializedSize);
+ }
+
+ [TestMethod]
+ public void DynamicMessageSerialization()
+ {
+ IBuilder builder = DynamicMessage.CreateBuilder(TestAllTypes.Descriptor);
+ reflectionTester.SetAllFieldsViaReflection(builder);
+ IMessage message = builder.WeakBuild();
+
+ ByteString rawBytes = message.ToByteString();
+ TestAllTypes message2 = TestAllTypes.ParseFrom(rawBytes);
+
+ TestUtil.AssertAllFieldsSet(message2);
+
+ // In fact, the serialized forms should be exactly the same, byte-for-byte.
+ Assert.AreEqual(TestUtil.GetAllSet().ToByteString(), rawBytes);
+ }
+
+ [TestMethod]
+ public void DynamicMessageParsing()
+ {
+ TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
+ TestUtil.SetAllFields(builder);
+ TestAllTypes message = builder.Build();
+
+ ByteString rawBytes = message.ToByteString();
+
+ IMessage message2 = DynamicMessage.ParseFrom(TestAllTypes.Descriptor, rawBytes);
+ reflectionTester.AssertAllFieldsSetViaReflection(message2);
+ }
+
+ [TestMethod]
+ public void DynamicMessagePackedSerialization()
+ {
+ IBuilder builder = DynamicMessage.CreateBuilder(TestPackedTypes.Descriptor);
+ packedReflectionTester.SetPackedFieldsViaReflection(builder);
+ IMessage message = builder.WeakBuild();
+
+ ByteString rawBytes = message.ToByteString();
+ TestPackedTypes message2 = TestPackedTypes.ParseFrom(rawBytes);
+
+ TestUtil.AssertPackedFieldsSet(message2);
+
+ // In fact, the serialized forms should be exactly the same, byte-for-byte.
+ Assert.AreEqual(TestUtil.GetPackedSet().ToByteString(), rawBytes);
+ }
+
+ [TestMethod]
+ public void testDynamicMessagePackedParsing()
+ {
+ TestPackedTypes.Builder builder = TestPackedTypes.CreateBuilder();
+ TestUtil.SetPackedFields(builder);
+ TestPackedTypes message = builder.Build();
+
+ ByteString rawBytes = message.ToByteString();
+
+ IMessage message2 = DynamicMessage.ParseFrom(TestPackedTypes.Descriptor, rawBytes);
+ packedReflectionTester.AssertPackedFieldsSetViaReflection(message2);
+ }
+
+ [TestMethod]
+ public void DynamicMessageCopy()
+ {
+ TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
+ TestUtil.SetAllFields(builder);
+ TestAllTypes message = builder.Build();
+
+ DynamicMessage copy = DynamicMessage.CreateBuilder(message).Build();
+ reflectionTester.AssertAllFieldsSetViaReflection(copy);
+ }
+
+ [TestMethod]
+ public void ToBuilder()
+ {
+ DynamicMessage.Builder builder =
+ DynamicMessage.CreateBuilder(TestAllTypes.Descriptor);
+ reflectionTester.SetAllFieldsViaReflection(builder);
+ int unknownFieldNum = 9;
+ ulong unknownFieldVal = 90;
+ builder.SetUnknownFields(UnknownFieldSet.CreateBuilder()
+ .AddField(unknownFieldNum,
+ UnknownField.CreateBuilder().AddVarint(unknownFieldVal).Build())
+ .Build());
+ DynamicMessage message = builder.Build();
+
+ DynamicMessage derived = message.ToBuilder().Build();
+ reflectionTester.AssertAllFieldsSetViaReflection(derived);
+
+ IList<ulong> values = derived.UnknownFields.FieldDictionary[unknownFieldNum].VarintList;
+ Assert.AreEqual(1, values.Count);
+ Assert.AreEqual(unknownFieldVal, values[0]);
+ }
+ }
+}
\ No newline at end of file |