public void RaiseCallWasIntercepted(IWritableFakeObjectCall call)
 {
     var handler = this.CallWasIntercepted;
     if (handler != null)
     {
         handler(this, new CallInterceptedEventArgs(call));
     }
 }
            public void ApplyNext(IWritableFakeObjectCall call)
            {
                if (this.enumerator == null || !this.enumerator.MoveNext())
                {
                    this.enumerator = this.Xml.Root.Elements().GetEnumerator();
                    this.enumerator.MoveNext();
                }

                call.SetReturnValue(Deserialize(this.enumerator.Current));
            }
Exemplo n.º 3
0
            private bool TryHandleToString(IWritableFakeObjectCall fakeObjectCall)
            {
                if (!fakeObjectCall.Method.Name.Equals("ToString"))
                {
                    return(false);
                }

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

                return(true);
            }
Exemplo n.º 4
0
 public void Apply(IWritableFakeObjectCall fakeObjectCall)
 {
     if (this.IsPropertyGetter(fakeObjectCall))
     {
         fakeObjectCall.SetReturnValue(this.Value);
     }
     else
     {
         this.Value = fakeObjectCall.Arguments[0];
     }
 }
Exemplo n.º 5
0
        private void Intercept(IWritableFakeObjectCall fakeObjectCall)
        {
            var ruleToUse =
                (from rule in this.AllRules
                 where rule.Rule.IsApplicableTo(fakeObjectCall) && rule.HasNotBeenCalledSpecifiedNumberOfTimes()
                 select rule).First();

            this.ApplyRule(ruleToUse, fakeObjectCall);

            FakeScope.Current.AddInterceptedCall(this, fakeObjectCall.AsReadOnly());
        }
Exemplo n.º 6
0
        internal void Intercept(IWritableFakeObjectCall fakeObjectCall)
        {
            this.recordedCallsField.Add(fakeObjectCall);

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

            this.ApplyRule(ruleToUse, fakeObjectCall);
        }
        private static object ResolveReturnValue(IWritableFakeObjectCall fakeObjectCall)
        {
            var generator = GetFakeObjectGeneratorForCall(fakeObjectCall);

            if (!generator.GenerateFakeObject())
            {
                return(Helpers.GetDefaultValueOfType(fakeObjectCall.Method.ReturnType));
            }

            return(generator.GeneratedFake);
        }
        /// <summary>
        /// Applies the call if the call has been recorded.
        /// </summary>
        /// <param name="call">The call to apply to from recording.</param>
        public void ApplyNext(IWritableFakeObjectCall call)
        {
            this.AssertThatCallQueueIsNotEmpty();

            var callToApply = this.callQueue.Dequeue();

            AssertThatMethodsMatches(call, callToApply);
            ApplyOutputArguments(call, callToApply);

            call.SetReturnValue(callToApply.RecordedCall.ReturnValue);
            callToApply.HasBeenApplied = true;
        }
Exemplo n.º 9
0
 public void Apply(IWritableFakeObjectCall fakeObjectCall)
 {
     if (this.Recorder.IsRecording)
     {
         this.WrappedObjectRule.Apply(fakeObjectCall);
         this.Recorder.RecordCall(fakeObjectCall.Freeze());
     }
     else
     {
         this.Recorder.ApplyNext(fakeObjectCall);
     }
 }
 /// <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(IWritableFakeObjectCall fakeObjectCall)
 {
     if (this.recorder.IsRecording)
     {
         this.wrappedRule.Apply(fakeObjectCall);
         this.recorder.RecordCall(fakeObjectCall.AsReadOnly());
     }
     else
     {
         this.recorder.ApplyNext(fakeObjectCall);
     }
 }
Exemplo n.º 11
0
            public void Apply(IWritableFakeObjectCall fakeObjectCall)
            {
                this.RecordedRule.ApplicableToMethod = fakeObjectCall.Method;

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

                this.fakeObject.AddRule(this.RecordedRule);

                fakeObjectCall.SetReturnValue(Helpers.GetDefaultValueOfType(fakeObjectCall.Method.ReturnType));
            }
Exemplo n.º 12
0
            private static bool TryHandleGetHashCode(IWritableFakeObjectCall fakeObjectCall)
            {
                if (!fakeObjectCall.Method.Name.Equals("GetHashCode"))
                {
                    return(false);
                }

                var fakeObject = Fake.GetFakeObject(fakeObjectCall.FakedObject);

                fakeObjectCall.SetReturnValue(fakeObject.GetHashCode());

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

                this.FakeObject.allUserRulesField.AddFirst(newRule);
            }
Exemplo n.º 14
0
        public void Apply_should_call_the_applicator_with_the_incoming_call()
        {
            IWritableFakeObjectCall callPassedToApplicator = null;
            var callPassedToRule = FakeCall.Create <IFoo>("Bar");

            var rule = CreateRule <IFoo>(x => x.Bar());

            rule.Applicator = x => callPassedToApplicator = x;

            rule.Apply(callPassedToRule);

            Assert.That(callPassedToApplicator, Is.SameAs(callPassedToRule));
        }
Exemplo n.º 15
0
            public void Apply(IWritableFakeObjectCall fakeObjectCall)
            {
                var newRule = new CallRuleMetadata
                {
                    Rule = new PropertyBehaviorRule(fakeObjectCall.Method, FakeObject)
                    {
                        Value = new FakeObject(fakeObjectCall.Method.ReturnType).Object
                    },
                    CalledNumberOfTimes = 1
                };

                this.FakeObject.allUserRulesField.AddFirst(newRule);
                newRule.Rule.Apply(fakeObjectCall);
            }
        public virtual void Apply(IWritableFakeObjectCall fakeObjectCall)
        {
            foreach (var action in this.Actions)
            {
                action.Invoke(fakeObjectCall);
            }

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

            if (this.CallBaseMethod)
            {
                fakeObjectCall.CallBaseMethod();
            }
        }
        private static ICollection <int> GetIndexesOfOutAndRefParameters(IWritableFakeObjectCall fakeObjectCall)
        {
            var indexes = new List <int>();

            var arguments = fakeObjectCall.Method.GetParameters();

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

            return(indexes);
        }
Exemplo n.º 18
0
            public void Apply(IWritableFakeObjectCall fakeObjectCall)
            {
                if (TryHandleToString(fakeObjectCall))
                {
                    return;
                }

                if (TryHandleGetHashCode(fakeObjectCall))
                {
                    return;
                }

                if (TryHandleEquals(fakeObjectCall))
                {
                    return;
                }
            }
Exemplo n.º 19
0
        public virtual void Apply(IWritableFakeObjectCall fakeObjectCall)
        {
            if (this.Applicator == null)
            {
                throw new InvalidOperationException(ExceptionMessages.ApplicatorNotSetExceptionMessage);
            }

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

            this.Applicator.Invoke(fakeObjectCall);

            if (this.CallBaseMethod)
            {
                fakeObjectCall.CallBaseMethod();
            }
        }
Exemplo n.º 20
0
        public void GenerateProxy_should_generate_result_that_raises_events_when_calls_are_intercepted(Type someInterfaceType)
        {
            var generator = this.CreateGenerator();

            var result = generator.GenerateProxy(someInterfaceType, this.fakeObject, this.container);

            IWritableFakeObjectCall interceptedCall = null;

            result.CallWasIntercepted += (s, e) =>
            {
                interceptedCall = e.Call;
            };

            var someInterface = (ISomeInterface)result.Proxy;

            someInterface.Bar();

            Assert.That(interceptedCall.Method.Name, Is.EqualTo("Bar"));
        }
        private void ApplyOutAndRefParametersValues(IWritableFakeObjectCall 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.Pairwise(this.OutAndRefParametersValues))
            {
                fakeObjectCall.SetArgumentValue(argument.First, argument.Second);
            }
        }
Exemplo n.º 22
0
        public void GenerateProxy_with_arguments_for_constructor_should_generate_proxies_raises_events_when_calls_made_to_proxy()
        {
            var generator = this.CreateGenerator();

            var result = generator.GenerateProxy(typeof(TypeWithConstructorThatTakesSingleString), this.fakeObject, new object[] { "foo" });

            IWritableFakeObjectCall interceptedCall = null;

            result.CallWasIntercepted += (s, e) =>
            {
                interceptedCall = e.Call;
            };

            var fake = result.Proxy as TypeWithConstructorThatTakesSingleString;

            fake.Bar();

            Assert.That(interceptedCall.Method.Name, Is.EqualTo("Bar"));
        }
Exemplo n.º 23
0
        public void Should_delegate_to_fake_call_processor_when_method_on_fake_is_called(Type typeThatImplementsInterfaceType)
        {
            // Arrange
            IWritableFakeObjectCall interceptedFakeObjectCall = null;

            var fakeCallProcessorProvider = CreateFakeCallProcessorProvider(c => interceptedFakeObjectCall = c);

            var result = this.generator.GenerateProxy(typeThatImplementsInterfaceType, Enumerable.Empty <Type>(), null, fakeCallProcessorProvider);

            var proxy = (IInterfaceType)result.GeneratedProxy;

            // Act
            proxy.Foo(1, 2);

            // Assert
            interceptedFakeObjectCall.Should().NotBeNull();
            interceptedFakeObjectCall.Arguments.Should().BeEquivalentTo(1, 2);
            interceptedFakeObjectCall.Method.Name.Should().Be(typeof(IInterfaceType).GetMethod("Foo").Name);
            interceptedFakeObjectCall.FakedObject.Should().BeSameAs(proxy);
        }
Exemplo n.º 24
0
            private bool TryHandleEquals(IWritableFakeObjectCall fakeObjectCall)
            {
                if (!fakeObjectCall.Method.Name.Equals("Equals"))
                {
                    return(false);
                }

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

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

                return(true);
            }
Exemplo n.º 25
0
        public void Should_raise_event_on_event_raiser_when_method_on_fake_is_called(Type typeThatImplementsInterfaceType)
        {
            // Arrange
            var result = this.generator.GenerateProxy(typeThatImplementsInterfaceType, Enumerable.Empty <Type>(), null);
            IWritableFakeObjectCall callMadeToProxy = null;

            result.CallInterceptedEventRaiser.CallWasIntercepted += (sender, e) =>
            {
                callMadeToProxy = e.Call;
            };

            var proxy = (IInterfaceType)result.GeneratedProxy;

            // Act
            proxy.Foo(1, 2);

            // Assert
            Assert.That(callMadeToProxy, Is.Not.Null);
            Assert.That(callMadeToProxy.Arguments, Is.EquivalentTo(new object[] { 1, 2 }));
            Assert.That(callMadeToProxy.Method.Name, Is.EqualTo(typeof(IInterfaceType).GetMethod("Foo").Name));
            Assert.That(callMadeToProxy.FakedObject, Is.SameAs(proxy));
        }
Exemplo n.º 26
0
        private void Intercept(IWritableFakeObjectCall fakeObjectCall)
        {
            var ruleToUse =
                (from rule in this.AllRules
                 where rule.Rule.IsApplicableTo(fakeObjectCall) && rule.HasNotBeenCalledSpecifiedNumberOfTimes()
                 select rule).First();

            var interceptedCall = new InterceptedCallAdapter(fakeObjectCall);

            try
            {
                ApplyRule(ruleToUse, interceptedCall);
            }
            finally
            {
                this.RecordInterceptedCall(interceptedCall);
            }
        }
Exemplo n.º 27
0
 private void ApplyRule(CallRuleMetadata rule, IWritableFakeObjectCall fakeObjectCall)
 {
     rule.CalledNumberOfTimes++;
     rule.Rule.Apply(fakeObjectCall);
 }
            public void Apply(IWritableFakeObjectCall fakeObjectCall)
            {
                EventCall eventCall = EventCall.GetEventCall(fakeObjectCall);

                this.HandleEventCall(eventCall);
            }
        public CallInterceptedEventArgs(IWritableFakeObjectCall call)
        {
            Guard.AgainstNull(call, "call");

            this.Call = call;
        }
 private static IEnumerable <Tuple <int, object> > GetIndicesAndValuesOfOutputParameters(IWritableFakeObjectCall call, CallData recordedCall)
 {
     return
         ((from argument in call.Method.GetParameters().Zip(Enumerable.Range(0, int.MaxValue))
           where argument.First.ParameterType.IsByRef
           select argument.Second).Zip(recordedCall.OutputArguments));
 }
        public CallInterceptedEventArgs(IWritableFakeObjectCall call)
        {
            Guard.AgainstNull(call, "call");

            this.Call = call;
        }
Exemplo n.º 32
0
        private void Intercept(IWritableFakeObjectCall 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();

            var interceptedCall = new InterceptedCallAdapter(fakeObjectCall);

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

                if (!interceptedCall.IgnoreCallInRecording)
                {
                    FakeScope.Current.AddInterceptedCall(this, readonlyCall);
                }

                foreach (var listener in this.interceptionListeners.Reverse())
                {
                    listener.OnAfterCallIntercepted(readonlyCall, ruleToUse.Rule);
                }
            }
        }
            public void Apply(IWritableFakeObjectCall fakeObjectCall)
            {
                var returnValue = Helpers.GetDefaultValueOfType(fakeObjectCall.Method.ReturnType);

                fakeObjectCall.SetReturnValue(returnValue);
            }
Exemplo n.º 34
0
 public InterceptedCallAdapter(IWritableFakeObjectCall call)
 {
     this.call = call;
 }
Exemplo n.º 35
0
 public InterceptedCallAdapter(IWritableFakeObjectCall call)
 {
     this.call = call;
 }