AnsiResponseParserTests.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. using System.Diagnostics;
  2. using System.Text;
  3. using Xunit.Abstractions;
  4. namespace UnitTests.ConsoleDrivers;
  5. public class AnsiResponseParserTests (ITestOutputHelper output)
  6. {
  7. AnsiResponseParser<int> _parser1 = new AnsiResponseParser<int> ();
  8. AnsiResponseParser _parser2 = 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? response1 = null;
  16. string? response2 = null;
  17. int i = 0;
  18. // Imagine that we are expecting a DAR
  19. _parser1.ExpectResponse ("c",(s)=> response1 = s);
  20. _parser2.ExpectResponse ("c", (s) => response2 = s);
  21. // First char is Escape which we must consume incase what follows is the DAR
  22. AssertConsumed (ansiStream, ref i); // Esc
  23. for (int c = 0; c < "[<0;10;20".Length; c++)
  24. {
  25. AssertConsumed (ansiStream, ref i);
  26. }
  27. // We see the M terminator
  28. AssertReleased (ansiStream, ref i, "\x1B[<0;10;20M");
  29. // Regular user typing
  30. for (int c = 0; c < "Hello".Length; c++)
  31. {
  32. AssertIgnored (ansiStream,"Hello"[c], ref i);
  33. }
  34. // Now we have entered the actual DAR we should be consuming these
  35. for (int c = 0; c < "\x1B[0".Length; c++)
  36. {
  37. AssertConsumed (ansiStream, ref i);
  38. }
  39. // Consume the terminator 'c' and expect this to call the above event
  40. Assert.Null (response1);
  41. Assert.Null (response1);
  42. AssertConsumed (ansiStream, ref i);
  43. Assert.NotNull (response2);
  44. Assert.Equal ("\x1B[0c", response2);
  45. Assert.NotNull (response2);
  46. Assert.Equal ("\x1B[0c", response2);
  47. }
  48. [Theory]
  49. [InlineData ("\x1B[<0;10;20MHi\x1B[0c", "c", "\x1B[0c", "\x1B[<0;10;20MHi")]
  50. [InlineData ("\x1B[<1;15;25MYou\x1B[1c", "c", "\x1B[1c", "\x1B[<1;15;25MYou")]
  51. [InlineData ("\x1B[0cHi\x1B[0c", "c", "\x1B[0c", "Hi\x1B[0c")]
  52. [InlineData ("\x1B[<0;0;0MHe\x1B[3c", "c", "\x1B[3c", "\x1B[<0;0;0MHe")]
  53. [InlineData ("\x1B[<0;1;2Da\x1B[0c\x1B[1c", "c", "\x1B[0c", "\x1B[<0;1;2Da\x1B[1c")]
  54. [InlineData ("\x1B[1;1M\x1B[3cAn", "c", "\x1B[3c", "\x1B[1;1MAn")]
  55. [InlineData ("hi\x1B[2c\x1B[<5;5;5m", "c", "\x1B[2c", "hi\x1B[<5;5;5m")]
  56. [InlineData ("\x1B[3c\x1B[4c\x1B[<0;0;0MIn", "c", "\u001b[3c", "\u001b[4c\u001b[<0;0;0MIn")]
  57. [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")]
  58. [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")]
  59. [InlineData ("Te\x1B[<2;2;2M\x1B[7c", "c", "\x1B[7c", "Te\x1B[<2;2;2M")]
  60. [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")]
  61. [InlineData ("\x1B[0;0M\x1B[<0;0;0M\x1B[3cT\x1B[1c", "c", "\u001b[3c", "\u001b[0;0M\u001b[<0;0;0MT\u001b[1c")]
  62. [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")]
  63. [InlineData ("\x1B[<5;5;5M\x1B[7cEx\x1B[8c", "c", "\x1B[7c", "\u001b[<5;5;5MEx\u001b[8c")]
  64. // Random characters and mixed inputs
  65. [InlineData ("\x1B[<1;1;1MJJ\x1B[9c", "c", "\x1B[9c", "\x1B[<1;1;1MJJ")] // Mixed text
  66. [InlineData ("Be\x1B[0cAf", "c", "\x1B[0c", "BeAf")] // Escape in the middle of the string
  67. [InlineData ("\x1B[<0;0;0M\x1B[2cNot e", "c", "\x1B[2c", "\x1B[<0;0;0MNot e")] // Unexpected sequence followed by text
  68. [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
  69. [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
  70. [InlineData ("\x1B[<3;3;3Mabc\x1B[4cde", "c", "\x1B[4c", "\x1B[<3;3;3Mabcde")] // Escape sequences mixed with regular text
  71. // Edge cases
  72. [InlineData ("\x1B[0c\x1B[0c\x1B[0c", "c", "\x1B[0c", "\x1B[0c\x1B[0c")] // Multiple identical responses
  73. [InlineData ("", "c", "", "")] // Empty input
  74. [InlineData ("Normal", "c", "", "Normal")] // No escape sequences
  75. [InlineData ("\x1B[<0;0;0M", "c", "", "\x1B[<0;0;0M")] // Escape sequence only
  76. [InlineData ("\x1B[1;2;3M\x1B[0c", "c", "\x1B[0c", "\x1B[1;2;3M")] // Last response consumed
  77. [InlineData ("Inpu\x1B[0c\x1B[1;0;0M", "c", "\x1B[0c", "Inpu\x1B[1;0;0M")] // Single input followed by escape
  78. [InlineData ("\x1B[2c\x1B[<5;6;7MDa", "c", "\x1B[2c", "\x1B[<5;6;7MDa")] // Multiple escape sequences followed by text
  79. [InlineData ("\x1B[0cHi\x1B[1cGo", "c", "\x1B[0c", "Hi\u001b[1cGo")] // Normal text with multiple escape sequences
  80. [InlineData ("\x1B[<1;1;1MTe", "c", "", "\x1B[<1;1;1MTe")]
  81. // Add more test cases here...
  82. public void TestInputSequences (string ansiStream, string expectedTerminator, string expectedResponse, string expectedOutput)
  83. {
  84. var swGenBatches = Stopwatch.StartNew ();
  85. int tests = 0;
  86. var permutations = GetBatchPermutations (ansiStream,5).ToArray ();
  87. swGenBatches.Stop ();
  88. var swRunTest = Stopwatch.StartNew ();
  89. foreach (var batchSet in permutations)
  90. {
  91. string response1 = string.Empty;
  92. string response2 = string.Empty;
  93. // Register the expected response with the given terminator
  94. _parser1.ExpectResponse (expectedTerminator, s => response1 = s);
  95. _parser2.ExpectResponse (expectedTerminator, s => response2 = s);
  96. // Process the input
  97. StringBuilder actualOutput1 = new StringBuilder ();
  98. StringBuilder actualOutput2 = new StringBuilder ();
  99. foreach (var batch in batchSet)
  100. {
  101. var output1 = _parser1.ProcessInput (StringToBatch (batch));
  102. actualOutput1.Append (BatchToString (output1));
  103. var output2 = _parser2.ProcessInput (batch);
  104. actualOutput2.Append (output2);
  105. }
  106. // Assert the final output minus the expected response
  107. Assert.Equal (expectedOutput, actualOutput1.ToString());
  108. Assert.Equal (expectedResponse, response1);
  109. Assert.Equal (expectedOutput, actualOutput2.ToString ());
  110. Assert.Equal (expectedResponse, response2);
  111. tests++;
  112. }
  113. output.WriteLine ($"Tested {tests} in {swRunTest.ElapsedMilliseconds} ms (gen batches took {swGenBatches.ElapsedMilliseconds} ms)" );
  114. }
  115. private Tuple<char, int> [] StringToBatch (string batch)
  116. {
  117. return batch.Select ((k, i) => Tuple.Create (k, i)).ToArray ();
  118. }
  119. public static IEnumerable<string []> GetBatchPermutations (string input, int maxDepth = 3)
  120. {
  121. // Call the recursive method to generate batches with an initial depth of 0
  122. return GenerateBatches (input, 0, maxDepth, 0);
  123. }
  124. private static IEnumerable<string []> GenerateBatches (string input, int start, int maxDepth, int currentDepth)
  125. {
  126. // If we have reached the maximum recursion depth, return no results
  127. if (currentDepth >= maxDepth)
  128. {
  129. yield break; // No more batches can be generated at this depth
  130. }
  131. // If we have reached the end of the string, return an empty list
  132. if (start >= input.Length)
  133. {
  134. yield return new string [0];
  135. yield break;
  136. }
  137. // Iterate over the input string to create batches
  138. for (int i = start + 1; i <= input.Length; i++)
  139. {
  140. // Take a batch from 'start' to 'i'
  141. string batch = input.Substring (start, i - start);
  142. // Recursively get batches from the remaining substring, increasing the depth
  143. foreach (var remainingBatches in GenerateBatches (input, i, maxDepth, currentDepth + 1))
  144. {
  145. // Combine the current batch with the remaining batches
  146. var result = new string [1 + remainingBatches.Length];
  147. result [0] = batch;
  148. Array.Copy (remainingBatches, 0, result, 1, remainingBatches.Length);
  149. yield return result;
  150. }
  151. }
  152. }
  153. private void AssertIgnored (string ansiStream,char expected, ref int i)
  154. {
  155. var c2 = ansiStream [i];
  156. var c1 = NextChar (ansiStream, ref i);
  157. // Parser does not grab this key (i.e. driver can continue with regular operations)
  158. Assert.Equal ( c1,_parser1.ProcessInput (c1));
  159. Assert.Equal (expected,c1.Single().Item1);
  160. Assert.Equal (c2, _parser2.ProcessInput (c2.ToString()).Single());
  161. Assert.Equal (expected, c2 );
  162. }
  163. private void AssertConsumed (string ansiStream, ref int i)
  164. {
  165. // Parser grabs this key
  166. var c2 = ansiStream [i];
  167. var c1 = NextChar (ansiStream, ref i);
  168. Assert.Empty (_parser1.ProcessInput(c1));
  169. Assert.Empty (_parser2.ProcessInput (c2.ToString()));
  170. }
  171. private void AssertReleased (string ansiStream, ref int i, string expectedRelease)
  172. {
  173. var c2 = ansiStream [i];
  174. var c1 = NextChar (ansiStream, ref i);
  175. // Parser realizes it has grabbed content that does not belong to an outstanding request
  176. // Parser returns false to indicate to continue
  177. Assert.Equal(expectedRelease,BatchToString(_parser1.ProcessInput (c1)));
  178. Assert.Equal (expectedRelease, _parser2.ProcessInput (c2.ToString ()));
  179. }
  180. private string BatchToString (IEnumerable<Tuple<char, int>> processInput)
  181. {
  182. return new string(processInput.Select (a=>a.Item1).ToArray ());
  183. }
  184. private Tuple<char,int>[] NextChar (string ansiStream, ref int i)
  185. {
  186. return StringToBatch(ansiStream [i++].ToString());
  187. }
  188. }