public void RoundTrip_ExtensionGroups() { var message = new TestAllExtensions(); message.SetExtension(UnittestExtensions.OptionalGroupExtension, new OptionalGroup_extension { A = 10 }); message.GetOrInitializeExtension(UnittestExtensions.RepeatedGroupExtension).AddRange(new[] { new RepeatedGroup_extension { A = 10 }, new RepeatedGroup_extension { A = 20 }, new RepeatedGroup_extension { A = 30 } }); MessageParsingHelpers.AssertRoundtrip( TestAllExtensions.Parser.WithExtensionRegistry(new ExtensionRegistry() { UnittestExtensions.OptionalGroupExtension, UnittestExtensions.RepeatedGroupExtension }), message); }
public void RoundTrip_ExtensionGroups() { var message = new TestAllExtensions(); message.SetExtension(UnittestExtensions.OptionalGroupExtension, new OptionalGroup_extension { A = 10 }); message.GetOrInitializeExtension(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); }
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.GetOrInitializeExtension(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.GetOrInitializeExtension(UnittestExtensions.RepeatedBoolExtension).Add(true); Assert.True(message.IsInitialized()); }
public void GetRepeated() { var extensionValue = new TestAllTypes.Types.NestedMessage() { Bb = 42 }; var untypedExtension = new Extension <TestAllExtensions, IList>(RepeatedNestedMessageExtension.FieldNumber, codec: null); var wrongTypedExtension = new RepeatedExtension <TestAllExtensions, TestAllTypes>(RepeatedNestedMessageExtension.FieldNumber, codec: null); var message = new TestAllExtensions(); var value1 = message.GetExtension(untypedExtension); Assert.IsNull(value1); var repeatedField = message.GetOrInitializeExtension <TestAllTypes.Types.NestedMessage>(RepeatedNestedMessageExtension); repeatedField.Add(extensionValue); var value2 = message.GetExtension(untypedExtension); Assert.IsNotNull(value2); Assert.AreEqual(1, value2.Count); var valueBytes = ((IMessage)value2[0]).ToByteArray(); var parsedValue = TestProtos.Proto2.TestAllTypes.Types.NestedMessage.Parser.ParseFrom(valueBytes); Assert.AreEqual(extensionValue, parsedValue); var ex = Assert.Throws <InvalidOperationException>(() => message.GetExtension(wrongTypedExtension)); var expectedMessage = "The stored extension value has a type of 'Google.Protobuf.TestProtos.Proto2.TestAllTypes+Types+NestedMessage, Google.Protobuf.Test.TestProtos, Version=1.0.0.0, Culture=neutral, PublicKeyToken=a7d26565bac4d604'. " + "This a different from the requested type of 'Google.Protobuf.TestProtos.Proto2.TestAllTypes, Google.Protobuf.Test.TestProtos, Version=1.0.0.0, Culture=neutral, PublicKeyToken=a7d26565bac4d604'."; Assert.AreEqual(expectedMessage, ex.Message); }