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
38 changes: 38 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=AaGwPTWeBxEszC6s885i&open=AaGwPTWeBxEszC6s885i&pullRequest=192
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=AaGwPTWeBxEszC6s885j&open=AaGwPTWeBxEszC6s885j&pullRequest=192
}

[TestMethod]
Expand Down Expand Up @@ -1313,6 +1313,44 @@
Assert.AreEqual("src/x/a.cs", result[1]);
}

[TestMethod]
[DataRow("src/", "src/**")]
[DataRow("src/a.cs", "src/**")]
[DataRow("src/lib/b.cs", "src/**")]
[DataRow("a/b/", "a/**/b/**")]
[DataRow("/", "/**")]
[DataRow("-b/", "*/**")]
[DataRow("-/", "?/**")]
[DataRow("b-/", "b-/**")]
[DataRow(@"src\", @"src\**")]
public void TrailingGlobStarSegmentMatchesTheEmptyRemainder(string text, string filter)
{
Assert.IsTrue(TextFilter.IsMatch(text, filter, TextFilterType.Glob, TextFilterMatchOptions.ByWholeString));
Assert.IsTrue(TextFilter.IsMatch(text, filter));
}

[TestMethod]
[DataRow("src", "src/**")]
[DataRow("docs/a.cs", "src/**")]
[DataRow("foo", "foo**")]
public void TrailingGlobStarSegmentStillNeedsItsDirectory(string text, string filter)
{
Assert.IsFalse(TextFilter.IsMatch(text, filter, TextFilterType.Glob, TextFilterMatchOptions.ByWholeString));
}

[TestMethod]
public void TrailingGlobStarSegmentFiltersLikeASingleStar()
{
string[] items = ["src/", "src/a.cs", "src/lib/b.cs", "build/", "build/out.dll", "readme.md"];

List<string> included = ["src/", "src/a.cs", "src/lib/b.cs"];
List<string> excluded = ["src/", "src/a.cs", "src/lib/b.cs", "readme.md"];

CollectionAssert.AreEqual(included, TextFilter.Filter(items, "src/**").ToList());

Check warning on line 1349 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=AaGwPTWeBxEszC6s885k&open=AaGwPTWeBxEszC6s885k&pullRequest=192
CollectionAssert.AreEqual(TextFilter.Filter(items, "src/*").ToList(), TextFilter.Filter(items, "src/**").ToList());

Check warning on line 1350 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=AaGwPTWeBxEszC6s885l&open=AaGwPTWeBxEszC6s885l&pullRequest=192
CollectionAssert.AreEqual(excluded, TextFilter.Filter(items, "-build/**").ToList());

Check warning on line 1351 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=AaGwPTWeBxEszC6s885m&open=AaGwPTWeBxEszC6s885m&pullRequest=192
}

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

Assert.AreEqual(1, result.Count);

Check warning on line 1367 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=AaGwPTWeBxEszC6s885n&open=AaGwPTWeBxEszC6s885n&pullRequest=192
Assert.AreEqual("docs/", result[0]);
}

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

Assert.AreEqual(3, result.Count);

Check warning on line 1378 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=AaGwPTWeBxEszC6s885p&open=AaGwPTWeBxEszC6s885p&pullRequest=192
Assert.AreEqual("readme.md", result[0]);
Assert.AreEqual("src/a.cs", result[1]);
Assert.AreEqual("a b", result[2]);
Expand All @@ -1348,7 +1386,7 @@
{
List<string> result = [.. TextFilter.Filter(["readme.md", "src/a.cs", "docs/"], "+**/")];

Assert.AreEqual(1, result.Count);

Check warning on line 1389 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=AaGwPTWeBxEszC6s885o&open=AaGwPTWeBxEszC6s885o&pullRequest=192
Assert.AreEqual("docs/", result[0]);
}

Expand Down Expand Up @@ -1559,11 +1597,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 1604 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=AaGwPTWeBxEszC6s885q&open=AaGwPTWeBxEszC6s885q&pullRequest=192
}

[TestMethod]
Expand Down
18 changes: 17 additions & 1 deletion 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.

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=AaGwPTSPBxEszC6s885d&open=AaGwPTSPBxEszC6s885d&pullRequest=192
{
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=AaGwPTSPBxEszC6s885e&open=AaGwPTSPBxEszC6s885e&pullRequest=192
{
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 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=AaGwPTSPBxEszC6s885f&open=AaGwPTSPBxEszC6s885f&pullRequest=192
{
Ensure.NotNull(filterToken);
Ensure.NotNull(textTokens);
Expand Down Expand Up @@ -492,7 +492,23 @@
}

string tail = maskedToken[start..];
return [.. expansions.Select(prefix => prefix + tail).Distinct(StringComparer.Ordinal)];
return [.. expansions.Select(prefix => ExpandTrailingGlobStar(prefix + tail)).Distinct(StringComparer.Ordinal)];
}

// A trailing whole-segment "**", as in "src/**", should match the empty remainder too, so that
// "dir/**" matches at least what "dir/*" does, "dir/" included. With the separator masked,
// DotNet.Glob reads it as "**" joined to a literal, which needs at least one more character. Since
// * already crosses the masked separator, a plain "*" means the same and also matches nothing.
private static string ExpandTrailingGlobStar(string expansion)
{
if (!expansion.EndsWith("**", StringComparison.Ordinal))
{
return expansion;
}

int globStarStart = expansion.Length - 2;
bool isWholeSegment = globStarStart == 0 || expansion[globStarStart - 1] == MaskedPathSeparator;
return isWholeSegment ? expansion[..globStarStart] + "*" : expansion;
}

// DotNet.Glob reads a bracket class as exactly one token: a single range when it starts "x-y" with
Expand All @@ -509,7 +525,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 528 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 528 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=AaGwPTSPBxEszC6s885g&open=AaGwPTSPBxEszC6s885g&pullRequest=192
{
if (!token.Contains(']'))
{
Expand Down Expand Up @@ -712,7 +728,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 731 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 731 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=AaGwPTSPBxEszC6s885h&open=AaGwPTSPBxEszC6s885h&pullRequest=192
{
Ensure.NotNull(text);
Ensure.NotNull(filter);
Expand Down
Loading