AnsiKeyboardParserTests.cs 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace UnitTests.ConsoleDrivers;
  7. public class AnsiKeyboardParserTests
  8. {
  9. private readonly AnsiKeyboardParser _parser = new ();
  10. public static IEnumerable<object []> GetKeyboardTestData ()
  11. {
  12. // Test data for various ANSI escape sequences and their expected Key values
  13. yield return new object [] { "\u001b[A", Key.CursorUp };
  14. yield return new object [] { "\u001b[B", Key.CursorDown };
  15. yield return new object [] { "\u001b[C", Key.CursorRight };
  16. yield return new object [] { "\u001b[D", Key.CursorLeft };
  17. // Valid inputs with modifiers
  18. yield return new object [] { "\u001b[1;2A", Key.CursorUp.WithShift };
  19. yield return new object [] { "\u001b[1;3A", Key.CursorUp.WithAlt };
  20. yield return new object [] { "\u001b[1;4A", Key.CursorUp.WithAlt.WithShift };
  21. yield return new object [] { "\u001b[1;5A", Key.CursorUp.WithCtrl };
  22. yield return new object [] { "\u001b[1;6A", Key.CursorUp.WithCtrl.WithShift };
  23. yield return new object [] { "\u001b[1;7A", Key.CursorUp.WithCtrl.WithAlt };
  24. yield return new object [] { "\u001b[1;8A", Key.CursorUp.WithCtrl.WithAlt.WithShift };
  25. yield return new object [] { "\u001b[1;2B", Key.CursorDown.WithShift };
  26. yield return new object [] { "\u001b[1;3B", Key.CursorDown.WithAlt };
  27. yield return new object [] { "\u001b[1;4B", Key.CursorDown.WithAlt.WithShift };
  28. yield return new object [] { "\u001b[1;5B", Key.CursorDown.WithCtrl };
  29. yield return new object [] { "\u001b[1;6B", Key.CursorDown.WithCtrl.WithShift };
  30. yield return new object [] { "\u001b[1;7B", Key.CursorDown.WithCtrl.WithAlt };
  31. yield return new object [] { "\u001b[1;8B", Key.CursorDown.WithCtrl.WithAlt.WithShift };
  32. yield return new object [] { "\u001b[1;2C", Key.CursorRight.WithShift };
  33. yield return new object [] { "\u001b[1;3C", Key.CursorRight.WithAlt };
  34. yield return new object [] { "\u001b[1;4C", Key.CursorRight.WithAlt.WithShift };
  35. yield return new object [] { "\u001b[1;5C", Key.CursorRight.WithCtrl };
  36. yield return new object [] { "\u001b[1;6C", Key.CursorRight.WithCtrl.WithShift };
  37. yield return new object [] { "\u001b[1;7C", Key.CursorRight.WithCtrl.WithAlt };
  38. yield return new object [] { "\u001b[1;8C", Key.CursorRight.WithCtrl.WithAlt.WithShift };
  39. yield return new object [] { "\u001b[1;2D", Key.CursorLeft.WithShift };
  40. yield return new object [] { "\u001b[1;3D", Key.CursorLeft.WithAlt };
  41. yield return new object [] { "\u001b[1;4D", Key.CursorLeft.WithAlt.WithShift };
  42. yield return new object [] { "\u001b[1;5D", Key.CursorLeft.WithCtrl };
  43. yield return new object [] { "\u001b[1;6D", Key.CursorLeft.WithCtrl.WithShift };
  44. yield return new object [] { "\u001b[1;7D", Key.CursorLeft.WithCtrl.WithAlt };
  45. yield return new object [] { "\u001b[1;8D", Key.CursorLeft.WithCtrl.WithAlt.WithShift };
  46. // Invalid inputs
  47. yield return new object [] { "\u001b[Z", null };
  48. yield return new object [] { "\u001b[invalid", null };
  49. yield return new object [] { "\u001b[1", null };
  50. yield return new object [] { "\u001b[AB", null };
  51. yield return new object [] { "\u001b[;A", null };
  52. }
  53. // Consolidated test for all keyboard events (e.g., arrow keys)
  54. [Theory]
  55. [MemberData (nameof (GetKeyboardTestData))]
  56. public void ProcessKeyboardInput_ReturnsCorrectKey (string input, Key? expectedKey)
  57. {
  58. // Act
  59. Key? result = _parser.IsKeyboard (input)?.GetKey (input);
  60. // Assert
  61. Assert.Equal (expectedKey, result); // Verify the returned key matches the expected one
  62. }
  63. }