public void Apply(IInterceptedFakeObjectCall fakeObjectCall)
        {
            Guard.AgainstNull(fakeObjectCall, "fakeObjectCall");

            var returnValue = ResolveReturnValue(fakeObjectCall);
            fakeObjectCall.SetReturnValue(returnValue);
        }
Beispiel #2
0
            public void Apply(IInterceptedFakeObjectCall fakeObjectCall)
            {
                Guard.AgainstNull(fakeObjectCall, nameof(fakeObjectCall));

                var returnType = fakeObjectCall.Method.ReturnType;

                if (typeof(Task).GetTypeInfo().IsAssignableFrom(returnType))
                {
                    Task task;
                    if (returnType == typeof(Task))
                    {
                        task = TaskHelper.Canceled();
                    }
                    else
                    {
                        var taskResultType = returnType.GetTypeInfo().GetGenericArguments()[0];
                        task = TaskHelper.Canceled(taskResultType);
                    }

                    fakeObjectCall.SetReturnValue(task);
                }
                else
                {
                    var token = GetCanceledTokens(fakeObjectCall).First();
                    token.ThrowIfCancellationRequested();
                }
            }
            public void Apply(IInterceptedFakeObjectCall fakeObjectCall)
            {
                Guard.AgainstNull(fakeObjectCall, nameof(fakeObjectCall));

                var returnType = fakeObjectCall.Method.ReturnType;
                if (typeof(Task).GetTypeInfo().IsAssignableFrom(returnType))
                {
                    Task task;
                    if (returnType == typeof(Task))
                    {
                        task = TaskHelper.Canceled();
                    }
                    else
                    {
                        var taskResultType = returnType.GetTypeInfo().GetGenericArguments()[0];
                        task = TaskHelper.Canceled(taskResultType);
                    }

                    fakeObjectCall.SetReturnValue(task);
                }
                else
                {
                    var token = GetCanceledTokens(fakeObjectCall).First();
                    token.ThrowIfCancellationRequested();
                }
            }
Beispiel #4
0
        /// <summary>
        /// Applies an action to the call, might set a return value or throw
        /// an exception.
        /// </summary>
        /// <param name="fakeObjectCall">The call to apply the interceptor to.</param>
        public void Apply(IInterceptedFakeObjectCall fakeObjectCall)
        {
            var parameters = fakeObjectCall.Arguments.GetUnderlyingArgumentsArray();
            var valueFromWrappedInstance = fakeObjectCall.Method.Invoke(this.wrappedObject, parameters);

            fakeObjectCall.SetReturnValue(valueFromWrappedInstance);
        }
        public void Apply(IInterceptedFakeObjectCall fakeObjectCall)
        {
            Guard.AgainstNull(fakeObjectCall, "fakeObjectCall");

            var returnValue = ResolveReturnValue(fakeObjectCall);

            fakeObjectCall.SetReturnValue(returnValue);
        }
        /// <summary>
        /// Applies an action to the call, might set a return value or throw
        /// an exception.
        /// </summary>
        /// <param name="fakeObjectCall">The call to apply the interceptor to.</param>
        public void Apply(IInterceptedFakeObjectCall fakeObjectCall)
        {
            Guard.AgainstNull(fakeObjectCall, "fakeObjectCall");

            var parameters = fakeObjectCall.Arguments.GetUnderlyingArgumentsArray();
            var valueFromWrappedInstance = fakeObjectCall.Method.Invoke(this.wrappedObject, parameters);
            fakeObjectCall.SetReturnValue(valueFromWrappedInstance);
        }
            public override void Apply(IInterceptedFakeObjectCall fakeObjectCall)
            {
                Guard.AgainstNull(fakeObjectCall, nameof(fakeObjectCall));

                var returnType = fakeObjectCall.Method.ReturnType;

                if (typeof(Task).GetTypeInfo().IsAssignableFrom(returnType))
                {
                    Task task;
                    if (returnType == typeof(Task))
                    {
                        task = TaskHelper.Canceled();
                    }
                    else
                    {
                        var taskResultType = returnType.GetTypeInfo().GetGenericArguments()[0];
                        task = TaskHelper.Canceled(taskResultType);
                    }

                    fakeObjectCall.SetReturnValue(task);
                }
                else if (IsValueTask(returnType, out var taskResultType))
                {
                    if (taskResultType == null)
                    {
                        var canceledTask = TaskHelper.Canceled();
                        var ctor         = returnType.GetConstructor(new[] { typeof(Task) });
                        var valueTask    = ctor.Invoke(new object[] { canceledTask });
                        fakeObjectCall.SetReturnValue(valueTask);
                    }
                    else
                    {
                        var canceledTask = TaskHelper.Canceled(taskResultType);
                        var ctor         = returnType.GetConstructor(new[] { canceledTask.GetType() });
                        var valueTask    = ctor.Invoke(new object[] { canceledTask });
                        fakeObjectCall.SetReturnValue(valueTask);
                    }
                }
                else
                {
                    GetCanceledToken(fakeObjectCall)?.ThrowIfCancellationRequested();
                }
            }
            private static bool TryHandleToString(IInterceptedFakeObjectCall fakeObjectCall, FakeManager fakeManager)
            {
                if (!IsSameMethod(fakeObjectCall.Method, ToStringMethod))
                {
                    return(false);
                }

                fakeObjectCall.SetReturnValue("Faked " + fakeManager.FakeObjectType);

                return(true);
            }
Beispiel #9
0
            private bool TryHandleGetHashCode(IInterceptedFakeObjectCall fakeObjectCall)
            {
                if (!IsSameMethod(fakeObjectCall.Method, ObjectMethods[2]))
                {
                    return(false);
                }

                fakeObjectCall.SetReturnValue(this.fakeManager.GetHashCode());

                return(true);
            }
            private bool TryHandleToString(IInterceptedFakeObjectCall fakeObjectCall)
            {
                if (!fakeObjectCall.Method.MethodHandle.Equals(ObjectMethodsMethodHandles[1]))
                {
                    return(false);
                }

                fakeObjectCall.SetReturnValue("Faked {0}".FormatInvariant(this.FakeManager.FakeObjectType.FullName));

                return(true);
            }
            private bool TryHandleToString(IInterceptedFakeObjectCall fakeObjectCall)
            {
                if (!fakeObjectCall.Method.MethodHandle.Equals(ObjectMethodsMethodHandles[1]))
                {
                    return false;
                }

                fakeObjectCall.SetReturnValue("Faked {0}".FormatInvariant(this.FakeManager.FakeObjectType.FullName));

                return true;
            }
            private bool TryHandleEquals(IInterceptedFakeObjectCall fakeObjectCall)
            {
                if (!fakeObjectCall.Method.MethodHandle.Equals(ObjectMethodsMethodHandles[0]))
                {
                    return(false);
                }

                var argument = fakeObjectCall.Arguments[0] as ITaggable;

                if (argument != null)
                {
                    fakeObjectCall.SetReturnValue(argument.Tag.Equals(this.FakeManager));
                }
                else
                {
                    fakeObjectCall.SetReturnValue(false);
                }

                return(true);
            }
            private bool TryHandleGetHashCode(IInterceptedFakeObjectCall fakeObjectCall)
            {
                if (!fakeObjectCall.Method.MethodHandle.Equals(ObjectMethodsMethodHandles[2]))
                {
                    return false;
                }

                fakeObjectCall.SetReturnValue(this.FakeManager.GetHashCode());

                return true;
            }
            private static bool TryHandleGetHashCode(IInterceptedFakeObjectCall fakeObjectCall, FakeManager fakeManager)
            {
                if (!fakeObjectCall.Method.HasSameBaseMethodAs(GetHashCodeMethod))
                {
                    return(false);
                }

                fakeObjectCall.SetReturnValue(fakeManager.GetHashCode());

                return(true);
            }
            private bool TryHandleGetHashCode(IInterceptedFakeObjectCall fakeObjectCall)
            {
                if (!fakeObjectCall.Method.MethodHandle.Equals(ObjectMethodsMethodHandles[2]))
                {
                    return(false);
                }

                fakeObjectCall.SetReturnValue(this.FakeManager.GetHashCode());

                return(true);
            }
            private bool TryHandleToString(IInterceptedFakeObjectCall fakeObjectCall)
            {
                if (!IsSameMethod(fakeObjectCall.Method, ObjectMethods[1]))
                {
                    return(false);
                }

                fakeObjectCall.SetReturnValue("Faked " + this.fakeManager.FakeObjectType);

                return(true);
            }
            private static bool TryHandleToString(IInterceptedFakeObjectCall fakeObjectCall, FakeManager fakeManager)
            {
                if (!fakeObjectCall.Method.HasSameBaseMethodAs(ToStringMethod))
                {
                    return(false);
                }

                fakeObjectCall.SetReturnValue(fakeManager.FakeObjectDisplayName);

                return(true);
            }
            private bool TryHandleGetHashCode(IInterceptedFakeObjectCall fakeObjectCall)
            {
                if (!IsSameMethod(fakeObjectCall.Method, ObjectMethods[2]))
                {
                    return false;
                }

                fakeObjectCall.SetReturnValue(this.fakeManager.GetHashCode());

                return true;
            }
Beispiel #19
0
        /// <summary>
        /// Applies the call if the call has been recorded.
        /// </summary>
        /// <param name="fakeObjectCall">The call to apply to from recording.</param>
        public void ApplyNext(IInterceptedFakeObjectCall fakeObjectCall)
        {
            this.AssertThatCallQueueIsNotEmpty();

            var callToApply = this.callQueue.Dequeue();

            AssertThatMethodsMatches(fakeObjectCall, callToApply);
            ApplyOutputArguments(fakeObjectCall, callToApply);

            fakeObjectCall.SetReturnValue(callToApply.RecordedCall.ReturnValue);
            callToApply.HasBeenApplied = true;
        }
Beispiel #20
0
        /// <summary>
        /// Applies the call if the call has been recorded.
        /// </summary>
        /// <param name="fakeObjectCall">The call to apply to from recording.</param>
        public void ApplyNext(IInterceptedFakeObjectCall fakeObjectCall)
        {
            this.AssertThatCallQueueIsNotEmpty();

            var callToApply = this.callQueue.Dequeue();

            AssertThatMethodsMatches(fakeObjectCall, callToApply);
            ApplyOutputArguments(fakeObjectCall, callToApply);

            fakeObjectCall.SetReturnValue(callToApply.RecordedCall.ReturnValue);
            callToApply.HasBeenApplied = true;
        }
            private static bool TryHandleEquals(IInterceptedFakeObjectCall fakeObjectCall, FakeManager fakeManager)
            {
                if (!IsSameMethod(fakeObjectCall.Method, EqualsMethod))
                {
                    return(false);
                }

                var argument = fakeObjectCall.Arguments[0];

                if (argument is object)
                {
                    var  argumentFakeManager = Fake.TryGetFakeManager(argument);
                    bool hasSameFakeManager  = ReferenceEquals(argumentFakeManager, fakeManager);
                    fakeObjectCall.SetReturnValue(hasSameFakeManager);
                }
                else
                {
                    fakeObjectCall.SetReturnValue(false);
                }

                return(true);
            }
            private static bool TryHandleEquals(IInterceptedFakeObjectCall fakeObjectCall, FakeManager fakeManager)
            {
                if (!fakeObjectCall.Method.HasSameBaseMethodAs(EqualsMethod))
                {
                    return(false);
                }

                var argument = fakeObjectCall.Arguments[0];

                if (argument is not null)
                {
                    Fake.TryGetFakeManager(argument, out var argumentFakeManager);
                    bool hasSameFakeManager = ReferenceEquals(argumentFakeManager, fakeManager);
                    fakeObjectCall.SetReturnValue(hasSameFakeManager);
                }
                else
                {
                    fakeObjectCall.SetReturnValue(false);
                }

                return(true);
            }
            private bool TryHandleEquals(IInterceptedFakeObjectCall fakeObjectCall)
            {
                if (!IsSameMethod(fakeObjectCall.Method, ObjectMethods[0]))
                {
                    return(false);
                }

                var argument = fakeObjectCall.Arguments[0];

                if (argument != null)
                {
                    var  argumentFakeManager = this.fakeManagerAccessor.TryGetFakeManager(argument);
                    bool hasSameFakeManager  = ReferenceEquals(argumentFakeManager, this.fakeManager);
                    fakeObjectCall.SetReturnValue(hasSameFakeManager);
                }
                else
                {
                    fakeObjectCall.SetReturnValue(false);
                }

                return(true);
            }
            public void Apply(IInterceptedFakeObjectCall fakeObjectCall)
            {
                if (this.IsPropertyGetter(fakeObjectCall))
                {
                    fakeObjectCall.SetReturnValue(this.Value);
                }
                else
                {
                    this.Value = fakeObjectCall.Arguments[0];
                }

                this.fakeManager.MoveRuleToFront(this);
            }
Beispiel #25
0
            public void Apply(IInterceptedFakeObjectCall fakeObjectCall)
            {
                if (this.IsPropertyGetter(fakeObjectCall))
                {
                    fakeObjectCall.SetReturnValue(this.Value);
                }
                else
                {
                    this.Value = fakeObjectCall.Arguments[0];
                }

                this.fakeManager.MoveRuleToFront(this);
            }
            public void Apply(IInterceptedFakeObjectCall fakeObjectCall)
            {
                Guard.AgainstNull(fakeObjectCall, "fakeObjectCall");

                if (this.IsPropertyGetter(fakeObjectCall))
                {
                    fakeObjectCall.SetReturnValue(this.Value);
                }
                else
                {
                    this.Value = fakeObjectCall.Arguments.Last();
                }

                this.fakeManager.MoveRuleToFront(this);
            }
Beispiel #27
0
            public void Apply(IInterceptedFakeObjectCall fakeObjectCall)
            {
                Guard.AgainstNull(fakeObjectCall, "fakeObjectCall");

                if (this.IsPropertyGetter(fakeObjectCall))
                {
                    fakeObjectCall.SetReturnValue(this.Value);
                }
                else
                {
                    this.Value = fakeObjectCall.Arguments.Last();
                }

                this.fakeManager.MoveRuleToFront(this);
            }
Beispiel #28
0
        /// <summary>
        /// Applies the call if the call has been recorded.
        /// </summary>
        /// <param name="fakeObjectCall">The call to apply to from recording.</param>
        public void ApplyNext(IInterceptedFakeObjectCall fakeObjectCall)
        {
            var callToApply = this.recordedCalls.GetResponseForCall(fakeObjectCall);

            if (callToApply == null)
            {
                throw new Exception("Nenhum item gravado disponível para a operação");
            }

            // AssertThatMethodsMatches(fakeObjectCall, callToApply);
            ApplyOutputArguments(fakeObjectCall, callToApply);

            fakeObjectCall.SetReturnValue(callToApply.RecordedCall.ReturnValue);
            callToApply.HasBeenApplied = true;
        }
        private static void ApplyRecordedCall(RecordedCall recordedCall, IInterceptedFakeObjectCall fakeObjectCall)
        {
            fakeObjectCall.SetReturnValue(recordedCall.ReturnValue);

            int outAndRefIndex = 0;
            int parameterIndex = 0;

            foreach (var parameter in fakeObjectCall.Method.GetParameters())
            {
                if (parameter.ParameterType.IsByRef)
                {
                    fakeObjectCall.SetArgumentValue(parameterIndex, recordedCall.OutAndRefValues[outAndRefIndex++]);
                }

                ++parameterIndex;
            }
        }
        public static void CallWrappedMethod(this IInterceptedFakeObjectCall fakeObjectCall, object wrappedObject)
        {
            Guard.AgainstNull(fakeObjectCall, nameof(fakeObjectCall));
            Guard.AgainstNull(wrappedObject, nameof(wrappedObject));

            var    parameters = fakeObjectCall.Arguments.GetUnderlyingArgumentsArray();
            object?returnValue;

            try
            {
                if (fakeObjectCall.Method.IsSameMethodAs(EqualsMethod))
                {
                    var arg = parameters[0];
                    if (ReferenceEquals(arg, fakeObjectCall.FakedObject))
                    {
                        // fake.Equals(fake) returns true
                        returnValue = true;
                    }
                    else if (ReferenceEquals(arg, wrappedObject))
                    {
                        // fake.Equals(wrappedObject) returns wrappedObject.Equals(fake)
                        // This will be false if Equals isn't overriden (reference equality)
                        // and true if Equals is overriden to implement value semantics.
                        // This approach has the benefit of keeping Equals symmetrical.
                        returnValue = wrappedObject.Equals(fakeObjectCall.FakedObject);
                    }
                    else
                    {
                        // fake.Equals(somethingElse) is delegated to the wrapped object (no special case)
                        returnValue = wrappedObject.Equals(arg);
                    }
                }
                else
                {
                    returnValue = fakeObjectCall.Method.Invoke(wrappedObject, parameters);
                }
            }
            catch (TargetInvocationException ex)
            {
                ex.InnerException?.Rethrow();
                throw;
            }

            fakeObjectCall.SetReturnValue(returnValue);
        }
        /// <summary>
        /// Applies an action to the call, might set a return value or throw
        /// an exception.
        /// </summary>
        /// <param name="fakeObjectCall">The call to apply the interceptor to.</param>
        public void Apply(IInterceptedFakeObjectCall fakeObjectCall)
        {
            Guard.AgainstNull(fakeObjectCall, nameof(fakeObjectCall));

            var parameters = fakeObjectCall.Arguments.GetUnderlyingArgumentsArray();
            object valueFromWrappedInstance;
            try
            {
                valueFromWrappedInstance = fakeObjectCall.Method.Invoke(this.wrappedObject, parameters);
            }
            catch (TargetInvocationException ex)
            {
                ex.InnerException?.Rethrow();
                throw;
            }

            fakeObjectCall.SetReturnValue(valueFromWrappedInstance);
        }
        /// <summary>
        /// Applies an action to the call, might set a return value or throw
        /// an exception.
        /// </summary>
        /// <param name="fakeObjectCall">The call to apply the interceptor to.</param>
        public void Apply(IInterceptedFakeObjectCall fakeObjectCall)
        {
            Guard.AgainstNull(fakeObjectCall, nameof(fakeObjectCall));

            var    parameters = fakeObjectCall.Arguments.GetUnderlyingArgumentsArray();
            object valueFromWrappedInstance;

            try
            {
                valueFromWrappedInstance = fakeObjectCall.Method.Invoke(this.wrappedObject, parameters);
            }
            catch (TargetInvocationException ex)
            {
                ex.InnerException?.Rethrow();
                throw;
            }

            fakeObjectCall.SetReturnValue(valueFromWrappedInstance);
        }
Beispiel #33
0
        public void Apply(IInterceptedFakeObjectCall fakeObjectCall)
        {
            fakeObjectCall.DoNotRecordCall();

            this.recordedRule.ApplicableToMethod = fakeObjectCall.Method;

            if (this.recordedRule.IsApplicableToArguments == null)
            {
                this.CreateArgumentsPredicateFromArguments(fakeObjectCall);
            }

            if (this.recordedRule.IsAssertion)
            {
                this.DoAssertion(fakeObjectCall);
            }

            this.fakeManager.AddRuleFirst(this.recordedRule);

            fakeObjectCall.SetReturnValue(Helpers.GetDefaultValueOfType(fakeObjectCall.Method.ReturnType));
        }
        public void Apply(IInterceptedFakeObjectCall fakeObjectCall)
        {
            var returnValue = ResolveReturnValue(fakeObjectCall);

            fakeObjectCall.SetReturnValue(returnValue);
        }
        public void Apply(IInterceptedFakeObjectCall fakeObjectCall)
        {
            Guard.AgainstNull(fakeObjectCall, nameof(fakeObjectCall));

            fakeObjectCall.SetReturnValue(fakeObjectCall.GetDefaultReturnValue());
        }
Beispiel #36
0
            public override void Apply(IInterceptedFakeObjectCall fakeObjectCall)
            {
                Guard.AgainstNull(fakeObjectCall, nameof(fakeObjectCall));

                fakeObjectCall.SetReturnValue(fakeObjectCall.GetDefaultReturnValue());
            }
            private bool TryHandleEquals(IInterceptedFakeObjectCall fakeObjectCall)
            {
                if (!fakeObjectCall.Method.MethodHandle.Equals(ObjectMethodsMethodHandles[0]))
                {
                    return false;
                }

                var argument = fakeObjectCall.Arguments[0] as ITaggable;

                if (argument != null)
                {
                    fakeObjectCall.SetReturnValue(argument.Tag.Equals(this.FakeManager));
                }
                else
                {
                    fakeObjectCall.SetReturnValue(false);
                }

                return true;
            }
 private static void SetReturnValue(IInterceptedFakeObjectCall fakeObjectCall, RecordedCall recordedCall)
 {
     fakeObjectCall.SetReturnValue(recordedCall.ReturnValue);
 }
            public void Apply(IInterceptedFakeObjectCall fakeObjectCall)
            {
                Guard.AgainstNull(fakeObjectCall, nameof(fakeObjectCall));

                fakeObjectCall.SetReturnValue(this.Value);
            }
Beispiel #40
0
        public void Apply(IInterceptedFakeObjectCall fakeObjectCall)
        {
            var returnValue = ResolveReturnValue(fakeObjectCall);

            fakeObjectCall.SetReturnValue(returnValue);
        }