Beispiel #1
0
        private static void AssertParseError(string error, string text)
        {
            TestAllTypes.Builder builder   = TestAllTypes.CreateBuilder();
            Exception            exception = Assert.Throws <FormatException>(() => TextFormat.Merge(text, TestUtil.CreateExtensionRegistry(), builder));

            Assert.AreEqual(error, exception.Message);
        }
Beispiel #2
0
        public void TestHasMethod()
        {
            // Optional non-message fields don't have HasFoo method generated
            Type proto2Type = typeof(Google.ProtocolBuffers.TestProtos.TestAllTypes);
            Type proto3Type = typeof(TestAllTypes);

            CheckHasMethodRemoved(proto2Type, proto3Type, "OptionalInt32");
            CheckHasMethodRemoved(proto2Type, proto3Type, "OptionalString");
            CheckHasMethodRemoved(proto2Type, proto3Type, "OptionalBytes");
            CheckHasMethodRemoved(proto2Type, proto3Type, "OptionalNestedEnum");

            Type proto2BuilderType = typeof(Google.ProtocolBuffers.TestProtos.TestAllTypes.Builder);
            Type proto3BuilderType = typeof(TestAllTypes.Builder);

            CheckHasMethodRemoved(proto2BuilderType, proto3BuilderType, "OptionalInt32");
            CheckHasMethodRemoved(proto2BuilderType, proto3BuilderType, "OptionalString");
            CheckHasMethodRemoved(proto2BuilderType, proto3BuilderType, "OptionalBytes");
            CheckHasMethodRemoved(proto2BuilderType, proto3BuilderType, "OptionalNestedEnum");

            // message fields still have the HasFoo method generated
            Assert.IsFalse(TestAllTypes.CreateBuilder().Build().HasOptionalNestedMessage);
            Assert.IsFalse(TestAllTypes.CreateBuilder().HasOptionalNestedMessage);

            // oneof fields don't have the HasFoo method (even for message types)
            CheckHasMethodRemoved(proto2Type, proto3Type, "OneofUint32");
            CheckHasMethodRemoved(proto2Type, proto3Type, "OneofString");
            CheckHasMethodRemoved(proto2Type, proto3Type, "OneofNestedMessage");

            CheckHasMethodRemoved(proto2BuilderType, proto3BuilderType, "OneofUint32");
            CheckHasMethodRemoved(proto2BuilderType, proto3BuilderType, "OneofString");
            CheckHasMethodRemoved(proto2BuilderType, proto3BuilderType, "OneofNestedMessage");
        }
 public void ParseAngleBrackets()
 {
     TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
     TextFormat.Merge("OptionalGroup: < a: 1 >", builder);
     Assert.IsTrue(builder.HasOptionalGroup);
     Assert.AreEqual(1, builder.OptionalGroup.A);
 }
        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]);
        }
        public void ParseCompatibility()
        {
            string original = "repeated_float: inf\n" +
                              "repeated_float: -inf\n" +
                              "repeated_float: nan\n" +
                              "repeated_float: inff\n" +
                              "repeated_float: -inff\n" +
                              "repeated_float: nanf\n" +
                              "repeated_float: 1.0f\n" +
                              "repeated_float: infinityf\n" +
                              "repeated_float: -Infinityf\n" +
                              "repeated_double: infinity\n" +
                              "repeated_double: -infinity\n" +
                              "repeated_double: nan\n";
            string canonical = "repeated_float: Infinity\n" +
                               "repeated_float: -Infinity\n" +
                               "repeated_float: NaN\n" +
                               "repeated_float: Infinity\n" +
                               "repeated_float: -Infinity\n" +
                               "repeated_float: NaN\n" +
                               "repeated_float: 1\n" + // Java has 1.0; this is fine
                               "repeated_float: Infinity\n" +
                               "repeated_float: -Infinity\n" +
                               "repeated_double: Infinity\n" +
                               "repeated_double: -Infinity\n" +
                               "repeated_double: NaN\n";

            TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
            TextFormat.Merge(original, builder);
            Assert.AreEqual(canonical, builder.Build().ToString());
        }
        public void Clear()
        {
            AbstractMessageWrapper message =
                new AbstractMessageWrapper.Builder(TestAllTypes.CreateBuilder(TestUtil.GetAllSet())).Clear().Build();

            TestUtil.AssertClear((TestAllTypes)message.WrappedMessage);
        }
        public void PrintExotic()
        {
            IMessage message = TestAllTypes.CreateBuilder()
                               // Signed vs. unsigned numbers.
                               .AddRepeatedInt32(-1)
                               .AddRepeatedUint32(uint.MaxValue)
                               .AddRepeatedInt64(-1)
                               .AddRepeatedUint64(ulong.MaxValue)

                               .AddRepeatedInt32(1 << 31)
                               .AddRepeatedUint32(1U << 31)
                               .AddRepeatedInt64(1L << 63)
                               .AddRepeatedUint64(1UL << 63)

                               // Floats of various precisions and exponents.
                               .AddRepeatedDouble(123)
                               .AddRepeatedDouble(123.5)
                               .AddRepeatedDouble(0.125)
                               .AddRepeatedDouble(123e15)
                               .AddRepeatedDouble(123.5e20)
                               .AddRepeatedDouble(123.5e-20)
                               .AddRepeatedDouble(123.456789)
                               .AddRepeatedDouble(Double.PositiveInfinity)
                               .AddRepeatedDouble(Double.NegativeInfinity)
                               .AddRepeatedDouble(Double.NaN)

                               // Strings and bytes that needing escaping.
                               .AddRepeatedString("\0\u0001\u0007\b\f\n\r\t\v\\\'\"\u1234")
                               .AddRepeatedBytes(Bytes("\0\u0001\u0007\b\f\n\r\t\v\\\'\"\u00fe"))
                               .Build();

            Assert.AreEqual(ExoticText, message.ToString());
        }
        public void Defaults()
        {
            TestUtil.AssertClear(TestAllTypes.DefaultInstance);
            TestUtil.AssertClear(TestAllTypes.CreateBuilder().Build());

            Assert.AreEqual("\u1234", TestExtremeDefaultValues.DefaultInstance.Utf8String);
        }
        public void Copy()
        {
            AbstractMessageWrapper message =
                new AbstractMessageWrapper.Builder(TestAllTypes.CreateBuilder()).MergeFrom(TestUtil.GetAllSet()).Build();

            TestUtil.AssertAllFieldsSet((TestAllTypes)message.WrappedMessage);
        }
Beispiel #10
0
        public void ReadHugeBlob()
        {
            // Allocate and initialize a 1MB blob.
            byte[] blob = new byte[1 << 20];
            for (int i = 0; i < blob.Length; i++)
            {
                blob[i] = (byte)i;
            }

            // Make a message containing it.
            TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
            TestUtil.SetAllFields(builder);
            builder.SetOptionalBytes(ByteString.CopyFrom(blob));
            TestAllTypes message = builder.Build();

            // Serialize and parse it.  Make sure to parse from an InputStream, not
            // directly from a ByteString, so that CodedInputStream uses buffered
            // reading.
            TestAllTypes message2 = TestAllTypes.ParseFrom(message.ToByteString().CreateCodedInput());

            Assert.AreEqual(message.OptionalBytes, message2.OptionalBytes);

            // Make sure all the other fields were parsed correctly.
            TestAllTypes message3 = TestAllTypes.CreateBuilder(message2)
                                    .SetOptionalBytes(TestUtil.GetAllSet().OptionalBytes)
                                    .Build();

            TestUtil.AssertAllFieldsSet(message3);
        }
 public void ReflectionDefaults()
 {
     TestUtil.TestInMultipleCultures(() => {
         reflectionTester.AssertClearViaReflection(TestAllTypes.DefaultInstance);
         reflectionTester.AssertClearViaReflection(TestAllTypes.CreateBuilder().Build());
     });
 }
        public void Parsing()
        {
            IBuilder builder = new AbstractMessageWrapper.Builder(TestAllTypes.CreateBuilder());
            AbstractMessageWrapper message = (AbstractMessageWrapper)builder.WeakMergeFrom(TestUtil.GetAllSet().ToByteString()).WeakBuild();

            TestUtil.AssertAllFieldsSet((TestAllTypes)message.WrappedMessage);
        }
 public void BuildNewBuilderIsDefaultInstance()
 {
     Assert.IsTrue(ReferenceEquals(TestAllTypes.DefaultInstance, new TestAllTypes.Builder().Build()));
     Assert.IsTrue(ReferenceEquals(TestAllTypes.DefaultInstance, TestAllTypes.CreateBuilder().Build()));
     //last test, if you clear a builder it reverts to default instance
     Assert.IsTrue(ReferenceEquals(TestAllTypes.DefaultInstance,
                                   TestAllTypes.CreateBuilder().SetOptionalBool(true).Build().ToBuilder().Clear().Build()));
 }
        public void MergeFromDynamic()
        {
            TestAllTypes result = (TestAllTypes)TestAllTypes.CreateBuilder(MergeDest)
                                  .MergeFrom(DynamicMessage.CreateBuilder(MergeSource).Build())
                                  .Build();

            Assert.AreEqual(MergeResultText, result.ToString());
        }
 public void Parse()
 {
     TestUtil.TestInMultipleCultures(() => {
         TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
         TextFormat.Merge(AllFieldsSetText, builder);
         TestUtil.AssertAllFieldsSet(builder.Build());
     });
 }
        public void ToBuilder()
        {
            TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
            TestUtil.SetAllFields(builder);
            TestAllTypes message = builder.Build();

            TestUtil.AssertAllFieldsSet(message.ToBuilder().Build());
        }
 public void ReflectionGetOneof()
 {
     TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
     reflectionTester.SetAllFieldsViaReflection(builder);
     Descriptors.OneofDescriptor oneof = TestAllTypes.Descriptor.Oneofs[0];
     Descriptors.FieldDescriptor field = TestAllTypes.Descriptor.FindFieldByName("oneof_bytes");
     Assert.AreSame(field, builder.OneofFieldDescriptor(oneof));
 }
        public void ReflectionSetters()
        {
            TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
            reflectionTester.SetAllFieldsViaReflection(builder);
            TestAllTypes message = builder.Build();

            TestUtil.AssertAllFieldsSet(message);
        }
        public void ReflectionRepeatedSetters()
        {
            TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
            reflectionTester.SetAllFieldsViaReflection(builder);
            reflectionTester.ModifyRepeatedFieldsViaReflection(builder);
            TestAllTypes message = builder.Build();

            TestUtil.AssertRepeatedFieldsModified(message);
        }
        public void MergeFrom()
        {
            AbstractMessageWrapper result = (AbstractMessageWrapper)
                                            new AbstractMessageWrapper.Builder(TestAllTypes.CreateBuilder(MergeDest))
                                            .MergeFrom(MergeSource)
                                            .Build();

            Assert.AreEqual(MergeResultText, result.ToString());
        }
        public void ParseExotic()
        {
            TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
            TextFormat.Merge(ExoticText, builder);

            // Too lazy to check things individually.  Don't try to debug this
            // if testPrintExotic() is Assert.Failing.
            Assert.AreEqual(ExoticText, builder.Build().ToString());
        }
 private static void AssertParseError(string error, string text)
 {
     TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
     try {
         TextFormat.Merge(text, TestUtil.CreateExtensionRegistry(), builder);
         Assert.Fail("Expected parse exception.");
     } catch (FormatException e) {
         Assert.AreEqual(error, e.Message);
     }
 }
Beispiel #23
0
        public void DynamicMessageCopy()
        {
            TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
            TestUtil.SetAllFields(builder);
            TestAllTypes message = builder.Build();

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

            reflectionTester.AssertAllFieldsSetViaReflection(copy);
        }
 public void ParseComment()
 {
     TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
     TextFormat.Merge(
         "# this is a comment\n" +
         "optional_int32: 1  # another comment\n" +
         "optional_int64: 2\n" +
         "# EOF comment", builder);
     Assert.AreEqual(1, builder.OptionalInt32);
     Assert.AreEqual(2, builder.OptionalInt64);
 }
Beispiel #25
0
        public void TestMessageField()
        {
            TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
            Assert.IsFalse(builder.HasOptionalNestedMessage);
            Assert.IsFalse(builder.Build().HasOptionalNestedMessage);

            // Unlike non-message fields, if we set default value to message field, the field
            // shoule be seem as present.
            builder.SetOptionalNestedMessage(TestAllTypes.Types.NestedMessage.DefaultInstance);
            Assert.IsTrue(builder.HasOptionalNestedMessage);
            Assert.IsTrue(builder.Build().HasOptionalNestedMessage);
        }
        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 }));
        }
 public void SettersRejectNull()
 {
     TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
     TestUtil.AssertArgumentNullException(() => builder.SetOptionalString(null));
     TestUtil.AssertArgumentNullException(() => builder.SetOptionalBytes(null));
     TestUtil.AssertArgumentNullException(() => builder.SetOptionalNestedMessage((TestAllTypes.Types.NestedMessage)null));
     TestUtil.AssertArgumentNullException(() => builder.SetOptionalNestedMessage((TestAllTypes.Types.NestedMessage.Builder)null));
     TestUtil.AssertArgumentNullException(() => builder.AddRepeatedString(null));
     TestUtil.AssertArgumentNullException(() => builder.AddRepeatedBytes(null));
     TestUtil.AssertArgumentNullException(() => builder.AddRepeatedNestedMessage((TestAllTypes.Types.NestedMessage)null));
     TestUtil.AssertArgumentNullException(() => builder.AddRepeatedNestedMessage((TestAllTypes.Types.NestedMessage.Builder)null));
 }
Beispiel #28
0
        public void DynamicMessageParsing()
        {
            TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
            TestUtil.SetAllFields(builder);
            TestAllTypes message = builder.Build();

            ByteString rawBytes = message.ToByteString();

            IMessage message2 = DynamicMessage.ParseFrom(TestAllTypes.Descriptor, rawBytes);

            reflectionTester.AssertAllFieldsSetViaReflection(message2);
        }
        public void ReflectionClearOneof()
        {
            TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
            reflectionTester.SetAllFieldsViaReflection(builder);
            OneofDescriptor oneof = TestAllTypes.Descriptor.Oneofs[0];
            FieldDescriptor field = TestAllTypes.Descriptor.FindFieldByName("oneof_bytes");

            Assert.IsTrue(builder.HasOneof(oneof));
            Assert.IsTrue(builder.HasField(field));
            builder.ClearOneof(oneof);
            Assert.IsFalse(builder.HasOneof(oneof));
            Assert.IsFalse(builder.HasField(field));
        }
        public void SettingRepeatedForeignMessageUsingBuilder()
        {
            TestAllTypes message = TestAllTypes.CreateBuilder()
                                   // Pass builder for foreign message instance.
                                   .AddRepeatedForeignMessage(ForeignMessage.CreateBuilder().SetC(456))
                                   .Build();
            TestAllTypes expectedMessage = TestAllTypes.CreateBuilder()
                                           // Create expected version passing foreign message instance explicitly.
                                           .AddRepeatedForeignMessage(ForeignMessage.CreateBuilder().SetC(456).Build())
                                           .Build();

            Assert.AreEqual(expectedMessage, message);
        }