public void TestSetOneofClearsOthers()
        {
            TestOneof2.Builder builder = TestOneof2.CreateBuilder();
            TestOneof2         message =
                builder.SetFooInt(123).SetFooString("foo").Build();

            Assert.IsTrue(message.HasFooString);
            TestUtil.AssertAtMostOneFieldSetOneof(message);

            message = builder.SetFooCord("bar").Build();
            Assert.IsTrue(message.HasFooCord);
            TestUtil.AssertAtMostOneFieldSetOneof(message);

            message = builder.SetFooStringPiece("baz").Build();
            Assert.IsTrue(message.HasFooStringPiece);
            TestUtil.AssertAtMostOneFieldSetOneof(message);

            message = builder.SetFooBytes(TestUtil.ToBytes("qux")).Build();
            Assert.IsTrue(message.HasFooBytes);
            TestUtil.AssertAtMostOneFieldSetOneof(message);

            message = builder.SetFooEnum(TestOneof2.Types.NestedEnum.FOO).Build();
            Assert.IsTrue(message.HasFooEnum);
            TestUtil.AssertAtMostOneFieldSetOneof(message);

            message = builder.SetFooMessage(
                TestOneof2.Types.NestedMessage.CreateBuilder().SetQuxInt(234).Build()).Build();
            Assert.IsTrue(message.HasFooMessage);
            TestUtil.AssertAtMostOneFieldSetOneof(message);

            message = builder.SetFooInt(123).Build();
            Assert.IsTrue(message.HasFooInt);
            TestUtil.AssertAtMostOneFieldSetOneof(message);
        }
        public void DynamicOneofMessage()
        {
            DynamicMessage.Builder builder =
                DynamicMessage.CreateBuilder(TestAllTypes.Descriptor);
            OneofDescriptor oneof = TestAllTypes.Descriptor.Oneofs[0];

            Assert.False(builder.HasOneof(oneof));
            Assert.AreSame(null, builder.OneofFieldDescriptor(oneof));

            reflectionTester.SetAllFieldsViaReflection(builder);
            Assert.True(builder.HasOneof(oneof));
            FieldDescriptor field = oneof.Field(3);

            Assert.AreSame(field, builder.OneofFieldDescriptor(oneof));
            Assert.AreEqual(TestUtil.ToBytes("604"), builder[field]);

            DynamicMessage message = builder.BuildPartial();

            Assert.IsTrue(message.HasOneof(oneof));

            DynamicMessage.Builder mergedBuilder =
                DynamicMessage.CreateBuilder(TestAllTypes.Descriptor);
            FieldDescriptor mergedField = oneof.Field(0);

            mergedBuilder[mergedField] = 123U;
            Assert.IsTrue(mergedBuilder.HasField(mergedField));
            mergedBuilder.MergeFrom(message);
            Assert.IsTrue(mergedBuilder.HasField(field));
            Assert.IsFalse(mergedBuilder.HasField(mergedField));

            mergedBuilder.ClearOneof(oneof);
            Assert.AreSame(null, mergedBuilder.OneofFieldDescriptor(oneof));
            message = mergedBuilder.Build();
            Assert.AreSame(null, message.OneofFieldDescriptor(oneof));
        }
        public void RepeatedAppendRejectsNull()
        {
            TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();

            ForeignMessage foreignMessage = ForeignMessage.CreateBuilder().SetC(12).Build();

            TestUtil.AssertArgumentNullException(
                () => builder.AddRangeRepeatedForeignMessage(new[] { foreignMessage, null }));
            TestUtil.AssertArgumentNullException(() => builder.AddRangeRepeatedForeignMessage(null));
            TestUtil.AssertArgumentNullException(() => builder.AddRangeRepeatedForeignEnum(null));
            TestUtil.AssertArgumentNullException(() => builder.AddRangeRepeatedString(new[] { "one", null }));
            TestUtil.AssertArgumentNullException(
                () => builder.AddRangeRepeatedBytes(new[] { TestUtil.ToBytes("one"), null }));
        }