FakeComponentFactory.cs 1.6 KB

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