/// <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>
        /// 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);
        }
 private static bool MethodsEquals(MethodInfo method, ProxyMethodExpectationTriplet triplet)
 {
     if (method.IsGenericMethod == false)
     {
         return(triplet.Method == method);
     }
     return(triplet.Method.GetGenericMethodDefinition() == method.GetGenericMethodDefinition());
 }
        /// <summary>
        /// Records the specified call with the specified args on the mocked object.
        /// </summary>
        /// <param name="proxy">Mocked object.</param>
        /// <param name="method">Method.</param>
        /// <param name="expectation">Expectation.</param>
        protected override void DoRecord(object proxy, MethodInfo method, IExpectation expectation)
        {
            Validate.IsNotNull(proxy, "proxy");
            Validate.IsNotNull(method, "method");
            Validate.IsNotNull(expectation, "expectation");
            ProxyMethodExpectationTriplet entry = new ProxyMethodExpectationTriplet(proxy, method, expectation);

            recordedActions.Add(entry);
        }
示例#5
0
        /// <summary>
        /// Determines if the object equal to this instance
        /// </summary>
        /// <param name="obj">Obj.</param>
        /// <returns></returns>
        public override bool Equals(object obj)
        {
            ProxyMethodExpectationTriplet other = obj as ProxyMethodExpectationTriplet;

            if (other == null)
            {
                return(false);
            }
            return(method == other.method &&
                   MockedObjectsEquality.Instance.Compare(proxy, other.proxy) == 0 &&
                   expectation == other.expectation);
        }
 /// <summary>
 /// Handle the real execution of this method for the derived class
 /// </summary>
 protected override void DoRemoveExpectation(IExpectation expectation)
 {
     for (int i = 0; i < recordedActions.Count; i++)
     {
         ProxyMethodExpectationTriplet triplet = recordedActions[i] as ProxyMethodExpectationTriplet;
         if (triplet != null)
         {
             if (triplet.Expectation == expectation)
             {
                 recordedActions.RemoveAt(i);
             }
         }
         //Action cannot be another recorder, since then the RemoveExpectation would've
         //passed us to the top most recorder.
     }
 }
        /// <summary>
        /// Gets the next expected calls string.
        /// </summary>
        public override string GetExpectedCallsMessage()
        {
            StringBuilder sb = new StringBuilder();

            sb.Append("Unordered: { ");
            foreach (object action in recordedActions)
            {
                ProxyMethodExpectationTriplet triplet = action as ProxyMethodExpectationTriplet;
                if (triplet != null)
                {
                    sb.Append(triplet.Expectation.ErrorMessage);
                }
                else
                {
                    string nested = ((IMethodRecorder)action).GetExpectedCallsMessage();
                    sb.Append(nested);
                }
            }
            sb.Append(" }");
            return(sb.ToString());
        }
 /// <summary>
 /// Replaces the old expectation with the new expectation for the specified proxy/method pair.
 /// This replace ALL expectations that equal to old expectations.
 /// </summary>
 /// <param name="proxy">Proxy.</param>
 /// <param name="method">Method.</param>
 /// <param name="oldExpectation">Old expectation.</param>
 /// <param name="newExpectation">New expectation.</param>
 protected override void DoReplaceExpectation(object proxy, MethodInfo method, IExpectation oldExpectation, IExpectation newExpectation)
 {
     Validate.IsNotNull(proxy, "proxy");
     Validate.IsNotNull(method, "method");
     Validate.IsNotNull(oldExpectation, "oldExpectation");
     Validate.IsNotNull(newExpectation, "newExpectation");
     foreach (object action in recordedActions)
     {
         ProxyMethodExpectationTriplet triplet = action as ProxyMethodExpectationTriplet;
         if (triplet != null)
         {
             if (MockedObjectsEquality.Instance.Equals(triplet.Proxy, proxy) &&
                 triplet.Method == method &&
                 triplet.Expectation == oldExpectation)
             {
                 triplet.Expectation = newExpectation;
             }
         }
         //Action cannot be another recorder, since then the RemoveExpectation would've
         //passed us to the top most recorder.
     }
 }
示例#9
0
        /// <summary>
        /// Gets the next expected calls string.
        /// </summary>
        public override string GetExpectedCallsMessage()
        {
            StringBuilder sb = new StringBuilder();

            sb.Append("Ordered: { ");
            if (recordedActions.Count > 0)
            {
                ProxyMethodExpectationTriplet triplet = recordedActions[0] as ProxyMethodExpectationTriplet;
                if (triplet != null)
                {
                    sb.Append(triplet.Expectation.ErrorMessage);
                }
                else                 //Action is another recorder
                {
                    sb.Append(((IMethodRecorder)recordedActions[0]).GetExpectedCallsMessage());
                }
            }
            else
            {
                sb.Append("No method call is expected");
            }
            sb.Append(" }");
            return(sb.ToString());
        }
        public void EqualsTest()
        {
            ProxyInstance proxy1 = new ProxyInstance(null);
            ProxyInstance proxy2 = new ProxyInstance(null);
            MethodInfo method1 = typeof(string).GetMethod("StartsWith", new Type[] { typeof(string) }),
                method2 = endsWith;
            IExpectation expectation1 = new AnyArgsExpectation(new FakeInvocation(method1), new Range(1, 1)),
                expectation2 = new AnyArgsExpectation(new FakeInvocation(method2), new Range(1, 1));
            ProxyMethodExpectationTriplet same1 = new ProxyMethodExpectationTriplet(proxy1, method1, expectation1),
                same2 = new ProxyMethodExpectationTriplet(proxy1, method1, expectation1);
            Assert.AreEqual(same1, same2);
            Assert.AreEqual(same2, same1);

            ProxyMethodExpectationTriplet proxyDiff1 = new ProxyMethodExpectationTriplet(proxy1, method1, expectation1),
                proxyDiff2 = new ProxyMethodExpectationTriplet(proxy2, method1, expectation1);
            Assert.AreNotEqual(proxyDiff2, proxyDiff1);
            Assert.AreNotEqual(proxyDiff1, proxyDiff2);

            ProxyMethodExpectationTriplet methodDiff1 = new ProxyMethodExpectationTriplet(proxy1, method1, expectation1),
                methodDiff2 = new ProxyMethodExpectationTriplet(proxy1, method2, expectation1);

            Assert.AreNotEqual(methodDiff1, methodDiff2);
            Assert.AreNotEqual(methodDiff2, methodDiff1);

            ProxyMethodExpectationTriplet expectationDiff1 = new ProxyMethodExpectationTriplet(proxy1, method1, expectation1),
                expectationDiff2 = new ProxyMethodExpectationTriplet(proxy1, method1, expectation2);

            Assert.AreNotEqual(expectationDiff1, expectationDiff2);
            Assert.AreNotEqual(expectationDiff2, expectationDiff1);

            ProxyMethodExpectationTriplet allDiff1 = new ProxyMethodExpectationTriplet(proxy1, method1, expectation1),
                allDiff2 = new ProxyMethodExpectationTriplet(proxy2, method2, expectation2);

            Assert.AreNotEqual(allDiff1, allDiff2);
            Assert.AreNotEqual(allDiff2, allDiff1);
        }
 public void FalseOnEqualToNull()
 {
     ProxyMethodExpectationTriplet triplet = new ProxyMethodExpectationTriplet(proxy, this.endsWith, this.expectation);
     Assert.IsFalse(triplet.Equals(null));
 }
 private static bool MethodsEquals(MethodInfo method, ProxyMethodExpectationTriplet triplet)
 {
     if(method.IsGenericMethod==false)
         return triplet.Method == method;
     return triplet.Method.GetGenericMethodDefinition() == method.GetGenericMethodDefinition();
 }
 /// <summary>
 /// Records the specified call with the specified args on the mocked object.
 /// </summary>
 /// <param name="proxy">Mocked object.</param>
 /// <param name="method">Method.</param>
 /// <param name="expectation">Expectation.</param>
 protected override void DoRecord(object proxy, MethodInfo method, IExpectation expectation)
 {
     Validate.IsNotNull(proxy, "proxy");
     Validate.IsNotNull(method, "method");
     Validate.IsNotNull(expectation, "expectation");
     ProxyMethodExpectationTriplet entry = new ProxyMethodExpectationTriplet(proxy, method, expectation);
     recordedActions.Add(entry);
 }
示例#14
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));
        }
 public void ReturnSamevaluesAsInCtor()
 {
     ProxyMethodExpectationTriplet triplet = new ProxyMethodExpectationTriplet(proxy, this.endsWith, this.expectation);
     Assert.AreEqual(proxy, triplet.Proxy);
     Assert.AreEqual(endsWith, triplet.Method);
     Assert.AreEqual(expectation, triplet.Expectation);
 }
        /// <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));
        }
 public void GetHashCodeReturnSameValue()
 {
     ProxyMethodExpectationTriplet triplet = new ProxyMethodExpectationTriplet(proxy, this.endsWith, this.expectation);
     Assert.AreEqual(triplet.GetHashCode(), triplet.GetHashCode());
 }