Exemple #1
0
        private void SetReturnDelegate(Delegate value)
        {
            if (value != null)
            {
                ValidateReturnDelegate(value);
            }

            this.valueDel        = value;
            this.returnValueKind = ReturnValueKind.Explicit;
        }
Exemple #2
0
        private void SetReturnDelegate(Delegate value)
        {
            if (value == null)
            {
                this.valueDel = (Func <TResult>)(() => default(TResult));
            }
            else
            {
                this.valueDel = value;
            }

            this.returnValueKind = ReturnValueKind.Explicit;
        }
Exemple #3
0
        private void SetReturnDelegate(Delegate value)
        {
            if (value == null)
            {
                // A `null` reference (instead of a valid delegate) is interpreted as the actual return value.
                // This is necessary because the compiler might have picked the unexpected overload for calls like `Returns(null)`,
                // or the user might have picked an overload like `Returns<T>(null)`,
                // and instead of in `Returns(TResult)`, we ended up in `Returns(Delegate)` or `Returns(Func)`,
                // which likely isn't what the user intended.
                // So here we do what we would've done in `Returns(TResult)`:
                this.valueDel = (Func <TResult>)(() => default(TResult));
            }
            else
            {
                ValidateReturnDelegate(value);
                this.valueDel = value;
            }

            this.returnValueKind = ReturnValueKind.Explicit;
        }
Exemple #4
0
 public override void SetCallBaseResponse()
 {
     this.returnValueKind = ReturnValueKind.CallBase;
 }
Exemple #5
0
        public void SetReturnsResponse(Delegate value)
        {
            if (value == null)
            {
                // A `null` reference (instead of a valid delegate) is interpreted as the actual return value.
                // This is necessary because the compiler might have picked the unexpected overload for calls
                // like `Returns(null)`, or the user might have picked an overload like `Returns<T>(null)`,
                // and instead of in `Returns(TResult)`, we ended up in `Returns(Delegate)` or `Returns(Func)`,
                // which likely isn't what the user intended.
                // So here we do what we would've done in `Returns(TResult)`:
                this.valueDel = new Func <object>(() => this.ReturnType.GetDefaultValue());
            }
            else if (this.ReturnType == typeof(Delegate))
            {
                // If `TResult` is `Delegate`, that is someone is setting up the return value of a method
                // that returns a `Delegate`, then we have arrived here because C# picked the wrong overload:
                // We don't want to invoke the passed delegate to get a return value; the passed delegate
                // already is the return value.
                this.valueDel = new Func <Delegate>(() => value);
            }
            else
            {
                ValidateCallback(value);
                this.valueDel = value;
            }

            this.returnValueKind = ReturnValueKind.Explicit;

            void ValidateCallback(Delegate callback)
            {
                var callbackMethod = callback.GetMethodInfo();

                // validate number of parameters:

                var numberOfActualParameters = callbackMethod.GetParameters().Length;

                if (callbackMethod.IsStatic)
                {
                    if (callbackMethod.IsExtensionMethod() || callback.Target != null)
                    {
                        numberOfActualParameters--;
                    }
                }

                if (numberOfActualParameters > 0)
                {
                    var numberOfExpectedParameters = this.Method.GetParameters().Length;
                    if (numberOfActualParameters != numberOfExpectedParameters)
                    {
                        throw new ArgumentException(
                                  string.Format(
                                      CultureInfo.CurrentCulture,
                                      Resources.InvalidCallbackParameterCountMismatch,
                                      numberOfExpectedParameters,
                                      numberOfActualParameters));
                    }
                }

                // validate return type:

                var actualReturnType = callbackMethod.ReturnType;

                if (actualReturnType == typeof(void))
                {
                    throw new ArgumentException(Resources.InvalidReturnsCallbackNotADelegateWithReturnType);
                }

                var expectedReturnType = this.Method.ReturnType;

                if (!expectedReturnType.IsAssignableFrom(actualReturnType))
                {
                    throw new ArgumentException(
                              string.Format(
                                  CultureInfo.CurrentCulture,
                                  Resources.InvalidCallbackReturnTypeMismatch,
                                  expectedReturnType,
                                  actualReturnType));
                }
            }
        }
Exemple #6
0
 public IReturnsResult <TMock> CallBase()
 {
     this.returnValueKind = ReturnValueKind.CallBase;
     return(this);
 }