示例#1
0
        /// <summary>
        /// Gets the fake object an expression is called on.
        /// </summary>
        /// <param name="fakeObjectCall">The call expression.</param>
        /// <returns>A FakeObject.</returns>
        /// <exception cref="ArgumentNullException">The fakeObjectCall is null.</exception>
        /// <exception cref="ArgumentException">The specified expression is not an expression where a call is made to a faked object.</exception>
        public FakeObject GetFakeObjectCallIsMadeOn(LambdaExpression fakeObjectCall)
        {
            Guard.IsNotNull(fakeObjectCall, "fakeObjectCall");

            Expression callTargetExpression = null;

            var methodExpression = fakeObjectCall.Body as MethodCallExpression;

            if (methodExpression != null)
            {
                callTargetExpression = methodExpression.Object;
            }
            else
            {
                var propertyExpression = fakeObjectCall.Body as MemberExpression;
                callTargetExpression = propertyExpression.Expression;
            }

            if (callTargetExpression == null)
            {
                throw new ArgumentException("The specified call is not made on a fake object.");
            }

            return(Fake.GetFakeObject(ExpressionManager.GetValueProducedByExpression(callTargetExpression)));
        }
示例#2
0
 private static object[] GetArgumentsForConstructor(MethodCallExpression methodExpression)
 {
     return
         ((from argumentAndType in methodExpression.Arguments.Pairwise(methodExpression.Method.GetParameters())
           select new { Value = ExpressionManager.GetValueProducedByExpression(argumentAndType.First), Type = argumentAndType.Second.ParameterType })
          .SkipWhile(x => x.Type.Equals(typeof(IExtensibleIs)))
          .Select(x => x.Value)
          .ToArray());
 }
        /// <summary>
        /// Gets an argument validator for the argument represented by the expression.
        /// </summary>
        /// <param name="argument">The argument.</param>
        /// <returns>An IArgumentValidator used to validated arguments in IFakeObjectCalls.</returns>
        public virtual IArgumentValidator GetArgumentValidator(Expression argument)
        {
            IArgumentValidator result = null;

            if (!TryGetArgumentValidator(argument, out result))
            {
                result = new EqualityArgumentValidator(ExpressionManager.GetValueProducedByExpression(argument));
            }

            return(result);
        }
        private static bool TryGetAbstractValidator(Expression argument, out IArgumentValidator result)
        {
            var unary = argument as UnaryExpression;

            if (unary != null && IsArgumentValidatorConversionMethod(unary.Method))
            {
                result = ExpressionManager.GetValueProducedByExpression(unary.Operand) as IArgumentValidator;
                return(true);
            }

            var member = argument as MemberExpression;

            if (member != null && IsArgumentValidatorArgumentProperty(member))
            {
                result = ExpressionManager.GetValueProducedByExpression(member.Expression) as IArgumentValidator;
                return(true);
            }

            result = ExpressionManager.GetValueProducedByExpression(argument) as IArgumentValidator;
            return(result != null);
        }