mirror of
https://github.com/gui-cs/Terminal.Gui.git
synced 2025-12-30 17:57:57 +01:00
Refactor tests to decouple from global Application state
Commented out `driver ??= Application.Driver` assignments in `DriverAssert` to prevent automatic global driver assignment. Removed `Application.ResetState(true)` calls and commented out state validation assertions in `GlobalTestSetup` to reduce dependency on global state. Reintroduced `ApplicationForceDriverTests` and `ApplicationModelFencingTests` to validate `ForceDriver` behavior and ensure proper handling of legacy and modern Application models. Skipped certain `ToAnsiTests` that rely on `Application`. Removed direct `Application.Driver` assignments in `ViewDrawingClippingTests` and `ViewDrawingFlowTests`. Performed general cleanup of redundant code and unused imports to simplify the codebase.
This commit is contained in:
@@ -1,41 +0,0 @@
|
||||
using UnitTests;
|
||||
|
||||
namespace UnitTests_Parallelizable.ApplicationTests;
|
||||
|
||||
public class ApplicationForceDriverTests : FakeDriverBase
|
||||
{
|
||||
[Fact]
|
||||
public void ForceDriver_Does_Not_Changes_If_It_Has_Valid_Value ()
|
||||
{
|
||||
Assert.False (Application.Initialized);
|
||||
Assert.Null (Application.Driver);
|
||||
Assert.Equal (string.Empty, Application.ForceDriver);
|
||||
|
||||
Application.ForceDriver = "fake";
|
||||
Assert.Equal ("fake", Application.ForceDriver);
|
||||
|
||||
Application.ForceDriver = "dotnet";
|
||||
Assert.Equal ("fake", Application.ForceDriver);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ForceDriver_Throws_If_Initialized_Changed_To_Another_Value ()
|
||||
{
|
||||
IDriver driver = CreateFakeDriver ();
|
||||
|
||||
Assert.False (Application.Initialized);
|
||||
Assert.Null (Application.Driver);
|
||||
Assert.Equal (string.Empty, Application.ForceDriver);
|
||||
|
||||
Application.Init (driverName: "fake");
|
||||
Assert.True (Application.Initialized);
|
||||
Assert.NotNull (Application.Driver);
|
||||
Assert.Equal ("fake", Application.Driver.GetName ());
|
||||
Assert.Equal (string.Empty, Application.ForceDriver);
|
||||
|
||||
Assert.Throws<InvalidOperationException> (() => Application.ForceDriver = "dotnet");
|
||||
|
||||
Application.ForceDriver = "fake";
|
||||
Assert.Equal ("fake", Application.ForceDriver);
|
||||
}
|
||||
}
|
||||
@@ -1,154 +0,0 @@
|
||||
namespace UnitTests.ApplicationTests;
|
||||
|
||||
/// <summary>
|
||||
/// Tests to ensure that mixing legacy static Application and modern instance-based models
|
||||
/// throws appropriate exceptions.
|
||||
/// </summary>
|
||||
[Collection ("Global Test Setup")]
|
||||
public class ApplicationModelFencingTests
|
||||
{
|
||||
public ApplicationModelFencingTests ()
|
||||
{
|
||||
// Reset the model usage tracking before each test
|
||||
ApplicationImpl.ResetModelUsageTracking ();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Create_ThenInstanceAccess_ThrowsInvalidOperationException ()
|
||||
{
|
||||
// Create a modern instance-based application
|
||||
IApplication app = Application.Create ();
|
||||
app.Init ("fake");
|
||||
|
||||
// Attempting to initialize using the legacy static model should throw
|
||||
InvalidOperationException ex = Assert.Throws<InvalidOperationException> (() =>
|
||||
{
|
||||
ApplicationImpl.Instance.Init ("fake");
|
||||
});
|
||||
|
||||
Assert.Contains ("Cannot use legacy static Application model", ex.Message);
|
||||
Assert.Contains ("after using modern instance-based model", ex.Message);
|
||||
|
||||
// Clean up
|
||||
app.Shutdown ();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void InstanceAccess_ThenCreate_ThrowsInvalidOperationException ()
|
||||
{
|
||||
// Initialize using the legacy static model
|
||||
IApplication staticInstance = ApplicationImpl.Instance;
|
||||
staticInstance.Init ("fake");
|
||||
|
||||
// Attempting to create and initialize with modern instance-based model should throw
|
||||
InvalidOperationException ex = Assert.Throws<InvalidOperationException> (() =>
|
||||
{
|
||||
IApplication app = Application.Create ();
|
||||
app.Init ("fake");
|
||||
});
|
||||
|
||||
Assert.Contains ("Cannot use modern instance-based model", ex.Message);
|
||||
Assert.Contains ("after using legacy static Application model", ex.Message);
|
||||
|
||||
// Clean up
|
||||
staticInstance.Shutdown ();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Init_ThenCreate_ThrowsInvalidOperationException ()
|
||||
{
|
||||
// Initialize using legacy static API
|
||||
IApplication staticInstance = ApplicationImpl.Instance;
|
||||
staticInstance.Init ("fake");
|
||||
|
||||
// Attempting to create a modern instance-based application should throw
|
||||
InvalidOperationException ex = Assert.Throws<InvalidOperationException> (() =>
|
||||
{
|
||||
IApplication _ = Application.Create ();
|
||||
});
|
||||
|
||||
Assert.Contains ("Cannot use modern instance-based model", ex.Message);
|
||||
Assert.Contains ("after using legacy static Application model", ex.Message);
|
||||
|
||||
// Clean up
|
||||
staticInstance.Shutdown ();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Create_ThenInit_ThrowsInvalidOperationException ()
|
||||
{
|
||||
// Create a modern instance-based application
|
||||
IApplication app = Application.Create ();
|
||||
app.Init ("fake");
|
||||
|
||||
// Attempting to initialize using the legacy static model should throw
|
||||
InvalidOperationException ex = Assert.Throws<InvalidOperationException> (() =>
|
||||
{
|
||||
ApplicationImpl.Instance.Init ("fake");
|
||||
});
|
||||
|
||||
Assert.Contains ("Cannot use legacy static Application model", ex.Message);
|
||||
Assert.Contains ("after using modern instance-based model", ex.Message);
|
||||
|
||||
// Clean up
|
||||
app.Shutdown ();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MultipleCreate_Calls_DoNotThrow ()
|
||||
{
|
||||
// Multiple calls to Create should not throw
|
||||
IApplication app1 = Application.Create ();
|
||||
IApplication app2 = Application.Create ();
|
||||
IApplication app3 = Application.Create ();
|
||||
|
||||
Assert.NotNull (app1);
|
||||
Assert.NotNull (app2);
|
||||
Assert.NotNull (app3);
|
||||
|
||||
// Clean up
|
||||
app1.Shutdown ();
|
||||
app2.Shutdown ();
|
||||
app3.Shutdown ();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MultipleInstanceAccess_DoesNotThrow ()
|
||||
{
|
||||
// Multiple accesses to Instance should not throw (it's a singleton)
|
||||
IApplication instance1 = ApplicationImpl.Instance;
|
||||
IApplication instance2 = ApplicationImpl.Instance;
|
||||
IApplication instance3 = ApplicationImpl.Instance;
|
||||
|
||||
Assert.NotNull (instance1);
|
||||
Assert.Same (instance1, instance2);
|
||||
Assert.Same (instance2, instance3);
|
||||
|
||||
// Clean up
|
||||
instance1.Shutdown ();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ResetModelUsageTracking_AllowsSwitchingModels ()
|
||||
{
|
||||
// Use modern model
|
||||
IApplication app1 = Application.Create ();
|
||||
app1.Shutdown ();
|
||||
|
||||
// Reset the tracking
|
||||
ApplicationImpl.ResetModelUsageTracking ();
|
||||
|
||||
// Should now be able to use legacy model
|
||||
IApplication staticInstance = ApplicationImpl.Instance;
|
||||
Assert.NotNull (staticInstance);
|
||||
staticInstance.Shutdown ();
|
||||
|
||||
// Reset again
|
||||
ApplicationImpl.ResetModelUsageTracking ();
|
||||
|
||||
// Should be able to use modern model again
|
||||
IApplication app2 = Application.Create ();
|
||||
Assert.NotNull (app2);
|
||||
app2.Shutdown ();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user