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

Test that StringComparers and StringComparisons are kept in sync #9527

Merged
merged 4 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ internal static class StringComparisons
public static StringComparison PropertyLiteralValues => StringComparison.OrdinalIgnoreCase;
public static StringComparison PropertyValues => StringComparison.Ordinal;
public static StringComparison RuleNames => StringComparison.OrdinalIgnoreCase;
public static StringComparison CategoryNames => StringComparison.OrdinalIgnoreCase;
public static StringComparison ConfigurationDimensionNames => StringComparison.Ordinal;
public static StringComparison ConfigurationDimensionValues => StringComparison.Ordinal;
public static StringComparison DependencyIds => StringComparison.OrdinalIgnoreCase;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
// Licensed to the .NET Foundation under one or more agreements. The .NET Foundation licenses this file to you under the MIT license. See the LICENSE.md file in the project root for more information.

using System.Reflection;

namespace Microsoft.VisualStudio;

public sealed class StringComparersTests
{
[Fact]
public void StringComparersAndStringComparisonsMatch()
{
var flags = BindingFlags.Public | BindingFlags.Static;

var comparers = typeof(StringComparers).GetProperties(flags).OrderBy(c => c.Name, StringComparer.Ordinal).ToList();
var comparisons = typeof(StringComparisons).GetProperties(flags).OrderBy(c => c.Name, StringComparer.Ordinal).ToList();

var comparerNames = comparers.Select(c => c.Name).ToList();
var comparisonNames = comparisons.Select(c => c.Name).ToList();

// Check that all comparers have a matching comparison.
// Include details about which ones are missing in the test failure message to make fixing easier.
var extraComparers = comparerNames.Except(comparisonNames, StringComparer.Ordinal).ToList();
var extraComparisons = comparisonNames.Except(comparerNames, StringComparer.Ordinal).ToList();

if (extraComparers.Count + extraComparisons.Count != 0)
{
Assert.Fail($"""
Mismatched {nameof(StringComparers)} and {nameof(StringComparisons)}:
- Comparers without matching comparisons: {string.Join(", ", extraComparers)}
- Comparisons without matching comparers: {string.Join(", ", extraComparisons)}
""");
}

var comparerValues = comparers.Select(c => (c.Name, Value: (StringComparer)c.GetValue(null)!)).ToList();
var comparisonValues = comparisons.Select(c => (c.Name, Value: (StringComparison)c.GetValue(null)!)).ToList();

// Check that all comparer values match the corresponding comparison values.
foreach (var (comparer, comparison) in comparerValues.Zip(comparisonValues, (a, b) => (a, b)))
{
Assert.Equal(comparer.Name, comparison.Name, StringComparer.Ordinal);

var comparerKind = GetComparerKind(comparer.Value);
var comparisonKind = GetComparisonKind(comparison.Value);

if (!string.Equals(comparerKind, comparisonKind, StringComparison.Ordinal))
{
Assert.Fail($"""
Mismatched comparisons:
- {nameof(StringComparers)}.{comparer.Name} = {comparerKind}
- {nameof(StringComparisons)}.{comparer.Name} = {comparisonKind}
""");
}
}

static string GetComparerKind(StringComparer comparer)
{
foreach (var (c, name) in Comparers())
{
if (Equals(c, comparer))
{
return name;
}
}

Assert.Fail("Unknown comparer: " + comparer);
return null!; // Unreachable

static IEnumerable<(StringComparer, string)> Comparers()
{
yield return (StringComparer.Ordinal, nameof(StringComparer.Ordinal));
yield return (StringComparer.OrdinalIgnoreCase, nameof(StringComparer.OrdinalIgnoreCase));
yield return (StringComparer.CurrentCulture, nameof(StringComparer.CurrentCulture));
yield return (StringComparer.CurrentCultureIgnoreCase, nameof(StringComparer.CurrentCultureIgnoreCase));
yield return (StringComparer.InvariantCulture, nameof(StringComparer.InvariantCulture));
yield return (StringComparer.InvariantCultureIgnoreCase, nameof(StringComparer.InvariantCultureIgnoreCase));
}
}

static string GetComparisonKind(StringComparison comparison)
{
foreach (var (c, name) in Comparisons())
{
if (c == comparison)
{
return name;
}
}

Assert.Fail("Unknown comparison: " + comparison);
return null!; // Unreachable

static IEnumerable<(StringComparison, string)> Comparisons()
{
yield return (StringComparison.Ordinal, nameof(StringComparison.Ordinal));
yield return (StringComparison.OrdinalIgnoreCase, nameof(StringComparison.OrdinalIgnoreCase));
yield return (StringComparison.CurrentCulture, nameof(StringComparison.CurrentCulture));
yield return (StringComparison.CurrentCultureIgnoreCase, nameof(StringComparison.CurrentCultureIgnoreCase));
yield return (StringComparison.InvariantCulture, nameof(StringComparison.InvariantCulture));
yield return (StringComparison.InvariantCultureIgnoreCase, nameof(StringComparison.InvariantCultureIgnoreCase));
}
}
}
}
Loading