Exemplo n.º 1
0
 public void ExtensionReflectionDefaults()
 {
     TestUtil.TestInMultipleCultures(() => {
         extensionsReflectionTester.AssertClearViaReflection(TestAllExtensions.DefaultInstance);
         extensionsReflectionTester.AssertClearViaReflection(TestAllExtensions.CreateBuilder().Build());
     });
 }
Exemplo n.º 2
0
        public void ExtensionReflectionSetters()
        {
            TestAllExtensions.Builder builder = TestAllExtensions.CreateBuilder();
            extensionsReflectionTester.SetAllFieldsViaReflection(builder);
            TestAllExtensions message = builder.Build();

            TestUtil.AssertAllExtensionsSet(message);
        }
Exemplo n.º 3
0
 public void ParseExtensions()
 {
     TestAllExtensions.Builder builder = TestAllExtensions.CreateBuilder();
     TextFormat.Merge(AllExtensionsSetText,
                      TestUtil.CreateExtensionRegistry(),
                      builder);
     TestUtil.AssertAllExtensionsSet(builder.Build());
 }
Exemplo n.º 4
0
        public void ExtensionAccessors()
        {
            TestAllExtensions.Builder builder = TestAllExtensions.CreateBuilder();
            TestUtil.SetAllExtensions(builder);
            TestAllExtensions message = builder.Build();

            TestUtil.AssertAllExtensionsSet(message);
        }
Exemplo n.º 5
0
        public void ExtensionReflectionRepeatedSetters()
        {
            TestAllExtensions.Builder builder = TestAllExtensions.CreateBuilder();
            extensionsReflectionTester.SetAllFieldsViaReflection(builder);
            extensionsReflectionTester.ModifyRepeatedFieldsViaReflection(builder);
            TestAllExtensions message = builder.Build();

            TestUtil.AssertRepeatedExtensionsModified(message);
        }
Exemplo n.º 6
0
        public void ExtensionRepeatedSetters()
        {
            TestAllExtensions.Builder builder = TestAllExtensions.CreateBuilder();
            TestUtil.SetAllExtensions(builder);
            TestUtil.ModifyRepeatedExtensions(builder);
            TestAllExtensions message = builder.Build();

            TestUtil.AssertRepeatedExtensionsModified(message);
        }
Exemplo n.º 7
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));
        }
Exemplo n.º 8
0
 public void ClearExtension()
 {
     // ClearExtension() is not actually used in TestUtil, so try it manually.
     Assert.IsFalse(TestAllExtensions.CreateBuilder()
                    .SetExtension(UnitTestProtoFile.OptionalInt32Extension, 1)
                    .ClearExtension(UnitTestProtoFile.OptionalInt32Extension)
                    .HasExtension(UnitTestProtoFile.OptionalInt32Extension));
     Assert.AreEqual(0, TestAllExtensions.CreateBuilder()
                     .AddExtension(UnitTestProtoFile.RepeatedInt32Extension, 1)
                     .ClearExtension(UnitTestProtoFile.RepeatedInt32Extension)
                     .GetExtensionCount(UnitTestProtoFile.RepeatedInt32Extension));
 }
        public void EqualsAndHashCode()
        {
            TestAllTypes      a = TestUtil.GetAllSet();
            TestAllTypes      b = TestAllTypes.CreateBuilder().Build();
            TestAllTypes      c = TestAllTypes.CreateBuilder(b).AddRepeatedString("x").Build();
            TestAllTypes      d = TestAllTypes.CreateBuilder(c).AddRepeatedString("y").Build();
            TestAllExtensions e = TestUtil.GetAllExtensionsSet();
            TestAllExtensions f = TestAllExtensions.CreateBuilder(e)
                                  .AddExtension(UnitTestProtoFile.RepeatedInt32Extension, 999).Build();

            CheckEqualsIsConsistent(a);
            CheckEqualsIsConsistent(b);
            CheckEqualsIsConsistent(c);
            CheckEqualsIsConsistent(d);
            CheckEqualsIsConsistent(e);
            CheckEqualsIsConsistent(f);

            CheckNotEqual(a, b);
            CheckNotEqual(a, c);
            CheckNotEqual(a, d);
            CheckNotEqual(a, e);
            CheckNotEqual(a, f);

            CheckNotEqual(b, c);
            CheckNotEqual(b, d);
            CheckNotEqual(b, e);
            CheckNotEqual(b, f);

            CheckNotEqual(c, d);
            CheckNotEqual(c, e);
            CheckNotEqual(c, f);

            CheckNotEqual(d, e);
            CheckNotEqual(d, f);

            CheckNotEqual(e, f);

            // Deserializing into the TestEmptyMessage such that every field is an UnknownFieldSet.Field
            TestEmptyMessage eUnknownFields = TestEmptyMessage.ParseFrom(e.ToByteArray());
            TestEmptyMessage fUnknownFields = TestEmptyMessage.ParseFrom(f.ToByteArray());

            CheckNotEqual(eUnknownFields, fUnknownFields);
            CheckEqualsIsConsistent(eUnknownFields);
            CheckEqualsIsConsistent(fUnknownFields);

            // Subseqent reconstitutions should be identical
            TestEmptyMessage eUnknownFields2 = TestEmptyMessage.ParseFrom(e.ToByteArray());

            CheckEqualsIsConsistent(eUnknownFields, eUnknownFields2);
        }
Exemplo n.º 10
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);
        }
Exemplo n.º 11
0
        public void RequiredExtension()
        {
            TestAllExtensions.Builder builder = TestAllExtensions.CreateBuilder();

            Assert.IsTrue(builder.IsInitialized);

            builder.SetExtension(TestRequired.Single, TestRequiredUninitialized);
            Assert.IsFalse(builder.IsInitialized);

            builder.SetExtension(TestRequired.Single, TestRequiredInitialized);
            Assert.IsTrue(builder.IsInitialized);

            builder.AddExtension(TestRequired.Multi, TestRequiredUninitialized);
            Assert.IsFalse(builder.IsInitialized);

            builder.SetExtension(TestRequired.Multi, 0, TestRequiredInitialized);
            Assert.IsTrue(builder.IsInitialized);
        }
        public void EqualsAndHashCode()
        {
            TestAllTypes      a = TestUtil.GetAllSet();
            TestAllTypes      b = TestAllTypes.CreateBuilder().Build();
            TestAllTypes      c = TestAllTypes.CreateBuilder(b).AddRepeatedString("x").Build();
            TestAllTypes      d = TestAllTypes.CreateBuilder(c).AddRepeatedString("y").Build();
            TestAllExtensions e = TestUtil.GetAllExtensionsSet();
            TestAllExtensions f = TestAllExtensions.CreateBuilder(e)
                                  .AddExtension(UnitTestProtoFile.RepeatedInt32Extension, 999).Build();

            CheckEqualsIsConsistent(a);
            CheckEqualsIsConsistent(b);
            CheckEqualsIsConsistent(c);
            CheckEqualsIsConsistent(d);
            CheckEqualsIsConsistent(e);
            CheckEqualsIsConsistent(f);

            CheckNotEqual(a, b);
            CheckNotEqual(a, c);
            CheckNotEqual(a, d);
            CheckNotEqual(a, e);
            CheckNotEqual(a, f);

            CheckNotEqual(b, c);
            CheckNotEqual(b, d);
            CheckNotEqual(b, e);
            CheckNotEqual(b, f);

            CheckNotEqual(c, d);
            CheckNotEqual(c, e);
            CheckNotEqual(c, f);

            CheckNotEqual(d, e);
            CheckNotEqual(d, f);

            CheckNotEqual(e, f);
        }
Exemplo n.º 13
0
 public void ExtensionReflectionRepeatedSettersRejectNull()
 {
     TestAllExtensions.Builder builder = TestAllExtensions.CreateBuilder();
     extensionsReflectionTester.AssertReflectionRepeatedSettersRejectNull(builder);
 }
Exemplo n.º 14
0
 public void ExtensionDefaults()
 {
     TestUtil.AssertExtensionsClear(TestAllExtensions.DefaultInstance);
     TestUtil.AssertExtensionsClear(TestAllExtensions.CreateBuilder().Build());
 }
Exemplo n.º 15
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));
        }
 public void ExtensionReflectionDefaults()
 {
     extensionsReflectionTester.AssertClearViaReflection(TestAllExtensions.DefaultInstance);
     extensionsReflectionTester.AssertClearViaReflection(TestAllExtensions.CreateBuilder().Build());
 }