-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
Add more known Regex patterns to increase test coverage. #1617
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -136,5 +136,88 @@ string DomainMapper(Match match) | |
} | ||
} | ||
} | ||
|
||
// These patterns come from real-world customer usages | ||
|
||
[Theory] | ||
[InlineData("https://foo.com:443/bar/17/groups/0ad1/providers/Network/public/4e-ip?version=16", "Network/public/4e-ip")] | ||
[InlineData("ftp://443/notproviders/17/groups/0ad1/providers/Network/public/4e-ip?version=16", "Network/public/4e-ip")] | ||
[InlineData("ftp://443/providersnot/17/groups/0ad1/providers/Network/public/4e-ip?version=16", "Network/public/4e-ip")] | ||
public void ExtractResourceUri(string url, string expected) | ||
{ | ||
foreach (RegexOptions options in new[] { RegexOptions.Compiled, RegexOptions.None }) | ||
{ | ||
Regex r = new Regex(@"/providers/(.+?)\?", options); | ||
Match m = r.Match(url); | ||
Assert.True(m.Success); | ||
Assert.Equal(2, m.Groups.Count); | ||
Assert.Equal(expected, m.Groups[1].Value); | ||
} | ||
} | ||
|
||
[Theory] | ||
[InlineData("IsValidCSharpName", true)] | ||
[InlineData("_IsValidCSharpName", true)] | ||
[InlineData("__", true)] | ||
[InlineData("a\u2169", true)] // \u2169 is in {Nl} | ||
[InlineData("\u2169b", true)] // \u2169 is in {Nl} | ||
[InlineData("a\u0600", true)] // \u0600 is in {Cf} | ||
[InlineData("\u0600b", false)] // \u0600 is in {Cf} | ||
[InlineData("a\u0300", true)] // \u0300 is in {Mn} | ||
[InlineData("\u0300b", false)] // \u0300 is in {Mn} | ||
[InlineData("https://foo.com:443/bar/17/groups/0ad1/providers/Network/public/4e-ip?version=16", false)] | ||
[InlineData("[email protected]", false)] | ||
[InlineData("~david", false)] | ||
[InlineData("david~", false)] | ||
public void IsValidCSharpName(string value, bool isExpectedMatch) | ||
{ | ||
const string StartCharacterRegex = @"_|[\p{Lu}\p{Ll}\p{Lt}\p{Lm}\p{Lo}\p{Nl}]"; | ||
const string PartCharactersRegex = @"[\p{Lu}\p{Ll}\p{Lt}\p{Lm}\p{Lo}\p{Nl}\p{Mn}\p{Mc}\p{Nd}\p{Pc}\p{Cf}]"; | ||
|
||
const string IdentifierRegex = @"^(" + StartCharacterRegex + ")(" + PartCharactersRegex + ")*$"; | ||
|
||
foreach (RegexOptions options in new[] { RegexOptions.Compiled, RegexOptions.None }) | ||
{ | ||
Regex r = new Regex(IdentifierRegex, options); | ||
Assert.Equal(isExpectedMatch, r.IsMatch(value)); | ||
} | ||
} | ||
|
||
[Theory] | ||
[InlineData("; this is a comment", true)] | ||
[InlineData("\t; so is this", true)] | ||
[InlineData(" ; and this", true)] | ||
[InlineData(";", true)] | ||
[InlineData(";comment\nNotThisBecauseOfNewLine", false)] | ||
[InlineData("-;not a comment", false)] | ||
public void IsCommentLine(string value, bool isExpectedMatch) | ||
{ | ||
const string CommentLineRegex = @"^\s*;\s*(.*?)\s*$"; | ||
|
||
foreach (RegexOptions options in new[] { RegexOptions.Compiled, RegexOptions.None }) | ||
{ | ||
Regex r = new Regex(CommentLineRegex, options); | ||
Assert.Equal(isExpectedMatch, r.IsMatch(value)); | ||
} | ||
} | ||
|
||
[Theory] | ||
[InlineData("[ThisIsASection]", true)] | ||
[InlineData(" [ThisIsASection] ", true)] | ||
[InlineData("\t[ThisIs\\ASection]\t", true)] | ||
[InlineData("\t[This.Is:(A+Section)]\t", true)] | ||
[InlineData("[This Is Not]", false)] | ||
[InlineData("This is not[]", false)] | ||
[InlineData("[Nor This]/", false)] | ||
public void IsSectionLine(string value, bool isExpectedMatch) | ||
{ | ||
const string SectionLineRegex = @"^\s*\[([\w\.\-\+:\/\(\)\\]+)\]\s*$"; | ||
|
||
foreach (RegexOptions options in new[] { RegexOptions.Compiled, RegexOptions.None }) | ||
{ | ||
Regex r = new Regex(SectionLineRegex, options); | ||
Assert.Equal(isExpectedMatch, r.IsMatch(value)); | ||
} | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: I see this came from some ancient BCL blog (at least) but I suggest to replace with a [email protected] address that definitely doesn't belong to anyone.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's actually in the docs:
https://docs.microsoft.com/en-us/dotnet/standard/base-types/how-to-verify-that-strings-are-in-valid-email-format#compile-the-code
I just put all of the docs samples into our tests, basically verbatim.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And I just copied it from the above test.
I can anonymize all the emails (ex.
[email protected]
) in this file, if you two think it is necessary.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Funny enough,
proseware.com
redirects me tohttps://www.microsoft.com/en-us/
. So I think it is safe. 😉https://www.whois.com/whois/proseware.com