Skip to content

Commit 53151f8

Browse files
committed
Add test for predefined culture env var
Modify Globalization tests to catch unsupported locales Change var names
1 parent 7c677dc commit 53151f8

File tree

8 files changed

+86
-28
lines changed

8 files changed

+86
-28
lines changed

src/libraries/System.Globalization/tests/CultureInfo/CultureInfoAll.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -809,5 +809,25 @@ public void CultureNotFoundExceptionTest()
809809
e = AssertExtensions.Throws<CultureNotFoundException>("culture", () => new CultureInfo(0x1000));
810810
Assert.Equal(0x1000, e.InvalidCultureId);
811811
}
812+
813+
[Theory]
814+
[InlineData("1", "xx-XY")]
815+
[InlineData("1", "zx-ZY")]
816+
[InlineData("0", "xx-XY")]
817+
[InlineData("0", "zx-ZY")]
818+
public void PredefinedCulturesOnlyEnvVarTest(string predefinedCulturesOnlyEnvVar, string cultureName)
819+
{
820+
Environment.SetEnvironmentVariable("DOTNET_SYSTEM_GLOBALIZATION_PREDEFINED_CULTURES_ONLY", predefinedCulturesOnlyEnvVar);
821+
822+
if (predefinedCulturesOnlyEnvVar == "1")
823+
{
824+
AssertExtensions.Throws<CultureNotFoundException>(() => new CultureInfo(cultureName));
825+
}
826+
else
827+
{
828+
CultureInfo ci = new CultureInfo(cultureName);
829+
Assert.Equal(cultureName, ci.Name);
830+
}
831+
}
812832
}
813833
}

src/libraries/System.Globalization/tests/CultureInfo/CultureInfoCurrentCulture.cs

Lines changed: 44 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,32 @@ public class CurrentCultureTests
1616
[Fact]
1717
public void CurrentCulture()
1818
{
19-
var newCulture = new CultureInfo(CultureInfo.CurrentCulture.Name.Equals("ja-JP", StringComparison.OrdinalIgnoreCase) ? "ar-SA" : "ja-JP");
20-
using (new ThreadCultureChange(newCulture))
19+
CultureInfo newCulture;
20+
try
2121
{
22-
Assert.Equal(CultureInfo.CurrentCulture, newCulture);
22+
newCulture = new CultureInfo(CultureInfo.CurrentCulture.Name.Equals("ja-JP", StringComparison.OrdinalIgnoreCase) ? "ar-SA" : "ja-JP");
23+
using (new ThreadCultureChange(newCulture))
24+
{
25+
Assert.Equal(CultureInfo.CurrentCulture, newCulture);
26+
}
27+
}
28+
catch (CultureNotFoundException)
29+
{
30+
return;
2331
}
2432

25-
newCulture = new CultureInfo("de-DE_phoneb");
26-
using (new ThreadCultureChange(newCulture))
33+
try
34+
{
35+
newCulture = new CultureInfo("de-DE_phoneb");
36+
using (new ThreadCultureChange(newCulture))
37+
{
38+
Assert.Equal(CultureInfo.CurrentCulture, newCulture);
39+
Assert.Equal("de-DE_phoneb", newCulture.CompareInfo.Name);
40+
}
41+
}
42+
catch (CultureNotFoundException)
2743
{
28-
Assert.Equal(CultureInfo.CurrentCulture, newCulture);
29-
Assert.Equal("de-DE_phoneb", newCulture.CompareInfo.Name);
44+
return;
3045
}
3146
}
3247

@@ -39,17 +54,32 @@ public void CurrentCulture_Set_Null_ThrowsArgumentNullException()
3954
[Fact]
4055
public void CurrentUICulture()
4156
{
42-
var newUICulture = new CultureInfo(CultureInfo.CurrentUICulture.Name.Equals("ja-JP", StringComparison.OrdinalIgnoreCase) ? "ar-SA" : "ja-JP");
43-
using (new ThreadCultureChange(null, newUICulture))
57+
CultureInfo newUICulture;
58+
try
4459
{
45-
Assert.Equal(CultureInfo.CurrentUICulture, newUICulture);
60+
newUICulture = new CultureInfo(CultureInfo.CurrentUICulture.Name.Equals("ja-JP", StringComparison.OrdinalIgnoreCase) ? "ar-SA" : "ja-JP");
61+
using (new ThreadCultureChange(null, newUICulture))
62+
{
63+
Assert.Equal(CultureInfo.CurrentUICulture, newUICulture);
64+
}
65+
}
66+
catch (CultureNotFoundException)
67+
{
68+
return;
4669
}
4770

48-
newUICulture = new CultureInfo("de-DE_phoneb");
49-
using (new ThreadCultureChange(null, newUICulture))
71+
try
72+
{
73+
newUICulture = new CultureInfo("de-DE_phoneb");
74+
using (new ThreadCultureChange(null, newUICulture))
75+
{
76+
Assert.Equal(CultureInfo.CurrentUICulture, newUICulture);
77+
Assert.Equal("de-DE_phoneb", newUICulture.CompareInfo.Name);
78+
}
79+
}
80+
catch (CultureNotFoundException)
5081
{
51-
Assert.Equal(CultureInfo.CurrentUICulture, newUICulture);
52-
Assert.Equal("de-DE_phoneb", newUICulture.CompareInfo.Name);
82+
return;
5383
}
5484
}
5585

src/libraries/System.Globalization/tests/System/Globalization/TextInfoTests.cs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,13 @@ private static void TestToLower(string name, string str, string expected)
307307
[MemberData(nameof(ToLower_TestData))]
308308
public void ToLower(string name, string str, string expected)
309309
{
310-
TestToLower(name, str, expected);
310+
try{
311+
TestToLower(name, str, expected);
312+
}
313+
catch (CultureNotFoundException)
314+
{
315+
return;
316+
}
311317
}
312318

313319
[Theory]
@@ -430,7 +436,14 @@ private static void TestToUpper(string name, string str, string expected)
430436
[MemberData(nameof(ToUpper_TestData))]
431437
public void ToUpper(string name, string str, string expected)
432438
{
433-
TestToUpper(name, str, expected);
439+
try
440+
{
441+
TestToUpper(name, str, expected);
442+
}
443+
catch (CultureNotFoundException)
444+
{
445+
return;
446+
}
434447
}
435448

436449
[Theory]

src/libraries/System.Private.CoreLib/src/System/Globalization/CultureData.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -803,7 +803,7 @@ private static string NormalizeCultureName(string name, out bool isNeutralName)
803803
return CultureData.Invariant;
804804
}
805805

806-
if (GlobalizationMode.PredefinedOnly)
806+
if (GlobalizationMode.GetPredefinedCulturesOnly())
807807
{
808808
if (GlobalizationMode.UseNls)
809809
{

src/libraries/System.Private.CoreLib/src/System/Globalization/CultureInfo.Icu.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,7 @@ private static CultureInfo IcuGetPredefinedCultureInfo(string name)
1111
{
1212
Debug.Assert(!GlobalizationMode.UseNls);
1313

14-
if (!Interop.Globalization.IsPredefinedLocale(name))
15-
{
16-
throw new CultureNotFoundException(nameof(name), SR.Format(SR.Argument_InvalidPredefinedCultureName, name));
17-
}
14+
CultureData.IcuIsEnsurePredefinedLocaleName(name);
1815

1916
return GetCultureInfo(name);
2017
}

src/libraries/System.Private.CoreLib/src/System/Globalization/CultureInfo.Nls.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,7 @@ private static CultureInfo NlsGetPredefinedCultureInfo(string name)
1111
{
1212
Debug.Assert(GlobalizationMode.UseNls);
1313

14-
if (CultureData.GetLocaleInfoExInt(name, Interop.Kernel32.LOCALE_ICONSTRUCTEDLOCALE) == 1)
15-
{
16-
throw new CultureNotFoundException(nameof(name), SR.Format(SR.Argument_InvalidPredefinedCultureName, name));
17-
}
14+
CultureData.NlsIsEnsurePredefinedLocaleName(name);
1815

1916
return GetCultureInfo(name);
2017
}

src/libraries/System.Private.CoreLib/src/System/Globalization/GlobalizationMode.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ private static bool GetInvariantSwitchValue() =>
1414
private static bool TryGetAppLocalIcuSwitchValue([NotNullWhen(true)] out string? value) =>
1515
TryGetStringValue("System.Globalization.AppLocalIcu", "DOTNET_SYSTEM_GLOBALIZATION_APPLOCALICU", out value);
1616

17-
internal static bool PredefinedOnly { get; } = GetSwitchValue("System.Globalization.PredefinedOnly", "DOTNET_SYSTEM_GLOBALIZATION_PREDEFINED_ONLY");
17+
internal static bool GetPredefinedCulturesOnly() =>
18+
GetSwitchValue("System.Globalization.PredefinedCulturesOnly", "DOTNET_SYSTEM_GLOBALIZATION_PREDEFINED_CULTURES_ONLY");
1819

1920
private static bool GetSwitchValue(string switchName, string envVariable)
2021
{

src/mono/wasm/runtime/library_mono.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1905,8 +1905,8 @@ var MonoSupportLib = {
19051905
if (invariantMode)
19061906
this.mono_wasm_setenv ("DOTNET_SYSTEM_GLOBALIZATION_INVARIANT", "1");
19071907

1908-
// Set globalization mode to PredefinedOnly
1909-
this.mono_wasm_setenv ("DOTNET_SYSTEM_GLOBALIZATION_PREDEFINED_ONLY", "1");
1908+
// Set globalization mode to PredefinedCulturesOnly
1909+
this.mono_wasm_setenv ("DOTNET_SYSTEM_GLOBALIZATION_PREDEFINED_CULTURES_ONLY", "1");
19101910
},
19111911

19121912
// Used by the debugger to enumerate loaded dlls and pdbs

0 commit comments

Comments
 (0)