The first CI-based analysis of main imported 104 info-level Roslyn suggestions as issues (Sonar's own rules found nothing). Applied the fixes the IDE offers, one diagnostic at a time: - IDE0290: primary constructors (28 types). Fields stay readonly and are initialized from the parameters; <param> docs moved to the type. - MVVMTK0042: [ObservableProperty] on public partial properties instead of fields (70 properties); initializers still set the backing field silently. UpdateViewModel's constructor now sets Status through the property, before anything subscribes. - SYSLIB1054: CreateDirect3D11DeviceFromDXGIDevice uses [LibraryImport]. - IDE0007 / IDE0028: var, collection expression. - Tests: index the monitor list directly (CA1826); seal ResxLocalizationServiceTests like the other disposable test classes (CA1816). - Long primary-constructor parameter lists are wrapped one per line. - CLAUDE.md: note that SonarQube Cloud reports these suggestions. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
203 lines
6.9 KiB
C#
203 lines
6.9 KiB
C#
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
// Copyright (c) CaptureImage Contributors
|
|
|
|
using System.Globalization;
|
|
using AwesomeAssertions;
|
|
using CaptureImage.Core.Abstractions;
|
|
using CaptureImage.ViewModels.Localization;
|
|
using Microsoft.Extensions.Logging.Abstractions;
|
|
using Xunit;
|
|
|
|
namespace CaptureImage.ViewModels.Tests.Localization;
|
|
|
|
/// <summary>
|
|
/// Covers <see cref="ResxLocalizationService"/>. Snapshots thread culture in the ctor and
|
|
/// restores in <see cref="Dispose"/> so setting <c>CultureInfo.CurrentCulture</c> from one
|
|
/// test doesn't leak into the next one running on the same test runner thread.
|
|
/// </summary>
|
|
[Collection(LocalizationTestCollection.Name)]
|
|
public sealed class ResxLocalizationServiceTests : IDisposable
|
|
{
|
|
private readonly CultureInfo _savedUICulture;
|
|
private readonly CultureInfo _savedCulture;
|
|
private readonly CultureInfo? _savedDefaultUICulture;
|
|
private readonly CultureInfo? _savedDefaultCulture;
|
|
|
|
public ResxLocalizationServiceTests()
|
|
{
|
|
_savedUICulture = CultureInfo.CurrentUICulture;
|
|
_savedCulture = CultureInfo.CurrentCulture;
|
|
_savedDefaultUICulture = CultureInfo.DefaultThreadCurrentUICulture;
|
|
_savedDefaultCulture = CultureInfo.DefaultThreadCurrentCulture;
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
CultureInfo.CurrentUICulture = _savedUICulture;
|
|
CultureInfo.CurrentCulture = _savedCulture;
|
|
CultureInfo.DefaultThreadCurrentUICulture = _savedDefaultUICulture;
|
|
CultureInfo.DefaultThreadCurrentCulture = _savedDefaultCulture;
|
|
}
|
|
|
|
[Fact]
|
|
public void SetCulture_Vietnamese_UpdatesCurrentCultureAndThreadCulture()
|
|
{
|
|
var svc = new ResxLocalizationService(NullLogger<ResxLocalizationService>.Instance);
|
|
var vi = CultureInfo.GetCultureInfo("vi-VN");
|
|
|
|
svc.SetCulture(vi);
|
|
|
|
svc.CurrentCulture.Name.Should().Be("vi-VN");
|
|
CultureInfo.CurrentUICulture.Name.Should().Be("vi-VN");
|
|
CultureInfo.CurrentCulture.Name.Should().Be("vi-VN");
|
|
CultureInfo.DefaultThreadCurrentUICulture?.Name.Should().Be("vi-VN");
|
|
CultureInfo.DefaultThreadCurrentCulture?.Name.Should().Be("vi-VN");
|
|
}
|
|
|
|
[Fact]
|
|
public void SetCulture_Arabic_FlipsFlowDirectionToRightToLeft()
|
|
{
|
|
var svc = new ResxLocalizationService(NullLogger<ResxLocalizationService>.Instance);
|
|
|
|
svc.SetCulture(CultureInfo.GetCultureInfo("ar-SA"));
|
|
|
|
svc.CurrentFlowDirection.Should().Be(TextFlowDirection.RightToLeft);
|
|
}
|
|
|
|
[Fact]
|
|
public void SetCulture_English_FlowDirectionLeftToRight()
|
|
{
|
|
var svc = new ResxLocalizationService(NullLogger<ResxLocalizationService>.Instance);
|
|
|
|
// Default is en-US; switch elsewhere first so SetCulture actually fires.
|
|
svc.SetCulture(CultureInfo.GetCultureInfo("ar-SA"));
|
|
svc.SetCulture(CultureInfo.GetCultureInfo("en-US"));
|
|
|
|
svc.CurrentFlowDirection.Should().Be(TextFlowDirection.LeftToRight);
|
|
}
|
|
|
|
[Fact]
|
|
public void SetCulture_RaisesItemIndexer_CurrentCulture_AndFlowDirection_Once_Each()
|
|
{
|
|
var svc = new ResxLocalizationService(NullLogger<ResxLocalizationService>.Instance);
|
|
var names = new List<string?>();
|
|
svc.PropertyChanged += (_, e) => names.Add(e.PropertyName);
|
|
|
|
svc.SetCulture(CultureInfo.GetCultureInfo("vi-VN"));
|
|
|
|
names.Should().Contain("Item[]");
|
|
names.Should().Contain(nameof(ILocalizationService.CurrentCulture));
|
|
names.Should().Contain(nameof(ILocalizationService.CurrentFlowDirection));
|
|
names.Count(n => n == "Item[]").Should().Be(1);
|
|
}
|
|
|
|
[Fact]
|
|
public void SetCulture_AlsoRaisesCultureChangedEvent()
|
|
{
|
|
var svc = new ResxLocalizationService(NullLogger<ResxLocalizationService>.Instance);
|
|
var cultureChanged = 0;
|
|
svc.CultureChanged += (_, _) => cultureChanged++;
|
|
|
|
svc.SetCulture(CultureInfo.GetCultureInfo("vi-VN"));
|
|
|
|
cultureChanged.Should().Be(1);
|
|
}
|
|
|
|
[Fact]
|
|
public void SetCulture_SameAsCurrent_IsNoOp()
|
|
{
|
|
var svc = new ResxLocalizationService(NullLogger<ResxLocalizationService>.Instance);
|
|
// Constructor seeds with en-US.
|
|
var events = 0;
|
|
svc.PropertyChanged += (_, _) => events++;
|
|
var cultureChanged = 0;
|
|
svc.CultureChanged += (_, _) => cultureChanged++;
|
|
|
|
svc.SetCulture(CultureInfo.GetCultureInfo("en-US"));
|
|
|
|
events.Should().Be(0);
|
|
cultureChanged.Should().Be(0);
|
|
}
|
|
|
|
[Fact]
|
|
public void SetCulture_Null_Throws()
|
|
{
|
|
var svc = new ResxLocalizationService(NullLogger<ResxLocalizationService>.Instance);
|
|
|
|
Action act = () => svc.SetCulture(null!);
|
|
|
|
act.Should().Throw<ArgumentNullException>();
|
|
}
|
|
|
|
[Fact]
|
|
public void Indexer_MissingKey_ReturnsKeyInBrackets()
|
|
{
|
|
var svc = new ResxLocalizationService(NullLogger<ResxLocalizationService>.Instance);
|
|
|
|
var result = svc["NoSuchKey_DefinitelyMissing_1234"];
|
|
|
|
result.Should().Be("[NoSuchKey_DefinitelyMissing_1234]");
|
|
}
|
|
|
|
[Fact]
|
|
public void Indexer_EmptyKey_ReturnsEmptyString()
|
|
{
|
|
var svc = new ResxLocalizationService(NullLogger<ResxLocalizationService>.Instance);
|
|
|
|
svc[""].Should().BeEmpty();
|
|
}
|
|
|
|
[Fact]
|
|
public void Indexer_KnownKey_ReturnsEnglishByDefault()
|
|
{
|
|
var svc = new ResxLocalizationService(NullLogger<ResxLocalizationService>.Instance);
|
|
|
|
svc["Nav_Dashboard"].Should().Be("Dashboard");
|
|
}
|
|
|
|
[Fact]
|
|
public void Indexer_AfterSetCulture_ReturnsLocalizedValue()
|
|
{
|
|
var svc = new ResxLocalizationService(NullLogger<ResxLocalizationService>.Instance);
|
|
svc.SetCulture(CultureInfo.GetCultureInfo("vi-VN"));
|
|
|
|
svc["Nav_Dashboard"].Should().Be("Bảng điều khiển");
|
|
}
|
|
|
|
[Fact]
|
|
public void SupportedCultures_IncludesShippedLanguages()
|
|
{
|
|
var svc = new ResxLocalizationService(NullLogger<ResxLocalizationService>.Instance);
|
|
|
|
var names = svc.SupportedCultures.Select(c => c.Name).ToArray();
|
|
|
|
names.Should().Contain("en-US");
|
|
names.Should().Contain("vi-VN");
|
|
names.Should().Contain("ar-SA");
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData("vi-VN", "vi-VN")]
|
|
[InlineData("vi", "vi-VN")]
|
|
[InlineData("zh-CN", "zh-Hans")]
|
|
[InlineData("pt-PT", "pt-BR")]
|
|
[InlineData("fr-CA", "fr-FR")]
|
|
[InlineData("en-GB", "en-US")]
|
|
[InlineData("sw-KE", "en-US")]
|
|
public void MatchSupportedCulture_PrefersSameLanguage_ThenEnglish(string requested, string expected)
|
|
{
|
|
var svc = new ResxLocalizationService(NullLogger<ResxLocalizationService>.Instance);
|
|
|
|
svc.MatchSupportedCulture(CultureInfo.GetCultureInfo(requested)).Name.Should().Be(expected);
|
|
}
|
|
|
|
[Fact]
|
|
public void MatchSupportedCulture_Invariant_FallsBackToEnglish()
|
|
{
|
|
var svc = new ResxLocalizationService(NullLogger<ResxLocalizationService>.Instance);
|
|
|
|
svc.MatchSupportedCulture(CultureInfo.InvariantCulture).Name.Should().Be("en-US");
|
|
svc.MatchSupportedCulture(null).Name.Should().Be("en-US");
|
|
}
|
|
}
|