From 7e969736678028d79fddcc3e6c479f5a9304d1a7 Mon Sep 17 00:00:00 2001 From: David Date: Thu, 4 Jan 2024 11:37:18 -0500 Subject: [PATCH 1/2] fix: Fix DynamicData support --- src/TestApp/shared/SanityTests.cs | 23 +++++++++++++++++++ .../Engine/UnitTestMethodInfo.cs | 19 +++++++-------- 2 files changed, 33 insertions(+), 9 deletions(-) diff --git a/src/TestApp/shared/SanityTests.cs b/src/TestApp/shared/SanityTests.cs index c2b0039..1ddf6dc 100644 --- a/src/TestApp/shared/SanityTests.cs +++ b/src/TestApp/shared/SanityTests.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Linq; using System.Text; using System.Threading.Tasks; using Microsoft.VisualStudio.TestTools.UnitTesting; @@ -44,6 +45,28 @@ public async Task When_Test_ContentHelper() [DataRow("goodbye", DisplayName = "goodbye test")] public void Is_Sane_With_Cases(string text) { +#pragma warning disable CA1861 // Prefer static readonly + Assert.IsTrue(new[] { "hello", "goodbye" }.Contains(text)); + } + + [TestMethod] + [DynamicData(nameof(DynamicData), DynamicDataSourceType.Property)] + [DynamicData(nameof(GetDynamicData), DynamicDataSourceType.Method)] + public void Is_Sane_With_DynamicData(string text) + { + Assert.IsTrue(new[] { "hello", "goodbye" }.Contains(text)); + } + + public static IEnumerable DynamicData { get; } = new[] + { + new object[] { "hello" }, + new object[] { "goodbye" }, + }; + + public static IEnumerable GetDynamicData() + { + yield return new object[] { "hello" }; + yield return new object[] { "goodbye" }; } #if DEBUG diff --git a/src/Uno.UI.RuntimeTests.Engine.Library/Engine/UnitTestMethodInfo.cs b/src/Uno.UI.RuntimeTests.Engine.Library/Engine/UnitTestMethodInfo.cs index 1ddfd29..ff973d7 100644 --- a/src/Uno.UI.RuntimeTests.Engine.Library/Engine/UnitTestMethodInfo.cs +++ b/src/Uno.UI.RuntimeTests.Engine.Library/Engine/UnitTestMethodInfo.cs @@ -4,6 +4,7 @@ #if !IS_UNO_RUNTIMETEST_PROJECT #pragma warning disable #endif +#pragma warning disable CA1852 // Make class final : unnecessary breaking change using System; using System.Collections.Generic; @@ -93,15 +94,15 @@ public IEnumerable GetCases(CancellationToken ct) foreach (var testCaseSource in _casesParameters) { // Note: CT is not propagated when using a test data source - foreach (var caseData in testCaseSource.GetData(Method)) - { - var data = testCaseSource - .GetData(Method) - .SelectMany(x => x) - .ToArray(); - - cases.Add(new TestCase { Parameters = data, DisplayName = testCaseSource.GetDisplayName(Method, data) }); - } + var testCases = testCaseSource + .GetData(Method) + .Select(caseData => new TestCase + { + Parameters = caseData, + DisplayName = testCaseSource.GetDisplayName(Method, caseData) + }); + + cases.AddRange(testCases); } if (_injectedPointerTypes.Any()) From b3ce5369afdfbc52940cd5b43e0c24833436f878 Mon Sep 17 00:00:00 2001 From: David Date: Thu, 4 Jan 2024 11:37:30 -0500 Subject: [PATCH 2/2] chore: Update CA rules --- src/TestApp/shared/HotReloadTests.cs | 4 ++-- .../Engine/ExternalRunner/_Private/DevServer.cs | 2 +- .../Engine/ExternalRunner/_Private/SecondaryApp.cs | 3 ++- src/Uno.UI.RuntimeTests.Engine.Library/Engine/TestCase.cs | 2 +- .../Engine/TestCaseResult.cs | 1 + .../Engine/UI/UnitTestsControl.CustomConsoleOutput.cs | 4 ++-- .../Engine/UI/UnitTestsControl.TestRun.cs | 1 + .../Engine/UI/UnitTestsControl.cs | 2 +- .../Library/Helpers/HotReloadHelper.DevServer.cs | 2 +- .../Library/Helpers/HotReloadHelper.Local.cs | 2 +- .../Library/Helpers/HotReloadHelper.NotSupported.cs | 2 +- .../Library/Helpers/HotReloadHelper.cs | 2 +- .../Library/Helpers/InputInjectorHelper.cs | 2 +- 13 files changed, 16 insertions(+), 13 deletions(-) diff --git a/src/TestApp/shared/HotReloadTests.cs b/src/TestApp/shared/HotReloadTests.cs index 1518df2..b67f4b1 100644 --- a/src/TestApp/shared/HotReloadTests.cs +++ b/src/TestApp/shared/HotReloadTests.cs @@ -47,13 +47,13 @@ public async Task Is_SourcesEditable(CancellationToken ct) var file = Path.Combine(dir, sutPath); Assert.IsTrue(File.Exists(file)); - Assert.IsTrue(File.ReadAllText(file).Contains("Original text")); + Assert.IsTrue((await File.ReadAllTextAsync(file, ct)).Contains("Original text")); await using var _ = await HotReloadHelper.UpdateSourceFile(sutPath, "Original text", "Updated text from Can_Edit_File", waitForMetadataUpdate: false, ct); await TestHelper.WaitFor(() => File.ReadAllText(file).Contains("Updated text from Can_Edit_File"), ct); - Assert.IsTrue(File.ReadAllText(file).Contains("Updated text from Can_Edit_File")); + Assert.IsTrue((await File.ReadAllTextAsync(file, ct)).Contains("Updated text from Can_Edit_File")); } [TestMethod] diff --git a/src/Uno.UI.RuntimeTests.Engine.Library/Engine/ExternalRunner/_Private/DevServer.cs b/src/Uno.UI.RuntimeTests.Engine.Library/Engine/ExternalRunner/_Private/DevServer.cs index 2404f62..7aa4aaa 100644 --- a/src/Uno.UI.RuntimeTests.Engine.Library/Engine/ExternalRunner/_Private/DevServer.cs +++ b/src/Uno.UI.RuntimeTests.Engine.Library/Engine/ExternalRunner/_Private/DevServer.cs @@ -205,7 +205,7 @@ private static int GetTcpPort() } catch (global::System.Exception e) { - global::Microsoft.Extensions.Logging.LoggerExtensions.LogError(_log, "Failed to kill dev server", e); + global::Microsoft.Extensions.Logging.LoggerExtensions.LogError(_log, e, "Failed to kill dev server"); } await _process.WaitForExitAsync(global::System.Threading.CancellationToken.None); diff --git a/src/Uno.UI.RuntimeTests.Engine.Library/Engine/ExternalRunner/_Private/SecondaryApp.cs b/src/Uno.UI.RuntimeTests.Engine.Library/Engine/ExternalRunner/_Private/SecondaryApp.cs index 78d9d73..4216c7b 100644 --- a/src/Uno.UI.RuntimeTests.Engine.Library/Engine/ExternalRunner/_Private/SecondaryApp.cs +++ b/src/Uno.UI.RuntimeTests.Engine.Library/Engine/ExternalRunner/_Private/SecondaryApp.cs @@ -69,12 +69,13 @@ internal static partial class SecondaryApp } } + private static readonly global::System.Text.Json.JsonSerializerOptions _serializeOpts = new global::System.Text.Json.JsonSerializerOptions { DefaultIgnoreCondition = global::System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingDefault }; private static int _instance; private static async global::System.Threading.Tasks.Task RunLocalApp(string devServerHost, int devServerPort, UnitTestEngineConfig config, bool isAppVisible, global::System.Threading.CancellationToken ct) { var testOutput = global::System.IO.Path.GetTempFileName(); - var configJson = global::System.Text.Json.JsonSerializer.Serialize(config, new global::System.Text.Json.JsonSerializerOptions { DefaultIgnoreCondition = global::System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingDefault }); + var configJson = global::System.Text.Json.JsonSerializer.Serialize(config, _serializeOpts); var childStartInfo = new global::System.Diagnostics.ProcessStartInfo( global::System.Environment.ProcessPath ?? throw new global::System.InvalidOperationException("Cannot determine the current app executable path"), diff --git a/src/Uno.UI.RuntimeTests.Engine.Library/Engine/TestCase.cs b/src/Uno.UI.RuntimeTests.Engine.Library/Engine/TestCase.cs index 54c88ea..f8986cd 100644 --- a/src/Uno.UI.RuntimeTests.Engine.Library/Engine/TestCase.cs +++ b/src/Uno.UI.RuntimeTests.Engine.Library/Engine/TestCase.cs @@ -3,6 +3,7 @@ #if !IS_UNO_RUNTIMETEST_PROJECT #pragma warning disable #endif +#pragma warning disable CA1852 // Make class final : unnecessary breaking change using System; using System.Linq; @@ -11,7 +12,6 @@ namespace Uno.UI.RuntimeTests; #if !UNO_RUNTIMETESTS_DISABLE_UI - internal record TestCase { public object[] Parameters { get; init; } = Array.Empty(); diff --git a/src/Uno.UI.RuntimeTests.Engine.Library/Engine/TestCaseResult.cs b/src/Uno.UI.RuntimeTests.Engine.Library/Engine/TestCaseResult.cs index 3a2045d..be370cc 100644 --- a/src/Uno.UI.RuntimeTests.Engine.Library/Engine/TestCaseResult.cs +++ b/src/Uno.UI.RuntimeTests.Engine.Library/Engine/TestCaseResult.cs @@ -3,6 +3,7 @@ #if !IS_UNO_RUNTIMETEST_PROJECT #pragma warning disable #endif +#pragma warning disable CA1852 // Make class final : unnecessary breaking change using System; using System.Linq; diff --git a/src/Uno.UI.RuntimeTests.Engine.Library/Engine/UI/UnitTestsControl.CustomConsoleOutput.cs b/src/Uno.UI.RuntimeTests.Engine.Library/Engine/UI/UnitTestsControl.CustomConsoleOutput.cs index 171db78..f98e179 100644 --- a/src/Uno.UI.RuntimeTests.Engine.Library/Engine/UI/UnitTestsControl.CustomConsoleOutput.cs +++ b/src/Uno.UI.RuntimeTests.Engine.Library/Engine/UI/UnitTestsControl.CustomConsoleOutput.cs @@ -15,7 +15,7 @@ namespace Uno.UI.RuntimeTests { public sealed partial class UnitTestsControl { - private class ConsoleOutputRecorder : IDisposable + private sealed class ConsoleOutputRecorder : IDisposable { private readonly TextWriterDuplicator _duplicator; private readonly TextWriter _originalOutput; @@ -52,7 +52,7 @@ public void Dispose() } } - private class TextWriterDuplicator : TextWriter + private sealed class TextWriterDuplicator : TextWriter { private readonly TextWriter _inner; private readonly StringBuilder _accumulator = new StringBuilder(); diff --git a/src/Uno.UI.RuntimeTests.Engine.Library/Engine/UI/UnitTestsControl.TestRun.cs b/src/Uno.UI.RuntimeTests.Engine.Library/Engine/UI/UnitTestsControl.TestRun.cs index b62c3ab..71b570d 100644 --- a/src/Uno.UI.RuntimeTests.Engine.Library/Engine/UI/UnitTestsControl.TestRun.cs +++ b/src/Uno.UI.RuntimeTests.Engine.Library/Engine/UI/UnitTestsControl.TestRun.cs @@ -5,6 +5,7 @@ #if !IS_UNO_RUNTIMETEST_PROJECT #pragma warning disable #endif +#pragma warning disable CA1852 // Make class final : unnecessary breaking change public sealed partial class UnitTestsControl { diff --git a/src/Uno.UI.RuntimeTests.Engine.Library/Engine/UI/UnitTestsControl.cs b/src/Uno.UI.RuntimeTests.Engine.Library/Engine/UI/UnitTestsControl.cs index a8d7e53..fffc6ac 100644 --- a/src/Uno.UI.RuntimeTests.Engine.Library/Engine/UI/UnitTestsControl.cs +++ b/src/Uno.UI.RuntimeTests.Engine.Library/Engine/UI/UnitTestsControl.cs @@ -725,7 +725,7 @@ private async Task ExecuteTestsForInstance( .Select(method => new UnitTestMethodInfo(instance, method)) .ToArray(); - if (!tests.Any() || testClassInfo.Type == null) + if (tests.Length <= 0 || testClassInfo.Type == null) { return; } diff --git a/src/Uno.UI.RuntimeTests.Engine.Library/Library/Helpers/HotReloadHelper.DevServer.cs b/src/Uno.UI.RuntimeTests.Engine.Library/Library/Helpers/HotReloadHelper.DevServer.cs index 6de51d0..6f5f669 100644 --- a/src/Uno.UI.RuntimeTests.Engine.Library/Library/Helpers/HotReloadHelper.DevServer.cs +++ b/src/Uno.UI.RuntimeTests.Engine.Library/Library/Helpers/HotReloadHelper.DevServer.cs @@ -39,7 +39,7 @@ public UpdateFile ToMessage() }; } - private class DevServerUpdater : IFileUpdater + private sealed class DevServerUpdater : IFileUpdater { /// public async ValueTask EnsureReady(CancellationToken ct) diff --git a/src/Uno.UI.RuntimeTests.Engine.Library/Library/Helpers/HotReloadHelper.Local.cs b/src/Uno.UI.RuntimeTests.Engine.Library/Library/Helpers/HotReloadHelper.Local.cs index 6deaecd..d566b65 100644 --- a/src/Uno.UI.RuntimeTests.Engine.Library/Library/Helpers/HotReloadHelper.Local.cs +++ b/src/Uno.UI.RuntimeTests.Engine.Library/Library/Helpers/HotReloadHelper.Local.cs @@ -15,7 +15,7 @@ static partial void TryUseLocalFileUpdater() public static void UseLocalFileUpdater() => Use(new LocalFileUpdater()); - private class LocalFileUpdater : IFileUpdater + private sealed class LocalFileUpdater : IFileUpdater { /// public async global::System.Threading.Tasks.ValueTask EnsureReady(global::System.Threading.CancellationToken ct) { } diff --git a/src/Uno.UI.RuntimeTests.Engine.Library/Library/Helpers/HotReloadHelper.NotSupported.cs b/src/Uno.UI.RuntimeTests.Engine.Library/Library/Helpers/HotReloadHelper.NotSupported.cs index b462e4d..a99bc49 100644 --- a/src/Uno.UI.RuntimeTests.Engine.Library/Library/Helpers/HotReloadHelper.NotSupported.cs +++ b/src/Uno.UI.RuntimeTests.Engine.Library/Library/Helpers/HotReloadHelper.NotSupported.cs @@ -8,7 +8,7 @@ namespace Uno.UI.RuntimeTests; public static partial class HotReloadHelper { - private class NotSupported : IFileUpdater + private sealed class NotSupported : IFileUpdater { /// public global::System.Threading.Tasks.ValueTask EnsureReady(global::System.Threading.CancellationToken ct) diff --git a/src/Uno.UI.RuntimeTests.Engine.Library/Library/Helpers/HotReloadHelper.cs b/src/Uno.UI.RuntimeTests.Engine.Library/Library/Helpers/HotReloadHelper.cs index 4053fb8..5583e17 100644 --- a/src/Uno.UI.RuntimeTests.Engine.Library/Library/Helpers/HotReloadHelper.cs +++ b/src/Uno.UI.RuntimeTests.Engine.Library/Library/Helpers/HotReloadHelper.cs @@ -28,7 +28,7 @@ public partial record FileEdit(string FilePath, string OldText, string NewText) public FileEdit Revert() => this with { OldText = NewText, NewText = OldText }; } - private record RevertFileEdit(FileEdit Edition, bool WaitForMetadataUpdate) : global::System.IAsyncDisposable + private sealed record RevertFileEdit(FileEdit Edition, bool WaitForMetadataUpdate) : global::System.IAsyncDisposable { /// public async global::System.Threading.Tasks.ValueTask DisposeAsync() diff --git a/src/Uno.UI.RuntimeTests.Engine.Library/Library/Helpers/InputInjectorHelper.cs b/src/Uno.UI.RuntimeTests.Engine.Library/Library/Helpers/InputInjectorHelper.cs index a040c0f..41f8b93 100644 --- a/src/Uno.UI.RuntimeTests.Engine.Library/Library/Helpers/InputInjectorHelper.cs +++ b/src/Uno.UI.RuntimeTests.Engine.Library/Library/Helpers/InputInjectorHelper.cs @@ -112,7 +112,7 @@ public void InjectMouseInput(IEnumerable input) public void InjectMouseInput(params InjectedInputMouseInfo?[] input) => Injector.InjectMouseInput(input.Where(i => i is not null).Cast()); - private record PointerSubscription(InputInjectorHelper Injector, PointerDeviceType Previous, PointerDeviceType Current) : IDisposable + private sealed record PointerSubscription(InputInjectorHelper Injector, PointerDeviceType Previous, PointerDeviceType Current) : IDisposable { /// public void Dispose()