예제 #1
0
        /// <summary>
        /// Get the expectation for this method on this object with this arguments
        /// </summary>
        public IExpectation GetRecordedExpectation(IInvocation invocation, object proxy, MethodInfo method, object[] args)
        {
            Validate.IsNotNull(proxy, "proxy");
            Validate.IsNotNull(method, "method");
            Validate.IsNotNull(args, "args");
            if (replayerToCall != null)
            {
                return(replayerToCall.GetRecordedExpectation(invocation, proxy, method, args));
            }

            //merge recorders that contains only a single empty recorder
            if (recordedActions.Count == 1 && recordedActions[0] is IMethodRecorder)
            {
                replayerToCall = (IMethodRecorder)recordedActions[0];
                return(replayerToCall.GetRecordedExpectation(invocation, proxy, method, args));
            }

            IExpectation expectation = DoGetRecordedExpectation(invocation, proxy, method, args);

            if (HasExpectations == false)
            {
                MoveToParentReplayer();
            }
            return(expectation);
        }
        /// <summary>
        /// Gets the all expectations for proxy.
        /// </summary>
        /// <param name="proxy">Mocked object.</param>
        /// <returns>List of all relevant expectation</returns>
        protected override ExpectationsList DoGetAllExpectationsForProxy(object proxy)
        {
            Validate.IsNotNull(proxy, "proxy");

            ExpectationsList expectations = new ExpectationsList();

            foreach (object action in recordedActions)
            {
                ProxyMethodExpectationTriplet triplet = action as ProxyMethodExpectationTriplet;
                if (triplet != null)
                {
                    if (MockedObjectsEquality.Instance.Equals(triplet.Proxy, proxy))
                    {
                        expectations.Add(triplet.Expectation);
                    }
                }
                else                 //Action is another recorder
                {
                    IMethodRecorder  innerRecorder        = (IMethodRecorder)action;
                    ExpectationsList expectationsForProxy = innerRecorder.GetAllExpectationsForProxy(proxy);
                    expectations.AddRange(expectationsForProxy);
                }
            }
            return(expectations);
        }
        /// <summary>
        /// Gets the all expectations for a mocked object and method combination,
        /// regardless of the expected arguments / callbacks / contraints.
        /// </summary>
        /// <param name="proxy">Mocked object.</param>
        /// <param name="method">Method.</param>
        /// <returns>List of all relevant expectation</returns>
        public override ExpectationsList GetAllExpectationsForProxyAndMethod(object proxy, MethodInfo method)
        {
            Validate.IsNotNull(proxy, "proxy");
            Validate.IsNotNull(method, "method");

            ExpectationsList expectations = new ExpectationsList();

            foreach (object action in recordedActions)
            {
                ProxyMethodExpectationTriplet triplet = action as ProxyMethodExpectationTriplet;
                if (triplet != null)
                {
                    if (MockedObjectsEquality.Instance.Equals(triplet.Proxy, proxy) &&
                        MethodsEquals(method, triplet))
                    {
                        expectations.Add(triplet.Expectation);
                    }
                }
                else                 //Action is another recorder
                {
                    IMethodRecorder innerRecorder = (IMethodRecorder)action;
                    expectations.AddRange(innerRecorder.GetAllExpectationsForProxyAndMethod(proxy, method));
                }
            }
            return(expectations);
        }
예제 #4
0
 /// <summary>
 /// Creates a new <see cref="RecorderChanger"/> instance.
 /// </summary>
 public RecorderChanger(MockRepository repository, IMethodRecorder recorder, IMethodRecorder newRecorder)
 {
     this.recorder   = recorder;
     this.repository = repository;
     repository.PushRecorder(newRecorder);
     this.recorder.AddRecorder(newRecorder);
 }
예제 #5
0
 /// <summary>
 /// Initializes a new instance of the <see cref="RecorderChanger"/> class. 
 /// Creates a new <see cref="RecorderChanger"/> instance.
 /// </summary>
 /// <param name="repository">
 /// The repository.
 /// </param>
 /// <param name="recorder">
 /// The recorder.
 /// </param>
 /// <param name="newRecorder">
 /// The new Recorder.
 /// </param>
 public RecorderChanger(MockRepository repository, IMethodRecorder recorder, IMethodRecorder newRecorder)
 {
     this.recorder = recorder;
     this.repository = repository;
     repository.PushRecorder(newRecorder);
     this.recorder.AddRecorder(newRecorder);
 }
예제 #6
0
 /// <summary>
 /// Moves to parent recorder.
 /// </summary>
 public void MoveToParentReplayer()
 {
     replayerToCall = null;
     if (parentRecorder == null || HasExpectations)
     {
         return;
     }
     parentRecorder.MoveToParentReplayer();
 }
예제 #7
0
 public IMethodRecorderTests()
 {
     demo = MockRepository.GenerateStrictMock(typeof(IDemo), null, null) as IDemo;
     voidNoArgs = typeof (IDemo).GetMethod("VoidNoArgs");
     voidThreeArgs = typeof (IDemo).GetMethod("VoidThreeStringArgs");
     expectationOne = new AnyArgsExpectation(new FakeInvocation(this.voidNoArgs), new Range(1, 1));
     expectationTwo = new AnyArgsExpectation(new FakeInvocation(voidThreeArgs), new Range(1, 1));
     recorder = CreateRecorder();
     ChildSetup();
 }
        public ComplexOrderingTests()
        {
            recorder = new UnorderedMethodRecorder(new ProxyMethodExpectationsDictionary());
            nestedRecorder = new UnorderedMethodRecorder(new ProxyMethodExpectationsDictionary());
            recorder.AddRecorder(nestedRecorder);

            proxy = new object();
            method = typeof (object).GetMethod("ToString");
            expectation = new AnyArgsExpectation(new FakeInvocation(method), new Range(1, 1));
            args = new object[0];
        }
예제 #9
0
 public IMethodRecorderTests()
 {
     mocks          = new MockRepository();
     demo           = this.mocks.StrictMock(typeof(IDemo)) as IDemo;
     voidNoArgs     = typeof(IDemo).GetMethod("VoidNoArgs");
     voidThreeArgs  = typeof(IDemo).GetMethod("VoidThreeStringArgs");
     expectationOne = new AnyArgsExpectation(new FakeInvocation(this.voidNoArgs), new Range(1, 1));
     expectationTwo = new AnyArgsExpectation(new FakeInvocation(voidThreeArgs), new Range(1, 1));
     recorder       = CreateRecorder();
     ChildSetup();
 }
예제 #10
0
        public MethodRecorderBaseTests()
        {
            recorder     = new UnorderedMethodRecorder(new ProxyMethodExpectationsDictionary());
            testRecorder = new TestMethodRecorder();
            recorder.AddRecorder(testRecorder);

            proxy       = new object();
            method      = typeof(object).GetMethod("ToString");
            expectation = new AnyArgsExpectation(new FakeInvocation(method), new Range(1, 1));
            args        = new object[0];
        }
예제 #11
0
 /// <summary>
 /// Adds the recorder and turn it into the active recorder.
 /// </summary>
 /// <param name="recorder">Recorder.</param>
 public void AddRecorder(IMethodRecorder recorder)
 {
     if (recorderToCall != null)
     {
         recorderToCall.AddRecorder(recorder);
     }
     else
     {
         DoAddRecorder(recorder);
         recorderToCall = recorder;
     }
 }
예제 #12
0
 /// <summary>
 /// Moves to previous recorder.
 /// </summary>
 public bool MoveToPreviousRecorder()
 {
     if (recorderToCall == null)
     {
         return(true);
     }
     if (recorderToCall.MoveToPreviousRecorder())
     {
         recorderToCall = null;
     }
     return(false);
 }
예제 #13
0
 /// <summary>
 /// Should this replayer be considered valid for this call?
 /// </summary>
 protected bool ShouldConsiderThisReplayer(IMethodRecorder replayer)
 {
     return replayersToIgnoreForThisCall.Contains(replayer) == false;
 }
예제 #14
0
 /// <summary>
 /// Creates a new <see cref="OrderedMethodRecorder"/> instance.
 /// </summary>
 /// <param name="parentRecorder">Parent recorder.</param>
 /// <param name="repeatableMethods">Repetable methods</param>
 public OrderedMethodRecorder(IMethodRecorder parentRecorder, ProxyMethodExpectationsDictionary repeatableMethods)
     : base(parentRecorder, repeatableMethods)
 {
 }
예제 #15
0
 /// <summary>
 /// Creates a new <see cref="MethodRecorderBase"/> instance.
 /// </summary>
 protected MethodRecorderBase(ProxyMethodExpectationsDictionary repeatableMethods)
 {
     this.repeatableMethods = repeatableMethods;
     recorderToCall         = null;
     replayerToCall         = null;
 }
예제 #16
0
 /// <summary>
 /// Handle the real execution of this method for the derived class
 /// </summary>
 protected abstract void DoAddRecorder(IMethodRecorder recorder);
예제 #17
0
        /// <summary>
        /// Handles the real getting of the recorded expectation or null.
        /// </summary>
        protected override IExpectation DoGetRecordedExpectationOrNull(object proxy, MethodInfo method, object[] args)
        {
            Validate.IsNotNull(proxy, "proxy");
            Validate.IsNotNull(method, "method");
            Validate.IsNotNull(args, "args");
            // Need this because we may want to modify the recordedAction list as we traverse it
            // See: ClearReplayerToCall();
            ArrayList traversalSafeCopy = new ArrayList(recordedActions);
            bool      allSatisfied      = true;

            foreach (object action in traversalSafeCopy)
            {
                ProxyMethodExpectationTriplet triplet = action as ProxyMethodExpectationTriplet;
                if (triplet != null)
                {
                    if (MockedObjectsEquality.Instance.Equals(triplet.Proxy, proxy) &&
                        triplet.Method == method &&
                        triplet.Expectation.CanAcceptCalls &&
                        triplet.Expectation.IsExpected(args))
                    {
                        triplet.Expectation.AddActualCall();
                        return(triplet.Expectation);
                    }
                    if (!triplet.Expectation.ExpectationSatisfied)
                    {
                        allSatisfied = false;
                    }
                }
                else                 //Action is another recorder
                {
                    IMethodRecorder innerRecorder = (IMethodRecorder)action;
                    if (ShouldConsiderThisReplayer(innerRecorder) == false)
                    {
                        continue;
                    }
                    IExpectation expectation = innerRecorder.GetRecordedExpectationOrNull(proxy, method, args);
                    if (expectation != null)
                    {
                        replayerToCall = innerRecorder;
                        return(expectation);
                    }
                    if (innerRecorder.HasExpectations)
                    {
                        allSatisfied = false;
                    }
                }
            }
            // We still have unsatisifed expectation or we don't have a parent recorder
            if (!allSatisfied || parentRecorder == null)
            {
                return(null);
            }
            // We only reach this place if we still has valid expectations, but they are not
            // mandatory, (AtLeastOnce(), etc). In this case, the recorder (and its children) cannot satisfy the
            // expectation, so we move to the parent recorder and let it handle it.
            parentRecorder.ClearReplayerToCall(this);
            // We need this to give the correct exception if the method is an unepxected one.
            // Check the redirection in UnexpectedMethodCall()
            parentRecorderRedirection = parentRecorder;
            return(parentRecorder.GetRecordedExpectationOrNull(proxy, method, args));
        }
예제 #18
0
 /// <summary>
 /// Creates a new <see cref="MethodRecorderBase"/> instance.
 /// </summary>
 /// <param name="parentRecorder">Parent recorder.</param>
 /// <param name="repeatableMethods">Repeatable methods</param>
 protected MethodRecorderBase(IMethodRecorder parentRecorder, ProxyMethodExpectationsDictionary repeatableMethods)
     : this(repeatableMethods)
 {
     this.parentRecorder = parentRecorder;
 }
예제 #19
0
 /// <summary>
 /// Creates a new <see cref="MethodRecorderBase"/> instance.
 /// </summary>
 /// <param name="parentRecorder">Parent recorder.</param>
 /// <param name="repeatableMethods">Repeatable methods</param>
 protected MethodRecorderBase(IMethodRecorder parentRecorder, ProxyMethodExpectationsDictionary repeatableMethods)
     : this(repeatableMethods)
 {
     this.parentRecorder = parentRecorder;
 }
예제 #20
0
 protected override void DoAddRecorder(IMethodRecorder recorder)
 {
     DoAddRecorderCalled = true;
 }
예제 #21
0
 /// <summary>
 /// Creates a new <see cref="OrderedMethodRecorder"/> instance.
 /// </summary>
 /// <param name="parentRecorder">Parent recorder.</param>
 /// <param name="repeatableMethods">Repetable methods</param>
 public OrderedMethodRecorder(IMethodRecorder parentRecorder, ProxyMethodExpectationsDictionary repeatableMethods)
     : base(parentRecorder, repeatableMethods)
 {
 }
예제 #22
0
 /// <summary>
 /// Handles the real getting of the recorded expectation or null.
 /// </summary>
 protected override IExpectation DoGetRecordedExpectationOrNull(object proxy, MethodInfo method, object[] args)
 {
     Validate.IsNotNull(proxy, "proxy");
     Validate.IsNotNull(method, "method");
     Validate.IsNotNull(args, "args");
     // Need this because we may want to modify the recordedAction list as we traverse it
     // See: ClearReplayerToCall();
     ArrayList traversalSafeCopy = new ArrayList(recordedActions);
     bool allSatisfied = true;
     foreach (object action in traversalSafeCopy)
     {
         ProxyMethodExpectationTriplet triplet = action as ProxyMethodExpectationTriplet;
         if (triplet != null)
         {
             if (MockedObjectsEquality.Instance.Equals(triplet.Proxy, proxy) &&
                 triplet.Method == method &&
                 triplet.Expectation.CanAcceptCalls &&
                 triplet.Expectation.IsExpected(args))
             {
                 triplet.Expectation.AddActualCall();
                 return triplet.Expectation;
             }
             if(!triplet.Expectation.ExpectationSatisfied)
                 allSatisfied = false;
         }
         else //Action is another recorder
         {
             IMethodRecorder innerRecorder = (IMethodRecorder) action;
             if(ShouldConsiderThisReplayer(innerRecorder)==false)
                 continue;
             IExpectation expectation = innerRecorder.GetRecordedExpectationOrNull(proxy, method, args);
             if (expectation != null)
             {
                 replayerToCall = innerRecorder;
                 return expectation;
             }
             if(innerRecorder.HasExpectations)
                 allSatisfied = false;
         }
     }
     // We still have unsatisifed expectation or we don't have a parent recorder
     if (!allSatisfied || parentRecorder==null)
         return null;
     // We only reach this place if we still has valid expectations, but they are not
     // mandatory, (AtLeastOnce(), etc). In this case, the recorder (and its children) cannot satisfy the
     // expectation, so we move to the parent recorder and let it handle it.
     parentRecorder.ClearReplayerToCall(this);
     // We need this to give the correct exception if the method is an unepxected one.
     // Check the redirection in UnexpectedMethodCall()
     parentRecorderRedirection = parentRecorder;
     return parentRecorder.GetRecordedExpectationOrNull(proxy, method, args);
 }
예제 #23
0
 /// <summary>
 /// Handle the real execution of this method for the derived class
 /// </summary>
 protected override void DoAddRecorder(IMethodRecorder recorder)
 {
     recordedActions.Add(recorder);
 }
예제 #24
0
 /// <summary>
 /// Creates a new <see cref="MethodRecorderBase"/> instance.
 /// </summary>
 protected MethodRecorderBase(ProxyMethodExpectationsDictionary repeatableMethods)
 {
     this.repeatableMethods = repeatableMethods;
     recorderToCall = null;
     replayerToCall = null;
 }
 protected override void DoAddRecorder(IMethodRecorder recorder)
 {
     DoAddRecorderCalled = true;
 }
예제 #26
0
 /// <summary>
 /// Adds the recorder and turn it into the active recorder.
 /// </summary>
 /// <param name="recorder">Recorder.</param>
 public void AddRecorder(IMethodRecorder recorder)
 {
     if (recorderToCall != null)
         recorderToCall.AddRecorder(recorder);
     else
     {
         DoAddRecorder(recorder);
         recorderToCall = recorder;
     }
 }
예제 #27
0
 /// <summary>
 /// Handle the real execution of this method for the derived class
 /// </summary>
 protected override void DoAddRecorder(IMethodRecorder recorder)
 {
     recordedActions.Add(recorder);
 }
예제 #28
0
 public void SetUp()
 {
     mocks = new MockRepository();
     demo = this.mocks.StrictMock(typeof (IDemo)) as IDemo;
     voidNoArgs = typeof (IDemo).GetMethod("VoidNoArgs");
     voidThreeArgs = typeof (IDemo).GetMethod("VoidThreeStringArgs");
     expectationOne = new AnyArgsExpectation(new FakeInvocation(this.voidNoArgs), new Range(1, 1));
     expectationTwo = new AnyArgsExpectation(new FakeInvocation(voidThreeArgs), new Range(1, 1));
     recorder = CreateRecorder();
     ChildSetup();
 }
예제 #29
0
        /// <summary>
        /// Get the expectation for this method on this object with this arguments 
        /// </summary>
        public IExpectation GetRecordedExpectation(IInvocation invocation, object proxy, MethodInfo method, object[] args)
        {
            Validate.IsNotNull(proxy, "proxy");
            Validate.IsNotNull(method, "method");
            Validate.IsNotNull(args, "args");
            if (replayerToCall != null)
                return replayerToCall.GetRecordedExpectation(invocation, proxy, method, args);

            //merge recorders that contains only a single empty recorder
            if (recordedActions.Count == 1 && recordedActions[0] is IMethodRecorder)
            {
                replayerToCall = (IMethodRecorder)recordedActions[0];
                return replayerToCall.GetRecordedExpectation(invocation, proxy, method, args);
            }

            IExpectation expectation = DoGetRecordedExpectation(invocation, proxy, method, args);
            if (HasExpectations == false)
                MoveToParentReplayer();
            return expectation;
        }
예제 #30
0
 /// <summary>
 /// Moves to parent recorder.
 /// </summary>
 public void MoveToParentReplayer()
 {
     replayerToCall = null;
     if (parentRecorder == null || HasExpectations)
         return;
     parentRecorder.MoveToParentReplayer();
 }
예제 #31
0
 /// <summary>
 /// Clear the replayer to call (and all its chain of replayers).
 /// This also removes it from the list of expectations, so it will never be considered again
 /// </summary>
 public void ClearReplayerToCall(IMethodRecorder childReplayer)
 {
     replayerToCall = null;
     //recordedActions.Remove(childReplayer);
     replayersToIgnoreForThisCall.Add(childReplayer);
 }
예제 #32
0
 /// <summary>
 /// Moves to previous recorder.
 /// </summary>
 public bool MoveToPreviousRecorder()
 {
     if (recorderToCall == null)
         return true;
     if (recorderToCall.MoveToPreviousRecorder())
         recorderToCall = null;
     return false;
 }
예제 #33
0
 /// <summary>
 /// Should this replayer be considered valid for this call?
 /// </summary>
 protected bool ShouldConsiderThisReplayer(IMethodRecorder replayer)
 {
     return(replayersToIgnoreForThisCall.Contains(replayer) == false);
 }
예제 #34
0
 /// <summary>
 /// Handle the real execution of this method for the derived class
 /// </summary>
 protected abstract void DoAddRecorder(IMethodRecorder recorder);
예제 #35
0
 /// <summary>
 /// Clear the replayer to call (and all its chain of replayers).
 /// This also removes it from the list of expectations, so it will never be considered again
 /// </summary>
 public void ClearReplayerToCall(IMethodRecorder childReplayer)
 {
     replayerToCall = null;
     //recordedActions.Remove(childReplayer);
     replayersToIgnoreForThisCall.Add(childReplayer);
 }
예제 #36
0
        /// <summary>
        /// Handles the real getting of the recorded expectation or null.
        /// </summary>
        protected override IExpectation DoGetRecordedExpectationOrNull(object proxy, MethodInfo method, object[] args)
        {
            int actionPos = 0;

            while (actionPos < recordedActions.Count)
            {
                ProxyMethodExpectationTriplet triplet = recordedActions[actionPos] as ProxyMethodExpectationTriplet;
                if (triplet != null)
                {
                    if (MockedObjectsEquality.Instance.Equals(triplet.Proxy, proxy) &&
                        triplet.Method == method &&
                        triplet.Expectation.CanAcceptCalls &&
                        triplet.Expectation.IsExpected(args))
                    {
                        // Ensure that this expectation is the first one in the list.
                        while (actionPos > 0)
                        {
                            recordedActions.RemoveAt(0);
                            actionPos--;
                        }

                        // This call satisfies that expectation.
                        triplet.Expectation.AddActualCall();

                        // Is this expectation complete?
                        if (!triplet.Expectation.CanAcceptCalls)
                        {
                            recordedActions.RemoveAt(0);
                        }

                        return(triplet.Expectation);
                    }
                    else
                    {
                        // The expectation didn't match.  Is the expectation satisfied, so can we consider
                        // looking at the next expectation?
                        if (triplet.Expectation.ExpectationSatisfied)
                        {
                            actionPos++;
                        }
                        else
                        {
                            // No.
                            return(null);
                        }
                    }
                }
                else                 // Action is another recorder
                {
                    IMethodRecorder innerRecorder = (IMethodRecorder)recordedActions[actionPos];
                    if (innerRecorder.HasExpectations == false)                     // so don't need to consider it
                    {
                        actionPos++;
                        continue;
                    }
                    if (ShouldConsiderThisReplayer(innerRecorder) == false)
                    {
                        break;
                    }
                    IExpectation expectation = innerRecorder.GetRecordedExpectationOrNull(proxy, method, args);
                    if (expectation != null)
                    {
                        // Ensure that this expectation is the first one in the list.
                        while (actionPos > 0)
                        {
                            recordedActions.RemoveAt(0);
                            actionPos--;
                        }

                        replayerToCall = innerRecorder;
                        recordedActions.RemoveAt(0);
                        return(expectation);
                    }
                    break;
                }
            }
            if (parentRecorder == null)
            {
                return(null);
            }
            // We only reach this place if we still has valid expectations, but they are not
            // mandatory, (AtLeastOnce(), etc). In this case, the recorder (and its children) cannot satisfy the
            // expectation, so we move to the parent recorder and let it handle it.
            parentRecorder.ClearReplayerToCall(this);
            // We need this to give the correct exception if the method is an unepxected one.
            // Check the redirection in UnexpectedMethodCall()
            parentRecorderRedirection = parentRecorder;
            return(parentRecorder.GetRecordedExpectationOrNull(proxy, method, args));
        }