private static ArgumentCollection CreateArgumentCollection <TFake>(TFake fake, LambdaExpression callSpecification) { var methodCall = callSpecification.Body as MethodCallExpression; MethodInfo method = null; object[] arguments = null; if (methodCall != null) { method = methodCall.Method; arguments = (from argument in methodCall.Arguments select ExpressionManager.GetValueProducedByExpression(argument)).ToArray(); } else { var propertyCall = callSpecification.Body as MemberExpression; var property = propertyCall.Member as PropertyInfo; method = property.GetGetMethod(); arguments = new object[] { }; } return(new ArgumentCollection(arguments, method)); }
private IEnumerable <object> GetConstructorArgumentsFromExpression(Expression <Func <T> > constructorCall) { if (constructorCall.Body.NodeType != ExpressionType.New) { throw new ArgumentException(ExceptionMessages.NonConstructorExpressionMessage); } var constructorArguments = from argument in ((NewExpression)constructorCall.Body).Arguments select ExpressionManager.GetValueProducedByExpression(argument); return(constructorArguments); }
private static IArgumentValidator GetArgumentValidator(Expression expressionArgument) { IArgumentValidator validator = null; if (!TryGetCustomArgumentValidator(expressionArgument, out validator)) { return(new EqualityArgumentValidator() { ValidArgument = ExpressionManager.GetValueProducedByExpression(expressionArgument) }); } return(validator); }
public static ArgumentValidator <Expression> ProducesValue(this ArgumentValidations <Expression> validations, object expectedValue) { return(ArgumentValidator <Expression> .Create(validations, x => object.Equals(expectedValue, ExpressionManager.GetValueProducedByExpression(x)), "Expression that produces the value {0}".FormatInvariant(expectedValue))); }
public bool IsValid(object argument) { var expression = argument as Expression; return(expression != null && object.Equals(expectedValue, ExpressionManager.GetValueProducedByExpression(expression))); }