Exemple #1
0
        public bool ShouldPass(int channel, IMethod method)
        {
            if (method.SentOnValidChannel(channel) == false)
            {
                throw new CommandInvalidException($"{ method.GetType()} method is not valid on channel {channel}.");
            }

            if (channel > _channelMax)
            {
                throw new ChannelErrorException($"Channel {channel} not allowed. Maximum channel allowed is {_channelMax}.");
            }

            var methodExpectation = _expectationManager.Get <MethodExpectation>(channel);

            if (methodExpectation.MethodResponses.Any())
            {
                if (methodExpectation.MethodResponses.Contains(method.GetType()) == false)
                {
                    throw new UnexpectedFrameException($"Did not expect { method.GetType().FullName}. Expected: {string.Join(", ", methodExpectation.MethodResponses.Select(type => type.FullName))}.");
                }
            }

            if (method is Connection.TuneOk tuneOk)
            {
                _channelMax = tuneOk.ChannelMax.Value == 0 ? short.MaxValue : tuneOk.ChannelMax.Value;
                _frameMax   = tuneOk.FrameMax.Value == 0 ? long.MaxValue : tuneOk.FrameMax.Value;
            }

            if (method is IContentMethod contentMethod)
            {
                _expectationManager.Set(channel, new ContentHeaderExpectation());
                _contentMethodStates[channel] = contentMethod;
                return(false);
            }

            methodExpectation = new MethodExpectation(_expectedMethodManager.GetExpectingMethodsFor(method.GetType()));

            _expectationManager.Set(channel, methodExpectation);

            return(true);
        }
 public VerificationException(MethodInfo method, MethodExpectation expectation)
     : base(String.Format(MESSAGE, method.GetFullName(), expectation.Message))
 {
 }
        public override void Execute(Invocation invocation)
        {
            Debug.Assert(invocation.Method != null);
            Debug.Assert(invocation.Method.ReturnType != null);

            var method = invocation.Method;

            if (this.mock.CallBase)
            {
#if FEATURE_DEFAULT_INTERFACE_IMPLEMENTATIONS
                var tryCallDefaultInterfaceImplementation = false;
#endif

                var declaringType = method.DeclaringType;
                if (declaringType.IsInterface)
                {
                    if (this.mock.MockedType.IsInterface)
                    {
                        // Case 1: Interface method of an interface proxy.

#if FEATURE_DEFAULT_INTERFACE_IMPLEMENTATIONS
                        // Fall through to invoke default implementation (if one exists).
                        tryCallDefaultInterfaceImplementation = true;
#else
                        // There is no base method to call, so fall through.
#endif
                    }
                    else
                    {
                        Debug.Assert(mock.MockedType.IsClass);
                        Debug.Assert(mock.ImplementsInterface(declaringType));

                        // Case 2: Explicitly implemented interface method of a class proxy.

                        if (this.mock.InheritedInterfaces.Contains(declaringType))
                        {
                            // Case 2a: Re-implemented interface.
                            // The base class has its own implementation. Only call base method if it isn't an event accessor.
                            if (!method.IsEventAddAccessor() && !method.IsEventRemoveAccessor())
                            {
                                invocation.ReturnValue = invocation.CallBase();
                                return;
                            }
                        }
                        else
                        {
                            Debug.Assert(this.mock.AdditionalInterfaces.Contains(declaringType));

                            // Case 2b: Additional interface.

#if FEATURE_DEFAULT_INTERFACE_IMPLEMENTATIONS
                            // Fall through to invoke default implementation (if one exists).
                            tryCallDefaultInterfaceImplementation = true;
#else
                            // There is no base method to call, so fall through.
#endif
                        }
                    }
                }
                else
                {
                    Debug.Assert(declaringType.IsClass);

                    // Case 3: Non-interface method of a class proxy.
                    // Only call base method if it isn't abstract.
                    if (!method.IsAbstract)
                    {
                        invocation.ReturnValue = invocation.CallBase();
                        return;
                    }
                }

#if FEATURE_DEFAULT_INTERFACE_IMPLEMENTATIONS
                if (tryCallDefaultInterfaceImplementation && !method.IsAbstract)
                {
                    // Invoke default implementation.
                    invocation.ReturnValue = invocation.CallBase();
                    return;
                }
#endif
            }

            if (method.ReturnType != typeof(void))
            {
                var returnValue = this.mock.GetDefaultValue(method, out var innerMock);
                if (innerMock != null && invocation.MatchingSetup == null)
                {
                    var setup = new InnerMockSetup(originalExpression: null, this.mock, expectation: MethodExpectation.CreateFrom(invocation), returnValue);
                    this.mock.MutableSetups.Add(setup);
                    setup.Execute(invocation);
                }
                else
                {
                    invocation.ReturnValue = returnValue;
                }
            }
        }