Esempio n. 1
0
            public AssertionFailure[] CaptureFailures(GallioAction action, bool captureExceptionAsAssertionFailure)
            {
                try
                {
                    action();
                }
                catch (ThreadAbortException)
                {
                    throw;
                }
                catch (AssertionFailureException ex)
                {
                    if (!ex.IsSilent)
                    {
                        SubmitFailure(ex.Failure, true);
                    }
                }
                catch (TestException)
                {
                    throw;
                }
                catch (Exception ex)
                {
                    if (!captureExceptionAsAssertionFailure)
                    {
                        throw;
                    }

                    SubmitFailure(AssertionFailureBuilder.WrapExceptionAsAssertionFailure(ex), true);
                }

                return(GetSavedFailuresAsArray());
            }
Esempio n. 2
0
        /// <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);
            }
        }