Skip to content
Open
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
34 changes: 34 additions & 0 deletions TextFilter.Test/TextFilterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1064,6 +1064,40 @@
}
}

[TestMethod]
[DataRow("[a]]", "[a][]]")]
[DataRow("[]]]", "[]][]]")]
[DataRow("*[[][0-9]]", "*[[][0-9][]]")]
public void ExpandGlobClassesWritesALiteralBracketAfterAClassAsItsOwnClass(string token, string expected) =>
Assert.AreEqual(expected, TextFilter.ExpandGlobClasses(token));

[TestMethod]
[DataRow("*[[][0-9]]", "items[3]", "items[3")]
[DataRow("[a]]", "a]", "a")]
[DataRow("[ab]]", "b]", "b")]
[DataRow("[!a]]", "b]", "b")]
[DataRow("*[[][0-9a-f]]", "h[c]", "h[c")]
[DataRow("[a]]]", "a]]", "a]")]
[DataRow("*[[]DEBUG]*", "x[DEBUG]y", "xDEBUGy")]
public void GlobClassFollowedByALiteralBracketMatchesTheBracket(string pattern, string matching, string notMatching)
{
// DotNet.Glob read a "]" straight after a class as one more member of that class, so the pattern
// needed one character fewer than it was written with (#172).
foreach (TextFilterCaseSensitivity caseSensitivity in new[] { TextFilterCaseSensitivity.CaseSensitive, TextFilterCaseSensitivity.CaseInsensitive })
{
Assert.IsTrue(TextFilter.IsMatch(matching, pattern, TextFilterType.Glob, TextFilterMatchOptions.ByWholeString, caseSensitivity), $"{pattern} should match {matching} ({caseSensitivity})");
Assert.IsFalse(TextFilter.IsMatch(notMatching, pattern, TextFilterType.Glob, TextFilterMatchOptions.ByWholeString, caseSensitivity), $"{pattern} should not match {notMatching} ({caseSensitivity})");
}
}

[TestMethod]
public void FilterWithAClassFollowedByALiteralBracketKeepsAndExcludesTheRightItems()
{
List<string> items = ["items[3]", "items[3", "notes.txt"];
CollectionAssert.AreEqual(new List<string> { "items[3]" }, TextFilter.Filter(items, "*[[][0-9]]").ToList());

Check warning on line 1097 in TextFilter.Test/TextFilterTests.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use 'Assert.AreSequenceEqual' instead of 'CollectionAssert.AreEqual'

See more on https://sonarcloud.io/project/issues?id=ktsu-dev_TextFilter&issues=AaGV0_dRfHPHje6d_UfR&open=AaGV0_dRfHPHje6d_UfR&pullRequest=177
CollectionAssert.AreEqual(new List<string> { "items[3", "notes.txt" }, TextFilter.Filter(items, "-*[[][0-9]]").ToList());

Check warning on line 1098 in TextFilter.Test/TextFilterTests.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use 'Assert.AreSequenceEqual' instead of 'CollectionAssert.AreEqual'

See more on https://sonarcloud.io/project/issues?id=ktsu-dev_TextFilter&issues=AaGV0_dRfHPHje6d_UfS&open=AaGV0_dRfHPHje6d_UfS&pullRequest=177
}

[TestMethod]
[DataRow("-readme[a-")]
[DataRow("!readme[a-")]
Expand Down
9 changes: 9 additions & 0 deletions TextFilter/TextFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@
private static readonly TimeSpan RegexMatchTimeout = TimeSpan.FromSeconds(1);

[System.Diagnostics.CodeAnalysis.SuppressMessage("Performance", "SYSLIB1045:Convert to 'GeneratedRegexAttribute'.", Justification = "Not available in older frameworks")]
private static Regex RegexMatchAnything() => new(".*", RegexOptions.Compiled);

Check warning on line 128 in TextFilter/TextFilter.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Pass a timeout to limit the execution time.

Check warning on line 128 in TextFilter/TextFilter.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Pass a timeout to limit the execution time.

Check warning on line 128 in TextFilter/TextFilter.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Pass a timeout to limit the execution time.

Check warning on line 128 in TextFilter/TextFilter.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Pass a timeout to limit the execution time.

Check warning on line 128 in TextFilter/TextFilter.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Pass a timeout to limit the execution time.

// Stands in for a pattern that has already run out of time once. The timeout bounds a single
// IsMatch call, so without this the same pathological pattern would pay it again for every word
Expand Down Expand Up @@ -300,7 +300,7 @@

return ExcludedTokenPrefixes.Contains(prefix)
? TextFilterTokenType.Excluded
: RequiredTokenPrefixes.Contains(prefix)

Check warning on line 303 in TextFilter/TextFilter.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Extract this nested ternary operation into an independent statement.

Check warning on line 303 in TextFilter/TextFilter.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Extract this nested ternary operation into an independent statement.

Check warning on line 303 in TextFilter/TextFilter.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Extract this nested ternary operation into an independent statement.

Check warning on line 303 in TextFilter/TextFilter.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Extract this nested ternary operation into an independent statement.

Check warning on line 303 in TextFilter/TextFilter.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Extract this nested ternary operation into an independent statement.
? TextFilterTokenType.Required
: TextFilterTokenType.Optional;
})
Expand Down Expand Up @@ -535,6 +535,15 @@

result.Append(token, index, open - index).Append(rewritten);
index = close + 1;

// DotNet.Glob reads a "]" straight after a class as one more member of it, so "[a]]" would
// match "a" and not "a]". Each such "]" is written as the one-member class "[]]", which it
// reads as the literal the pattern means.
while (index < token.Length && token[index] == ']')
{
result.Append("[]]");
index++;
}
}

return result.Append(token, index, token.Length - index).ToString();
Expand Down
Loading