public void TestMissingExtensionsLite() { const int optionalInt32 = 12345678; TestAllExtensionsLite.Builder builder = TestAllExtensionsLite.CreateBuilder(); builder.SetExtension(UnitTestLiteProtoFile.OptionalInt32ExtensionLite, optionalInt32); builder.AddExtension(UnitTestLiteProtoFile.RepeatedDoubleExtensionLite, 1.1); builder.AddExtension(UnitTestLiteProtoFile.RepeatedDoubleExtensionLite, 1.2); builder.AddExtension(UnitTestLiteProtoFile.RepeatedDoubleExtensionLite, 1.3); TestAllExtensionsLite msg = builder.Build(); Assert.IsTrue(msg.HasExtension(UnitTestLiteProtoFile.OptionalInt32ExtensionLite)); Assert.AreEqual(3, msg.GetExtensionCount(UnitTestLiteProtoFile.RepeatedDoubleExtensionLite)); byte[] bits = msg.ToByteArray(); TestAllExtensionsLite copy = TestAllExtensionsLite.ParseFrom(bits); Assert.IsFalse(copy.HasExtension(UnitTestLiteProtoFile.OptionalInt32ExtensionLite)); Assert.AreEqual(0, copy.GetExtensionCount(UnitTestLiteProtoFile.RepeatedDoubleExtensionLite)); Assert.AreNotEqual(msg, copy); //The lite runtime removes all unknown fields and extensions byte[] copybits = copy.ToByteArray(); Assert.AreEqual(0, copybits.Length); }
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()); }
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)); }
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)); }