public void WeirdDtoTests()
        {
            DtoInfo <WeirdDto> info = DtoInfo.GetInfo <WeirdDto>();

            info.Properties.Count.Should().Be(2);
            Invoking(() => info.CreateNew()).Should().Throw <InvalidOperationException>();

            WeirdDto dto = new(1, 2);

            Invoking(() => info.ShallowClone(dto)).Should().Throw <InvalidOperationException>();

            var property = info.GetProperty <int>("IntegerProperty");

            info.GetProperty(x => x.IntegerProperty).Should().Be(property);
            property.Name.Should().Be("IntegerProperty");
            property.ValueType.Should().Be(typeof(int));
            property.IsReadOnly.Should().BeTrue();
            ((PropertyInfo)property.MemberInfo).GetMethod !.Name.Should().Be("get_IntegerProperty");
            property.GetValue(dto).Should().Be(dto.IntegerProperty);
            Invoking(() => property.SetValue(dto, 24)).Should().Throw <InvalidOperationException>();

            var field = info.GetProperty <int>("IntegerField");

            info.GetProperty(x => x.IntegerField).Should().Be(field);
            field.Name.Should().Be("IntegerField");
            field.ValueType.Should().Be(typeof(int));
            field.IsReadOnly.Should().BeTrue();
            ((FieldInfo)field.MemberInfo).Name.Should().Be("IntegerField");
            field.GetValue(dto).Should().Be(dto.IntegerField);
            Invoking(() => field.SetValue(dto, 24)).Should().Throw <InvalidOperationException>();
        }
        public void StrongColor()
        {
            var info = DtoInfo.GetInfo <Color>();

            info.CreateNew().Should().Be(new Color(0, 0, 0));
            info.CreateNew(("r", (byte)1), ("g", (byte)2), ("b", (byte)3)).Should().Be(new Color(255, 1, 2, 3));
            info.CreateNew(("r", (byte)1), ("g", (byte)2), ("b", (byte)3), ("a", (byte)4)).Should().Be(new Color(4, 1, 2, 3));
        }
        public void WeakColor()
        {
            var info = DtoInfo.GetInfo(typeof(Color));

            info.CreateNew().Should().Be(new Color(0, 0, 0));
            info.CreateNew(("r", (byte)1), ("g", (byte)2), ("b", (byte)3)).Should().Be(new Color(255, 1, 2, 3));
            info.CreateNew(("r", (byte)1), ("g", (byte)2), ("b", (byte)3), ("a", (byte)4)).Should().Be(new Color(4, 1, 2, 3));
        }
        public void WeakNullablePoint()
        {
            var info = DtoInfo.GetInfo(typeof(Point?));

            info.CreateNew().Should().Be(default(Point));
            info.CreateNew(("x", 1)).Should().Be(new Point {
                X = 1
            });
        }
        public void StrongInitOnly()
        {
            var info = DtoInfo.GetInfo <InitOnlyDto>();
            var dto  = info.CreateNew(("a", 1), ("b", 2));

            dto.A.Should().Be(1);
            dto.B.Should().Be(2);
            info.ShallowClone(dto).A.Should().Be(1);
        }
        public void StrongNullablePoint()
        {
            var info = DtoInfo.GetInfo <Point?>();

            info.CreateNew().Should().Be(default(Point));
            info.CreateNew(("x", 1)).Should().Be(new Point {
                X = 1
            });
        }
        public static Assertable <TResponse> AssertResponse <TResponse>(this TResponse response)
            where TResponse : AutoWebServiceResponse
        {
            if (response == null)
            {
                throw new ArgumentNullException(nameof(response));
            }

            var exception        = response.CreateException();
            var statusCodeString = exception.ResponseStatusCode?.ToString();

            var properties = DtoInfo.GetInfo(typeof(TResponse))
                             .Properties
                             .Select(p => (p.Name, IsContent: IsContentProperty(p.Name), Value: p.GetValue(response)))
                             .Where(p => !IsDefault(p.Value))
                             .ToList();

            var assertable = AssertEx.HasValue(response, "response")
                             .Context(GetContext(exception, properties));

            if (properties.Any(p => p.IsContent && p.Value.GetType().IsClass))
            {
                assertable = assertable.WithExtrator(TryExtractStatusProperty);
            }

            return(assertable);

            bool TryExtractStatusProperty(
                LambdaExpression sourceExpression,
                out LambdaExpression hasValueExpression,
                out LambdaExpression remainingExpression)
            {
                hasValueExpression  = null;
                remainingExpression = null;

                var sourceParameter = sourceExpression.Parameters.Single();
                var visitor         = new MemberReplacingExpressionVisitor(sourceParameter, "response");
                var replacedBody    = visitor.Visit(sourceExpression.Body);

                if (!visitor.TryGetSingleClassParameter(out var memberInfo, out var responseParameter) || !IsContentProperty(memberInfo.Name))
                {
                    return(false);
                }

                hasValueExpression  = Expression.Lambda(Expression.MakeMemberAccess(sourceParameter, memberInfo), sourceParameter);
                remainingExpression = Expression.Lambda(replacedBody, responseParameter);

                return(true);
            }

            // Logic matches https://github.com/Faithlife/FaithlifeWebRequests/blob/5d04e85c62ae0ccea2ca7e45f5d40d650a7acd0b/src/Faithlife.WebRequests/Json/AutoWebServiceRequest.cs#L142
            bool IsContentProperty(string propertyName)
            => string.Equals(statusCodeString, propertyName, StringComparison.OrdinalIgnoreCase);
        }
        public void OnePropertyInfoTests()
        {
            DtoInfo <OneProperty> info = DtoInfo.GetInfo <OneProperty>();

            info.Properties.Count.Should().Be(1);
            info.CreateNew().GetType().Should().Be(typeof(OneProperty));

            OneProperty dto = new() { Integer = 42 };

            info.ShallowClone(dto).Integer.Should().Be(dto.Integer);
        }
        public void EmptyDtoTests()
        {
            DtoInfo <EmptyDto> info = DtoInfo.GetInfo <EmptyDto>();

            info.Properties.Should().BeEmpty();
            info.CreateNew().GetType().Should().Be(typeof(EmptyDto));
            info.ShallowClone(new EmptyDto()).Should().NotBeNull();
            Invoking(() => info.GetProperty("Nope")).Should().Throw <ArgumentException>();
            info.TryGetProperty("Nope").Should().BeNull();
            Invoking(() => info.GetProperty <int>("Nope")).Should().Throw <ArgumentException>();
            info.TryGetProperty <int>("Nope").Should().BeNull();
        }
        public void StrongMixedDto()
        {
            var info = DtoInfo.GetInfo <MixedDto>();

            info.CreateNew().Should().BeEquivalentTo(new MixedDto());
            info.CreateNew(("string", "hey")).Should().BeEquivalentTo(new MixedDto {
                String = "hey"
            });
            info.CreateNew(("integer", 1), ("string", "wow")).Should().BeEquivalentTo(new MixedDto(1)
            {
                String = "wow"
            });
        }
        public void WeakMixedDto()
        {
            IDtoInfo info = DtoInfo.GetInfo(typeof(MixedDto));

            info.CreateNew().Should().BeEquivalentTo(new MixedDto());
            info.CreateNew(("string", "hey")).Should().BeEquivalentTo(new MixedDto {
                String = "hey"
            });
            info.CreateNew(("integer", 1), ("string", "wow")).Should().BeEquivalentTo(new MixedDto(1)
            {
                String = "wow"
            });
        }
        public void OnePropertyWeakPropertyTests()
        {
            DtoInfo <OneProperty>      info     = DtoInfo.GetInfo <OneProperty>();
            IDtoProperty <OneProperty> property = info.GetProperty("Integer");

            property.Name.Should().Be("Integer");
            property.ValueType.Should().Be(typeof(int));
            property.IsReadOnly.Should().BeFalse();
            ((PropertyInfo)property.MemberInfo).GetMethod !.Name.Should().Be("get_Integer");

            OneProperty dto = new() { Integer = 42 };

            property.GetValue(dto).Should().Be(dto.Integer);
            property.SetValue(dto, 24);
            dto.Integer.Should().Be(24);
        }
        public void OneFieldWeakestFieldTests()
        {
            DtoInfo <OneField> info     = DtoInfo.GetInfo <OneField>();
            IDtoProperty       property = info.Properties.Single();

            property.Name.Should().Be("Integer");
            property.ValueType.Should().Be(typeof(int));
            property.IsReadOnly.Should().BeFalse();
            ((FieldInfo)property.MemberInfo).Name.Should().Be("Integer");

            OneField dto = new() { Integer = 42 };

            property.GetValue(dto).Should().Be(dto.Integer);
            property.SetValue(dto, 24);
            dto.Integer.Should().Be(24);
        }
        public void OnePropertyWeakInfoTests()
        {
            IDtoInfo info = DtoInfo.GetInfo(typeof(OneProperty));

            info.Properties.Count.Should().Be(1);
            info.CreateNew().GetType().Should().Be(typeof(OneProperty));

            OneProperty dto = new() { Integer = 42 };

            ((OneProperty)info.ShallowClone(dto)).Integer.Should().Be(dto.Integer);

            info.GetProperty("Integer").Name.Should().Be("Integer");
            info.TryGetProperty("Integer") !.Name.Should().Be("Integer");

            info.GetProperty("integer").Name.Should().Be("Integer");
            info.TryGetProperty("integer") !.Name.Should().Be("Integer");
        }
        public void WeakAnonymousType()
        {
            var obj  = new { Integer = 3, String = "three" };
            var info = DtoInfo.GetInfo(obj.GetType());

            info.Properties.Should().HaveCount(2);
            var property = info.GetProperty("Integer");

            property.IsReadOnly.Should().BeTrue();
            property.GetValue(obj).Should().Be(3);
            property.GetValue(info.ShallowClone(obj)).Should().Be(3);
            property.GetValue(info.CreateNew()).Should().Be(0);
            property.GetValue(info.CreateNew((property, 4))).Should().Be(4);
            property.GetValue(info.CreateNew(("integer", 4))).Should().Be(4);
            Invoking(() => info.CreateNew(("nope", 4))).Should().Throw <ArgumentException>();
            Invoking(() => info.CreateNew(("integer", "4"))).Should().Throw <ArgumentException>();
        }
        public void OneFieldStrongFieldTests()
        {
            DtoInfo <OneField>          info     = DtoInfo.GetInfo <OneField>();
            DtoProperty <OneField, int> property = info.GetProperty <int>("Integer");

            info.GetProperty(x => x.Integer).Should().Be(property);
            property.Name.Should().Be("Integer");
            property.ValueType.Should().Be(typeof(int));
            property.IsReadOnly.Should().BeFalse();
            ((FieldInfo)property.MemberInfo).Name.Should().Be("Integer");

            OneField dto = new() { Integer = 42 };

            property.GetValue(dto).Should().Be(dto.Integer);
            property.SetValue(dto, 24);
            dto.Integer.Should().Be(24);
        }
示例#17
0
 public void DtoInfoCreateNew()
 {
     m_dto = DtoInfo.GetInfo <BenchmarkDto>().CreateNew(("Id", 1L), ("Name", "one"));
 }
示例#18
0
 public void DtoInfoCreateNew()
 {
     m_dto = DtoInfo.GetInfo <BenchmarkDto>().CreateNew();
 }
        public void ShallowCloneThrowsOnNull()
        {
            DtoInfo <EmptyDto> info = DtoInfo.GetInfo <EmptyDto>();

            Invoking(() => info.ShallowClone(null !)).Should().Throw <ArgumentNullException>();
        }