diff --git a/csharp/src/Google.Protobuf.Test/GeneratedMessageTest.Proto2.cs b/csharp/src/Google.Protobuf.Test/GeneratedMessageTest.Proto2.cs new file mode 100644 index 0000000000..b613cc8037 --- /dev/null +++ b/csharp/src/Google.Protobuf.Test/GeneratedMessageTest.Proto2.cs @@ -0,0 +1,268 @@ +using Google.Protobuf.TestProtos.Proto2; +using NUnit.Framework; + +namespace Google.Protobuf +{ + /// + /// Tests around the generated TestAllTypes message in unittest.proto + /// + public partial class GeneratedMessageTest + { + [Test] + public void DefaultProto2Values() + { + var message = new TestAllTypes(); + Assert.AreEqual(false, message.OptionalBool); + Assert.AreEqual(ByteString.Empty, message.OptionalBytes); + Assert.AreEqual(0.0, message.OptionalDouble); + Assert.AreEqual(0, message.OptionalFixed32); + Assert.AreEqual(0L, message.OptionalFixed64); + Assert.AreEqual(0.0f, message.OptionalFloat); + Assert.AreEqual(ForeignEnum.ForeignFoo, message.OptionalForeignEnum); + Assert.IsNull(message.OptionalForeignMessage); + Assert.AreEqual(ImportEnum.ImportFoo, message.OptionalImportEnum); + Assert.IsNull(message.OptionalImportMessage); + Assert.AreEqual(0, message.OptionalInt32); + Assert.AreEqual(0L, message.OptionalInt64); + Assert.AreEqual(TestAllTypes.Types.NestedEnum.Foo, message.OptionalNestedEnum); + Assert.IsNull(message.OptionalNestedMessage); + Assert.IsNull(message.OptionalPublicImportMessage); + Assert.AreEqual(0, message.OptionalSfixed32); + Assert.AreEqual(0L, message.OptionalSfixed64); + Assert.AreEqual(0, message.OptionalSint32); + Assert.AreEqual(0L, message.OptionalSint64); + Assert.AreEqual("", message.OptionalString); + Assert.AreEqual(0U, message.OptionalUint32); + Assert.AreEqual(0UL, message.OptionalUint64); + + // Repeated fields + Assert.AreEqual(0, message.RepeatedBool.Count); + Assert.AreEqual(0, message.RepeatedBytes.Count); + Assert.AreEqual(0, message.RepeatedDouble.Count); + Assert.AreEqual(0, message.RepeatedFixed32.Count); + Assert.AreEqual(0, message.RepeatedFixed64.Count); + Assert.AreEqual(0, message.RepeatedFloat.Count); + Assert.AreEqual(0, message.RepeatedForeignEnum.Count); + Assert.AreEqual(0, message.RepeatedForeignMessage.Count); + Assert.AreEqual(0, message.RepeatedImportEnum.Count); + Assert.AreEqual(0, message.RepeatedImportMessage.Count); + Assert.AreEqual(0, message.RepeatedNestedEnum.Count); + Assert.AreEqual(0, message.RepeatedNestedMessage.Count); + Assert.AreEqual(0, message.RepeatedSfixed32.Count); + Assert.AreEqual(0, message.RepeatedSfixed64.Count); + Assert.AreEqual(0, message.RepeatedSint32.Count); + Assert.AreEqual(0, message.RepeatedSint64.Count); + Assert.AreEqual(0, message.RepeatedString.Count); + Assert.AreEqual(0, message.RepeatedUint32.Count); + Assert.AreEqual(0, message.RepeatedUint64.Count); + + // Oneof fields + Assert.AreEqual(TestAllTypes.OneofFieldOneofCase.None, message.OneofFieldCase); + Assert.AreEqual(0, message.OneofUint32); + Assert.AreEqual("", message.OneofString); + Assert.AreEqual(ByteString.Empty, message.OneofBytes); + Assert.IsNull(message.OneofNestedMessage); + + Assert.AreEqual(true, message.DefaultBool); + Assert.AreEqual(ByteString.CopyFromUtf8("world"), message.DefaultBytes); + Assert.AreEqual("123", message.DefaultCord); + Assert.AreEqual(52e3, message.DefaultDouble); + Assert.AreEqual(47, message.DefaultFixed32); + Assert.AreEqual(48, message.DefaultFixed64); + Assert.AreEqual(51.5, message.DefaultFloat); + Assert.AreEqual(ForeignEnum.ForeignBar, message.DefaultForeignEnum); + Assert.AreEqual(ImportEnum.ImportBar, message.DefaultImportEnum); + Assert.AreEqual(41, message.DefaultInt32); + Assert.AreEqual(42, message.DefaultInt64); + Assert.AreEqual(TestAllTypes.Types.NestedEnum.Bar, message.DefaultNestedEnum); + Assert.AreEqual(49, message.DefaultSfixed32); + Assert.AreEqual(-50, message.DefaultSfixed64); + Assert.AreEqual(-45, message.DefaultSint32); + Assert.AreEqual(46, message.DefaultSint64); + Assert.AreEqual("hello", message.DefaultString); + Assert.AreEqual("abc", message.DefaultStringPiece); + Assert.AreEqual(43, message.DefaultUint32); + Assert.AreEqual(44, message.DefaultUint64); + + Assert.False(message.HasDefaultBool); + Assert.False(message.HasDefaultBytes); + Assert.False(message.HasDefaultCord); + Assert.False(message.HasDefaultDouble); + Assert.False(message.HasDefaultFixed32); + Assert.False(message.HasDefaultFixed64); + Assert.False(message.HasDefaultFloat); + Assert.False(message.HasDefaultForeignEnum); + Assert.False(message.HasDefaultImportEnum); + Assert.False(message.HasDefaultInt32); + Assert.False(message.HasDefaultInt64); + Assert.False(message.HasDefaultNestedEnum); + Assert.False(message.HasDefaultSfixed32); + Assert.False(message.HasDefaultSfixed64); + Assert.False(message.HasDefaultSint32); + Assert.False(message.HasDefaultSint64); + Assert.False(message.HasDefaultString); + Assert.False(message.HasDefaultStringPiece); + Assert.False(message.HasDefaultUint32); + Assert.False(message.HasDefaultUint64); + } + + [Test] + public void FieldPresence() + { + var message = new TestAllTypes(); + + Assert.False(message.HasOptionalBool); + Assert.False(message.OptionalBool); + + message.OptionalBool = true; + + Assert.True(message.HasOptionalBool); + Assert.True(message.OptionalBool); + + message.OptionalBool = false; + + Assert.True(message.HasOptionalBool); + Assert.False(message.OptionalBool); + + message.ClearOptionalBool(); + + Assert.False(message.HasOptionalBool); + Assert.False(message.OptionalBool); + + Assert.False(message.HasDefaultBool); + Assert.True(message.DefaultBool); + + message.DefaultBool = false; + + Assert.True(message.HasDefaultBool); + Assert.False(message.DefaultBool); + + message.DefaultBool = true; + + Assert.True(message.HasDefaultBool); + Assert.True(message.DefaultBool); + + message.ClearDefaultBool(); + + Assert.False(message.HasDefaultBool); + Assert.True(message.DefaultBool); + } + + [Test] + public void RequiredFields() + { + var message = new TestRequired(); + Assert.False(message.IsInitialized()); + + message.A = 1; + message.B = 2; + message.C = 3; + + Assert.True(message.IsInitialized()); + } + + [Test] + public void RequiredFieldsInExtensions() + { + var message = new TestAllExtensions(); + Assert.True(message.IsInitialized()); + + message.SetExtension(TestRequired.Extensions.Single, new TestRequired()); + + Assert.False(message.IsInitialized()); + + var extensionMessage = message.GetExtension(TestRequired.Extensions.Single); + extensionMessage.A = 1; + extensionMessage.B = 2; + extensionMessage.C = 3; + + Assert.True(message.IsInitialized()); + + message.GetOrRegisterExtension(TestRequired.Extensions.Multi); + + Assert.True(message.IsInitialized()); + + message.GetExtension(TestRequired.Extensions.Multi).Add(new TestRequired()); + + Assert.False(message.IsInitialized()); + + extensionMessage = message.GetExtension(TestRequired.Extensions.Multi)[0]; + extensionMessage.A = 1; + extensionMessage.B = 2; + extensionMessage.C = 3; + + Assert.True(message.IsInitialized()); + + message.SetExtension(UnittestExtensions.OptionalBoolExtension, true); + + Assert.True(message.IsInitialized()); + + message.GetOrRegisterExtension(UnittestExtensions.RepeatedBoolExtension).Add(true); + + Assert.True(message.IsInitialized()); + } + + [Test] + public void RequiredFieldInNestedMessageMapValue() + { + var message = new TestRequiredMap(); + message.Foo.Add(0, new TestRequiredMap.Types.NestedMessage()); + + Assert.False(message.IsInitialized()); + + message.Foo[0].RequiredInt32 = 12; + + Assert.True(message.IsInitialized()); + } + + [Test] + public void RoundTrip_Groups() + { + var message = new TestAllTypes + { + OptionalGroup = new TestAllTypes.Types.OptionalGroup + { + A = 10 + }, + RepeatedGroup = + { + new TestAllTypes.Types.RepeatedGroup { A = 10 }, + new TestAllTypes.Types.RepeatedGroup { A = 20 }, + new TestAllTypes.Types.RepeatedGroup { A = 30 } + } + }; + + byte[] bytes = message.ToByteArray(); + TestAllTypes parsed = TestAllTypes.Parser.ParseFrom(bytes); + Assert.AreEqual(message, parsed); + } + + [Test] + public void RoundTrip_ExtensionGroups() + { + var message = new TestAllExtensions(); + message.SetExtension(UnittestExtensions.OptionalGroupExtension, new OptionalGroup_extension { A = 10 }); + message.GetOrRegisterExtension(UnittestExtensions.RepeatedGroupExtension).AddRange(new[] + { + new RepeatedGroup_extension { A = 10 }, + new RepeatedGroup_extension { A = 20 }, + new RepeatedGroup_extension { A = 30 } + }); + + byte[] bytes = message.ToByteArray(); + TestAllExtensions extendable_parsed = TestAllExtensions.Parser.WithExtensionRegistry(new ExtensionRegistry() { UnittestExtensions.OptionalGroupExtension, UnittestExtensions.RepeatedGroupExtension }).ParseFrom(bytes); + Assert.AreEqual(message, extendable_parsed); + } + + [Test] + public void RoundTrip_NestedExtensionGroup() + { + var message = new TestGroupExtension(); + message.SetExtension(TestNestedExtension.Extensions.OptionalGroupExtension, new TestNestedExtension.Types.OptionalGroup_extension { A = 10 }); + + byte[] bytes = message.ToByteArray(); + TestGroupExtension extendable_parsed = TestGroupExtension.Parser.WithExtensionRegistry(new ExtensionRegistry() { TestNestedExtension.Extensions.OptionalGroupExtension }).ParseFrom(bytes); + Assert.AreEqual(message, extendable_parsed); + } + } +} diff --git a/csharp/src/Google.Protobuf.Test/GeneratedMessageTest.cs b/csharp/src/Google.Protobuf.Test/GeneratedMessageTest.cs index 6b2af137ba..103df7dd2f 100644 --- a/csharp/src/Google.Protobuf.Test/GeneratedMessageTest.cs +++ b/csharp/src/Google.Protobuf.Test/GeneratedMessageTest.cs @@ -45,7 +45,7 @@ namespace Google.Protobuf /// /// Tests around the generated TestAllTypes message. /// - public class GeneratedMessageTest + public partial class GeneratedMessageTest { [Test] public void EmptyMessageFieldDistinctFromMissingMessageField() @@ -112,50 +112,6 @@ namespace Google.Protobuf Assert.AreEqual("", message.OneofString); Assert.AreEqual(ByteString.Empty, message.OneofBytes); Assert.IsNull(message.OneofNestedMessage); - - // proto2 default values - var message2 = new Proto2.TestAllTypes(); - Assert.AreEqual(true, message2.DefaultBool); - Assert.AreEqual(ByteString.CopyFromUtf8("world"), message2.DefaultBytes); - Assert.AreEqual("123", message2.DefaultCord); - Assert.AreEqual(52e3, message2.DefaultDouble); - Assert.AreEqual(47, message2.DefaultFixed32); - Assert.AreEqual(48, message2.DefaultFixed64); - Assert.AreEqual(51.5, message2.DefaultFloat); - Assert.AreEqual(Proto2.ForeignEnum.ForeignBar, message2.DefaultForeignEnum); - Assert.AreEqual(Proto2.ImportEnum.ImportBar, message2.DefaultImportEnum); - Assert.AreEqual(41, message2.DefaultInt32); - Assert.AreEqual(42, message2.DefaultInt64); - Assert.AreEqual(Proto2.TestAllTypes.Types.NestedEnum.Bar, message2.DefaultNestedEnum); - Assert.AreEqual(49, message2.DefaultSfixed32); - Assert.AreEqual(-50, message2.DefaultSfixed64); - Assert.AreEqual(-45, message2.DefaultSint32); - Assert.AreEqual(46, message2.DefaultSint64); - Assert.AreEqual("hello", message2.DefaultString); - Assert.AreEqual("abc", message2.DefaultStringPiece); - Assert.AreEqual(43, message2.DefaultUint32); - Assert.AreEqual(44, message2.DefaultUint64); - - Assert.False(message2.HasDefaultBool); - Assert.False(message2.HasDefaultBytes); - Assert.False(message2.HasDefaultCord); - Assert.False(message2.HasDefaultDouble); - Assert.False(message2.HasDefaultFixed32); - Assert.False(message2.HasDefaultFixed64); - Assert.False(message2.HasDefaultFloat); - Assert.False(message2.HasDefaultForeignEnum); - Assert.False(message2.HasDefaultImportEnum); - Assert.False(message2.HasDefaultInt32); - Assert.False(message2.HasDefaultInt64); - Assert.False(message2.HasDefaultNestedEnum); - Assert.False(message2.HasDefaultSfixed32); - Assert.False(message2.HasDefaultSfixed64); - Assert.False(message2.HasDefaultSint32); - Assert.False(message2.HasDefaultSint64); - Assert.False(message2.HasDefaultString); - Assert.False(message2.HasDefaultStringPiece); - Assert.False(message2.HasDefaultUint32); - Assert.False(message2.HasDefaultUint64); } [Test] @@ -168,115 +124,6 @@ namespace Google.Protobuf Assert.Throws(() => message.OneofBytes = null); } - [Test] - public void FieldPresence() - { - var message = new Proto2.TestAllTypes(); - - Assert.False(message.HasOptionalBool); - Assert.False(message.OptionalBool); - - message.OptionalBool = true; - - Assert.True(message.HasOptionalBool); - Assert.True(message.OptionalBool); - - message.OptionalBool = false; - - Assert.True(message.HasOptionalBool); - Assert.False(message.OptionalBool); - - message.ClearOptionalBool(); - - Assert.False(message.HasOptionalBool); - Assert.False(message.OptionalBool); - - Assert.False(message.HasDefaultBool); - Assert.True(message.DefaultBool); - - message.DefaultBool = false; - - Assert.True(message.HasDefaultBool); - Assert.False(message.DefaultBool); - - message.DefaultBool = true; - - Assert.True(message.HasDefaultBool); - Assert.True(message.DefaultBool); - - message.ClearDefaultBool(); - - Assert.False(message.HasDefaultBool); - Assert.True(message.DefaultBool); - } - - [Test] - public void RequiredFields() - { - var message = new Proto2.TestRequired(); - Assert.False(message.IsInitialized()); - - message.A = 1; - message.B = 2; - message.C = 3; - - Assert.True(message.IsInitialized()); - } - - [Test] - public void RequiredFieldsInExtensions() - { - var message = new Proto2.TestAllExtensions(); - Assert.True(message.IsInitialized()); - - message.SetExtension(Proto2.TestRequired.Extensions.Single, new Proto2.TestRequired()); - - Assert.False(message.IsInitialized()); - - var extensionMessage = message.GetExtension(Proto2.TestRequired.Extensions.Single); - extensionMessage.A = 1; - extensionMessage.B = 2; - extensionMessage.C = 3; - - Assert.True(message.IsInitialized()); - - message.GetOrRegisterExtension(Proto2.TestRequired.Extensions.Multi); - - Assert.True(message.IsInitialized()); - - message.GetExtension(Proto2.TestRequired.Extensions.Multi).Add(new Proto2.TestRequired()); - - Assert.False(message.IsInitialized()); - - extensionMessage = message.GetExtension(Proto2.TestRequired.Extensions.Multi)[0]; - extensionMessage.A = 1; - extensionMessage.B = 2; - extensionMessage.C = 3; - - Assert.True(message.IsInitialized()); - - message.SetExtension(Proto2.UnittestExtensions.OptionalBoolExtension, true); - - Assert.True(message.IsInitialized()); - - message.GetOrRegisterExtension(Proto2.UnittestExtensions.RepeatedBoolExtension).Add(true); - - Assert.True(message.IsInitialized()); - } - - [Test] - public void RequiredFieldInNestedMessageMapValue() - { - var message = new Proto2.TestRequiredMap(); - message.Foo.Add(0, new Proto2.TestRequiredMap.Types.NestedMessage()); - - Assert.False(message.IsInitialized()); - - message.Foo[0].RequiredInt32 = 12; - - Assert.True(message.IsInitialized()); - } - [Test] public void RoundTrip_Empty() { @@ -356,56 +203,6 @@ namespace Google.Protobuf Assert.AreEqual(message, parsed); } - [Test] - public void RoundTrip_Groups() - { - var message = new Proto2.TestAllTypes - { - OptionalGroup = new Proto2.TestAllTypes.Types.OptionalGroup - { - A = 10 - }, - RepeatedGroup = - { - new Proto2.TestAllTypes.Types.RepeatedGroup { A = 10 }, - new Proto2.TestAllTypes.Types.RepeatedGroup { A = 20 }, - new Proto2.TestAllTypes.Types.RepeatedGroup { A = 30 } - } - }; - - byte[] bytes = message.ToByteArray(); - Proto2.TestAllTypes parsed = Proto2.TestAllTypes.Parser.ParseFrom(bytes); - Assert.AreEqual(message, parsed); - } - - [Test] - public void RoundTrip_ExtensionGroups() - { - var message = new Proto2.TestAllExtensions(); - message.SetExtension(Proto2.UnittestExtensions.OptionalGroupExtension, new Proto2.OptionalGroup_extension { A = 10 }); - message.GetOrRegisterExtension(Proto2.UnittestExtensions.RepeatedGroupExtension).AddRange(new[] - { - new Proto2.RepeatedGroup_extension { A = 10 }, - new Proto2.RepeatedGroup_extension { A = 20 }, - new Proto2.RepeatedGroup_extension { A = 30 } - }); - - byte[] bytes = message.ToByteArray(); - Proto2.TestAllExtensions extendable_parsed = Proto2.TestAllExtensions.Parser.WithExtensionRegistry(new ExtensionRegistry() { Proto2.UnittestExtensions.OptionalGroupExtension, Proto2.UnittestExtensions.RepeatedGroupExtension }).ParseFrom(bytes); - Assert.AreEqual(message, extendable_parsed); - } - - [Test] - public void RoundTrip_NestedExtensionGroup() - { - var message = new Proto2.TestGroupExtension(); - message.SetExtension(Proto2.TestNestedExtension.Extensions.OptionalGroupExtension, new Proto2.TestNestedExtension.Types.OptionalGroup_extension { A = 10 }); - - byte[] bytes = message.ToByteArray(); - Proto2.TestGroupExtension extendable_parsed = Proto2.TestGroupExtension.Parser.WithExtensionRegistry(new ExtensionRegistry() { Proto2.TestNestedExtension.Extensions.OptionalGroupExtension }).ParseFrom(bytes); - Assert.AreEqual(message, extendable_parsed); - } - // Note that not every map within map_unittest_proto3 is used. They all go through very // similar code paths. The fact that all maps are present is validation that we have codecs // for every type. diff --git a/csharp/src/Google.Protobuf.Test/Reflection/CustomOptionsTest.cs b/csharp/src/Google.Protobuf.Test/Reflection/CustomOptionsTest.cs index 527fa8ae1c..02fe778fbc 100644 --- a/csharp/src/Google.Protobuf.Test/Reflection/CustomOptionsTest.cs +++ b/csharp/src/Google.Protobuf.Test/Reflection/CustomOptionsTest.cs @@ -73,8 +73,8 @@ namespace Google.Protobuf.Test.Reflection [Test] public void ScalarOptions() { - var options = CustomOptionOtherValues.Descriptor.CustomOptions; var d = CustomOptionOtherValues.Descriptor; + var options = d.CustomOptions; AssertOption(-100, options.TryGetInt32, Int32Opt, d.GetOption); AssertOption(12.3456789f, options.TryGetFloat, FloatOpt, d.GetOption); AssertOption(1.234567890123456789d, options.TryGetDouble, DoubleOpt, d.GetOption); @@ -86,8 +86,9 @@ namespace Google.Protobuf.Test.Reflection [Test] public void MessageOptions() { - var options = VariousComplexOptions.Descriptor.CustomOptions; - AssertOption(new ComplexOptionType1 { Foo = 42, Foo4 = { 99, 88 } }, options.TryGetMessage, ComplexOpt1, VariousComplexOptions.Descriptor.GetOption); + var d = VariousComplexOptions.Descriptor; + var options = d.CustomOptions; + AssertOption(new ComplexOptionType1 { Foo = 42, Foo4 = { 99, 88 } }, options.TryGetMessage, ComplexOpt1, d.GetOption); AssertOption(new ComplexOptionType2 { Baz = 987, @@ -95,8 +96,8 @@ namespace Google.Protobuf.Test.Reflection Fred = new ComplexOptionType4 { Waldo = 321 }, Barney = { new ComplexOptionType4 { Waldo = 101 }, new ComplexOptionType4 { Waldo = 212 } } }, - options.TryGetMessage, ComplexOpt2, VariousComplexOptions.Descriptor.GetOption); - AssertOption(new ComplexOptionType3 { Qux = 9 }, options.TryGetMessage, ComplexOpt3, VariousComplexOptions.Descriptor.GetOption); + options.TryGetMessage, ComplexOpt2, d.GetOption); + AssertOption(new ComplexOptionType3 { Qux = 9 }, options.TryGetMessage, ComplexOpt3, d.GetOption); } [Test] @@ -132,8 +133,8 @@ namespace Google.Protobuf.Test.Reflection [Test] public void MinValues() { - var options = CustomOptionMinIntegerValues.Descriptor.CustomOptions; var d = CustomOptionMinIntegerValues.Descriptor; + var options = d.CustomOptions; AssertOption(false, options.TryGetBool, BoolOpt, d.GetOption); AssertOption(int.MinValue, options.TryGetInt32, Int32Opt, d.GetOption); AssertOption(long.MinValue, options.TryGetInt64, Int64Opt, d.GetOption); @@ -150,8 +151,8 @@ namespace Google.Protobuf.Test.Reflection [Test] public void MaxValues() { - var options = CustomOptionMaxIntegerValues.Descriptor.CustomOptions; var d = CustomOptionMaxIntegerValues.Descriptor; + var options = d.CustomOptions; AssertOption(true, options.TryGetBool, BoolOpt, d.GetOption); AssertOption(int.MaxValue, options.TryGetInt32, Int32Opt, d.GetOption); AssertOption(long.MaxValue, options.TryGetInt64, Int64Opt, d.GetOption); diff --git a/csharp/src/Google.Protobuf.Test/Reflection/FieldAccessTest.cs b/csharp/src/Google.Protobuf.Test/Reflection/FieldAccessTest.cs index 6332afc470..fcfff4f7ac 100644 --- a/csharp/src/Google.Protobuf.Test/Reflection/FieldAccessTest.cs +++ b/csharp/src/Google.Protobuf.Test/Reflection/FieldAccessTest.cs @@ -86,64 +86,40 @@ namespace Google.Protobuf.Reflection var dictionary = (IDictionary) fields[TestMap.MapStringStringFieldNumber].Accessor.GetValue(mapMessage); Assert.AreEqual(mapMessage.MapStringString, dictionary); Assert.AreEqual("value1", dictionary["key1"]); + } + + [Test] + public void GetValue_IncorrectType() + { + IMessage message = SampleMessages.CreateFullTestAllTypes(); + var fields = message.Descriptor.Fields; + Assert.Throws(() => fields[TestProtos.TestAllTypes.SingleBoolFieldNumber].Accessor.GetValue(new TestMap())); } [Test] - public void GetExtensionValue() + public void HasValue_Proto3() { - var message = SampleMessages.CreateFullTestAllExtensions(); - - // test that the reflector works, since the reflector just runs through IExtendableMessage - Assert.AreEqual(message.GetExtension(OptionalBoolExtension), Proto2.TestAllExtensions.Descriptor.FindFieldByNumber(OptionalBoolExtension.FieldNumber).Accessor.GetValue(message)); + IMessage message = SampleMessages.CreateFullTestAllTypes(); + var fields = message.Descriptor.Fields; + Assert.Throws(() => fields[TestProtos.TestAllTypes.SingleBoolFieldNumber].Accessor.HasValue(message)); } [Test] - public void GetRepeatedExtensionValue() + public void HasValue() { - // check to make sure repeated accessor uses GetOrRegister - var message = new Proto2.TestAllExtensions(); + IMessage message = new Proto2.TestAllTypes(); + var fields = message.Descriptor.Fields; + var accessor = fields[Proto2.TestAllTypes.OptionalBoolFieldNumber].Accessor; - Assert.IsNull(message.GetExtension(RepeatedBoolExtension)); - Assert.IsNotNull(Proto2.TestAllExtensions.Descriptor.FindFieldByNumber(RepeatedBoolExtension.FieldNumber).Accessor.GetValue(message)); - Assert.IsNotNull(message.GetExtension(RepeatedBoolExtension)); + Assert.False(accessor.HasValue(message)); - message.ClearExtension(RepeatedBoolExtension); - Assert.IsNull(message.GetExtension(RepeatedBoolExtension)); - } - - [Test] - public void Clear() - { - var message = SampleMessages.CreateFullTestAllTypes(); - var fields = TestProtos.TestAllTypes.Descriptor.Fields; - fields[TestProtos.TestAllTypes.SingleBoolFieldNumber].Accessor.Clear(message); - fields[TestProtos.TestAllTypes.SingleInt32FieldNumber].Accessor.Clear(message); - fields[TestProtos.TestAllTypes.SingleStringFieldNumber].Accessor.Clear(message); - fields[TestProtos.TestAllTypes.SingleBytesFieldNumber].Accessor.Clear(message); - fields[TestProtos.TestAllTypes.SingleForeignEnumFieldNumber].Accessor.Clear(message); - fields[TestProtos.TestAllTypes.SingleForeignMessageFieldNumber].Accessor.Clear(message); - fields[TestProtos.TestAllTypes.RepeatedDoubleFieldNumber].Accessor.Clear(message); - - var expected = new TestAllTypes(SampleMessages.CreateFullTestAllTypes()) - { - SingleBool = false, - SingleInt32 = 0, - SingleString = "", - SingleBytes = ByteString.Empty, - SingleForeignEnum = 0, - SingleForeignMessage = null, - }; - expected.RepeatedDouble.Clear(); - - Assert.AreEqual(expected, message); - - // Separately, maps. - var mapMessage = new TestMap { MapStringString = { { "key1", "value1" }, { "key2", "value2" } } }; - fields = TestMap.Descriptor.Fields; - fields[TestMap.MapStringStringFieldNumber].Accessor.Clear(mapMessage); - Assert.AreEqual(0, mapMessage.MapStringString.Count); + accessor.SetValue(message, true); + Assert.True(accessor.HasValue(message)); + + accessor.Clear(message); + Assert.False(accessor.HasValue(message)); } - + [Test] public void SetValue_SingleFields() { @@ -170,8 +146,8 @@ namespace Google.Protobuf.Reflection }; Assert.AreEqual(expected, message); - } - + } + [Test] public void SetValue_SingleFields_WrongType() { @@ -196,14 +172,6 @@ namespace Google.Protobuf.Reflection Assert.Throws(() => fields[TestProtos.TestAllTypes.RepeatedDoubleFieldNumber].Accessor.SetValue(message, new double[10])); } - [Test] - public void GetValue_IncorrectType() - { - IMessage message = SampleMessages.CreateFullTestAllTypes(); - var fields = message.Descriptor.Fields; - Assert.Throws(() => fields[TestProtos.TestAllTypes.SingleBoolFieldNumber].Accessor.GetValue(new TestMap())); - } - [Test] public void Oneof() { @@ -222,6 +190,39 @@ namespace Google.Protobuf.Reflection oneof.Accessor.Clear(message); Assert.AreEqual(TestProtos.TestAllTypes.OneofFieldOneofCase.None, message.OneofFieldCase); + } + + [Test] + public void Clear() + { + var message = SampleMessages.CreateFullTestAllTypes(); + var fields = TestProtos.TestAllTypes.Descriptor.Fields; + fields[TestProtos.TestAllTypes.SingleBoolFieldNumber].Accessor.Clear(message); + fields[TestProtos.TestAllTypes.SingleInt32FieldNumber].Accessor.Clear(message); + fields[TestProtos.TestAllTypes.SingleStringFieldNumber].Accessor.Clear(message); + fields[TestProtos.TestAllTypes.SingleBytesFieldNumber].Accessor.Clear(message); + fields[TestProtos.TestAllTypes.SingleForeignEnumFieldNumber].Accessor.Clear(message); + fields[TestProtos.TestAllTypes.SingleForeignMessageFieldNumber].Accessor.Clear(message); + fields[TestProtos.TestAllTypes.RepeatedDoubleFieldNumber].Accessor.Clear(message); + + var expected = new TestAllTypes(SampleMessages.CreateFullTestAllTypes()) + { + SingleBool = false, + SingleInt32 = 0, + SingleString = "", + SingleBytes = ByteString.Empty, + SingleForeignEnum = 0, + SingleForeignMessage = null, + }; + expected.RepeatedDouble.Clear(); + + Assert.AreEqual(expected, message); + + // Separately, maps. + var mapMessage = new TestMap { MapStringString = { { "key1", "value1" }, { "key2", "value2" } } }; + fields = TestMap.Descriptor.Fields; + fields[TestMap.MapStringStringFieldNumber].Accessor.Clear(mapMessage); + Assert.AreEqual(0, mapMessage.MapStringString.Count); } [Test] @@ -240,5 +241,28 @@ namespace Google.Protobuf.Reflection Assert.Throws(() => descriptor.Fields[999999].ToString()); Assert.Throws(() => descriptor.Fields["not found"].ToString()); } + + [Test] + public void GetExtensionValue() + { + var message = SampleMessages.CreateFullTestAllExtensions(); + + // test that the reflector works, since the reflector just runs through IExtendableMessage + Assert.AreEqual(message.GetExtension(OptionalBoolExtension), Proto2.TestAllExtensions.Descriptor.FindFieldByNumber(OptionalBoolExtension.FieldNumber).Accessor.GetValue(message)); + } + + [Test] + public void GetRepeatedExtensionValue() + { + // check to make sure repeated accessor uses GetOrRegister + var message = new Proto2.TestAllExtensions(); + + Assert.IsNull(message.GetExtension(RepeatedBoolExtension)); + Assert.IsNotNull(Proto2.TestAllExtensions.Descriptor.FindFieldByNumber(RepeatedBoolExtension.FieldNumber).Accessor.GetValue(message)); + Assert.IsNotNull(message.GetExtension(RepeatedBoolExtension)); + + message.ClearExtension(RepeatedBoolExtension); + Assert.IsNull(message.GetExtension(RepeatedBoolExtension)); + } } }