Repository: dotnet/aspnetcore
File: src/Components/WebAssembly/WebAssembly/test/Hosting/WebAssemblyCultureProviderTest.cs (C#)

Code before:
using Microsoft.AspNetCore.InternalTesting;

namespace Microsoft.AspNetCore.Components.WebAssembly.Hosting;

public class WebAssemblyCultureProviderTest
{
    [Theory]
    [InlineData("fr-FR", new[] { "fr-FR", "fr" })]
    [InlineData("tzm-Latn-DZ", new[] { "tzm-Latn-DZ", "tzm-Latn", "tzm" })]
    public void GetCultures_ReturnsCultureClosure(string cultureName, string[] expected)
    {
        // Arrange
        var culture = new CultureInfo(cultureName);

        // Act
        var actual = WebAssemblyCultureProvider.GetCultures(culture);

        // Assert
        Assert.Equal(expected, actual);
    }

    [Theory]
    [InlineData("fr-FR", "tzm-Latn-DZ", new[] { "fr-FR", "tzm-Latn-DZ", "fr", "tzm-Latn", "tzm" })]
    [InlineData("fr-FR", null, new[] { "fr-FR", "fr" })]
    [InlineData(null, null, new string[0])]
    public void GetCultures_ReturnCultureClosureWithUICulture(string cultureName, string uiCultureName, string[] expected)
    {
        // Arrange
        var culture = cultureName != null ? new CultureInfo(cultureName) : null;
        var uiCulture = uiCultureName != null ? new CultureInfo(uiCultureName) : null;
        // Act
        var actual = WebAssemblyCultureProvider.GetCultures(culture, uiCulture);
        // Assert
        Assert.Equal(expected, actual);
    }

    [Fact]
    public void ThrowIfCultureChangeIsUnsupported_ThrowsIfCulturesAreDifferentAndICUShardingIsUsed()
    {
        // Arrange
        Environment.SetEnvironmentVariable("__BLAZOR_SHARDED_ICU", "1");
        try
        {
            // WebAssembly is initialized with en-US
            var cultureProvider = new WebAssemblyCultureProvider(new CultureInfo("en-US"), new CultureInfo("en-US"));

            // Culture is changed to fr-FR as part of the app
            using var cultureReplacer = new CultureReplacer("fr-FR");

            var ex = Assert.Throws<InvalidOperationException>(() => cultureProvider.ThrowIfCultureChangeIsUnsupported());
            Assert.Equal("Blazor detected a change in the application's culture that is not supported with the current project configuration. " +

Review comment (refactor):
You're using hash set, so there should be no duplicates right now but let's add test data with same culture for both args to make sure that the list has always unique elements. Or same-parent cultures if you prefer, e.g. "en-US" and "en-GB", to make sure they won't have "en" twice.

Code after:
using Microsoft.AspNetCore.InternalTesting;

namespace Microsoft.AspNetCore.Components.WebAssembly.Hosting;

public class WebAssemblyCultureProviderTest
{
    [Theory]
    [InlineData("fr-FR", new[] { "fr-FR", "fr" })]
    [InlineData("tzm-Latn-DZ", new[] { "tzm-Latn-DZ", "tzm-Latn", "tzm" })]
    public void GetCultures_ReturnsCultureClosure(string cultureName, string[] expected)
    {
        // Arrange
        var culture = new CultureInfo(cultureName);

        // Act
        var actual = WebAssemblyCultureProvider.GetCultures(culture);

        // Assert
        Assert.Equal(expected, actual);
    }

    [Theory]
    [InlineData("fr-FR", "tzm-Latn-DZ", new[] { "fr-FR", "fr", "tzm-Latn-DZ", "tzm-Latn", "tzm" })]
    [InlineData("en-US", "en-GB", new[] { "en-US", "en", "en-GB" })]
    [InlineData("fr-FR", null, new[] { "fr-FR", "fr" })]
    public void GetCultures_ReturnCultureClosureWithUICulture(string cultureName, string uiCultureName, string[] expected)
    {
        // Arrange
        var culture = cultureName != null ? new CultureInfo(cultureName) : null;
        var uiCulture = uiCultureName != null ? new CultureInfo(uiCultureName) : null;
        // Act
        var actual = WebAssemblyCultureProvider.GetCultures(culture, uiCulture);
        // Assert
        Assert.Equal(expected, actual);
    }

    [Fact]
    public void ThrowIfCultureChangeIsUnsupported_ThrowsIfCulturesAreDifferentAndICUShardingIsUsed()
    {
        // Arrange
        Environment.SetEnvironmentVariable("__BLAZOR_SHARDED_ICU", "1");
        try
        {
            // WebAssembly is initialized with en-US
            var cultureProvider = new WebAssemblyCultureProvider(new CultureInfo("en-US"), new CultureInfo("en-US"));

            // Culture is changed to fr-FR as part of the app
            using var cultureReplacer = new CultureReplacer("fr-FR");

            var ex = Assert.Throws<InvalidOperationException>(() => cultureProvider.ThrowIfCultureChangeIsUnsupported());
            Assert.Equal("Blazor detected a change in the application's culture that is not supported with the current project configuration. " +