public override ConstraintResult ApplyTo <TActual>(TActual actual) { var @delegate = ConstraintUtils.RequireActual <Delegate>(actual, nameof(actual)); var invokeMethod = @delegate.GetType().GetTypeInfo().GetMethod("Invoke"); if (invokeMethod.GetParameters().Length != 0) { throw new ArgumentException("Delegate must be parameterless.", nameof(actual)); } var stopwatch = new System.Diagnostics.Stopwatch(); if (AsyncToSyncAdapter.IsAsyncOperation(@delegate)) { stopwatch.Start(); AsyncToSyncAdapter.Await(() => @delegate.DynamicInvoke()); stopwatch.Stop(); } else { stopwatch.Start(); @delegate.DynamicInvoke(); stopwatch.Stop(); } return(new Result(this, stopwatch.ElapsedMilliseconds)); }
internal static Exception Intercept(object invocation) { var invocationDescriptor = GetInvocationDescriptor(invocation); #if ASYNC if (AsyncToSyncAdapter.IsAsyncOperation(invocationDescriptor.Delegate)) { try { AsyncToSyncAdapter.Await(invocationDescriptor.Invoke); return(null); } catch (Exception ex) { return(ex); } } #endif { using (new TestExecutionContext.IsolatedContext()) { try { invocationDescriptor.Invoke(); return(null); } catch (Exception ex) { return(ex); } } } }
private static object InvokeDelegate <T>(ActualValueDelegate <T> del) { if (AsyncToSyncAdapter.IsAsyncOperation(del)) { return(AsyncToSyncAdapter.Await(() => del.Invoke())); } 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 (AsyncToSyncAdapter.IsAsyncOperation(del)) { return(ApplyTo(AsyncToSyncAdapter.Await(() => del.Invoke()))); } return(ApplyTo(GetTestObject(del))); }
private object RunTestMethod(TestExecutionContext context) { if (AsyncToSyncAdapter.IsAsyncOperation(testMethod.Method.MethodInfo)) { return(AsyncToSyncAdapter.Await(() => InvokeTestMethod(context))); } return(InvokeTestMethod(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> /// <remarks> /// The value has already been validated when this method is called. /// </remarks> protected virtual ConstraintResult ApplyConstraint <T>(ActualValueDelegate <T> del) { #if NYI // async if (AsyncToSyncAdapter.IsAsyncOperation(del)) { return(ApplyConstraint(AsyncToSyncAdapter.Await(() => del.Invoke()))); } #endif return(ApplyConstraint(del())); }
private static void RunSetUpOrTearDownMethod(TestExecutionContext context, MethodInfo method) { Guard.ArgumentNotAsyncVoid(method, nameof(method)); #if ASYNC if (AsyncToSyncAdapter.IsAsyncOperation(method)) { AsyncToSyncAdapter.Await(() => InvokeMethod(method, context)); } else #endif InvokeMethod(method, context); }
private void RunSetUpOrTearDownMethod(TestExecutionContext context, IMethodInfo method) { Guard.ArgumentNotAsyncVoid(method.MethodInfo, nameof(method)); _methodValidator?.Validate(method.MethodInfo); if (AsyncToSyncAdapter.IsAsyncOperation(method.MethodInfo)) { AsyncToSyncAdapter.Await(() => InvokeMethod(method, context)); } else { InvokeMethod(method, context); } }
public static Exception?ThrowsAsync(IResolveConstraint expression, AsyncTestDelegate code, string?message, params object?[]?args) { Exception?caughtException = null; try { AsyncToSyncAdapter.Await(code.Invoke); } catch (Exception e) { caughtException = e; } Assert.That(caughtException, expression, message, args); return(caughtException); }
private object RunTestMethod(TestExecutionContext context) { #if ASYNC if (AsyncToSyncAdapter.IsAsyncOperation(testMethod.Method.MethodInfo)) { try { return(AsyncToSyncAdapter.Await(() => InvokeTestMethod(context))); } catch (Exception e) { throw new NUnitException("Rethrown", e); } } #endif return(InvokeTestMethod(context)); }
/// <summary> /// Executes the code and returns success if an exception is thrown. /// </summary> /// <param name="actual">A delegate representing the code to be tested</param> /// <returns>True if an exception is thrown, otherwise false</returns> public override ConstraintResult ApplyTo <TActual>(TActual actual) { TestDelegate code = actual as TestDelegate; Exception caughtException = null; if (code != null) { try { code(); } catch (Exception ex) { caughtException = ex; } } #if ASYNC AsyncTestDelegate asyncCode = actual as AsyncTestDelegate; if (asyncCode != null) { try { AsyncToSyncAdapter.Await(asyncCode.Invoke); } catch (Exception ex) { caughtException = ex; } } if (code == null && asyncCode == null) #else else #endif { throw new ArgumentException(string.Format("The actual value must be a TestDelegate or AsyncTestDelegate but was {0}", actual.GetType().Name), nameof(actual)); } return(new ThrowsExceptionConstraintResult(this, caughtException)); }
public static void Multiple(AsyncTestDelegate testDelegate) { TestExecutionContext context = TestExecutionContext.CurrentContext; Guard.OperationValid(context != null, "There is no current test execution context."); context.MultipleAssertLevel++; try { AsyncToSyncAdapter.Await(testDelegate.Invoke); } finally { context.MultipleAssertLevel--; } if (context.MultipleAssertLevel == 0 && context.CurrentResult.PendingFailures > 0) { context.CurrentResult.RecordTestCompletion(); throw new MultipleAssertException(context.CurrentResult); } }