public void PrintMessageSet()
        {
            TestMessageSet messageSet =
                TestMessageSet.CreateBuilder()
                .SetExtension(
                    TestMessageSetExtension1.MessageSetExtension,
                    TestMessageSetExtension1.CreateBuilder().SetI(123).Build())
                .SetExtension(
                    TestMessageSetExtension2.MessageSetExtension,
                    TestMessageSetExtension2.CreateBuilder().SetStr("foo").Build())
                .Build();

            Assert.AreEqual(MessageSetText, messageSet.ToString());
        }
        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);
        }
Esempio n. 3
0
        public void SerializeMessageSet()
        {
            // Set up a TestMessageSet with two known messages and an unknown one.
            TestMessageSet messageSet =
                TestMessageSet.CreateBuilder()
                .SetExtension(
                    TestMessageSetExtension1.MessageSetExtension,
                    TestMessageSetExtension1.CreateBuilder().SetI(123).Build())
                .SetExtension(
                    TestMessageSetExtension2.MessageSetExtension,
                    TestMessageSetExtension2.CreateBuilder().SetStr("foo").Build())
                .SetUnknownFields(
                    UnknownFieldSet.CreateBuilder()
                    .AddField(UnknownTypeId,
                              UnknownField.CreateBuilder()
                              .AddLengthDelimited(ByteString.CopyFromUtf8("bar"))
                              .Build())
                    .Build())
                .Build();

            ByteString data = messageSet.ToByteString();

            // Parse back using RawMessageSet and check the contents.
            RawMessageSet raw = RawMessageSet.ParseFrom(data);

            Assert.AreEqual(0, raw.UnknownFields.FieldDictionary.Count);

            Assert.AreEqual(3, raw.ItemCount);
            Assert.AreEqual(TypeId1, raw.ItemList[0].TypeId);
            Assert.AreEqual(TypeId2, raw.ItemList[1].TypeId);
            Assert.AreEqual(UnknownTypeId, raw.ItemList[2].TypeId);

            TestMessageSetExtension1 message1 = TestMessageSetExtension1.ParseFrom(raw.GetItem(0).Message.ToByteArray());

            Assert.AreEqual(123, message1.I);

            TestMessageSetExtension2 message2 = TestMessageSetExtension2.ParseFrom(raw.GetItem(1).Message.ToByteArray());

            Assert.AreEqual("foo", message2.Str);

            Assert.AreEqual("bar", raw.GetItem(2).Message.ToStringUtf8());
        }