public static void ICollectionOfT_CopyTo() { string[] expected = new[] { "This ", "is ", "a ", "sentence" }; ICollection<Capture> collection = CreateCollection(); Capture[] array = new Capture[collection.Count]; collection.CopyTo(array, 0); Assert.Equal(expected, array.Select(c => c.ToString())); }
public static void ICollection_CopyTo(int index) { Regex regex = new Regex(@"(?<A1>a*)(?<A2>b*)(?<A3>c*)"); CaptureCollection captures = regex.Match("aaabbccccccccccaaaabc").Captures; ICollection collection = captures; Capture[] copy = new Capture[collection.Count + index]; collection.CopyTo(copy, index); for (int i = 0; i < index; i++) { Assert.Null(copy[i]); } for (int i = index; i < copy.Length; i++) { Assert.Same(captures[i - index], copy[i]); } }
public static void VerifyMatch(Match match, bool expectedSuccess, Capture[] 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); } } }
public void Match(string pattern, string input, RegexOptions options, int beginning, int length, Capture[] 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); } }
public static void VerifyMatch(Match match, Capture 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); }
public static void VerifyMatches(MatchCollection matches, Capture[] expected) { Assert.Equal(expected.Length, matches.Count); for (int i = 0; i < matches.Count; i++) { VerifyMatch(matches[i], expected[i]); } }
public static void VerifyMatches(Match match, Capture[] expected) { for (int i = 0; match.Success; i++, match = match.NextMatch()) { VerifyMatch(match, expected[i]); } }
public void Matches(string pattern, string input, RegexOptions options, Capture[] 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); }
public static void Equal(string expected, Capture actual) { Assert.Equal(expected, actual.Value); }