public void Create_IfInOutByRefMethodReturnsTask_RoundtripsArguments(bool isInstance) { // Arrange MethodInfo method = GetMethodInfo(isInstance, "TestInOutByRefReturnTask"); int expectedA = 1; string expectedInitialB = "B"; string expectedFinalB = "b"; object[] expectedC = new object[] { new object(), default(int), String.Empty }; // Act IMethodInvoker <MethodInvokerFactoryTests> invoker = MethodInvokerFactory.Create <MethodInvokerFactoryTests>(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)); }; MethodInvokerFactoryTests instance = GetInstance(isInstance); 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_IfMultipleInputParameters_PassesInputArguments(bool isInstance) { // Arrange MethodInfo method = GetMethodInfo(isInstance, "TestIntStringObjectArray"); int expectedA = 1; string expectedB = "B"; object[] expectedC = new object[] { new object() }; // Act IMethodInvoker <MethodInvokerFactoryTests> invoker = MethodInvokerFactory.Create <MethodInvokerFactoryTests>(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); }; MethodInvokerFactoryTests instance = GetInstance(isInstance); object[] arguments = new object[] { expectedA, expectedB, expectedC, callback }; invoker.InvokeAsync(instance, arguments).GetAwaiter().GetResult(); Assert.True(callbackCalled); }
public void Create_IfMultipleOutputParameters_SetsOutputArguments(bool isInstance) { // Arrange MethodInfo method = GetMethodInfo(isInstance, "TestOutIntStringObjectArray"); int expectedA = 1; string expectedB = "B"; object[] expectedC = new object[] { new object() }; // Act IMethodInvoker <MethodInvokerFactoryTests> invoker = MethodInvokerFactory.Create <MethodInvokerFactoryTests>(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; }; MethodInvokerFactoryTests instance = GetInstance(isInstance); 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_IfTReflectedIsNotReflectedType_Throws(bool isInstance) { // Arrange MethodInfo method = GetMethodInfo(isInstance, "ReturnVoid"); // Act & Assert ExceptionAssert.ThrowsInvalidOperation(() => MethodInvokerFactory.Create <object>(method), "TReflected must match the method's ReflectedType."); }
public void Create_IfMethodReturnsNonTask_Throws() { // Arrange MethodInfo method = GetMethodInfo("ReturnInt"); // Act & Assert ExceptionAssert.ThrowsNotSupported(() => MethodInvokerFactory.Create <MethodInvokerFactoryTests>(method), "Methods may only return void or Task."); }
public void Create_IfMethodIsNull_Throws() { // Arrange MethodInfo method = null; // Act & Assert ExceptionAssert.ThrowsArgumentNull(() => MethodInvokerFactory.Create <MethodInvokerFactoryTests>(method), "method"); }
public void Create_IfStaticMethodReturnsTask_ReturnsTaskInvoker(bool isInstance) { // Arrange MethodInfo method = GetMethodInfo(isInstance, "ReturnTask"); // Act IMethodInvoker <MethodInvokerFactoryTests> invoker = MethodInvokerFactory.Create <MethodInvokerFactoryTests>(method); // Assert Assert.IsType <TaskMethodInvoker <MethodInvokerFactoryTests> >(invoker); }
public void Create_IfStaticMethodReturnsVoid_ReturnsVoidInvoker(bool isInstance) { // Arrange MethodInfo method = GetMethodInfo(isInstance, "ReturnVoid"); // Act IMethodInvoker <MethodInvokerFactoryTests, object> invoker = MethodInvokerFactory.Create <MethodInvokerFactoryTests, object>(method); // Assert Assert.IsType <VoidMethodInvoker <MethodInvokerFactoryTests, object> >(invoker); }
public void Create_IfReturnsTaskAndTaskCanceled_ReturnsCanceledTask(bool isInstance) { // Arrange MethodInfo method = GetMethodInfo(isInstance, "ReturnCanceledTask"); // Act IMethodInvoker <MethodInvokerFactoryTests> invoker = MethodInvokerFactory.Create <MethodInvokerFactoryTests>(method); // Assert MethodInvokerFactoryTests instance = GetInstance(isInstance); Task task = invoker.InvokeAsync(instance, null); Assert.NotNull(task); task.WaitUntilCompleted(); Assert.Equal(TaskStatus.Canceled, task.Status); }
public void Create_IfParameterlessMethod_CanInvoke(bool isInstance) { // Arrange MethodInfo method = GetMethodInfo(isInstance, "ParameterlessMethod"); // Act IMethodInvoker <MethodInvokerFactoryTests> invoker = MethodInvokerFactory.Create <MethodInvokerFactoryTests>(method); try { // Assert MethodInvokerFactoryTests instance = GetInstance(isInstance); invoker.InvokeAsync(instance, null).GetAwaiter().GetResult(); Assert.True(_parameterlessMethodCalled); } finally { _parameterlessMethodCalled = false; } }
public void Create_IfMultipleReferenceParameters_RoundtripsArguments(bool isInstance) { // Arrange MethodInfo method = GetMethodInfo(isInstance, "TestByRefIntStringObjectArray"); 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 <MethodInvokerFactoryTests> invoker = MethodInvokerFactory.Create <MethodInvokerFactoryTests>(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; }; MethodInvokerFactoryTests instance = GetInstance(isInstance); 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]); }
public void CreateTest() { MethodInvokerFactory target = new MethodInvokerFactory(); MethodInfo[] myMethodInfo; Type myType = typeof(MyClass); // Get the type and fields of FieldInfoClass. myMethodInfo = myType.GetMethods(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public); MethodInfo key = myMethodInfo[0]; IMethodInvoker actual; actual = target.Create(key); Assert.IsNotNull(actual); }