/// <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); }
/// <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); }
/// <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); }
/// <summary> /// Moves to parent recorder. /// </summary> public void MoveToParentReplayer() { replayerToCall = null; if (parentRecorder == null || HasExpectations) { return; } parentRecorder.MoveToParentReplayer(); }
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]; }
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(); }
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]; }
/// <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; } }
/// <summary> /// Moves to previous recorder. /// </summary> public bool MoveToPreviousRecorder() { if (recorderToCall == null) { return(true); } if (recorderToCall.MoveToPreviousRecorder()) { recorderToCall = null; } return(false); }
/// <summary> /// Should this replayer be considered valid for this call? /// </summary> protected bool ShouldConsiderThisReplayer(IMethodRecorder replayer) { return replayersToIgnoreForThisCall.Contains(replayer) == false; }
/// <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) { }
/// <summary> /// Creates a new <see cref="MethodRecorderBase"/> instance. /// </summary> protected MethodRecorderBase(ProxyMethodExpectationsDictionary repeatableMethods) { this.repeatableMethods = repeatableMethods; recorderToCall = null; replayerToCall = null; }
/// <summary> /// Handle the real execution of this method for the derived class /// </summary> protected abstract void DoAddRecorder(IMethodRecorder recorder);
/// <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)); }
/// <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; }
protected override void DoAddRecorder(IMethodRecorder recorder) { DoAddRecorderCalled = true; }
/// <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); }
/// <summary> /// Handle the real execution of this method for the derived class /// </summary> protected override void DoAddRecorder(IMethodRecorder recorder) { recordedActions.Add(recorder); }
/// <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; } }
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(); }
/// <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> /// Moves to parent recorder. /// </summary> public void MoveToParentReplayer() { replayerToCall = null; if (parentRecorder == null || HasExpectations) return; parentRecorder.MoveToParentReplayer(); }
/// <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); }
/// <summary> /// Moves to previous recorder. /// </summary> public bool MoveToPreviousRecorder() { if (recorderToCall == null) return true; if (recorderToCall.MoveToPreviousRecorder()) recorderToCall = null; return false; }
/// <summary> /// Should this replayer be considered valid for this call? /// </summary> protected bool ShouldConsiderThisReplayer(IMethodRecorder replayer) { return(replayersToIgnoreForThisCall.Contains(replayer) == false); }
/// <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)); }