MockConsoleDriver.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. #nullable enable
  2. using System.Text;
  3. /// <summary>
  4. /// Minimal mock <see cref="IConsoleDriver"/> implementation for parallelizable unit tests.
  5. /// This is a lightweight stub that provides just enough driver functionality for tests that need
  6. /// to set driver properties but don't need full Application initialization or I/O simulation.
  7. /// </summary>
  8. /// <remarks>
  9. /// <para>
  10. /// <strong>Purpose:</strong> This mock is specifically designed for tests in the
  11. /// UnitTestsParallelizable project that need to test view behavior in isolation without
  12. /// the overhead of full driver initialization. It's intentionally minimal and thread-safe.
  13. /// </para>
  14. /// <para>
  15. /// <strong>Not a replacement for FakeDriver:</strong> For tests that need full Application
  16. /// initialization, event firing, input simulation, or output verification, use
  17. /// <see cref="FakeDriver"/> in the UnitTests project with <see cref="AutoInitShutdownAttribute"/>.
  18. /// </para>
  19. /// <para>
  20. /// <strong>Thread Safety:</strong> Each test creates its own instance, making it safe for
  21. /// parallel test execution.
  22. /// </para>
  23. /// </remarks>
  24. internal class MockConsoleDriver : IConsoleDriver
  25. {
  26. public event EventHandler<Attribute>? AttributeSet;
  27. private IClipboard? _clipboard;
  28. private Rectangle _screen;
  29. private Region? _clip;
  30. private int _col;
  31. private int _cols;
  32. private Cell [,]? _contents;
  33. private int _left;
  34. private int _row;
  35. private int _rows;
  36. private int _top;
  37. private bool _supportsTrueColor;
  38. private bool _force16Colors;
  39. private Attribute _currentAttribute;
  40. /// <inheritdoc />
  41. public IClipboard? Clipboard => _clipboard;
  42. /// <inheritdoc />
  43. public Rectangle Screen => _screen;
  44. /// <inheritdoc />
  45. public Region? Clip
  46. {
  47. get => _clip;
  48. set => _clip = value;
  49. }
  50. /// <inheritdoc />
  51. public int Col => _col;
  52. /// <inheritdoc />
  53. public int Cols
  54. {
  55. get => _cols;
  56. set => _cols = value;
  57. }
  58. /// <inheritdoc />
  59. public Cell [,]? Contents
  60. {
  61. get => _contents;
  62. set => _contents = value;
  63. }
  64. /// <inheritdoc />
  65. public int Left
  66. {
  67. get => _left;
  68. set => _left = value;
  69. }
  70. /// <inheritdoc />
  71. public int Row => _row;
  72. /// <inheritdoc />
  73. public int Rows
  74. {
  75. get => _rows;
  76. set => _rows = value;
  77. }
  78. /// <inheritdoc />
  79. public int Top
  80. {
  81. get => _top;
  82. set => _top = value;
  83. }
  84. /// <inheritdoc />
  85. public bool SupportsTrueColor => _supportsTrueColor;
  86. /// <inheritdoc />
  87. public bool Force16Colors
  88. {
  89. get => _force16Colors;
  90. set => _force16Colors = value;
  91. }
  92. /// <inheritdoc />
  93. public Attribute CurrentAttribute
  94. {
  95. get => _currentAttribute;
  96. set => _currentAttribute = value;
  97. }
  98. /// <inheritdoc />
  99. public string GetVersionInfo () { return string.Empty; }
  100. /// <inheritdoc />
  101. public void WriteRaw (string ansi) { }
  102. /// <inheritdoc />
  103. public bool IsRuneSupported (Rune rune) { return true; }
  104. /// <inheritdoc />
  105. public bool IsValidLocation (Rune rune, int col, int row) { return true; }
  106. /// <inheritdoc />
  107. public void Move (int col, int row)
  108. {
  109. _col = col;
  110. _row = row;
  111. }
  112. /// <inheritdoc />
  113. public void AddRune (Rune rune) { }
  114. /// <inheritdoc />
  115. public void AddRune (char c) { }
  116. /// <inheritdoc />
  117. public void AddStr (string str) { }
  118. /// <inheritdoc />
  119. public void ClearContents () { }
  120. /// <inheritdoc />
  121. public event EventHandler<EventArgs>? ClearedContents;
  122. /// <inheritdoc />
  123. public void FillRect (Rectangle rect, Rune rune = default) { }
  124. /// <inheritdoc />
  125. public void FillRect (Rectangle rect, char c) { }
  126. /// <inheritdoc />
  127. public bool GetCursorVisibility (out CursorVisibility visibility)
  128. {
  129. visibility = CursorVisibility.Invisible;
  130. return false;
  131. }
  132. /// <inheritdoc />
  133. public void Refresh () { }
  134. /// <inheritdoc />
  135. public bool SetCursorVisibility (CursorVisibility visibility) { throw new NotImplementedException (); }
  136. /// <inheritdoc />
  137. public event EventHandler<SizeChangedEventArgs>? SizeChanged;
  138. /// <inheritdoc />
  139. public void Suspend () { }
  140. /// <inheritdoc />
  141. public void UpdateCursor () {}
  142. /// <inheritdoc />
  143. public void Init () { }
  144. /// <inheritdoc />
  145. public void End () { }
  146. /// <inheritdoc />
  147. /// <inheritdoc />
  148. public Attribute SetAttribute (Attribute c)
  149. {
  150. Attribute oldAttribute = _currentAttribute;
  151. _currentAttribute = c;
  152. AttributeSet?.Invoke (this, c);
  153. return oldAttribute;
  154. }
  155. /// <inheritdoc />
  156. public Attribute GetAttribute ()
  157. {
  158. return _currentAttribute;
  159. }
  160. /// <inheritdoc />
  161. public Attribute MakeColor (in Color foreground, in Color background) { throw new NotImplementedException (); }
  162. /// <inheritdoc />
  163. public event EventHandler<MouseEventArgs>? MouseEvent;
  164. /// <inheritdoc />
  165. public event EventHandler<Key>? KeyDown;
  166. /// <inheritdoc />
  167. public event EventHandler<Key>? KeyUp;
  168. /// <inheritdoc />
  169. public void SendKeys (char keyChar, ConsoleKey key, bool shift, bool alt, bool ctrl) { throw new NotImplementedException (); }
  170. /// <inheritdoc />
  171. public void QueueAnsiRequest (AnsiEscapeSequenceRequest request) { throw new NotImplementedException (); }
  172. /// <inheritdoc />
  173. public AnsiRequestScheduler GetRequestScheduler () { throw new NotImplementedException (); }
  174. }