예제 #1
0
        private CaptureData(string value, int index, int length, bool createCaptures)
        {
            Value  = value;
            Index  = index;
            Length = length;

            // Prevent a StackOverflow recursion in the constructor
            if (createCaptures)
            {
                Captures = new CaptureData[] { new CaptureData(value, index, length, false) };
            }
        }
예제 #2
0
        private static void VerifyMatch(Match match, CaptureData expected)
        {
            Assert.True(match.Success);
            Assert.Equal(expected.Value, match.Value);
            Assert.Equal(expected.Index, match.Index);
            Assert.Equal(expected.Length, match.Length);

            Assert.Equal(expected.Value, match.Groups[0].Value);
            Assert.Equal(expected.Index, match.Groups[0].Index);
            Assert.Equal(expected.Length, match.Groups[0].Length);

            Assert.Equal(1, match.Captures.Count);
            Assert.Equal(expected.Value, match.Captures[0].Value);
            Assert.Equal(expected.Index, match.Captures[0].Index);
            Assert.Equal(expected.Length, match.Captures[0].Length);
        }
예제 #3
0
        public static void VerifyMatch(Match match, CaptureData expected)
        {
            Assert.True(match.Success);
            Assert.Equal(expected.Value, match.Value);
            Assert.Equal(expected.Index, match.Index);
            Assert.Equal(expected.Length, match.Length);

            Assert.Equal(expected.Value, match.Groups[0].Value);
            Assert.Equal(expected.Index, match.Groups[0].Index);
            Assert.Equal(expected.Length, match.Groups[0].Length);

            Assert.Equal(1, match.Captures.Count);
            Assert.Equal(expected.Value, match.Captures[0].Value);
            Assert.Equal(expected.Index, match.Captures[0].Index);
            Assert.Equal(expected.Length, match.Captures[0].Length);
        }
예제 #4
0
        public void Matches(string pattern, string input, RegexOptions options, CaptureData[] expected)
        {
            if (options == RegexOptions.None)
            {
                Regex regexBasic = new Regex(pattern);
                VerifyMatches(regexBasic.Matches(input), expected);
                VerifyMatches(regexBasic.Match(input), expected);

                VerifyMatches(Regex.Matches(input, pattern), expected);
                VerifyMatches(Regex.Match(input, pattern), expected);
            }

            Regex regexAdvanced = new Regex(pattern, options);
            VerifyMatches(regexAdvanced.Matches(input), expected);
            VerifyMatches(regexAdvanced.Match(input), expected);

            VerifyMatches(Regex.Matches(input, pattern, options), expected);
            VerifyMatches(Regex.Match(input, pattern, options), expected);
        }
예제 #5
0
 public static void VerifyMatches(MatchCollection matches, CaptureData[] expected)
 {
     Assert.Equal(expected.Length, matches.Count);
     for (int i = 0; i < matches.Count; i++)
     {
         VerifyMatch(matches[i], expected[i]);
     }
 }
예제 #6
0
 public static void VerifyMatches(Match match, CaptureData[] expected)
 {
     for (int i = 0; match.Success; i++, match = match.NextMatch())
     {
         VerifyMatch(match, expected[i]);
     }
 }
예제 #7
0
        public void Match(string pattern, string input, RegexOptions options, int beginning, int length, CaptureData[] expected)
        {
            bool isDefaultStart = RegexHelpers.IsDefaultStart(input, options, beginning);
            bool isDefaultCount = RegexHelpers.IsDefaultStart(input, options, length);
            if (options == RegexOptions.None)
            {
                if (isDefaultStart  && isDefaultCount)
                {
                    // Use Match(string) or Match(string, string)
                    VerifyMatch(new Regex(pattern).Match(input), true, expected);
                    VerifyMatch(Regex.Match(input, pattern), true, expected);

                    Assert.True(new Regex(pattern).IsMatch(input));
                    Assert.True(Regex.IsMatch(input, pattern));
                }
                if (beginning + length == input.Length)
                {
                    // Use Match(string, int)
                    VerifyMatch(new Regex(pattern).Match(input, beginning), true, expected);

                    Assert.True(new Regex(pattern).IsMatch(input, beginning));
                }
                else
                {
                    // Use Match(string, int, int)
                    VerifyMatch(new Regex(pattern).Match(input, beginning, length), true, expected);
                }
            }
            if (isDefaultStart && isDefaultCount)
            {
                // Use Match(string) or Match(string, string, RegexOptions)
                VerifyMatch(new Regex(pattern, options).Match(input), true, expected);
                VerifyMatch(Regex.Match(input, pattern, options), true, expected);

                Assert.True(Regex.IsMatch(input, pattern, options));
            }
            if (beginning + length == input.Length)
            {
                // Use Match(string, int)
                VerifyMatch(new Regex(pattern, options).Match(input, beginning), true, expected);
            }
            if ((options & RegexOptions.RightToLeft) == 0)
            {
                // Use Match(string, int, int)
                VerifyMatch(new Regex(pattern, options).Match(input, beginning, length), true, expected);
            }
        }
예제 #8
0
        public static void VerifyMatch(Match match, bool expectedSuccess, CaptureData[] expected)
        {
            Assert.Equal(expectedSuccess, match.Success);

            Assert.Equal(expected[0].Value, match.Value);
            Assert.Equal(expected[0].Index, match.Index);
            Assert.Equal(expected[0].Length, match.Length);

            Assert.Equal(1, match.Captures.Count);
            Assert.Equal(expected[0].Value, match.Captures[0].Value);
            Assert.Equal(expected[0].Index, match.Captures[0].Index);
            Assert.Equal(expected[0].Length, match.Captures[0].Length);

            Assert.Equal(expected.Length, match.Groups.Count);
            for (int i = 0; i < match.Groups.Count; i++)
            {
                Assert.Equal(expectedSuccess, match.Groups[i].Success);

                Assert.Equal(expected[i].Value, match.Groups[i].Value);
                Assert.Equal(expected[i].Index, match.Groups[i].Index);
                Assert.Equal(expected[i].Length, match.Groups[i].Length);

                Assert.Equal(expected[i].Captures.Length, match.Groups[i].Captures.Count);
                for (int j = 0; j < match.Groups[i].Captures.Count; j++)
                {
                    Assert.Equal(expected[i].Captures[j].Value, match.Groups[i].Captures[j].Value);
                    Assert.Equal(expected[i].Captures[j].Index, match.Groups[i].Captures[j].Index);
                    Assert.Equal(expected[i].Captures[j].Length, match.Groups[i].Captures[j].Length);
                }
            }
        }