Skip to content
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
26 changes: 26 additions & 0 deletions TextFilter.Test/TextFilterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -792,4 +792,30 @@
TextFilter.Filter(["file1.txt"], pattern, TextFilterType.Glob, TextFilterMatchOptions.ByWordAny, caseSensitivity).ToList());
}
}

[TestMethod]
[DataRow("", TextFilterType.Regex)]
[DataRow(" ", TextFilterType.Regex)]
[DataRow("", TextFilterType.Glob)]
[DataRow(" ", TextFilterType.Glob)]
public void ByWordAllDoesNotMatchTextWithNoWords(string text, TextFilterType filterType)
{
// Blank text splits into no words, and All over nothing is vacuously true, so the regex path
// used to match it against every pattern while the glob path did not. Both now agree.
Assert.IsFalse(TextFilter.IsMatch(text, "zzz", filterType, TextFilterMatchOptions.ByWordAll));
CollectionAssert.AreEqual(
new List<string>(),
TextFilter.Filter([text], "zzz", filterType, TextFilterMatchOptions.ByWordAll).ToList());

Check warning on line 808 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=AaDbxqBJz5PIa1mb9LAT&open=AaDbxqBJz5PIa1mb9LAT&pullRequest=109
}

[TestMethod]
public void RegexByWordAllFilterDropsBlankEntries()
{
CollectionAssert.AreEqual(
new List<string>(),
TextFilter.Filter(["", "hello"], "zzz", TextFilterType.Regex, TextFilterMatchOptions.ByWordAll).ToList());

Check warning on line 816 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=AaDbxqBJz5PIa1mb9LAU&open=AaDbxqBJz5PIa1mb9LAU&pullRequest=109
CollectionAssert.AreEqual(
new List<string> { "hello" },
TextFilter.Filter(["", "hello"], "hel", TextFilterType.Regex, TextFilterMatchOptions.ByWordAll).ToList());

Check warning on line 819 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=AaDbxqBJz5PIa1mb9LAV&open=AaDbxqBJz5PIa1mb9LAV&pullRequest=109
}
}
8 changes: 8 additions & 0 deletions TextFilter/TextFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,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 126 in TextFilter/TextFilter.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Pass a timeout to limit the execution time.

Check warning on line 126 in TextFilter/TextFilter.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Pass a timeout to limit the execution time.

Check warning on line 126 in TextFilter/TextFilter.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Pass a timeout to limit the execution time.

Check warning on line 126 in TextFilter/TextFilter.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Pass a timeout to limit the execution time.

private static Glob GlobMatchAnything() => Glob.Parse("*");

Expand Down Expand Up @@ -277,7 +277,7 @@

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

Check warning on line 280 in TextFilter/TextFilter.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Extract this nested ternary operation into an independent statement.

Check warning on line 280 in TextFilter/TextFilter.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Extract this nested ternary operation into an independent statement.

Check warning on line 280 in TextFilter/TextFilter.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Extract this nested ternary operation into an independent statement.

Check warning on line 280 in TextFilter/TextFilter.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Extract this nested ternary operation into an independent statement.

Check warning on line 280 in TextFilter/TextFilter.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Extract this nested ternary operation into an independent statement.

Check warning on line 280 in TextFilter/TextFilter.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Extract this nested ternary operation into an independent statement.
? TextFilterTokenType.Required
: TextFilterTokenType.Optional;
})
Expand Down Expand Up @@ -475,6 +475,14 @@
AddBounded(RegexCache, cacheKey, regex);
}

// Text with no words -- "" or " " split by word -- has nothing for the pattern to match.
// Without this, Enumerable.All over the empty set is vacuously true, so under ByWordAll blank
// text matched every pattern while the glob path reported no match for the same input.
if (textTokens.Count == 0)
{
return false;
}

Func<IEnumerable<string>, Func<string, bool>, bool> matchFunc = textFilterMatchOptions is TextFilterMatchOptions.ByWordAny
? Enumerable.Any
: Enumerable.All;
Expand Down
Loading