|
3 | 3 | // See the LICENSE file in the project root for more information. |
4 | 4 |
|
5 | 5 | using System.Collections.Generic; |
| 6 | +using System.Diagnostics; |
6 | 7 | using System.Globalization; |
| 8 | +using System.Linq; |
7 | 9 | using Xunit; |
8 | 10 |
|
9 | 11 | namespace System.Text.RegularExpressions.Tests |
10 | 12 | { |
11 | | - public class RegexGroupTests |
| 13 | + public class RegexGroupTests : RemoteExecutorTestBase |
12 | 14 | { |
13 | 15 | private static readonly CultureInfo s_enUSCulture = new CultureInfo("en-US"); |
14 | 16 | private static readonly CultureInfo s_invariantCulture = new CultureInfo(""); |
@@ -622,43 +624,53 @@ public static IEnumerable<object[]> Groups_CustomCulture_TestData() |
622 | 624 | [MemberData(nameof(Groups_CustomCulture_TestData))] |
623 | 625 | public void Groups(string pattern, string input, RegexOptions options, CultureInfo cultureInfo, string[] expectedGroups) |
624 | 626 | { |
625 | | - CultureInfo originalCulture = CultureInfo.CurrentCulture; |
626 | | - try |
| 627 | + const string EmptyPlaceholder = "-"; |
| 628 | + const char Seperator = ';'; |
| 629 | + |
| 630 | + string outerPattern = Convert.ToBase64String(Encoding.UTF8.GetBytes(pattern)); |
| 631 | + string outerInput = Convert.ToBase64String(Encoding.UTF8.GetBytes(input)); |
| 632 | + string outerOptions = ((int)options).ToString(); |
| 633 | + string outerCultureInfo = cultureInfo != null ? cultureInfo.ToString() : EmptyPlaceholder; |
| 634 | + string outerExpectedGroups = expectedGroups != null && expectedGroups.Length > 0 ? "\"" + Convert.ToBase64String(Encoding.UTF8.GetBytes(string.Join(Seperator.ToString(), expectedGroups.Select(s => s == string.Empty ? EmptyPlaceholder : s).ToArray()))) + "\"" : EmptyPlaceholder; |
| 635 | + |
| 636 | + RemoteInvoke((innerPatternEnc, innerInputEnc, innerOptionsEnc, innerCultureInfoEnc, innerExpectedGroupsEnc) => |
627 | 637 | { |
| 638 | + string innerPattern = Encoding.UTF8.GetString(Convert.FromBase64String(innerPatternEnc)); |
| 639 | + string innerInput = Encoding.UTF8.GetString(Convert.FromBase64String(innerInputEnc)); |
| 640 | + RegexOptions innerOptions = (RegexOptions)int.Parse(innerOptionsEnc); |
| 641 | + CultureInfo innerCultureInfo = innerCultureInfoEnc != EmptyPlaceholder ? new CultureInfo(innerCultureInfoEnc) : null; |
| 642 | + string[] innerExpectedGroups = innerExpectedGroupsEnc != EmptyPlaceholder ? Encoding.UTF8.GetString(Convert.FromBase64String(innerExpectedGroupsEnc.Trim('"'))).Split(Seperator).Select(s => s == EmptyPlaceholder ? string.Empty : s).ToArray() : new string[] { }; |
| 643 | + |
628 | 644 | // In invariant culture, the unicode char matches differ from expected values provided. |
629 | | - if (originalCulture.Equals(CultureInfo.InvariantCulture)) |
| 645 | + if (CultureInfo.CurrentCulture.Equals(CultureInfo.InvariantCulture)) |
630 | 646 | { |
631 | | - CultureInfo.CurrentCulture = s_enUSCulture; |
| 647 | + CultureInfo.CurrentCulture = new CultureInfo("en-US"); |
632 | 648 | } |
633 | | - if (cultureInfo != null) |
| 649 | + if (innerCultureInfo != null) |
634 | 650 | { |
635 | | - CultureInfo.CurrentCulture = cultureInfo; |
| 651 | + CultureInfo.CurrentCulture = innerCultureInfo; |
636 | 652 | } |
637 | | - Regex regex = new Regex(pattern, options); |
638 | | - Match match = regex.Match(input); |
| 653 | + |
| 654 | + Regex regex = new Regex(innerPattern, innerOptions); |
| 655 | + Match match = regex.Match(innerInput); |
639 | 656 | Assert.True(match.Success); |
640 | 657 |
|
641 | | - Assert.Equal(expectedGroups.Length, match.Groups.Count); |
642 | | - Assert.True(expectedGroups[0] == match.Value, string.Format("Culture used: {0}", CultureInfo.CurrentCulture)); |
| 658 | + Assert.Equal(innerExpectedGroups.Length, match.Groups.Count); |
| 659 | + Assert.True(innerExpectedGroups[0] == match.Value, string.Format("Culture used: {0}", CultureInfo.CurrentCulture)); |
643 | 660 |
|
644 | 661 | int[] groupNumbers = regex.GetGroupNumbers(); |
645 | 662 | string[] groupNames = regex.GetGroupNames(); |
646 | | - for (int i = 0; i < expectedGroups.Length; i++) |
| 663 | + for (int i = 0; i < innerExpectedGroups.Length; i++) |
647 | 664 | { |
648 | | - Assert.Equal(expectedGroups[i], match.Groups[groupNumbers[i]].Value); |
| 665 | + Assert.Equal(innerExpectedGroups[i], match.Groups[groupNumbers[i]].Value); |
649 | 666 | Assert.Equal(match.Groups[groupNumbers[i]], match.Groups[groupNames[i]]); |
650 | 667 |
|
651 | 668 | Assert.Equal(groupNumbers[i], regex.GroupNumberFromName(groupNames[i])); |
652 | 669 | Assert.Equal(groupNames[i], regex.GroupNameFromNumber(groupNumbers[i])); |
653 | 670 | } |
654 | | - } |
655 | | - finally |
656 | | - { |
657 | | - if (cultureInfo != null || originalCulture.Equals(CultureInfo.InvariantCulture)) |
658 | | - { |
659 | | - CultureInfo.CurrentCulture = originalCulture; |
660 | | - } |
661 | | - } |
| 671 | + |
| 672 | + return SuccessExitCode; |
| 673 | + }, outerPattern, outerInput, outerOptions, outerCultureInfo, outerExpectedGroups).Dispose(); |
662 | 674 | } |
663 | 675 | } |
664 | 676 | } |
0 commit comments