Ejemplo n.º 1
0
        public void TestRestoreFromOtherType()
        {
            TestInteropPerson person = TestInteropPerson.CreateBuilder()
                                       .SetId(123)
                                       .SetName("abc")
                                       .SetEmail("*****@*****.**")
                                       .AddRangeCodes(new[] { 1, 2, 3 })
                                       .AddPhone(TestInteropPerson.Types.PhoneNumber.CreateBuilder().SetNumber("555-1234").Build())
                                       .AddPhone(TestInteropPerson.Types.PhoneNumber.CreateBuilder().SetNumber("555-5678").Build())
                                       .AddAddresses(
                TestInteropPerson.Types.Addresses.CreateBuilder().SetAddress("123 Seseme").SetCity("Wonderland").
                SetState("NA").SetZip(12345).Build())
                                       .SetExtension(UnittestExtrasFull.EmployeeId,
                                                     TestInteropEmployeeId.CreateBuilder().SetNumber("123").Build())
                                       .Build();

            Assert.IsTrue(person.IsInitialized);

            TestEmptyMessage temp = TestEmptyMessage.ParseFrom(person.ToByteArray());

            Assert.AreEqual(7, temp.UnknownFields.FieldDictionary.Count);
            temp = temp.ToBuilder().Build();
            Assert.AreEqual(7, temp.UnknownFields.FieldDictionary.Count);

            ExtensionRegistry registry = ExtensionRegistry.CreateInstance();

            UnittestExtrasFull.RegisterAllExtensions(registry);

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

            Assert.AreEqual(person, copy);
            Assert.AreEqual(person.ToByteArray(), copy.ToByteArray());
        }
Ejemplo n.º 2
0
        public void TestConvertFromFullComplete()
        {
            TestInteropPerson person = TestInteropPerson.CreateBuilder()
                                       .SetId(123)
                                       .SetName("abc")
                                       .SetEmail("*****@*****.**")
                                       .AddRangeCodes(new[] { 1, 2, 3 })
                                       .AddPhone(TestInteropPerson.Types.PhoneNumber.CreateBuilder().SetNumber("555-1234").Build())
                                       .AddPhone(TestInteropPerson.Types.PhoneNumber.CreateBuilder().SetNumber("555-5678").Build())
                                       .AddAddresses(
                TestInteropPerson.Types.Addresses.CreateBuilder().SetAddress("123 Seseme").SetCity("Wonderland").
                SetState("NA").SetZip(12345).Build())
                                       .SetExtension(UnittestExtrasFull.EmployeeId,
                                                     TestInteropEmployeeId.CreateBuilder().SetNumber("123").Build())
                                       .Build();

            Assert.IsTrue(person.IsInitialized);

            ExtensionRegistry registry = ExtensionRegistry.CreateInstance();

            UnittestExtrasLite.RegisterAllExtensions(registry);
            byte[] fullBytes = person.ToByteArray();

            TestInteropPersonLite copy = TestInteropPersonLite.ParseFrom(fullBytes, registry);

            byte[] liteBytes = copy.ToByteArray();

            Assert.AreEqual(fullBytes, liteBytes);
        }
Ejemplo n.º 3
0
        public void TestIBuilderLiteWeakMergeFromByteStringExtensions()
        {
            TestAllExtensionsLite copy,
                                  msg = TestAllExtensionsLite.CreateBuilder()
                                        .SetExtension(UnitTestLiteProtoFile.OptionalStringExtensionLite,
                                                      "Should be merged.").Build();

            copy = TestAllExtensionsLite.DefaultInstance;
            Assert.AreNotEqual(msg.ToByteArray(), copy.ToByteArray());

            copy =
                (TestAllExtensionsLite)
                ((IBuilderLite)copy.ToBuilder()).WeakMergeFrom(msg.ToByteString(), ExtensionRegistry.Empty).WeakBuild();
            Assert.AreNotEqual(msg.ToByteArray(), copy.ToByteArray());

            ExtensionRegistry registry = ExtensionRegistry.CreateInstance();

            UnitTestLiteProtoFile.RegisterAllExtensions(registry);

            copy =
                (TestAllExtensionsLite)
                ((IBuilderLite)copy.ToBuilder()).WeakMergeFrom(msg.ToByteString(), registry).WeakBuild();
            TestUtil.AssertBytesEqual(msg.ToByteArray(), copy.ToByteArray());
            Assert.AreEqual("Should be merged.", copy.GetExtension(UnitTestLiteProtoFile.OptionalStringExtensionLite));
        }
Ejemplo n.º 4
0
        public void TestConvertFromLiteComplete()
        {
            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("555-5678").Build())
                                           .AddAddresses(
                TestInteropPersonLite.Types.Addresses.CreateBuilder().SetAddress("123 Seseme").SetCity("Wonderland")
                .SetState("NA").SetZip(12345).Build())
                                           .SetExtension(UnitTestExtrasLiteProtoFile.EmployeeIdLite,
                                                         TestInteropEmployeeIdLite.CreateBuilder().SetNumber("123").Build())
                                           .Build();

            Assert.IsTrue(person.IsInitialized);

            ExtensionRegistry registry = ExtensionRegistry.CreateInstance();

            UnitTestExtrasFullProtoFile.RegisterAllExtensions(registry);

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

            TestUtil.AssertBytesEqual(person.ToByteArray(), copy.ToByteArray());
        }
        public void PackedParsingOfUnpackedInputExtensions()
        {
            byte[]            bytes    = TestUnpackedTypes.ParseFrom(TestUtil.GetPackedSet().ToByteArray()).ToByteArray();
            ExtensionRegistry registry = ExtensionRegistry.CreateInstance();

            UnitTestProtoFile.RegisterAllExtensions(registry);
            TestPackedExtensions message = TestPackedExtensions.ParseFrom(bytes, registry);

            TestUtil.AssertPackedExtensionsSet(message);
        }
Ejemplo n.º 6
0
        public void ParseMessageSet()
        {
            ExtensionRegistry extensionRegistry = ExtensionRegistry.CreateInstance();

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

            // Set up a RawMessageSet with two known messages and an unknown one.
            RawMessageSet raw =
                RawMessageSet.CreateBuilder()
                .AddItem(
                    RawMessageSet.Types.Item.CreateBuilder()
                    .SetTypeId(TypeId1)
                    .SetMessage(
                        TestMessageSetExtension1.CreateBuilder()
                        .SetI(123)
                        .Build().ToByteString())
                    .Build())
                .AddItem(
                    RawMessageSet.Types.Item.CreateBuilder()
                    .SetTypeId(TypeId2)
                    .SetMessage(
                        TestMessageSetExtension2.CreateBuilder()
                        .SetStr("foo")
                        .Build().ToByteString())
                    .Build())
                .AddItem(
                    RawMessageSet.Types.Item.CreateBuilder()
                    .SetTypeId(UnknownTypeId)
                    .SetMessage(ByteString.CopyFromUtf8("bar"))
                    .Build())
                .Build();

            ByteString data = raw.ToByteString();

            // Parse as a TestMessageSet and check the contents.
            TestMessageSet messageSet =
                TestMessageSet.ParseFrom(data, extensionRegistry);

            Assert.AreEqual(123, messageSet.GetExtension(TestMessageSetExtension1.MessageSetExtension).I);
            Assert.AreEqual("foo", messageSet.GetExtension(TestMessageSetExtension2.MessageSetExtension).Str);

            // Check for unknown field with type LENGTH_DELIMITED,
            //   number UNKNOWN_TYPE_ID, and contents "bar".
            UnknownFieldSet unknownFields = messageSet.UnknownFields;

            Assert.AreEqual(1, unknownFields.FieldDictionary.Count);
            Assert.IsTrue(unknownFields.HasField(UnknownTypeId));

            UnknownField field = unknownFields[UnknownTypeId];

            Assert.AreEqual(1, field.LengthDelimitedList.Count);
            Assert.AreEqual("bar", field.LengthDelimitedList[0].ToStringUtf8());
        }
Ejemplo n.º 7
0
        public void ExtensionWriterTestMessages()
        {
            TestAllExtensionsLite.Builder b = TestAllExtensionsLite.CreateBuilder().SetExtension(
                UnitTestLiteProtoFile.OptionalForeignMessageExtensionLite, ForeignMessageLite.CreateBuilder().SetC(123).Build());
            TestAllExtensionsLite copy, msg = b.Build();

            ExtensionRegistry registry = ExtensionRegistry.CreateInstance();

            UnitTestLiteProtoFile.RegisterAllExtensions(registry);

            copy = TestAllExtensionsLite.ParseFrom(msg.ToByteArray(), registry);
            Assert.AreEqual(msg.ToByteArray(), copy.ToByteArray());
        }
Ejemplo n.º 8
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);
        }
Ejemplo n.º 9
0
        public void ExtensionWriterTestPacked()
        {
            TestPackedExtensionsLite msg = BuildPackedExtensions();

            ExtensionRegistry registry = ExtensionRegistry.CreateInstance();

            UnittestLite.RegisterAllExtensions(registry);

            TestPackedExtensionsLite.Builder copyBuilder =
                TestPackedExtensionsLite.CreateBuilder().MergeFrom(msg.ToByteArray(), registry);
            TestPackedExtensionsLite copy = copyBuilder.Build();

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

            AssertPackedExtensions(copy);
        }
Ejemplo n.º 10
0
        public void TestUnpackedFromPackedInput()
        {
            byte[] packedData = BuildPackedExtensions().ToByteArray();

            TestUnpackedTypesLite unpacked = TestUnpackedTypesLite.ParseFrom(packedData);
            TestPackedTypesLite   packed   = TestPackedTypesLite.ParseFrom(unpacked.ToByteArray());

            Assert.AreEqual(packedData, packed.ToByteArray());

            unpacked = TestUnpackedTypesLite.ParseFrom(packed.ToByteArray());

            ExtensionRegistry registry = ExtensionRegistry.CreateInstance();

            UnittestLite.RegisterAllExtensions(registry);
            AssertPackedExtensions(TestPackedExtensionsLite.ParseFrom(unpacked.ToByteArray(), registry));
        }
Ejemplo n.º 11
0
        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);
        }
Ejemplo n.º 12
0
        public void TestRecoverMissingExtensions()
        {
            const int optionalInt32 = 12345678;

            TestAllExtensions.Builder builder = TestAllExtensions.CreateBuilder();
            builder.SetExtension(Unittest.OptionalInt32Extension, optionalInt32);
            builder.AddExtension(Unittest.RepeatedDoubleExtension, 1.1);
            builder.AddExtension(Unittest.RepeatedDoubleExtension, 1.2);
            builder.AddExtension(Unittest.RepeatedDoubleExtension, 1.3);
            TestAllExtensions msg = builder.Build();

            Assert.IsTrue(msg.HasExtension(Unittest.OptionalInt32Extension));
            Assert.AreEqual(3, msg.GetExtensionCount(Unittest.RepeatedDoubleExtension));

            byte[]            bits = msg.ToByteArray();
            TestAllExtensions copy = TestAllExtensions.ParseFrom(bits);

            Assert.IsFalse(copy.HasExtension(Unittest.OptionalInt32Extension));
            Assert.AreEqual(0, copy.GetExtensionCount(Unittest.RepeatedDoubleExtension));
            Assert.AreNotEqual(msg, copy);

            //Even though copy does not understand the typees they serialize correctly
            byte[] copybits = copy.ToByteArray();
            Assert.AreEqual(bits, copybits);

            ExtensionRegistry registry = ExtensionRegistry.CreateInstance();

            Unittest.RegisterAllExtensions(registry);

            //Now we can take those copy bits and restore the full message with extensions
            copy = TestAllExtensions.ParseFrom(copybits, registry);
            Assert.IsTrue(copy.HasExtension(Unittest.OptionalInt32Extension));
            Assert.AreEqual(3, copy.GetExtensionCount(Unittest.RepeatedDoubleExtension));

            Assert.AreEqual(msg, copy);
            Assert.AreEqual(bits, copy.ToByteArray());

            //If we modify the object this should all continue to work as before
            copybits = copy.ToBuilder().Build().ToByteArray();
            Assert.AreEqual(bits, copybits);

            //If we replace extension the object this should all continue to work as before
            copybits = copy.ToBuilder()
                       .SetExtension(Unittest.OptionalInt32Extension, optionalInt32)
                       .Build().ToByteArray();
            Assert.AreEqual(bits, copybits);
        }
        public void TestBuilderWithExtensions()
        {
            TestXmlMessage.Builder builder = TestXmlMessage.CreateBuilder()
                                             .SetValid(true)
                                             .SetText("text")
                                             .AddTextlines("a")
                                             .AddTextlines("b")
                                             .AddTextlines("c")
                                             .SetNumber(0x1010101010)
                                             .AddNumbers(1)
                                             .AddNumbers(2)
                                             .AddNumbers(3)
                                             .SetChild(TestXmlChild.CreateBuilder()
                                                       .AddOptions(EnumOptions.ONE)
                                                       .SetBinary(ByteString.CopyFrom(new byte[1])))
                                             .AddChildren(TestXmlMessage.Types.Children.CreateBuilder()
                                                          .AddOptions(EnumOptions.TWO)
                                                          .SetBinary(ByteString.CopyFrom(new byte[2])))
                                             .AddChildren(TestXmlMessage.Types.Children.CreateBuilder()
                                                          .AddOptions(EnumOptions.THREE)
                                                          .SetBinary(ByteString.CopyFrom(new byte[3])))
                                             .SetExtension(UnitTestXmlSerializerTestProtoFile.ExtensionText, " extension text value ! ")
                                             .SetExtension(UnitTestXmlSerializerTestProtoFile.ExtensionMessage, new TestXmlExtension.Builder().SetNumber(42).Build())
                                             .AddExtension(UnitTestXmlSerializerTestProtoFile.ExtensionNumber, 100)
                                             .AddExtension(UnitTestXmlSerializerTestProtoFile.ExtensionNumber, 101)
                                             .AddExtension(UnitTestXmlSerializerTestProtoFile.ExtensionNumber, 102)
                                             .SetExtension(UnitTestXmlSerializerTestProtoFile.ExtensionEnum, EnumOptions.ONE)
            ;

            ExtensionRegistry registry = ExtensionRegistry.CreateInstance();

            UnitTestXmlSerializerTestProtoFile.RegisterAllExtensions(registry);

            MemoryStream ms = new MemoryStream();

            new BinaryFormatter().Serialize(ms, builder);

            ms.Position = 0;
            //you need to provide the extension registry as context to the serializer
            BinaryFormatter bff = new BinaryFormatter(null, new StreamingContext(StreamingContextStates.All, registry));

            TestXmlMessage.Builder copy = (TestXmlMessage.Builder)bff.Deserialize(ms);

            // And all extensions will be defined.
            Assert.AreEqual(builder.Build(), copy.Build());
        }
Ejemplo n.º 14
0
        public void ParseExtensions()
        {
            // TestAllTypes and TestAllExtensions should have compatible wire formats,
            // so if we serealize a TestAllTypes then parse it as TestAllExtensions
            // it should work.

            TestAllTypes message  = TestUtil.GetAllSet();
            ByteString   rawBytes = message.ToByteString();

            ExtensionRegistry registry = ExtensionRegistry.CreateInstance();

            TestUtil.RegisterAllExtensions(registry);
            registry = registry.AsReadOnly();

            TestAllExtensions message2 = TestAllExtensions.ParseFrom(rawBytes, registry);

            TestUtil.AssertAllExtensionsSet(message2);
        }
Ejemplo n.º 15
0
        public void TestUnpackedAndPackedExtensions()
        {
            TestPackedExtensionsLite original = BuildPackedExtensions();

            AssertPackedExtensions(original);

            ExtensionRegistry registry = ExtensionRegistry.CreateInstance();

            UnittestLite.RegisterAllExtensions(registry);
            UnittestExtrasLite.RegisterAllExtensions(registry);

            TestUnpackedExtensionsLite unpacked = TestUnpackedExtensionsLite.ParseFrom(original.ToByteArray(), registry);

            TestPackedExtensionsLite packed = TestPackedExtensionsLite.ParseFrom(unpacked.ToByteArray(), registry);

            Assert.AreEqual(original, packed);
            Assert.AreEqual(original.ToByteArray(), packed.ToByteArray());
            AssertPackedExtensions(packed);
        }
Ejemplo n.º 16
0
        public void ExtensionWriterTestSetExtensionLists()
        {
            TestAllExtensionsLite msg, copy;

            TestAllExtensionsLite.Builder builder = TestAllExtensionsLite.CreateBuilder()
                                                    .SetExtension(UnitTestLiteProtoFile.RepeatedBoolExtensionLite, new[] { true, false })
                                                    .SetExtension(UnitTestLiteProtoFile.RepeatedCordExtensionLite, new[] { "123", "456" })
                                                    .SetExtension(UnitTestLiteProtoFile.RepeatedForeignEnumExtensionLite, new[] { ForeignEnumLite.FOREIGN_LITE_BAZ, ForeignEnumLite.FOREIGN_LITE_FOO })
            ;

            msg = builder.Build();
            ExtensionRegistry registry = ExtensionRegistry.CreateInstance();

            UnitTestLiteProtoFile.RegisterAllExtensions(registry);

            copy = TestAllExtensionsLite.ParseFrom(msg.ToByteArray(), registry);
            Assert.AreEqual(msg.ToByteArray(), copy.ToByteArray());

            Assert.AreEqual(ForeignEnumLite.FOREIGN_LITE_FOO, copy.GetExtension(UnitTestLiteProtoFile.RepeatedForeignEnumExtensionLite, 1));
        }
Ejemplo n.º 17
0
        public void TestXmlWithExtensionEnum()
        {
            TestXmlMessage message = TestXmlMessage.CreateBuilder()
                                     .SetExtension(UnittestExtrasXmltest.ExtensionEnum, EnumOptions.ONE)
                                     .Build();

            StringWriter sw = new StringWriter();

            XmlFormatWriter.CreateInstance(sw).WriteMessage("root", message);

            string xml = sw.ToString();

            ExtensionRegistry registry = ExtensionRegistry.CreateInstance();

            UnittestExtrasXmltest.RegisterAllExtensions(registry);

            XmlFormatReader rdr  = XmlFormatReader.CreateInstance(xml);
            TestXmlMessage  copy = rdr.Merge(TestXmlMessage.CreateBuilder(), registry).Build();

            Assert.AreEqual(message, copy);
        }
        public void TestXmlWithExtensionMessage()
        {
            TestXmlMessage message = TestXmlMessage.CreateBuilder()
                                     .SetExtension(UnitTestXmlSerializerTestProtoFile.ExtensionMessage,
                                                   new TestXmlExtension.Builder().SetNumber(42).Build()).Build();

            StringWriter sw = new StringWriter();

            XmlFormatWriter.CreateInstance(sw).WriteMessage("root", message);

            string xml = sw.ToString();

            ExtensionRegistry registry = ExtensionRegistry.CreateInstance();

            UnitTestXmlSerializerTestProtoFile.RegisterAllExtensions(registry);

            XmlFormatReader rdr  = XmlFormatReader.CreateInstance(xml);
            TestXmlMessage  copy = rdr.Merge(TestXmlMessage.CreateBuilder(), registry).Build();

            Assert.AreEqual(message, copy);
        }
Ejemplo n.º 19
0
        public void TestBuilderLiteMergeFromStreamExtensions()
        {
            TestAllExtensionsLite copy, msg = TestAllExtensionsLite.CreateBuilder()
                                              .SetExtension(UnitTestLiteProtoFile.OptionalStringExtensionLite, "Should be merged.").Build();

            copy = TestAllExtensionsLite.DefaultInstance;
            Assert.AreNotEqual(msg.ToByteArray(), copy.ToByteArray());

            Stream s = new MemoryStream();

            msg.WriteTo(s);
            s.Position = 0;

            ExtensionRegistry registry = ExtensionRegistry.CreateInstance();

            UnitTestLiteProtoFile.RegisterAllExtensions(registry);

            copy = copy.ToBuilder().MergeFrom(s, registry).Build();
            Assert.AreEqual(msg.ToByteArray(), copy.ToByteArray());
            Assert.AreEqual("Should be merged.", copy.GetExtension(UnitTestLiteProtoFile.OptionalStringExtensionLite));
        }
Ejemplo n.º 20
0
        protected void FormatterAssert <TMessage>(TMessage message, params string[] expecting) where TMessage : IMessageLite
        {
            StringWriter sw = new StringWriter();

            JsonFormatWriter.CreateInstance(sw).WriteMessage(message);

            Content = sw.ToString();

            ExtensionRegistry registry = ExtensionRegistry.CreateInstance();

            UnittestExtrasXmltest.RegisterAllExtensions(registry);

            IMessageLite copy =
                JsonFormatReader.CreateInstance(Content)
                .Merge(message.WeakCreateBuilderForType(), registry).WeakBuild();

            Assert.AreEqual(typeof(TMessage), copy.GetType());
            Assert.AreEqual(message, copy);
            foreach (string expect in expecting)
            {
                Assert.IsTrue(Content.IndexOf(expect) >= 0);
            }
        }
Ejemplo n.º 21
0
        public void ExtensionWriterTestPacked()
        {
            TestPackedExtensionsLite.Builder builder = TestPackedExtensionsLite.CreateBuilder()
                                                       .AddExtension(UnitTestLiteProtoFile.PackedBoolExtensionLite, true)
                                                       .AddExtension(UnitTestLiteProtoFile.PackedDoubleExtensionLite, 123)
                                                       .AddExtension(UnitTestLiteProtoFile.PackedFixed32ExtensionLite, 123u)
                                                       .AddExtension(UnitTestLiteProtoFile.PackedFixed64ExtensionLite, 123u)
                                                       .AddExtension(UnitTestLiteProtoFile.PackedFloatExtensionLite, 123)
                                                       .AddExtension(UnitTestLiteProtoFile.PackedInt32ExtensionLite, 123)
                                                       .AddExtension(UnitTestLiteProtoFile.PackedInt64ExtensionLite, 123)
                                                       .AddExtension(UnitTestLiteProtoFile.PackedSfixed32ExtensionLite, 123)
                                                       .AddExtension(UnitTestLiteProtoFile.PackedSfixed64ExtensionLite, 123)
                                                       .AddExtension(UnitTestLiteProtoFile.PackedSint32ExtensionLite, 123)
                                                       .AddExtension(UnitTestLiteProtoFile.PackedSint64ExtensionLite, 123)
                                                       .AddExtension(UnitTestLiteProtoFile.PackedUint32ExtensionLite, 123u)
                                                       .AddExtension(UnitTestLiteProtoFile.PackedUint64ExtensionLite, 123u)
                                                       .AddExtension(UnitTestLiteProtoFile.PackedBoolExtensionLite, true)
                                                       .AddExtension(UnitTestLiteProtoFile.PackedDoubleExtensionLite, 123)
                                                       .AddExtension(UnitTestLiteProtoFile.PackedFixed32ExtensionLite, 123u)
                                                       .AddExtension(UnitTestLiteProtoFile.PackedFixed64ExtensionLite, 123u)
                                                       .AddExtension(UnitTestLiteProtoFile.PackedFloatExtensionLite, 123)
                                                       .AddExtension(UnitTestLiteProtoFile.PackedInt32ExtensionLite, 123)
                                                       .AddExtension(UnitTestLiteProtoFile.PackedInt64ExtensionLite, 123)
                                                       .AddExtension(UnitTestLiteProtoFile.PackedSfixed32ExtensionLite, 123)
                                                       .AddExtension(UnitTestLiteProtoFile.PackedSfixed64ExtensionLite, 123)
                                                       .AddExtension(UnitTestLiteProtoFile.PackedSint32ExtensionLite, 123)
                                                       .AddExtension(UnitTestLiteProtoFile.PackedSint64ExtensionLite, 123)
                                                       .AddExtension(UnitTestLiteProtoFile.PackedUint32ExtensionLite, 123u)
                                                       .AddExtension(UnitTestLiteProtoFile.PackedUint64ExtensionLite, 123u);

            TestPackedExtensionsLite msg = builder.Build();

            ExtensionRegistry registry = ExtensionRegistry.CreateInstance();

            UnitTestLiteProtoFile.RegisterAllExtensions(registry);

            TestPackedExtensionsLite.Builder copyBuilder = TestPackedExtensionsLite.CreateBuilder().MergeFrom(msg.ToByteArray(), registry);
            TestPackedExtensionsLite         copy        = copyBuilder.Build();

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

            Assert.AreEqual(true, copy.GetExtension(UnitTestLiteProtoFile.PackedBoolExtensionLite, 0));
            Assert.AreEqual(123, copy.GetExtension(UnitTestLiteProtoFile.PackedDoubleExtensionLite, 0));
            Assert.AreEqual(123u, copy.GetExtension(UnitTestLiteProtoFile.PackedFixed32ExtensionLite, 0));
            Assert.AreEqual(123u, copy.GetExtension(UnitTestLiteProtoFile.PackedFixed64ExtensionLite, 0));
            Assert.AreEqual(123, copy.GetExtension(UnitTestLiteProtoFile.PackedFloatExtensionLite, 0));
            Assert.AreEqual(123, copy.GetExtension(UnitTestLiteProtoFile.PackedInt32ExtensionLite, 0));
            Assert.AreEqual(123, copy.GetExtension(UnitTestLiteProtoFile.PackedInt64ExtensionLite, 0));
            Assert.AreEqual(123, copy.GetExtension(UnitTestLiteProtoFile.PackedSfixed32ExtensionLite, 0));
            Assert.AreEqual(123, copy.GetExtension(UnitTestLiteProtoFile.PackedSfixed64ExtensionLite, 0));
            Assert.AreEqual(123, copy.GetExtension(UnitTestLiteProtoFile.PackedSint32ExtensionLite, 0));
            Assert.AreEqual(123, copy.GetExtension(UnitTestLiteProtoFile.PackedSint64ExtensionLite, 0));
            Assert.AreEqual(123u, copy.GetExtension(UnitTestLiteProtoFile.PackedUint32ExtensionLite, 0));
            Assert.AreEqual(123u, copy.GetExtension(UnitTestLiteProtoFile.PackedUint64ExtensionLite, 0));

            Assert.AreEqual(true, copy.GetExtension(UnitTestLiteProtoFile.PackedBoolExtensionLite, 1));
            Assert.AreEqual(123, copy.GetExtension(UnitTestLiteProtoFile.PackedDoubleExtensionLite, 1));
            Assert.AreEqual(123u, copy.GetExtension(UnitTestLiteProtoFile.PackedFixed32ExtensionLite, 1));
            Assert.AreEqual(123u, copy.GetExtension(UnitTestLiteProtoFile.PackedFixed64ExtensionLite, 1));
            Assert.AreEqual(123, copy.GetExtension(UnitTestLiteProtoFile.PackedFloatExtensionLite, 1));
            Assert.AreEqual(123, copy.GetExtension(UnitTestLiteProtoFile.PackedInt32ExtensionLite, 1));
            Assert.AreEqual(123, copy.GetExtension(UnitTestLiteProtoFile.PackedInt64ExtensionLite, 1));
            Assert.AreEqual(123, copy.GetExtension(UnitTestLiteProtoFile.PackedSfixed32ExtensionLite, 1));
            Assert.AreEqual(123, copy.GetExtension(UnitTestLiteProtoFile.PackedSfixed64ExtensionLite, 1));
            Assert.AreEqual(123, copy.GetExtension(UnitTestLiteProtoFile.PackedSint32ExtensionLite, 1));
            Assert.AreEqual(123, copy.GetExtension(UnitTestLiteProtoFile.PackedSint64ExtensionLite, 1));
            Assert.AreEqual(123u, copy.GetExtension(UnitTestLiteProtoFile.PackedUint32ExtensionLite, 1));
            Assert.AreEqual(123u, copy.GetExtension(UnitTestLiteProtoFile.PackedUint64ExtensionLite, 1));
        }
Ejemplo n.º 22
0
 public TBuilder MergeDelimitedFrom(Stream input)
 {
     return(MergeDelimitedFrom(input, ExtensionRegistry.CreateInstance()));
 }
Ejemplo n.º 23
0
 public TBuilder MergeFrom(ByteString data)
 {
     return(MergeFrom(data, ExtensionRegistry.CreateInstance()));
 }
Ejemplo n.º 24
0
        public void ExtensionWriterTest()
        {
            TestAllExtensions.Builder builder = TestAllExtensions.CreateBuilder()
                                                .SetExtension(Unittest.DefaultBoolExtension, true)
                                                .SetExtension(Unittest.DefaultBytesExtension, ByteString.CopyFromUtf8("123"))
                                                .SetExtension(Unittest.DefaultCordExtension, "123")
                                                .SetExtension(Unittest.DefaultDoubleExtension, 123)
                                                .SetExtension(Unittest.DefaultFixed32Extension, 123u)
                                                .SetExtension(Unittest.DefaultFixed64Extension, 123u)
                                                .SetExtension(Unittest.DefaultFloatExtension, 123)
                                                .SetExtension(Unittest.DefaultForeignEnumExtension, ForeignEnum.FOREIGN_BAZ)
                                                .SetExtension(Unittest.DefaultImportEnumExtension, ImportEnum.IMPORT_BAZ)
                                                .SetExtension(Unittest.DefaultInt32Extension, 123)
                                                .SetExtension(Unittest.DefaultInt64Extension, 123)
                                                .SetExtension(Unittest.DefaultNestedEnumExtension, TestAllTypes.Types.NestedEnum.FOO)
                                                .SetExtension(Unittest.DefaultSfixed32Extension, 123)
                                                .SetExtension(Unittest.DefaultSfixed64Extension, 123)
                                                .SetExtension(Unittest.DefaultSint32Extension, 123)
                                                .SetExtension(Unittest.DefaultSint64Extension, 123)
                                                .SetExtension(Unittest.DefaultStringExtension, "123")
                                                .SetExtension(Unittest.DefaultStringPieceExtension, "123")
                                                .SetExtension(Unittest.DefaultUint32Extension, 123u)
                                                .SetExtension(Unittest.DefaultUint64Extension, 123u)
                                                //Optional
                                                .SetExtension(Unittest.OptionalBoolExtension, true)
                                                .SetExtension(Unittest.OptionalBytesExtension, ByteString.CopyFromUtf8("123"))
                                                .SetExtension(Unittest.OptionalCordExtension, "123")
                                                .SetExtension(Unittest.OptionalDoubleExtension, 123)
                                                .SetExtension(Unittest.OptionalFixed32Extension, 123u)
                                                .SetExtension(Unittest.OptionalFixed64Extension, 123u)
                                                .SetExtension(Unittest.OptionalFloatExtension, 123)
                                                .SetExtension(Unittest.OptionalForeignEnumExtension, ForeignEnum.FOREIGN_BAZ)
                                                .SetExtension(Unittest.OptionalImportEnumExtension, ImportEnum.IMPORT_BAZ)
                                                .SetExtension(Unittest.OptionalInt32Extension, 123)
                                                .SetExtension(Unittest.OptionalInt64Extension, 123)
                                                .SetExtension(Unittest.OptionalNestedEnumExtension, TestAllTypes.Types.NestedEnum.FOO)
                                                .SetExtension(Unittest.OptionalSfixed32Extension, 123)
                                                .SetExtension(Unittest.OptionalSfixed64Extension, 123)
                                                .SetExtension(Unittest.OptionalSint32Extension, 123)
                                                .SetExtension(Unittest.OptionalSint64Extension, 123)
                                                .SetExtension(Unittest.OptionalStringExtension, "123")
                                                .SetExtension(Unittest.OptionalStringPieceExtension, "123")
                                                .SetExtension(Unittest.OptionalUint32Extension, 123u)
                                                .SetExtension(Unittest.OptionalUint64Extension, 123u)
                                                //Repeated
                                                .AddExtension(Unittest.RepeatedBoolExtension, true)
                                                .AddExtension(Unittest.RepeatedBytesExtension, ByteString.CopyFromUtf8("123"))
                                                .AddExtension(Unittest.RepeatedCordExtension, "123")
                                                .AddExtension(Unittest.RepeatedDoubleExtension, 123)
                                                .AddExtension(Unittest.RepeatedFixed32Extension, 123u)
                                                .AddExtension(Unittest.RepeatedFixed64Extension, 123u)
                                                .AddExtension(Unittest.RepeatedFloatExtension, 123)
                                                .AddExtension(Unittest.RepeatedForeignEnumExtension, ForeignEnum.FOREIGN_BAZ)
                                                .AddExtension(Unittest.RepeatedImportEnumExtension, ImportEnum.IMPORT_BAZ)
                                                .AddExtension(Unittest.RepeatedInt32Extension, 123)
                                                .AddExtension(Unittest.RepeatedInt64Extension, 123)
                                                .AddExtension(Unittest.RepeatedNestedEnumExtension, TestAllTypes.Types.NestedEnum.FOO)
                                                .AddExtension(Unittest.RepeatedSfixed32Extension, 123)
                                                .AddExtension(Unittest.RepeatedSfixed64Extension, 123)
                                                .AddExtension(Unittest.RepeatedSint32Extension, 123)
                                                .AddExtension(Unittest.RepeatedSint64Extension, 123)
                                                .AddExtension(Unittest.RepeatedStringExtension, "123")
                                                .AddExtension(Unittest.RepeatedStringPieceExtension, "123")
                                                .AddExtension(Unittest.RepeatedUint32Extension, 123u)
                                                .AddExtension(Unittest.RepeatedUint64Extension, 123u)
            ;
            TestAllExtensions msg = builder.Build();

            ExtensionRegistry registry = ExtensionRegistry.CreateInstance();

            Unittest.RegisterAllExtensions(registry);

            TestAllExtensions.Builder copyBuilder = TestAllExtensions.CreateBuilder().MergeFrom(msg.ToByteArray(),
                                                                                                registry);
            TestAllExtensions copy = copyBuilder.Build();

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

            Assert.AreEqual(true, copy.GetExtension(Unittest.DefaultBoolExtension));
            Assert.AreEqual(ByteString.CopyFromUtf8("123"), copy.GetExtension(Unittest.DefaultBytesExtension));
            Assert.AreEqual("123", copy.GetExtension(Unittest.DefaultCordExtension));
            Assert.AreEqual(123, copy.GetExtension(Unittest.DefaultDoubleExtension));
            Assert.AreEqual(123u, copy.GetExtension(Unittest.DefaultFixed32Extension));
            Assert.AreEqual(123u, copy.GetExtension(Unittest.DefaultFixed64Extension));
            Assert.AreEqual(123, copy.GetExtension(Unittest.DefaultFloatExtension));
            Assert.AreEqual(ForeignEnum.FOREIGN_BAZ, copy.GetExtension(Unittest.DefaultForeignEnumExtension));
            Assert.AreEqual(ImportEnum.IMPORT_BAZ, copy.GetExtension(Unittest.DefaultImportEnumExtension));
            Assert.AreEqual(123, copy.GetExtension(Unittest.DefaultInt32Extension));
            Assert.AreEqual(123, copy.GetExtension(Unittest.DefaultInt64Extension));
            Assert.AreEqual(TestAllTypes.Types.NestedEnum.FOO,
                            copy.GetExtension(Unittest.DefaultNestedEnumExtension));
            Assert.AreEqual(123, copy.GetExtension(Unittest.DefaultSfixed32Extension));
            Assert.AreEqual(123, copy.GetExtension(Unittest.DefaultSfixed64Extension));
            Assert.AreEqual(123, copy.GetExtension(Unittest.DefaultSint32Extension));
            Assert.AreEqual(123, copy.GetExtension(Unittest.DefaultSint64Extension));
            Assert.AreEqual("123", copy.GetExtension(Unittest.DefaultStringExtension));
            Assert.AreEqual("123", copy.GetExtension(Unittest.DefaultStringPieceExtension));
            Assert.AreEqual(123u, copy.GetExtension(Unittest.DefaultUint32Extension));
            Assert.AreEqual(123u, copy.GetExtension(Unittest.DefaultUint64Extension));

            Assert.AreEqual(true, copy.GetExtension(Unittest.OptionalBoolExtension));
            Assert.AreEqual(ByteString.CopyFromUtf8("123"), copy.GetExtension(Unittest.OptionalBytesExtension));
            Assert.AreEqual("123", copy.GetExtension(Unittest.OptionalCordExtension));
            Assert.AreEqual(123, copy.GetExtension(Unittest.OptionalDoubleExtension));
            Assert.AreEqual(123u, copy.GetExtension(Unittest.OptionalFixed32Extension));
            Assert.AreEqual(123u, copy.GetExtension(Unittest.OptionalFixed64Extension));
            Assert.AreEqual(123, copy.GetExtension(Unittest.OptionalFloatExtension));
            Assert.AreEqual(ForeignEnum.FOREIGN_BAZ, copy.GetExtension(Unittest.OptionalForeignEnumExtension));
            Assert.AreEqual(ImportEnum.IMPORT_BAZ, copy.GetExtension(Unittest.OptionalImportEnumExtension));
            Assert.AreEqual(123, copy.GetExtension(Unittest.OptionalInt32Extension));
            Assert.AreEqual(123, copy.GetExtension(Unittest.OptionalInt64Extension));
            Assert.AreEqual(TestAllTypes.Types.NestedEnum.FOO,
                            copy.GetExtension(Unittest.OptionalNestedEnumExtension));
            Assert.AreEqual(123, copy.GetExtension(Unittest.OptionalSfixed32Extension));
            Assert.AreEqual(123, copy.GetExtension(Unittest.OptionalSfixed64Extension));
            Assert.AreEqual(123, copy.GetExtension(Unittest.OptionalSint32Extension));
            Assert.AreEqual(123, copy.GetExtension(Unittest.OptionalSint64Extension));
            Assert.AreEqual("123", copy.GetExtension(Unittest.OptionalStringExtension));
            Assert.AreEqual("123", copy.GetExtension(Unittest.OptionalStringPieceExtension));
            Assert.AreEqual(123u, copy.GetExtension(Unittest.OptionalUint32Extension));
            Assert.AreEqual(123u, copy.GetExtension(Unittest.OptionalUint64Extension));

            Assert.AreEqual(true, copy.GetExtension(Unittest.RepeatedBoolExtension, 0));
            Assert.AreEqual(ByteString.CopyFromUtf8("123"),
                            copy.GetExtension(Unittest.RepeatedBytesExtension, 0));
            Assert.AreEqual("123", copy.GetExtension(Unittest.RepeatedCordExtension, 0));
            Assert.AreEqual(123, copy.GetExtension(Unittest.RepeatedDoubleExtension, 0));
            Assert.AreEqual(123u, copy.GetExtension(Unittest.RepeatedFixed32Extension, 0));
            Assert.AreEqual(123u, copy.GetExtension(Unittest.RepeatedFixed64Extension, 0));
            Assert.AreEqual(123, copy.GetExtension(Unittest.RepeatedFloatExtension, 0));
            Assert.AreEqual(ForeignEnum.FOREIGN_BAZ,
                            copy.GetExtension(Unittest.RepeatedForeignEnumExtension, 0));
            Assert.AreEqual(ImportEnum.IMPORT_BAZ, copy.GetExtension(Unittest.RepeatedImportEnumExtension, 0));
            Assert.AreEqual(123, copy.GetExtension(Unittest.RepeatedInt32Extension, 0));
            Assert.AreEqual(123, copy.GetExtension(Unittest.RepeatedInt64Extension, 0));
            Assert.AreEqual(TestAllTypes.Types.NestedEnum.FOO,
                            copy.GetExtension(Unittest.RepeatedNestedEnumExtension, 0));
            Assert.AreEqual(123, copy.GetExtension(Unittest.RepeatedSfixed32Extension, 0));
            Assert.AreEqual(123, copy.GetExtension(Unittest.RepeatedSfixed64Extension, 0));
            Assert.AreEqual(123, copy.GetExtension(Unittest.RepeatedSint32Extension, 0));
            Assert.AreEqual(123, copy.GetExtension(Unittest.RepeatedSint64Extension, 0));
            Assert.AreEqual("123", copy.GetExtension(Unittest.RepeatedStringExtension, 0));
            Assert.AreEqual("123", copy.GetExtension(Unittest.RepeatedStringPieceExtension, 0));
            Assert.AreEqual(123u, copy.GetExtension(Unittest.RepeatedUint32Extension, 0));
            Assert.AreEqual(123u, copy.GetExtension(Unittest.RepeatedUint64Extension, 0));
        }
Ejemplo n.º 25
0
 public virtual TBuilder MergeFrom(ICodedInputStream input)
 {
     return(MergeFrom(input, ExtensionRegistry.CreateInstance()));
 }