AnsiKeyboardParserTests.cs 6.3 KB

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