/// <summary>
        /// Checks if action throws either a RegexParseException or an ArgumentException depending on the
        /// environment and the supplied error.
        /// </summary>
        /// <param name="error">The expected parse error</param>
        /// <param name="action">The action to invoke.</param>
        static partial void Throws(RegexParseError error, int offset, Action action)
        {
            try
            {
                action();
            }
            catch (RegexParseException e)
            {
                RegexParseError regexParseError = e.Error;

                // Success if provided error matches and offset is correct.
                if (error == regexParseError)
                {
                    Assert.Equal(offset, e.Offset);
                    return;
                }

                throw new XunitException($"Expected RegexParseException with error: ({error}) -> Actual error: {regexParseError})");
            }
            catch (Exception e)
            {
                throw new XunitException($"Expected RegexParseException -> Actual: ({e})");
            }

            throw new XunitException($"Expected RegexParseException with error: ({error}) -> Actual: No exception thrown");
        }
        /// <summary>
        /// Checks if action throws either a RegexParseException or an ArgumentException depending on the
        /// environment and the supplied error.
        /// </summary>
        /// <param name="error">The expected parse error</param>
        /// <param name="action">The action to invoke.</param>
        static partial void Throws(string pattern, RegexOptions options, RegexParseError error, int offset, Action action)
        {
            try
            {
                action();
            }
            catch (RegexParseException e)
            {
                RegexParseError regexParseError = e.Error;

                // Success if provided error matches and offset is correct.
                if (error == regexParseError)
                {
                    Assert.Equal(offset, e.Offset);
                    LogActual(pattern, options, regexParseError, e.Offset);
                    return;
                }

                LogActual(pattern, options, regexParseError, e.Offset);
                throw new XunitException($"Expected RegexParseException with error {error} offset {offset} -> Actual error: {regexParseError} offset {e.Offset})");
            }
            catch (Exception e)
            {
                throw new XunitException($"Expected RegexParseException for pattern '{pattern}' -> Actual: ({e})");
            }

            LogActual(pattern, options, RegexParseError.Unknown, -1);
            throw new XunitException($"Expected RegexParseException with error: ({error}) -> Actual: No exception thrown");
        }
Esempio n. 3
0
        internal static string GetDescription(this RegexParseError error)
        {
            var descriptionAttribute = typeof(RegexParseError)
                                       .GetField(error.ToString())
                                       .GetCustomAttributes(typeof(DescriptionAttribute), false)
                                       .FirstOrDefault() as DescriptionAttribute;

            return(descriptionAttribute?.Description);
        }
        /// <summary>
        /// Checks that action throws either a RegexParseException or an ArgumentException depending on the
        /// environment and the supplied error.
        /// </summary>
        /// <param name="error">The expected parse error</param>
        /// <param name="action">The action to invoke.</param>
        static partial void Throws(RegexParseError error, int offset, Action action)
        {
            try
            {
                action();
            }
            catch (ArgumentException)
            {
                // On NetFramework, all we care about is whether the exception is thrown.
                return;
            }
            catch (Exception e)
            {
                throw new XunitException($"Expected ArgumentException -> Actual: {e}");
            }

            throw new XunitException($"Expected ArgumentException with error: ({error}) -> Actual: No exception thrown");
        }
 public RegexParseException(RegexParseError error, int offset, string message)
     : base(message)
 {
     Error  = error;
     Offset = offset;
 }
Esempio n. 6
0
        /// <summary>
        /// Construct a <see cref="RegexParseException"/> that creates a default message based on the given <see cref="RegexParseError"/> value.
        /// </summary>
        /// <param name="pattern">The pattern of the regular expression.</param>
        /// <param name="error">The <see cref="RegexParseError"/> value detailing the type of parse error.</param>
        /// <param name="offset">The zero-based offset in the regular expression where the parse error occurs.</param>
        private static string MakeMessage(string pattern, RegexParseError error, int offset)
        {
            string message;

            switch (error)
            {
            case RegexParseError.Unknown:
                message = SR.Format(SR.MakeException, pattern, offset, SR.Generic);
                break;

            case RegexParseError.AlternationHasTooManyConditions:
                message = SR.Format(SR.MakeException, pattern, offset, SR.AlternationHasTooManyConditions);
                break;

            case RegexParseError.AlternationHasMalformedCondition:
                message = SR.Format(SR.MakeException, pattern, offset, SR.AlternationHasMalformedCondition);
                break;

            case RegexParseError.InvalidUnicodePropertyEscape:
                message = SR.Format(SR.MakeException, pattern, offset, SR.InvalidUnicodePropertyEscape);
                break;

            case RegexParseError.MalformedUnicodePropertyEscape:
                message = SR.Format(SR.MakeException, pattern, offset, SR.MalformedUnicodePropertyEscape);
                break;

            case RegexParseError.UnrecognizedEscape:
                message = SR.Format(SR.MakeException, pattern, offset, SR.UnrecognizedEscape);
                break;

            case RegexParseError.UnrecognizedControlCharacter:
                message = SR.Format(SR.MakeException, pattern, offset, SR.UnrecognizedControlCharacter);
                break;

            case RegexParseError.MissingControlCharacter:
                message = SR.Format(SR.MakeException, pattern, offset, SR.MissingControlCharacter);
                break;

            case RegexParseError.InsufficientOrInvalidHexDigits:
                message = SR.Format(SR.MakeException, pattern, offset, SR.InsufficientOrInvalidHexDigits);
                break;

            case RegexParseError.QuantifierOrCaptureGroupOutOfRange:
                message = SR.Format(SR.MakeException, pattern, offset, SR.QuantifierOrCaptureGroupOutOfRange);
                break;

            case RegexParseError.UndefinedNamedReference:
                message = SR.Format(SR.MakeException, pattern, offset, SR.UndefinedNamedReferenceNoPlaceholder);
                break;

            case RegexParseError.UndefinedNumberedReference:
                message = SR.Format(SR.MakeException, pattern, offset, SR.UndefinedNumberedReferenceNoPlaceholder);
                break;

            case RegexParseError.MalformedNamedReference:
                message = SR.Format(SR.MakeException, pattern, offset, SR.MalformedNamedReference);
                break;

            case RegexParseError.UnescapedEndingBackslash:
                message = SR.Format(SR.MakeException, pattern, offset, SR.UnescapedEndingBackslash);
                break;

            case RegexParseError.UnterminatedComment:
                message = SR.Format(SR.MakeException, pattern, offset, SR.UnterminatedComment);
                break;

            case RegexParseError.InvalidGroupingConstruct:
                message = SR.Format(SR.MakeException, pattern, offset, SR.InvalidGroupingConstruct);
                break;

            case RegexParseError.AlternationHasNamedCapture:
                message = SR.Format(SR.MakeException, pattern, offset, SR.AlternationHasNamedCapture);
                break;

            case RegexParseError.AlternationHasComment:
                message = SR.Format(SR.MakeException, pattern, offset, SR.AlternationHasComment);
                break;

            case RegexParseError.AlternationHasMalformedReference:
                message = SR.Format(SR.MakeException, pattern, offset, SR.AlternationHasMalformedReferenceNoPlaceholder);
                break;

            case RegexParseError.AlternationHasUndefinedReference:
                message = SR.Format(SR.MakeException, pattern, offset, SR.AlternationHasUndefinedReferenceNoPlaceholder);
                break;

            case RegexParseError.CaptureGroupNameInvalid:
                message = SR.Format(SR.MakeException, pattern, offset, SR.CaptureGroupNameInvalid);
                break;

            case RegexParseError.CaptureGroupOfZero:
                message = SR.Format(SR.MakeException, pattern, offset, SR.CaptureGroupOfZero);
                break;

            case RegexParseError.UnterminatedBracket:
                message = SR.Format(SR.MakeException, pattern, offset, SR.UnterminatedBracket);
                break;

            case RegexParseError.ExclusionGroupNotLast:
                message = SR.Format(SR.MakeException, pattern, offset, SR.ExclusionGroupNotLast);
                break;

            case RegexParseError.ReversedCharacterRange:
                message = SR.Format(SR.MakeException, pattern, offset, SR.ReversedCharacterRange);
                break;

            case RegexParseError.ShorthandClassInCharacterRange:
                message = SR.Format(SR.MakeException, pattern, offset, SR.ShorthandClassInCharacterRangeNoPlaceholder);
                break;

            case RegexParseError.InsufficientClosingParentheses:
                message = SR.Format(SR.MakeException, pattern, offset, SR.InsufficientClosingParentheses);
                break;

            case RegexParseError.ReversedQuantifierRange:
                message = SR.Format(SR.MakeException, pattern, offset, SR.ReversedQuantifierRange);
                break;

            case RegexParseError.NestedQuantifiersNotParenthesized:
                message = SR.Format(SR.MakeException, pattern, offset, SR.NestedQuantifiersNotParenthesizedNoPlaceholder);
                break;

            case RegexParseError.QuantifierAfterNothing:
                message = SR.Format(SR.MakeException, pattern, offset, SR.QuantifierAfterNothing);
                break;

            case RegexParseError.InsufficientOpeningParentheses:
                message = SR.Format(SR.MakeException, pattern, offset, SR.InsufficientOpeningParentheses);
                break;

            case RegexParseError.UnrecognizedUnicodeProperty:
                message = SR.Format(SR.MakeException, pattern, offset, SR.UnrecognizedUnicodePropertyNoPlaceholder);
                break;

            default:
                message = SR.Format(SR.MakeException, pattern, offset, SR.Generic);
                break;
            }

            return(message);
        }
Esempio n. 7
0
 internal RegexParseException(string pattern, RegexParseError error, int offset) : base(MakeMessage(pattern, error, offset))
 {
     Error  = error;
     Offset = offset;
 }