Skip to content
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
Prev Previous commit
Next Next commit
Fix Invariant Test to Work with WASM environment
  • Loading branch information
tarekgh committed Jun 30, 2021
commit 1fdba5976b85fe07d403ad1367382c2fa217622d
66 changes: 42 additions & 24 deletions src/libraries/System.Globalization/tests/Invariant/InvariantMode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Buffers;
using System.Reflection;
using System.Buffers.Binary;
using System.Collections.Generic;
using System.IO;
Expand All @@ -13,6 +14,23 @@ namespace System.Globalization.Tests
{
public class InvariantModeTests
{
private static bool PredefinedCulturesOnlyIsDisabled => !PredefinedCulturesOnly();
private static bool PredefinedCulturesOnly()
{
bool ret;

try
{
ret = (bool) typeof(object).Assembly.GetType("System.Globalization.GlobalizationMode").GetProperty("PredefinedCulturesOnly", BindingFlags.Static | BindingFlags.NonPublic).GetValue(null);
}
catch
{
ret = false;
}

return ret;
}

public static IEnumerable<object[]> Cultures_TestData()
{
yield return new object[] { "en-US" };
Expand Down Expand Up @@ -490,13 +508,13 @@ public static IEnumerable<object[]> GetUnicode_TestData()
yield return new object[] { "xn--de-jg4avhby1noc0d", 0, 21, "\u30D1\u30D5\u30A3\u30FC\u0064\u0065\u30EB\u30F3\u30D0" };
}

[Fact]
[ConditionalFact(nameof(PredefinedCulturesOnlyIsDisabled))]
public static void IcuShouldNotBeLoaded()
{
Assert.False(PlatformDetection.IsIcuGlobalization);
}

[Theory]
[ConditionalTheory(nameof(PredefinedCulturesOnlyIsDisabled))]
[MemberData(nameof(Cultures_TestData))]
public void TestCultureData(string cultureName)
{
Expand Down Expand Up @@ -636,7 +654,7 @@ public void TestCultureData(string cultureName)
Assert.True(cultureName.Equals(ci.CompareInfo.Name, StringComparison.OrdinalIgnoreCase));
}

[Theory]
[ConditionalTheory(nameof(PredefinedCulturesOnlyIsDisabled))]
[MemberData(nameof(Cultures_TestData))]
public void SetCultureData(string cultureName)
{
Expand All @@ -652,13 +670,13 @@ public void SetCultureData(string cultureName)
Assert.Throws<ArgumentOutOfRangeException>(() => ci.DateTimeFormat.Calendar = new TaiwanCalendar());
}

[Fact]
[ConditionalFact(nameof(PredefinedCulturesOnlyIsDisabled))]
public void TestEnum()
{
Assert.Equal(new CultureInfo[1] { CultureInfo.InvariantCulture }, CultureInfo.GetCultures(CultureTypes.AllCultures));
}

[Theory]
[ConditionalTheory(nameof(PredefinedCulturesOnlyIsDisabled))]
[MemberData(nameof(Cultures_TestData))]
public void TestSortVersion(string cultureName)
{
Expand All @@ -670,7 +688,7 @@ public void TestSortVersion(string cultureName)
Assert.Equal(version, new CultureInfo(cultureName).CompareInfo.Version);
}

[Theory]
[ConditionalTheory(nameof(PredefinedCulturesOnlyIsDisabled))]
[InlineData(0, 0)]
[InlineData(1, 2)]
[InlineData(100_000, 200_000)]
Expand All @@ -683,7 +701,7 @@ public void TestGetSortKeyLength_Valid(int inputLength, int expectedSortKeyLengt
Assert.Equal(expectedSortKeyLength, CultureInfo.InvariantCulture.CompareInfo.GetSortKeyLength(dummySpan));
}

[Theory]
[ConditionalTheory(nameof(PredefinedCulturesOnlyIsDisabled))]
[InlineData(0x4000_0000)]
[InlineData(int.MaxValue)]
public unsafe void TestGetSortKeyLength_OverlongArgument(int inputLength)
Expand All @@ -698,7 +716,7 @@ public unsafe void TestGetSortKeyLength_OverlongArgument(int inputLength)
});
}

[Theory]
[ConditionalTheory(nameof(PredefinedCulturesOnlyIsDisabled))]
[InlineData("Hello", CompareOptions.None, "Hello")]
[InlineData("Hello", CompareOptions.IgnoreWidth, "Hello")]
[InlineData("Hello", CompareOptions.IgnoreCase, "HELLO")]
Expand Down Expand Up @@ -741,7 +759,7 @@ public unsafe void TestSortKey_FromSpan(string input, CompareOptions options, st
}
}

[Fact]
[ConditionalFact(nameof(PredefinedCulturesOnlyIsDisabled))]
public void TestSortKey_ZeroWeightCodePoints()
{
// In the invariant globalization mode, there's no such thing as a zero-weight code point,
Expand All @@ -753,7 +771,7 @@ public void TestSortKey_ZeroWeightCodePoints()
Assert.NotEqual(0, SortKey.Compare(sortKeyForEmptyString, sortKeyForZeroWidthJoiner));
}

[Theory]
[ConditionalTheory(nameof(PredefinedCulturesOnlyIsDisabled))]
[InlineData("", "", 0)]
[InlineData("", "not-empty", -1)]
[InlineData("not-empty", "", 1)]
Expand Down Expand Up @@ -794,7 +812,7 @@ private static StringComparison GetStringComparison(CompareOptions options)
return sc;
}

[Theory]
[ConditionalTheory(nameof(PredefinedCulturesOnlyIsDisabled))]
[MemberData(nameof(IndexOf_TestData))]
public void TestIndexOf(string source, string value, int startIndex, int count, CompareOptions options, int result)
{
Expand Down Expand Up @@ -841,7 +859,7 @@ static void TestCore(CompareInfo compareInfo, string source, string value, int s
}
}

[Theory]
[ConditionalTheory(nameof(PredefinedCulturesOnlyIsDisabled))]
[MemberData(nameof(LastIndexOf_TestData))]
public void TestLastIndexOf(string source, string value, int startIndex, int count, CompareOptions options, int result)
{
Expand Down Expand Up @@ -901,7 +919,7 @@ static void TestCore(CompareInfo compareInfo, string source, string value, int s
}
}

[Theory]
[ConditionalTheory(nameof(PredefinedCulturesOnlyIsDisabled))]
[MemberData(nameof(IsPrefix_TestData))]
public void TestIsPrefix(string source, string value, CompareOptions options, bool result)
{
Expand Down Expand Up @@ -936,7 +954,7 @@ public void TestIsPrefix(string source, string value, CompareOptions options, bo
}
}

[Theory]
[ConditionalTheory(nameof(PredefinedCulturesOnlyIsDisabled))]
[MemberData(nameof(IsSuffix_TestData))]
public void TestIsSuffix(string source, string value, CompareOptions options, bool result)
{
Expand Down Expand Up @@ -971,7 +989,7 @@ public void TestIsSuffix(string source, string value, CompareOptions options, bo
}
}

[Theory]
[ConditionalTheory(nameof(PredefinedCulturesOnlyIsDisabled))]
[InlineData("", false)]
[InlineData('x', true)]
[InlineData('\ud800', true)] // standalone high surrogate
Expand All @@ -988,7 +1006,7 @@ public void TestIsSortable(object sourceObj, bool expectedResult)
}
}

[Theory]
[ConditionalTheory(nameof(PredefinedCulturesOnlyIsDisabled))]
[MemberData(nameof(Compare_TestData))]
public void TestCompare(string source, string value, CompareOptions options, int result)
{
Expand Down Expand Up @@ -1019,7 +1037,7 @@ public void TestCompare(string source, string value, CompareOptions options, int
}


[Theory]
[ConditionalTheory(nameof(PredefinedCulturesOnlyIsDisabled))]
[MemberData(nameof(ToLower_TestData))]
public void TestToLower(string upper, string lower, bool result)
{
Expand All @@ -1030,7 +1048,7 @@ public void TestToLower(string upper, string lower, bool result)
}
}

[Theory]
[ConditionalTheory(nameof(PredefinedCulturesOnlyIsDisabled))]
[MemberData(nameof(ToUpper_TestData))]
public void TestToUpper(string lower, string upper, bool result)
{
Expand All @@ -1041,7 +1059,7 @@ public void TestToUpper(string lower, string upper, bool result)
}
}

[Theory]
[ConditionalTheory(nameof(PredefinedCulturesOnlyIsDisabled))]
[InlineData("", NormalizationForm.FormC)]
[InlineData("\uFB01", NormalizationForm.FormC)]
[InlineData("\uFB01", NormalizationForm.FormD)]
Expand All @@ -1063,7 +1081,7 @@ public void TestNormalization(string s, NormalizationForm form)
Assert.Equal(s, s.Normalize(form));
}

[Theory]
[ConditionalTheory(nameof(PredefinedCulturesOnlyIsDisabled))]
[MemberData(nameof(GetAscii_TestData))]
public void GetAscii(string unicode, int index, int count, string expected)
{
Expand All @@ -1078,7 +1096,7 @@ public void GetAscii(string unicode, int index, int count, string expected)
Assert.Equal(expected, new IdnMapping().GetAscii(unicode, index, count));
}

[Theory]
[ConditionalTheory(nameof(PredefinedCulturesOnlyIsDisabled))]
[MemberData(nameof(GetUnicode_TestData))]
public void GetUnicode(string ascii, int index, int count, string expected)
{
Expand All @@ -1093,7 +1111,7 @@ public void GetUnicode(string ascii, int index, int count, string expected)
Assert.Equal(expected, new IdnMapping().GetUnicode(ascii, index, count));
}

[Fact]
[ConditionalFact(nameof(PredefinedCulturesOnlyIsDisabled))]
public void TestHashing()
{
StringComparer cultureComparer = StringComparer.Create(CultureInfo.GetCultureInfo("tr-TR"), true);
Expand All @@ -1102,7 +1120,7 @@ public void TestHashing()
Assert.Equal(ordinalComparer.GetHashCode(turkishString), cultureComparer.GetHashCode(turkishString));
}

[Theory]
[ConditionalTheory(nameof(PredefinedCulturesOnlyIsDisabled))]
[InlineData('a', 'A', 'a')]
[InlineData('A', 'A', 'a')]
[InlineData('i', 'I', 'i')] // to verify that we don't special-case the Turkish I in the invariant globalization mode
Expand All @@ -1121,7 +1139,7 @@ public void TestRune(int original, int expectedToUpper, int expectedToLower)
Assert.Equal(expectedToLower, Rune.ToLower(originalRune, CultureInfo.GetCultureInfo("tr-TR")).Value);
}

[Fact]
[ConditionalFact(nameof(PredefinedCulturesOnlyIsDisabled))]
public void TestGetCultureInfo_PredefinedOnly_ReturnsSame()
{
Assert.Equal(CultureInfo.GetCultureInfo("en-US"), CultureInfo.GetCultureInfo("en-US", predefinedOnly: true));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -846,15 +846,15 @@ private static unsafe string GetFileMuiPath(string filePath, CultureInfo culture
/// If a localized resource file exists, we LoadString resource ID "123" and
/// return it to our caller.
/// </summary>
private static string GetLocalizedNameByMuiNativeResource(string resource, CultureInfo? cultureInfo = null)
private static string GetLocalizedNameByMuiNativeResource(string resource)
{
if (string.IsNullOrEmpty(resource))
if (string.IsNullOrEmpty(resource) || (GlobalizationMode.Invariant && GlobalizationMode.PredefinedCulturesOnly))
{
return string.Empty;
}

// Use the current UI culture when culture not specified
cultureInfo ??= CultureInfo.CurrentUICulture;
CultureInfo cultureInfo = CultureInfo.CurrentUICulture;

// parse "@tzres.dll, -100"
//
Expand Down