Files
Terminal.Gui/Tests/UnitTests/Application/RunStateTests.cs
Copilot a0979368cb Fixes #4125. Remove legacy MainLoop infrastructure (#4343)
* Initial plan

* Phase 1: Update IConsoleDriver.Init() to return void instead of MainLoop

Co-authored-by: tig <585482+tig@users.noreply.github.com>

* Phase 2: Remove legacy MainLoop infrastructure

Co-authored-by: tig <585482+tig@users.noreply.github.com>

* Complete Phase 1 and Phase 2 - All tests pass

Co-authored-by: tig <585482+tig@users.noreply.github.com>

* Update deep dive docs to reflect MainLoop removal

Co-authored-by: tig <585482+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>
Co-authored-by: Tig <tig@users.noreply.github.com>
2025-10-26 13:39:44 -06:00

103 lines
2.4 KiB
C#

// Alias Console to MockConsole so we don't accidentally use Console
using System.Numerics;
using Terminal.Gui.Drivers;
using UnitTests;
namespace UnitTests.ApplicationTests;
/// <summary>These tests focus on Application.RunState and the various ways it can be changed.</summary>
public class RunStateTests
{
public RunStateTests ()
{
#if DEBUG_IDISPOSABLE
View.EnableDebugIDisposableAsserts = true;
View.Instances.Clear ();
RunState.Instances.Clear ();
#endif
}
[Fact]
[AutoInitShutdown]
public void Begin_End_Cleans_Up_RunState ()
{
// Test null Toplevel
Assert.Throws<ArgumentNullException> (() => Application.Begin (null));
var top = new Toplevel ();
RunState rs = Application.Begin (top);
Assert.NotNull (rs);
Application.End (rs);
Assert.NotNull (Application.Top);
// v2 does not use main loop, it uses MainLoop<T> and its internal
//Assert.NotNull (Application.MainLoop);
Assert.NotNull (Application.Driver);
top.Dispose ();
Shutdown ();
#if DEBUG_IDISPOSABLE
Assert.True (rs.WasDisposed);
#endif
Assert.Null (Application.Top);
Assert.Null (Application.Driver);
}
[Fact]
public void Dispose_Cleans_Up_RunState ()
{
var rs = new RunState (null);
Assert.NotNull (rs);
// Should not throw because Toplevel was null
rs.Dispose ();
#if DEBUG_IDISPOSABLE
Assert.True (rs.WasDisposed);
#endif
var top = new Toplevel ();
rs = new RunState (top);
Assert.NotNull (rs);
// Should throw because Toplevel was not cleaned up
Assert.Throws<InvalidOperationException> (() => rs.Dispose ());
rs.Toplevel.Dispose ();
rs.Toplevel = null;
rs.Dispose ();
#if DEBUG_IDISPOSABLE
Assert.True (rs.WasDisposed);
Assert.True (top.WasDisposed);
#endif
}
[Fact]
public void New_Creates_RunState ()
{
var rs = new RunState (null);
Assert.Null (rs.Toplevel);
var top = new Toplevel ();
rs = new RunState (top);
Assert.Equal (top, rs.Toplevel);
}
private void Shutdown ()
{
Application.Shutdown ();
#if DEBUG_IDISPOSABLE
// Validate there are no outstanding RunState-based instances left
foreach (RunState inst in RunState.Instances)
{
Assert.True (inst.WasDisposed);
}
#endif
}
}