public void Create_IfMultipleInputParameters_PassesInputArguments(string functionName)
        {
            // Arrange
            MethodInfo method    = GetMethodInfo(functionName);
            int        expectedA = 1;
            string     expectedB = "B";

            object[] expectedC = new object[] { new object() };

            // Act
            IMethodInvoker <DefaultMethodInvokerFactoryTests, object> invoker =
                _methodInvokerFactory.Create <DefaultMethodInvokerFactoryTests, object>(method);

            // Assert
            Assert.NotNull(invoker);
            bool callbackCalled = false;
            Action <int, string, object> callback = (a, b, c) =>
            {
                callbackCalled = true;
                Assert.Equal(expectedA, a);
                Assert.Same(expectedB, b);
                Assert.Same(expectedC, c);
            };
            DefaultMethodInvokerFactoryTests instance = GetInstance(!method.IsStatic);

            object[] arguments = new object[] { expectedA, expectedB, expectedC, callback };
            invoker.InvokeAsync(instance, arguments).GetAwaiter().GetResult();
            Assert.True(callbackCalled);
        }
        public void Create_IfInOutByRefMethodReturnsTask_RoundtripsArguments(string functionName)
        {
            // Arrange
            MethodInfo method           = GetMethodInfo(functionName);
            int        expectedA        = 1;
            string     expectedInitialB = "B";
            string     expectedFinalB   = "b";

            object[] expectedC = new object[] { new object(), default(int), String.Empty };

            // Act
            IMethodInvoker <DefaultMethodInvokerFactoryTests, object> invoker =
                _methodInvokerFactory.Create <DefaultMethodInvokerFactoryTests, object>(method);

            // Assert
            Assert.NotNull(invoker);
            bool             callbackCalled = false;
            InOutRefTaskFunc callback       = delegate(int a, ref string b, out object[] c)
            {
                callbackCalled = true;
                Assert.Equal(expectedA, a);
                Assert.Same(expectedInitialB, b);
                b = expectedFinalB;
                c = expectedC;
                return(Task.FromResult(0));
            };
            DefaultMethodInvokerFactoryTests instance = GetInstance(!method.IsStatic);

            object[] arguments = new object[] { expectedA, expectedInitialB, null, callback };
            invoker.InvokeAsync(instance, arguments).GetAwaiter().GetResult();
            Assert.True(callbackCalled);
            Assert.Same(expectedFinalB, arguments[1]);
            Assert.Same(expectedC, arguments[2]);
        }
        public void Create_IfMultipleOutputParameters_SetsOutputArguments(string functionName)
        {
            // Arrange
            MethodInfo method    = GetMethodInfo(functionName);
            int        expectedA = 1;
            string     expectedB = "B";

            object[] expectedC = new object[] { new object() };

            // Act
            IMethodInvoker <DefaultMethodInvokerFactoryTests, object> invoker =
                _methodInvokerFactory.Create <DefaultMethodInvokerFactoryTests, object>(method);

            // Assert
            Assert.NotNull(invoker);
            bool      callbackCalled = false;
            OutAction callback       = delegate(out int a, out string b, out object[] c)
            {
                callbackCalled = true;
                a = expectedA;
                b = expectedB;
                c = expectedC;
            };
            DefaultMethodInvokerFactoryTests instance = GetInstance(!method.IsStatic);

            object[] arguments = new object[] { default(int), null, null, callback };
            invoker.InvokeAsync(instance, arguments).GetAwaiter().GetResult();
            Assert.True(callbackCalled);
            Assert.Equal(expectedA, arguments[0]);
            Assert.Same(expectedB, arguments[1]);
            Assert.Same(expectedC, arguments[2]);
        }
        public void Create_IfReturnsTaskAndTaskCanceled_ReturnsCanceledTask(string functionName)
        {
            // Arrange
            MethodInfo method = GetMethodInfo(functionName);

            // Act
            IMethodInvoker <DefaultMethodInvokerFactoryTests, object> invoker =
                _methodInvokerFactory.Create <DefaultMethodInvokerFactoryTests, object>(method);

            // Assert
            DefaultMethodInvokerFactoryTests instance = GetInstance(!method.IsStatic);
            Task task = invoker.InvokeAsync(instance, null);

            Assert.NotNull(task);
            task.WaitUntilCompleted();
            Assert.Equal(TaskStatus.Canceled, task.Status);
        }
        public void Create_IfParameterlessMethod_CanInvoke(string functionName)
        {
            // Arrange
            MethodInfo method = GetMethodInfo(functionName);

            // Act
            IMethodInvoker <DefaultMethodInvokerFactoryTests, object> invoker =
                _methodInvokerFactory.Create <DefaultMethodInvokerFactoryTests, object>(method);

            try
            {
                // Assert
                DefaultMethodInvokerFactoryTests instance = GetInstance(!method.IsStatic);
                invoker.InvokeAsync(instance, null).GetAwaiter().GetResult();
                Assert.True(_parameterlessMethodCalled);
            }
            finally
            {
                _parameterlessMethodCalled = false;
            }
        }
        public void Create_IfMultipleReferenceParameters_RoundtripsArguments(string functionName)
        {
            // Arrange
            MethodInfo method           = GetMethodInfo(functionName);
            int        expectedInitialA = 1;
            string     expectedInitialB = "B";

            object[] expectedInitialC = new object[] { new object() };
            int      expectedFinalA   = 2;
            string   expectedFinalB   = "b";

            object[] expectedFinalC = new object[] { new object(), default(int), String.Empty };

            // Act
            IMethodInvoker <DefaultMethodInvokerFactoryTests, object> invoker =
                _methodInvokerFactory.Create <DefaultMethodInvokerFactoryTests, object>(method);

            // Assert
            Assert.NotNull(invoker);
            bool        callbackCalled = false;
            ByRefAction callback       = delegate(ref int a, ref string b, ref object[] c)
            {
                callbackCalled = true;
                Assert.Equal(expectedInitialA, a);
                Assert.Same(expectedInitialB, b);
                Assert.Same(expectedInitialC, c);
                a = expectedFinalA;
                b = expectedFinalB;
                c = expectedFinalC;
            };
            DefaultMethodInvokerFactoryTests instance = GetInstance(!method.IsStatic);

            object[] arguments = new object[] { expectedInitialA, expectedInitialB, expectedInitialC, callback };
            invoker.InvokeAsync(instance, arguments).GetAwaiter().GetResult();
            Assert.True(callbackCalled);
            Assert.Equal(expectedFinalA, arguments[0]);
            Assert.Same(expectedFinalB, arguments[1]);
            Assert.Same(expectedFinalC, arguments[2]);
        }