Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit tests fail on Linux for case-insensitive string comparisons #122

Closed
axunonb opened this issue Oct 22, 2019 · 1 comment
Closed

Unit tests fail on Linux for case-insensitive string comparisons #122

axunonb opened this issue Oct 22, 2019 · 1 comment

Comments

@axunonb
Copy link
Member

axunonb commented Oct 22, 2019

While unit test succeed on Windows .NetFramework and Windows .NetCore, they fail on Linux .NetCore.

@axunonb
Copy link
Member Author

axunonb commented Oct 22, 2019

The reason for this are the following methods in class SmartSettings:

internal IEqualityComparer<string> GetCaseSensitivityComparer()
{
    {
        switch (CaseSensitivity)
        {
            case CaseSensitivityType.CaseSensitive:
                return StringComparer.CurrentCulture;
            case CaseSensitivityType.CaseInsensitive:
                return StringComparer.CurrentCultureIgnoreCase;
            default:
                throw new InvalidOperationException(
                    $"The case sensitivity type [{CaseSensitivity}] is unknown.");
        }
    }
}

internal StringComparison GetCaseSensitivityComparison()
{
    {
        switch (CaseSensitivity)
        {
            case CaseSensitivityType.CaseSensitive:
                return StringComparison.CurrentCulture;
            case CaseSensitivityType.CaseInsensitive:
                return StringComparison.CurrentCultureIgnoreCase;
            default:
                throw new InvalidOperationException(
                    $"The case sensitivity type [{CaseSensitivity}] is unknown.");
        }
    }
}

The string are compared linguistically since many years. However, different platforms are handling this in different ways, and .NetCore makes use of the functions provided by the OS.

In most cases it can be assumed that selectors and variables should be comparable without language specifics. Examples why:

// Turkish character variations for "I"
CultureInfo.CurrentCulture = new CultureInfo("en-US");
string.Equals("i", "I", StringComparison.CurrentCultureIgnoreCase); // true
string.Equals("i", "İ", StringComparison.CurrentCultureIgnoreCase); // false

CultureInfo.CurrentCulture = new CultureInfo("tr-TR");
string.Equals("i", "I", StringComparison.CurrentCultureIgnoreCase)); // false
string.Equals("i", "İ", StringComparison.CurrentCultureIgnoreCase)); // true

// The Thai culture doesn't contain '.', so comparisons can be a surprising (tested on Ubuntu v18):
CultureInfo.CurrentCulture = new CultureInfo("th_TH.UTF8");
"Test".StartsWith(".", StringComparison.CurrentCulture); // true!!!
"12.4".IndexOf(".", StringComparison.CurrentCulture);    // 0

Conclusion: Internal comparisons should not be culture-specific, but Ordinal or OrdinalIgnoreCase respectively. However, this must be considered as a breaking change.

@axunonb axunonb changed the title Unit test fail on Linux for case-insensitive string comparisons Unit tests fail on Linux for case-insensitive string comparisons Oct 22, 2019
@axunonb axunonb self-assigned this Oct 22, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant