Exemple #1
0
            public void Apply(IInterceptedFakeObjectCall fakeObjectCall)
            {
                Guard.AgainstNull(fakeObjectCall, nameof(fakeObjectCall));

                var returnType = fakeObjectCall.Method.ReturnType;

                if (typeof(Task).GetTypeInfo().IsAssignableFrom(returnType))
                {
                    Task task;
                    if (returnType == typeof(Task))
                    {
                        task = TaskHelper.Canceled();
                    }
                    else
                    {
                        var taskResultType = returnType.GetTypeInfo().GetGenericArguments()[0];
                        task = TaskHelper.Canceled(taskResultType);
                    }

                    fakeObjectCall.SetReturnValue(task);
                }
                else
                {
                    var token = GetCanceledTokens(fakeObjectCall).First();
                    token.ThrowIfCancellationRequested();
                }
            }
        public override Task WriteToStreamAsync(Type type, object value, Stream writeStream, HttpContent content, TransportContext transportContext, CancellationToken cancellationToken)
        {
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }
            if (writeStream == null)
            {
                throw new ArgumentNullException(nameof(writeStream));
            }

            if (cancellationToken.IsCancellationRequested)
            {
                return(TaskHelper.Canceled());
            }

            try
            {
                this.WriteToStream(type, value, writeStream, content);
                return(TaskHelper.Completed());
            }
            catch (Exception ex)
            {
                return(TaskHelper.FromError(ex));
            }
        }
            public override void Apply(IInterceptedFakeObjectCall fakeObjectCall)
            {
                Guard.AgainstNull(fakeObjectCall, nameof(fakeObjectCall));

                var returnType = fakeObjectCall.Method.ReturnType;

                if (typeof(Task).GetTypeInfo().IsAssignableFrom(returnType))
                {
                    Task task;
                    if (returnType == typeof(Task))
                    {
                        task = TaskHelper.Canceled();
                    }
                    else
                    {
                        var taskResultType = returnType.GetTypeInfo().GetGenericArguments()[0];
                        task = TaskHelper.Canceled(taskResultType);
                    }

                    fakeObjectCall.SetReturnValue(task);
                }
                else if (IsValueTask(returnType, out var taskResultType))
                {
                    if (taskResultType == null)
                    {
                        var canceledTask = TaskHelper.Canceled();
                        var ctor         = returnType.GetConstructor(new[] { typeof(Task) });
                        var valueTask    = ctor.Invoke(new object[] { canceledTask });
                        fakeObjectCall.SetReturnValue(valueTask);
                    }
                    else
                    {
                        var canceledTask = TaskHelper.Canceled(taskResultType);
                        var ctor         = returnType.GetConstructor(new[] { canceledTask.GetType() });
                        var valueTask    = ctor.Invoke(new object[] { canceledTask });
                        fakeObjectCall.SetReturnValue(valueTask);
                    }
                }
                else
                {
                    GetCanceledToken(fakeObjectCall)?.ThrowIfCancellationRequested();
                }
            }