mirror of
https://github.com/gui-cs/Terminal.Gui.git
synced 2025-12-26 15:57:56 +01:00
* Initial plan * Add IRunnable interface, Runnable base class, and RunnableSessionToken Co-authored-by: tig <585482+tig@users.noreply.github.com> * Add comprehensive parallelizable unit tests for IRunnable Co-authored-by: tig <585482+tig@users.noreply.github.com> * Add 41 more unit tests for comprehensive IRunnable coverage - Added ApplicationRunnableIntegrationTests with 29 integration tests covering Begin/End/Run lifecycle - Added RunnableEdgeCasesTests with 24 edge case and error condition tests - Tests cover event propagation, cancellation scenarios, nested runnables, result handling - Fixed App property not being set in Begin() method - Total test count increased from 23 to 64 tests for IRunnable functionality Co-authored-by: tig <585482+tig@users.noreply.github.com> * Fix parallel test failures in CI/CD - Fixed IsModal property to check RunnableSessionStack instead of just TopRunnable - Added "fake" driver parameter to Application.Init() in integration tests - Fixed Begin() to capture old IsModal value before pushing to stack - Moved App property assignment before stack operations to ensure proper state - Skipped 2 tests that use Run() with main loop (not suitable for parallel tests) - All 11,654 parallelizable tests now pass (4 skipped) Co-authored-by: tig <585482+tig@users.noreply.github.com> * Refactor Application with IRunnable and session management Modernized `Application` and `ApplicationImpl` by introducing `IRunnable` and `RunnableSessionToken` for improved session management. Deprecated legacy methods and added `Obsolete` attributes to indicate their removal. Simplified method bodies using expression-bodied members and null-coalescing assignments. Enhanced lifecycle management in `ApplicationImpl` by removing redundant code and improving `SessionStack` iteration. Introduced `IToplevelTransitionManager` to handle top-level state changes. Updated `Runnable<TResult>` to implement `IRunnable<TResult>` with lifecycle event handling for `IsRunning` and `IsModal` states. Improved result management during lifecycle transitions. Removed legacy classes like `SessionToken` and consolidated their functionality into the new constructs. Updated and expanded the test suite to cover `IRunnable` lifecycle events, `RunnableSessionToken` behavior, and integration with `Application`. Performed code cleanup, improved readability, and updated documentation with detailed remarks and examples. Added new unit tests for edge cases and lifecycle behavior. * Implement fluent API for Init/Run/Shutdown with automatic disposal - Changed Init() to return IApplication for fluent chaining - Changed Run<TRunnable>() to return IApplication (breaking change from TRunnable) - Changed Shutdown() to return object? (extracts and returns result from last Run<T>()) - Added FrameworkOwnedRunnable property to track runnable created by Run<T>() - Shutdown() automatically disposes framework-owned runnables - Created FluentExample demonstrating: Application.Create().Init().Run<ColorPickerView>().Shutdown() - Disposal semantics: framework creates → framework disposes; caller creates → caller disposes Co-authored-by: tig <585482+tig@users.noreply.github.com> * New Example: Demonstrates new Fluent API using ColorPicker Conditional compilation (`#if POST_4148`) to support both a new Fluent API and a traditional approach for running `ColorPickerView`. The Fluent API simplifies the application lifecycle with method chaining and automatic disposal, while the traditional approach retains explicit lifecycle management. Refactor `ColorPickerView` to support both approaches: - Add an `instructions` label for user guidance. - Replace `_okButton` and `_cancelButton` with local `Button` instances. - Use a new `ColorPicker` with enhanced styling options. Add a warning log for WIP issue (#4148) in `ApplicationImpl.Run.cs` to highlight limitations with non-`Toplevel` views as runnables. Update `Terminal.sln` to include the new `FluentExample` project with appropriate build configurations. Improve code readability with verbatim string literals and better alignment/indentation. * Introduce `RunnableWrapper` for making any View runnable Added the `RunnableWrapper<TView, TResult>` pattern to enable any `View` to be run as a blocking session with typed results, without requiring inheritance from `Runnable<TResult>` or implementation of `IRunnable<TResult>`. - Added `RunnableWrapperExample` project to demonstrate usage. - Introduced `ApplicationRunnableExtensions` and `ViewRunnableExtensions` for clean, type-safe APIs to run views with or without result extraction. - Updated `CodeSharingStrategy.md` to document reduced duplication using `#if POST_4148` directives. - Added `RunnableWrapper.md` with detailed documentation and examples. - Created runnable examples in `Program.cs` showcasing various use cases. - Improved maintainability by reducing code duplication by 86% and increasing shared code by 264%. - Gated all new functionality behind the `POST_4148` feature flag for backward compatibility. * Simplified `#if POST_4148` usage to reduce duplication and improve clarity. Refactored `RunnableWrapper` to use a parameterless constructor with `required` properties, ensuring type safety and better lifecycle management. Updated `AllViewsView` with new commands, improved generic handling, and enhanced logging. Refactored `ApplicationRunnableExtensions` and `ViewRunnableExtensions` for cleaner initialization and event handling. Enhanced `TestsAllViews` to handle required properties and constraints dynamically. Updated documentation to reflect new designs and provide clearer examples. Improved overall code readability, consistency, and maintainability while leveraging modern C# features. * Update docfx documentation for IRunnable architecture - Updated View.md with comprehensive IRunnable section - Interface-based architecture explanation - Fluent API patterns and examples - Disposal semantics ("whoever creates it, owns it") - Result extraction patterns - Lifecycle properties and CWP-compliant events - Marked legacy Modal Views section for clarity - Updated application.md with IRunnable deep dive - Key features and benefits - Fluent API patterns with method chaining - Disposal semantics table - Creating runnable views with examples - Lifecycle properties and events - RunnableSessionStack management - Updated IApplication interface documentation - Updated runnable-architecture-proposal.md - Marked Phase 1 as COMPLETE ✅ - Updated status to "Phase 1 Complete - Phase 2 In Progress" - Documented all implemented features - Added bonus features (fluent API, automatic disposal) - Included migration examples All documentation is now clear, concise, and complete relative to Phase 1 implementation. Co-authored-by: tig <585482+tig@users.noreply.github.com> --------- Co-authored-by: Tig <tig@users.noreply.github.com> Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: tig <585482+tig@users.noreply.github.com>
328 lines
9.0 KiB
C#
328 lines
9.0 KiB
C#
using Xunit.Abstractions;
|
|
|
|
namespace UnitTests_Parallelizable.ApplicationTests.RunnableTests;
|
|
|
|
/// <summary>
|
|
/// Tests for edge cases and error conditions in IRunnable implementation.
|
|
/// </summary>
|
|
public class RunnableEdgeCasesTests (ITestOutputHelper output)
|
|
{
|
|
private readonly ITestOutputHelper _output = output;
|
|
|
|
[Fact]
|
|
public void RunnableSessionToken_CannotDisposeWithRunnableSet ()
|
|
{
|
|
// Arrange
|
|
Runnable<int> runnable = new ();
|
|
RunnableSessionToken token = new (runnable);
|
|
|
|
// Act & Assert
|
|
var ex = Assert.Throws<InvalidOperationException> (() => token.Dispose ());
|
|
Assert.Contains ("Runnable must be null", ex.Message);
|
|
}
|
|
|
|
[Fact]
|
|
public void RunnableSessionToken_CanDisposeAfterClearingRunnable ()
|
|
{
|
|
// Arrange
|
|
Runnable<int> runnable = new ();
|
|
RunnableSessionToken token = new (runnable);
|
|
token.Runnable = null;
|
|
|
|
// Act & Assert - Should not throw
|
|
token.Dispose ();
|
|
}
|
|
|
|
[Fact]
|
|
public void Runnable_MultipleEventSubscribers_AllInvoked ()
|
|
{
|
|
// Arrange
|
|
Runnable<int> runnable = new ();
|
|
var subscriber1Called = false;
|
|
var subscriber2Called = false;
|
|
var subscriber3Called = false;
|
|
|
|
runnable.IsRunningChanging += (s, e) => subscriber1Called = true;
|
|
runnable.IsRunningChanging += (s, e) => subscriber2Called = true;
|
|
runnable.IsRunningChanging += (s, e) => subscriber3Called = true;
|
|
|
|
// Act
|
|
runnable.RaiseIsRunningChanging (false, true);
|
|
|
|
// Assert
|
|
Assert.True (subscriber1Called);
|
|
Assert.True (subscriber2Called);
|
|
Assert.True (subscriber3Called);
|
|
}
|
|
|
|
[Fact]
|
|
public void Runnable_EventSubscriber_CanCancelAfterOthers ()
|
|
{
|
|
// Arrange
|
|
Runnable<int> runnable = new ();
|
|
var subscriber1Called = false;
|
|
var subscriber2Called = false;
|
|
|
|
runnable.IsRunningChanging += (s, e) => subscriber1Called = true;
|
|
|
|
runnable.IsRunningChanging += (s, e) =>
|
|
{
|
|
subscriber2Called = true;
|
|
e.Cancel = true; // Second subscriber cancels
|
|
};
|
|
|
|
// Act
|
|
bool canceled = runnable.RaiseIsRunningChanging (false, true);
|
|
|
|
// Assert
|
|
Assert.True (subscriber1Called);
|
|
Assert.True (subscriber2Called);
|
|
Assert.True (canceled);
|
|
}
|
|
|
|
[Fact]
|
|
public void Runnable_Result_CanBeSetMultipleTimes ()
|
|
{
|
|
// Arrange
|
|
Runnable<int> runnable = new ();
|
|
|
|
// Act
|
|
runnable.Result = 1;
|
|
runnable.Result = 2;
|
|
runnable.Result = 3;
|
|
|
|
// Assert
|
|
Assert.Equal (3, runnable.Result);
|
|
}
|
|
|
|
[Fact]
|
|
public void Runnable_Result_ClearedOnMultipleStarts ()
|
|
{
|
|
// Arrange
|
|
Runnable<int> runnable = new () { Result = 42 };
|
|
|
|
// Act & Assert - First start
|
|
runnable.RaiseIsRunningChanging (false, true);
|
|
Assert.Equal (0, runnable.Result);
|
|
|
|
// Set result again
|
|
runnable.Result = 99;
|
|
Assert.Equal (99, runnable.Result);
|
|
|
|
// Second start should clear again
|
|
runnable.RaiseIsRunningChanging (false, true);
|
|
Assert.Equal (0, runnable.Result);
|
|
}
|
|
|
|
[Fact]
|
|
public void Runnable_NullableResult_DefaultsToNull ()
|
|
{
|
|
// Arrange & Act
|
|
Runnable<string> runnable = new ();
|
|
|
|
// Assert
|
|
Assert.Null (runnable.Result);
|
|
}
|
|
|
|
[Fact]
|
|
public void Runnable_NullableResult_CanBeExplicitlyNull ()
|
|
{
|
|
// Arrange
|
|
Runnable<string> runnable = new () { Result = "test" };
|
|
|
|
// Act
|
|
runnable.Result = null;
|
|
|
|
// Assert
|
|
Assert.Null (runnable.Result);
|
|
}
|
|
|
|
[Fact]
|
|
public void Runnable_ComplexType_Result ()
|
|
{
|
|
// Arrange
|
|
Runnable<ComplexResult> runnable = new ();
|
|
ComplexResult result = new () { Value = 42, Text = "test" };
|
|
|
|
// Act
|
|
runnable.Result = result;
|
|
|
|
// Assert
|
|
Assert.NotNull (runnable.Result);
|
|
Assert.Equal (42, runnable.Result.Value);
|
|
Assert.Equal ("test", runnable.Result.Text);
|
|
}
|
|
|
|
[Fact]
|
|
public void Runnable_IsRunning_WithNoApp ()
|
|
{
|
|
// Arrange
|
|
Runnable<int> runnable = new ();
|
|
|
|
// Don't set App property
|
|
|
|
// Act & Assert
|
|
Assert.False (runnable.IsRunning);
|
|
}
|
|
|
|
[Fact]
|
|
public void Runnable_IsModal_WithNoApp ()
|
|
{
|
|
// Arrange
|
|
Runnable<int> runnable = new ();
|
|
|
|
// Don't set App property
|
|
|
|
// Act & Assert
|
|
Assert.False (runnable.IsModal);
|
|
}
|
|
|
|
[Fact]
|
|
public void Runnable_VirtualMethods_CanBeOverridden ()
|
|
{
|
|
// Arrange
|
|
OverriddenRunnable runnable = new ();
|
|
|
|
// Act
|
|
bool canceledRunning = runnable.RaiseIsRunningChanging (false, true);
|
|
runnable.RaiseIsRunningChangedEvent (true);
|
|
bool canceledModal = runnable.RaiseIsModalChanging (false, true);
|
|
runnable.RaiseIsModalChangedEvent (true);
|
|
|
|
// Assert
|
|
Assert.True (runnable.OnIsRunningChangingCalled);
|
|
Assert.True (runnable.OnIsRunningChangedCalled);
|
|
Assert.True (runnable.OnIsModalChangingCalled);
|
|
Assert.True (runnable.OnIsModalChangedCalled);
|
|
}
|
|
|
|
[Fact]
|
|
public void Runnable_RequestStop_WithNoApp ()
|
|
{
|
|
// Arrange
|
|
Runnable<int> runnable = new ();
|
|
|
|
// Don't set App property
|
|
|
|
// Act & Assert - Should not throw
|
|
runnable.RequestStop ();
|
|
}
|
|
|
|
[Fact]
|
|
public void RunnableSessionToken_Constructor_RequiresRunnable ()
|
|
{
|
|
// This is implicitly tested by the constructor signature,
|
|
// but let's verify it creates with non-null runnable
|
|
|
|
// Arrange
|
|
Runnable<int> runnable = new ();
|
|
|
|
// Act
|
|
RunnableSessionToken token = new (runnable);
|
|
|
|
// Assert
|
|
Assert.NotNull (token.Runnable);
|
|
}
|
|
|
|
[Fact]
|
|
public void Runnable_EventArgs_PreservesValues ()
|
|
{
|
|
// Arrange
|
|
Runnable<int> runnable = new ();
|
|
bool? capturedOldValue = null;
|
|
bool? capturedNewValue = null;
|
|
|
|
runnable.IsRunningChanging += (s, e) =>
|
|
{
|
|
capturedOldValue = e.CurrentValue;
|
|
capturedNewValue = e.NewValue;
|
|
};
|
|
|
|
// Act
|
|
runnable.RaiseIsRunningChanging (false, true);
|
|
|
|
// Assert
|
|
Assert.NotNull (capturedOldValue);
|
|
Assert.NotNull (capturedNewValue);
|
|
Assert.False (capturedOldValue.Value);
|
|
Assert.True (capturedNewValue.Value);
|
|
}
|
|
|
|
[Fact]
|
|
public void Runnable_IsModalChanged_EventArgs_PreservesValue ()
|
|
{
|
|
// Arrange
|
|
Runnable<int> runnable = new ();
|
|
bool? capturedValue = null;
|
|
|
|
runnable.IsModalChanged += (s, e) => { capturedValue = e.Value; };
|
|
|
|
// Act
|
|
runnable.RaiseIsModalChangedEvent (true);
|
|
|
|
// Assert
|
|
Assert.NotNull (capturedValue);
|
|
Assert.True (capturedValue.Value);
|
|
}
|
|
|
|
[Fact]
|
|
public void Runnable_DifferentGenericTypes_Independent ()
|
|
{
|
|
// Arrange & Act
|
|
Runnable<int> intRunnable = new () { Result = 42 };
|
|
Runnable<string> stringRunnable = new () { Result = "test" };
|
|
Runnable<bool> boolRunnable = new () { Result = true };
|
|
|
|
// Assert
|
|
Assert.Equal (42, intRunnable.Result);
|
|
Assert.Equal ("test", stringRunnable.Result);
|
|
Assert.True (boolRunnable.Result);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Complex result type for testing.
|
|
/// </summary>
|
|
private class ComplexResult
|
|
{
|
|
public int Value { get; set; }
|
|
public string? Text { get; set; }
|
|
}
|
|
|
|
/// <summary>
|
|
/// Runnable that tracks virtual method calls.
|
|
/// </summary>
|
|
private class OverriddenRunnable : Runnable<int>
|
|
{
|
|
public bool OnIsRunningChangingCalled { get; private set; }
|
|
public bool OnIsRunningChangedCalled { get; private set; }
|
|
public bool OnIsModalChangingCalled { get; private set; }
|
|
public bool OnIsModalChangedCalled { get; private set; }
|
|
|
|
protected override bool OnIsRunningChanging (bool oldIsRunning, bool newIsRunning)
|
|
{
|
|
OnIsRunningChangingCalled = true;
|
|
|
|
return base.OnIsRunningChanging (oldIsRunning, newIsRunning);
|
|
}
|
|
|
|
protected override void OnIsRunningChanged (bool newIsRunning)
|
|
{
|
|
OnIsRunningChangedCalled = true;
|
|
base.OnIsRunningChanged (newIsRunning);
|
|
}
|
|
|
|
protected override bool OnIsModalChanging (bool oldIsModal, bool newIsModal)
|
|
{
|
|
OnIsModalChangingCalled = true;
|
|
|
|
return base.OnIsModalChanging (oldIsModal, newIsModal);
|
|
}
|
|
|
|
protected override void OnIsModalChanged (bool newIsModal)
|
|
{
|
|
OnIsModalChangedCalled = true;
|
|
base.OnIsModalChanged (newIsModal);
|
|
}
|
|
}
|
|
}
|