From 8a8918403f9545046ee11c90bc9563d7f9b3bb73 Mon Sep 17 00:00:00 2001 From: Julian Lechner Date: Thu, 26 Nov 2026 09:20:52 +0100 Subject: [PATCH] test(i18n): check both string tables and the keys used in code Co-Authored-By: Claude Opus 5.5 --- tests/Radiosender.Tests/LocalizationTests.cs | 78 ++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 tests/Radiosender.Tests/LocalizationTests.cs diff --git a/tests/Radiosender.Tests/LocalizationTests.cs b/tests/Radiosender.Tests/LocalizationTests.cs new file mode 100644 index 0000000..6d50530 --- /dev/null +++ b/tests/Radiosender.Tests/LocalizationTests.cs @@ -0,0 +1,78 @@ +using System.Globalization; +using System.IO; +using System.Resources; +using System.Runtime.CompilerServices; +using System.Text.RegularExpressions; +using System.Xml.Linq; +using Radiosender.Models; +using Radiosender.Services.Localization; + +namespace Radiosender.Tests; + +// Static checks only: switching Loc here would race with tests that assert German messages. +public sealed partial class LocalizationTests +{ + private static readonly string AppFolder = FindAppFolder(); + private static readonly string StringsFolder = Path.Combine(AppFolder, "Services", "Localization"); + + // From this source file, not the test output: tests may build to another folder. + private static string FindAppFolder([CallerFilePath] string source = "") + { + for (var dir = new DirectoryInfo(Path.GetDirectoryName(source)!); dir is not null; dir = dir.Parent) + { + var app = Path.Combine(dir.FullName, "src", "Radiosender"); + if (Directory.Exists(app)) + return app; + } + throw new DirectoryNotFoundException("src/Radiosender not found above the test sources."); + } + + private static Dictionary Read(string file) => + XDocument.Load(Path.Combine(StringsFolder, file)).Root!.Elements("data") + .ToDictionary(d => (string)d.Attribute("name")!, d => (string?)d.Element("value") ?? ""); + + [Fact] + public void GermanAndEnglish_HaveTheSameKeysAndPlaceholders() + { + var german = Read("Strings.resx"); + var english = Read("Strings.en.resx"); + + Assert.Equal(german.Keys.Order(), english.Keys.Order()); + Assert.All(german, entry => + { + Assert.False(string.IsNullOrWhiteSpace(entry.Value), entry.Key); + Assert.False(string.IsNullOrWhiteSpace(english[entry.Key]), entry.Key); + Assert.Equal(Placeholders(entry.Value), Placeholders(english[entry.Key])); + }); + } + + [Fact] + public void EveryKeyUsedInCode_Exists() + { + var keys = Read("Strings.resx").Keys.ToHashSet(); + var used = Directory.EnumerateFiles(AppFolder, "*.*", SearchOption.AllDirectories) + .Where(f => (f.EndsWith(".cs") || f.EndsWith(".xaml")) && !f.Contains($"{Path.DirectorySeparatorChar}obj{Path.DirectorySeparatorChar}")) + .SelectMany(f => UsedKey().Matches(File.ReadAllText(f)).Select(m => (File: Path.GetFileName(f), Key: m.Groups["key"].Value))); + var kinds = Enum.GetNames().Select(k => (File: "ScheduleItem.cs", Key: $"Kind_{k}")); + + // "Key" is the placeholder in doc comments of Loc and TrExtension. + var missing = used.Concat(kinds).Where(u => u.Key != "Key" && !keys.Contains(u.Key)).ToList(); + + Assert.True(missing.Count == 0, $"Missing keys: {string.Join(", ", missing)}"); + } + + [Fact] + public void EnglishSatellite_IsFound() + { + var texts = new ResourceManager($"{typeof(Loc).Namespace}.Strings", typeof(Loc).Assembly); + + Assert.Equal("Language", texts.GetString("Settings_Language", CultureInfo.GetCultureInfo("en-US"))); + Assert.Equal("Sprache", texts.GetString("Settings_Language", CultureInfo.GetCultureInfo("de-DE"))); + } + + private static string[] Placeholders(string text) => + [.. Regex.Matches(text, @"\{\d+[^}]*\}").Select(m => m.Value).Distinct().Order()]; + + [GeneratedRegex("""(?:Loc\.[TF]\(\s*"|\{l:Tr\s+)(?[A-Za-z0-9_]+)""")] + private static partial Regex UsedKey(); +}