AnsiResponseParserTests.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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<int> _parser = new AnsiResponseParser<int> ();
  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 ("\x1B[0c", response);
  42. }
  43. [Theory]
  44. [InlineData ("\x1B[<0;10;20MHi\x1B[0c", "c", "\x1B[0c", "\x1B[<0;10;20MHi")]
  45. [InlineData ("\x1B[<1;15;25MYou\x1B[1c", "c", "\x1B[1c", "\x1B[<1;15;25MYou")]
  46. [InlineData ("\x1B[0cHi\x1B[0c", "c", "\x1B[0c", "Hi\x1B[0c")]
  47. [InlineData ("\x1B[<0;0;0MHe\x1B[3c", "c", "\x1B[3c", "\x1B[<0;0;0MHe")]
  48. [InlineData ("\x1B[<0;1;2Da\x1B[0c\x1B[1c", "c", "\x1B[0c", "\x1B[<0;1;2Da\x1B[1c")]
  49. [InlineData ("\x1B[1;1M\x1B[3cAn", "c", "\x1B[3c", "\x1B[1;1MAn")]
  50. [InlineData ("hi\x1B[2c\x1B[<5;5;5m", "c", "\x1B[2c", "hi\x1B[<5;5;5m")]
  51. [InlineData ("\x1B[3c\x1B[4c\x1B[<0;0;0MIn", "c", "\u001b[3c", "\u001b[4c\u001b[<0;0;0MIn")]
  52. [InlineData ("\x1B[<1;2;3M\x1B[0c\x1B[<1;2;3M\x1B[2c", "c", "\x1B[0c", "\x1B[<1;2;3M\x1B[<1;2;3M\u001b[2c")]
  53. [InlineData ("\x1B[<0;1;1MHi\x1B[6c\x1B[2c\x1B[<1;0;0MT", "c", "\x1B[6c", "\x1B[<0;1;1MHi\x1B[2c\x1B[<1;0;0MT")]
  54. [InlineData ("Te\x1B[<2;2;2M\x1B[7c", "c", "\x1B[7c", "Te\x1B[<2;2;2M")]
  55. [InlineData ("\x1B[0c\x1B[<0;0;0M\x1B[3c\x1B[0c\x1B[1;0MT", "c", "\x1B[0c", "\x1B[<0;0;0M\x1B[3c\x1B[0c\x1B[1;0MT")]
  56. [InlineData ("\x1B[0;0M\x1B[<0;0;0M\x1B[3cT\x1B[1c", "c", "\u001b[3c", "\u001b[0;0M\u001b[<0;0;0MT\u001b[1c")]
  57. [InlineData ("\x1B[3c\x1B[<0;0;0M\x1B[0c\x1B[<1;1;1MIn\x1B[1c", "c", "\u001b[3c", "\u001b[<0;0;0M\u001b[0c\u001b[<1;1;1MIn\u001b[1c")]
  58. [InlineData ("\x1B[<5;5;5M\x1B[7cEx\x1B[8c", "c", "\x1B[7c", "\u001b[<5;5;5MEx\u001b[8c")]
  59. // Random characters and mixed inputs
  60. [InlineData ("\x1B[<1;1;1MJJ\x1B[9c", "c", "\x1B[9c", "\x1B[<1;1;1MJJ")] // Mixed text
  61. [InlineData ("Be\x1B[0cAf", "c", "\x1B[0c", "BeAf")] // Escape in the middle of the string
  62. [InlineData ("\x1B[<0;0;0M\x1B[2cNot e", "c", "\x1B[2c", "\x1B[<0;0;0MNot e")] // Unexpected sequence followed by text
  63. [InlineData ("Just te\x1B[<0;0;0M\x1B[3c\x1B[2c\x1B[4c", "c", "\x1B[3c", "Just te\x1B[<0;0;0M\x1B[2c\x1B[4c")] // Multiple unexpected responses
  64. [InlineData ("\x1B[1;2;3M\x1B[0c\x1B[2;2M\x1B[0;0;0MTe", "c", "\x1B[0c", "\x1B[1;2;3M\x1B[2;2M\x1B[0;0;0MTe")] // Multiple commands with responses
  65. [InlineData ("\x1B[<3;3;3Mabc\x1B[4cde", "c", "\x1B[4c", "\x1B[<3;3;3Mabcde")] // Escape sequences mixed with regular text
  66. // Edge cases
  67. [InlineData ("\x1B[0c\x1B[0c\x1B[0c", "c", "\x1B[0c", "\x1B[0c\x1B[0c")] // Multiple identical responses
  68. [InlineData ("", "c", "", "")] // Empty input
  69. [InlineData ("Normal", "c", "", "Normal")] // No escape sequences
  70. [InlineData ("\x1B[<0;0;0M", "c", "", "\x1B[<0;0;0M")] // Escape sequence only
  71. [InlineData ("\x1B[1;2;3M\x1B[0c", "c", "\x1B[0c", "\x1B[1;2;3M")] // Last response consumed
  72. [InlineData ("Inpu\x1B[0c\x1B[1;0;0M", "c", "\x1B[0c", "Inpu\x1B[1;0;0M")] // Single input followed by escape
  73. [InlineData ("\x1B[2c\x1B[<5;6;7MDa", "c", "\x1B[2c", "\x1B[<5;6;7MDa")] // Multiple escape sequences followed by text
  74. [InlineData ("\x1B[0cHi\x1B[1cGo", "c", "\x1B[0c", "Hi\u001b[1cGo")] // Normal text with multiple escape sequences
  75. [InlineData ("\x1B[<1;1;1MTe", "c", "", "\x1B[<1;1;1MTe")]
  76. // Add more test cases here...
  77. public void TestInputSequences (string ansiStream, string expectedTerminator, string expectedResponse, string expectedOutput)
  78. {
  79. var swGenBatches = Stopwatch.StartNew ();
  80. int tests = 0;
  81. var permutations = GetBatchPermutations (ansiStream,7).ToArray ();
  82. swGenBatches.Stop ();
  83. var swRunTest = Stopwatch.StartNew ();
  84. foreach (var batchSet in permutations)
  85. {
  86. string response = string.Empty;
  87. // Register the expected response with the given terminator
  88. _parser.ExpectResponse (expectedTerminator, s => response = s);
  89. // Process the input
  90. StringBuilder actualOutput = new StringBuilder ();
  91. foreach (var batch in batchSet)
  92. {
  93. var output = _parser.ProcessInput (StringToBatch (batch));
  94. actualOutput.Append (BatchToString (output));
  95. }
  96. // Assert the final output minus the expected response
  97. Assert.Equal (expectedOutput, actualOutput.ToString());
  98. Assert.Equal (expectedResponse, response);
  99. tests++;
  100. }
  101. output.WriteLine ($"Tested {tests} in {swRunTest.ElapsedMilliseconds} ms (gen batches took {swGenBatches.ElapsedMilliseconds} ms)" );
  102. }
  103. private Tuple<char, int> [] StringToBatch (string batch)
  104. {
  105. return batch.Select ((k, i) => Tuple.Create (k, i)).ToArray ();
  106. }
  107. public static IEnumerable<string []> GetBatchPermutations (string input, int maxDepth = 3)
  108. {
  109. // Call the recursive method to generate batches with an initial depth of 0
  110. return GenerateBatches (input, 0, maxDepth, 0);
  111. }
  112. private static IEnumerable<string []> GenerateBatches (string input, int start, int maxDepth, int currentDepth)
  113. {
  114. // If we have reached the maximum recursion depth, return no results
  115. if (currentDepth >= maxDepth)
  116. {
  117. yield break; // No more batches can be generated at this depth
  118. }
  119. // If we have reached the end of the string, return an empty list
  120. if (start >= input.Length)
  121. {
  122. yield return new string [0];
  123. yield break;
  124. }
  125. // Iterate over the input string to create batches
  126. for (int i = start + 1; i <= input.Length; i++)
  127. {
  128. // Take a batch from 'start' to 'i'
  129. string batch = input.Substring (start, i - start);
  130. // Recursively get batches from the remaining substring, increasing the depth
  131. foreach (var remainingBatches in GenerateBatches (input, i, maxDepth, currentDepth + 1))
  132. {
  133. // Combine the current batch with the remaining batches
  134. var result = new string [1 + remainingBatches.Length];
  135. result [0] = batch;
  136. Array.Copy (remainingBatches, 0, result, 1, remainingBatches.Length);
  137. yield return result;
  138. }
  139. }
  140. }
  141. private void AssertIgnored (string ansiStream,char expected, ref int i)
  142. {
  143. var c = NextChar (ansiStream, ref i);
  144. // Parser does not grab this key (i.e. driver can continue with regular operations)
  145. Assert.Equal ( c,_parser.ProcessInput (c));
  146. Assert.Equal (expected,c.Single().Item1);
  147. }
  148. private void AssertConsumed (string ansiStream, ref int i)
  149. {
  150. // Parser grabs this key
  151. var c = NextChar (ansiStream, ref i);
  152. Assert.Empty (_parser.ProcessInput(c));
  153. }
  154. private void AssertReleased (string ansiStream, ref int i, string expectedRelease)
  155. {
  156. var c = NextChar (ansiStream, ref i);
  157. // Parser realizes it has grabbed content that does not belong to an outstanding request
  158. // Parser returns false to indicate to continue
  159. Assert.Equal(expectedRelease,BatchToString(_parser.ProcessInput (c)));
  160. }
  161. private string BatchToString (IEnumerable<Tuple<char, int>> processInput)
  162. {
  163. return new string(processInput.Select (a=>a.Item1).ToArray ());
  164. }
  165. private Tuple<char,int>[] NextChar (string ansiStream, ref int i)
  166. {
  167. return StringToBatch(ansiStream [i++].ToString());
  168. }
  169. }