Пример #1
0
        public void WhenInvokeMethodContextHasResultSetAndThenExceptionItShouldHaveExceptionAndNoResult()
        {
            // Given
            var method = typeof(InvokeMethodTests).GetMethod(nameof(WhenInvokeMethodContextIsCreatedItShouldNotHaveResult)) !;

            var parameters = new object[0];

            var context = new InvokeMethodContext(
                method,
                parameters,
                this);

            var exception = new Exception();
            var result    = new object();

            // When
            context.SetResult(result);
            context.SetException(exception);

            // Then
            context.HasResult.Should().BeFalse();
            context.Result.Should().BeNull();
            context.HasException.Should().BeTrue();
            context.Exception.Should().BeSameAs(exception);
        }
Пример #2
0
        public void WhenInvokeMethodContextIsNotSetTypedReturnShouldFail()
        {
            // Given
            var method = typeof(InvokeMethodTests).GetMethod(nameof(WhenInvokeMethodContextIsCreatedItShouldNotHaveResult)) !;

            var parameters = new object[0];

            var context = new InvokeMethodContext(
                method,
                parameters,
                this);

            // Then
            Action action = () => context.Return <int>();

            action.Should()
            .Throw <InvokeMethodWithoutResultException>();
        }
Пример #3
0
        public static async Task CallAsyncFunc(InvokeMethodContext context, CancellationToken cancellationToken)
        {
            try
            {
                var task = (Task)context.Method.Invoke(
                    obj: context.Instance,
                    parameters: context.Parameters) !;

                await task;

                var taskType = task.GetType();
                if (taskType.IsGenericType && taskType.GetGenericTypeDefinition() == typeof(Task <>))
                {
                    var property = TaskResultPropertyCache
                                   .GetOrAdd(taskType, t => taskType.GetProperty(
                                                 name: nameof(Task <string> .Result),
                                                 bindingAttr: BindingFlags.Public | BindingFlags.Instance,
                                                 binder: default,
Пример #4
0
        public static void CallSyncFunc(InvokeMethodContext context)
        {
            try
            {
                var result = context.Method.Invoke(
                    obj: context.Instance,
                    parameters: context.Parameters);

                context.SetResult(result);
            }
            catch (TargetInvocationException exception)
            {
                context.SetException(exception.InnerException ?? exception);
            }
            catch (Exception exception)
            {
                context.SetException(exception);
            }
        }
Пример #5
0
        public void WhenInvokeMethodContextHasResultTypedReturnShouldNotFail()
        {
            // Given
            var method = typeof(InvokeMethodTests).GetMethod(nameof(WhenInvokeMethodContextIsCreatedItShouldNotHaveResult)) !;

            var parameters = new object[0];

            var context = new InvokeMethodContext(
                method,
                parameters,
                this);

            var result = 42;

            // When
            context.SetResult(result);

            // Then
            context.Return <int>().Should().Be(42);
        }
Пример #6
0
        public void WhenInvokeMethodContextIsCreatedItShouldNotHaveResult()
        {
            // Given
            var method = typeof(InvokeMethodTests).GetMethod(nameof(WhenInvokeMethodContextIsCreatedItShouldNotHaveResult)) !;

            var parameters = new object[0];

            var context = new InvokeMethodContext(
                method,
                parameters,
                this);

            // Then
            context.Method.Should().BeSameAs(method);
            context.Parameters.Should().BeSameAs(parameters);
            context.Instance.Should().BeSameAs(this);
            context.HasResult.Should().BeFalse();
            context.Result.Should().BeNull();
            context.HasException.Should().BeFalse();
            context.Exception.Should().BeNull();
        }
Пример #7
0
        public void WhenInvokeMethodContextHasExceptionTypedReturnShouldFail()
        {
            // Given
            var method = typeof(InvokeMethodTests).GetMethod(nameof(WhenInvokeMethodContextIsCreatedItShouldNotHaveResult)) !;

            var parameters = new object[0];

            var context = new InvokeMethodContext(
                method,
                parameters,
                this);

            var exception = new FormatException();

            // When
            context.SetException(exception);

            // Then
            Action action = () => context.Return <int>();

            action.Should()
            .Throw <TargetInvocationException>()
            .WithInnerException <FormatException>();
        }