AnsiKeyboardParserTests.cs 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. }
  49. // Consolidated test for all keyboard events (e.g., arrow keys)
  50. [Theory]
  51. [MemberData (nameof (GetKeyboardTestData))]
  52. public void ProcessKeyboardInput_ReturnsCorrectKey (string? input, Key? expectedKey)
  53. {
  54. // Act
  55. Key? result = _parser.IsKeyboard (input)?.GetKey (input);
  56. // Assert
  57. Assert.Equal (expectedKey, result); // Verify the returned key matches the expected one
  58. }
  59. }