/// <summary> /// Submits a failure if the assertion failure object is non-null. /// </summary> /// <param name="failure">Failure to be submited, or null if none.</param> public static void Fail(AssertionFailure failure) { if (failure != null) { AssertionContext context = AssertionContext.CurrentContext; if (context != null) { context.SubmitFailure(failure); } else { throw new AssertionFailureException(failure, false); } } }
/// <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; }