Example #1
0
        public async Task AsyncAction_WithVoidReturnType()
        {
            var methodWithVoidReturnType = new MethodWithVoidReturnType(TestController.VoidAction);
            var result = await ReflectedActionExecutor.ExecuteAsync(
                methodWithVoidReturnType.GetMethodInfo(),
                null,
                (IDictionary <string, object>) null);

            Assert.Same(null, result);
        }
Example #2
0
        public async Task SyncAction()
        {
            string inputString = "hello";
            var    syncMethod  = new SyncMethod(_controller.Echo);
            var    result      = await ReflectedActionExecutor.ExecuteAsync(
                syncMethod.GetMethodInfo(),
                _controller,
                new Dictionary <string, object>() { { "input", inputString } });

            Assert.Equal(inputString, result);
        }
Example #3
0
        public async Task AsyncAction_TaskOfTaskOfValueReturnType()
        {
            int    inputParam1      = 1;
            string inputParam2      = "Second Parameter";
            var    actionParameters = new Dictionary <string, object> {
                { "i", inputParam1 }, { "s", inputParam2 }
            };

            var methodWithTaskOfTaskOfIntReturnType = new MethodWithTaskOfTaskOfIntReturnType(_controller.TaskOfTaskAction);
            var result = await(Task <int>)(await ReflectedActionExecutor.ExecuteAsync(
                                               methodWithTaskOfTaskOfIntReturnType.GetMethodInfo(),
                                               _controller,
                                               actionParameters));

            Assert.Equal(inputParam1, result);
        }
Example #4
0
        public async Task SyncAction_WithException()
        {
            string inputString       = "hello";
            var    syncMethod        = new SyncMethod(_controller.EchoWithException);
            var    expectedException = "The method or operation is not implemented.";

            await AssertThrowsAsync <NotImplementedException>(
                async() =>
                await ReflectedActionExecutor.ExecuteAsync(
                    syncMethod.GetMethodInfo(),
                    _controller,
                    new Dictionary <string, object>()
            {
                { "input", inputString }
            }),
                expectedException);
        }
Example #5
0
        public async Task AsyncAction_WithAsyncKeywordThrows()
        {
            int    inputParam1      = 1;
            string inputParam2      = "Second Parameter";
            var    actionParameters = new Dictionary <string, object> {
                { "i", inputParam1 }, { "s", inputParam2 }
            };

            var methodWithTaskOfIntReturnType = new MethodWithTaskOfIntReturnType(_controller.TaskActionWithException);

            await AssertThrowsAsync <NotImplementedException>(
                async() =>
                await ReflectedActionExecutor.ExecuteAsync(
                    methodWithTaskOfIntReturnType.GetMethodInfo(),
                    _controller,
                    actionParameters),
                "Not Implemented Exception");
        }
Example #6
0
        public async Task ParametersInRandomOrder()
        {
            int    inputParam1 = 1;
            string inputParam2 = "Second Parameter";

            // Note that the order of parameters is reversed
            var actionParameters = new Dictionary <string, object> {
                { "s", inputParam2 }, { "i", inputParam1 }
            };
            var methodWithTaskOfIntReturnType = new MethodWithTaskOfIntReturnType(_controller.TaskValueTypeAction);

            var result = await ReflectedActionExecutor.ExecuteAsync(
                methodWithTaskOfIntReturnType.GetMethodInfo(),
                _controller,
                actionParameters);

            Assert.Equal(inputParam1, result);
        }
Example #7
0
        public async Task AsyncAction_WithDynamicReturnTypeThrows()
        {
            int    inputParam1      = 1;
            string inputParam2      = "Second Parameter";
            var    actionParameters = new Dictionary <string, object> {
                { "i", inputParam1 }, { "s", inputParam2 }
            };

            var    dynamicTaskMethod = new ReturnTaskAsDynamicValue(_controller.ReturnTaskAsDynamicValue);
            string expectedException = string.Format(
                CultureInfo.CurrentCulture,
                "The method 'ReturnTaskAsDynamicValue' on type '{0}' returned a Task instance even though it is not an asynchronous method.",
                typeof(TestController));

            await AssertThrowsAsync <InvalidOperationException>(
                async() =>
                await ReflectedActionExecutor.ExecuteAsync(
                    dynamicTaskMethod.GetMethodInfo(),
                    _controller,
                    actionParameters),
                expectedException);
        }
Example #8
0
        public async Task AsyncAction_ReturningUnwrappedTaskThrows()
        {
            int    inputParam1      = 1;
            string inputParam2      = "Second Parameter";
            var    actionParameters = new Dictionary <string, object> {
                { "i", inputParam1 }, { "s", inputParam2 }
            };

            var methodWithUnwrappedTask = new MethodWithTaskReturnType(_controller.UnwrappedTask);

            await AssertThrowsAsync <InvalidOperationException>(
                async() =>
                await ReflectedActionExecutor.ExecuteAsync(
                    methodWithUnwrappedTask.GetMethodInfo(),
                    _controller,
                    actionParameters),
                string.Format(CultureInfo.CurrentCulture,
                              "The method 'UnwrappedTask' on type '{0}' returned an instance of '{1}'. Make sure to call Unwrap on the returned value to avoid unobserved faulted Task.",
                              typeof(TestController),
                              typeof(Task <Task>).FullName
                              ));
        }
Example #9
0
        public async Task InvalidParameterValueThrows()
        {
            string inputParam2 = "Second Parameter";

            var actionParameters = new Dictionary <string, object> {
                { "i", "Some Invalid Value" }, { "s", inputParam2 }
            };
            var methodWithTaskOfIntReturnType = new MethodWithTaskOfIntReturnType(_controller.TaskValueTypeAction);
            var expectedException             = string.Format(
                CultureInfo.CurrentCulture,
                "Object of type '{0}' cannot be converted to type '{1}'.",
                typeof(string),
                typeof(int));

            // If it is an unrecognized derived type we throw an InvalidOperationException.
            await AssertThrowsAsync <ArgumentException>(
                async() =>
                await ReflectedActionExecutor.ExecuteAsync(
                    methodWithTaskOfIntReturnType.GetMethodInfo(),
                    _controller,
                    actionParameters),
                expectedException);
        }
Example #10
0
        public async Task AsyncAction_WithCustomTaskReturnTypeThrows()
        {
            int    inputParam1      = 1;
            string inputParam2      = "Second Parameter";
            var    actionParameters = new Dictionary <string, object> {
                { "i", inputParam1 }, { "s", inputParam2 }
            };

            // If it is an unrecognized derived type we throw an InvalidOperationException.
            var methodWithCutomTaskReturnType = new MethodWithCustomTaskReturnType(_controller.TaskActionWithCustomTaskReturnType);

            string expectedException = string.Format(
                CultureInfo.CurrentCulture,
                "The method 'TaskActionWithCustomTaskReturnType' on type '{0}' returned a Task instance even though it is not an asynchronous method.",
                typeof(TestController));

            await AssertThrowsAsync <InvalidOperationException>(
                async() =>
                await ReflectedActionExecutor.ExecuteAsync(
                    methodWithCutomTaskReturnType.GetMethodInfo(),
                    _controller,
                    actionParameters),
                expectedException);
        }