Ejemplo n.º 1
0
        public void NonNullDefaultIsPreservedThroughSerialization()
        {
            var message = new TestWellKnownTypes
            {
                StringField = "",
                BytesField  = ByteString.Empty,
                BoolField   = false,
                FloatField  = 0f,
                DoubleField = 0d,
                Int32Field  = 0,
                Int64Field  = 0,
                Uint32Field = 0,
                Uint64Field = 0
            };

            MessageParsingHelpers.AssertRoundtrip(TestWellKnownTypes.Parser, message, parsed =>
            {
                Assert.AreEqual("", parsed.StringField);
                Assert.AreEqual(ByteString.Empty, parsed.BytesField);
                Assert.AreEqual(false, parsed.BoolField);
                Assert.AreEqual(0f, parsed.FloatField);
                Assert.AreEqual(0d, parsed.DoubleField);
                Assert.AreEqual(0, parsed.Int32Field);
                Assert.AreEqual(0L, parsed.Int64Field);
                Assert.AreEqual(0U, parsed.Uint32Field);
                Assert.AreEqual(0UL, parsed.Uint64Field);
            });
        }
Ejemplo n.º 2
0
        public void NonDefaultSingleValues()
        {
            var message = new TestWellKnownTypes
            {
                StringField = "x",
                BytesField  = ByteString.CopyFrom(1, 2, 3),
                BoolField   = true,
                FloatField  = 12.5f,
                DoubleField = 12.25d,
                Int32Field  = 1,
                Int64Field  = 2,
                Uint32Field = 3,
                Uint64Field = 4
            };

            MessageParsingHelpers.AssertRoundtrip(TestWellKnownTypes.Parser, message, parsed =>
            {
                Assert.AreEqual("x", parsed.StringField);
                Assert.AreEqual(ByteString.CopyFrom(1, 2, 3), parsed.BytesField);
                Assert.AreEqual(true, parsed.BoolField);
                Assert.AreEqual(12.5f, parsed.FloatField);
                Assert.AreEqual(12.25d, parsed.DoubleField);
                Assert.AreEqual(1, parsed.Int32Field);
                Assert.AreEqual(2L, parsed.Int64Field);
                Assert.AreEqual(3U, parsed.Uint32Field);
                Assert.AreEqual(4UL, parsed.Uint64Field);
            });
        }
Ejemplo n.º 3
0
        public void MapWrappersSerializeDeserialize()
        {
            // Note: no null values here, as they are prohibited in map fields
            // (despite being representable).
            var message = new MapWellKnownTypes
            {
                BoolField  = { { 10, false }, { 20, true } },
                BytesField =
                {
                    {   -1, ByteString.CopyFrom(1, 2, 3) },
                    {   10, ByteString.CopyFrom(4, 5, 6) },
                    { 1000, ByteString.Empty },
                },
                DoubleField = { { 1, 12.5 }, { 10, -1.5 }, { 20, 0d } },
                FloatField  = { { 2, 123.25f }, { 3, -20f }, { 4, 0f } },
                Int32Field  = { { 5, int.MaxValue }, { 6, int.MinValue }, { 7, 0 } },
                Int64Field  = { { 8, long.MaxValue }, { 9, long.MinValue }, { 10, 0L } },
                StringField = { { 11, "First" }, { 12, "Second" }, { 13, "" } },
                Uint32Field = { { 15, uint.MaxValue }, { 16, uint.MinValue }, { 17, 0U } },
                Uint64Field = { { 18, ulong.MaxValue }, { 19, ulong.MinValue }, { 20, 0UL } },
            };

            // Just to test a single value for sanity...
            Assert.AreEqual("Second", message.StringField[12]);

            MessageParsingHelpers.AssertRoundtrip(MapWellKnownTypes.Parser, message);
        }
Ejemplo n.º 4
0
 private void AssertOneofRoundTrip(OneofWellKnownTypes message)
 {
     // Normal roundtrip, but explicitly checking the case...
     MessageParsingHelpers.AssertRoundtrip(OneofWellKnownTypes.Parser, message, parsed =>
     {
         Assert.AreEqual(message.OneofFieldCase, parsed.OneofFieldCase);
     });
 }
Ejemplo n.º 5
0
        public void RepeatedWrappersSerializeDeserialize()
        {
            var message = new RepeatedWellKnownTypes
            {
                BoolField   = { true, false },
                BytesField  = { ByteString.CopyFrom(1, 2, 3), ByteString.CopyFrom(4, 5, 6), ByteString.Empty },
                DoubleField = { 12.5, -1.5, 0d },
                FloatField  = { 123.25f, -20f, 0f },
                Int32Field  = { int.MaxValue, int.MinValue, 0 },
                Int64Field  = { long.MaxValue, long.MinValue, 0L },
                StringField = { "First", "Second", "" },
                Uint32Field = { uint.MaxValue, uint.MinValue, 0U },
                Uint64Field = { ulong.MaxValue, ulong.MinValue, 0UL },
            };

            // Just to test a single value for sanity...
            Assert.AreEqual("Second", message.StringField[1]);

            MessageParsingHelpers.AssertRoundtrip(RepeatedWellKnownTypes.Parser, message);
        }
Ejemplo n.º 6
0
        public void NegativeSingleValues()
        {
            var message = new TestWellKnownTypes
            {
                FloatField  = -12.5f,
                DoubleField = -12.25d,
                Int32Field  = -1,
                Int64Field  = -2
            };

            MessageParsingHelpers.AssertWritingMessage(message);

            MessageParsingHelpers.AssertRoundtrip(TestWellKnownTypes.Parser, message, parsed =>
            {
                Assert.AreEqual(-12.5f, parsed.FloatField);
                Assert.AreEqual(-12.25d, parsed.DoubleField);
                Assert.AreEqual(-1, parsed.Int32Field);
                Assert.AreEqual(-2L, parsed.Int64Field);
            });
        }