FakeComponentFactory.cs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. using System.Collections.Concurrent;
  2. namespace Terminal.Gui.Drivers;
  3. /// <summary>
  4. /// <see cref="IComponentFactory{T}"/> implementation for fake/mock console I/O used in unit tests.
  5. /// This factory creates instances that simulate console behavior without requiring a real terminal.
  6. /// </summary>
  7. public class FakeComponentFactory : ComponentFactoryImpl<ConsoleKeyInfo>
  8. {
  9. private readonly FakeInput? _input;
  10. private readonly IOutput? _output;
  11. private readonly ISizeMonitor? _sizeMonitor;
  12. /// <summary>
  13. /// Creates a new FakeComponentFactory with optional output capture.
  14. /// </summary>
  15. /// <param name="input"></param>
  16. /// <param name="output">Optional fake output to capture what would be written to console.</param>
  17. /// <param name="sizeMonitor"></param>
  18. public FakeComponentFactory (FakeInput? input = null, IOutput? output = null, ISizeMonitor? sizeMonitor = null)
  19. {
  20. _input = input;
  21. _output = output;
  22. _sizeMonitor = sizeMonitor;
  23. }
  24. /// <inheritdoc/>
  25. public override ISizeMonitor CreateSizeMonitor (IOutput consoleOutput, IOutputBuffer outputBuffer)
  26. {
  27. return _sizeMonitor ?? new SizeMonitorImpl (consoleOutput);
  28. }
  29. /// <inheritdoc/>
  30. public override IInput<ConsoleKeyInfo> CreateInput ()
  31. {
  32. return _input ?? new FakeInput ();
  33. }
  34. /// <inheritdoc/>
  35. public override IInputProcessor CreateInputProcessor (ConcurrentQueue<ConsoleKeyInfo> inputBuffer) { return new FakeInputProcessor (inputBuffer); }
  36. /// <inheritdoc/>
  37. public override IOutput CreateOutput ()
  38. {
  39. return _output ?? new FakeOutput ();
  40. }
  41. }