From bef602a9c4c4cf3c482b51c7772837a92989d1f3 Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 12 Oct 2026 10:34:33 +0000 Subject: [PATCH] test: the language guard switches back to English before failing the test 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 Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC --- tests/Titano.App.Tests/Program.cs | 9 ++- .../Localization/LanguageGuardTests.cs | 62 +++++++++++++++++++ tests/Titano.Core.Tests/Program.cs | 9 ++- 3 files changed, 76 insertions(+), 4 deletions(-) create mode 100644 tests/Titano.Core.Tests/Localization/LanguageGuardTests.cs diff --git a/tests/Titano.App.Tests/Program.cs b/tests/Titano.App.Tests/Program.cs index f414976b..d6dc628f 100644 --- a/tests/Titano.App.Tests/Program.cs +++ b/tests/Titano.App.Tests/Program.cs @@ -1,3 +1,4 @@ +using System.Globalization; using Titano.Core.Localization; using Titano.Testing; @@ -9,10 +10,14 @@ internal static class TestMain { // The UI language is process-wide: a test that leaves English outside a serial class races with the English // assertions of the classes running beside it, so it fails at the switch instead of making another one flaky. + // English comes back before the failure, so the classes beside it never see the other language (switching back + // passes this guard). Loc.CultureChanged += (_, _) => { - if (TestContext.Current is { IsSerial: false } test && Loc.Culture.TwoLetterISOLanguageName != "en") - throw new AssertionException($"{test.TestName} switched the UI language to '{Loc.Culture.Name}' outside a [Serial] class"); + if (TestContext.Current is not { IsSerial: false } test || Loc.Culture.TwoLetterISOLanguageName == "en") return; + var culture = Loc.Culture.Name; + Loc.SetCulture(CultureInfo.GetCultureInfo("en")); + throw new AssertionException($"{test.TestName} switched the UI language to '{culture}' outside a [Serial] class"); }; return TestRunner.RunAsync(args, typeof(TestMain).Assembly); } diff --git a/tests/Titano.Core.Tests/Localization/LanguageGuardTests.cs b/tests/Titano.Core.Tests/Localization/LanguageGuardTests.cs new file mode 100644 index 00000000..6d84d20f --- /dev/null +++ b/tests/Titano.Core.Tests/Localization/LanguageGuardTests.cs @@ -0,0 +1,62 @@ +using System.Globalization; +using Titano.Core.Localization; +using Titano.Testing; + +namespace Titano.Core.Tests.Localization; + +/// +/// The guard 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. +/// +[Serial] +public sealed class LanguageGuardTests +{ + /// A fixture (assembly discovery leaves nested classes out) that runs beside others and switches to Hebrew. + 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(); + 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")); + } + } +} diff --git a/tests/Titano.Core.Tests/Program.cs b/tests/Titano.Core.Tests/Program.cs index a6af6d91..cd6c1560 100644 --- a/tests/Titano.Core.Tests/Program.cs +++ b/tests/Titano.Core.Tests/Program.cs @@ -1,3 +1,4 @@ +using System.Globalization; using Titano.Core.Localization; using Titano.Testing; @@ -9,10 +10,14 @@ internal static class TestMain { // The UI language is process-wide: a test that leaves English outside a serial class races with the English // assertions of the classes running beside it, so it fails at the switch instead of making another one flaky. + // English comes back before the failure, so the classes beside it never see the other language (switching back + // passes this guard). Loc.CultureChanged += (_, _) => { - if (TestContext.Current is { IsSerial: false } test && Loc.Culture.TwoLetterISOLanguageName != "en") - throw new AssertionException($"{test.TestName} switched the UI language to '{Loc.Culture.Name}' outside a [Serial] class"); + if (TestContext.Current is not { IsSerial: false } test || Loc.Culture.TwoLetterISOLanguageName == "en") return; + var culture = Loc.Culture.Name; + Loc.SetCulture(CultureInfo.GetCultureInfo("en")); + throw new AssertionException($"{test.TestName} switched the UI language to '{culture}' outside a [Serial] class"); }; return TestRunner.RunAsync(args, typeof(TestMain).Assembly); }