| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535 |
- #nullable enable
- using System.Collections.Concurrent;
- using Xunit.Abstractions;
- using Xunit.Sdk;
- namespace DriverTests;
- /// <summary>
- /// Low-level tests for IInput and IOutput implementations across all drivers.
- /// These tests are designed to fail with good error messages when run in environments
- /// without a real terminal (like GitHub Actions).
- /// </summary>
- public class IInputOutputTests (ITestOutputHelper output)
- {
- private readonly ITestOutputHelper _output = output;
- #region DotNet Driver Tests
- [Fact]
- [Trait ("Category", "LowLevelDriver")]
- public void NetInput_Constructor_DoesNotThrow_WhenNoTerminalAvailable ()
- {
- // Arrange & Act
- Exception? exception = Record.Exception (() =>
- {
- using var input = new NetInput ();
- _output.WriteLine ("NetInput created successfully");
- });
- // Assert
- if (exception != null)
- {
- _output.WriteLine ($"FAILED: NetInput constructor threw: {exception.GetType ().Name}: {exception.Message}");
- _output.WriteLine ($"Stack trace: {exception.StackTrace}");
- }
- Assert.Null (exception);
- }
- [Fact]
- [Trait ("Category", "LowLevelDriver")]
- public void NetInput_Peek_DoesNotThrow_WhenNoTerminalAvailable ()
- {
- // Arrange
- using var input = new NetInput ();
- // Act
- Exception? exception = Record.Exception (() =>
- {
- bool hasInput = input.Peek ();
- _output.WriteLine ($"NetInput.Peek() returned: {hasInput}");
- });
- // Assert
- if (exception != null)
- {
- _output.WriteLine ($"FAILED: NetInput.Peek() threw: {exception.GetType ().Name}: {exception.Message}");
- _output.WriteLine ($"Stack trace: {exception.StackTrace}");
- }
- Assert.Null (exception);
- }
- [Fact]
- [Trait ("Category", "LowLevelDriver")]
- public void NetInput_Read_DoesNotThrow_WhenNoTerminalAvailable ()
- {
- // Arrange
- using var input = new NetInput ();
- // Act
- Exception? exception = Record.Exception (() =>
- {
- List<ConsoleKeyInfo> items = input.Read ().ToList ();
- _output.WriteLine ($"NetInput.Read() returned {items.Count} items");
- });
- // Assert
- if (exception != null)
- {
- _output.WriteLine ($"FAILED: NetInput.Read() threw: {exception.GetType ().Name}: {exception.Message}");
- _output.WriteLine ($"Stack trace: {exception.StackTrace}");
- }
- Assert.Null (exception);
- }
- [Fact]
- [Trait ("Category", "LowLevelDriver")]
- public void NetInput_Dispose_DoesNotThrow_WhenNoTerminalAvailable ()
- {
- // Arrange
- var input = new NetInput ();
- // Act
- Exception? exception = Record.Exception (() =>
- {
- input.Dispose ();
- _output.WriteLine ("NetInput disposed successfully");
- });
- // Assert
- if (exception != null)
- {
- _output.WriteLine ($"FAILED: NetInput.Dispose() threw: {exception.GetType ().Name}: {exception.Message}");
- _output.WriteLine ($"Stack trace: {exception.StackTrace}");
- }
- Assert.Null (exception);
- }
- [Fact]
- [Trait ("Category", "LowLevelDriver")]
- public void NetOutput_Constructor_DoesNotThrow_WhenNoTerminalAvailable ()
- {
- // Arrange & Act
- Exception? exception = Record.Exception (() =>
- {
- using var output = new NetOutput ();
- _output.WriteLine ("NetOutput created successfully");
- });
- // Assert
- if (exception != null)
- {
- _output.WriteLine ($"FAILED: NetOutput constructor threw: {exception.GetType ().Name}: {exception.Message}");
- _output.WriteLine ($"Stack trace: {exception.StackTrace}");
- }
- Assert.Null (exception);
- }
- [Fact]
- [Trait ("Category", "LowLevelDriver")]
- public void NetOutput_GetSize_ReturnsDefaultSize_WhenNoTerminalAvailable ()
- {
- // Arrange
- using var output = new NetOutput ();
- // Act
- Size size = default;
- Exception? exception = Record.Exception (() =>
- {
- size = output.GetSize ();
- _output.WriteLine ($"NetOutput.GetSize() returned: {size.Width}x{size.Height}");
- });
- // Assert
- if (exception != null)
- {
- _output.WriteLine ($"FAILED: NetOutput.GetSize() threw: {exception.GetType ().Name}: {exception.Message}");
- _output.WriteLine ($"Stack trace: {exception.StackTrace}");
- }
- Assert.Null (exception);
- Assert.True (size.Width > 0, "Width should be > 0");
- Assert.True (size.Height > 0, "Height should be > 0");
- }
- [Fact]
- [Trait ("Category", "LowLevelDriver")]
- public void NetOutput_Write_DoesNotThrow_WhenNoTerminalAvailable ()
- {
- // Arrange
- using var output = new NetOutput ();
- // Act
- Exception? exception = Record.Exception (() =>
- {
- ReadOnlySpan<char> text = "Test".AsSpan ();
- output.Write (text);
- _output.WriteLine ("NetOutput.Write() succeeded");
- });
- // Assert
- if (exception != null)
- {
- _output.WriteLine ($"FAILED: NetOutput.Write() threw: {exception.GetType ().Name}: {exception.Message}");
- _output.WriteLine ($"Stack trace: {exception.StackTrace}");
- }
- Assert.Null (exception);
- }
- [Fact]
- [Trait ("Category", "LowLevelDriver")]
- public void NetOutput_SetCursorPosition_DoesNotThrow_WhenNoTerminalAvailable ()
- {
- // Arrange
- using var output = new NetOutput ();
- // Act
- Exception? exception = Record.Exception (() =>
- {
- output.SetCursorPosition (0, 0);
- _output.WriteLine ("NetOutput.SetCursorPosition() succeeded");
- });
- // Assert
- if (exception != null)
- {
- _output.WriteLine ($"FAILED: NetOutput.SetCursorPosition() threw: {exception.GetType ().Name}: {exception.Message}");
- _output.WriteLine ($"Stack trace: {exception.StackTrace}");
- }
- Assert.Null (exception);
- }
- [Fact]
- [Trait ("Category", "LowLevelDriver")]
- public void NetInputProcessor_Constructor_DoesNotThrow ()
- {
- // Arrange & Act
- Exception? exception = Record.Exception (() =>
- {
- ConcurrentQueue<ConsoleKeyInfo> queue = new ();
- var processor = new NetInputProcessor (queue);
- _output.WriteLine ("NetInputProcessor created successfully");
- });
- // Assert
- if (exception != null)
- {
- _output.WriteLine ($"FAILED: NetInputProcessor constructor threw: {exception.GetType ().Name}: {exception.Message}");
- _output.WriteLine ($"Stack trace: {exception.StackTrace}");
- }
- Assert.Null (exception);
- }
- #endregion
- #region Unix Driver Tests
- [Fact]
- [Trait ("Category", "LowLevelDriver")]
- [Trait ("Platform", "Unix")]
- public void UnixInput_Constructor_DoesNotThrow_WhenNoTerminalAvailable ()
- {
- // Arrange & Act
- Exception? exception = Record.Exception (() =>
- {
- try
- {
- using var input = new UnixInput ();
- _output.WriteLine ("UnixInput created successfully");
- }
- catch (Exception ex)
- {
- _output.WriteLine ($"Expected failure on non-terminal: {ex.Message}");
- throw new XunitException (
- $"UnixInput failed in non-terminal environment: {ex.Message}\nThis is expected in GitHub Actions. The driver should detect this and handle gracefully.");
- }
- });
- // Assert
- if (exception != null && !(exception is XunitException))
- {
- _output.WriteLine ($"FAILED: UnixInput constructor threw: {exception.GetType ().Name}: {exception.Message}");
- _output.WriteLine ($"Stack trace: {exception.StackTrace}");
- }
- }
- [Fact]
- [Trait ("Category", "LowLevelDriver")]
- [Trait ("Platform", "Unix")]
- public void UnixOutput_Constructor_DoesNotThrow_WhenNoTerminalAvailable ()
- {
- if (OperatingSystem.IsWindows ())
- {
- _output.WriteLine ("Skipping Unix test on Windows");
- return;
- }
- // Arrange & Act
- Exception? exception = Record.Exception (() =>
- {
- using var output = new UnixOutput ();
- _output.WriteLine ("UnixOutput created successfully");
- });
- // Assert
- if (exception != null)
- {
- _output.WriteLine ($"FAILED: UnixOutput constructor threw: {exception.GetType ().Name}: {exception.Message}");
- _output.WriteLine ($"Stack trace: {exception.StackTrace}");
- }
- Assert.Null (exception);
- }
- [Fact]
- [Trait ("Category", "LowLevelDriver")]
- [Trait ("Platform", "Unix")]
- public void UnixOutput_GetSize_ReturnsDefaultSize_WhenNoTerminalAvailable ()
- {
- if (OperatingSystem.IsWindows ())
- {
- _output.WriteLine ("Skipping Unix test on Windows");
- return;
- }
- // Arrange
- using var output = new UnixOutput ();
- // Act
- Size size = default;
- Exception? exception = Record.Exception (() =>
- {
- size = output.GetSize ();
- _output.WriteLine ($"UnixOutput.GetSize() returned: {size.Width}x{size.Height}");
- });
- // Assert
- Assert.Null (exception);
- Assert.Equal (80, size.Width);
- Assert.Equal (25, size.Height);
- }
- #endregion
- #region Windows Driver Tests
- [Fact]
- [Trait ("Category", "LowLevelDriver")]
- [Trait ("Platform", "Windows")]
- public void WindowsInput_Constructor_DoesNotThrow_WhenNoTerminalAvailable ()
- {
- if (!OperatingSystem.IsWindows ())
- {
- _output.WriteLine ("Skipping Windows test on non-Windows");
- return;
- }
- // Arrange & Act
- Exception? exception = Record.Exception (() =>
- {
- using var input = new WindowsInput ();
- _output.WriteLine ("WindowsInput created successfully");
- });
- // Assert
- if (exception != null)
- {
- _output.WriteLine ($"FAILED: WindowsInput constructor threw: {exception.GetType ().Name}: {exception.Message}");
- _output.WriteLine ($"Stack trace: {exception.StackTrace}");
- }
- Assert.Null (exception);
- }
- [Fact]
- [Trait ("Category", "LowLevelDriver")]
- [Trait ("Platform", "Windows")]
- public void WindowsOutput_Constructor_DoesNotThrow_WhenNoTerminalAvailable ()
- {
- if (!OperatingSystem.IsWindows ())
- {
- _output.WriteLine ("Skipping Windows test on non-Windows");
- return;
- }
- // Arrange & Act
- Exception? exception = Record.Exception (() =>
- {
- try
- {
- using var output = new WindowsOutput ();
- _output.WriteLine ("WindowsOutput created successfully");
- }
- catch (Exception ex)
- {
- _output.WriteLine ($"WindowsOutput threw during construction: {ex.GetType ().Name}: {ex.Message}");
- throw;
- }
- });
- // Assert
- if (exception != null)
- {
- _output.WriteLine ($"FAILED: WindowsOutput constructor threw: {exception.GetType ().Name}: {exception.Message}");
- _output.WriteLine ($"Stack trace: {exception.StackTrace}");
- }
- Assert.Null (exception);
- }
- #endregion
- #region Fake Driver Tests
- [Fact]
- [Trait ("Category", "LowLevelDriver")]
- public void FakeInput_Constructor_DoesNotThrow ()
- {
- // Arrange & Act
- Exception? exception = Record.Exception (() =>
- {
- using var input = new FakeInput ();
- _output.WriteLine ("FakeInput created successfully");
- });
- // Assert
- Assert.Null (exception);
- }
- [Fact]
- [Trait ("Category", "LowLevelDriver")]
- public void FakeOutput_Constructor_DoesNotThrow ()
- {
- // Arrange & Act
- Exception? exception = Record.Exception (() =>
- {
- using var output = new FakeOutput ();
- _output.WriteLine ("FakeOutput created successfully");
- });
- // Assert
- Assert.Null (exception);
- }
- [Fact]
- [Trait ("Category", "LowLevelDriver")]
- public void FakeOutput_GetSize_ReturnsExpectedSize ()
- {
- // Arrange
- using var output = new FakeOutput ();
- // Act
- Size size = output.GetSize ();
- _output.WriteLine ($"FakeOutput.GetSize() returned: {size.Width}x{size.Height}");
- // Assert
- Assert.True (size.Width > 0);
- Assert.True (size.Height > 0);
- }
- #endregion
- #region Component Factory Tests
- [Fact]
- [Trait ("Category", "LowLevelDriver")]
- public void NetComponentFactory_CreateInput_DoesNotThrow ()
- {
- // Arrange
- var factory = new NetComponentFactory ();
- // Act
- Exception? exception = Record.Exception (() =>
- {
- using IInput<ConsoleKeyInfo> input = factory.CreateInput ();
- _output.WriteLine ("NetComponentFactory.CreateInput() succeeded");
- });
- // Assert
- if (exception != null)
- {
- _output.WriteLine ($"FAILED: NetComponentFactory.CreateInput() threw: {exception.GetType ().Name}: {exception.Message}");
- _output.WriteLine ($"Stack trace: {exception.StackTrace}");
- }
- Assert.Null (exception);
- }
- [Fact]
- [Trait ("Category", "LowLevelDriver")]
- public void NetComponentFactory_CreateOutput_DoesNotThrow ()
- {
- // Arrange
- var factory = new NetComponentFactory ();
- // Act
- Exception? exception = Record.Exception (() =>
- {
- using IOutput output = factory.CreateOutput ();
- _output.WriteLine ("NetComponentFactory.CreateOutput() succeeded");
- });
- // Assert
- if (exception != null)
- {
- _output.WriteLine ($"FAILED: NetComponentFactory.CreateOutput() threw: {exception.GetType ().Name}: {exception.Message}");
- _output.WriteLine ($"Stack trace: {exception.StackTrace}");
- }
- Assert.Null (exception);
- }
- [Fact]
- [Trait ("Category", "LowLevelDriver")]
- public void FakeComponentFactory_CreateInput_DoesNotThrow ()
- {
- // Arrange
- var factory = new FakeComponentFactory ();
- // Act
- Exception? exception = Record.Exception (() =>
- {
- using IInput<ConsoleKeyInfo> input = factory.CreateInput ();
- _output.WriteLine ("FakeComponentFactory.CreateInput() succeeded");
- });
- // Assert
- Assert.Null (exception);
- }
- [Fact]
- [Trait ("Category", "LowLevelDriver")]
- public void FakeComponentFactory_CreateOutput_DoesNotThrow ()
- {
- // Arrange
- var factory = new FakeComponentFactory ();
- // Act
- Exception? exception = Record.Exception (() =>
- {
- using IOutput output = factory.CreateOutput ();
- _output.WriteLine ("FakeComponentFactory.CreateOutput() succeeded");
- });
- // Assert
- Assert.Null (exception);
- }
- #endregion
- }
|