Loc.SetCulture stores the new culture before it raises CultureChanged, so the guard's exception left the process in the other language until the offending test's own cleanup ran, or for good, and the parallel classes asserting English failed beside it. Both test programs now restore English first (the switch back passes the guard) and then fail the test, naming it and the language it chose. LanguageGuardTests runs a non-serial fixture through the runner and checks the failure message, that English is back and that later listeners only hear of English. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
63 lines
2.1 KiB
C#
63 lines
2.1 KiB
C#
using System.Globalization;
|
|
using Titano.Core.Localization;
|
|
using Titano.Testing;
|
|
|
|
namespace Titano.Core.Tests.Localization;
|
|
|
|
/// <summary>
|
|
/// The guard <see cref="TestMain"/> installs: a test outside a [Serial] class that leaves English fails, and English is
|
|
/// back before anything else runs. Serial itself: the fixture it runs switches the language for a moment.
|
|
/// </summary>
|
|
[Serial]
|
|
public sealed class LanguageGuardTests
|
|
{
|
|
/// <summary>A fixture (assembly discovery leaves nested classes out) that runs beside others and switches to Hebrew.</summary>
|
|
public sealed class SwitchesBesideOthers
|
|
{
|
|
[Test]
|
|
public void Body()
|
|
{
|
|
Loc.SetCulture(CultureInfo.GetCultureInfo("he"));
|
|
Assert.Fail("the switch went through");
|
|
}
|
|
}
|
|
|
|
[Test]
|
|
public async Task ATestBesideOthersThatLeavesEnglishFailsAndEnglishIsBackAtOnce()
|
|
{
|
|
Loc.SetCulture(CultureInfo.GetCultureInfo("en"));
|
|
var cultures = new List<string>();
|
|
void Record(object? sender, EventArgs e) => cultures.Add(Loc.Culture.Name);
|
|
Loc.CultureChanged += Record;
|
|
try
|
|
{
|
|
var result = await TestRunner.ExecuteAsync(TestRunner.Discover(typeof(SwitchesBesideOthers)).Single());
|
|
|
|
Assert.Equal(TestOutcome.Failed, result.Outcome);
|
|
Assert.Equal(typeof(SwitchesBesideOthers).FullName + ".Body switched the UI language to 'he' outside a [Serial] class", result.Message);
|
|
Assert.Equal("en", Loc.Culture.Name);
|
|
// The guard runs first and switches back inside the change, so later listeners only ever hear of English.
|
|
Assert.SequenceEqual(new[] { "en" }, cultures);
|
|
}
|
|
finally
|
|
{
|
|
Loc.CultureChanged -= Record;
|
|
Loc.SetCulture(CultureInfo.GetCultureInfo("en"));
|
|
}
|
|
}
|
|
|
|
[Test]
|
|
public void ASerialTestMaySwitchTheLanguage()
|
|
{
|
|
try
|
|
{
|
|
Loc.SetCulture(CultureInfo.GetCultureInfo("he"));
|
|
Assert.Equal("he", Loc.Culture.Name);
|
|
}
|
|
finally
|
|
{
|
|
Loc.SetCulture(CultureInfo.GetCultureInfo("en"));
|
|
}
|
|
}
|
|
}
|