Exemple #1
0
        private void HandleMethodCallExpression(
            MethodCallExpression argumentMethodCallExpression,
            Expression argument)
        {
            var isMatchesMethod = argumentMethodCallExpression.Method.Equals(
                typeof(MocksanePredicates)
                .GetMethod(nameof(MocksanePredicates.Matches))?
                .MakeGenericMethod(argument.Type));

            if (isMatchesMethod)
            {
                if (argumentMethodCallExpression.Arguments.Count < 1)
                {
                    throw new InvalidOperationException(
                              "The matches method requires a parameter defining the predicate to use for the parameter.");
                }

                ParameterPredicates.Add(parameter =>
                {
                    var predicateBoundedType = typeof(Func <,>).MakeGenericType(argument.Type, typeof(bool));
                    var funcBoundedType      = typeof(Func <>).MakeGenericType(predicateBoundedType);
                    var compiledFunction     = Expression.Lambda(
                        funcBoundedType,
                        argumentMethodCallExpression.Arguments[0])
                                               .Compile();

                    var predicate    = compiledFunction.DynamicInvoke();
                    var invokeMethod = predicateBoundedType.GetMethod("Invoke");

                    if (invokeMethod == null)
                    {
                        return(false);
                    }

                    return((bool)invokeMethod.Invoke(predicate, new[] { parameter }));
                });

                return;
            }

            var isAnyMethod = argumentMethodCallExpression.Method.Equals(
                typeof(MocksanePredicates)
                .GetMethod(nameof(MocksanePredicates.Any))?
                .MakeGenericMethod(argument.Type));

            if (isAnyMethod)
            {
                ParameterPredicates.Add(parameter => true);
                return;
            }

            var methodBoundedType = typeof(Func <>).MakeGenericType(argument.Type);
            var compiledMethod    = Expression.Lambda(methodBoundedType, argumentMethodCallExpression).Compile();

            var returnValue = compiledMethod.DynamicInvoke();

            ParameterPredicates.Add(parameter => parameter == returnValue || parameter.Equals(returnValue));
        }
Exemple #2
0
 private void HandleMemberExpression(MemberExpression memberExpression)
 {
     ParameterPredicates.Add(parameter =>
     {
         var boundedType = typeof(Func <>).MakeGenericType(memberExpression.Type);
         var compiled    = Expression.Lambda(boundedType, memberExpression).Compile();
         var returnValue = compiled.DynamicInvoke();
         return(parameter == returnValue || parameter.Equals(returnValue));
     });
 }
Exemple #3
0
 private void HandleConstantExpression(ConstantExpression constantExpression)
 {
     ParameterPredicates.Add(parameter =>
                             parameter == constantExpression.Value || parameter.Equals(constantExpression.Value));
 }