示例#1
0
        public void TestEqualityOnExtensions()
        {
            TestAllExtensionsLite msg  = TestAllExtensionsLite.DefaultInstance;
            TestAllExtensionsLite copy = msg.ToBuilder().Build();

            Assert.AreEqual(msg.GetHashCode(), copy.GetHashCode());
            Assert.IsTrue(msg.Equals(copy));
            msg = msg.ToBuilder().SetExtension(UnitTestLiteProtoFile.OptionalStringExtensionLite, "Hi").Build();
            Assert.AreNotEqual(msg.GetHashCode(), copy.GetHashCode());
            Assert.IsFalse(msg.Equals(copy));
            copy = copy.ToBuilder().SetExtension(UnitTestLiteProtoFile.OptionalStringExtensionLite, "Hi").Build();
            Assert.AreEqual(msg.GetHashCode(), copy.GetHashCode());
            Assert.IsTrue(msg.Equals(copy));
        }
示例#2
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);
        }