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

Add more known Regex patterns to increase test coverage. #1617

Merged
merged 1 commit into from
Jan 11, 2020
Merged
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 @@ -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)]
Copy link
Member

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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see this came from some ancient BCL blog

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.

Copy link
Member Author

@eerhardt eerhardt Jan 10, 2020

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.

Copy link
Member Author

@eerhardt eerhardt Jan 10, 2020

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 to https://www.microsoft.com/en-us/. So I think it is safe. 😉

https://www.whois.com/whois/proseware.com

[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));
}
}
}
}