Skip to content

Commit

Permalink
Merge pull request #9527 from drewnoakes/string-comparers-sync-test
Browse files Browse the repository at this point in the history
Test that StringComparers and StringComparisons are kept in sync
  • Loading branch information
drewnoakes committed Aug 28, 2024
2 parents 1e2df7c + 36921a6 commit be5d271
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 2 deletions.
4 changes: 2 additions & 2 deletions docs/build-acceleration.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ Looking through the build output with the following points in mind:

## Reference Assemblies

A reference assembly is a DLLs that models the public API of a project, without any actual implementation.
A reference assembly is a DLL that models the public API of a project, without any actual implementation.

During build, reference assembly timestamps are only updated when their project's public API changes. Incremental build systems use file system timestamps for many of their optimisations. If project changes are internal-only (e.g. method bodies, private members added/removed, documentation changed) then the timestamp is not changed. Knowing the time at which a public API was last changed allows skipping some compilation.

Expand All @@ -184,7 +184,7 @@ In our example, `A` only need to recompile if it has its own changes, or if `B`'

Production of a reference assembly is controlled by the `ProduceReferenceAssembly` MSBuild property, and the feature is part of MSBuild directly. This means it works well outside of VS, in case you also do CLI builds. Note that most CI builds are non-incremental (they happen on fresh clones), so this property has no impact there.

When `ProduceReferenceAssembly` was introduced in .NET 5, it was only enabled by default for .NET 5 and later. We investigated changing the default for earlier frameworks too, but this caused issues in a very small number of highly customised builds and we take backwards compatability very seriously. That said, it's generally desirable to configure projects to produce reference assemblies, regardless of whether you use Build Acceleration or not.
When `ProduceReferenceAssembly` was introduced in .NET 5, it was only enabled by default for .NET 5 and later. We investigated changing the default for earlier frameworks too, but this caused issues in a very small number of highly customised builds and we take backwards compatibility very seriously. That said, it's generally desirable to configure projects to produce reference assemblies, regardless of whether you use Build Acceleration or not.

For more information, see [Reference Assemblies](https://learn.microsoft.com/dotnet/standard/assembly/reference-assemblies) on Microsoft Learn.

Expand Down
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,138 @@
// 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.Globalization;
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 currentCulture = CultureInfo.CurrentCulture;
var currentUICulture = CultureInfo.CurrentUICulture;
var defaultThreadCurrentCulture = CultureInfo.DefaultThreadCurrentCulture;
var defaultThreadCurrentUICulture = CultureInfo.DefaultThreadCurrentUICulture;

try
{
// Temporarily set the culture to en-AU to ensure consistent results.
// This prevents test failures when the current culture is the invariant culture.
CultureInfo.CurrentCulture = new CultureInfo("en-AU");
CultureInfo.CurrentUICulture = new CultureInfo("en-AU");
CultureInfo.DefaultThreadCurrentCulture = new CultureInfo("en-AU");
CultureInfo.DefaultThreadCurrentUICulture = new CultureInfo("en-AU");

ValidateSets();

ValidateValues();
}
finally
{
CultureInfo.CurrentCulture = currentCulture;
CultureInfo.CurrentUICulture = currentUICulture;
CultureInfo.DefaultThreadCurrentCulture = defaultThreadCurrentCulture;
CultureInfo.DefaultThreadCurrentUICulture = defaultThreadCurrentUICulture;
}

void ValidateSets()
{
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)}
""");
}
}

void ValidateValues()
{
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}
""");
}
}

return;

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

Assert.Fail("Unknown comparer: " + comparer);
throw Assumes.NotReachable();

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);
throw Assumes.NotReachable();

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));
}
}
}
}
}

0 comments on commit be5d271

Please sign in to comment.