diff --git a/tests/Titano.App.Tests/Archive/ArchiveLanguageTests.cs b/tests/Titano.App.Tests/Archive/ArchiveLanguageTests.cs new file mode 100644 index 00000000..2fbbce94 --- /dev/null +++ b/tests/Titano.App.Tests/Archive/ArchiveLanguageTests.cs @@ -0,0 +1,61 @@ +using System.Globalization; +using Titano.App.Archiving; +using Titano.Core.Archiving; +using Titano.Core.Localization; +using Titano.Testing; +using static Titano.App.Tests.Archiving.ArchiveLogicTests; + +namespace Titano.App.Tests.Archiving; + +/// +/// Archive texts in other languages. They switch the process-wide UI language, so the class is serial: in parallel it +/// would race with the English assertions of the classes running beside it. +/// +[Serial] +public sealed class ArchiveLanguageTests +{ + public ArchiveLanguageTests() => Loc.SetCulture(CultureInfo.GetCultureInfo("en")); + + [Test] + public void MonthColumnsAreInitialsOnlyWhereTheyAreLetters() + { + var numbers = Enumerable.Range(1, 12).Select(m => m.ToString(CultureInfo.InvariantCulture)).ToArray(); + try + { + Assert.Equal("J F M A M J J A S O N D", string.Join(' ', ArchiveFigures.MonthLabels())); + Loc.SetCulture(CultureInfo.GetCultureInfo("it")); + Assert.Equal("G F M A M G L A S O N D", string.Join(' ', ArchiveFigures.MonthLabels())); + // Numbered abbreviations ("10月", "Tháng 10") would read "1" for October to December, or "T" for every month. + foreach (var culture in new[] { "ja", "ko", "zh-Hans", "zh-Hant", "vi", "th", "ar", "he" }) + { + Loc.SetCulture(CultureInfo.GetCultureInfo(culture)); + Assert.SequenceEqual(numbers, ArchiveFigures.MonthLabels(), culture); + } + } + finally + { + Loc.SetCulture(CultureInfo.GetCultureInfo("en")); + } + } + + [Test] + public void TreeRowsShowTheirWholeTitleAndTheKeysOfTheReadingDirection() + { + var harbour = Seq("Construction cam · weekly", new DateTime(2026, 9, 28), 3_600, 72_000_000_000); + var tree = ArchiveTree.Build(Main, ArchiveAvailability.Online, [harbour]); + var year = new ArchiveTreeRowViewModel(tree.Root.Children[0], ArchiveAvailability.Online, _ => { }); + var sequence = new ArchiveTreeRowViewModel(tree.Root.Children[0].Children[0], ArchiveAvailability.Online, _ => { }); + Assert.Equal("Construction cam · weekly · 3,600 photos · 72 GB", sequence.RowToolTip); + Assert.Equal("2026 · 1 sequence · 72 GB · Right arrow opens the year, Left arrow closes it", year.RowToolTip); + try + { + Loc.SetCulture(CultureInfo.GetCultureInfo("he")); + Assert.Equal(Loc.L("Tooltips.Archive.YearRowRtl", "2026" + Loc.Separator + year.Detail), year.RowToolTip, "the arrow keys of a mirrored tree"); + Assert.Contains("‏", year.RowToolTip, "right-to-left marks keep the year and its figures apart"); + } + finally + { + Loc.SetCulture(CultureInfo.GetCultureInfo("en")); + } + } +} diff --git a/tests/Titano.App.Tests/Archive/ArchiveLogicTests.cs b/tests/Titano.App.Tests/Archive/ArchiveLogicTests.cs index 25ad9d8d..4bd6cca6 100644 --- a/tests/Titano.App.Tests/Archive/ArchiveLogicTests.cs +++ b/tests/Titano.App.Tests/Archive/ArchiveLogicTests.cs @@ -17,11 +17,11 @@ namespace Titano.App.Tests.Archiving; public sealed class ArchiveLogicTests { - private static readonly Core.Model.Archive Main = new() { Id = Guid.CreateVersion7(), Name = "TitanoArchive", RootPath = "/archive" }; + internal static readonly Core.Model.Archive Main = new() { Id = Guid.CreateVersion7(), Name = "TitanoArchive", RootPath = "/archive" }; public ArchiveLogicTests() => Loc.SetCulture(CultureInfo.GetCultureInfo("en")); - private static Sequence Seq(string title, DateTime? first, int frames = 100, long bytes = 1_000_000, params string[] tags) => new() + internal static Sequence Seq(string title, DateTime? first, int frames = 100, long bytes = 1_000_000, params string[] tags) => new() { Id = Guid.CreateVersion7(), ArchiveId = Main.Id, @@ -170,28 +170,6 @@ public sealed class ArchiveLogicTests Assert.Empty(ArchiveFigures.PhotosPerMonth(Stats() with { ByMonth = [] }, DateTime.Today)); } - [Test] - public void MonthColumnsAreInitialsOnlyWhereTheyAreLetters() - { - var numbers = Enumerable.Range(1, 12).Select(m => m.ToString(CultureInfo.InvariantCulture)).ToArray(); - try - { - Assert.Equal("J F M A M J J A S O N D", string.Join(' ', ArchiveFigures.MonthLabels())); - Loc.SetCulture(CultureInfo.GetCultureInfo("it")); - Assert.Equal("G F M A M G L A S O N D", string.Join(' ', ArchiveFigures.MonthLabels())); - // Numbered abbreviations ("10月", "Tháng 10") would read "1" for October to December, or "T" for every month. - foreach (var culture in new[] { "ja", "ko", "zh-Hans", "zh-Hant", "vi", "th", "ar", "he" }) - { - Loc.SetCulture(CultureInfo.GetCultureInfo(culture)); - Assert.SequenceEqual(numbers, ArchiveFigures.MonthLabels(), culture); - } - } - finally - { - Loc.SetCulture(CultureInfo.GetCultureInfo("en")); - } - } - [Test] public void TilesAndHealthSummariseTheIndex() { @@ -246,27 +224,6 @@ public sealed class ArchiveLogicTests Assert.Equal(Loc.L("Archive.Error.StalePlan"), ArchiveFailures.Cause(new StaleRenamePlanException(Guid.NewGuid()))); } - [Test] - public void TreeRowsShowTheirWholeTitleAndTheKeysOfTheReadingDirection() - { - var harbour = Seq("Construction cam · weekly", new DateTime(2026, 9, 28), 3_600, 72_000_000_000); - var tree = ArchiveTree.Build(Main, ArchiveAvailability.Online, [harbour]); - var year = new ArchiveTreeRowViewModel(tree.Root.Children[0], ArchiveAvailability.Online, _ => { }); - var sequence = new ArchiveTreeRowViewModel(tree.Root.Children[0].Children[0], ArchiveAvailability.Online, _ => { }); - Assert.Equal("Construction cam · weekly · 3,600 photos · 72 GB", sequence.RowToolTip); - Assert.Equal("2026 · 1 sequence · 72 GB · Right arrow opens the year, Left arrow closes it", year.RowToolTip); - try - { - Loc.SetCulture(CultureInfo.GetCultureInfo("he")); - Assert.Equal(Loc.L("Tooltips.Archive.YearRowRtl", "2026" + Loc.Separator + year.Detail), year.RowToolTip, "the arrow keys of a mirrored tree"); - Assert.Contains("\u200F", year.RowToolTip, "right-to-left marks keep the year and its figures apart"); - } - finally - { - Loc.SetCulture(CultureInfo.GetCultureInfo("en")); - } - } - [Test] public void RenameTemplatesAreCheckedAndThePlanListedByMode() { diff --git a/tests/Titano.App.Tests/Import/CardPoolTests.cs b/tests/Titano.App.Tests/Import/CardPoolTests.cs index 91e7f88e..02875fd6 100644 --- a/tests/Titano.App.Tests/Import/CardPoolTests.cs +++ b/tests/Titano.App.Tests/Import/CardPoolTests.cs @@ -8,9 +8,9 @@ namespace Titano.App.Tests.Import; public sealed class CardPoolTests { - private static readonly CardDevice CardA = new(@"F:\", "EOS_DIGITAL", 0x3F2A9C11, 128_000_000_000, 31_800_000_000, true, true); + internal static readonly CardDevice CardA = new(@"F:\", "EOS_DIGITAL", 0x3F2A9C11, 128_000_000_000, 31_800_000_000, true, true); private static readonly CardDevice GoPro = new(@"E:\", "GOPRO", 0x11112222, 64_000_000_000, 52_600_000_000, true, true); - private static readonly CardDevice Usb = new(@"G:\", "BACKUP", 0x99990000, 2_000_000_000_000, 1_800_000_000_000, true, false); + internal static readonly CardDevice Usb = new(@"G:\", "BACKUP", 0x99990000, 2_000_000_000_000, 1_800_000_000_000, true, false); public CardPoolTests() => Loc.SetCulture(CultureInfo.GetCultureInfo("en")); @@ -77,25 +77,6 @@ public sealed class CardPoolTests Assert.Equal(@"Folder · D:\Shoots\2026", pool[2].ToolTip); } - [Test] - public void PathsAndSerialsStayLeftToRightInARightToLeftLanguage() - { - try - { - Loc.SetCulture(CultureInfo.GetCultureInfo("he")); - var pool = CardPool.Build([Usb with { Label = string.Empty }, CardA], [], @"D:\Shoots\"); - Assert.Equal("\u202AG:\\\u202C", pool[1].Title, "a drive root keeps its separator at its end"); - Assert.Equal("\u202AD:\\Shoots\\\u202C", pool[2].Title); - Assert.Equal("\u202AD:\\Shoots\\\u202C", pool[2].Subtitle); - Assert.Contains("\u202A3F2A-9C11\u202C", pool[0].IdentityText); - Assert.Equal("EOS_DIGITAL", pool[0].Title, "a label is left to bidi"); - } - finally - { - Loc.SetCulture(CultureInfo.GetCultureInfo("en")); - } - } - [Test] public void AKnownCardWithoutDcimIsStillACard() { diff --git a/tests/Titano.App.Tests/Import/ImportLanguageTests.cs b/tests/Titano.App.Tests/Import/ImportLanguageTests.cs new file mode 100644 index 00000000..c9150447 --- /dev/null +++ b/tests/Titano.App.Tests/Import/ImportLanguageTests.cs @@ -0,0 +1,55 @@ +using System.Globalization; +using Titano.App.Import; +using Titano.Core.Localization; +using Titano.Testing; +using static Titano.App.Tests.Import.CardPoolTests; +using static Titano.App.Tests.Import.ImportPlansTests; + +namespace Titano.App.Tests.Import; + +/// +/// Import texts in a right-to-left language. They switch the process-wide UI language, so the class is serial: in +/// parallel it would race with the English assertions of the classes running beside it. +/// +[Serial] +public sealed class ImportLanguageTests +{ + public ImportLanguageTests() => Loc.SetCulture(CultureInfo.GetCultureInfo("en")); + + [Test] + public void PathsAndSerialsStayLeftToRightInARightToLeftLanguage() + { + try + { + Loc.SetCulture(CultureInfo.GetCultureInfo("he")); + var pool = CardPool.Build([Usb with { Label = string.Empty }, CardA], [], @"D:\Shoots\"); + Assert.Equal("\u202AG:\\\u202C", pool[1].Title, "a drive root keeps its separator at its end"); + Assert.Equal("\u202AD:\\Shoots\\\u202C", pool[2].Title); + Assert.Equal("\u202AD:\\Shoots\\\u202C", pool[2].Subtitle); + Assert.Contains("\u202A3F2A-9C11\u202C", pool[0].IdentityText); + Assert.Equal("EOS_DIGITAL", pool[0].Title, "a label is left to bidi"); + } + finally + { + Loc.SetCulture(CultureInfo.GetCultureInfo("en")); + } + } + + [Test] + public void ARightToLeftLanguageReadsTheSessionTimesWithItsOwnArrow() + { + var plan = ThreeSessions(); + Assert.Contains("→", ImportPlans.Sessions(plan, ImportDestinationKind.Archive, new HashSet())[0].TimeText); + try + { + Loc.SetCulture(CultureInfo.GetCultureInfo("he")); + var time = ImportPlans.Sessions(plan, ImportDestinationKind.Archive, new HashSet())[0].TimeText; + Assert.Contains("←", time); + Assert.False(time.Contains('→'), "a right-to-left reader would see the arrow point back to the start"); + } + finally + { + Loc.SetCulture(CultureInfo.GetCultureInfo("en")); + } + } +} diff --git a/tests/Titano.App.Tests/Import/ImportPlansTests.cs b/tests/Titano.App.Tests/Import/ImportPlansTests.cs index 4be55830..797c5ab0 100644 --- a/tests/Titano.App.Tests/Import/ImportPlansTests.cs +++ b/tests/Titano.App.Tests/Import/ImportPlansTests.cs @@ -11,7 +11,7 @@ public sealed class ImportPlansTests { public ImportPlansTests() => Loc.SetCulture(CultureInfo.GetCultureInfo("en")); - private static ImportPlan ThreeSessions(long? available = 200_000_000) + internal static ImportPlan ThreeSessions(long? available = 200_000_000) { var duplicate = Guid.CreateVersion7(); return Plan(available, [new ImportProblem("DCIM/100CANON/IMG_0099.CR2", "Access denied")], @@ -140,24 +140,6 @@ public sealed class ImportPlansTests Assert.Equal("Split at IMG_0002.CR2", file.SplitName); } - [Test] - public void ARightToLeftLanguageReadsTheSessionTimesWithItsOwnArrow() - { - var plan = ThreeSessions(); - Assert.Contains("→", ImportPlans.Sessions(plan, ImportDestinationKind.Archive, new HashSet())[0].TimeText); - try - { - Loc.SetCulture(CultureInfo.GetCultureInfo("he")); - var time = ImportPlans.Sessions(plan, ImportDestinationKind.Archive, new HashSet())[0].TimeText; - Assert.Contains("←", time); - Assert.False(time.Contains('→'), "a right-to-left reader would see the arrow point back to the start"); - } - finally - { - Loc.SetCulture(CultureInfo.GetCultureInfo("en")); - } - } - [Test] public void TheNamePreviewShowsTheFirstThreeCopiedFiles() { diff --git a/tests/Titano.App.Tests/Program.cs b/tests/Titano.App.Tests/Program.cs index e4838431..f414976b 100644 --- a/tests/Titano.App.Tests/Program.cs +++ b/tests/Titano.App.Tests/Program.cs @@ -1,8 +1,19 @@ +using Titano.Core.Localization; using Titano.Testing; namespace Titano.App.Tests; internal static class TestMain { - public static Task Main(string[] args) => TestRunner.RunAsync(args, typeof(TestMain).Assembly); + public static Task Main(string[] args) + { + // 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. + 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"); + }; + return TestRunner.RunAsync(args, typeof(TestMain).Assembly); + } } diff --git a/tests/Titano.Core.Tests/Decoding/FrameWindowIsolationTests.cs b/tests/Titano.Core.Tests/Decoding/FrameWindowIsolationTests.cs new file mode 100644 index 00000000..021b643f --- /dev/null +++ b/tests/Titano.Core.Tests/Decoding/FrameWindowIsolationTests.cs @@ -0,0 +1,67 @@ +using Titano.Core.Imaging; +using Titano.Testing; +using static Titano.Core.Tests.Decoding.FrameWindowTests; + +namespace Titano.Core.Tests.Decoding; + +/// +/// cancellation (BRIEF §20.4 cancellation gate). Unobserved task exceptions are counted +/// process-wide, so the class is serial: a faulted task another test left behind would otherwise be blamed on it. +/// +[Serial] +public sealed class FrameWindowIsolationTests(TestContext context) +{ + [Test] + [Timeout(10_000)] + public async Task CancellationLeavesNoSpillFileAndNoUnobservedErrors() + { + // Faulted tasks the earlier tests left for the finalizer are reported now, before this test starts counting. + GC.Collect(); + GC.WaitForPendingFinalizers(); + var unobserved = 0; + void OnUnobserved(object? sender, UnobservedTaskExceptionEventArgs e) => Interlocked.Increment(ref unobserved); + TaskScheduler.UnobservedTaskException += OnUnobserved; + try + { + var dir = context.CreateTempDirectory(); + var source = new Source(); + string? spill; + using var cts = new CancellationTokenSource(); + var window = new FrameWindow(50, source.Load, new FrameWindowOptions { Behind = 3, Ahead = 2, MemoryBudget = 2 * FrameBytes, SpillDirectory = dir }); + try + { + for (var i = 0; i < 6; i++) + { + window.MoveTo(i); + using var a = await window.GetAsync(i); + if (i > 0) + { + using var b = await window.GetAsync(i - 1); + } + } + spill = window.SpillPath; + Assert.NotNull(spill); + Assert.FileExists(spill!); + source.Delay = TimeSpan.FromSeconds(30); + window.MoveTo(20); + var pending = window.GetAsync(20, cts.Token); + cts.CancelAfter(50); + await Assert.ThrowsAsync(async () => await pending); + } + finally + { + await window.DisposeAsync(); + } + Assert.FileMissing(spill!); + Assert.Equal(0, source.Live()); + await Assert.ThrowsAsync(async () => await window.GetAsync(1)); + } + finally + { + GC.Collect(); + GC.WaitForPendingFinalizers(); + TaskScheduler.UnobservedTaskException -= OnUnobserved; + } + Assert.Equal(0, unobserved); + } +} diff --git a/tests/Titano.Core.Tests/Decoding/FrameWindowTests.cs b/tests/Titano.Core.Tests/Decoding/FrameWindowTests.cs index 06edd17b..1a2305b2 100644 --- a/tests/Titano.Core.Tests/Decoding/FrameWindowTests.cs +++ b/tests/Titano.Core.Tests/Decoding/FrameWindowTests.cs @@ -3,15 +3,15 @@ using Titano.Testing; namespace Titano.Core.Tests.Decoding; -/// : memory bound independent of sequence length, spill round trip, read-ahead, -/// cancellation and deterministic disposal (BRIEF §8.4, §20.4 memory and cancellation gates). +/// : memory bound independent of sequence length, spill round trip, read-ahead and +/// deterministic disposal (BRIEF §8.4, §20.4 memory gate; the cancellation gate is in ). public sealed class FrameWindowTests(TestContext context) { private const int Size = 64; - private const long FrameBytes = Size * Size * 3 * sizeof(float); + internal const long FrameBytes = Size * Size * 3 * sizeof(float); /// Synthetic loader whose frames encode their index; records every buffer it hands out. - private sealed class Source + internal sealed class Source { private readonly List _made = []; public int Loads; @@ -126,57 +126,6 @@ public sealed class FrameWindowTests(TestContext context) Assert.Equal(4, source.Loads); } - [Test] - [Timeout(10_000)] - public async Task CancellationLeavesNoSpillFileAndNoUnobservedErrors() - { - var unobserved = 0; - void OnUnobserved(object? sender, UnobservedTaskExceptionEventArgs e) => Interlocked.Increment(ref unobserved); - TaskScheduler.UnobservedTaskException += OnUnobserved; - try - { - var dir = context.CreateTempDirectory(); - var source = new Source(); - string? spill; - using var cts = new CancellationTokenSource(); - var window = new FrameWindow(50, source.Load, new FrameWindowOptions { Behind = 3, Ahead = 2, MemoryBudget = 2 * FrameBytes, SpillDirectory = dir }); - try - { - for (var i = 0; i < 6; i++) - { - window.MoveTo(i); - using var a = await window.GetAsync(i); - if (i > 0) - { - using var b = await window.GetAsync(i - 1); - } - } - spill = window.SpillPath; - Assert.NotNull(spill); - Assert.FileExists(spill!); - source.Delay = TimeSpan.FromSeconds(30); - window.MoveTo(20); - var pending = window.GetAsync(20, cts.Token); - cts.CancelAfter(50); - await Assert.ThrowsAsync(async () => await pending); - } - finally - { - await window.DisposeAsync(); - } - Assert.FileMissing(spill!); - Assert.Equal(0, source.Live()); - await Assert.ThrowsAsync(async () => await window.GetAsync(1)); - } - finally - { - GC.Collect(); - GC.WaitForPendingFinalizers(); - TaskScheduler.UnobservedTaskException -= OnUnobserved; - } - Assert.Equal(0, unobserved); - } - [Test] public async Task LoaderFailuresSurfaceAndCanBeRetried() { diff --git a/tests/Titano.Core.Tests/Program.cs b/tests/Titano.Core.Tests/Program.cs index 905d7a45..a6af6d91 100644 --- a/tests/Titano.Core.Tests/Program.cs +++ b/tests/Titano.Core.Tests/Program.cs @@ -1,8 +1,19 @@ +using Titano.Core.Localization; using Titano.Testing; namespace Titano.Core.Tests; internal static class TestMain { - public static Task Main(string[] args) => TestRunner.RunAsync(args, typeof(TestMain).Assembly); + public static Task Main(string[] args) + { + // 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. + 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"); + }; + return TestRunner.RunAsync(args, typeof(TestMain).Assembly); + } } diff --git a/tests/Titano.Core.Tests/Render/RenderIsolationTests.cs b/tests/Titano.Core.Tests/Render/RenderIsolationTests.cs index 50456937..5ac8bf13 100644 --- a/tests/Titano.Core.Tests/Render/RenderIsolationTests.cs +++ b/tests/Titano.Core.Tests/Render/RenderIsolationTests.cs @@ -16,6 +16,9 @@ public sealed class RenderIsolationTests [Test] public async Task FailuresLeaveNoUnobservedTaskExceptions() { + // Faulted tasks the earlier tests left for the finalizer are reported now, before this test starts counting. + GC.Collect(); + GC.WaitForPendingFinalizers(); var unobserved = 0; void Handler(object? sender, UnobservedTaskExceptionEventArgs e) => Interlocked.Increment(ref unobserved); TaskScheduler.UnobservedTaskException += Handler; diff --git a/tests/Titano.Testing/TestContext.cs b/tests/Titano.Testing/TestContext.cs index 3c31cc3d..d5fcb401 100644 --- a/tests/Titano.Testing/TestContext.cs +++ b/tests/Titano.Testing/TestContext.cs @@ -9,18 +9,33 @@ namespace Titano.Testing; /// public sealed class TestContext : IDisposable { + private static readonly AsyncLocal CurrentTest = new(); private readonly ConcurrentQueue _log = new(); private readonly List _tempDirectories = []; private readonly Lock _gate = new(); - internal TestContext(string testName, CancellationToken cancellation) + internal TestContext(string testName, bool isSerial, CancellationToken cancellation) { TestName = testName; + IsSerial = isSerial; Cancellation = cancellation; } + /// + /// The test running on this flow of execution (its constructor, body and the tasks it starts); null outside tests. + /// Lets a test program guard process-wide state, which only a serial test may change. + /// + public static TestContext? Current + { + get => CurrentTest.Value; + internal set => CurrentTest.Value = value; + } + public string TestName { get; } + /// True when the test's class is : no other test runs beside it. + public bool IsSerial { get; } + /// Signalled when the test times out or the run is aborted. public CancellationToken Cancellation { get; } diff --git a/tests/Titano.Testing/TestRunner.cs b/tests/Titano.Testing/TestRunner.cs index 3b0576f6..91c18b07 100644 --- a/tests/Titano.Testing/TestRunner.cs +++ b/tests/Titano.Testing/TestRunner.cs @@ -100,30 +100,41 @@ public static class TestRunner } foreach (var type in types.Where(t => t is { IsClass: true, IsPublic: true } && !t.IsAbstract).OrderBy(t => t.FullName, StringComparer.Ordinal)) + list.AddRange(Discover(type)); + return list; + } + + /// + /// The tests of one class, in declaration order. Assembly discovery takes top-level classes only, so a class nested + /// in a test class is a fixture that runs only when a test hands it to . + /// + public static IReadOnlyList Discover(Type type) + { + var list = new List(); + foreach (var method in type.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static).OrderBy(m => m.MetadataToken)) { - foreach (var method in type.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static).OrderBy(m => m.MetadataToken)) + if (method.GetCustomAttribute() is not null) { - if (method.GetCustomAttribute() is not null) - { - list.Add(new TestCase(type, method, null, null)); - } - else if (method.GetCustomAttribute() is not null) - { - foreach (var c in method.GetCustomAttributes()) - list.Add(new TestCase(type, method, c.Arguments, c.Name)); - } + list.Add(new TestCase(type, method, null, null)); + } + else if (method.GetCustomAttribute() is not null) + { + foreach (var c in method.GetCustomAttributes()) + list.Add(new TestCase(type, method, c.Arguments, c.Name)); } } return list; } - private static async Task ExecuteAsync(TestCase test) + /// Runs one test (constructor, body, disposal) under its timeout and returns its outcome. + public static async Task ExecuteAsync(TestCase test) { + ArgumentNullException.ThrowIfNull(test); if (test.StaticSkipReason is { } reason) return new TestResult(test, TestOutcome.Skipped, TimeSpan.Zero, reason, null, string.Empty); using var cts = new CancellationTokenSource(); - using var context = new TestContext(test.FullName, cts.Token); + using var context = new TestContext(test.FullName, test.Serial, cts.Token); var sw = Stopwatch.StartNew(); try { @@ -152,29 +163,21 @@ public static class TestRunner private static async Task InvokeAsync(TestCase test, TestContext context) { + TestContext.Current = context; object? instance = null; try { if (!test.Method.IsStatic) { var ctor = test.Type.GetConstructor([typeof(TestContext)]); - instance = ctor is not null + instance = Unwrapped(() => ctor is not null ? ctor.Invoke([context]) - : Activator.CreateInstance(test.Type); + : Activator.CreateInstance(test.Type)); } var parameters = test.Method.GetParameters(); var args = BuildArguments(test, parameters, context); - object? returned; - try - { - returned = test.Method.Invoke(instance, args); - } - catch (TargetInvocationException tie) when (tie.InnerException is not null) - { - ExceptionDispatchInfo.Capture(tie.InnerException).Throw(); - throw; - } + var returned = Unwrapped(() => test.Method.Invoke(instance, args)); switch (returned) { @@ -200,6 +203,23 @@ public static class TestRunner } } + /// + /// Makes a reflection call and rethrows what the constructor or method threw, so a skip stays a skip and a failure + /// carries its own message and stack trace instead of "Exception has been thrown by the target of an invocation". + /// + private static object? Unwrapped(Func call) + { + try + { + return call(); + } + catch (TargetInvocationException tie) when (tie.InnerException is not null) + { + ExceptionDispatchInfo.Capture(tie.InnerException).Throw(); + throw; + } + } + private static object?[] BuildArguments(TestCase test, ParameterInfo[] parameters, TestContext context) { var supplied = test.Arguments ?? []; diff --git a/tests/Titano.Tools.Tests/TestRunnerTests.cs b/tests/Titano.Tools.Tests/TestRunnerTests.cs new file mode 100644 index 00000000..3328d6fe --- /dev/null +++ b/tests/Titano.Tools.Tests/TestRunnerTests.cs @@ -0,0 +1,102 @@ +using Titano.Testing; + +namespace Titano.Tools.Tests; + +/// +/// The in-house runner reports what a test class did, not how reflection called it (BRIEF §20.1). The nested classes +/// are fixtures: assembly discovery leaves them out, and each test hands one to the runner. +/// +public sealed class TestRunnerTests(TestContext context) +{ + public sealed class SkippingConstructor + { + public SkippingConstructor() => TestContext.Skip("no server configured"); + + [Test] + public void Body() => Assert.Fail("the body ran after the constructor skipped"); + } + + public sealed class FailingConstructor + { + public FailingConstructor(TestContext context) => Assert.Fail($"{context.TestName} could not start its fixture"); + + [Test] + public void Body() => Assert.Fail("the body ran after the constructor failed"); + } + + public sealed class ThrowingFieldInitializer + { + private readonly string _root = Environment.GetEnvironmentVariable("TITANO_NO_SUCH_VARIABLE") ?? throw new InvalidOperationException("no root folder"); + + [Test] + public void Body() => Assert.Fail("the body ran with " + _root); + } + + public sealed class BesideOthers + { + [Test] + public async Task SeesItsOwnContext(TestContext context) + { + Assert.Same(context, TestContext.Current); + Assert.False(context.IsSerial); + await Task.Yield(); + Assert.Same(context, await Task.Run(() => TestContext.Current)); + } + } + + [Serial] + public sealed class Alone + { + [Test] + public void KnowsItRunsAlone(TestContext context) => Assert.True(context.IsSerial && TestContext.Current == context); + } + + private static async Task RunAsync() + { + var test = TestRunner.Discover(typeof(T)).Single(); + return await TestRunner.ExecuteAsync(test); + } + + [Test] + public void NestedFixturesAreNotDiscoveredWithTheAssembly() + { + var names = TestRunner.Discover(typeof(TestRunnerTests).Assembly).Select(t => t.FullName).ToList(); + Assert.Contains(typeof(TestRunnerTests).FullName + "." + nameof(NestedFixturesAreNotDiscoveredWithTheAssembly), names); + Assert.False(names.Any(n => n.Contains('+', StringComparison.Ordinal)), "a nested class would run as a real test"); + } + + [Test] + public async Task ASkipInTheConstructorSkipsTheTest() + { + var result = await RunAsync(); + Assert.Equal(TestOutcome.Skipped, result.Outcome); + Assert.Equal("no server configured", result.Message); + } + + [Test] + public async Task AFailureInTheConstructorKeepsItsMessage() + { + var result = await RunAsync(); + Assert.Equal(TestOutcome.Failed, result.Outcome); + Assert.Equal(typeof(FailingConstructor).FullName + ".Body could not start its fixture", result.Message); + Assert.Contains(nameof(FailingConstructor), result.StackTrace); + } + + [Test] + public async Task AnExceptionInAFieldInitializerIsReportedAsItself() + { + var result = await RunAsync(); + Assert.Equal(TestOutcome.Failed, result.Outcome); + Assert.Equal("System.InvalidOperationException: no root folder", result.Message); + } + + [Test] + public async Task TheCurrentContextFollowsEachTestAndItsTasks() + { + var parallel = await RunAsync(); + Assert.Equal(TestOutcome.Passed, parallel.Outcome, parallel.Message); + var alone = await RunAsync(); + Assert.Equal(TestOutcome.Passed, alone.Outcome, alone.Message); + Assert.Same(context, TestContext.Current); + } +}