AnsiKeyboardParserTests.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #nullable enable
  2. namespace UnitTests_Parallelizable.DriverTests;
  3. public class AnsiKeyboardParserTests
  4. {
  5. private readonly AnsiKeyboardParser _parser = new ();
  6. public static IEnumerable<object? []> GetKeyboardTestData ()
  7. {
  8. // Test data for various ANSI escape sequences and their expected Key values
  9. yield return ["\u001b[A", Key.CursorUp];
  10. yield return ["\u001b[B", Key.CursorDown];
  11. yield return ["\u001b[C", Key.CursorRight];
  12. yield return ["\u001b[D", Key.CursorLeft];
  13. // Valid inputs with modifiers
  14. yield return ["\u001b[1;2A", Key.CursorUp.WithShift];
  15. yield return ["\u001b[1;3A", Key.CursorUp.WithAlt];
  16. yield return ["\u001b[1;4A", Key.CursorUp.WithAlt.WithShift];
  17. yield return ["\u001b[1;5A", Key.CursorUp.WithCtrl];
  18. yield return ["\u001b[1;6A", Key.CursorUp.WithCtrl.WithShift];
  19. yield return ["\u001b[1;7A", Key.CursorUp.WithCtrl.WithAlt];
  20. yield return ["\u001b[1;8A", Key.CursorUp.WithCtrl.WithAlt.WithShift];
  21. yield return ["\u001b[1;2B", Key.CursorDown.WithShift];
  22. yield return ["\u001b[1;3B", Key.CursorDown.WithAlt];
  23. yield return ["\u001b[1;4B", Key.CursorDown.WithAlt.WithShift];
  24. yield return ["\u001b[1;5B", Key.CursorDown.WithCtrl];
  25. yield return ["\u001b[1;6B", Key.CursorDown.WithCtrl.WithShift];
  26. yield return ["\u001b[1;7B", Key.CursorDown.WithCtrl.WithAlt];
  27. yield return ["\u001b[1;8B", Key.CursorDown.WithCtrl.WithAlt.WithShift];
  28. yield return ["\u001b[1;2C", Key.CursorRight.WithShift];
  29. yield return ["\u001b[1;3C", Key.CursorRight.WithAlt];
  30. yield return ["\u001b[1;4C", Key.CursorRight.WithAlt.WithShift];
  31. yield return ["\u001b[1;5C", Key.CursorRight.WithCtrl];
  32. yield return ["\u001b[1;6C", Key.CursorRight.WithCtrl.WithShift];
  33. yield return ["\u001b[1;7C", Key.CursorRight.WithCtrl.WithAlt];
  34. yield return ["\u001b[1;8C", Key.CursorRight.WithCtrl.WithAlt.WithShift];
  35. yield return ["\u001b[1;2D", Key.CursorLeft.WithShift];
  36. yield return ["\u001b[1;3D", Key.CursorLeft.WithAlt];
  37. yield return ["\u001b[1;4D", Key.CursorLeft.WithAlt.WithShift];
  38. yield return ["\u001b[1;5D", Key.CursorLeft.WithCtrl];
  39. yield return ["\u001b[1;6D", Key.CursorLeft.WithCtrl.WithShift];
  40. yield return ["\u001b[1;7D", Key.CursorLeft.WithCtrl.WithAlt];
  41. yield return ["\u001b[1;8D", Key.CursorLeft.WithCtrl.WithAlt.WithShift];
  42. // Invalid inputs
  43. yield return ["\u001b[Z", null!];
  44. yield return ["\u001b[invalid", null!];
  45. yield return ["\u001b[1", null!];
  46. yield return ["\u001b[AB", null!];
  47. yield return ["\u001b[;A", null!];
  48. // Test data for various ANSI escape sequences and their expected Key values
  49. yield return ["\u001b[3;5~", Key.Delete.WithCtrl];
  50. // Additional special keys
  51. yield return ["\u001b[H", Key.Home];
  52. yield return ["\u001b[F", Key.End];
  53. yield return ["\u001b[2~", Key.InsertChar];
  54. yield return ["\u001b[5~", Key.PageUp];
  55. yield return ["\u001b[6~", Key.PageDown];
  56. // Home, End with modifiers
  57. yield return ["\u001b[1;2H", Key.Home.WithShift];
  58. yield return ["\u001b[1;3H", Key.Home.WithAlt];
  59. yield return ["\u001b[1;5F", Key.End.WithCtrl];
  60. // Insert with modifiers
  61. yield return ["\u001b[2;2~", Key.InsertChar.WithShift];
  62. yield return ["\u001b[2;3~", Key.InsertChar.WithAlt];
  63. yield return ["\u001b[2;5~", Key.InsertChar.WithCtrl];
  64. // PageUp/PageDown with modifiers
  65. yield return ["\u001b[5;2~", Key.PageUp.WithShift];
  66. yield return ["\u001b[6;3~", Key.PageDown.WithAlt];
  67. yield return ["\u001b[6;5~", Key.PageDown.WithCtrl];
  68. // Function keys F1-F4 (common ESC O sequences)
  69. yield return ["\u001bOP", Key.F1];
  70. yield return ["\u001bOQ", Key.F2];
  71. yield return ["\u001bOR", Key.F3];
  72. yield return ["\u001bOS", Key.F4];
  73. // Extended function keys F1-F12 with CSI sequences
  74. yield return ["\u001b[11~", Key.F1];
  75. yield return ["\u001b[12~", Key.F2];
  76. yield return ["\u001b[13~", Key.F3];
  77. yield return ["\u001b[14~", Key.F4];
  78. yield return ["\u001b[15~", Key.F5];
  79. yield return ["\u001b[17~", Key.F6];
  80. yield return ["\u001b[18~", Key.F7];
  81. yield return ["\u001b[19~", Key.F8];
  82. yield return ["\u001b[20~", Key.F9];
  83. yield return ["\u001b[21~", Key.F10];
  84. yield return ["\u001b[23~", Key.F11];
  85. yield return ["\u001b[24~", Key.F12];
  86. // Function keys with modifiers
  87. yield return ["\u001b[1;2P", Key.F1.WithShift];
  88. yield return ["\u001b[1;3Q", Key.F2.WithAlt];
  89. yield return ["\u001b[1;5R", Key.F3.WithCtrl];
  90. // Keys with Alt modifiers
  91. yield return ["\u001ba", Key.A.WithAlt, true];
  92. yield return ["\u001bA", Key.A.WithShift.WithAlt, true];
  93. yield return ["\u001b1", Key.D1.WithAlt, true];
  94. // Keys with Ctrl and Alt modifiers
  95. yield return ["\u001b\u0001", Key.A.WithCtrl.WithAlt, true];
  96. yield return ["\u001b\u001a", Key.Z.WithCtrl.WithAlt, true];
  97. // Keys with Ctrl, Shift and Alt modifiers
  98. yield return ["\u001b\u001f", Key.D7.WithCtrl.WithShift.WithAlt, true];
  99. }
  100. // Consolidated test for all keyboard events (e.g., arrow keys)
  101. [Theory]
  102. [MemberData (nameof (GetKeyboardTestData))]
  103. public void ProcessKeyboardInput_ReturnsCorrectKey (string? input, Key? expectedKey, bool isLastMinute = false)
  104. {
  105. // Act
  106. Key? result = _parser.IsKeyboard (input, isLastMinute)?.GetKey (input);
  107. // Assert
  108. Assert.Equal (expectedKey, result); // Verify the returned key matches the expected one
  109. }
  110. }