AnsiResponseParserTests.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. using System.Diagnostics;
  2. using System.Text;
  3. using Microsoft.VisualStudio.TestPlatform.Utilities;
  4. using Xunit.Abstractions;
  5. namespace UnitTests.ConsoleDrivers;
  6. public class AnsiResponseParserTests (ITestOutputHelper output)
  7. {
  8. AnsiResponseParser _parser = new AnsiResponseParser ();
  9. [Fact]
  10. public void TestInputProcessing ()
  11. {
  12. string ansiStream = "\x1B[<0;10;20M" + // ANSI escape for mouse move at (10, 20)
  13. "Hello" + // User types "Hello"
  14. "\x1B[0c"; // Device Attributes response (e.g., terminal identification i.e. DAR)
  15. string? response = null;
  16. int i = 0;
  17. // Imagine that we are expecting a DAR
  18. _parser.ExpectResponse ("c",(s)=> response = s);
  19. // First char is Escape which we must consume incase what follows is the DAR
  20. AssertConsumed (ansiStream, ref i); // Esc
  21. for (int c = 0; c < "[<0;10;20".Length; c++)
  22. {
  23. AssertConsumed (ansiStream, ref i);
  24. }
  25. // We see the M terminator
  26. AssertReleased (ansiStream, ref i, "\x1B[<0;10;20M");
  27. // Regular user typing
  28. for (int c = 0; c < "Hello".Length; c++)
  29. {
  30. AssertIgnored (ansiStream,"Hello"[c], ref i);
  31. }
  32. // Now we have entered the actual DAR we should be consuming these
  33. for (int c = 0; c < "\x1B[0".Length; c++)
  34. {
  35. AssertConsumed (ansiStream, ref i);
  36. }
  37. // Consume the terminator 'c' and expect this to call the above event
  38. Assert.Null (response);
  39. AssertConsumed (ansiStream, ref i);
  40. Assert.NotNull (response);
  41. Assert.Equal ("\u001b[0c", response);
  42. }
  43. [Theory]
  44. [InlineData ("\x1B[<0;10;20MHello\x1B[0c", "c", "\u001b[0c", "\x1B[<0;10;20MHello")]
  45. [InlineData ("\x1B[<1;15;25MWorld\x1B[1c", "c", "\u001b[1c", "\x1B[<1;15;25MWorld")]
  46. // Add more test cases here...
  47. public void TestInputSequences (string ansiStream, string expectedTerminator, string expectedResponse, string expectedOutput)
  48. {
  49. var swGenBatches = Stopwatch.StartNew ();
  50. int tests = 0;
  51. var permutations = GetBatchPermutations (ansiStream).ToArray ();
  52. swGenBatches.Stop ();
  53. var swRunTest = Stopwatch.StartNew ();
  54. foreach (var batchSet in permutations)
  55. {
  56. string? response = null;
  57. // Register the expected response with the given terminator
  58. _parser.ExpectResponse (expectedTerminator, s => response = s);
  59. // Process the input
  60. StringBuilder actualOutput = new StringBuilder ();
  61. foreach (var batch in batchSet)
  62. {
  63. actualOutput.Append (_parser.ProcessInput (batch));
  64. }
  65. // Assert the final output minus the expected response
  66. Assert.Equal (expectedOutput, actualOutput.ToString());
  67. Assert.Equal (expectedResponse, response);
  68. tests++;
  69. }
  70. output.WriteLine ($"Tested {tests} in {swRunTest.ElapsedMilliseconds} ms (gen batches took {swGenBatches.ElapsedMilliseconds} ms)" );
  71. }
  72. public static IEnumerable<string []> GetBatchPermutations (string input)
  73. {
  74. // Call the recursive method to generate batches
  75. return GenerateBatches (input, 0);
  76. }
  77. private static IEnumerable<string []> GenerateBatches (string input, int start)
  78. {
  79. // If we have reached the end of the string, return an empty list
  80. if (start >= input.Length)
  81. {
  82. yield return new string [0];
  83. yield break;
  84. }
  85. // Iterate over the input string to create batches
  86. for (int i = start + 1; i <= input.Length; i++)
  87. {
  88. // Take a batch from 'start' to 'i'
  89. string batch = input.Substring (start, i - start);
  90. // Recursively get batches from the remaining substring
  91. foreach (var remainingBatches in GenerateBatches (input, i))
  92. {
  93. // Combine the current batch with the remaining batches
  94. var result = new string [1 + remainingBatches.Length];
  95. result [0] = batch;
  96. Array.Copy (remainingBatches, 0, result, 1, remainingBatches.Length);
  97. yield return result;
  98. }
  99. }
  100. }
  101. private void AssertIgnored (string ansiStream,char expected, ref int i)
  102. {
  103. var c = NextChar (ansiStream, ref i);
  104. // Parser does not grab this key (i.e. driver can continue with regular operations)
  105. Assert.Equal ( c,_parser.ProcessInput (c));
  106. Assert.Equal (expected,c.Single());
  107. }
  108. private void AssertConsumed (string ansiStream, ref int i)
  109. {
  110. // Parser grabs this key
  111. var c = NextChar (ansiStream, ref i);
  112. Assert.Empty (_parser.ProcessInput(c));
  113. }
  114. private void AssertReleased (string ansiStream, ref int i, string expectedRelease)
  115. {
  116. var c = NextChar (ansiStream, ref i);
  117. // Parser realizes it has grabbed content that does not belong to an outstanding request
  118. // Parser returns false to indicate to continue
  119. Assert.Equal(expectedRelease,_parser.ProcessInput (c));
  120. }
  121. private string NextChar (string ansiStream, ref int i)
  122. {
  123. return ansiStream [i++].ToString();
  124. }
  125. }