Пример #1
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);
        }
Пример #2
0
        public void ExtensionMergeFrom()
        {
            TestAllExtensions original = TestAllExtensions.CreateBuilder()
                                         .SetExtension(UnitTestProtoFile.OptionalInt32Extension, 1).Build();
            TestAllExtensions merged =
                TestAllExtensions.CreateBuilder().MergeFrom(original).Build();

            Assert.IsTrue((merged.HasExtension(UnitTestProtoFile.OptionalInt32Extension)));
            Assert.AreEqual(1, (int)merged.GetExtension(UnitTestProtoFile.OptionalInt32Extension));
        }
Пример #3
0
        public void TestDefaultValueRoundTrip()
        {
            var message = new TestAllExtensions();

            message.SetExtension(OptionalBoolExtension, false);
            Assert.IsFalse(message.GetExtension(OptionalBoolExtension));
            Assert.IsTrue(message.HasExtension(OptionalBoolExtension));

            var bytes    = message.ToByteArray();
            var registry = new ExtensionRegistry {
                OptionalBoolExtension
            };
            var parsed = TestAllExtensions.Parser.WithExtensionRegistry(registry).ParseFrom(bytes);

            Assert.IsFalse(parsed.GetExtension(OptionalBoolExtension));
            Assert.IsTrue(parsed.HasExtension(OptionalBoolExtension));
        }
Пример #4
0
        public void DefaultExtensionValues()
        {
            var message = new TestAllExtensions();

            Assert.AreEqual(false, message.GetExtension(OptionalBoolExtension));
            Assert.AreEqual(ByteString.Empty, message.GetExtension(OptionalBytesExtension));
            Assert.AreEqual(0.0, message.GetExtension(OptionalDoubleExtension));
            Assert.AreEqual(0, message.GetExtension(OptionalFixed32Extension));
            Assert.AreEqual(0L, message.GetExtension(OptionalFixed64Extension));
            Assert.AreEqual(0.0f, message.GetExtension(OptionalFloatExtension));
            Assert.AreEqual(ForeignEnum.ForeignFoo, message.GetExtension(OptionalForeignEnumExtension));
            Assert.IsNull(message.GetExtension(OptionalForeignMessageExtension));
            Assert.AreEqual(ImportEnum.ImportFoo, message.GetExtension(OptionalImportEnumExtension));
            Assert.IsNull(message.GetExtension(OptionalImportMessageExtension));
            Assert.AreEqual(0, message.GetExtension(OptionalInt32Extension));
            Assert.AreEqual(0L, message.GetExtension(OptionalInt64Extension));
            Assert.AreEqual(Proto2.TestAllTypes.Types.NestedEnum.Foo, message.GetExtension(OptionalNestedEnumExtension));
            Assert.IsNull(message.GetExtension(OptionalNestedMessageExtension));
            Assert.IsNull(message.GetExtension(OptionalPublicImportMessageExtension));
            Assert.AreEqual(0, message.GetExtension(OptionalSfixed32Extension));
            Assert.AreEqual(0L, message.GetExtension(OptionalSfixed64Extension));
            Assert.AreEqual(0, message.GetExtension(OptionalSint32Extension));
            Assert.AreEqual(0L, message.GetExtension(OptionalSint64Extension));
            Assert.AreEqual("", message.GetExtension(OptionalStringExtension));
            Assert.AreEqual(0U, message.GetExtension(OptionalUint32Extension));
            Assert.AreEqual(0UL, message.GetExtension(OptionalUint64Extension));

            // Repeated fields
            Assert.IsNull(message.GetExtension(RepeatedBoolExtension));
            Assert.IsNull(message.GetExtension(RepeatedBytesExtension));
            Assert.IsNull(message.GetExtension(RepeatedDoubleExtension));
            Assert.IsNull(message.GetExtension(RepeatedFixed32Extension));
            Assert.IsNull(message.GetExtension(RepeatedFixed64Extension));
            Assert.IsNull(message.GetExtension(RepeatedFloatExtension));
            Assert.IsNull(message.GetExtension(RepeatedForeignEnumExtension));
            Assert.IsNull(message.GetExtension(RepeatedForeignMessageExtension));
            Assert.IsNull(message.GetExtension(RepeatedImportEnumExtension));
            Assert.IsNull(message.GetExtension(RepeatedImportMessageExtension));
            Assert.IsNull(message.GetExtension(RepeatedNestedEnumExtension));
            Assert.IsNull(message.GetExtension(RepeatedNestedMessageExtension));
            Assert.IsNull(message.GetExtension(RepeatedSfixed32Extension));
            Assert.IsNull(message.GetExtension(RepeatedSfixed64Extension));
            Assert.IsNull(message.GetExtension(RepeatedSint32Extension));
            Assert.IsNull(message.GetExtension(RepeatedSint64Extension));
            Assert.IsNull(message.GetExtension(RepeatedStringExtension));
            Assert.IsNull(message.GetExtension(RepeatedUint32Extension));
            Assert.IsNull(message.GetExtension(RepeatedUint64Extension));

            // Oneof fields
            Assert.AreEqual(0, message.GetExtension(OneofUint32Extension));
            Assert.AreEqual("", message.GetExtension(OneofStringExtension));
            Assert.AreEqual(ByteString.Empty, message.GetExtension(OneofBytesExtension));
            Assert.IsNull(message.GetExtension(OneofNestedMessageExtension));

            Assert.AreEqual(true, message.GetExtension(DefaultBoolExtension));
            Assert.AreEqual(ByteString.CopyFromUtf8("world"), message.GetExtension(DefaultBytesExtension));
            Assert.AreEqual("123", message.GetExtension(DefaultCordExtension));
            Assert.AreEqual(52e3, message.GetExtension(DefaultDoubleExtension));
            Assert.AreEqual(47, message.GetExtension(DefaultFixed32Extension));
            Assert.AreEqual(48, message.GetExtension(DefaultFixed64Extension));
            Assert.AreEqual(51.5, message.GetExtension(DefaultFloatExtension));
            Assert.AreEqual(ForeignEnum.ForeignBar, message.GetExtension(DefaultForeignEnumExtension));
            Assert.AreEqual(ImportEnum.ImportBar, message.GetExtension(DefaultImportEnumExtension));
            Assert.AreEqual(41, message.GetExtension(DefaultInt32Extension));
            Assert.AreEqual(42, message.GetExtension(DefaultInt64Extension));
            Assert.AreEqual(Proto2.TestAllTypes.Types.NestedEnum.Bar, message.GetExtension(DefaultNestedEnumExtension));
            Assert.AreEqual(49, message.GetExtension(DefaultSfixed32Extension));
            Assert.AreEqual(-50, message.GetExtension(DefaultSfixed64Extension));
            Assert.AreEqual(-45, message.GetExtension(DefaultSint32Extension));
            Assert.AreEqual(46, message.GetExtension(DefaultSint64Extension));
            Assert.AreEqual("hello", message.GetExtension(DefaultStringExtension));
            Assert.AreEqual("abc", message.GetExtension(DefaultStringPieceExtension));
            Assert.AreEqual(43, message.GetExtension(DefaultUint32Extension));
            Assert.AreEqual(44, message.GetExtension(DefaultUint64Extension));

            Assert.False(message.HasExtension(DefaultBoolExtension));
            Assert.False(message.HasExtension(DefaultBytesExtension));
            Assert.False(message.HasExtension(DefaultCordExtension));
            Assert.False(message.HasExtension(DefaultDoubleExtension));
            Assert.False(message.HasExtension(DefaultFixed32Extension));
            Assert.False(message.HasExtension(DefaultFixed64Extension));
            Assert.False(message.HasExtension(DefaultFloatExtension));
            Assert.False(message.HasExtension(DefaultForeignEnumExtension));
            Assert.False(message.HasExtension(DefaultImportEnumExtension));
            Assert.False(message.HasExtension(DefaultInt32Extension));
            Assert.False(message.HasExtension(DefaultInt64Extension));
            Assert.False(message.HasExtension(DefaultNestedEnumExtension));
            Assert.False(message.HasExtension(DefaultSfixed32Extension));
            Assert.False(message.HasExtension(DefaultSfixed64Extension));
            Assert.False(message.HasExtension(DefaultSint32Extension));
            Assert.False(message.HasExtension(DefaultSint64Extension));
            Assert.False(message.HasExtension(DefaultStringExtension));
            Assert.False(message.HasExtension(DefaultStringPieceExtension));
            Assert.False(message.HasExtension(DefaultUint32Extension));
            Assert.False(message.HasExtension(DefaultUint64Extension));
        }