Пример #1
0
        public void TestLiteExtensions()
        {
            // TODO(kenton):  Unlike other features of the lite library, extensions are
            //   implemented completely differently from the regular library.  We
            //   should probably test them more thoroughly.

            TestAllExtensionsLite message =
                TestAllExtensionsLite.CreateBuilder()
                .SetExtension(UnitTestLiteProtoFile.OptionalInt32ExtensionLite, 123)
                .AddExtension(UnitTestLiteProtoFile.RepeatedStringExtensionLite, "hello")
                .SetExtension(UnitTestLiteProtoFile.OptionalNestedEnumExtensionLite,
                              TestAllTypesLite.Types.NestedEnum.BAZ)
                .SetExtension(UnitTestLiteProtoFile.OptionalNestedMessageExtensionLite,
                              TestAllTypesLite.Types.NestedMessage.CreateBuilder().SetBb(7).Build())
                .Build();

            // Test copying a message, since coping extensions actually does use a
            // different code path between lite and regular libraries, and as of this
            // writing, parsing hasn't been implemented yet.
            TestAllExtensionsLite message2 = message.ToBuilder().Build();

            Assert.AreEqual(123, (int)message2.GetExtension(
                                UnitTestLiteProtoFile.OptionalInt32ExtensionLite));
            Assert.AreEqual(1, message2.GetExtensionCount(
                                UnitTestLiteProtoFile.RepeatedStringExtensionLite));
            Assert.AreEqual(1, message2.GetExtension(
                                UnitTestLiteProtoFile.RepeatedStringExtensionLite).Count);
            Assert.AreEqual("hello", message2.GetExtension(
                                UnitTestLiteProtoFile.RepeatedStringExtensionLite, 0));
            Assert.AreEqual(TestAllTypesLite.Types.NestedEnum.BAZ, message2.GetExtension(
                                UnitTestLiteProtoFile.OptionalNestedEnumExtensionLite));
            Assert.AreEqual(7, message2.GetExtension(
                                UnitTestLiteProtoFile.OptionalNestedMessageExtensionLite).Bb);
        }
Пример #2
0
        public void ExtensionWriterTest()
        {
            TestAllExtensionsLite.Builder builder = TestAllExtensionsLite.CreateBuilder()
                                                    .SetExtension(UnittestLite.DefaultBoolExtensionLite, true)
                                                    .SetExtension(UnittestLite.DefaultBytesExtensionLite, ByteString.CopyFromUtf8("123"))
                                                    .SetExtension(UnittestLite.DefaultCordExtensionLite, "123")
                                                    .SetExtension(UnittestLite.DefaultDoubleExtensionLite, 123)
                                                    .SetExtension(UnittestLite.DefaultFixed32ExtensionLite, 123u)
                                                    .SetExtension(UnittestLite.DefaultFixed64ExtensionLite, 123u)
                                                    .SetExtension(UnittestLite.DefaultFloatExtensionLite, 123)
                                                    .SetExtension(UnittestLite.DefaultForeignEnumExtensionLite, ForeignEnumLite.FOREIGN_LITE_BAZ)
                                                    .SetExtension(UnittestLite.DefaultImportEnumExtensionLite, ImportEnumLite.IMPORT_LITE_BAZ)
                                                    .SetExtension(UnittestLite.DefaultInt32ExtensionLite, 123)
                                                    .SetExtension(UnittestLite.DefaultInt64ExtensionLite, 123)
                                                    .SetExtension(UnittestLite.DefaultNestedEnumExtensionLite,
                                                                  TestAllTypesLite.Types.NestedEnum.FOO)
                                                    .SetExtension(UnittestLite.DefaultSfixed32ExtensionLite, 123)
                                                    .SetExtension(UnittestLite.DefaultSfixed64ExtensionLite, 123)
                                                    .SetExtension(UnittestLite.DefaultSint32ExtensionLite, 123)
                                                    .SetExtension(UnittestLite.DefaultSint64ExtensionLite, 123)
                                                    .SetExtension(UnittestLite.DefaultStringExtensionLite, "123")
                                                    .SetExtension(UnittestLite.DefaultStringPieceExtensionLite, "123")
                                                    .SetExtension(UnittestLite.DefaultUint32ExtensionLite, 123u)
                                                    .SetExtension(UnittestLite.DefaultUint64ExtensionLite, 123u)
                                                    //Optional
                                                    .SetExtension(UnittestLite.OptionalBoolExtensionLite, true)
                                                    .SetExtension(UnittestLite.OptionalBytesExtensionLite, ByteString.CopyFromUtf8("123"))
                                                    .SetExtension(UnittestLite.OptionalCordExtensionLite, "123")
                                                    .SetExtension(UnittestLite.OptionalDoubleExtensionLite, 123)
                                                    .SetExtension(UnittestLite.OptionalFixed32ExtensionLite, 123u)
                                                    .SetExtension(UnittestLite.OptionalFixed64ExtensionLite, 123u)
                                                    .SetExtension(UnittestLite.OptionalFloatExtensionLite, 123)
                                                    .SetExtension(UnittestLite.OptionalForeignEnumExtensionLite, ForeignEnumLite.FOREIGN_LITE_BAZ)
                                                    .SetExtension(UnittestLite.OptionalImportEnumExtensionLite, ImportEnumLite.IMPORT_LITE_BAZ)
                                                    .SetExtension(UnittestLite.OptionalInt32ExtensionLite, 123)
                                                    .SetExtension(UnittestLite.OptionalInt64ExtensionLite, 123)
                                                    .SetExtension(UnittestLite.OptionalNestedEnumExtensionLite,
                                                                  TestAllTypesLite.Types.NestedEnum.FOO)
                                                    .SetExtension(UnittestLite.OptionalSfixed32ExtensionLite, 123)
                                                    .SetExtension(UnittestLite.OptionalSfixed64ExtensionLite, 123)
                                                    .SetExtension(UnittestLite.OptionalSint32ExtensionLite, 123)
                                                    .SetExtension(UnittestLite.OptionalSint64ExtensionLite, 123)
                                                    .SetExtension(UnittestLite.OptionalStringExtensionLite, "123")
                                                    .SetExtension(UnittestLite.OptionalStringPieceExtensionLite, "123")
                                                    .SetExtension(UnittestLite.OptionalUint32ExtensionLite, 123u)
                                                    .SetExtension(UnittestLite.OptionalUint64ExtensionLite, 123u)
                                                    //Repeated
                                                    .AddExtension(UnittestLite.RepeatedBoolExtensionLite, true)
                                                    .AddExtension(UnittestLite.RepeatedBytesExtensionLite, ByteString.CopyFromUtf8("123"))
                                                    .AddExtension(UnittestLite.RepeatedCordExtensionLite, "123")
                                                    .AddExtension(UnittestLite.RepeatedDoubleExtensionLite, 123)
                                                    .AddExtension(UnittestLite.RepeatedFixed32ExtensionLite, 123u)
                                                    .AddExtension(UnittestLite.RepeatedFixed64ExtensionLite, 123u)
                                                    .AddExtension(UnittestLite.RepeatedFloatExtensionLite, 123)
                                                    .AddExtension(UnittestLite.RepeatedForeignEnumExtensionLite, ForeignEnumLite.FOREIGN_LITE_BAZ)
                                                    .AddExtension(UnittestLite.RepeatedImportEnumExtensionLite, ImportEnumLite.IMPORT_LITE_BAZ)
                                                    .AddExtension(UnittestLite.RepeatedInt32ExtensionLite, 123)
                                                    .AddExtension(UnittestLite.RepeatedInt64ExtensionLite, 123)
                                                    .AddExtension(UnittestLite.RepeatedNestedEnumExtensionLite,
                                                                  TestAllTypesLite.Types.NestedEnum.FOO)
                                                    .AddExtension(UnittestLite.RepeatedSfixed32ExtensionLite, 123)
                                                    .AddExtension(UnittestLite.RepeatedSfixed64ExtensionLite, 123)
                                                    .AddExtension(UnittestLite.RepeatedSint32ExtensionLite, 123)
                                                    .AddExtension(UnittestLite.RepeatedSint64ExtensionLite, 123)
                                                    .AddExtension(UnittestLite.RepeatedStringExtensionLite, "123")
                                                    .AddExtension(UnittestLite.RepeatedStringPieceExtensionLite, "123")
                                                    .AddExtension(UnittestLite.RepeatedUint32ExtensionLite, 123u)
                                                    .AddExtension(UnittestLite.RepeatedUint64ExtensionLite, 123u)
            ;
            TestAllExtensionsLite msg = builder.Build();

            ExtensionRegistry registry = ExtensionRegistry.CreateInstance();

            UnittestLite.RegisterAllExtensions(registry);

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

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

            Assert.AreEqual(true, copy.GetExtension(UnittestLite.DefaultBoolExtensionLite));
            Assert.AreEqual(ByteString.CopyFromUtf8("123"),
                            copy.GetExtension(UnittestLite.DefaultBytesExtensionLite));
            Assert.AreEqual("123", copy.GetExtension(UnittestLite.DefaultCordExtensionLite));
            Assert.AreEqual(123, copy.GetExtension(UnittestLite.DefaultDoubleExtensionLite));
            Assert.AreEqual(123u, copy.GetExtension(UnittestLite.DefaultFixed32ExtensionLite));
            Assert.AreEqual(123u, copy.GetExtension(UnittestLite.DefaultFixed64ExtensionLite));
            Assert.AreEqual(123, copy.GetExtension(UnittestLite.DefaultFloatExtensionLite));
            Assert.AreEqual(ForeignEnumLite.FOREIGN_LITE_BAZ,
                            copy.GetExtension(UnittestLite.DefaultForeignEnumExtensionLite));
            Assert.AreEqual(ImportEnumLite.IMPORT_LITE_BAZ,
                            copy.GetExtension(UnittestLite.DefaultImportEnumExtensionLite));
            Assert.AreEqual(123, copy.GetExtension(UnittestLite.DefaultInt32ExtensionLite));
            Assert.AreEqual(123, copy.GetExtension(UnittestLite.DefaultInt64ExtensionLite));
            Assert.AreEqual(TestAllTypesLite.Types.NestedEnum.FOO,
                            copy.GetExtension(UnittestLite.DefaultNestedEnumExtensionLite));
            Assert.AreEqual(123, copy.GetExtension(UnittestLite.DefaultSfixed32ExtensionLite));
            Assert.AreEqual(123, copy.GetExtension(UnittestLite.DefaultSfixed64ExtensionLite));
            Assert.AreEqual(123, copy.GetExtension(UnittestLite.DefaultSint32ExtensionLite));
            Assert.AreEqual(123, copy.GetExtension(UnittestLite.DefaultSint64ExtensionLite));
            Assert.AreEqual("123", copy.GetExtension(UnittestLite.DefaultStringExtensionLite));
            Assert.AreEqual("123", copy.GetExtension(UnittestLite.DefaultStringPieceExtensionLite));
            Assert.AreEqual(123u, copy.GetExtension(UnittestLite.DefaultUint32ExtensionLite));
            Assert.AreEqual(123u, copy.GetExtension(UnittestLite.DefaultUint64ExtensionLite));

            Assert.AreEqual(true, copy.GetExtension(UnittestLite.OptionalBoolExtensionLite));
            Assert.AreEqual(ByteString.CopyFromUtf8("123"),
                            copy.GetExtension(UnittestLite.OptionalBytesExtensionLite));
            Assert.AreEqual("123", copy.GetExtension(UnittestLite.OptionalCordExtensionLite));
            Assert.AreEqual(123, copy.GetExtension(UnittestLite.OptionalDoubleExtensionLite));
            Assert.AreEqual(123u, copy.GetExtension(UnittestLite.OptionalFixed32ExtensionLite));
            Assert.AreEqual(123u, copy.GetExtension(UnittestLite.OptionalFixed64ExtensionLite));
            Assert.AreEqual(123, copy.GetExtension(UnittestLite.OptionalFloatExtensionLite));
            Assert.AreEqual(ForeignEnumLite.FOREIGN_LITE_BAZ,
                            copy.GetExtension(UnittestLite.OptionalForeignEnumExtensionLite));
            Assert.AreEqual(ImportEnumLite.IMPORT_LITE_BAZ,
                            copy.GetExtension(UnittestLite.OptionalImportEnumExtensionLite));
            Assert.AreEqual(123, copy.GetExtension(UnittestLite.OptionalInt32ExtensionLite));
            Assert.AreEqual(123, copy.GetExtension(UnittestLite.OptionalInt64ExtensionLite));
            Assert.AreEqual(TestAllTypesLite.Types.NestedEnum.FOO,
                            copy.GetExtension(UnittestLite.OptionalNestedEnumExtensionLite));
            Assert.AreEqual(123, copy.GetExtension(UnittestLite.OptionalSfixed32ExtensionLite));
            Assert.AreEqual(123, copy.GetExtension(UnittestLite.OptionalSfixed64ExtensionLite));
            Assert.AreEqual(123, copy.GetExtension(UnittestLite.OptionalSint32ExtensionLite));
            Assert.AreEqual(123, copy.GetExtension(UnittestLite.OptionalSint64ExtensionLite));
            Assert.AreEqual("123", copy.GetExtension(UnittestLite.OptionalStringExtensionLite));
            Assert.AreEqual("123", copy.GetExtension(UnittestLite.OptionalStringPieceExtensionLite));
            Assert.AreEqual(123u, copy.GetExtension(UnittestLite.OptionalUint32ExtensionLite));
            Assert.AreEqual(123u, copy.GetExtension(UnittestLite.OptionalUint64ExtensionLite));

            Assert.AreEqual(true, copy.GetExtension(UnittestLite.RepeatedBoolExtensionLite, 0));
            Assert.AreEqual(ByteString.CopyFromUtf8("123"),
                            copy.GetExtension(UnittestLite.RepeatedBytesExtensionLite, 0));
            Assert.AreEqual("123", copy.GetExtension(UnittestLite.RepeatedCordExtensionLite, 0));
            Assert.AreEqual(123, copy.GetExtension(UnittestLite.RepeatedDoubleExtensionLite, 0));
            Assert.AreEqual(123u, copy.GetExtension(UnittestLite.RepeatedFixed32ExtensionLite, 0));
            Assert.AreEqual(123u, copy.GetExtension(UnittestLite.RepeatedFixed64ExtensionLite, 0));
            Assert.AreEqual(123, copy.GetExtension(UnittestLite.RepeatedFloatExtensionLite, 0));
            Assert.AreEqual(ForeignEnumLite.FOREIGN_LITE_BAZ,
                            copy.GetExtension(UnittestLite.RepeatedForeignEnumExtensionLite, 0));
            Assert.AreEqual(ImportEnumLite.IMPORT_LITE_BAZ,
                            copy.GetExtension(UnittestLite.RepeatedImportEnumExtensionLite, 0));
            Assert.AreEqual(123, copy.GetExtension(UnittestLite.RepeatedInt32ExtensionLite, 0));
            Assert.AreEqual(123, copy.GetExtension(UnittestLite.RepeatedInt64ExtensionLite, 0));
            Assert.AreEqual(TestAllTypesLite.Types.NestedEnum.FOO,
                            copy.GetExtension(UnittestLite.RepeatedNestedEnumExtensionLite, 0));
            Assert.AreEqual(123, copy.GetExtension(UnittestLite.RepeatedSfixed32ExtensionLite, 0));
            Assert.AreEqual(123, copy.GetExtension(UnittestLite.RepeatedSfixed64ExtensionLite, 0));
            Assert.AreEqual(123, copy.GetExtension(UnittestLite.RepeatedSint32ExtensionLite, 0));
            Assert.AreEqual(123, copy.GetExtension(UnittestLite.RepeatedSint64ExtensionLite, 0));
            Assert.AreEqual("123", copy.GetExtension(UnittestLite.RepeatedStringExtensionLite, 0));
            Assert.AreEqual("123", copy.GetExtension(UnittestLite.RepeatedStringPieceExtensionLite, 0));
            Assert.AreEqual(123u, copy.GetExtension(UnittestLite.RepeatedUint32ExtensionLite, 0));
            Assert.AreEqual(123u, copy.GetExtension(UnittestLite.RepeatedUint64ExtensionLite, 0));
        }