internal static Exception Intercept(object invocation) { var invocationDescriptor = GetInvocationDescriptor(invocation); #if NET_4_0 || NET_4_5 || PORTABLE if (AsyncInvocationRegion.IsAsyncOperation(invocationDescriptor.Delegate)) { using (var region = AsyncInvocationRegion.Create(invocationDescriptor.Delegate)) { try { object result = invocationDescriptor.Invoke(); region.WaitForPendingOperationsToComplete(result); return(null); } catch (Exception ex) { return(ex); } } } else #endif { try { invocationDescriptor.Invoke(); return(null); } catch (Exception ex) { return(ex); } } }
private object RunTestMethod(TestExecutionContext context) { // [DuongNT]: Sleep 50 ms to reduce CPU consumption ++TSettings.CurTCIndex; if (TSettings.CurTCIndex % 10 == 0) { Thread.Sleep(2000); } else { Thread.Sleep(TSettings.GetInstance().GetDefaultTCDelay()); } TUnit.TLogger.Write("##### Running Test Case [" + TSettings.CurTCIndex + "]: " + testMethod.Method.TypeInfo.FullName + "." + testMethod.Method.Name); #if NET_4_0 || NET_4_5 || PORTABLE if (AsyncInvocationRegion.IsAsyncOperation(testMethod.Method.MethodInfo)) { return(RunAsyncTestMethod(context)); } else #endif return(RunNonAsyncTestMethod(context)); }
/// <summary> /// Test whether the constraint is satisfied by an /// ActualValueDelegate that returns the value to be tested. /// The default implementation simply evaluates the delegate /// but derived classes may override it to provide for delayed /// processing. /// </summary> /// <param name="del">An <see cref="ActualValueDelegate{T}"/></param> /// <returns>True for success, false for failure</returns> public virtual bool Matches <T>(ActualValueDelegate <T> del) { if (AsyncInvocationRegion.IsAsyncOperation(del)) { using (var region = AsyncInvocationRegion.Create(del)) return(Matches(region.WaitForPendingOperationsToComplete(del()))); } return(Matches(del())); }
private static object InvokeDelegate <T>(ActualValueDelegate <T> del) { if (AsyncInvocationRegion.IsAsyncOperation(del)) { using (AsyncInvocationRegion region = AsyncInvocationRegion.Create(del)) return(region.WaitForPendingOperationsToComplete(del())); } return(del()); }
/// <summary> /// Applies the constraint to an ActualValueDelegate that returns /// the value to be tested. The default implementation simply evaluates /// the delegate but derived classes may override it to provide for /// delayed processing. /// </summary> /// <param name="del">An ActualValueDelegate</param> /// <returns>A ConstraintResult</returns> public virtual ConstraintResult ApplyTo <TActual>(ActualValueDelegate <TActual> del) { #if ASYNC if (AsyncInvocationRegion.IsAsyncOperation(del)) { using (var region = AsyncInvocationRegion.Create(del)) return(ApplyTo(region.WaitForPendingOperationsToComplete(del()))); } #endif return(ApplyTo(GetTestObject(del))); }
private void RunSetUpOrTearDownMethod(TestExecutionContext context, MethodInfo method) { #if ASYNC if (AsyncInvocationRegion.IsAsyncOperation(method)) { RunAsyncMethod(method, context); } else #endif RunNonAsyncMethod(method, context); }
private object RunTestMethod(TestExecutionContext context) { #if ASYNC if (AsyncInvocationRegion.IsAsyncOperation(testMethod.Method.MethodInfo)) { return(RunAsyncTestMethod(context)); } else #endif return(RunNonAsyncTestMethod(context)); }
/// <summary> /// Applies the constraint to an ActualValueDelegate that returns /// the value to be tested. The default implementation simply evaluates /// the delegate but derived classes may override it to provide for /// delayed processing. /// </summary> /// <param name="del">An ActualValueDelegate</param> /// <returns>A ConstraintResult</returns> public virtual ConstraintResult ApplyTo <TActual>(ActualValueDelegate <TActual> del) { #if NET_4_0 || NET_4_5 || PORTABLE if (AsyncInvocationRegion.IsAsyncOperation(del)) { using (var region = AsyncInvocationRegion.Create(del)) return(ApplyTo(region.WaitForPendingOperationsToComplete(del()))); } #endif return(ApplyTo(del())); }
private object RunTestMethod(TestExecutionContext context) { #if NET_4_0 || NET_4_5 || PORTABLE if (AsyncInvocationRegion.IsAsyncOperation(testMethod.Method)) { return(RunAsyncTestMethod(context)); } else #endif return(RunNonAsyncTestMethod(context)); }
public void RunSetUpOrTearDownMethod(TestExecutionContext context, MethodInfo method) { #if NET_4_0 || NET_4_5 || PORTABLE if (AsyncInvocationRegion.IsAsyncOperation(method)) { RunAsyncMethod(method, context); } else #endif RunNonAsyncMethod(method, context); }
private static object InvokeDelegate <T>(ActualValueDelegate <T> del) { #if NET_4_0 || NET_4_5 || NETSTANDARD1_3 || NETSTANDARD1_6 if (AsyncInvocationRegion.IsAsyncOperation(del)) { using (AsyncInvocationRegion region = AsyncInvocationRegion.Create(del)) return(region.WaitForPendingOperationsToComplete(del())); } #endif return(del()); }
internal static Exception Intercept(object invocation) { var invocationDescriptor = GetInvocationDescriptor(invocation); #if ASYNC if (AsyncInvocationRegion.IsAsyncOperation(invocationDescriptor.Delegate)) { using (var region = AsyncInvocationRegion.Create(invocationDescriptor.Delegate)) { try { object result = invocationDescriptor.Invoke(); region.WaitForPendingOperationsToComplete(result); return(null); } catch (Exception ex) { return(ex); } } } else #endif { using (new TestExecutionContext.IsolatedContext()) { try { invocationDescriptor.Invoke(); return(null); } catch (Exception ex) { return(ex); } } } }
/// <summary> /// Helper method that checks the signature of a TestMethod and /// any supplied parameters to determine if the test is valid. /// /// Currently, NUnitTestMethods are required to be public, /// non-abstract methods, either static or instance, /// returning void. They may take arguments but the _values must /// be provided or the TestMethod is not considered runnable. /// /// Methods not meeting these criteria will be marked as /// non-runnable and the method will return false in that case. /// </summary> /// <param name="testMethod">The TestMethod to be checked. If it /// is found to be non-runnable, it will be modified.</param> /// <param name="parms">Parameters to be used for this test, or null</param> /// <returns>True if the method signature is valid, false if not</returns> private static bool CheckTestMethodSignature(TestMethod testMethod, ParameterSet parms) { if (testMethod.Method.IsAbstract) { return(MarkAsNotRunnable(testMethod, "Method is abstract")); } if (!testMethod.Method.IsPublic) { return(MarkAsNotRunnable(testMethod, "Method is not public")); } ParameterInfo[] parameters; #if NETCF if (testMethod.Method.IsGenericMethodDefinition) { if (parms != null && parms.Arguments != null) { testMethod.Method = testMethod.Method.MakeGenericMethodEx(parms.Arguments); if (testMethod.Method == null) { return(MarkAsNotRunnable(testMethod, "Cannot determine generic types by probing")); } parameters = testMethod.Method.GetParameters(); } else { parameters = new ParameterInfo[0]; } } else #endif parameters = testMethod.Method.GetParameters(); #if !NETCF int minArgsNeeded = 0; foreach (var parameter in parameters) { // IsOptional is supported since .NET 1.1 if (!parameter.IsOptional) { minArgsNeeded++; } } #else int minArgsNeeded = parameters.Length; #endif int maxArgsNeeded = parameters.Length; object[] arglist = null; int argsProvided = 0; if (parms != null) { testMethod.parms = parms; testMethod.RunState = parms.RunState; arglist = parms.Arguments; if (arglist != null) { argsProvided = arglist.Length; } if (testMethod.RunState != RunState.Runnable) { return(false); } } #if NETCF Type returnType = testMethod.Method.IsGenericMethodDefinition && (parms == null || parms.Arguments == null) ? typeof(void) : (Type)testMethod.Method.ReturnType; #else Type returnType = testMethod.Method.ReturnType; #endif #if NET_4_0 || NET_4_5 if (AsyncInvocationRegion.IsAsyncOperation(testMethod.Method)) { if (returnType == typeof(void)) { return(MarkAsNotRunnable(testMethod, "Async test method must have non-void return type")); } var returnsGenericTask = returnType.IsGenericType && returnType.GetGenericTypeDefinition() == typeof(System.Threading.Tasks.Task <>); if (returnsGenericTask && (parms == null || !parms.HasExpectedResult)) { return(MarkAsNotRunnable(testMethod, "Async test method must have non-generic Task return type when no result is expected")); } if (!returnsGenericTask && parms != null && parms.HasExpectedResult) { return(MarkAsNotRunnable(testMethod, "Async test method must have Task<T> return type when a result is expected")); } } else #endif if (returnType == typeof(void)) { if (parms != null && parms.HasExpectedResult) { return(MarkAsNotRunnable(testMethod, "Method returning void cannot have an expected result")); } } else if (parms == null || !parms.HasExpectedResult) { return(MarkAsNotRunnable(testMethod, "Method has non-void return value, but no result is expected")); } if (argsProvided > 0 && maxArgsNeeded == 0) { return(MarkAsNotRunnable(testMethod, "Arguments provided for method not taking any")); } if (argsProvided == 0 && minArgsNeeded > 0) { return(MarkAsNotRunnable(testMethod, "No arguments were provided")); } if (argsProvided < minArgsNeeded) { return(MarkAsNotRunnable(testMethod, string.Format("Not enough arguments provided, provide at least {0} arguments.", minArgsNeeded))); } if (argsProvided > maxArgsNeeded) { return(MarkAsNotRunnable(testMethod, string.Format("Too many arguments provided, provide at most {0} arguments.", maxArgsNeeded))); } if (testMethod.Method.IsGenericMethodDefinition && arglist != null) { var typeArguments = GetTypeArgumentsForMethod(testMethod.Method, arglist); foreach (Type o in typeArguments) { if (o == null || o == TypeHelper.NonmatchingType) { return(MarkAsNotRunnable(testMethod, "Unable to determine type arguments for method")); } } testMethod.Method = testMethod.Method.MakeGenericMethod(typeArguments); parameters = testMethod.Method.GetParameters(); } if (arglist != null && parameters != null) { TypeHelper.ConvertArgumentList(arglist, parameters); } return(true); }
/// <summary> /// Helper method that checks the signature of a TestMethod and /// any supplied parameters to determine if the test is valid. /// /// Currently, NUnitTestMethods are required to be public, /// non-abstract methods, either static or instance, /// returning void. They may take arguments but the _values must /// be provided or the TestMethod is not considered runnable. /// /// Methods not meeting these criteria will be marked as /// non-runnable and the method will return false in that case. /// </summary> /// <param name="testMethod">The TestMethod to be checked. If it /// is found to be non-runnable, it will be modified.</param> /// <param name="parms">Parameters to be used for this test, or null</param> /// <returns>True if the method signature is valid, false if not</returns> private static bool CheckTestMethodSignature(TestMethod testMethod, ParameterSet parms) { if (testMethod.Method.IsAbstract) { return(MarkAsNotRunnable(testMethod, "Method is abstract")); } if (!testMethod.Method.IsPublic) { return(MarkAsNotRunnable(testMethod, "Method is not public")); } #if NETCF // TODO: Get this to work if (testMethod.Method.IsGenericMethodDefinition) { return(MarkAsNotRunnable(testMethod, "Generic test methods are not yet supported under .NET CF")); } #endif ParameterInfo[] parameters = testMethod.Method.GetParameters(); int argsNeeded = parameters.Length; object[] arglist = null; int argsProvided = 0; if (parms != null) { testMethod.parms = parms; testMethod.RunState = parms.RunState; arglist = parms.Arguments; if (arglist != null) { argsProvided = arglist.Length; } if (testMethod.RunState != RunState.Runnable) { return(false); } } Type returnType = testMethod.Method.ReturnType; #if NET_4_0 || NET_4_5 if (AsyncInvocationRegion.IsAsyncOperation(testMethod.Method)) { if (returnType == typeof(void)) { return(MarkAsNotRunnable(testMethod, "Async test method must have non-void return type")); } var returnsGenericTask = returnType.IsGenericType && returnType.GetGenericTypeDefinition() == typeof(System.Threading.Tasks.Task <>); if (returnsGenericTask && (parms == null || !parms.HasExpectedResult)) { return(MarkAsNotRunnable(testMethod, "Async test method must have non-generic Task return type when no result is expected")); } if (!returnsGenericTask && parms != null && parms.HasExpectedResult) { return(MarkAsNotRunnable(testMethod, "Async test method must have Task<T> return type when a result is expected")); } } else #endif if (returnType == typeof(void)) { if (parms != null && parms.HasExpectedResult) { return(MarkAsNotRunnable(testMethod, "Method returning void cannot have an expected result")); } } else if (parms == null || !parms.HasExpectedResult) { return(MarkAsNotRunnable(testMethod, "Method has non-void return value, but no result is expected")); } if (argsProvided > 0 && argsNeeded == 0) { return(MarkAsNotRunnable(testMethod, "Arguments provided for method not taking any")); } if (argsProvided == 0 && argsNeeded > 0) { return(MarkAsNotRunnable(testMethod, "No arguments were provided")); } if (argsProvided != argsNeeded) { return(MarkAsNotRunnable(testMethod, "Wrong number of arguments provided")); } #if !NETCF if (testMethod.Method.IsGenericMethodDefinition) { Type[] typeArguments = GetTypeArgumentsForMethod(testMethod.Method, arglist); foreach (object o in typeArguments) { if (o == null) { return(MarkAsNotRunnable(testMethod, "Unable to determine type arguments for method")); } } testMethod.Method = testMethod.Method.MakeGenericMethod(typeArguments); parameters = testMethod.Method.GetParameters(); } #endif if (arglist != null && parameters != null) { TypeHelper.ConvertArgumentList(arglist, parameters); } return(true); }
/// <summary> /// Helper method that checks the signature of a TestMethod and /// any supplied parameters to determine if the test is valid. /// /// Currently, NUnitTestMethods are required to be public, /// non-abstract methods, either static or instance, /// returning void. They may take arguments but the values must /// be provided or the TestMethod is not considered runnable. /// /// Methods not meeting these criteria will be marked as /// non-runnable and the method will return false in that case. /// </summary> /// <param name="testMethod">The TestMethod to be checked. If it /// is found to be non-runnable, it will be modified.</param> /// <param name="parms">Parameters to be used for this test, or null</param> /// <returns>True if the method signature is valid, false if not</returns> /// <remarks> /// The return value is no longer used internally, but is retained /// for testing purposes. /// </remarks> private static bool CheckTestMethodSignature(TestMethod testMethod, TestCaseParameters parms) { if (testMethod.Method.IsAbstract) { return(MarkAsNotRunnable(testMethod, "Method is abstract")); } if (!testMethod.Method.IsPublic) { return(MarkAsNotRunnable(testMethod, "Method is not public")); } ParameterInfo[] parameters; parameters = testMethod.Method.GetParameters(); int minArgsNeeded = 0; foreach (var parameter in parameters) { // IsOptional is supported since .NET 1.1 if (!parameter.IsOptional) { minArgsNeeded++; } } int maxArgsNeeded = parameters.Length; object[] arglist = null; int argsProvided = 0; if (parms != null) { testMethod.parms = parms; testMethod.RunState = parms.RunState; arglist = parms.Arguments; if (arglist != null) { argsProvided = arglist.Length; } if (testMethod.RunState != RunState.Runnable) { return(false); } } Type returnType = testMethod.Method.ReturnType; #if ASYNC if (AsyncInvocationRegion.IsAsyncOperation(testMethod.Method)) { if (returnType == typeof(void)) { return(MarkAsNotRunnable(testMethod, "Async test method must have non-void return type")); } var returnsGenericTask = returnType.GetTypeInfo().IsGenericType&& returnType.GetGenericTypeDefinition() == typeof(System.Threading.Tasks.Task <>); if (returnsGenericTask && (parms == null || !parms.HasExpectedResult)) { return(MarkAsNotRunnable(testMethod, "Async test method must have non-generic Task return type when no result is expected")); } if (!returnsGenericTask && parms != null && parms.HasExpectedResult) { return(MarkAsNotRunnable(testMethod, "Async test method must have Task<T> return type when a result is expected")); } } else #endif if (returnType == typeof(void)) { if (parms != null && parms.HasExpectedResult) { return(MarkAsNotRunnable(testMethod, "Method returning void cannot have an expected result")); } } else if (parms == null || !parms.HasExpectedResult) { return(MarkAsNotRunnable(testMethod, "Method has non-void return value, but no result is expected")); } if (argsProvided > 0 && maxArgsNeeded == 0) { return(MarkAsNotRunnable(testMethod, "Arguments provided for method with no parameters")); } if (argsProvided == 0 && minArgsNeeded > 0) { return(MarkAsNotRunnable(testMethod, "No arguments were provided")); } if (argsProvided < minArgsNeeded) { return(MarkAsNotRunnable(testMethod, string.Format("Not enough arguments provided, provide at least {0} arguments.", minArgsNeeded))); } if (argsProvided > maxArgsNeeded) { return(MarkAsNotRunnable(testMethod, string.Format("Too many arguments provided, provide at most {0} arguments.", maxArgsNeeded))); } if (testMethod.Method.IsGenericMethodDefinition && arglist != null) { Type[] typeArguments; if (!new GenericMethodHelper(testMethod.Method).TryGetTypeArguments(arglist, out typeArguments)) { return(MarkAsNotRunnable(testMethod, "Unable to determine type arguments for method")); } testMethod.Method = testMethod.Method.MakeGenericMethod(typeArguments); parameters = testMethod.Method.GetParameters(); } if (arglist != null && parameters != null) { TypeHelper.ConvertArgumentList(arglist, parameters); } return(true); }