public AssertionFailure[] CaptureFailures(GallioAction action, AssertionFailureBehavior assertionFailureBehavior, bool captureExceptionAsAssertionFailure) { if (action == null) { throw new ArgumentNullException("action"); } Scope newScope = new Scope(this, assertionFailureBehavior); Scope oldScope = null; try { oldScope = Interlocked.Exchange(ref scope, newScope); return(newScope.CaptureFailures(action, captureExceptionAsAssertionFailure)); } finally { if (oldScope != null && Interlocked.CompareExchange(ref scope, oldScope, newScope) != newScope) { throw new NotSupportedException("The current implementation does not support capturing failures concurrently."); } } }
/// <summary> /// Performs an action and combines the possible assertion failures /// that were observed within the block, into a single outer failure with /// a common explanation. /// </summary> /// <param name="action">The action to invoke.</param> /// <param name="assertionFailureBehavior">The assertion failure behavior to use while the action runs.</param> /// <param name="explanation">A function that takes an array of inner failures and /// returns a single outer failure with a common explanation.</param> /// <exception cref="ArgumentNullException">Thrown if <paramref name="action"/> is null.</exception> /// <exception cref="ArgumentNullException">Thrown if <paramref name="explanation"/> is null.</exception> public static void Explain(Action action, AssertionFailureBehavior assertionFailureBehavior, AssertionFailureExplanation explanation) { if (action == null) { throw new ArgumentNullException("action"); } if (explanation == null) { throw new ArgumentNullException("explanation"); } Verify(() => { AssertionFailure[] failures = Eval(action, assertionFailureBehavior); return(failures.Length == 0 ? null : explanation(failures)); }); }
/// <summary> /// Performs an action and returns an array containing the assertion failures /// that were observed within the block. /// </summary> /// <remarks> /// <para> /// If the action throws an exception it is reported as an assertion failure /// unless the exception is a <see cref="TestException"/> (except <see cref="AssertionFailureException"/>) /// in which case the exception is rethrown by this method. This behavior enables special /// test exceptions such as <see cref="TestTerminatedException" /> to be used to terminate /// the test at any point instead of being reported as assertion failures. /// </para> /// </remarks> /// <param name="action">The action to invoke.</param> /// <param name="assertionFailureBehavior">The assertion failure behavior to use while the action runs.</param> /// <returns>The array of failures, may be empty if none.</returns> /// <exception cref="ArgumentNullException">Thrown if <paramref name="action"/> is null.</exception> /// <exception cref="InvalidOperationException">Thrown if there is no current assertion context.</exception> public static AssertionFailure[] Eval(Action action, AssertionFailureBehavior assertionFailureBehavior) { if (action == null) { throw new ArgumentNullException("action"); } AssertionContext context = AssertionContext.CurrentContext; if (context != null) { return(context.CaptureFailures(action, assertionFailureBehavior, true)); } else { try { action(); } catch (ThreadAbortException) { throw; } catch (AssertionFailureException ex) { return(new[] { ex.Failure }); } catch (TestException) { throw; } catch (Exception ex) { return(new[] { AssertionFailureBuilder.WrapExceptionAsAssertionFailure(ex) }); } return(EmptyArray <AssertionFailure> .Instance); } }
public Scope(AssertionContext context, AssertionFailureBehavior assertionFailureBehavior) { this.context = context; this.assertionFailureBehavior = assertionFailureBehavior; }
public Scope(AssertionContext context, AssertionFailureBehavior assertionFailureBehavior) { this.context = context; this.assertionFailureBehavior = assertionFailureBehavior; }
public AssertionFailure[] CaptureFailures(Action action, AssertionFailureBehavior assertionFailureBehavior, bool captureExceptionAsAssertionFailure) { if (action == null) throw new ArgumentNullException("action"); Scope newScope = new Scope(this, assertionFailureBehavior); Scope oldScope = null; try { oldScope = Interlocked.Exchange(ref scope, newScope); return newScope.CaptureFailures(action, captureExceptionAsAssertionFailure); } finally { if (oldScope != null && Interlocked.CompareExchange(ref scope, oldScope, newScope) != newScope) throw new NotSupportedException("The current implementation does not support capturing failures concurrently."); } }
/// <summary> /// Performs an action and returns an array containing the assertion failures /// that were observed within the block. /// </summary> /// <remarks> /// <para> /// If the action throws an exception it is reported as an assertion failure /// unless the exception is a <see cref="TestException"/> (except <see cref="AssertionFailureException"/>) /// in which case the exception is rethrown by this method. This behavior enables special /// test exceptions such as <see cref="TestTerminatedException" /> to be used to terminate /// the test at any point instead of being reported as assertion failures. /// </para> /// </remarks> /// <param name="action">The action to invoke.</param> /// <param name="assertionFailureBehavior">The assertion failure behavior to use while the action runs.</param> /// <returns>The array of failures, may be empty if none.</returns> /// <exception cref="ArgumentNullException">Thrown if <paramref name="action"/> is null.</exception> /// <exception cref="InvalidOperationException">Thrown if there is no current assertion context.</exception> public static AssertionFailure[] Eval(Action action, AssertionFailureBehavior assertionFailureBehavior) { if (action == null) throw new ArgumentNullException("action"); AssertionContext context = AssertionContext.CurrentContext; if (context != null) { return context.CaptureFailures(action, assertionFailureBehavior, true); } else { try { action(); } catch (ThreadAbortException) { throw; } catch (AssertionFailureException ex) { return new[] { ex.Failure }; } catch (TestException) { throw; } catch (Exception ex) { return new[] { AssertionFailureBuilder.WrapExceptionAsAssertionFailure(ex) }; } return EmptyArray<AssertionFailure>.Instance; } }
/// <summary> /// Performs an action and combines the possible assertion failures /// that were observed within the block, into a single outer failure with /// a common explanation. /// </summary> /// <param name="action">The action to invoke.</param> /// <param name="assertionFailureBehavior">The assertion failure behavior to use while the action runs.</param> /// <param name="explanation">A function that takes an array of inner failures and /// returns a single outer failure with a common explanation.</param> /// <exception cref="ArgumentNullException">Thrown if <paramref name="action"/> is null.</exception> /// <exception cref="ArgumentNullException">Thrown if <paramref name="explanation"/> is null.</exception> public static void Explain(Action action, AssertionFailureBehavior assertionFailureBehavior, AssertionFailureExplanation explanation) { if (action == null) throw new ArgumentNullException("action"); if (explanation == null) throw new ArgumentNullException("explanation"); Verify(() => { AssertionFailure[] failures = Eval(action, assertionFailureBehavior); return failures.Length == 0 ? null : explanation(failures); }); }