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 DynamicMessageCopy()
        {
            TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
            TestUtil.SetAllFields(builder);
            TestAllTypes message = builder.Build();

            DynamicMessage copy = DynamicMessage.CreateBuilder(message).Build();

            reflectionTester.AssertAllFieldsSetViaReflection(copy);

            // Oneof
            FieldDescriptor bytesField =
                TestAllTypes.Descriptor.FindFieldByName("oneof_bytes");
            FieldDescriptor uint32Field =
                TestAllTypes.Descriptor.FindFieldByName("oneof_uint32");

            Assert.True(copy.HasField(bytesField));
            Assert.False(copy.HasField(uint32Field));

            DynamicMessage.Builder dynamicBuilder = DynamicMessage.CreateBuilder(message);
            dynamicBuilder[uint32Field] = 123U;
            DynamicMessage copy2 = dynamicBuilder.Build();

            Assert.IsFalse(copy2.HasField(bytesField));
            Assert.IsTrue(copy2.HasField(uint32Field));
            Assert.AreEqual(123U, copy2[uint32Field]);
        }
Beispiel #3
0
 public void DoubleBuildError()
 {
     DynamicMessage.Builder builder = DynamicMessage.CreateBuilder(TestAllTypes.Descriptor);
     builder.Build();
     try {
         builder.Build();
         Assert.Fail("Should have thrown exception.");
     } catch (InvalidOperationException) {
         // Success.
     }
 }
        public void RequiredDynamic()
        {
            MessageDescriptor descriptor = TestRequired.Descriptor;

            DynamicMessage.Builder builder = DynamicMessage.CreateBuilder(descriptor);

            Assert.IsFalse(builder.IsInitialized);
            builder[descriptor.FindDescriptor <FieldDescriptor>("a")] = 1;
            Assert.IsFalse(builder.IsInitialized);
            builder[descriptor.FindDescriptor <FieldDescriptor>("b")] = 1;
            Assert.IsFalse(builder.IsInitialized);
            builder[descriptor.FindDescriptor <FieldDescriptor>("c")] = 1;
            Assert.IsTrue(builder.IsInitialized);
        }
        public void RequiredDynamicForeign()
        {
            MessageDescriptor descriptor = TestRequiredForeign.Descriptor;

            DynamicMessage.Builder builder = DynamicMessage.CreateBuilder(descriptor);

            Assert.IsTrue(builder.IsInitialized);

            builder[descriptor.FindDescriptor <FieldDescriptor>("optional_message")] = TestRequiredUninitialized;
            Assert.IsFalse(builder.IsInitialized);

            builder[descriptor.FindDescriptor <FieldDescriptor>("optional_message")] = TestRequiredInitialized;
            Assert.IsTrue(builder.IsInitialized);

            builder.AddRepeatedField(descriptor.FindDescriptor <FieldDescriptor>("repeated_message"), TestRequiredUninitialized);
            Assert.IsFalse(builder.IsInitialized);

            builder.SetRepeatedField(descriptor.FindDescriptor <FieldDescriptor>("repeated_message"), 0, TestRequiredInitialized);
            Assert.IsTrue(builder.IsInitialized);
        }
Beispiel #6
0
        public void ToBuilder()
        {
            DynamicMessage.Builder builder =
                DynamicMessage.CreateBuilder(TestAllTypes.Descriptor);
            reflectionTester.SetAllFieldsViaReflection(builder);
            int   unknownFieldNum = 9;
            ulong unknownFieldVal = 90;

            builder.SetUnknownFields(UnknownFieldSet.CreateBuilder()
                                     .AddField(unknownFieldNum,
                                               UnknownField.CreateBuilder().AddVarint(unknownFieldVal).Build())
                                     .Build());
            DynamicMessage message = builder.Build();

            DynamicMessage derived = message.ToBuilder().Build();

            reflectionTester.AssertAllFieldsSetViaReflection(derived);

            IList <ulong> values = derived.UnknownFields.FieldDictionary[unknownFieldNum].VarintList;

            Assert.AreEqual(1, values.Count);
            Assert.AreEqual(unknownFieldVal, values[0]);
        }
 public void DoubleBuildError()
 {
     DynamicMessage.Builder builder = DynamicMessage.CreateBuilder(TestAllTypes.Descriptor);
     builder.Build();
     Assert.Throws <InvalidOperationException>(() => builder.Build());
 }