Ejemplo n.º 1
0
        protected override MatchContext Match(Ust ust, MatchContext context)
        {
            var tryCatchStatement = ust as TryCatchStatement;

            if (tryCatchStatement == null)
            {
                return(context.Fail());
            }

            MatchContext newContext;

            if (tryCatchStatement.CatchClauses == null)
            {
                newContext = context.Fail();
            }
            else
            {
                bool result = tryCatchStatement.CatchClauses.Any(catchClause =>
                {
                    if (IsCatchBodyEmpty && catchClause.Body.Statements.Any())
                    {
                        return(false);
                    }

                    return(!ExceptionTypes.Any() ||
                           ExceptionTypes.Any(type => type.MatchUst(catchClause.Type, context).Success));
                });

                newContext = context.Set(result);
            }

            return(newContext.AddUstIfSuccess(tryCatchStatement));
        }
        /// <summary>
        /// Verifies that the type of the exception thrown by the unit test is expected
        /// </summary>
        /// <param name="exception">The exception thrown by the unit test</param>
        protected override void Verify(Exception exception)
        {
            var seleniumException = exception as SeleniumTestFailedException;

            if (seleniumException != null)
            {
                if (AllowDerivedTypes)
                {
                    if (!seleniumException.InnerExceptions.All(s => ExceptionTypes.Any(n => n.IsInstanceOfType(s))))
                    {
                        RethrowIfAssertException(exception);
                        var exp = seleniumException.InnerExceptions
                                  .First(s => ExceptionTypes.Any(n => n.IsInstanceOfType(s)));
                        throw new Exception(string.Format((IFormatProvider)CultureInfo.CurrentCulture, $"Test method threw exception {exp.GetType()}, but exception {string.Join(", ", ExceptionTypes.Select(s => s.FullName))} was expected. Exception message: {exp.Message}"));
                    }
                }

                if (!seleniumException.InnerExceptions.All(s => ExceptionTypes.Any(n => s.GetType() == n)))
                {
                    RethrowIfAssertException(exception);
                    var exp = seleniumException.InnerExceptions.First(s => ExceptionTypes.Any(n => s.GetType() != n));
                    throw new Exception(string.Format((IFormatProvider)CultureInfo.CurrentCulture, $"Test method threw exception {exp.GetType()}, but exception {string.Join(", ", ExceptionTypes.Select(s => s.FullName))} was expected. Exception message: {exp.Message}"));
                }
            }
            else if (seleniumException == null && exception != null)
            {
                RethrowIfAssertException(exception);
                throw new Exception($"Test method threw exception {exception.GetType()}, but exception {string.Join(", ", ExceptionTypes.Select(s => s.FullName))} was expected. Exception message: {exception.Message}", exception);
            }
            else
            {
                RethrowIfAssertException(exception);
                throw new Exception(string.Format((IFormatProvider)CultureInfo.CurrentCulture, NoExceptionMessage ?? "Test method did not throw exception."));
            }
        }