Commit ba02091e authored by Jon Skeet's avatar Jon Skeet

New unit tests and corresponding bug fixes.

parent bef2caf5
This diff is collapsed.
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);
}
}
}
......@@ -48,6 +48,8 @@
<Compile Include="ByteStringTest.cs" />
<Compile Include="CodedInputStreamTest.cs" />
<Compile Include="CodedOutputStreamTest.cs" />
<Compile Include="DescriptorsTest.cs" />
<Compile Include="DynamicMessageTest.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ReflectionTester.cs" />
<Compile Include="TestProtos\UnitTestEmbedOptimizeForProtoFile.cs" />
......
......@@ -33,6 +33,13 @@ namespace Google.ProtocolBuffers.Descriptors {
file.DescriptorPool.AddSymbol(this);
}
/// <value>
/// If this is a nested type, get the outer descriptor, otherwise null.
/// </value>
public MessageDescriptor ContainingType {
get { return containingType; }
}
/// <value>
/// An unmodifiable list of this message type's fields.
/// </value>
......
......@@ -28,7 +28,7 @@ namespace Google.ProtocolBuffers.Descriptors {
/// The method's input type.
/// </value>
public MessageDescriptor OutputType {
get { return inputType; }
get { return outputType; }
}
internal MethodDescriptor(MethodDescriptorProto proto, FileDescriptor file,
......
......@@ -28,6 +28,15 @@ namespace Google.ProtocolBuffers.Descriptors {
get { return methods; }
}
/// <summary>
/// Finds a method by name.
/// </summary>
/// <param name="name">The unqualified name of the method (e.g. "Foo").</param>
/// <returns>The method's decsriptor, or null if not found.</returns>
public MethodDescriptor FindMethodByName(String name) {
return File.DescriptorPool.FindSymbol<MethodDescriptor>(FullName + "." + name);
}
internal void CrossLink() {
foreach (MethodDescriptor method in methods) {
method.CrossLink();
......
......@@ -233,6 +233,10 @@ namespace Google.ProtocolBuffers {
this.unknownFields = UnknownFieldSet.DefaultInstance;
}
public DynamicMessage Build() {
return (DynamicMessage)((IBuilder)this).Build();
}
public override IBuilder Clear() {
fields.Clear();
return this;
......
......@@ -325,8 +325,13 @@ namespace Google.ProtocolBuffers {
if (fields.TryGetValue(field, out result)) {
return result;
}
// This will just do the right thing
if (field.MappedType == MappedType.Message) {
if (field.IsRepeated) {
return new List<object>();
} else {
return null;
}
}
return field.DefaultValue;
}
set {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment