Merge() public static method

public static Merge ( TextReader reader, ExtensionRegistry registry, IBuilder builder ) : void
reader TextReader
registry ExtensionRegistry
builder IBuilder
return void
 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 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());
        }
Example #3
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);
        }
 public void ParseExtensions()
 {
     TestAllExtensions.Builder builder = TestAllExtensions.CreateBuilder();
     TextFormat.Merge(AllExtensionsSetText,
                      TestUtil.CreateExtensionRegistry(),
                      builder);
     TestUtil.AssertAllExtensionsSet(builder.Build());
 }
 public void Parse()
 {
     TestUtil.TestInMultipleCultures(() => {
         TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
         TextFormat.Merge(AllFieldsSetText, builder);
         TestUtil.AssertAllFieldsSet(builder.Build());
     });
 }
        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);
     }
 }
 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);
 }
Example #9
0
        public void TestCompareStringValues()
        {
            TestInteropPersonLite person = TestInteropPersonLite.CreateBuilder()
                                           .SetId(123)
                                           .SetName("abc")
                                           .SetEmail("*****@*****.**")
                                           .AddRangeCodes(new[] { 1, 2, 3 })
                                           .AddPhone(TestInteropPersonLite.Types.PhoneNumber.CreateBuilder().SetNumber("555-1234").Build())
                                           .AddPhone(
                TestInteropPersonLite.Types.PhoneNumber.CreateBuilder().SetNumber(
                    System.Text.Encoding.UTF8.GetString(AllBytes.ToByteArray(), 0, AllBytes.Length)).Build())
                                           .AddAddresses(
                TestInteropPersonLite.Types.Addresses.CreateBuilder().SetAddress("123 Seseme").SetCity("Wonderland")
                .SetState("NA").SetZip(12345).Build())
                                           .SetExtension(UnittestExtrasLite.EmployeeIdLite,
                                                         TestInteropEmployeeIdLite.CreateBuilder().SetNumber("123").Build())
                                           .Build();

            Assert.IsTrue(person.IsInitialized);

            ExtensionRegistry registry = ExtensionRegistry.CreateInstance();

            UnittestExtrasFull.RegisterAllExtensions(registry);

            TestInteropPerson copy = TestInteropPerson.ParseFrom(person.ToByteArray(), registry);

            Assert.AreEqual(person.ToByteArray(), copy.ToByteArray());

            TestInteropPerson.Builder copyBuilder = TestInteropPerson.CreateBuilder();
            TextFormat.Merge(
                person.ToString().Replace("[protobuf_unittest_extra.employee_id_lite]",
                                          "[protobuf_unittest_extra.employee_id]"), registry, copyBuilder);

            copy = copyBuilder.Build();
            Assert.AreEqual(person.ToByteArray(), copy.ToByteArray());

            string liteText = person.ToString().TrimEnd().Replace("\r", "");
            string fullText = copy.ToString().TrimEnd().Replace("\r", "");

            //map the extension type
            liteText = liteText.Replace("[protobuf_unittest_extra.employee_id_lite]",
                                        "[protobuf_unittest_extra.employee_id]");
            //lite version does not indent
            while (fullText.IndexOf("\n ", StringComparison.Ordinal) >= 0)
            {
                fullText = fullText.Replace("\n ", "\n");
            }

            Assert.AreEqual(fullText, liteText);
        }
        public void ParseMessageSet()
        {
            ExtensionRegistry extensionRegistry = ExtensionRegistry.CreateInstance();

            extensionRegistry.Add(TestMessageSetExtension1.MessageSetExtension);
            extensionRegistry.Add(TestMessageSetExtension2.MessageSetExtension);

            TestMessageSet.Builder builder = TestMessageSet.CreateBuilder();
            TextFormat.Merge(MessageSetText, extensionRegistry, builder);
            TestMessageSet messageSet = builder.Build();

            Assert.IsTrue(messageSet.HasExtension(TestMessageSetExtension1.MessageSetExtension));
            Assert.AreEqual(123, messageSet.GetExtension(TestMessageSetExtension1.MessageSetExtension).I);
            Assert.IsTrue(messageSet.HasExtension(TestMessageSetExtension2.MessageSetExtension));
            Assert.AreEqual("foo", messageSet.GetExtension(TestMessageSetExtension2.MessageSetExtension).Str);
        }
        public void ParseLongString()
        {
            string longText =
                "123456789012345678901234567890123456789012345678901234567890" +
                "123456789012345678901234567890123456789012345678901234567890" +
                "123456789012345678901234567890123456789012345678901234567890" +
                "123456789012345678901234567890123456789012345678901234567890" +
                "123456789012345678901234567890123456789012345678901234567890" +
                "123456789012345678901234567890123456789012345678901234567890" +
                "123456789012345678901234567890123456789012345678901234567890" +
                "123456789012345678901234567890123456789012345678901234567890" +
                "123456789012345678901234567890123456789012345678901234567890" +
                "123456789012345678901234567890123456789012345678901234567890" +
                "123456789012345678901234567890123456789012345678901234567890" +
                "123456789012345678901234567890123456789012345678901234567890" +
                "123456789012345678901234567890123456789012345678901234567890" +
                "123456789012345678901234567890123456789012345678901234567890" +
                "123456789012345678901234567890123456789012345678901234567890" +
                "123456789012345678901234567890123456789012345678901234567890";

            TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
            TextFormat.Merge("optional_string: \"" + longText + "\"", builder);
            Assert.AreEqual(longText, builder.OptionalString);
        }
 public void ParseNumericEnum()
 {
     TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
     TextFormat.Merge("optional_nested_enum: 2", builder);
     Assert.AreEqual(TestAllTypes.Types.NestedEnum.BAR, builder.OptionalNestedEnum);
 }
 public void ParseReader()
 {
     TestAllTypes.Builder builder = TestAllTypes.CreateBuilder();
     TextFormat.Merge(new StringReader(AllFieldsSetText), builder);
     TestUtil.AssertAllFieldsSet(builder.Build());
 }