diff options
author | Jon Skeet <skeet@pobox.com> | 2008-08-14 20:35:24 +0100 |
---|---|---|
committer | Jon Skeet <skeet@pobox.com> | 2008-08-14 20:35:24 +0100 |
commit | ba02091ef822ffa9248fdd6450703e5ff045838e (patch) | |
tree | 9ca66f702a890ec4f8740a69857d87cf6e439280 /csharp/ProtocolBuffers.Test/DynamicMessageTest.cs | |
parent | bef2caf5e49dbcf6b926d01cfb0948dedee49c93 (diff) | |
download | protobuf-ba02091ef822ffa9248fdd6450703e5ff045838e.tar.gz protobuf-ba02091ef822ffa9248fdd6450703e5ff045838e.tar.bz2 protobuf-ba02091ef822ffa9248fdd6450703e5ff045838e.zip |
New unit tests and corresponding bug fixes.
Diffstat (limited to 'csharp/ProtocolBuffers.Test/DynamicMessageTest.cs')
-rw-r--r-- | csharp/ProtocolBuffers.Test/DynamicMessageTest.cs | 103 |
1 files changed, 103 insertions, 0 deletions
diff --git a/csharp/ProtocolBuffers.Test/DynamicMessageTest.cs b/csharp/ProtocolBuffers.Test/DynamicMessageTest.cs new file mode 100644 index 00000000..4a666935 --- /dev/null +++ b/csharp/ProtocolBuffers.Test/DynamicMessageTest.cs @@ -0,0 +1,103 @@ +using System; +using System.Collections.Generic; +using System.Text; +using NUnit.Framework; +using Google.ProtocolBuffers.TestProtos; + +namespace Google.ProtocolBuffers { + [TestFixture] + public class DynamicMessageTest { + + private ReflectionTester reflectionTester; + + private ReflectionTester extensionsReflectionTester; + + [SetUp] + public void SetUp() { + reflectionTester = new ReflectionTester(TestAllTypes.Descriptor, null); + extensionsReflectionTester = new ReflectionTester(TestAllExtensions.Descriptor, TestUtil.CreateExtensionRegistry()); + } + + [Test] + public void DynamicMessageAccessors() { + IBuilder builder = DynamicMessage.CreateBuilder(TestAllTypes.Descriptor); + reflectionTester.SetAllFieldsViaReflection(builder); + IMessage message = builder.Build(); + reflectionTester.AssertAllFieldsSetViaReflection(message); + } + + [Test] + 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.Build(); + extensionsReflectionTester.AssertAllFieldsSetViaReflection(message); + } + + [Test] + public void DynamicMessageRepeatedSetters() { + IBuilder builder = DynamicMessage.CreateBuilder(TestAllTypes.Descriptor); + reflectionTester.SetAllFieldsViaReflection(builder); + reflectionTester.ModifyRepeatedFieldsViaReflection(builder); + IMessage message = builder.Build(); + reflectionTester.AssertRepeatedFieldsModifiedViaReflection(message); + } + + [Test] + public void DynamicMessageDefaults() { + reflectionTester.AssertClearViaReflection(DynamicMessage.GetDefaultInstance(TestAllTypes.Descriptor)); + reflectionTester.AssertClearViaReflection(DynamicMessage.CreateBuilder(TestAllTypes.Descriptor).Build()); + } + + [Test] + public void DynamicMessageSerializedSize() { + TestAllTypes message = TestUtil.GetAllSet(); + + IBuilder dynamicBuilder = DynamicMessage.CreateBuilder(TestAllTypes.Descriptor); + reflectionTester.SetAllFieldsViaReflection(dynamicBuilder); + IMessage dynamicMessage = dynamicBuilder.Build(); + + Assert.AreEqual(message.SerializedSize, dynamicMessage.SerializedSize); + } + + [Test] + public void DynamicMessageSerialization() { + IBuilder builder = DynamicMessage.CreateBuilder(TestAllTypes.Descriptor); + reflectionTester.SetAllFieldsViaReflection(builder); + IMessage message = builder.Build(); + + 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); + } + + [Test] + 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); + } + + [Test] + public void DynamicMessageCopy() { + TestAllTypes.Builder builder = TestAllTypes.CreateBuilder(); + TestUtil.SetAllFields(builder); + TestAllTypes message = builder.Build(); + + DynamicMessage copy = DynamicMessage.CreateBuilder(message).Build(); + reflectionTester.AssertAllFieldsSetViaReflection(copy); + } + } +} |