Commit ba02091e authored by Jon Skeet's avatar Jon Skeet

New unit tests and corresponding bug fixes.

parent bef2caf5
using System;
using System.Collections.Generic;
using System.Text;
using NUnit.Framework;
using Google.ProtocolBuffers.Descriptors;
using Google.ProtocolBuffers.TestProtos;
namespace Google.ProtocolBuffers {
/// <summary>
/// Tests for descriptors. (Not in its own namespace or broken up into individual classes as the
/// size doesn't warrant it. On the other hand, this makes me feel a bit dirty...)
/// </summary>
[TestFixture]
public class DescriptorsTest {
[Test]
public void FileDescriptor() {
FileDescriptor file = UnitTestProtoFile.Descriptor;
Assert.AreEqual("google/protobuf/unittest.proto", file.Name);
Assert.AreEqual("protobuf_unittest", file.Package);
Assert.AreEqual("UnittestProto", file.Options.JavaOuterClassname);
Assert.AreEqual("google/protobuf/unittest.proto", file.Proto.Name);
Assert.AreEqual(1, file.Dependencies.Count);
Assert.AreEqual(UnitTestImportProtoFile.Descriptor, file.Dependencies[0]);
MessageDescriptor messageType = TestAllTypes.Descriptor;
Assert.AreEqual(messageType, file.MessageTypes[0]);
Assert.AreEqual(messageType, file.FindTypeByName<MessageDescriptor>("TestAllTypes"));
Assert.IsNull(file.FindTypeByName<MessageDescriptor>("NoSuchType"));
Assert.IsNull(file.FindTypeByName<MessageDescriptor>("protobuf_unittest.TestAllTypes"));
for (int i = 0; i < file.MessageTypes.Count; i++) {
Assert.AreEqual(i, file.MessageTypes[i].Index);
}
Assert.AreEqual(file.EnumTypes[0], file.FindTypeByName<EnumDescriptor>("ForeignEnum"));
Assert.IsNull(file.FindTypeByName<EnumDescriptor>("NoSuchType"));
Assert.IsNull(file.FindTypeByName<EnumDescriptor>("protobuf_unittest.ForeignEnum"));
Assert.AreEqual(1, UnitTestImportProtoFile.Descriptor.EnumTypes.Count);
Assert.AreEqual("ImportEnum", UnitTestImportProtoFile.Descriptor.EnumTypes[0].Name);
for (int i = 0; i < file.EnumTypes.Count; i++) {
Assert.AreEqual(i, file.EnumTypes[i].Index);
}
ServiceDescriptor service = TestService.Descriptor;
Assert.AreEqual(service, file.Services[0]);
Assert.AreEqual(service, file.FindTypeByName<ServiceDescriptor>("TestService"));
Assert.IsNull(file.FindTypeByName<ServiceDescriptor>("NoSuchType"));
Assert.IsNull(file.FindTypeByName<ServiceDescriptor>("protobuf_unittest.TestService"));
Assert.AreEqual(0, UnitTestImportProtoFile.Descriptor.Services.Count);
for (int i = 0; i < file.Services.Count; i++) {
Assert.AreEqual(i, file.Services[i].Index);
}
FieldDescriptor extension = UnitTestProtoFile.OptionalInt32Extension.Descriptor;
Assert.AreEqual(extension, file.Extensions[0]);
Assert.AreEqual(extension, file.FindTypeByName<FieldDescriptor>("optional_int32_extension"));
Assert.IsNull(file.FindTypeByName<FieldDescriptor>("no_such_ext"));
Assert.IsNull(file.FindTypeByName<FieldDescriptor>("protobuf_unittest.optional_int32_extension"));
Assert.AreEqual(0, UnitTestImportProtoFile.Descriptor.Extensions.Count);
for (int i = 0; i < file.Extensions.Count; i++) {
Assert.AreEqual(i, file.Extensions[i].Index);
}
}
[Test]
public void MessageDescriptor() {
MessageDescriptor messageType = TestAllTypes.Descriptor;
MessageDescriptor nestedType = TestAllTypes.Types.NestedMessage.Descriptor;
Assert.AreEqual("TestAllTypes", messageType.Name);
Assert.AreEqual("protobuf_unittest.TestAllTypes", messageType.FullName);
Assert.AreEqual(UnitTestProtoFile.Descriptor, messageType.File);
Assert.IsNull(messageType.ContainingType);
Assert.AreEqual(DescriptorProtos.MessageOptions.DefaultInstance, messageType.Options);
Assert.AreEqual("TestAllTypes", messageType.Proto.Name);
Assert.AreEqual("NestedMessage", nestedType.Name);
Assert.AreEqual("protobuf_unittest.TestAllTypes.NestedMessage", nestedType.FullName);
Assert.AreEqual(UnitTestProtoFile.Descriptor, nestedType.File);
Assert.AreEqual(messageType, nestedType.ContainingType);
FieldDescriptor field = messageType.Fields[0];
Assert.AreEqual("optional_int32", field.Name);
Assert.AreEqual(field, messageType.FindDescriptor<FieldDescriptor>("optional_int32"));
Assert.IsNull(messageType.FindDescriptor<FieldDescriptor>("no_such_field"));
Assert.AreEqual(field, messageType.FindFieldByNumber(1));
Assert.IsNull(messageType.FindFieldByNumber(571283));
for (int i = 0; i < messageType.Fields.Count; i++) {
Assert.AreEqual(i, messageType.Fields[i].Index);
}
Assert.AreEqual(nestedType, messageType.NestedTypes[0]);
Assert.AreEqual(nestedType, messageType.FindDescriptor<MessageDescriptor>("NestedMessage"));
Assert.IsNull(messageType.FindDescriptor<MessageDescriptor>("NoSuchType"));
for (int i = 0; i < messageType.NestedTypes.Count; i++) {
Assert.AreEqual(i, messageType.NestedTypes[i].Index);
}
Assert.AreEqual(messageType.EnumTypes[0], messageType.FindDescriptor<EnumDescriptor>("NestedEnum"));
Assert.IsNull(messageType.FindDescriptor<EnumDescriptor>("NoSuchType"));
for (int i = 0; i < messageType.EnumTypes.Count; i++) {
Assert.AreEqual(i, messageType.EnumTypes[i].Index);
}
}
[Test]
public void FieldDescriptor() {
MessageDescriptor messageType = TestAllTypes.Descriptor;
FieldDescriptor primitiveField = messageType.FindDescriptor<FieldDescriptor>("optional_int32");
FieldDescriptor enumField = messageType.FindDescriptor<FieldDescriptor>("optional_nested_enum");
FieldDescriptor messageField = messageType.FindDescriptor<FieldDescriptor>("optional_foreign_message");
FieldDescriptor cordField = messageType.FindDescriptor<FieldDescriptor>("optional_cord");
FieldDescriptor extension = UnitTestProtoFile.OptionalInt32Extension.Descriptor;
FieldDescriptor nestedExtension = TestRequired.Types.Single.Descriptor;
Assert.AreEqual("optional_int32", primitiveField.Name);
Assert.AreEqual("protobuf_unittest.TestAllTypes.optional_int32",
primitiveField.FullName);
Assert.AreEqual(1, primitiveField.FieldNumber);
Assert.AreEqual(messageType, primitiveField.ContainingType);
Assert.AreEqual(UnitTestProtoFile.Descriptor, primitiveField.File);
Assert.AreEqual(FieldType.Int32, primitiveField.FieldType);
Assert.AreEqual(MappedType.Int32, primitiveField.MappedType);
Assert.AreEqual(DescriptorProtos.FieldOptions.DefaultInstance, primitiveField.Options);
Assert.IsFalse(primitiveField.IsExtension);
Assert.AreEqual("optional_int32", primitiveField.Proto.Name);
Assert.AreEqual("optional_nested_enum", enumField.Name);
Assert.AreEqual(FieldType.Enum, enumField.FieldType);
Assert.AreEqual(MappedType.Enum, enumField.MappedType);
// Assert.AreEqual(TestAllTypes.Types.NestedEnum.Descriptor, enumField.EnumType);
Assert.AreEqual("optional_foreign_message", messageField.Name);
Assert.AreEqual(FieldType.Message, messageField.FieldType);
Assert.AreEqual(MappedType.Message, messageField.MappedType);
Assert.AreEqual(ForeignMessage.Descriptor, messageField.MessageType);
Assert.AreEqual("optional_cord", cordField.Name);
Assert.AreEqual(FieldType.String, cordField.FieldType);
Assert.AreEqual(MappedType.String, cordField.MappedType);
Assert.AreEqual(DescriptorProtos.FieldOptions.Types.CType.CORD, cordField.Options.Ctype);
Assert.AreEqual("optional_int32_extension", extension.Name);
Assert.AreEqual("protobuf_unittest.optional_int32_extension", extension.FullName);
Assert.AreEqual(1, extension.FieldNumber);
Assert.AreEqual(TestAllExtensions.Descriptor, extension.ContainingType);
Assert.AreEqual(UnitTestProtoFile.Descriptor, extension.File);
Assert.AreEqual(FieldType.Int32, extension.FieldType);
Assert.AreEqual(MappedType.Int32, extension.MappedType);
Assert.AreEqual(DescriptorProtos.FieldOptions.DefaultInstance,
extension.Options);
Assert.IsTrue(extension.IsExtension);
Assert.AreEqual(null, extension.ExtensionScope);
Assert.AreEqual("optional_int32_extension", extension.Proto.Name);
Assert.AreEqual("single", nestedExtension.Name);
Assert.AreEqual("protobuf_unittest.TestRequired.single",
nestedExtension.FullName);
Assert.AreEqual(TestRequired.Descriptor,
nestedExtension.ExtensionScope);
}
[Test]
public void FieldDescriptorLabel() {
FieldDescriptor requiredField =
TestRequired.Descriptor.FindDescriptor<FieldDescriptor>("a");
FieldDescriptor optionalField =
TestAllTypes.Descriptor.FindDescriptor<FieldDescriptor>("optional_int32");
FieldDescriptor repeatedField =
TestAllTypes.Descriptor.FindDescriptor<FieldDescriptor>("repeated_int32");
Assert.IsTrue(requiredField.IsRequired);
Assert.IsFalse(requiredField.IsRepeated);
Assert.IsFalse(optionalField.IsRequired);
Assert.IsFalse(optionalField.IsRepeated);
Assert.IsFalse(repeatedField.IsRequired);
Assert.IsTrue(repeatedField.IsRepeated);
}
[Test]
public void FieldDescriptorDefault() {
MessageDescriptor d = TestAllTypes.Descriptor;
Assert.IsFalse(d.FindDescriptor<FieldDescriptor>("optional_int32").HasDefaultValue);
Assert.AreEqual(0, d.FindDescriptor<FieldDescriptor>("optional_int32").DefaultValue);
Assert.IsTrue(d.FindDescriptor<FieldDescriptor>("default_int32").HasDefaultValue);
Assert.AreEqual(41, d.FindDescriptor<FieldDescriptor>("default_int32").DefaultValue);
d = TestExtremeDefaultValues.Descriptor;
Assert.AreEqual(ByteString.CopyFrom("\u0000\u0001\u0007\b\f\n\r\t\u000b\\\'\"\u00fe", Encoding.GetEncoding(28591)),
d.FindDescriptor<FieldDescriptor>("escaped_bytes").DefaultValue);
Assert.AreEqual(uint.MaxValue, d.FindDescriptor<FieldDescriptor>("large_uint32").DefaultValue);
Assert.AreEqual(ulong.MaxValue, d.FindDescriptor<FieldDescriptor>("large_uint64").DefaultValue);
}
[Test]
public void EnumDescriptor() {
// Note: this test is a bit different to the Java version because there's no static way of getting to the descriptor
EnumDescriptor enumType = UnitTestProtoFile.Descriptor.FindTypeByName<EnumDescriptor>("ForeignEnum");
EnumDescriptor nestedType = TestAllTypes.Descriptor.FindDescriptor<EnumDescriptor>("NestedEnum");
Assert.AreEqual("ForeignEnum", enumType.Name);
Assert.AreEqual("protobuf_unittest.ForeignEnum", enumType.FullName);
Assert.AreEqual(UnitTestProtoFile.Descriptor, enumType.File);
Assert.IsNull(enumType.ContainingType);
Assert.AreEqual(DescriptorProtos.EnumOptions.DefaultInstance,
enumType.Options);
Assert.AreEqual("NestedEnum", nestedType.Name);
Assert.AreEqual("protobuf_unittest.TestAllTypes.NestedEnum",
nestedType.FullName);
Assert.AreEqual(UnitTestProtoFile.Descriptor, nestedType.File);
Assert.AreEqual(TestAllTypes.Descriptor, nestedType.ContainingType);
EnumValueDescriptor value = enumType.FindValueByName("FOREIGN_FOO");
Assert.AreEqual(value, enumType.Values[0]);
Assert.AreEqual("FOREIGN_FOO", value.Name);
Assert.AreEqual(4, value.Number);
Assert.AreEqual((int) ForeignEnum.FOREIGN_FOO, value.Number);
Assert.AreEqual(value, enumType.FindValueByNumber(4));
Assert.IsNull(enumType.FindValueByName("NO_SUCH_VALUE"));
for (int i = 0; i < enumType.Values.Count; i++) {
Assert.AreEqual(i, enumType.Values[i].Index);
}
}
[Test]
public void ServiceDescriptor() {
ServiceDescriptor service = TestService.Descriptor;
Assert.AreEqual("TestService", service.Name);
Assert.AreEqual("protobuf_unittest.TestService", service.FullName);
Assert.AreEqual(UnitTestProtoFile.Descriptor, service.File);
Assert.AreEqual(2, service.Methods.Count);
MethodDescriptor fooMethod = service.Methods[0];
Assert.AreEqual("Foo", fooMethod.Name);
Assert.AreEqual(FooRequest.Descriptor, fooMethod.InputType);
Assert.AreEqual(FooResponse.Descriptor, fooMethod.OutputType);
Assert.AreEqual(fooMethod, service.FindMethodByName("Foo"));
MethodDescriptor barMethod = service.Methods[1];
Assert.AreEqual("Bar", barMethod.Name);
Assert.AreEqual(BarRequest.Descriptor, barMethod.InputType);
Assert.AreEqual(BarResponse.Descriptor, barMethod.OutputType);
Assert.AreEqual(barMethod, service.FindMethodByName("Bar"));
Assert.IsNull(service.FindMethodByName("NoSuchMethod"));
for (int i = 0; i < service.Methods.Count; i++) {
Assert.AreEqual(i, service.Methods[i].Index);
}
}
}
}
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" />
......
......@@ -169,12 +169,12 @@ namespace Google.ProtocolBuffers {
internal void SetAllFieldsViaReflection(IBuilder message) {
message[f("optional_int32" )] = 101 ;
message[f("optional_int64" )] = 102L;
message[f("optional_uint32" )] = 103 ;
message[f("optional_uint64" )] = 104L;
message[f("optional_uint32" )] = 103U ;
message[f("optional_uint64" )] = 104UL;
message[f("optional_sint32" )] = 105 ;
message[f("optional_sint64" )] = 106L;
message[f("optional_fixed32" )] = 107 ;
message[f("optional_fixed64" )] = 108L;
message[f("optional_fixed32" )] = 107U ;
message[f("optional_fixed64" )] = 108UL;
message[f("optional_sfixed32")] = 109 ;
message[f("optional_sfixed64")] = 110L;
message[f("optional_float" )] = 111F;
......@@ -199,12 +199,12 @@ namespace Google.ProtocolBuffers {
message.AddRepeatedField(f("repeated_int32" ), 201 );
message.AddRepeatedField(f("repeated_int64" ), 202L);
message.AddRepeatedField(f("repeated_uint32" ), 203 );
message.AddRepeatedField(f("repeated_uint64" ), 204L);
message.AddRepeatedField(f("repeated_uint32" ), 203U );
message.AddRepeatedField(f("repeated_uint64" ), 204UL);
message.AddRepeatedField(f("repeated_sint32" ), 205 );
message.AddRepeatedField(f("repeated_sint64" ), 206L);
message.AddRepeatedField(f("repeated_fixed32" ), 207 );
message.AddRepeatedField(f("repeated_fixed64" ), 208L);
message.AddRepeatedField(f("repeated_fixed32" ), 207U );
message.AddRepeatedField(f("repeated_fixed64" ), 208UL);
message.AddRepeatedField(f("repeated_sfixed32"), 209 );
message.AddRepeatedField(f("repeated_sfixed64"), 210L);
message.AddRepeatedField(f("repeated_float" ), 211F);
......@@ -229,12 +229,12 @@ namespace Google.ProtocolBuffers {
// Add a second one of each field.
message.AddRepeatedField(f("repeated_int32" ), 301 );
message.AddRepeatedField(f("repeated_int64" ), 302L);
message.AddRepeatedField(f("repeated_uint32" ), 303 );
message.AddRepeatedField(f("repeated_uint64" ), 304L);
message.AddRepeatedField(f("repeated_uint32" ), 303U );
message.AddRepeatedField(f("repeated_uint64" ), 304UL);
message.AddRepeatedField(f("repeated_sint32" ), 305 );
message.AddRepeatedField(f("repeated_sint64" ), 306L);
message.AddRepeatedField(f("repeated_fixed32" ), 307 );
message.AddRepeatedField(f("repeated_fixed64" ), 308L);
message.AddRepeatedField(f("repeated_fixed32" ), 307U );
message.AddRepeatedField(f("repeated_fixed64" ), 308UL);
message.AddRepeatedField(f("repeated_sfixed32"), 309 );
message.AddRepeatedField(f("repeated_sfixed64"), 310L);
message.AddRepeatedField(f("repeated_float" ), 311F);
......@@ -267,12 +267,12 @@ namespace Google.ProtocolBuffers {
message[f("default_int32" )] = 401 ;
message[f("default_int64" )] = 402L;
message[f("default_uint32" )] = 403 ;
message[f("default_uint64" )] = 404L;
message[f("default_uint32" )] = 403U ;
message[f("default_uint64" )] = 404UL;
message[f("default_sint32" )] = 405 ;
message[f("default_sint64" )] = 406L;
message[f("default_fixed32" )] = 407 ;
message[f("default_fixed64" )] = 408L;
message[f("default_fixed32" )] = 407U ;
message[f("default_fixed64" )] = 408UL;
message[f("default_sfixed32")] = 409 ;
message[f("default_sfixed64")] = 410L;
message[f("default_float" )] = 411F;
......@@ -299,12 +299,12 @@ namespace Google.ProtocolBuffers {
internal void ModifyRepeatedFieldsViaReflection(IBuilder message) {
message[f("repeated_int32" ), 1] = 501 ;
message[f("repeated_int64" ), 1] = 502L;
message[f("repeated_uint32" ), 1] = 503 ;
message[f("repeated_uint64" ), 1] = 504L;
message[f("repeated_uint32" ), 1] = 503U ;
message[f("repeated_uint64" ), 1] = 504UL;
message[f("repeated_sint32" ), 1] = 505 ;
message[f("repeated_sint64" ), 1] = 506L;
message[f("repeated_fixed32" ), 1] = 507 ;
message[f("repeated_fixed64" ), 1] = 508L;
message[f("repeated_fixed32" ), 1] = 507U ;
message[f("repeated_fixed64" ), 1] = 508UL;
message[f("repeated_sfixed32"), 1] = 509 ;
message[f("repeated_sfixed64"), 1] = 510L;
message[f("repeated_float" ), 1] = 511F;
......@@ -332,7 +332,7 @@ namespace Google.ProtocolBuffers {
/// Asserts that all fields of the specified message are set to the values
/// assigned by SetAllFields, using the IMessage reflection interface.
/// </summary>
public void assertAllFieldsSetViaReflection(IMessage message) {
public void AssertAllFieldsSetViaReflection(IMessage message) {
Assert.IsTrue(message.HasField(f("optional_int32" )));
Assert.IsTrue(message.HasField(f("optional_int64" )));
Assert.IsTrue(message.HasField(f("optional_uint32" )));
......@@ -368,12 +368,12 @@ namespace Google.ProtocolBuffers {
Assert.AreEqual(101 , message[f("optional_int32" )]);
Assert.AreEqual(102L , message[f("optional_int64" )]);
Assert.AreEqual(103 , message[f("optional_uint32" )]);
Assert.AreEqual(104L , message[f("optional_uint64" )]);
Assert.AreEqual(103U , message[f("optional_uint32" )]);
Assert.AreEqual(104UL , message[f("optional_uint64" )]);
Assert.AreEqual(105 , message[f("optional_sint32" )]);
Assert.AreEqual(106L , message[f("optional_sint64" )]);
Assert.AreEqual(107 , message[f("optional_fixed32" )]);
Assert.AreEqual(108L , message[f("optional_fixed64" )]);
Assert.AreEqual(107U , message[f("optional_fixed32" )]);
Assert.AreEqual(108UL , message[f("optional_fixed64" )]);
Assert.AreEqual(109 , message[f("optional_sfixed32")]);
Assert.AreEqual(110L , message[f("optional_sfixed64")]);
Assert.AreEqual(111F , message[f("optional_float" )]);
......@@ -425,12 +425,12 @@ namespace Google.ProtocolBuffers {
Assert.AreEqual(201 , message[f("repeated_int32" ), 0]);
Assert.AreEqual(202L , message[f("repeated_int64" ), 0]);
Assert.AreEqual(203 , message[f("repeated_uint32" ), 0]);
Assert.AreEqual(204L , message[f("repeated_uint64" ), 0]);
Assert.AreEqual(203U , message[f("repeated_uint32" ), 0]);
Assert.AreEqual(204UL, message[f("repeated_uint64" ), 0]);
Assert.AreEqual(205 , message[f("repeated_sint32" ), 0]);
Assert.AreEqual(206L , message[f("repeated_sint64" ), 0]);
Assert.AreEqual(207 , message[f("repeated_fixed32" ), 0]);
Assert.AreEqual(208L , message[f("repeated_fixed64" ), 0]);
Assert.AreEqual(207U , message[f("repeated_fixed32" ), 0]);
Assert.AreEqual(208UL, message[f("repeated_fixed64" ), 0]);
Assert.AreEqual(209 , message[f("repeated_sfixed32"), 0]);
Assert.AreEqual(210L , message[f("repeated_sfixed64"), 0]);
Assert.AreEqual(211F , message[f("repeated_float" ), 0]);
......@@ -453,12 +453,12 @@ namespace Google.ProtocolBuffers {
Assert.AreEqual(301 , message[f("repeated_int32" ), 1]);
Assert.AreEqual(302L , message[f("repeated_int64" ), 1]);
Assert.AreEqual(303 , message[f("repeated_uint32" ), 1]);
Assert.AreEqual(304L , message[f("repeated_uint64" ), 1]);
Assert.AreEqual(303U , message[f("repeated_uint32" ), 1]);
Assert.AreEqual(304UL, message[f("repeated_uint64" ), 1]);
Assert.AreEqual(305 , message[f("repeated_sint32" ), 1]);
Assert.AreEqual(306L , message[f("repeated_sint64" ), 1]);
Assert.AreEqual(307 , message[f("repeated_fixed32" ), 1]);
Assert.AreEqual(308L , message[f("repeated_fixed64" ), 1]);
Assert.AreEqual(307U , message[f("repeated_fixed32" ), 1]);
Assert.AreEqual(308UL, message[f("repeated_fixed64" ), 1]);
Assert.AreEqual(309 , message[f("repeated_sfixed32"), 1]);
Assert.AreEqual(310L , message[f("repeated_sfixed64"), 1]);
Assert.AreEqual(311F , message[f("repeated_float" ), 1]);
......@@ -469,12 +469,8 @@ namespace Google.ProtocolBuffers {
Assert.AreEqual(317,((IMessage)message[f("repeatedgroup"), 1])[repeatedGroupA]);
Assert.AreEqual(318,((IMessage)message[f("repeated_nested_message"), 1])[nestedB]);
Assert.AreEqual(319,
((IMessage)message[f("repeated_foreign_message"), 1])
[foreignC]);
Assert.AreEqual(320,
((IMessage)message[f("repeated_import_message"), 1])
[importD]);
Assert.AreEqual(319,((IMessage)message[f("repeated_foreign_message"), 1])[foreignC]);
Assert.AreEqual(320,((IMessage)message[f("repeated_import_message"), 1])[importD]);
Assert.AreEqual( nestedBaz, message[f("repeated_nested_enum" ),1]);
Assert.AreEqual(foreignBaz, message[f("repeated_foreign_enum"),1]);
......@@ -510,12 +506,12 @@ namespace Google.ProtocolBuffers {
Assert.AreEqual(401 , message[f("default_int32" )]);
Assert.AreEqual(402L , message[f("default_int64" )]);
Assert.AreEqual(403 , message[f("default_uint32" )]);
Assert.AreEqual(404L , message[f("default_uint64" )]);
Assert.AreEqual(403U , message[f("default_uint32" )]);
Assert.AreEqual(404UL, message[f("default_uint64" )]);
Assert.AreEqual(405 , message[f("default_sint32" )]);
Assert.AreEqual(406L , message[f("default_sint64" )]);
Assert.AreEqual(407 , message[f("default_fixed32" )]);
Assert.AreEqual(408L , message[f("default_fixed64" )]);
Assert.AreEqual(407U , message[f("default_fixed32" )]);
Assert.AreEqual(408UL, message[f("default_fixed64" )]);
Assert.AreEqual(409 , message[f("default_sfixed32")]);
Assert.AreEqual(410L , message[f("default_sfixed64")]);
Assert.AreEqual(411F , message[f("default_float" )]);
......@@ -532,14 +528,11 @@ namespace Google.ProtocolBuffers {
Assert.AreEqual("425", message[f("default_cord")]);
}
// -------------------------------------------------------------------
/**
* Assert (using {@code junit.framework.Assert}} that all fields of
* {@code message} are cleared, and that getting the fields returns their
* default values, using the {@link Message} reflection interface.
*/
public void assertClearViaReflection(IMessage message) {
/// <summary>
/// Assert that all fields of the message are cleared, and that
/// getting the fields returns their default values, using the reflection interface.
/// </summary>
public void AssertClearViaReflection(IMessage message) {
// has_blah() should initially be false for all optional fields.
Assert.IsFalse(message.HasField(f("optional_int32" )));
Assert.IsFalse(message.HasField(f("optional_int64" )));
......@@ -572,12 +565,12 @@ namespace Google.ProtocolBuffers {
// Optional fields without defaults are set to zero or something like it.
Assert.AreEqual(0 , message[f("optional_int32" )]);
Assert.AreEqual(0L , message[f("optional_int64" )]);
Assert.AreEqual(0 , message[f("optional_uint32" )]);
Assert.AreEqual(0L , message[f("optional_uint64" )]);
Assert.AreEqual(0U , message[f("optional_uint32" )]);
Assert.AreEqual(0UL , message[f("optional_uint64" )]);
Assert.AreEqual(0 , message[f("optional_sint32" )]);
Assert.AreEqual(0L , message[f("optional_sint64" )]);
Assert.AreEqual(0 , message[f("optional_fixed32" )]);
Assert.AreEqual(0L , message[f("optional_fixed64" )]);
Assert.AreEqual(0U , message[f("optional_fixed32" )]);
Assert.AreEqual(0UL , message[f("optional_fixed64" )]);
Assert.AreEqual(0 , message[f("optional_sfixed32")]);
Assert.AreEqual(0L , message[f("optional_sfixed64")]);
Assert.AreEqual(0F , message[f("optional_float" )]);
......@@ -587,16 +580,12 @@ namespace Google.ProtocolBuffers {
Assert.AreEqual(ByteString.Empty, message[f("optional_bytes")]);
// Embedded messages should also be clear.
Assert.IsFalse(
((IMessage)message[f("optionalgroup")]).HasField(groupA));
Assert.IsFalse(
((IMessage)message[f("optional_nested_message")])
Assert.IsFalse(((IMessage)message[f("optionalgroup")]).HasField(groupA));
Assert.IsFalse(((IMessage)message[f("optional_nested_message")])
.HasField(nestedB));
Assert.IsFalse(
((IMessage)message[f("optional_foreign_message")])
Assert.IsFalse(((IMessage)message[f("optional_foreign_message")])
.HasField(foreignC));
Assert.IsFalse(
((IMessage)message[f("optional_import_message")])
Assert.IsFalse(((IMessage)message[f("optional_import_message")])
.HasField(importD));
Assert.AreEqual(0,((IMessage)message[f("optionalgroup")])[groupA]);
......@@ -667,12 +656,12 @@ namespace Google.ProtocolBuffers {
// Fields with defaults have their default values (duh).
Assert.AreEqual( 41 , message[f("default_int32" )]);
Assert.AreEqual( 42L , message[f("default_int64" )]);
Assert.AreEqual( 43 , message[f("default_uint32" )]);
Assert.AreEqual( 44L , message[f("default_uint64" )]);
Assert.AreEqual( 43U , message[f("default_uint32" )]);
Assert.AreEqual( 44UL , message[f("default_uint64" )]);
Assert.AreEqual(-45 , message[f("default_sint32" )]);
Assert.AreEqual( 46L , message[f("default_sint64" )]);
Assert.AreEqual( 47 , message[f("default_fixed32" )]);
Assert.AreEqual( 48L , message[f("default_fixed64" )]);
Assert.AreEqual( 47U , message[f("default_fixed32" )]);
Assert.AreEqual( 48UL , message[f("default_fixed64" )]);
Assert.AreEqual( 49 , message[f("default_sfixed32")]);
Assert.AreEqual(-50L , message[f("default_sfixed64")]);
Assert.AreEqual( 51.5F , message[f("default_float" )]);
......@@ -724,12 +713,12 @@ namespace Google.ProtocolBuffers {
Assert.AreEqual(201 , message[f("repeated_int32" ), 0]);
Assert.AreEqual(202L , message[f("repeated_int64" ), 0]);
Assert.AreEqual(203 , message[f("repeated_uint32" ), 0]);
Assert.AreEqual(204L , message[f("repeated_uint64" ), 0]);
Assert.AreEqual(203U , message[f("repeated_uint32" ), 0]);
Assert.AreEqual(204UL, message[f("repeated_uint64" ), 0]);
Assert.AreEqual(205 , message[f("repeated_sint32" ), 0]);
Assert.AreEqual(206L , message[f("repeated_sint64" ), 0]);
Assert.AreEqual(207 , message[f("repeated_fixed32" ), 0]);
Assert.AreEqual(208L , message[f("repeated_fixed64" ), 0]);
Assert.AreEqual(207U , message[f("repeated_fixed32" ), 0]);
Assert.AreEqual(208UL, message[f("repeated_fixed64" ), 0]);
Assert.AreEqual(209 , message[f("repeated_sfixed32"), 0]);
Assert.AreEqual(210L , message[f("repeated_sfixed64"), 0]);
Assert.AreEqual(211F , message[f("repeated_float" ), 0]);
......@@ -752,12 +741,12 @@ namespace Google.ProtocolBuffers {
Assert.AreEqual(501 , message[f("repeated_int32" ), 1]);
Assert.AreEqual(502L , message[f("repeated_int64" ), 1]);
Assert.AreEqual(503 , message[f("repeated_uint32" ), 1]);
Assert.AreEqual(504L , message[f("repeated_uint64" ), 1]);
Assert.AreEqual(503U , message[f("repeated_uint32" ), 1]);
Assert.AreEqual(504UL, message[f("repeated_uint64" ), 1]);
Assert.AreEqual(505 , message[f("repeated_sint32" ), 1]);
Assert.AreEqual(506L , message[f("repeated_sint64" ), 1]);
Assert.AreEqual(507 , message[f("repeated_fixed32" ), 1]);
Assert.AreEqual(508L , message[f("repeated_fixed64" ), 1]);
Assert.AreEqual(507U , message[f("repeated_fixed32" ), 1]);
Assert.AreEqual(508UL, message[f("repeated_fixed64" ), 1]);
Assert.AreEqual(509 , message[f("repeated_sfixed32"), 1]);
Assert.AreEqual(510L , message[f("repeated_sfixed64"), 1]);
Assert.AreEqual(511F , message[f("repeated_float" ), 1]);
......
......@@ -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,
......
......@@ -27,6 +27,15 @@ namespace Google.ProtocolBuffers.Descriptors {
public IList<MethodDescriptor> Methods {
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) {
......
......@@ -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