-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
GetCultureInfo.cs
235 lines (210 loc) · 10.9 KB
/
GetCultureInfo.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Collections.Generic;
using Microsoft.DotNet.RemoteExecutor;
using System.Diagnostics;
using System.Collections.Concurrent;
using Xunit;
namespace System.Globalization.Tests
{
public class GetCultureInfoTests
{
public static bool PlatformSupportsFakeCulture => (!PlatformDetection.IsWindows || (PlatformDetection.WindowsVersion >= 10 && !PlatformDetection.IsNetFramework)) && PlatformDetection.IsNotBrowser;
public static bool PlatformSupportsFakeCultureAndRemoteExecutor => PlatformSupportsFakeCulture && RemoteExecutor.IsSupported;
public static IEnumerable<object[]> GetCultureInfoTestData()
{
yield return new object[] { "en" };
yield return new object[] { "en-US" };
yield return new object[] { "ja-JP" };
yield return new object[] { "ar-SA" };
yield return new object[] { "xx-XX" };
yield return new object[] { "de-AT-1901" };
yield return new object[] { "zh-Hans" };
yield return new object[] { "zh-Hans-HK" };
yield return new object[] { "zh-Hans-MO" };
yield return new object[] { "zh-Hans-TW" };
yield return new object[] { "zh-Hant" };
yield return new object[] { "zh-Hant-CN" };
yield return new object[] { "zh-Hant-SG" };
if (PlatformDetection.IsIcuGlobalization)
{
if (PlatformDetection.IsNotWindows)
{
yield return new object[] { "x\u0000X-Yy", "x" }; // Null byte
yield return new object[] { "zh-cmn", "zh-CMN" };
yield return new object[] { "zh-CMN-HANS" };
yield return new object[] { "zh-cmn-Hant", "zh-CMN-HANT" };
}
yield return new object[] { "sgn-BE-FR" };
yield return new object[] { "zh-min-nan", "nan" };
yield return new object[] { "zh-gan", "gan" };
yield return new object[] { "zh-Hans-CN" };
yield return new object[] { "zh-Hans-SG" };
yield return new object[] { "zh-Hant-HK" };
yield return new object[] { "zh-Hant-MO" };
yield return new object[] { "zh-Hant-TW" };
yield return new object[] { "zh-yue", "yue" };
yield return new object[] { "zh-wuu", "wuu" };
}
else
{
yield return new object[] { "sgn-BE-FR", "sgn-BE-fr" };
yield return new object[] { "zh-Hans-CN", "zh-CN" };
yield return new object[] { "zh-Hans-SG", "zh-SG" };
yield return new object[] { "zh-Hant-HK", "zh-HK" };
yield return new object[] { "zh-Hant-MO", "zh-MO" };
yield return new object[] { "zh-Hant-TW", "zh-TW" };
}
}
[Theory]
[PlatformSpecific(TestPlatforms.Windows)] // Windows specific behavior
[InlineData("x\u0000X-Yy")]
[InlineData("zh-cmn")]
[InlineData("zh-CMN-HANS")]
[InlineData("zh-cmn-Hant")]
public void TestIvalidCultureNames(string cultureName)
{
Assert.Throws<CultureNotFoundException>(() => new CultureInfo(cultureName));
}
[ConditionalTheory(nameof(PlatformSupportsFakeCulture))]
[MemberData(nameof(GetCultureInfoTestData))]
public void GetCultureInfo(string name, string expected = null)
{
if (expected == null) expected = name;
Assert.Equal(expected, CultureInfo.GetCultureInfo(name).Name);
Assert.Equal(expected, CultureInfo.GetCultureInfo(name, predefinedOnly: false).Name);
}
[ConditionalTheory(nameof(PlatformSupportsFakeCulture))]
[InlineData("z")]
[InlineData("en@US")]
[InlineData("\uFFFF")]
[InlineData("\u0080")]
[InlineData("-foo")]
[InlineData("foo-")]
[InlineData("/foo")]
[InlineData("_bar")]
[InlineData("bar_")]
[InlineData("bar/")]
[InlineData("foo__bar")]
[InlineData("foo--bar")]
[InlineData("foo-_bar")]
[InlineData("foo_-bar")]
[InlineData("foo/bar")]
[InlineData("/")]
[InlineData("0123456789012345678901234567890123456789012345678901234567890123456789012345678901234")] // > 85 characters
public void TestInvalidCultureNames(string name)
{
Assert.Throws<CultureNotFoundException>(() => CultureInfo.GetCultureInfo(name));
Assert.Throws<CultureNotFoundException>(() => CultureInfo.GetCultureInfo(name, predefinedOnly: false));
Assert.Throws<CultureNotFoundException>(() => CultureInfo.GetCultureInfo(name, predefinedOnly: true));
}
[ConditionalTheory(nameof(PlatformSupportsFakeCulture))]
[InlineData("en")]
[InlineData("en-US")]
[InlineData("ja-JP")]
[InlineData("ar-SA")]
public void TestGetCultureInfoWithNoneConstructedCultures(string name)
{
Assert.Equal(name, CultureInfo.GetCultureInfo(name).Name);
Assert.Equal(name, CultureInfo.GetCultureInfo(name, predefinedOnly: false).Name);
Assert.Equal(name, CultureInfo.GetCultureInfo(name, predefinedOnly: true).Name);
}
[ConditionalTheory(nameof(PlatformSupportsFakeCulture))]
[InlineData("xx")]
[InlineData("xx-XX")]
[InlineData("xx-YY")]
public void TestFakeCultureNames(string name)
{
Assert.Equal(name, CultureInfo.GetCultureInfo(name).Name);
Assert.Equal(name, CultureInfo.GetCultureInfo(name, predefinedOnly: false).Name);
Assert.Throws<CultureNotFoundException>(() => CultureInfo.GetCultureInfo(name, predefinedOnly: true));
}
[ConditionalTheory(nameof(PlatformSupportsFakeCultureAndRemoteExecutor))]
[SkipOnPlatform(TestPlatforms.LinuxBionic, "Remote executor has problems with exit codes")]
[InlineData("1", "xx-XY")]
[InlineData("1", "zx-ZY")]
[InlineData("0", "xx-XY")]
[InlineData("0", "zx-ZY")]
public void PredefinedCulturesOnlyEnvVarTest(string predefinedCulturesOnlyEnvVar, string cultureName)
{
var psi = new ProcessStartInfo();
psi.Environment.Clear();
psi.Environment.Add("DOTNET_SYSTEM_GLOBALIZATION_PREDEFINED_CULTURES_ONLY", predefinedCulturesOnlyEnvVar);
RemoteExecutor.Invoke((culture, predefined) =>
{
if (predefined == "1")
{
AssertExtensions.Throws<CultureNotFoundException>(() => new CultureInfo(culture));
}
else
{
CultureInfo ci = new CultureInfo(culture);
Assert.Equal(culture, ci.Name);
}
}, cultureName, predefinedCulturesOnlyEnvVar, new RemoteInvokeOptions { StartInfo = psi }).Dispose();
}
[ConditionalTheory(typeof(RemoteExecutor), nameof(RemoteExecutor.IsSupported))]
[SkipOnPlatform(TestPlatforms.LinuxBionic, "Remote executor has problems with exit codes")]
[InlineData(true, true, false)]
[InlineData(true, true, true)]
[InlineData(true, false, true)]
[InlineData(false, true, true)]
[InlineData(false, true, false)]
[InlineData(false, false, true)]
public void TestAllowInvariantCultureOnly(bool enableInvariant, bool predefinedCulturesOnly, bool declarePredefinedCulturesOnly)
{
var psi = new ProcessStartInfo();
psi.Environment.Clear();
if (enableInvariant)
{
psi.Environment.Add("DOTNET_SYSTEM_GLOBALIZATION_INVARIANT", "true");
}
if (declarePredefinedCulturesOnly)
{
psi.Environment.Add("DOTNET_SYSTEM_GLOBALIZATION_PREDEFINED_CULTURES_ONLY", predefinedCulturesOnly ? "true" : "false");
}
bool restricted = enableInvariant && (declarePredefinedCulturesOnly ? predefinedCulturesOnly : true);
RemoteExecutor.Invoke((invariantEnabled, isRestricted) =>
{
bool restrictedMode = bool.Parse(isRestricted);
// First ensure we can create the current cultures regardless of the mode we are in
Assert.NotNull(CultureInfo.CurrentCulture);
Assert.NotNull(CultureInfo.CurrentUICulture);
// Invariant culture should be valid all the time
Assert.Equal("", new CultureInfo("").Name);
Assert.Equal("", CultureInfo.InvariantCulture.Name);
if (restrictedMode)
{
Assert.Equal("", CultureInfo.CurrentCulture.Name);
Assert.Equal("", CultureInfo.CurrentUICulture.Name);
// Throwing exception is testing accessing the resources in this restricted mode.
// We should retrieve the resources from the neutral resources in the main assemblies.
AssertExtensions.Throws<CultureNotFoundException>(() => new CultureInfo("en-US"));
AssertExtensions.Throws<CultureNotFoundException>(() => new CultureInfo("en"));
AssertExtensions.Throws<CultureNotFoundException>(() => new CultureInfo("ja-JP"));
AssertExtensions.Throws<CultureNotFoundException>(() => new CultureInfo("es"));
// Test throwing exceptions from non-core assemblies.
Exception exception = Record.Exception(() => new ConcurrentBag<string>(null));
Assert.NotNull(exception);
Assert.IsType<ArgumentNullException>(exception);
Assert.Equal("collection", (exception as ArgumentNullException).ParamName);
Assert.Equal("Value cannot be null. (Parameter 'collection')", exception.Message);
}
else
{
Assert.Equal("en-US", new CultureInfo("en-US").Name);
Assert.Equal("ja-JP", new CultureInfo("ja-JP").Name);
Assert.Equal("en", new CultureInfo("en").Name);
Assert.Equal("es", new CultureInfo("es").Name);
}
// Ensure the Invariant Mode functionality still work
if (bool.Parse(invariantEnabled))
{
Assert.True(CultureInfo.CurrentCulture.Calendar is GregorianCalendar);
Assert.True("abcd".Equals("ABCD", StringComparison.CurrentCultureIgnoreCase));
Assert.Equal("Invariant Language (Invariant Country)", CultureInfo.CurrentCulture.NativeName);
}
}, enableInvariant.ToString(), restricted.ToString(), new RemoteInvokeOptions { StartInfo = psi }).Dispose();
}
}
}