Commit cabd06d1 authored by Jon Skeet's avatar Jon Skeet

More tests, and implementation of UninitializedMessageException description.

parent 794409b3
......@@ -63,13 +63,13 @@ namespace Google.ProtocolBuffers {
TestRequired.Builder builder = TestRequired.CreateBuilder();
AbstractMessageWrapper.Builder abstractBuilder = new AbstractMessageWrapper.Builder(builder);
Assert.IsFalse(abstractBuilder.Initialized);
Assert.IsFalse(abstractBuilder.IsInitialized);
builder.A = 1;
Assert.IsFalse(abstractBuilder.Initialized);
Assert.IsFalse(abstractBuilder.IsInitialized);
builder.B = 1;
Assert.IsFalse(abstractBuilder.Initialized);
Assert.IsFalse(abstractBuilder.IsInitialized);
builder.C = 1;
Assert.IsTrue(abstractBuilder.Initialized);
Assert.IsTrue(abstractBuilder.IsInitialized);
}
[Test]
......@@ -77,19 +77,19 @@ namespace Google.ProtocolBuffers {
TestRequiredForeign.Builder builder = TestRequiredForeign.CreateBuilder();
AbstractMessageWrapper.Builder abstractBuilder = new AbstractMessageWrapper.Builder(builder);
Assert.IsTrue(abstractBuilder.Initialized);
Assert.IsTrue(abstractBuilder.IsInitialized);
builder.SetOptionalMessage(TestRequiredUninitialized);
Assert.IsFalse(abstractBuilder.Initialized);
Assert.IsFalse(abstractBuilder.IsInitialized);
builder.SetOptionalMessage(TestRequiredInitialized);
Assert.IsTrue(abstractBuilder.Initialized);
Assert.IsTrue(abstractBuilder.IsInitialized);
builder.AddRepeatedMessage(TestRequiredUninitialized);
Assert.IsFalse(abstractBuilder.Initialized);
Assert.IsFalse(abstractBuilder.IsInitialized);
builder.SetRepeatedMessage(0, TestRequiredInitialized);
Assert.IsTrue(abstractBuilder.Initialized);
Assert.IsTrue(abstractBuilder.IsInitialized);
}
// -----------------------------------------------------------------
......@@ -261,8 +261,8 @@ namespace Google.ProtocolBuffers {
this.wrappedBuilder = wrappedBuilder;
}
public override bool Initialized {
get { return wrappedBuilder.Initialized; }
public override bool IsInitialized {
get { return wrappedBuilder.IsInitialized; }
}
public override IDictionary<FieldDescriptor, object> AllFields {
......
using System;
using System.Collections.Generic;
using System.Text;
using Google.ProtocolBuffers.Descriptors;
using NUnit.Framework;
using Google.ProtocolBuffers.TestProtos;
......@@ -176,49 +177,41 @@ namespace Google.ProtocolBuffers {
public void ExtensionReflectionDefaults() {
extensionsReflectionTester.AssertClearViaReflection(TestAllExtensions.DefaultInstance);
extensionsReflectionTester.AssertClearViaReflection(TestAllExtensions.CreateBuilder().Build());
}
public void testClearExtension() {
// clearExtension() is not actually used in TestUtil, so try it manually.
}
[Test]
public void ClearExtension() {
// ClearExtension() is not actually used in TestUtil, so try it manually.
Assert.IsFalse(TestAllExtensions.CreateBuilder()
.SetExtension(UnitTestProtoFile.OptionalInt32Extension, 1)
.ClearExtension(UnitTestProtoFile.OptionalInt32Extension)
.HasExtension(UnitTestProtoFile.OptionalInt32Extension));
Assert.AreEqual(0,
TestAllExtensions.CreateBuilder()
Assert.AreEqual(0, TestAllExtensions.CreateBuilder()
.AddExtension(UnitTestProtoFile.RepeatedInt32Extension, 1)
.ClearExtension(UnitTestProtoFile.RepeatedInt32Extension)
.GetExtensionCount(UnitTestProtoFile.RepeatedInt32Extension));
}
// =================================================================
// multiple_files_test
/* FIXME: Find this proto!
[Test]
public void MultipleFilesOption() {
// We mostly just want to check that things compile.
MessageWithNoOuter message = MessageWithNoOuter.CreateBuilder()
.setNested(MessageWithNoOuter.NestedMessage.CreateBuilder().setI(1))
.addForeign(TestAllTypes.CreateBuilder().setOptionalInt32(1))
.setNestedEnum(MessageWithNoOuter.NestedEnum.BAZ)
.setForeignEnum(EnumWithNoOuter.BAR)
.SetNested(MessageWithNoOuter.Types.NestedMessage.CreateBuilder().SetI(1))
.AddForeign(TestAllTypes.CreateBuilder().SetOptionalInt32(1))
.SetNestedEnum(MessageWithNoOuter.Types.NestedEnum.BAZ)
.SetForeignEnum(EnumWithNoOuter.BAR)
.Build();
Assert.AreEqual(message, MessageWithNoOuter.parseFrom(message.toByteString()));
Assert.AreEqual(message, MessageWithNoOuter.ParseFrom(message.ToByteString()));
Assert.AreEqual(MultipleFilesTestProto.getDescriptor(),
MessageWithNoOuter.getDescriptor().getFile());
Assert.AreEqual(MultiFileProto.Descriptor, MessageWithNoOuter.Descriptor.File);
Descriptors.FieldDescriptor field =
MessageWithNoOuter.getDescriptor().findFieldByName("foreign_enum");
Assert.AreEqual(EnumWithNoOuter.BAR.getValueDescriptor(),
message.getField(field));
FieldDescriptor field = MessageWithNoOuter.Descriptor.FindDescriptor<FieldDescriptor>("foreign_enum");
Assert.AreEqual(MultiFileProto.Descriptor.FindTypeByName<EnumDescriptor>("EnumWithNoOuter")
.FindValueByNumber((int)EnumWithNoOuter.BAR), message[field]);
Assert.AreEqual(MultipleFilesTestProto.getDescriptor(),
ServiceWithNoOuter.getDescriptor().getFile());
Assert.AreEqual(MultiFileProto.Descriptor, ServiceWithNoOuter.Descriptor.File);
assertFalse(
TestAllExtensions.getDefaultInstance().hasExtension(
MultipleFilesTestProto.extensionWithOuter));
Assert.IsFalse(TestAllExtensions.DefaultInstance.HasExtension(MultiFileProto.ExtensionWithOuter));
}
*/
}
}
This diff is collapsed.
......@@ -51,8 +51,13 @@
<Compile Include="DescriptorsTest.cs" />
<Compile Include="DynamicMessageTest.cs" />
<Compile Include="GeneratedMessageTest.cs" />
<Compile Include="MessageTest.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ReflectionTester.cs" />
<Compile Include="TestProtos\EnumWithNoOuter.cs" />
<Compile Include="TestProtos\MessageWithNoOuter.cs" />
<Compile Include="TestProtos\MultiFileProto.cs" />
<Compile Include="TestProtos\ServiceWithNoOuter.cs" />
<Compile Include="TestProtos\UnitTestEmbedOptimizeForProtoFile.cs" />
<Compile Include="TestProtos\UnitTestImportProtoFile.cs" />
<Compile Include="TestProtos\UnitTestMessageSetProtoFile.cs" />
......
// Generated by the protocol buffer compiler. DO NOT EDIT!
using pb = global::Google.ProtocolBuffers;
using pbc = global::Google.ProtocolBuffers.Collections;
using pbd = global::Google.ProtocolBuffers.Descriptors;
using scg = global::System.Collections.Generic;
using self = global::Google.ProtocolBuffers.TestProtos;
namespace Google.ProtocolBuffers.TestProtos {
public enum EnumWithNoOuter {
FOO = 1,
BAR = 2,
}
}
This diff is collapsed.
// Generated by the protocol buffer compiler. DO NOT EDIT!
using pb = global::Google.ProtocolBuffers;
using pbc = global::Google.ProtocolBuffers.Collections;
using pbd = global::Google.ProtocolBuffers.Descriptors;
using scg = global::System.Collections.Generic;
using self = global::Google.ProtocolBuffers.TestProtos;
namespace Google.ProtocolBuffers.TestProtos {
public static partial class MultiFileProto {
#region Descriptor
public static pbd::FileDescriptor Descriptor {
get { return descriptor; }
}
private static readonly pbd::FileDescriptor descriptor = pbd::FileDescriptor.InternalBuildGeneratedFileFrom (
new byte[] {
0x0a, 0x3b, 0x73, 0x72, 0x63, 0x2f, 0x74, 0x65, 0x73, 0x74, 0x2f, 0x6a, 0x61, 0x76, 0x61, 0x2f, 0x63, 0x6f, 0x6d, 0x2f,
0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2f, 0x6d, 0x75, 0x6c, 0x74,
0x69, 0x70, 0x6c, 0x65, 0x5f, 0x66, 0x69, 0x6c, 0x65, 0x73, 0x5f, 0x74, 0x65, 0x73, 0x74, 0x2e, 0x70, 0x72, 0x6f, 0x74,
0x6f, 0x12, 0x11, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x5f, 0x75, 0x6e, 0x69, 0x74, 0x74, 0x65, 0x73, 0x74,
0x1a, 0x1e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2f, 0x75, 0x6e,
0x69, 0x74, 0x74, 0x65, 0x73, 0x74, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x22, 0xbf, 0x02, 0x0a, 0x12, 0x4d, 0x65, 0x73,
0x73, 0x61, 0x67, 0x65, 0x57, 0x69, 0x74, 0x68, 0x4e, 0x6f, 0x4f, 0x75, 0x74, 0x65, 0x72, 0x12, 0x43, 0x0a, 0x06, 0x6e,
0x65, 0x73, 0x74, 0x65, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x33, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62,
0x75, 0x66, 0x5f, 0x75, 0x6e, 0x69, 0x74, 0x74, 0x65, 0x73, 0x74, 0x2e, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x57,
0x69, 0x74, 0x68, 0x4e, 0x6f, 0x4f, 0x75, 0x74, 0x65, 0x72, 0x2e, 0x4e, 0x65, 0x73, 0x74, 0x65, 0x64, 0x4d, 0x65, 0x73,
0x73, 0x61, 0x67, 0x65, 0x12, 0x30, 0x0a, 0x07, 0x66, 0x6f, 0x72, 0x65, 0x69, 0x67, 0x6e, 0x18, 0x02, 0x20, 0x03, 0x28,
0x0b, 0x32, 0x1f, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x5f, 0x75, 0x6e, 0x69, 0x74, 0x74, 0x65, 0x73,
0x74, 0x2e, 0x54, 0x65, 0x73, 0x74, 0x41, 0x6c, 0x6c, 0x54, 0x79, 0x70, 0x65, 0x73, 0x12, 0x45, 0x0a, 0x0b, 0x6e, 0x65,
0x73, 0x74, 0x65, 0x64, 0x5f, 0x65, 0x6e, 0x75, 0x6d, 0x18, 0x03, 0x20, 0x01, 0x28, 0x0e, 0x32, 0x30, 0x2e, 0x70, 0x72,
0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x5f, 0x75, 0x6e, 0x69, 0x74, 0x74, 0x65, 0x73, 0x74, 0x2e, 0x4d, 0x65, 0x73, 0x73,
0x61, 0x67, 0x65, 0x57, 0x69, 0x74, 0x68, 0x4e, 0x6f, 0x4f, 0x75, 0x74, 0x65, 0x72, 0x2e, 0x4e, 0x65, 0x73, 0x74, 0x65,
0x64, 0x45, 0x6e, 0x75, 0x6d, 0x12, 0x38, 0x0a, 0x0c, 0x66, 0x6f, 0x72, 0x65, 0x69, 0x67, 0x6e, 0x5f, 0x65, 0x6e, 0x75,
0x6d, 0x18, 0x04, 0x20, 0x01, 0x28, 0x0e, 0x32, 0x22, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x5f, 0x75,
0x6e, 0x69, 0x74, 0x74, 0x65, 0x73, 0x74, 0x2e, 0x45, 0x6e, 0x75, 0x6d, 0x57, 0x69, 0x74, 0x68, 0x4e, 0x6f, 0x4f, 0x75,
0x74, 0x65, 0x72, 0x1a, 0x1a, 0x0a, 0x0d, 0x4e, 0x65, 0x73, 0x74, 0x65, 0x64, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65,
0x12, 0x09, 0x0a, 0x01, 0x69, 0x18, 0x01, 0x20, 0x01, 0x28, 0x05, 0x22, 0x15, 0x0a, 0x0a, 0x4e, 0x65, 0x73, 0x74, 0x65,
0x64, 0x45, 0x6e, 0x75, 0x6d, 0x12, 0x07, 0x0a, 0x03, 0x42, 0x41, 0x5a, 0x10, 0x03, 0x2a, 0x23, 0x0a, 0x0f, 0x45, 0x6e,
0x75, 0x6d, 0x57, 0x69, 0x74, 0x68, 0x4e, 0x6f, 0x4f, 0x75, 0x74, 0x65, 0x72, 0x12, 0x07, 0x0a, 0x03, 0x46, 0x4f, 0x4f,
0x10, 0x01, 0x12, 0x07, 0x0a, 0x03, 0x42, 0x41, 0x52, 0x10, 0x02, 0x32, 0x63, 0x0a, 0x12, 0x53, 0x65, 0x72, 0x76, 0x69,
0x63, 0x65, 0x57, 0x69, 0x74, 0x68, 0x4e, 0x6f, 0x4f, 0x75, 0x74, 0x65, 0x72, 0x12, 0x4d, 0x0a, 0x03, 0x46, 0x6f, 0x6f,
0x12, 0x25, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x5f, 0x75, 0x6e, 0x69, 0x74, 0x74, 0x65, 0x73, 0x74,
0x2e, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x57, 0x69, 0x74, 0x68, 0x4e, 0x6f, 0x4f, 0x75, 0x74, 0x65, 0x72, 0x1a,
0x1f, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x5f, 0x75, 0x6e, 0x69, 0x74, 0x74, 0x65, 0x73, 0x74, 0x2e,
0x54, 0x65, 0x73, 0x74, 0x41, 0x6c, 0x6c, 0x54, 0x79, 0x70, 0x65, 0x73, 0x3a, 0x44, 0x0a, 0x14, 0x65, 0x78, 0x74, 0x65,
0x6e, 0x73, 0x69, 0x6f, 0x6e, 0x5f, 0x77, 0x69, 0x74, 0x68, 0x5f, 0x6f, 0x75, 0x74, 0x65, 0x72, 0x12, 0x24, 0x2e, 0x70,
0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x5f, 0x75, 0x6e, 0x69, 0x74, 0x74, 0x65, 0x73, 0x74, 0x2e, 0x54, 0x65, 0x73,
0x74, 0x41, 0x6c, 0x6c, 0x45, 0x78, 0x74, 0x65, 0x6e, 0x73, 0x69, 0x6f, 0x6e, 0x73, 0x18, 0x87, 0xad, 0x4b, 0x20, 0x01,
0x28, 0x05, 0x42, 0x58, 0x42, 0x16, 0x4d, 0x75, 0x6c, 0x74, 0x69, 0x70, 0x6c, 0x65, 0x46, 0x69, 0x6c, 0x65, 0x73, 0x54,
0x65, 0x73, 0x74, 0x50, 0x72, 0x6f, 0x74, 0x6f, 0x50, 0x01, 0xc2, 0x3e, 0x21, 0x47, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e,
0x50, 0x72, 0x6f, 0x74, 0x6f, 0x63, 0x6f, 0x6c, 0x42, 0x75, 0x66, 0x66, 0x65, 0x72, 0x73, 0x2e, 0x54, 0x65, 0x73, 0x74,
0x50, 0x72, 0x6f, 0x74, 0x6f, 0x73, 0xca, 0x3e, 0x0e, 0x4d, 0x75, 0x6c, 0x74, 0x69, 0x46, 0x69, 0x6c, 0x65, 0x50, 0x72,
0x6f, 0x74, 0x6f, 0xd0, 0x3e, 0x01, 0xd8, 0x3e, 0x00, 0xe0, 0x3e, 0x01,
}, new pbd::FileDescriptor[] {
self::UnitTestProtoFile.Descriptor,
});
#endregion
#region Extensions
public static readonly pb::GeneratedExtensionBase<self::TestAllExtensions, int> ExtensionWithOuter =
pb::GeneratedSingleExtension<self::TestAllExtensions, int>.CreateInstance(Descriptor.Extensions[0]);
#endregion
#region Static variables
internal static readonly pbd::MessageDescriptor internal__static_protobuf_unittest_MessageWithNoOuter__Descriptor
= Descriptor.MessageTypes[0];
internal static pb::FieldAccess.FieldAccessorTable internal__static_protobuf_unittest_MessageWithNoOuter__FieldAccessorTable
= new pb::FieldAccess.FieldAccessorTable(internal__static_protobuf_unittest_MessageWithNoOuter__Descriptor,
new string[] { "Nested", "Foreign", "NestedEnum", "ForeignEnum", },
typeof (self::MessageWithNoOuter),
typeof (self::MessageWithNoOuter.Builder));
internal static readonly pbd::MessageDescriptor internal__static_protobuf_unittest_MessageWithNoOuter_NestedMessage__Descriptor
= internal__static_protobuf_unittest_MessageWithNoOuter__Descriptor.NestedTypes[0];
internal static pb::FieldAccess.FieldAccessorTable internal__static_protobuf_unittest_MessageWithNoOuter_NestedMessage__FieldAccessorTable
= new pb::FieldAccess.FieldAccessorTable(internal__static_protobuf_unittest_MessageWithNoOuter_NestedMessage__Descriptor,
new string[] { "I", },
typeof (self::MessageWithNoOuter.Types.NestedMessage),
typeof (self::MessageWithNoOuter.Types.NestedMessage.Builder));
#endregion
}
}
// Generated by the protocol buffer compiler. DO NOT EDIT!
using pb = global::Google.ProtocolBuffers;
using pbc = global::Google.ProtocolBuffers.Collections;
using pbd = global::Google.ProtocolBuffers.Descriptors;
using scg = global::System.Collections.Generic;
using self = global::Google.ProtocolBuffers.TestProtos;
namespace Google.ProtocolBuffers.TestProtos {
public abstract class ServiceWithNoOuter : pb::IService {
public abstract void Foo(
pb::IRpcController controller,
self::MessageWithNoOuter request,
global::System.Action<self::TestAllTypes> done);
public static pbd::ServiceDescriptor Descriptor {
get { return self::MultiFileProto.Descriptor.Services[0]; }
}
public pbd::ServiceDescriptor DescriptorForType {
get { return Descriptor; }
}
public void CallMethod(
pbd::MethodDescriptor method,
pb::IRpcController controller,
pb::IMessage request,
global::System.Action<pb::IMessage> done) {
if (method.Service != Descriptor) {
throw new global::System.ArgumentException(
"Service.CallMethod() given method descriptor for wrong " +
"service type.");
}
switch(method.Index) {
case 0:
this.Foo(controller, (self::MessageWithNoOuter)request,
pb::RpcUtil.SpecializeCallback<self::TestAllTypes>(
done));
return;
default:
throw new global::System.InvalidOperationException("Can't get here.");
}
}
public pb::IMessage GetRequestPrototype(pbd::MethodDescriptor method) {
if (method.Service != Descriptor) {
throw new global::System.ArgumentException(
"Service.GetRequestPrototype() given method " +
"descriptor for wrong service type.");
}
switch(method.Index) {
case 0:
return self::MessageWithNoOuter.DefaultInstance;
default:
throw new global::System.ArgumentException("Can't get here.");
}
}
public pb::IMessage GetResponsePrototype(pbd::MethodDescriptor method) {
if (method.Service != Descriptor) {
throw new global::System.ArgumentException(
"Service.GetResponsePrototype() given method " +
"descriptor for wrong service type.");
}
switch(method.Index) {
case 0:
return self::TestAllTypes.DefaultInstance;
default:
throw new global::System.ArgumentException("Can't get here.");
}
}
public static Stub CreateStub(
pb::IRpcChannel channel) {
return new Stub(channel);
}
public class Stub : self::ServiceWithNoOuter {
internal Stub(pb::IRpcChannel channel) {
this.channel = channel;
}
private readonly pb::IRpcChannel channel;
public pb::IRpcChannel Channel {
get { return channel; }
}
public override void Foo(
pb::IRpcController controller,
self::MessageWithNoOuter request,
global::System.Action<self::TestAllTypes> done) {
channel.CallMethod(
Descriptor.Methods[0],
controller,
request,
self::TestAllTypes.DefaultInstance,
pb::RpcUtil.GeneralizeCallback(done, self::TestAllTypes.DefaultInstance));
}
}
}
}
......@@ -59,9 +59,6 @@ namespace Google.ProtocolBuffers.TestProtos {
#region Messages
public sealed partial class TestEmbedOptimizedForSize : pb::GeneratedMessage<TestEmbedOptimizedForSize, TestEmbedOptimizedForSize.Builder> {
// Use TestEmbedOptimizedForSize.CreateBuilder() to construct.
private TestEmbedOptimizedForSize() {}
private static readonly TestEmbedOptimizedForSize defaultInstance = new TestEmbedOptimizedForSize();
public static TestEmbedOptimizedForSize DefaultInstance {
get { return defaultInstance; }
......@@ -153,7 +150,7 @@ namespace Google.ProtocolBuffers.TestProtos {
public static self::TestEmbedOptimizedForSize ParseFrom(byte[] data) {
return ((Builder) CreateBuilder().MergeFrom(data)).BuildParsed();
}
public static self::TestEmbedOptimizedForSize parseFrom(byte[] data,
public static self::TestEmbedOptimizedForSize ParseFrom(byte[] data,
pb::ExtensionRegistry extensionRegistry) {
return ((Builder) CreateBuilder().MergeFrom(data, extensionRegistry))
.BuildParsed();
......
......@@ -59,9 +59,6 @@ namespace Google.ProtocolBuffers.TestProtos {
#region Messages
public sealed partial class ImportMessage : pb::GeneratedMessage<ImportMessage, ImportMessage.Builder> {
// Use ImportMessage.CreateBuilder() to construct.
private ImportMessage() {}
private static readonly ImportMessage defaultInstance = new ImportMessage();
public static ImportMessage DefaultInstance {
get { return defaultInstance; }
......@@ -129,7 +126,7 @@ namespace Google.ProtocolBuffers.TestProtos {
public static self::ImportMessage ParseFrom(byte[] data) {
return ((Builder) CreateBuilder().MergeFrom(data)).BuildParsed();
}
public static self::ImportMessage parseFrom(byte[] data,
public static self::ImportMessage ParseFrom(byte[] data,
pb::ExtensionRegistry extensionRegistry) {
return ((Builder) CreateBuilder().MergeFrom(data, extensionRegistry))
.BuildParsed();
......
......@@ -109,9 +109,6 @@ namespace Google.ProtocolBuffers.TestProtos {
#region Messages
public sealed partial class TestMessageSet : pb::ExtendableMessage<TestMessageSet, TestMessageSet.Builder> {
// Use TestMessageSet.CreateBuilder() to construct.
private TestMessageSet() {}
private static readonly TestMessageSet defaultInstance = new TestMessageSet();
public static TestMessageSet DefaultInstance {
get { return defaultInstance; }
......@@ -167,7 +164,7 @@ namespace Google.ProtocolBuffers.TestProtos {
public static self::TestMessageSet ParseFrom(byte[] data) {
return ((Builder) CreateBuilder().MergeFrom(data)).BuildParsed();
}
public static self::TestMessageSet parseFrom(byte[] data,
public static self::TestMessageSet ParseFrom(byte[] data,
pb::ExtensionRegistry extensionRegistry) {
return ((Builder) CreateBuilder().MergeFrom(data, extensionRegistry))
.BuildParsed();
......@@ -277,9 +274,6 @@ namespace Google.ProtocolBuffers.TestProtos {
}
public sealed partial class TestMessageSetContainer : pb::GeneratedMessage<TestMessageSetContainer, TestMessageSetContainer.Builder> {
// Use TestMessageSetContainer.CreateBuilder() to construct.
private TestMessageSetContainer() {}
private static readonly TestMessageSetContainer defaultInstance = new TestMessageSetContainer();
public static TestMessageSetContainer DefaultInstance {
get { return defaultInstance; }
......@@ -350,7 +344,7 @@ namespace Google.ProtocolBuffers.TestProtos {
public static self::TestMessageSetContainer ParseFrom(byte[] data) {
return ((Builder) CreateBuilder().MergeFrom(data)).BuildParsed();
}
public static self::TestMessageSetContainer parseFrom(byte[] data,
public static self::TestMessageSetContainer ParseFrom(byte[] data,
pb::ExtensionRegistry extensionRegistry) {
return ((Builder) CreateBuilder().MergeFrom(data, extensionRegistry))
.BuildParsed();
......@@ -507,9 +501,6 @@ namespace Google.ProtocolBuffers.TestProtos {
}
public sealed partial class TestMessageSetExtension1 : pb::GeneratedMessage<TestMessageSetExtension1, TestMessageSetExtension1.Builder> {
// Use TestMessageSetExtension1.CreateBuilder() to construct.
private TestMessageSetExtension1() {}
private static readonly TestMessageSetExtension1 defaultInstance = new TestMessageSetExtension1();
public static TestMessageSetExtension1 DefaultInstance {
get { return defaultInstance; }
......@@ -584,7 +575,7 @@ namespace Google.ProtocolBuffers.TestProtos {
public static self::TestMessageSetExtension1 ParseFrom(byte[] data) {
return ((Builder) CreateBuilder().MergeFrom(data)).BuildParsed();
}
public static self::TestMessageSetExtension1 parseFrom(byte[] data,
public static self::TestMessageSetExtension1 ParseFrom(byte[] data,
pb::ExtensionRegistry extensionRegistry) {
return ((Builder) CreateBuilder().MergeFrom(data, extensionRegistry))
.BuildParsed();
......@@ -720,9 +711,6 @@ namespace Google.ProtocolBuffers.TestProtos {
}
public sealed partial class TestMessageSetExtension2 : pb::GeneratedMessage<TestMessageSetExtension2, TestMessageSetExtension2.Builder> {
// Use TestMessageSetExtension2.CreateBuilder() to construct.
private TestMessageSetExtension2() {}
private static readonly TestMessageSetExtension2 defaultInstance = new TestMessageSetExtension2();
public static TestMessageSetExtension2 DefaultInstance {
get { return defaultInstance; }
......@@ -797,7 +785,7 @@ namespace Google.ProtocolBuffers.TestProtos {
public static self::TestMessageSetExtension2 ParseFrom(byte[] data) {
return ((Builder) CreateBuilder().MergeFrom(data)).BuildParsed();
}
public static self::TestMessageSetExtension2 parseFrom(byte[] data,
public static self::TestMessageSetExtension2 ParseFrom(byte[] data,
pb::ExtensionRegistry extensionRegistry) {
return ((Builder) CreateBuilder().MergeFrom(data, extensionRegistry))
.BuildParsed();
......@@ -933,9 +921,6 @@ namespace Google.ProtocolBuffers.TestProtos {
}
public sealed partial class RawMessageSet : pb::GeneratedMessage<RawMessageSet, RawMessageSet.Builder> {
// Use RawMessageSet.CreateBuilder() to construct.
private RawMessageSet() {}
private static readonly RawMessageSet defaultInstance = new RawMessageSet();
public static RawMessageSet DefaultInstance {
get { return defaultInstance; }
......@@ -956,9 +941,6 @@ namespace Google.ProtocolBuffers.TestProtos {
#region Nested types
public static class Types {
public sealed partial class Item : pb::GeneratedMessage<Item, Item.Builder> {
// Use Item.CreateBuilder() to construct.
private Item() {}
private static readonly Item defaultInstance = new Item();
public static Item DefaultInstance {
get { return defaultInstance; }
......@@ -1044,7 +1026,7 @@ namespace Google.ProtocolBuffers.TestProtos {
public static self::RawMessageSet.Types.Item ParseFrom(byte[] data) {
return ((Builder) CreateBuilder().MergeFrom(data)).BuildParsed();
}
public static self::RawMessageSet.Types.Item parseFrom(byte[] data,
public static self::RawMessageSet.Types.Item ParseFrom(byte[] data,
pb::ExtensionRegistry extensionRegistry) {
return ((Builder) CreateBuilder().MergeFrom(data, extensionRegistry))
.BuildParsed();
......@@ -1263,7 +1245,7 @@ namespace Google.ProtocolBuffers.TestProtos {
public static self::RawMessageSet ParseFrom(byte[] data) {
return ((Builder) CreateBuilder().MergeFrom(data)).BuildParsed();
}
public static self::RawMessageSet parseFrom(byte[] data,
public static self::RawMessageSet ParseFrom(byte[] data,
pb::ExtensionRegistry extensionRegistry) {
return ((Builder) CreateBuilder().MergeFrom(data, extensionRegistry))
.BuildParsed();
......
......@@ -58,9 +58,6 @@ namespace Google.ProtocolBuffers.TestProtos {
#region Messages
public sealed partial class TestOptimizedForSize : pb::ExtendableMessage<TestOptimizedForSize, TestOptimizedForSize.Builder> {
// Use TestOptimizedForSize.CreateBuilder() to construct.
private TestOptimizedForSize() {}
private static readonly TestOptimizedForSize defaultInstance = new TestOptimizedForSize();
public static TestOptimizedForSize DefaultInstance {
get { return defaultInstance; }
......@@ -116,7 +113,7 @@ namespace Google.ProtocolBuffers.TestProtos {
public static self::TestOptimizedForSize ParseFrom(byte[] data) {
return ((Builder) CreateBuilder().MergeFrom(data)).BuildParsed();
}
public static self::TestOptimizedForSize parseFrom(byte[] data,
public static self::TestOptimizedForSize ParseFrom(byte[] data,
pb::ExtensionRegistry extensionRegistry) {
return ((Builder) CreateBuilder().MergeFrom(data, extensionRegistry))
.BuildParsed();
......
......@@ -12,7 +12,7 @@ namespace Google.ProtocolBuffers {
/// </summary>
public abstract class AbstractBuilder : IBuilder {
#region Unimplemented members of IBuilder
public abstract bool Initialized { get; }
public abstract bool IsInitialized { get; }
public abstract IDictionary<FieldDescriptor, object> AllFields { get; }
public abstract object this[FieldDescriptor field] { get; set; }
public abstract MessageDescriptor DescriptorForType { get; }
......@@ -78,7 +78,7 @@ namespace Google.ProtocolBuffers {
// implementations).
// TODO(jonskeet): Provide a function somewhere called makeDeepCopy()
// which allows people to make secure deep copies of messages.
foreach (KeyValuePair<FieldDescriptor, object> entry in AllFields) {
foreach (KeyValuePair<FieldDescriptor, object> entry in other.AllFields) {
FieldDescriptor field = entry.Key;
if (field.IsRepeated) {
// Concatenate repeated fields
......
......@@ -5,6 +5,11 @@ using System.Text;
using Google.ProtocolBuffers.Descriptors;
namespace Google.ProtocolBuffers {
/// <summary>
/// An implementation of IMessage that can represent arbitrary types, given a MessageaDescriptor.
/// TODO: Implement appropriate generics.
/// </summary>
public class DynamicMessage : AbstractMessage {
private readonly MessageDescriptor type;
......@@ -242,7 +247,7 @@ namespace Google.ProtocolBuffers {
return this;
}
public override IBuilder MergeFrom(IMessage other) {
public override IBuilder MergeFrom(IMessage other) {
if (other.DescriptorForType != type) {
throw new ArgumentException("MergeFrom(IMessage) can only merge messages of the same type.");
}
......@@ -251,7 +256,7 @@ namespace Google.ProtocolBuffers {
}
protected override IMessage BuildImpl() {
if (!Initialized) {
if (!IsInitialized) {
throw new UninitializedMessageException(new DynamicMessage(type, fields, unknownFields));
}
return BuildPartialImpl();
......@@ -263,7 +268,7 @@ namespace Google.ProtocolBuffers {
/// </summary>
/// <returns></returns>
internal DynamicMessage BuildParsed() {
if (!Initialized) {
if (!IsInitialized) {
throw new UninitializedMessageException(new DynamicMessage(type, fields, unknownFields)).AsInvalidProtocolBufferException();
}
return (DynamicMessage) BuildPartialImpl();
......@@ -283,7 +288,7 @@ namespace Google.ProtocolBuffers {
return result;
}
public override bool Initialized {
public override bool IsInitialized {
get { return fields.IsInitializedWithRespectTo(type); }
}
......
......@@ -26,7 +26,7 @@ namespace Google.ProtocolBuffers {
get { return MessageBeingBuilt.FieldAccesseorsFromBuilder; }
}
public override bool Initialized {
public override bool IsInitialized {
get { return MessageBeingBuilt.IsInitialized; }
}
......@@ -212,7 +212,7 @@ namespace Google.ProtocolBuffers {
/// TODO(jonskeet): This used to be generated for each class. Find out why.
/// </summary>
public TMessage BuildParsed() {
if (!Initialized) {
if (!IsInitialized) {
throw new UninitializedMessageException(MessageBeingBuilt).AsInvalidProtocolBufferException();
}
return BuildPartial();
......@@ -223,7 +223,7 @@ namespace Google.ProtocolBuffers {
/// TODO(jonskeet): This used to be generated for each class. Find out why.
/// </summary>
public TMessage Build() {
if (!Initialized) {
if (!IsInitialized) {
throw new UninitializedMessageException(MessageBeingBuilt);
}
return BuildPartial();
......
......@@ -33,7 +33,7 @@ namespace Google.ProtocolBuffers {
/// Returns true iff all required fields in the message and all
/// embedded messages are set.
/// </summary>
bool Initialized { get; }
bool IsInitialized { get; }
/// <summary>
/// Behaves like the equivalent property in IMessage&lt;T&gt;.
......@@ -172,7 +172,7 @@ namespace Google.ProtocolBuffers {
/// required fields, it will throw an UninitializedMessageException.
/// There are a few good ways to deal with this:
/// <list>
/// <item>Call Initialized to verify to verify that all required fields are
/// <item>Call IsInitialized to verify to verify that all required fields are
/// set before building.</item>
/// <item>Parse the message separately using one of the static ParseFrom
/// methods, then use MergeFrom(IMessage&lt;T&gt;) to merge it with
......
......@@ -14,15 +14,111 @@
// See the License for the specific language governing permissions and
// limitations under the License.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using Google.ProtocolBuffers.Collections;
using Google.ProtocolBuffers.Descriptors;
namespace Google.ProtocolBuffers {
public class UninitializedMessageException : Exception {
public UninitializedMessageException(IMessage message) {
private readonly IList<string> missingFields;
public UninitializedMessageException(IMessage message)
: this(FindMissingFields(message)) {
}
private UninitializedMessageException(IList<string> missingFields)
: base(BuildDescription(missingFields)) {
this.missingFields = Lists.AsReadOnly(missingFields);
}
/// <summary>
/// Converts this exception into an InvalidProtocolBufferException.
/// When a parsed message is missing required fields, this should be thrown
/// instead of UninitializedMessageException.
/// </summary>
public InvalidProtocolBufferException AsInvalidProtocolBufferException() {
return new InvalidProtocolBufferException(Message);
}
/// <summary>
/// Constructs the description string for a given list of missing fields.
/// </summary>
private static string BuildDescription(IEnumerable<string> missingFields) {
StringBuilder description = new StringBuilder("Message missing required fields: ");
bool first = true;
foreach(string field in missingFields) {
if (first) {
first = false;
} else {
description.Append(", ");
}
description.Append(field);
}
return description.ToString();
}
/// <summary>
/// Returns a list of the full "paths" of missing required
/// fields in the specified message.
/// </summary>
private static IList<String> FindMissingFields(IMessage message) {
List<String> results = new List<String>();
FindMissingFields(message, "", results);
return results;
}
/// <summary>
/// Recursive helper implementing FindMissingFields.
/// </summary>
private static void FindMissingFields(IMessage message, String prefix, List<String> results) {
foreach (FieldDescriptor field in message.DescriptorForType.Fields) {
if (field.IsRequired && !message.HasField(field)) {
results.Add(prefix + field.Name);
}
}
foreach (KeyValuePair<FieldDescriptor, object> entry in message.AllFields) {
FieldDescriptor field = entry.Key;
object value = entry.Value;
if (field.MappedType == MappedType.Message) {
if (field.IsRepeated) {
int i = 0;
foreach (object element in (IEnumerable) value) {
FindMissingFields((IMessage) element, SubMessagePrefix(prefix, field, i++), results);
}
} else {
if (message.HasField(field)) {
FindMissingFields((IMessage) value, SubMessagePrefix(prefix, field, -1), results);
}
}
}
}
}
private static String SubMessagePrefix(String prefix, FieldDescriptor field, int index) {
StringBuilder result = new StringBuilder(prefix);
if (field.IsExtension) {
result.Append('(')
.Append(field.FullName)
.Append(')');
} else {
result.Append(field.Name);
}
if (index != -1) {
result.Append('[')
.Append(index)
.Append(']');
}
result.Append('.');
return result.ToString();
}
}
}
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