コード例 #1
0
        public void TestLite()
        {
            // Since lite messages are a subset of regular messages, we can mostly
            // assume that the functionality of lite messages is already thoroughly
            // tested by the regular tests.  All this test really verifies is that
            // a proto with optimize_for = LITE_RUNTIME compiles correctly when
            // linked only against the lite library.  That is all tested at compile
            // time, leaving not much to do in this method.  Let's just do some random
            // stuff to make sure the lite message is actually here and usable.

            TestAllTypesLite message =
                TestAllTypesLite.CreateBuilder()
                .SetOptionalInt32(123)
                .AddRepeatedString("hello")
                .SetOptionalNestedMessage(
                    TestAllTypesLite.Types.NestedMessage.CreateBuilder().SetBb(7))
                .Build();

            ByteString data = message.ToByteString();

            TestAllTypesLite message2 = TestAllTypesLite.ParseFrom(data);

            Assert.AreEqual(123, message2.OptionalInt32);
            Assert.AreEqual(1, message2.RepeatedStringCount);
            Assert.AreEqual("hello", message2.RepeatedStringList[0]);
            Assert.AreEqual(7, message2.OptionalNestedMessage.Bb);
        }
コード例 #2
0
        public void TestAllTypesDefaultedRoundTrip()
        {
            TestAllTypesLite msg = TestAllTypesLite.DefaultInstance;

            Assert.IsTrue(msg.IsInitialized);
            TestAllTypesLite copy = TestAllTypesLite.CreateBuilder().MergeFrom(msg.ToByteArray()).Build();

            TestUtil.AssertBytesEqual(msg.ToByteArray(), copy.ToByteArray());
        }
コード例 #3
0
        public void TestIBuilderLiteWeakClear()
        {
            TestAllTypesLite copy, msg = TestAllTypesLite.DefaultInstance;

            copy = msg.ToBuilder().SetOptionalString("Should be removed.").Build();
            Assert.AreNotEqual(msg.ToByteArray(), copy.ToByteArray());

            copy = (TestAllTypesLite)((IBuilderLite)copy.ToBuilder()).WeakClear().WeakBuild();
            Assert.AreEqual(msg.ToByteArray(), copy.ToByteArray());
        }
コード例 #4
0
        public void TestBuilderLiteMergeFromCodedInputStream()
        {
            TestAllTypesLite copy, msg = TestAllTypesLite.CreateBuilder()
                                         .SetOptionalString("Should be merged.").Build();

            copy = TestAllTypesLite.DefaultInstance;
            Assert.AreNotEqual(msg.ToByteArray(), copy.ToByteArray());

            copy = copy.ToBuilder().MergeFrom(CodedInputStream.CreateInstance(new MemoryStream(msg.ToByteArray()))).Build();
            Assert.AreEqual(msg.ToByteArray(), copy.ToByteArray());
        }
コード例 #5
0
        public void TestIBuilderLiteWeakMergeFromByteString()
        {
            TestAllTypesLite copy, msg = TestAllTypesLite.CreateBuilder()
                                         .SetOptionalString("Should be merged.").Build();

            copy = TestAllTypesLite.DefaultInstance;
            Assert.AreNotEqual(msg.ToByteArray(), copy.ToByteArray());

            copy = (TestAllTypesLite)((IBuilderLite)copy.ToBuilder()).WeakMergeFrom(msg.ToByteString()).WeakBuild();
            Assert.AreEqual(msg.ToByteArray(), copy.ToByteArray());
        }
コード例 #6
0
        public void TestGeneratedBuilderLiteAddRange()
        {
            TestAllTypesLite copy, msg = TestAllTypesLite.CreateBuilder()
                                         .SetOptionalUint32(123)
                                         .AddRepeatedInt32(1)
                                         .AddRepeatedInt32(2)
                                         .AddRepeatedInt32(3)
                                         .Build();

            copy = msg.DefaultInstanceForType.ToBuilder().MergeFrom(msg).Build();
            Assert.AreEqual(msg.ToByteArray(), copy.ToByteArray());
        }
コード例 #7
0
        public void TestAllTypesToString()
        {
            TestAllTypesLite msg  = TestAllTypesLite.DefaultInstance;
            TestAllTypesLite copy = msg.ToBuilder().Build();

            Assert.AreEqual(msg.ToString(), copy.ToString());
            Assert.IsEmpty(msg.ToString());
            msg = msg.ToBuilder().SetOptionalInt32(-1).Build();
            Assert.AreEqual("optional_int32: -1", msg.ToString().TrimEnd());
            msg = msg.ToBuilder().SetOptionalString("abc123").Build();
            Assert.AreEqual("optional_int32: -1\noptional_string: \"abc123\"", msg.ToString().Replace("\r", "").TrimEnd());
        }
コード例 #8
0
        public void TestBuilderLiteMergeDelimitedFrom()
        {
            TestAllTypesLite copy, msg = TestAllTypesLite.CreateBuilder()
                                         .SetOptionalString("Should be merged.").Build();

            copy = TestAllTypesLite.DefaultInstance;
            Assert.AreNotEqual(msg.ToByteArray(), copy.ToByteArray());
            Stream s = new MemoryStream();

            msg.WriteDelimitedTo(s);
            s.Position = 0;
            copy       = copy.ToBuilder().MergeDelimitedFrom(s).Build();
            Assert.AreEqual(msg.ToByteArray(), copy.ToByteArray());
        }
コード例 #9
0
        public void TestAllTypesEquality()
        {
            TestAllTypesLite msg  = TestAllTypesLite.DefaultInstance;
            TestAllTypesLite copy = msg.ToBuilder().Build();

            Assert.AreEqual(msg.GetHashCode(), copy.GetHashCode());
            Assert.IsTrue(msg.Equals(copy));
            msg = msg.ToBuilder().SetOptionalString("Hi").Build();
            Assert.AreNotEqual(msg.GetHashCode(), copy.GetHashCode());
            Assert.IsFalse(msg.Equals(copy));
            copy = copy.ToBuilder().SetOptionalString("Hi").Build();
            Assert.AreEqual(msg.GetHashCode(), copy.GetHashCode());
            Assert.IsTrue(msg.Equals(copy));
        }
コード例 #10
0
        public void TestMergeFromCodedInputStream()
        {
            TestAllTypesLite copy, msg = TestAllTypesLite.CreateBuilder()
                                         .SetOptionalUint32(uint.MaxValue).Build();

            copy = TestAllTypesLite.DefaultInstance;
            Assert.AreNotEqual(msg.ToByteArray(), copy.ToByteArray());

            using (MemoryStream ms = new MemoryStream(msg.ToByteArray())) {
                CodedInputStream ci = CodedInputStream.CreateInstance(ms);
                copy = copy.ToBuilder().MergeFrom(ci).Build();
            }

            Assert.AreEqual(msg.ToByteArray(), copy.ToByteArray());
        }
コード例 #11
0
        public void TestMissingFieldsLite()
        {
            TestAllTypesLite msg = TestAllTypesLite.CreateBuilder()
                                   .SetOptionalInt32(123)
                                   .SetOptionalString("123")
                                   .Build();

            byte[] bits = msg.ToByteArray();
            TestAllExtensionsLite copy = TestAllExtensionsLite.ParseFrom(bits);

            Assert.AreNotEqual(msg, copy);

            //The lite runtime removes all unknown fields and extensions
            byte[] copybits = copy.ToByteArray();
            Assert.AreEqual(0, copybits.Length);
        }
コード例 #12
0
        public void TestAllTypesModifiedRoundTrip()
        {
            TestAllTypesLite msg = TestAllTypesLite.DefaultInstance;

            msg.ToBuilder()
            .SetOptionalBool(true)
            .SetOptionalCord("Hi")
            .SetOptionalDouble(1.123)
            .SetOptionalForeignEnum(ForeignEnumLite.FOREIGN_LITE_FOO)
            .SetOptionalForeignMessage(ForeignMessageLite.CreateBuilder().SetC('c').Build())
            .SetOptionalGroup(TestAllTypesLite.Types.OptionalGroup.CreateBuilder().SetA('a').Build())
            .SetOptionalImportEnum(ImportEnumLite.IMPORT_LITE_BAR)
            .SetOptionalInt32(32)
            .SetOptionalInt64(64)
            .SetOptionalNestedEnum(TestAllTypesLite.Types.NestedEnum.FOO)
            .SetOptionalString("SetOptionalString")
            .AddRepeatedGroup(TestAllTypesLite.Types.RepeatedGroup.CreateBuilder().SetA('a').Build())
            .AddRepeatedGroup(TestAllTypesLite.Types.RepeatedGroup.CreateBuilder().SetA('A').Build())
            ;
            TestAllTypesLite copy = TestAllTypesLite.CreateBuilder().MergeFrom(msg.ToByteArray()).Build();

            TestUtil.AssertBytesEqual(msg.ToByteArray(), copy.ToByteArray());
        }