internal void RemoveOrderedCall(IProxyCall call)
 {
     lock (orderedCalls)
     {
         orderedCalls.Remove(call);
     }
 }
Esempio n. 2
0
        public void AddCall(IProxyCall call, SetupKind kind)
        {
            var expr = call.SetupExpression.PartialMatcherAwareEval();
            var keyText = call.Method.DeclaringType.FullName + "::" + expr.ToStringFixed(true);
            if (kind == SetupKind.PropertySet)
            {
                keyText = "set::" + keyText;
            }

            var constants = new ConstantsVisitor(expr).Values;
            var key = new ExpressionKey(keyText, constants);

            if (!call.IsConditional)
            {
                // if it's not a conditional call, we do
                // all the override setups.
                // TODO maybe add the conditionals to other
                // record like calls to be user friendly and display
                // somethig like: non of this calls were performed.
                if (calls.ContainsKey(key))
                {
                    // Remove previous from ordered calls
                    InterceptionContext.RemoveOrderedCall(calls[key]);
                }

                calls[key] = call;
            }

            InterceptionContext.AddOrderedCall(call);
        }
Esempio n. 3
0
        public InterceptionAction HandleIntercept(ICallContext invocation, InterceptorContext ctx, CurrentInterceptContext localctx)
        {
            if (FluentMockContext.IsActive)
            {
                localctx.Call = null;
            }
            else
            {
                IProxyCall lastMatchingSetup = null;
                IProxyCall lastMatchingSetupForSameMethod = null;
                foreach (var setup in ctx.OrderedCalls)
                {
                    if (setup.Matches(invocation))
                    {
                        lastMatchingSetup = setup;
                        if (setup.Method == invocation.Method)
                        {
                            lastMatchingSetupForSameMethod = setup;
                        }
                    }
                }
                localctx.Call = lastMatchingSetupForSameMethod ?? lastMatchingSetup;
            }

            if (localctx.Call != null)
            {
                localctx.Call.EvaluatedSuccessfully();
            }
            else if (!FluentMockContext.IsActive && ctx.Behavior == MockBehavior.Strict)
            {
                throw new MockException(MockException.ExceptionReason.NoSetup, ctx.Behavior, invocation);
            }

            return(InterceptionAction.Continue);
        }
 internal void AddOrderedCall(IProxyCall call)
 {
     lock (orderedCalls)
     {
         orderedCalls.Add(call);
     }
 }
Esempio n. 5
0
        public void AddCall(IProxyCall call, SetupKind kind)
        {
            var expr    = call.SetupExpression.PartialMatcherAwareEval();
            var keyText = call.Method.DeclaringType.FullName + "::" + expr.ToStringFixed(true);

            if (kind == SetupKind.PropertySet)
            {
                keyText = "set::" + keyText;
            }

            var constants = new ConstantsVisitor(expr).Values;
            var key       = new ExpressionKey(keyText, constants);

            if (!call.IsConditional)
            {
                // if it's not a conditional call, we do
                // all the override setups.
                // TODO maybe add the conditionals to other
                // record like calls to be user friendly and display
                // somethig like: non of this calls were performed.
                if (calls.ContainsKey(key))
                {
                    // Remove previous from ordered calls
                    orderedCalls.Remove(calls[key]);
                }

                calls[key] = call;
            }

            orderedCalls.Add(call);
        }
Esempio n. 6
0
 private static void ThrowIfReturnValueRequired(IProxyCall call, ICallContext invocation, InterceptorContext ctx)
 {
     if (ctx.Behavior != MockBehavior.Loose &&
         invocation.Method != null &&
         invocation.Method.ReturnType != null &&
         invocation.Method.ReturnType != typeof(void))
     {
         if (!(call is IMethodCallReturn methodCallReturn && methodCallReturn.ProvidesReturnValue()))
         {
             throw new MockException(
                       MockException.ExceptionReason.ReturnValueRequired,
                       ctx.Behavior,
                       invocation);
         }
     }
 }
Esempio n. 7
0
 private void ThrowIfReturnValueRequired(IProxyCall call, ICallContext invocation)
 {
     if (behavior != MockBehavior.Loose &&
         invocation.Method != null &&
         invocation.Method.ReturnType != null &&
         invocation.Method.ReturnType != typeof(void))
     {
         var methodCall = call as MethodCallReturn;
         if (methodCall == null || !methodCall.HasReturnValue)
         {
             throw new MockException(
                       MockException.ExceptionReason.ReturnValueRequired,
                       behavior,
                       invocation);
         }
     }
 }
Esempio n. 8
0
 private void ThrowIfReturnValueRequired(IProxyCall call, ICallContext invocation)
 {
     if (ctx.Behavior != MockBehavior.Loose &&
         invocation.Method != null &&
         invocation.Method.ReturnType != null &&
         invocation.Method.ReturnType != typeof(void))
     {
         var methodCall = call as MethodCallReturn;
         if (methodCall == null || !methodCall.HasReturnValue)
         {
             throw new MockException(
                 MockException.ExceptionReason.ReturnValueRequired,
                 ctx.Behavior,
                 invocation);
         }
     }
 }
Esempio n. 9
0
        private static SetupViewModel GetSetup(
            IProxyCall proxyCall,
            IDictionary <ICallContext, CallViewModel> actualCalls)
        {
            if (proxyCall.Invoked)
            {
                var setupCalls = actualCalls.Keys.Where(ac => proxyCall.Matches(ac))
                                 .Select(ac => GetSetupCall(actualCalls[ac]));
                return(new SetupViewModel(
                           proxyCall.SetupExpression.ToStringFixed(),
                           proxyCall.IsVerifiable,
                           proxyCall.IsNever,
                           CreateExpandedContainer <CallViewModel>("Invocations", setupCalls)));
            }

            return(new SetupViewModel(
                       proxyCall.SetupExpression.ToStringFixed(),
                       proxyCall.IsVerifiable,
                       proxyCall.IsNever));
        }
Esempio n. 10
0
        public InterceptionAction HandleIntercept(ICallContext invocation, InterceptorContext ctx, CurrentInterceptContext localctx)
        {
            IProxyCall currentCall = localctx.Call;

            if (currentCall != null)
            {
                currentCall.SetOutParameters(invocation);

                // We first execute, as there may be a Throws
                // and therefore we might never get to the
                // next line.
                currentCall.Execute(invocation);
                ThrowIfReturnValueRequired(currentCall, invocation, ctx);
                return(InterceptionAction.Stop);
            }
            else
            {
                return(InterceptionAction.Continue);
            }
        }
		private static string GetRawSetups(IProxyCall[] failedSetups)
		{
			return failedSetups.Aggregate(string.Empty, (s, call) => s + call.ToString() + Environment.NewLine);

			//var message = new StringBuilder();
			//foreach (var setup in failedSetups)
			//{
			//   if (setup.FailMessage != null)
			//   {
			//      message.Append(setup.FailMessage).Append(": ");
			//   }

			//   var lambda = setup.SetupExpression.PartialMatcherAwareEval().ToLambda();
			//   var targetTypeName = lambda.Parameters[0].Type.Name;

			//   message.Append(targetTypeName).Append(" ").Append(lambda.ToStringFixed());

			//   if (setup.TestMethod != null)
			//   {
			//      message.AppendFormat(
			//         " ({0}() in {1}: line {2})",
			//         setup.TestMethod.Name,
			//         setup.FileName,
			//         setup.FileLine);
			//   }

			//   message.AppendLine();
			//}

			//return message.ToString();
		}
		private static string GetMessage(IProxyCall[] failedSetups)
		{
			return string.Format(
				CultureInfo.CurrentCulture,
				Resources.VerficationFailed,
				GetRawSetups(failedSetups));
		}
		public MockVerificationException(IProxyCall[] failedSetups)
			: base(ExceptionReason.VerificationFailed, GetMessage(failedSetups))
		{
			this.failedSetups = failedSetups;
		}
 internal void RemoveOrderedCall(IProxyCall call)
 {
     lock (orderedCalls)
     {
         orderedCalls.Remove(call);
     }
 }
 internal void AddOrderedCall(IProxyCall call)
 {
     lock (orderedCalls)
     {
         orderedCalls.Add(call);
     }
 }