79 lines
2.7 KiB
C#
79 lines
2.7 KiB
C#
using System.Collections;
|
|
using System.Globalization;
|
|
using System.IO;
|
|
using System.Reflection;
|
|
using System.Resources;
|
|
using System.Text.RegularExpressions;
|
|
using CursorLang.Core.Services;
|
|
|
|
namespace CursorLang.Settings.Tests.Views;
|
|
|
|
/// <summary>
|
|
/// The settings window markup against the resource strings.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// The resources live in Core and the markup lives here, so the check that the two
|
|
/// agree has to live here too. What the resources say among themselves — that every
|
|
/// key is translated, that the placeholders match — is checked by StringsTests, next
|
|
/// to the resources.
|
|
///
|
|
/// The markup is read as an embedded copy rather than off disk: a test that walks up
|
|
/// the folder tree looking for a .xaml file breaks the moment anything moves.
|
|
/// </remarks>
|
|
public sealed partial class MarkupStringsTests
|
|
{
|
|
private static readonly ResourceManager Resources =
|
|
new("CursorLang.Core.Resources.Strings", typeof(LocalizationService).Assembly);
|
|
|
|
/// <summary>
|
|
/// Every key the settings window markup asks for has to exist in the
|
|
/// resources: otherwise the user sees the key itself in its place.
|
|
/// </summary>
|
|
[Fact]
|
|
public void Every_key_from_the_settings_window_markup_exists_in_the_resources()
|
|
{
|
|
HashSet<string> known = [.. NeutralKeys()];
|
|
List<string> missing = [];
|
|
|
|
foreach (Match match in LocalizationBinding().Matches(ReadSettingsWindowMarkup()))
|
|
{
|
|
string key = match.Groups["key"].Value;
|
|
if (!known.Contains(key))
|
|
{
|
|
missing.Add(key);
|
|
}
|
|
}
|
|
|
|
Assert.Empty(missing);
|
|
}
|
|
|
|
/// <summary>The markup does ask for strings — otherwise the check above means nothing.</summary>
|
|
[Fact]
|
|
public void The_settings_window_markup_asks_for_resource_strings()
|
|
{
|
|
Assert.NotEmpty(LocalizationBinding().Matches(ReadSettingsWindowMarkup()));
|
|
}
|
|
|
|
private static IEnumerable<string> NeutralKeys()
|
|
{
|
|
ResourceSet set = Resources.GetResourceSet(CultureInfo.InvariantCulture, true, true)!;
|
|
|
|
foreach (DictionaryEntry entry in set)
|
|
{
|
|
yield return (string)entry.Key;
|
|
}
|
|
}
|
|
|
|
private static string ReadSettingsWindowMarkup()
|
|
{
|
|
using Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("MainWindow.xaml")
|
|
?? throw new InvalidOperationException("The settings window markup is not embedded in the test assembly");
|
|
|
|
using var reader = new StreamReader(stream);
|
|
return reader.ReadToEnd();
|
|
}
|
|
|
|
[GeneratedRegex(@"Localization\[(?<key>\w+)\]")]
|
|
private static partial Regex LocalizationBinding();
|
|
}
|