コード例 #1
0
        public override Expression Apply(Expression expression, Random random)
        {
            var i    = random.Next(_expressionFinder.FoundExpressions.Count);
            var expr = _expressionFinder.FoundExpressions[i];

            var entityType  = expr.Type.GetGenericArguments()[0];
            var navigations = Context.Model.FindEntityType(entityType)?.GetNavigations().ToList();

            var prm = Expression.Parameter(entityType, "prm");

            if (navigations != null && navigations.Any())
            {
                var j          = random.Next(navigations.Count);
                var navigation = navigations[j];

                var includeMethod = IncludeMethodInfo.MakeGenericMethod(entityType, navigation.ClrType);

                var injector = new ExpressionInjector(
                    _expressionFinder.FoundExpressions[i],
                    e => Expression.Call(
                        includeMethod,
                        e,
                        Expression.Lambda(Expression.Property(prm, navigation.Name), prm)));

                return(injector.Visit(expression));
            }

            return(expression);
        }
コード例 #2
0
        public override Expression Apply(Expression expression, Random random)
        {
            var i           = random.Next(_expressionFinder.FoundExpressions.Count);
            var expr        = _expressionFinder.FoundExpressions[i];
            var thenInclude = random.Next(3) > 0;

            var entityType          = expr.Type.GetGenericArguments()[0];
            var propertyType        = expr.Type.GetGenericArguments()[1];
            var propertyElementType = IsEnumerableType(propertyType) || propertyType.GetInterfaces().Any(ii => IsEnumerableType(ii))
                ? propertyType.GetGenericArguments()[0]
                : propertyType;

            var navigations = thenInclude
                ? Context.Model.FindEntityType(propertyElementType)?.GetNavigations().ToList()
                : Context.Model.FindEntityType(entityType)?.GetNavigations().ToList();

            var prm = thenInclude
                ? Expression.Parameter(propertyElementType, "prm")
                : Expression.Parameter(entityType, "prm");

            if (navigations != null &&
                navigations.Any())
            {
                var j          = random.Next(navigations.Count);
                var navigation = navigations[j];

                if (thenInclude)
                {
                    var thenIncludeMethod = IsEnumerableType(propertyType) || propertyType.GetInterfaces().Any(ii => IsEnumerableType(ii))
                        ? ThenIncludeCollectionMethodInfo.MakeGenericMethod(entityType, propertyElementType, navigation.ClrType)
                        : ThenIncludeReferenceMethodInfo.MakeGenericMethod(entityType, propertyElementType, navigation.ClrType);

                    var injector = new ExpressionInjector(
                        _expressionFinder.FoundExpressions[i],
                        e => Expression.Call(
                            thenIncludeMethod,
                            e,
                            Expression.Lambda(Expression.Property(prm, navigation.Name), prm)));

                    return(injector.Visit(expression));
                }
                else
                {
                    var includeMethod = IncludeMethodInfo.MakeGenericMethod(entityType, navigation.ClrType);

                    var injector = new ExpressionInjector(
                        _expressionFinder.FoundExpressions[i],
                        e => Expression.Call(
                            includeMethod,
                            e,
                            Expression.Lambda(Expression.Property(prm, navigation.Name), prm)));

                    return(injector.Visit(expression));
                }
            }

            return(expression);
        }
コード例 #3
0
        public override Expression Apply(Expression expression, Random random)
        {
            var i           = random.Next(_expressionFinder.FoundExpressions.Count);
            var methodNames = new[] { nameof(string.ToLower), nameof(string.ToUpper), nameof(string.Trim) };

            var methodInfos = methodNames.Select(n => typeof(string).GetRuntimeMethod(n, new Type[] { })).ToList();
            var methodInfo  = methodInfos[random.Next(methodInfos.Count)];

            var injector = new ExpressionInjector(_expressionFinder.FoundExpressions[i], e => Expression.Call(e, methodInfo));

            return(injector.Visit(expression));
        }
コード例 #4
0
        public override Expression Apply(Expression expression, Random random)
        {
            var i = random.Next(_expressionFinder.FoundExpressions.Count);

            var stringConcatMethodInfo
                = typeof(string).GetRuntimeMethod(
                      nameof(string.Concat),
                      new[] { typeof(string), typeof(string) });

            var injector = new ExpressionInjector(_expressionFinder.FoundExpressions[i], e => Expression.Add(e, e, stringConcatMethodInfo));

            return(injector.Visit(expression));
        }
        public override Expression Apply(Expression expression, Random random)
        {
            var i = random.Next(_expressionFinder.FoundExpressions.Count);

            var injector = new ExpressionInjector(
                _expressionFinder.FoundExpressions[i],
                e => Expression.Convert(
                    Expression.Coalesce(
                        e,
                        Expression.Default(e.Type.GetGenericArguments()[0])),
                    e.Type));

            return(injector.Visit(expression));
        }
コード例 #6
0
        public override Expression Apply(Expression expression, Random random)
        {
            var i = random.Next(_expressionFinder.FoundExpressions.Count);
            var expressionToInject = _expressionFinder.FoundExpressions.ToList()[i].Key;
            var j        = random.Next(_expressionFinder.FoundExpressions[expressionToInject].Count);
            var property = _expressionFinder.FoundExpressions[expressionToInject][j];

            var typeArgument = expressionToInject.Type.GetGenericArguments()[0];

            var isDescending = random.Next(3) == 0;
            var orderBy      = isDescending
                ? OrderByDescendingMethodInfo.MakeGenericMethod(typeArgument, property.PropertyType)
                : OrderByMethodInfo.MakeGenericMethod(typeArgument, property.PropertyType);

            var prm        = Expression.Parameter(typeArgument, "prm");
            var lambdaBody = (Expression)Expression.Property(prm, property);

            if (property.PropertyType.IsValueType &&
                !(property.PropertyType.IsGenericType && property.PropertyType.GetGenericTypeDefinition() == typeof(Nullable <>)))
            {
                var nullablePropertyType = typeof(Nullable <>).MakeGenericType(property.PropertyType);

                orderBy = isDescending
                    ? OrderByDescendingMethodInfo.MakeGenericMethod(typeArgument, nullablePropertyType)
                    : OrderByMethodInfo.MakeGenericMethod(typeArgument, nullablePropertyType);

                lambdaBody = Expression.Convert(lambdaBody, nullablePropertyType);
            }

            if (typeArgument == typeof(string))
            {
                // string.Length - make it nullable in case we access optional argument
                orderBy    = OrderByMethodInfo.MakeGenericMethod(typeArgument, typeof(int?));
                lambdaBody = Expression.Convert(lambdaBody, typeof(int?));
            }

            var lambda   = Expression.Lambda(lambdaBody, prm);
            var injector = new ExpressionInjector(expressionToInject, e => Expression.Call(orderBy, e, lambda));

            return(injector.Visit(expression));
        }
コード例 #7
0
        public override Expression Apply(Expression expression, Random random)
        {
            var i = random.Next(_expressionFinder.FoundExpressions.Count);

            var expr        = _expressionFinder.FoundExpressions[i];
            var elementType = expr.Type.GetGenericArguments()[0];

            var join = QueryableMethods.Join.MakeGenericMethod(elementType, elementType, elementType, elementType);

            var outerKeySelectorPrm = Expression.Parameter(elementType, "oks");
            var innerKeySelectorPrm = Expression.Parameter(elementType, "iks");

            var injector = new ExpressionInjector(
                _expressionFinder.FoundExpressions[i],
                e => Expression.Call(
                    join,
                    e,
                    e,
                    Expression.Lambda(outerKeySelectorPrm, outerKeySelectorPrm),
                    Expression.Lambda(innerKeySelectorPrm, innerKeySelectorPrm),
                    Expression.Lambda(outerKeySelectorPrm, outerKeySelectorPrm, innerKeySelectorPrm)));

            return(injector.Visit(expression));
        }
コード例 #8
0
        public override Expression Apply(Expression expression, Random random)
        {
            var i = random.Next(_expressionFinder.FoundExpressions.Count);
            var expressionToInject = _expressionFinder.FoundExpressions[i];

            var typeArgument = expressionToInject.Type.GetGenericArguments()[0];
            var prm          = Expression.Parameter(typeArgument, "prm");

            var candidateExpressions = new List <Expression>
            {
                Expression.Constant(random.Choose(new List <bool> {
                    true, false
                })),
            };

            if (typeArgument == typeof(bool))
            {
                candidateExpressions.Add(prm);
            }

            var properties = typeArgument.GetProperties().Where(p => !p.GetMethod.IsStatic).ToList();

            properties = FilterPropertyInfos(typeArgument, properties);

            var boolProperties = properties.Where(p => p.PropertyType == typeof(bool)).ToList();

            if (boolProperties.Any())
            {
                candidateExpressions.Add(Expression.Property(prm, random.Choose(boolProperties)));
            }

            // compare two properties
            var propertiesOfTheSameType = properties.GroupBy(p => p.PropertyType).Where(g => g.Count() > 1).ToList();

            if (propertiesOfTheSameType.Any())
            {
                var propertyGroup = random.Choose(propertiesOfTheSameType).ToList();

                var firstProperty  = random.Choose(propertyGroup);
                var secondProperty = random.Choose(propertyGroup.Where(p => p != firstProperty).ToList());

                candidateExpressions.Add(Expression.NotEqual(Expression.Property(prm, firstProperty), Expression.Property(prm, secondProperty)));
            }

            // compare property to constant
            if (properties.Any())
            {
                var property = random.Choose(properties);
                candidateExpressions.Add(
                    Expression.NotEqual(
                        Expression.Property(prm, property),
                        Expression.Default(property.PropertyType)));
            }

            if (IsEntityType(typeArgument))
            {
                var entityType            = Context.Model.FindEntityType(typeArgument);
                var navigations           = entityType.GetNavigations().ToList();
                var collectionNavigations = navigations.Where(n => n.IsCollection()).ToList();

                var collectionNavigation = random.Choose(collectionNavigations);
                if (collectionNavigation != null)
                {
                    var any = EnumerableAnyMethodInfo.MakeGenericMethod(collectionNavigation.ForeignKey.DeclaringEntityType.ClrType);

                    // collection.Any()
                    candidateExpressions.Add(
                        Expression.Call(
                            any,
                            Expression.Property(prm, collectionNavigation.PropertyInfo)));
                }

                var navigation = random.Choose(navigations);
            }

            var lambdaBody = random.Choose(candidateExpressions);

            var negated = random.Next(6) > 3;

            if (negated)
            {
                lambdaBody = Expression.Not(lambdaBody);
            }

            var where = WhereMethodInfo.MakeGenericMethod(typeArgument);
            var lambda   = Expression.Lambda(lambdaBody, prm);
            var injector = new ExpressionInjector(expressionToInject, e => Expression.Call(where, e, lambda));

            return(injector.Visit(expression));
        }