Skip to content

Commit efed7e4

Browse files
Test number and text formatting across eight cultures
A guard asserts the suite runs under the invariant culture, and a theory switches to en-US, fr-FR, de-DE, de-CH, nb-NO, ru-RU, tr-TR and ja-JP in turn, checking that sizes, percentages and grouped counts match the culture's own formatting and that strings come from its translation or the nearest fallback. Expected values are computed from the culture, not hard-coded.
1 parent 431dc3b commit efed7e4

1 file changed

Lines changed: 50 additions & 1 deletion

File tree

‎SpaceSharp.Tests/LocalizationTests.cs‎

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ public void FormatUsesTheCurrentCultureForNumbers()
146146
}
147147

148148
/// <summary>Sizes, percentages and resources follow the user's culture: a French Windows sees a decimal comma and French text.</summary>
149-
/// <remarks>The rest of the suite runs under the invariant culture (see <see cref="TestCulture"/>); this is the one test that asserts the other way.</remarks>
149+
/// <remarks>The rest of the suite runs under the invariant culture (see <see cref="TestCulture"/>); this and <see cref="FormattingFollowsTheCurrentCulture"/> assert the other way.</remarks>
150150
[Fact]
151151
public void FrenchCultureFormatsWithADecimalComma()
152152
{
@@ -161,4 +161,53 @@ public void FrenchCultureFormatsWithADecimalComma()
161161
}
162162
finally { (CultureInfo.CurrentCulture, CultureInfo.CurrentUICulture) = before; }
163163
}
164+
165+
/// <summary>The suite runs under the invariant culture whatever the machine is set to, so expected English text and decimal points hold everywhere.</summary>
166+
/// <remarks>Fails if <see cref="TestCulture"/> is removed or stops running before the tests.</remarks>
167+
[Fact]
168+
public void SuiteRunsUnderTheInvariantCulture()
169+
{
170+
Assert.Equal(CultureInfo.InvariantCulture, CultureInfo.CurrentCulture);
171+
Assert.Equal(CultureInfo.InvariantCulture, CultureInfo.CurrentUICulture);
172+
Assert.Equal("1.5 KB", SizeFormatter.Format(1536));
173+
Assert.Equal("Language", Strings.Get("Settings_Language"));
174+
}
175+
176+
/// <summary>Numbers follow the current culture's separators and text follows its translation, or English where there is none.</summary>
177+
/// <remarks>
178+
/// Covers a decimal comma (de-DE, fr-FR, ru-RU, tr-TR), a decimal point in a non-English culture (de-CH, ja-JP),
179+
/// a space as the thousands separator (nb-NO, fr-FR), a culture with a translation and one without (de-CH falls back to German, ja-JP has its own).
180+
/// </remarks>
181+
/// <param name="cultureName">The culture to run the assertions under.</param>
182+
[Theory]
183+
[InlineData("en-US")]
184+
[InlineData("fr-FR")]
185+
[InlineData("de-DE")]
186+
[InlineData("de-CH")]
187+
[InlineData("nb-NO")]
188+
[InlineData("ru-RU")]
189+
[InlineData("tr-TR")]
190+
[InlineData("ja-JP")]
191+
public void FormattingFollowsTheCurrentCulture(string cultureName)
192+
{
193+
CultureInfo culture = CultureInfo.GetCultureInfo(cultureName);
194+
(CultureInfo CurrentCulture, CultureInfo CurrentUICulture) before = (CultureInfo.CurrentCulture, CultureInfo.CurrentUICulture);
195+
try
196+
{
197+
CultureInfo.CurrentCulture = CultureInfo.CurrentUICulture = culture;
198+
199+
// The same numbers formatted with the culture by hand are what the app must produce.
200+
Assert.Equal(string.Format(culture, "{0:0.##} KB", 1.5), SizeFormatter.Format(1536));
201+
Assert.Equal(string.Format(culture, "{0:0.##} KB", 1100 / 1024.0), SizeFormatter.Compact(1100));
202+
Assert.Equal(string.Format(culture, "{0:0.#}%", 8.9), InspectSummary.Percent(0.089));
203+
Assert.Contains(1234.ToString("N0", culture), Strings.Format("Inspect_ItemsSelected", 1234));
204+
205+
// Text comes from the culture's resources, or the nearest parent, or English; never a bare key.
206+
string? expected = Strings.Manager.GetString("Settings_Language", culture);
207+
Assert.NotNull(expected);
208+
Assert.Equal(expected, Strings.Get("Settings_Language"));
209+
Assert.DoesNotContain("[", Strings.Get("Settings_Language"));
210+
}
211+
finally { (CultureInfo.CurrentCulture, CultureInfo.CurrentUICulture) = before; }
212+
}
164213
}

0 commit comments

Comments
 (0)