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
21 changes: 21 additions & 0 deletions TextFilter.Test/TextFilterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,27 @@ public void DoesMatchGlobWithByWordAllOptionReturnsFalse()
Assert.IsFalse(result, "Not all words matching with ByWordAll option should return false.");
}

[TestMethod]
public void DoesMatchGlobWithRequiredTokenByWordAllReturnsTrue()
{
bool result = TextFilter.DoesMatchGlob("hello world", "hello* +world", TextFilterMatchOptions.ByWordAll);
Assert.IsTrue(result, "A required token present among the text's words should return true under ByWordAll.");
}

[TestMethod]
public void DoesMatchGlobWithAllRequiredTokensByWordAllReturnsTrue()
{
bool result = TextFilter.DoesMatchGlob("hello world", "+hello +world", TextFilterMatchOptions.ByWordAll);
Assert.IsTrue(result, "Every required token being present should return true under ByWordAll.");
}

[TestMethod]
public void DoesMatchGlobWithMissingRequiredTokenByWordAllReturnsFalse()
{
bool result = TextFilter.DoesMatchGlob("hello world", "hello* +missing", TextFilterMatchOptions.ByWordAll);
Assert.IsFalse(result, "A required token absent from the text should still return false under ByWordAll.");
}

[TestMethod]
public void DoesMatchGlobHandlesPartialFilter()
{
Expand Down
10 changes: 5 additions & 5 deletions TextFilter/TextFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
private static ConcurrentDictionary<string, Glob> GlobCache { get; } = [];

[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 68 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 68 in TextFilter/TextFilter.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Pass a timeout to limit the execution time.

/// <summary>
/// Gets a hint for the specified filter type.
Expand Down Expand Up @@ -213,7 +213,7 @@

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

Check warning on line 216 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 216 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 216 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 216 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 @@ -283,11 +283,11 @@
return false; // optional tokens were set but text does not contain any optional tokens
}

Func<string, HashSet<string>, bool> requiredMatchFunc = textFilterMatchOptions is TextFilterMatchOptions.ByWordAny
? AnyTokenMatchesGlobFilter
: AllTokensMatchGlobFilter;

bool allRequiredMatches = requiredTokens.All(filterToken => requiredMatchFunc(filterToken, textTokens));
// A required token asks whether it appears among the text's words, which is the same
// question the excluded tokens above ask, so it uses the same function under every match
// option. Matching it with AllTokensMatchGlobFilter under ByWordAll would instead demand
// that every word in the text match the one required token, which no multi-word text can do.
bool allRequiredMatches = requiredTokens.All(filterToken => AnyTokenMatchesGlobFilter(filterToken, textTokens));

if (!allRequiredMatches)
{
Expand Down Expand Up @@ -355,7 +355,7 @@
{
try
{
regex = new Regex(filter, RegexOptions.Compiled);

Check warning on line 358 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 358 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 358 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 358 in TextFilter/TextFilter.cs

View workflow job for this annotation

GitHub Actions / Analyze & Release

Pass a timeout to limit the execution time.
}
catch (ArgumentException)
{
Expand Down
Loading