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
9 changes: 8 additions & 1 deletion TextFilter.Test/ReadmeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ namespace TextFilter.Test;

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Reflection;
Expand Down Expand Up @@ -89,7 +90,10 @@ public void EveryMethodInTheReadmeApiReferenceExistsWithItsDocumentedDefaults()
foreach (Match row in rows)
{
string methodName = row.Groups["method"].Value;
MethodInfo[] overloads = [.. methods.Where(m => m.Name == methodName)];
// The binary-compatibility overloads are hidden from IntelliSense and deliberately carry no
// defaults, since defaults on both sets would make calls ambiguous. The README documents the
// visible overloads, so only those are held to its defaults.
MethodInfo[] overloads = [.. methods.Where(m => m.Name == methodName && !IsHiddenFromCallers(m))];
if (overloads.Length == 0)
{
problems.Add($"{methodName} does not exist");
Expand All @@ -115,6 +119,9 @@ public void EveryMethodInTheReadmeApiReferenceExistsWithItsDocumentedDefaults()
Assert.IsEmpty(problems, string.Join("; ", problems));
}

private static bool IsHiddenFromCallers(MethodInfo method) =>
method.GetCustomAttribute<EditorBrowsableAttribute>()?.State == EditorBrowsableState.Never;

[TestMethod]
public void ReadmeMatchOptionsSampleGivesTheDocumentedResults()
{
Expand Down
41 changes: 41 additions & 0 deletions TextFilter.Test/TextFilterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1169,8 +1169,8 @@
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 1172 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=AaGw4eBN2vmpjYR1sSZQ&open=AaGw4eBN2vmpjYR1sSZQ&pullRequest=195
CollectionAssert.AreEqual(new List<string> { "items[3", "notes.txt" }, TextFilter.Filter(items, "-*[[][0-9]]").ToList());

Check warning on line 1173 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=AaGw4eBN2vmpjYR1sSZR&open=AaGw4eBN2vmpjYR1sSZR&pullRequest=195
}

[TestMethod]
Expand Down Expand Up @@ -1205,6 +1205,47 @@
TextFilter.Filter([text], "zzz", filterType, TextFilterMatchOptions.ByWordAll).ToList());
}

[TestMethod]
[DataRow(" ", @"\s")]
[DataRow(" ", @"^\s*$")]
[DataRow(" ", @"\s+")]
[DataRow("", "^$")]
[DataRow("", @"^\s*$")]
[DataRow(" \t ", @"^\s*$")]
public void RegexByWordAnyMatchesBlankTextWithAPatternForBlankText(string text, string pattern)
{
// Blank text splits into no words, and the no-words guard used to return before the whole-text
// attempt, so a filter written to find blank entries missed exactly those under the default.
Assert.IsTrue(TextFilter.IsMatch(text, pattern, TextFilterType.Regex), $"'{pattern}' should match '{text}' under ByWordAny.");
Assert.IsTrue(TextFilter.IsMatch(text, pattern, TextFilterType.Regex, TextFilterMatchOptions.ByWholeString), $"'{pattern}' should match '{text}' under ByWholeString.");
}

[TestMethod]
[DataRow(" ", @"\s")]
[DataRow("", "^$")]
[DataRow(" ", @"^\s*$")]
public void RegexByWordAllStillMatchesNoBlankText(string text, string pattern)
{
Assert.IsFalse(TextFilter.IsMatch(text, pattern, TextFilterType.Regex, TextFilterMatchOptions.ByWordAll));
}

[TestMethod]
[DataRow("")]
[DataRow(" ")]
public void RegexByWordAnyDoesNotMatchBlankTextWithAPatternThatNeedsAWord(string text)
{
Assert.IsFalse(TextFilter.IsMatch(text, "zzz", TextFilterType.Regex));
Assert.IsFalse(TextFilter.IsMatch(text, @"\w", TextFilterType.Regex));
}

[TestMethod]
public void RegexByWordAnyFilterKeepsTheBlankEntries()
{
CollectionAssert.AreEqual(
new List<string> { "", " " },
TextFilter.Filter(["", "hello", " ", "a b"], @"^\s*$", TextFilterType.Regex).ToList());
}

[TestMethod]
public void RegexByWordAllFilterDropsBlankEntries()
{
Expand Down Expand Up @@ -1326,7 +1367,7 @@
{
List<string> result = [.. TextFilter.Filter(["readme.md", "src/a.cs", "docs/", "a b"], filter)];

Assert.AreEqual(1, result.Count);

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

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use 'Assert.HasCount' instead of 'Assert.AreEqual'

See more on https://sonarcloud.io/project/issues?id=ktsu-dev_TextFilter&issues=AaGw4eBN2vmpjYR1sSZS&open=AaGw4eBN2vmpjYR1sSZS&pullRequest=195
Assert.AreEqual("docs/", result[0]);
}

Expand All @@ -1337,7 +1378,7 @@
{
List<string> result = [.. TextFilter.Filter(["readme.md", "src/a.cs", "docs/", "a b"], filter)];

Assert.AreEqual(3, result.Count);

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

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use 'Assert.HasCount' instead of 'Assert.AreEqual'

See more on https://sonarcloud.io/project/issues?id=ktsu-dev_TextFilter&issues=AaGw4eBN2vmpjYR1sSZT&open=AaGw4eBN2vmpjYR1sSZT&pullRequest=195
Assert.AreEqual("readme.md", result[0]);
Assert.AreEqual("src/a.cs", result[1]);
Assert.AreEqual("a b", result[2]);
Expand All @@ -1348,7 +1389,7 @@
{
List<string> result = [.. TextFilter.Filter(["readme.md", "src/a.cs", "docs/"], "+**/")];

Assert.AreEqual(1, result.Count);

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

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use 'Assert.HasCount' instead of 'Assert.AreEqual'

See more on https://sonarcloud.io/project/issues?id=ktsu-dev_TextFilter&issues=AaGw4eBN2vmpjYR1sSZU&open=AaGw4eBN2vmpjYR1sSZU&pullRequest=195
Assert.AreEqual("docs/", result[0]);
}

Expand All @@ -1368,9 +1409,9 @@
// Splitting only on ' ' left a chunk of other whitespace that trimmed to an empty token, and
// the glob tokenizer then read its first character. A pasted trailing tab or newline is enough.
Assert.IsTrue(TextFilter.IsMatch("foo", filter));
CollectionAssert.AreEqual(
new List<string> { "foo" },
TextFilter.Filter(["foo"], filter).ToList());

Check warning on line 1414 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=AaGw4eBN2vmpjYR1sSZV&open=AaGw4eBN2vmpjYR1sSZV&pullRequest=195
}

[TestMethod]
Expand Down Expand Up @@ -1559,11 +1600,11 @@
[TestMethod]
public void Pre17GenericFilterSignatureStillExists()
{
Assert.IsTrue(typeof(TextFilter).GetMethods().Any(m =>
m.Name == nameof(TextFilter.Filter)
&& m.IsGenericMethodDefinition
&& m.GetParameters().Length == 5
&& m.GetParameters()[4].ParameterType == typeof(TextFilterMatchOptions)));

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

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Use 'Assert.Contains' instead of 'Assert.IsTrue'

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

[TestMethod]
Expand Down
18 changes: 10 additions & 8 deletions TextFilter/TextFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@

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

Check warning on line 306 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 306 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 @@ -334,7 +334,7 @@
/// matches nor rejects anything; as a required token it matches everything; and as an excluded
/// token it excludes nothing.
/// </remarks>
public static bool DoesMatchGlob(string text, string filter, TextFilterMatchOptions textFilterMatchOptions, TextFilterCaseSensitivity caseSensitivity = TextFilterCaseSensitivity.CaseSensitive)

Check warning on line 337 in TextFilter/TextFilter.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

This method signature overlaps the one defined on line 35 in file 'TextFilter.BinaryCompatibility.cs', the default parameter value can't be used.

Check warning on line 337 in TextFilter/TextFilter.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

This method signature overlaps the one defined on line 35 in file 'TextFilter.BinaryCompatibility.cs', the default parameter value can't be used.

Check failure on line 337 in TextFilter/TextFilter.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

This method signature overlaps the one defined on line 35 in file 'TextFilter.BinaryCompatibility.cs', the default parameter value can't be used.

See more on https://sonarcloud.io/project/issues?id=ktsu-dev_TextFilter&issues=AaGw4d732vmpjYR1sSZL&open=AaGw4d732vmpjYR1sSZL&pullRequest=195
{
Ensure.NotNull(text);
Ensure.NotNull(filter);
Expand Down Expand Up @@ -408,7 +408,7 @@
/// <param name="textTokens">The set of text tokens to match against.</param>
/// <param name="caseSensitivity">Whether the match distinguishes uppercase from lowercase.</param>
/// <returns><c>true</c> if any token matches the glob filter token; otherwise, <c>false</c>.</returns>
public static bool AnyTokenMatchesGlobFilter(string filterToken, HashSet<string> textTokens, TextFilterCaseSensitivity caseSensitivity = TextFilterCaseSensitivity.CaseSensitive)

Check warning on line 411 in TextFilter/TextFilter.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

This method signature overlaps the one defined on line 45 in file 'TextFilter.BinaryCompatibility.cs', the default parameter value can't be used.

Check warning on line 411 in TextFilter/TextFilter.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

This method signature overlaps the one defined on line 45 in file 'TextFilter.BinaryCompatibility.cs', the default parameter value can't be used.

Check failure on line 411 in TextFilter/TextFilter.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

This method signature overlaps the one defined on line 45 in file 'TextFilter.BinaryCompatibility.cs', the default parameter value can't be used.

See more on https://sonarcloud.io/project/issues?id=ktsu-dev_TextFilter&issues=AaGw4d742vmpjYR1sSZM&open=AaGw4d742vmpjYR1sSZM&pullRequest=195
{
Ensure.NotNull(filterToken);
Ensure.NotNull(textTokens);
Expand All @@ -425,7 +425,7 @@
/// <param name="textTokens">The set of text tokens to match against.</param>
/// <param name="caseSensitivity">Whether the match distinguishes uppercase from lowercase.</param>
/// <returns><c>true</c> if all tokens match the glob filter token; otherwise, <c>false</c>.</returns>
public static bool AllTokensMatchGlobFilter(string filterToken, HashSet<string> textTokens, TextFilterCaseSensitivity caseSensitivity = TextFilterCaseSensitivity.CaseSensitive)

Check warning on line 428 in TextFilter/TextFilter.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

This method signature overlaps the one defined on line 50 in file 'TextFilter.BinaryCompatibility.cs', the default parameter value can't be used.

Check warning on line 428 in TextFilter/TextFilter.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

This method signature overlaps the one defined on line 50 in file 'TextFilter.BinaryCompatibility.cs', the default parameter value can't be used.

Check failure on line 428 in TextFilter/TextFilter.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

This method signature overlaps the one defined on line 50 in file 'TextFilter.BinaryCompatibility.cs', the default parameter value can't be used.

See more on https://sonarcloud.io/project/issues?id=ktsu-dev_TextFilter&issues=AaGw4d742vmpjYR1sSZN&open=AaGw4d742vmpjYR1sSZN&pullRequest=195
{
Ensure.NotNull(filterToken);
Ensure.NotNull(textTokens);
Expand Down Expand Up @@ -509,7 +509,7 @@
// MaxGlobClassMembers characters, or that has no members at all because every range is reversed.
private const int MaxGlobClassMembers = 4096;

internal static string? ExpandGlobClasses(string token)

Check warning on line 512 in TextFilter/TextFilter.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

Refactor this method to reduce its Cognitive Complexity from 16 to the 15 allowed.

Check failure on line 512 in TextFilter/TextFilter.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor this method to reduce its Cognitive Complexity from 16 to the 15 allowed.

See more on https://sonarcloud.io/project/issues?id=ktsu-dev_TextFilter&issues=AaGw4d742vmpjYR1sSZO&open=AaGw4d742vmpjYR1sSZO&pullRequest=195
{
if (!token.Contains(']'))
{
Expand Down Expand Up @@ -712,7 +712,7 @@
/// <see cref="TextFilterMatchOptions.ByWholeString"/> for those.
/// </para>
/// </remarks>
public static bool DoesMatchRegex(string text, string filter, TextFilterMatchOptions textFilterMatchOptions, TextFilterCaseSensitivity caseSensitivity = TextFilterCaseSensitivity.CaseSensitive)

Check warning on line 715 in TextFilter/TextFilter.cs

View workflow job for this annotation

GitHub Actions / ci / .NET / Analyze & Release

This method signature overlaps the one defined on line 40 in file 'TextFilter.BinaryCompatibility.cs', the default parameter value can't be used.

Check failure on line 715 in TextFilter/TextFilter.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

This method signature overlaps the one defined on line 40 in file 'TextFilter.BinaryCompatibility.cs', the default parameter value can't be used.

See more on https://sonarcloud.io/project/issues?id=ktsu-dev_TextFilter&issues=AaGw4d742vmpjYR1sSZP&open=AaGw4d742vmpjYR1sSZP&pullRequest=195
{
Ensure.NotNull(text);
Ensure.NotNull(filter);
Expand All @@ -729,6 +729,16 @@
// Swapped for RegexMatchNothing if the pattern times out partway through
Regex activeRegex = regex;

// No single word contains whitespace, so a pattern like "hello world" or "o\sw" can only ever
// match the whole text. Under ByWordAny, try that first; the per-word pass below still runs,
// so a word-anchored pattern like "^world" keeps matching "hello world". It comes before the
// no-words check below because blank text has no words at all, so a pattern written to find
// blank entries, such as "^\s*$" or "\s", can only ever match the whole text.
if (textFilterMatchOptions is TextFilterMatchOptions.ByWordAny && IsMatchWithinTimeout(ref activeRegex, cacheKey, text))
{
return true;
}

// 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.
Expand All @@ -737,14 +747,6 @@
return false;
}

// No single word contains whitespace, so a pattern like "hello world" or "o\sw" can only ever
// match the whole text. Under ByWordAny, try that first; the per-word pass below still runs,
// so a word-anchored pattern like "^world" keeps matching "hello world".
if (textFilterMatchOptions is TextFilterMatchOptions.ByWordAny && IsMatchWithinTimeout(ref activeRegex, cacheKey, text))
{
return true;
}

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