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
27 changes: 27 additions & 0 deletions TextFilter.Test/TextFilterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1020,4 +1020,31 @@
{
Assert.IsFalse(TextFilter.IsMatch(text, "a*", TextFilterType.Regex, TextFilterMatchOptions.ByWordAll));
}

private static readonly string[] PaddingCandidates = ["hello world", "helium", "help", "yellow", "abcdef"];
private static readonly string[] HelMatches = ["hello world", "helium", "help"];

[TestMethod]
[DataRow("hel ")]
[DataRow(" hel")]
[DataRow(" hel\t")]
public void FuzzyIsMatchIgnoresFilterPadding(string paddedFilter)
{
foreach (string text in PaddingCandidates)
{
Assert.AreEqual(TextFilter.IsMatch(text, "hel", TextFilterType.Fuzzy), TextFilter.IsMatch(text, paddedFilter, TextFilterType.Fuzzy),
$"Padding in '{paddedFilter}' should not change whether '{text}' fuzzy-matches.");
}
}

[TestMethod]
[DataRow("hel ")]
[DataRow(" hel")]
public void FuzzyFilterIgnoresFilterPadding(string paddedFilter)
{
List<string> strings = ["hello world", "helium", "help", "yellow"];
List<string> result = [.. TextFilter.Filter(strings, paddedFilter, TextFilterType.Fuzzy)];
CollectionAssert.AreEquivalent(HelMatches, result,
$"Padding in '{paddedFilter}' should not hide items that 'hel' matches.");

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

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

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

See more on https://sonarcloud.io/project/issues?id=ktsu-dev_TextFilter&issues=AaDpTr2_qHqnOTNnlVGP&open=AaDpTr2_qHqnOTNnlVGP&pullRequest=129
}
}
3 changes: 2 additions & 1 deletion TextFilter/TextFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,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 127 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 127 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 127 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 127 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 @@ -240,7 +240,8 @@
{
TextFilterType.Glob => DoesMatchGlob(text, filter, textFilterMatchOptions, caseSensitivity),
TextFilterType.Regex => DoesMatchRegex(text, filter, textFilterMatchOptions, caseSensitivity),
TextFilterType.Fuzzy => Fuzzy.Contains(text.AsSpan(), filter.AsSpan(), out score),
// Padding would otherwise become a space the text must contain, so " hel" matched nothing.
TextFilterType.Fuzzy => Fuzzy.Contains(text.AsSpan(), filter.AsSpan().Trim(), out score),
_ => throw new NotImplementedException($"{nameof(TextFilterType)}.{filterType} has not been implemented"),
};
}
Expand Down Expand Up @@ -277,7 +278,7 @@

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

Check warning on line 281 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 281 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 281 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 281 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 281 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 281 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
Loading