Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix more regex tests for .NET Framework #1485

Merged
merged 1 commit into from
Sep 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,17 @@ namespace System.Text.RegularExpressions.Tests
{
public class GetGroupNamesTests
{
public static IEnumerable<object[]> GetGroupNames_MemberData()
{
yield return new object[] { "(?<first_name>\\S+)\\s(?<last_name>\\S+)", RegexOptions.None, new string[] { "0", "first_name", "last_name" } };
if (PlatformDetection.IsNetCore)
{
yield return new object[] { "(?<first_name>\\S+)\\s(?<last_name>\\S+)", RegexHelpers.RegexOptionNonBacktracking, new string[] { "0" } };
}
}

[Theory]
[InlineData("(?<first_name>\\S+)\\s(?<last_name>\\S+)", RegexOptions.None, new string[] { "0", "first_name", "last_name" })]
[InlineData("(?<first_name>\\S+)\\s(?<last_name>\\S+)", RegexHelpers.RegexOptionNonBacktracking, new string[] { "0" })]
[MemberData(nameof(GetGroupNames_MemberData))]
public void GetGroupNames(string pattern, RegexOptions options, string[] expectedGroupNames)
{
Regex regex = new Regex(pattern, options);
Expand Down Expand Up @@ -115,23 +123,26 @@ public static IEnumerable<object[]> GroupNamesAndNumbers_TestData()
new string[] { "Ryan Byington", "Byington" }
};

yield return new object[]
if (PlatformDetection.IsNetCore)
{
"(?'15'\\S+)\\s(?'15'\\S+)", "Ryan Byington",
new string[] { "0" },
new int[] { 0 },
new string[] { "Ryan Byington" },
RegexHelpers.RegexOptionNonBacktracking
};

yield return new object[]
{
"(?<first_name>\\S+)\\s(?<last_name>\\S+)", "Ryan Byington",
new string[] { "0" },
new int[] { 0 },
new string[] { "Ryan Byington" },
RegexHelpers.RegexOptionNonBacktracking
};
yield return new object[]
{
"(?'15'\\S+)\\s(?'15'\\S+)", "Ryan Byington",
new string[] { "0" },
new int[] { 0 },
new string[] { "Ryan Byington" },
RegexHelpers.RegexOptionNonBacktracking
};

yield return new object[]
{
"(?<first_name>\\S+)\\s(?<last_name>\\S+)", "Ryan Byington",
new string[] { "0" },
new int[] { 0 },
new string[] { "Ryan Byington" },
RegexHelpers.RegexOptionNonBacktracking
};
}
}

[Theory]
Expand Down Expand Up @@ -162,28 +173,46 @@ public void GroupNamesAndNumbers(string pattern, string input, string[] expected
}
}

public static IEnumerable<object[]> GroupNameFromNumber_InvalidIndex_ReturnsEmptyString_MemberData()
{
yield return new object[] { "foo", 1 };
yield return new object[] { "foo", -1 };
yield return new object[] { "(?<first_name>\\S+)\\s(?<last_name>\\S+)", -1 };
yield return new object[] { "(?<first_name>\\S+)\\s(?<last_name>\\S+)", 3 };
yield return new object[] { @"((?<256>abc)\d+)?(?<16>xyz)(.*)", -1 };

if (PlatformDetection.IsNetCore)
{
yield return new object[] { "(f)(oo)", 1, RegexHelpers.RegexOptionNonBacktracking };
yield return new object[] { "(f)(oo)", -1, RegexHelpers.RegexOptionNonBacktracking };
yield return new object[] { "(f)(oo)", 2, RegexHelpers.RegexOptionNonBacktracking };
}
}

[Theory]
[InlineData("(f)(oo)", 1, RegexHelpers.RegexOptionNonBacktracking)]
[InlineData("(f)(oo)", -1, RegexHelpers.RegexOptionNonBacktracking)]
[InlineData("(f)(oo)", 2, RegexHelpers.RegexOptionNonBacktracking)]
[InlineData("foo", 1)]
[InlineData("foo", -1)]
[InlineData("(?<first_name>\\S+)\\s(?<last_name>\\S+)", -1)]
[InlineData("(?<first_name>\\S+)\\s(?<last_name>\\S+)", 3)]
[InlineData(@"((?<256>abc)\d+)?(?<16>xyz)(.*)", -1)]
[MemberData(nameof(GroupNameFromNumber_InvalidIndex_ReturnsEmptyString_MemberData))]
public void GroupNameFromNumber_InvalidIndex_ReturnsEmptyString(string pattern, int index, RegexOptions options = RegexOptions.None)
{
Assert.Same(string.Empty, new Regex(pattern, options).GroupNameFromNumber(index));
}

public static IEnumerable<object[]> GroupNumberFromName_InvalidName_ReturnsMinusOne_MemberData()
{
yield return new object[] { "foo", "no-such-name" };
yield return new object[] { "foo", "1" };
yield return new object[] { "(?<first_name>\\S+)\\s(?<last_name>\\S+)", "no-such-name" };
yield return new object[] { "(?<first_name>\\S+)\\s(?<last_name>\\S+)", "FIRST_NAME" };

if (PlatformDetection.IsNetCore)
{
yield return new object[] { "(f)(oo)", "no-such-name", RegexHelpers.RegexOptionNonBacktracking };
yield return new object[] { "(f)(oo)", "1", RegexHelpers.RegexOptionNonBacktracking };
yield return new object[] { "(f)(oo)", "2", RegexHelpers.RegexOptionNonBacktracking };
}
}

[Theory]
[InlineData("(f)(oo)", "no-such-name", RegexHelpers.RegexOptionNonBacktracking)]
[InlineData("(f)(oo)", "1", RegexHelpers.RegexOptionNonBacktracking)]
[InlineData("(f)(oo)", "2", RegexHelpers.RegexOptionNonBacktracking)]
[InlineData("foo", "no-such-name")]
[InlineData("foo", "1")]
[InlineData("(?<first_name>\\S+)\\s(?<last_name>\\S+)", "no-such-name")]
[InlineData("(?<first_name>\\S+)\\s(?<last_name>\\S+)", "FIRST_NAME")]
[MemberData(nameof(GroupNumberFromName_InvalidName_ReturnsMinusOne_MemberData))]
public void GroupNumberFromName_InvalidName_ReturnsMinusOne(string pattern, string name, RegexOptions options = RegexOptions.None)
{
Assert.Equal(-1, new Regex(pattern, options).GroupNumberFromName(name));
Expand Down
Loading