Jon Skeet | 6803686 | 2008-10-22 13:30:34 +0100 | [diff] [blame] | 1 | using Google.ProtocolBuffers.TestProtos; |
| 2 | // Protocol Buffers - Google's data interchange format |
| 3 | // Copyright 2008 Google Inc. |
| 4 | // http://code.google.com/p/protobuf/ |
| 5 | // |
| 6 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | // you may not use this file except in compliance with the License. |
| 8 | // You may obtain a copy of the License at |
| 9 | // |
| 10 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | // |
| 12 | // Unless required by applicable law or agreed to in writing, software |
| 13 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | // See the License for the specific language governing permissions and |
| 16 | // limitations under the License. |
| 17 | using NUnit.Framework; |
| 18 | |
| 19 | namespace Google.ProtocolBuffers { |
| 20 | [TestFixture] |
| 21 | public class DynamicMessageTest { |
| 22 | |
| 23 | private ReflectionTester reflectionTester; |
| 24 | private ReflectionTester extensionsReflectionTester; |
| 25 | |
| 26 | [SetUp] |
| 27 | public void SetUp() { |
| 28 | reflectionTester = ReflectionTester.CreateTestAllTypesInstance(); |
| 29 | extensionsReflectionTester = ReflectionTester.CreateTestAllExtensionsInstance(); |
| 30 | } |
| 31 | |
| 32 | [Test] |
| 33 | public void DynamicMessageAccessors() { |
| 34 | IBuilder builder = DynamicMessage.CreateBuilder(TestAllTypes.Descriptor); |
| 35 | reflectionTester.SetAllFieldsViaReflection(builder); |
| 36 | IMessage message = builder.WeakBuild(); |
| 37 | reflectionTester.AssertAllFieldsSetViaReflection(message); |
| 38 | } |
| 39 | |
| 40 | [Test] |
| 41 | public void DynamicMessageExtensionAccessors() { |
| 42 | // We don't need to extensively test DynamicMessage's handling of |
| 43 | // extensions because, frankly, it doesn't do anything special with them. |
| 44 | // It treats them just like any other fields. |
| 45 | IBuilder builder = DynamicMessage.CreateBuilder(TestAllExtensions.Descriptor); |
| 46 | extensionsReflectionTester.SetAllFieldsViaReflection(builder); |
| 47 | IMessage message = builder.WeakBuild(); |
| 48 | extensionsReflectionTester.AssertAllFieldsSetViaReflection(message); |
| 49 | } |
| 50 | |
| 51 | [Test] |
| 52 | public void DynamicMessageRepeatedSetters() { |
| 53 | IBuilder builder = DynamicMessage.CreateBuilder(TestAllTypes.Descriptor); |
| 54 | reflectionTester.SetAllFieldsViaReflection(builder); |
| 55 | reflectionTester.ModifyRepeatedFieldsViaReflection(builder); |
| 56 | IMessage message = builder.WeakBuild(); |
| 57 | reflectionTester.AssertRepeatedFieldsModifiedViaReflection(message); |
| 58 | } |
| 59 | |
| 60 | [Test] |
| 61 | public void DynamicMessageDefaults() { |
| 62 | reflectionTester.AssertClearViaReflection(DynamicMessage.GetDefaultInstance(TestAllTypes.Descriptor)); |
| 63 | reflectionTester.AssertClearViaReflection(DynamicMessage.CreateBuilder(TestAllTypes.Descriptor).Build()); |
| 64 | } |
| 65 | |
| 66 | [Test] |
| 67 | public void DynamicMessageSerializedSize() { |
| 68 | TestAllTypes message = TestUtil.GetAllSet(); |
| 69 | |
| 70 | IBuilder dynamicBuilder = DynamicMessage.CreateBuilder(TestAllTypes.Descriptor); |
| 71 | reflectionTester.SetAllFieldsViaReflection(dynamicBuilder); |
| 72 | IMessage dynamicMessage = dynamicBuilder.WeakBuild(); |
| 73 | |
| 74 | Assert.AreEqual(message.SerializedSize, dynamicMessage.SerializedSize); |
| 75 | } |
| 76 | |
| 77 | [Test] |
| 78 | public void DynamicMessageSerialization() { |
| 79 | IBuilder builder = DynamicMessage.CreateBuilder(TestAllTypes.Descriptor); |
| 80 | reflectionTester.SetAllFieldsViaReflection(builder); |
| 81 | IMessage message = builder.WeakBuild(); |
| 82 | |
| 83 | ByteString rawBytes = message.ToByteString(); |
| 84 | TestAllTypes message2 = TestAllTypes.ParseFrom(rawBytes); |
| 85 | |
| 86 | TestUtil.AssertAllFieldsSet(message2); |
| 87 | |
| 88 | // In fact, the serialized forms should be exactly the same, byte-for-byte. |
| 89 | Assert.AreEqual(TestUtil.GetAllSet().ToByteString(), rawBytes); |
| 90 | } |
| 91 | |
| 92 | [Test] |
| 93 | public void DynamicMessageParsing() { |
| 94 | TestAllTypes.Builder builder = TestAllTypes.CreateBuilder(); |
| 95 | TestUtil.SetAllFields(builder); |
| 96 | TestAllTypes message = builder.Build(); |
| 97 | |
| 98 | ByteString rawBytes = message.ToByteString(); |
| 99 | |
| 100 | IMessage message2 = DynamicMessage.ParseFrom(TestAllTypes.Descriptor, rawBytes); |
| 101 | reflectionTester.AssertAllFieldsSetViaReflection(message2); |
| 102 | } |
| 103 | |
| 104 | [Test] |
| 105 | public void DynamicMessageCopy() { |
| 106 | TestAllTypes.Builder builder = TestAllTypes.CreateBuilder(); |
| 107 | TestUtil.SetAllFields(builder); |
| 108 | TestAllTypes message = builder.Build(); |
| 109 | |
| 110 | DynamicMessage copy = DynamicMessage.CreateBuilder(message).Build(); |
| 111 | reflectionTester.AssertAllFieldsSetViaReflection(copy); |
| 112 | } |
| 113 | } |
| 114 | } |