mirror of
https://github.com/gui-cs/Terminal.Gui.git
synced 2025-12-27 08:17:53 +01:00
* Initial plan * Add SuperViewChanging event infrastructure and tests Co-authored-by: tig <585482+tig@users.noreply.github.com> * Refactor SuperViewChanging to follow Cancellable Work Pattern with cancellation support Co-authored-by: tig <585482+tig@users.noreply.github.com> * Remove SuperViewChangingEventArgs class and use CancelEventArgs<View?> directly Co-authored-by: tig <585482+tig@users.noreply.github.com> * Simplified SuperViewChanged * Refactor SuperView change to use CWP and event args Refactored the handling of SuperView changes in the View hierarchy to use the Cancellable Work Pattern (CWP) and standardized event argument types. The SuperView property is now settable only via an internal SetSuperView method, which leverages CWP for property changes and cancellation. - Updated OnSuperViewChanging to accept ValueChangingEventArgs<View?>. - SuperViewChanging event now uses EventHandler<ValueChangingEventArgs<View?>>; cancellation is via e.Handled = true. - OnSuperViewChanged and SuperViewChanged now use ValueChangedEventArgs<View?>. - All SuperView assignments in Add, Remove, and RemoveAll now use SetSuperView, which returns a bool for cancellation. - CWPPropertyHelper.ChangeProperty now accepts a sender parameter, passed to event handlers. - All property changes in View now pass this as sender to ChangeProperty. - Updated event handler signatures and overrides in MenuBar, StatusBar, TextField, and TextView. - Updated unit tests to use new event args and cancellation pattern. - Minor code cleanups and improved comments. These changes modernize and standardize property change handling, improving API consistency, extensibility, and testability. * Refactor subview removal and event arg types Removed redundant index check when removing subviews in View.cs, simplifying the cleanup loop. Updated TextField.OnSuperViewChanged to use a non-nullable ValueChangedEventArgs<View> parameter, enforcing stricter type safety. --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: tig <585482+tig@users.noreply.github.com> Co-authored-by: Tig <tig@users.noreply.github.com>
UnitTests.Parallelizable
This project contains unit tests that can run in parallel without interference. Tests here must not depend on global state or static Application infrastructure.
Important Notes
- Many tests in
UnitTestsblindly use the the legacy model they don't actually need to - These tests CAN be rewritten to remove unnecessary dependencies and migrated here
- Many tests APPEAR to be integration tests but are just poorly written and cover multiple surface areas - these can be split into focused unit tests
- When in doubt, analyze if the test truly needs global state or can be refactored
Example Migrations
Simple Property Test (no changes needed)
// Before (in UnitTests)
[Fact]
public void Constructor_Sets_Defaults ()
{
var view = new Button ();
Assert.Empty (view.Text);
}
// After (in UnitTests.Parallelizable) - just move it!
[Fact]
public void Constructor_Sets_Defaults ()
{
var view = new Button ();
Assert.Empty (view.Text);
}
Remove Unnecessary [SetupFakeApplication]
// Before (in UnitTests)
[Fact]
[SetupFakeApplication]
public void Event_Fires_When_Property_Changes ()
{
var view = new Button ();
var fired = false;
view.TextChanged += (s, e) => fired = true;
view.Text = "Hello";
Assert.True (fired);
}
// After (in UnitTests.Parallelizable) - remove attribute!
[Fact]
public void Event_Fires_When_Property_Changes ()
{
var view = new Button ();
var fired = false;
view.TextChanged += (s, e) => fired = true;
view.Text = "Hello";
Assert.True (fired);
}
Replace Application.Begin with View Initialization
// Before (in UnitTests)
[Fact]
[AutoInitShutdown]
public void Focus_Test ()
{
var view = new Button ();
var top = new Runnable ();
top.Add (view);
Application.Begin (top);
view.SetFocus ();
Assert.True (view.HasFocus);
top.Dispose ();
}
// After (in UnitTests.Parallelizable) - use BeginInit/EndInit!
[Fact]
public void Focus_Test ()
{
var superView = new View ();
var view = new Button ();
superView.Add (view);
superView.BeginInit ();
superView.EndInit ();
view.SetFocus ();
Assert.True (view.HasFocus);
}
Running Tests
Tests in this project run in parallel automatically. To run them:
dotnet test Tests/UnitTestsParallelizable/UnitTests.Parallelizable.csproj