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
42 changes: 42 additions & 0 deletions TextFilter.Test/GlobDefaultOptionsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright (c) 2023-2026 ktsu-dev contributors

namespace TextFilter.Test;

using DotNet.Globbing;
using ktsu.TextFilter;
using Microsoft.VisualStudio.TestTools.UnitTesting;

/// <summary>
/// TextFilter's case-sensitive globs stay case-sensitive when the host flips DotNet.Glob's
/// process-wide <see cref="GlobOptions.Default"/> (ktsu-dev/TextFilter#188).
/// </summary>
[TestClass]
public class GlobDefaultOptionsTests
{
// Patterns no other test uses, so none of them is already in TextFilter's glob cache when the
// default is flipped and each is parsed for the first time under it.
[TestMethod]
[DoNotParallelize]
public void CaseSensitiveGlobIgnoresCaseInsensitiveGlobalDefault()
{
bool original = GlobOptions.Default.Evaluation.CaseInsensitive;
GlobOptions.Default.Evaluation.CaseInsensitive = true;
try
{
Assert.IsFalse(TextFilter.IsMatch("IMG.JPG188", "*.jpg188"),
"the default sensitivity should still be case-sensitive");
Assert.IsFalse(TextFilter.IsMatch("IMG.PNG188", "*.png188", TextFilterType.Glob,
TextFilterMatchOptions.ByWordAny, TextFilterCaseSensitivity.CaseSensitive),
"an explicit CaseSensitive should still be case-sensitive");
Assert.IsTrue(TextFilter.IsMatch("IMG.GIF188", "-*.gif188"),
"an exclusion should not hide an item that differs only in case");
Assert.IsTrue(TextFilter.IsMatch("IMG.BMP188", "*.bmp188", TextFilterType.Glob,
TextFilterMatchOptions.ByWordAny, TextFilterCaseSensitivity.CaseInsensitive),
"CaseInsensitive should still ignore case");
}
finally
{
GlobOptions.Default.Evaluation.CaseInsensitive = original;
}
}
}
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
6 changes: 5 additions & 1 deletion TextFilter/TextFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@
private static string CacheKey(string pattern, TextFilterCaseSensitivity caseSensitivity) =>
caseSensitivity is TextFilterCaseSensitivity.CaseInsensitive ? "i:" + pattern : "s:" + pattern;

// Both sensitivities pass their own options. Glob.Parse without options reads DotNet.Glob's static,
// mutable GlobOptions.Default, so a host that sets GlobOptions.Default.Evaluation.CaseInsensitive
// would otherwise turn TextFilter's case-sensitive globs case-insensitive.
private static readonly GlobOptions CaseSensitiveGlobOptions = new() { Evaluation = { CaseInsensitive = false } };
private static readonly GlobOptions CaseInsensitiveGlobOptions = new() { Evaluation = { CaseInsensitive = true } };

// Filter patterns are caller-supplied text, so a pattern with catastrophic backtracking would
Expand Down Expand Up @@ -303,7 +307,7 @@

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

Check warning on line 310 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 310 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 +338,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 341 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 341 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 341 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=AaGtckHC6XUfQ6iTMm4R&open=AaGtckHC6XUfQ6iTMm4R&pullRequest=189
{
Ensure.NotNull(text);
Ensure.NotNull(filter);
Expand Down Expand Up @@ -408,7 +412,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 415 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 415 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 415 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=AaGtckHC6XUfQ6iTMm4S&open=AaGtckHC6XUfQ6iTMm4S&pullRequest=189
{
Ensure.NotNull(filterToken);
Ensure.NotNull(textTokens);
Expand All @@ -425,7 +429,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 432 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 432 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 432 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=AaGtckHC6XUfQ6iTMm4T&open=AaGtckHC6XUfQ6iTMm4T&pullRequest=189
{
Ensure.NotNull(filterToken);
Ensure.NotNull(textTokens);
Expand Down Expand Up @@ -509,7 +513,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 516 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 516 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=AaGtckHC6XUfQ6iTMm4U&open=AaGtckHC6XUfQ6iTMm4U&pullRequest=189
{
if (!token.Contains(']'))
{
Expand Down Expand Up @@ -672,7 +676,7 @@
.Where(pattern => pattern.Length != 0)
.Select(pattern => caseSensitivity is TextFilterCaseSensitivity.CaseInsensitive
? Glob.Parse(pattern, CaseInsensitiveGlobOptions)
: Glob.Parse(pattern))];
: Glob.Parse(pattern, CaseSensitiveGlobOptions))];
globs = parsed is { Length: > 0 } ? parsed : null;
}
catch (Exception ex) when (ex is not OutOfMemoryException)
Expand Down Expand Up @@ -712,7 +716,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 719 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 719 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=AaGtckHC6XUfQ6iTMm4V&open=AaGtckHC6XUfQ6iTMm4V&pullRequest=189
{
Ensure.NotNull(text);
Ensure.NotNull(filter);
Expand Down
Loading