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

            var returnValue = ResolveReturnValue(fakeObjectCall);
            fakeObjectCall.SetReturnValue(returnValue);
        }
            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();
                }
            }
        /// <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);
        }
        void IFakeObjectCallRule.Apply(IInterceptedFakeObjectCall invocation)
        {
            this.ApplyWasCalled = true;

            if (this.Apply != null)
            {
                this.Apply(invocation);
            }
        }
        public static object ResolveReturnValue(IInterceptedFakeObjectCall fakeObjectCall)
        {
            object result;
            if (!FakeManager.TryCreateDummy(fakeObjectCall.Method.ReturnType, out result))
            {
                return fakeObjectCall.Method.ReturnType.GetDefaultValue();
            }

            return result;
        }
            public void Apply(IInterceptedFakeObjectCall fakeObjectCall)
            {
                var newRule = new CallRuleMetadata
                                  {
                                      CalledNumberOfTimes = 1,
                                      Rule = new PropertyBehaviorRule(fakeObjectCall.Method, this.FakeManager) { Value = fakeObjectCall.Arguments[0] }
                                  };

                this.FakeManager.allUserRulesField.AddFirst(newRule);
            }
            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;
            }
            public void Apply(IInterceptedFakeObjectCall fakeObjectCall)
            {
                var newRule = new CallRuleMetadata
                                  {
                                      Rule = new PropertyBehaviorRule(fakeObjectCall.Method, FakeManager) { Value = CreateFake(fakeObjectCall.Method.ReturnType) },
                                      CalledNumberOfTimes = 1
                                  };

                this.FakeManager.allUserRulesField.AddFirst(newRule);
                newRule.Rule.Apply(fakeObjectCall);
            }
        private static object ResolveReturnValue(IInterceptedFakeObjectCall fakeObjectCall)
        {
            object result = null;

            if (!FakeManager.TryCreateDummy(fakeObjectCall.Method.ReturnType, out result))
            {
                result = Helpers.GetDefaultValueOfType(fakeObjectCall.Method.ReturnType);
            }

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

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

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

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

                return true;
            }
 /// <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)
 {
     if (this.recorder.IsRecording)
     {
         this.wrappedRule.Apply(fakeObjectCall);
         this.recorder.RecordCall(fakeObjectCall.AsReadOnly());
     }
     else
     {
         this.recorder.ApplyNext(fakeObjectCall);
     }
 }
        /// <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;
        }
            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);
            }
            public void Apply(IInterceptedFakeObjectCall fakeObjectCall)
            {
                Guard.AgainstNull(fakeObjectCall, "fakeObjectCall");

                var newRule = new CallRuleMetadata
                                  {
                                      CalledNumberOfTimes = 1,
                                      Rule = new PropertyBehaviorRule(fakeObjectCall.Method, this.fakeManager)
                                                 {
                                                     Indices = fakeObjectCall.Arguments.Take(fakeObjectCall.Arguments.Count - 1).ToArray(),
                                                     Value = fakeObjectCall.Arguments.Last()
                                                 }
                                  };

                this.fakeManager.allUserRulesField.AddFirst(newRule);
            }
            public void Apply(IInterceptedFakeObjectCall fakeObjectCall)
            {
                if (this.TryHandleToString(fakeObjectCall))
                {
                    return;
                }

                if (this.TryHandleGetHashCode(fakeObjectCall))
                {
                    return;
                }

                if (this.TryHandleEquals(fakeObjectCall))
                {
                    return;
                }
            }
            public void Apply(IInterceptedFakeObjectCall fakeObjectCall)
            {
                Guard.AgainstNull(fakeObjectCall, "fakeObjectCall");

                var newRule = new CallRuleMetadata
                                  {
                                      Rule = new PropertyBehaviorRule(fakeObjectCall.Method, FakeManager)
                                      {
                                          Value = CreateFake(fakeObjectCall.Method.ReturnType),
                                          Indices = fakeObjectCall.Arguments.ToArray(),
                                      },
                                      CalledNumberOfTimes = 1
                                  };

                this.FakeManager.allUserRulesField.AddFirst(newRule);
                newRule.Rule.Apply(fakeObjectCall);
            }
            public void Apply(IInterceptedFakeObjectCall fakeObjectCall)
            {
                Guard.AgainstNull(fakeObjectCall, nameof(fakeObjectCall));

                var newRule = new CallRuleMetadata
                                  {
                                      Rule = new PropertyBehaviorRule(fakeObjectCall.Method, this.fakeManager)
                                      {
                                          Value = fakeObjectCall.GetDefaultReturnValue(),
                                          Indices = fakeObjectCall.Arguments.ToArray(),
                                      },
                                      CalledNumberOfTimes = 1
                                  };

                this.fakeManager.AllUserRules.AddFirst(newRule);
                newRule.Rule.Apply(fakeObjectCall);
            }
        public virtual void Apply(IInterceptedFakeObjectCall fakeObjectCall)
        {
            Guard.AgainstNull(fakeObjectCall, "fakeObjectCall");

            foreach (var action in this.Actions)
            {
                action.Invoke(fakeObjectCall);
            }

            this.Applicator.Invoke(fakeObjectCall);
            this.ApplyOutAndRefParametersValueProducer(fakeObjectCall);

            if (this.CallBaseMethod)
            {
                fakeObjectCall.CallBaseMethod();
            }
        }
        /// <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);
        }
            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 void ApplyOutAndRefParametersValues(IInterceptedFakeObjectCall fakeObjectCall)
        {
            if (this.OutAndRefParametersValues == null)
            {
                return;
            }

            var indexes = GetIndexesOfOutAndRefParameters(fakeObjectCall);

            if (this.OutAndRefParametersValues.Count != indexes.Count)
            {
                throw new InvalidOperationException(ExceptionMessages.NumberOfOutAndRefParametersDoesNotMatchCall);
            }

            foreach (var argument in indexes.Zip(this.OutAndRefParametersValues))
            {
                fakeObjectCall.SetArgumentValue(argument.Item1, argument.Item2);
            }
        }
        public virtual void Apply(IInterceptedFakeObjectCall fakeObjectCall)
        {
            foreach (var action in this.Actions)
            {
                action.Invoke(fakeObjectCall);
            }

            this.Applicator.Invoke(fakeObjectCall);
            this.ApplyOutAndRefParametersValues(fakeObjectCall);

            if (this.CallBaseMethod)
            {
                fakeObjectCall.CallBaseMethod();
            }
        }
Exemple #25
0
 private static IEnumerable <Tuple <int, object> > GetIndicesAndValuesOfOutputParameters(IInterceptedFakeObjectCall call, CallData recordedCall)
 {
     return
         ((from argument in call.Method.GetParameters().Zip(Enumerable.Range(0, int.MaxValue))
           where argument.Item1.ParameterType.IsByRef
           select argument.Item2).Zip(recordedCall.OutputArguments));
 }
        private static ICollection<int> GetIndexesOfOutAndRefParameters(IInterceptedFakeObjectCall fakeObjectCall)
        {
            var indexes = new List<int>();

            var arguments = fakeObjectCall.Method.GetParameters();
            for (var i = 0; i < arguments.Length; i++)
            {
                if (arguments[i].ParameterType.IsByRef)
                {
                    indexes.Add(i);
                }
            }

            return indexes;
        }
Exemple #27
0
 private static void ProcessFakeObjectCall(IFakeCallProcessor fakeCallProcessor, IInterceptedFakeObjectCall interceptedCall)
 {
     fakeCallProcessor.Process(interceptedCall);
 }
            public void Apply(IInterceptedFakeObjectCall fakeObjectCall)
            {
                var eventCall = EventCall.GetEventCall(fakeObjectCall);

                this.HandleEventCall(eventCall);
            }
Exemple #29
0
            public void Apply(IInterceptedFakeObjectCall fakeObjectCall)
            {
                var eventCall = EventCall.GetEventCall(fakeObjectCall);

                this.HandleEventCall(eventCall);
            }
Exemple #30
0
 private static void ApplyRule(CallRuleMetadata rule, IInterceptedFakeObjectCall fakeObjectCall)
 {
     logger.Debug("Applying rule {0}.", rule.Rule.ToString());
     rule.CalledNumberOfTimes++;
     rule.Rule.Apply(fakeObjectCall);
 }
            public void Apply(IInterceptedFakeObjectCall fakeObjectCall)
            {
                Guard.AgainstNull(fakeObjectCall, nameof(fakeObjectCall));

                fakeObjectCall.SetReturnValue(this.Value);
            }
Exemple #32
0
 private static void ApplyRule(CallRuleMetadata rule, IInterceptedFakeObjectCall fakeObjectCall)
 {
     rule.CalledNumberOfTimes++;
     rule.Rule.Apply(fakeObjectCall);
 }
 public void Apply(IInterceptedFakeObjectCall fakeObjectCall)
 {
     throw new ExpectationException(ExceptionMessages.CallToUnconfiguredMethodOfStrictFake(fakeObjectCall));
 }
Exemple #34
0
 void IFakeObjectCallRule.Apply(IInterceptedFakeObjectCall invocation)
 {
     this.ApplyWasCalled = true;
     this.Apply(invocation);
 }
 private static void ApplyOutputArguments(IInterceptedFakeObjectCall call, CallDataMetadata callToApply)
 {
     foreach (var outputArgument in call.Method.GetParameters()
         .Select((parameter, index) => new { Parameter = parameter, Index = index })
         .Where(argument => argument.Parameter.ParameterType.IsByRef)
         .Select(argument => argument.Index).Zip(callToApply.RecordedCall.OutputArguments, (index, outputArgument) => new { Index = index, Value = outputArgument }))
     {
         call.SetArgumentValue(outputArgument.Index, outputArgument.Value);
     }
 }
Exemple #36
0
 private static object DefaultReturnValue(IInterceptedFakeObjectCall fakeObjectCall)
 {
     return(DefaultReturnValueRule.ResolveReturnValue(fakeObjectCall));
 }
Exemple #37
0
        public void Apply(IInterceptedFakeObjectCall fakeObjectCall)
        {
            var returnValue = ResolveReturnValue(fakeObjectCall);

            fakeObjectCall.SetReturnValue(returnValue);
        }
        void IFakeCallProcessor.Process(IInterceptedFakeObjectCall fakeObjectCall)
        {
            foreach (var listener in this.interceptionListeners)
            {
                listener.OnBeforeCallIntercepted(fakeObjectCall);
            }

            var ruleToUse =
                (from rule in this.AllRules
                 where rule.Rule.IsApplicableTo(fakeObjectCall) && rule.HasNotBeenCalledSpecifiedNumberOfTimes()
                 select rule).First();

            try
            {
                ApplyRule(ruleToUse, fakeObjectCall);
            }
            finally
            {
                var readonlyCall = fakeObjectCall.AsReadOnly();

                this.RecordCall(readonlyCall);

                foreach (var listener in this.interceptionListeners.Reverse())
                {
                    listener.OnAfterCallIntercepted(readonlyCall, ruleToUse.Rule);
                }
            }
        }
 /// <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) => fakeObjectCall.CallWrappedMethod(this.WrappedObject);
 private static void AssertThatMethodsMatches(IInterceptedFakeObjectCall call, CallDataMetadata callToApply)
 {
     if (!callToApply.RecordedCall.Method.Equals(call.Method))
     {
         throw new RecordingException(ExceptionMessages.MethodMissmatchWhenPlayingBackRecording);
     }
 }
 public static object GetDefaultReturnValue(this IInterceptedFakeObjectCall call) =>
 DummyManager.TryCreateDummy(call.Method.ReturnType, new LoopDetectingResolutionContext(), out object result)
         ? result
         : call.Method.ReturnType.GetDefaultValue();
 private static void ApplyRule(CallRuleMetadata rule, IInterceptedFakeObjectCall fakeObjectCall)
 {
     rule.CalledNumberOfTimes++;
     rule.Rule.Apply(fakeObjectCall);
 }
 private static void SetReturnValue(IInterceptedFakeObjectCall fakeObjectCall, RecordedCall recordedCall)
 {
     fakeObjectCall.SetReturnValue(recordedCall.ReturnValue);
 }
Exemple #44
0
 public abstract void Apply(IInterceptedFakeObjectCall fakeObjectCall);
Exemple #45
0
            public override void Apply(IInterceptedFakeObjectCall fakeObjectCall)
            {
                Guard.AgainstNull(fakeObjectCall, nameof(fakeObjectCall));

                fakeObjectCall.SetReturnValue(fakeObjectCall.GetDefaultReturnValue());
            }