Files
cursor-lang/CursorLang.Tests/Models/KeyboardLayoutTests.cs
T
2026-08-09 18:31:43 +05:00

77 lines
2.3 KiB
C#

using System.Globalization;
using CursorLang.Models;
namespace CursorLang.Tests.Models;
public sealed class KeyboardLayoutTests
{
[Theory]
[InlineData(0x0409, "EN")]
[InlineData(0x0419, "RU")]
[InlineData(0x040C, "FR")]
[InlineData(0x0407, "DE")]
public void The_short_name_comes_from_the_language_code(int localeId, string expected)
{
KeyboardLayout layout = KeyboardLayout.FromLocaleId(localeId);
Assert.Equal(expected, layout.ShortName);
Assert.Equal(localeId, layout.LocaleId);
}
[Fact]
public void The_full_name_joins_the_short_name_and_the_native_language_name()
{
KeyboardLayout layout = KeyboardLayout.FromLocaleId(0x0419);
Assert.Equal($"RU — {new CultureInfo(0x0419).NativeName}", layout.DisplayName);
}
// A layout may belong to a language the system does not know — that is not an error
[Fact]
public void An_unknown_locale_is_shown_by_its_own_code()
{
int unknown = FindUnknownLocaleId();
KeyboardLayout layout = KeyboardLayout.FromLocaleId(unknown);
string expected = $"0x{unknown:X4}";
Assert.Equal(expected, layout.ShortName);
Assert.Equal(expected, layout.DisplayName);
}
[Fact]
public void Layouts_of_the_same_locale_are_equal()
{
Assert.Equal(KeyboardLayout.FromLocaleId(0x0409), KeyboardLayout.FromLocaleId(0x0409));
Assert.NotEqual(KeyboardLayout.FromLocaleId(0x0409), KeyboardLayout.FromLocaleId(0x0419));
}
[Fact]
public void A_layout_can_also_be_built_directly()
{
var layout = new KeyboardLayout(1, "XX", "XX — language");
Assert.Equal(1, layout.LocaleId);
Assert.Equal("XX", layout.ShortName);
Assert.Equal("XX — language", layout.DisplayName);
}
// An identifier with no culture behind it in Windows
private static int FindUnknownLocaleId()
{
for (int candidate = 0x1000; candidate <= 0xFFFF; candidate++)
{
try
{
_ = new CultureInfo(candidate);
}
catch (CultureNotFoundException)
{
return candidate;
}
}
throw new InvalidOperationException("The system knows every locale identifier");
}
}