Esempio n. 1
0
        private MethodCallResult CreateMethodCallResult(FakeObjectCall fakeObjectCall)
        {
            var result = new MethodCallResult(this.ResolveReturnValue(fakeObjectCall));

            this.AddOutAndRefValues(result, fakeObjectCall);
            return(result);
        }
Esempio n. 2
0
        private static MethodCall CreateMethodCallForGetter(FakeObjectCall fakeCall)
        {
            var methodName        = "get_" + fakeCall.Method.Name.Substring(4);
            var numberOfArguments = fakeCall.Arguments.Count() - 1;

            var arguments  = fakeCall.Arguments.Take(numberOfArguments);
            var parameters = fakeCall.Method.GetParameters().Take(numberOfArguments);

            return(new MethodCall(fakeCall.Method.DeclaringType, methodName, parameters, arguments));
        }
Esempio n. 3
0
        /// <summary>
        /// Applies an action to the call. If the method is not void, obtains a return value and sets it
        /// for the call. If there are any ref or out parameters, obtains values for them and sets them
        /// for the call.
        /// </summary>
        /// <param name="interceptedFakeObjectCall">The call to apply the rule to.</param>
        public void Apply(IInterceptedFakeObjectCall interceptedFakeObjectCall)
        {
            if (interceptedFakeObjectCall == null)
            {
                throw new ArgumentNullException(nameof(interceptedFakeObjectCall));
            }

            var fakeObjectCall = new FakeObjectCall(interceptedFakeObjectCall);
            var callResult     = this.resultSource.GetOrAdd(CreateMethodCall(fakeObjectCall), () => this.CreateMethodCallResult(fakeObjectCall));

            callResult.ApplyToCall(fakeObjectCall);
        }
Esempio n. 4
0
        /// <summary>
        /// Stores the value provided in the property setter to be returned from later
        /// calls to the corresponding getter.
        /// </summary>
        /// <param name="interceptedFakeObjectCall">The call to apply the rule to.</param>
        public void Apply(IInterceptedFakeObjectCall interceptedFakeObjectCall)
        {
            if (interceptedFakeObjectCall == null)
            {
                throw new ArgumentNullException(nameof(interceptedFakeObjectCall));
            }

            var fakeObjectCall = new FakeObjectCall(interceptedFakeObjectCall);
            var methodCall     = CreateMethodCallForGetter(fakeObjectCall);

            this.resultCache.Put(methodCall, new MethodCallResult(fakeObjectCall.Arguments.Last()));
        }
Esempio n. 5
0
        public void ApplyToCall(FakeObjectCall fakeObjectCall)
        {
            fakeObjectCall.SetReturnValue(this.returnValue);
            if (outAndRefValues == null)
            {
                return;
            }

            foreach (var positionedValue in this.outAndRefValues)
            {
                fakeObjectCall.SetArgumentValue(positionedValue.Position, positionedValue.Value);
            }
        }
Esempio n. 6
0
        private void AddOutAndRefValues(MethodCallResult result, FakeObjectCall fakeObjectCall)
        {
            var parameters = fakeObjectCall.Method.GetParameters();

            for (int i = 0; i < parameters.Length; i++)
            {
                var parameterParameterType = parameters[i].ParameterType;
                if (parameterParameterType.IsByRef)
                {
                    var value = this.context.Resolve(parameterParameterType.GetElementType());
                    result.AddOutOrRefValue(i, value);
                }
            }
        }
Esempio n. 7
0
        private object ResolveReturnValue(FakeObjectCall fakeObjectCall)
        {
            var methodReturnType = fakeObjectCall.Method.ReturnType;

            return(methodReturnType == typeof(void) ? null : this.context.Resolve(methodReturnType));
        }
Esempio n. 8
0
        private static MethodCall CreateMethodCall(FakeObjectCall fakeCall)
        {
            var parameters = fakeCall.Method.GetParameters();

            return(new MethodCall(fakeCall.Method.DeclaringType, fakeCall.Method.Name, parameters, fakeCall.Arguments));
        }