예제 #1
0
        public void Nullable()
        {
            // The converter will never be provided with a null value; that's handled elsewhere.
            // But we need to make sure the nullability is swallowed, effectively.
            var property  = typeof(PropertyHolder).GetProperty(nameof(PropertyHolder.NullableTuple2));
            var converter = ValueTupleConverter.MaybeCreateConverter(property);

            Assert.NotNull(converter);

            var map = new Dictionary <string, Value>
            {
                { "name", new Value {
                      StringValue = "abc"
                  } },
                { "value", new Value {
                      IntegerValue = 42
                  } }
            };

            (string name, int value)tuple = ((string, int))converter.DeserializeMap(DeserializationContext, map);
            Assert.Equal("abc", tuple.name);
            Assert.Equal(42, tuple.value);

            var serialized = converter.Serialize(SerializationContext, tuple);

            Assert.Equal(map, serialized.MapValue.Fields);
        }
예제 #2
0
        public void SimpleProperties_DeserializeMap([CombinatorialValues(2, 3, 4, 5, 6, 7)] int arity)
        {
            var property  = typeof(PropertyHolder).GetProperty($"Tuple{arity}");
            var converter = ValueTupleConverter.MaybeCreateConverter(property);

            Assert.NotNull(converter);
            var map = Enumerable.Range(1, arity).ToDictionary(x => $"v{x}", x => new Value {
                IntegerValue = x
            });
            var actualTupleValue   = converter.DeserializeMap(DeserializationContext, map);
            var expectedTupleValue = s_sampleValuesByArity[arity];

            Assert.Equal(expectedTupleValue, actualTupleValue);
        }
예제 #3
0
        public void SimpleProperties_Serialize([CombinatorialValues(2, 3, 4, 5, 6, 7)] int arity)
        {
            var property  = typeof(PropertyHolder).GetProperty($"Tuple{arity}");
            var converter = ValueTupleConverter.MaybeCreateConverter(property);

            Assert.NotNull(converter);
            var actualProtoValue   = converter.Serialize(SerializationContext, s_sampleValuesByArity[arity]);
            var expectedProtoValue = new Value
            {
                MapValue = new MapValue
                {
                    Fields = { { Enumerable.Range(1, arity).ToDictionary(x => $"v{x}", x => new Value {
                                IntegerValue = x
                            }) } }
                }
            };

            Assert.Equal(expectedProtoValue, actualProtoValue);
        }
예제 #4
0
        public void CustomConversion()
        {
            var property  = typeof(PropertyHolder).GetProperty(nameof(PropertyHolder.TupleWithGuid));
            var converter = ValueTupleConverter.MaybeCreateConverter(property);

            Assert.NotNull(converter);

            var tuple      = (name : "xyz", guid : Guid.NewGuid());
            var serialized = converter.Serialize(SerializationContext, tuple);

            var expected = new Dictionary <string, Value>
            {
                { "name", new Value {
                      StringValue = "xyz"
                  } },
                { "guid", new Value {
                      StringValue = $"{CustomGuidConverter.Prefix}{tuple.guid}"
                  } }
            };

            Assert.Equal(expected, serialized.MapValue.Fields);

            Assert.Equal(tuple, converter.DeserializeValue(DeserializationContext, serialized));
        }
예제 #5
0
        public void InvalidProperty(string propertyName)
        {
            var property = typeof(PropertyHolder).GetProperty(propertyName);

            Assert.Null(ValueTupleConverter.MaybeCreateConverter(property));
        }