AnsiResponseParserTests.cs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795
  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. /// <summary>
  10. /// Used for the T value in batches that are passed to the AnsiResponseParser&lt;int&gt; (parser1)
  11. /// </summary>
  12. private int tIndex = 0;
  13. [Fact]
  14. public void TestInputProcessing ()
  15. {
  16. string ansiStream = "\u001b[<0;10;20M" + // ANSI escape for mouse move at (10, 20)
  17. "Hello" + // User types "Hello"
  18. "\u001b[0c"; // Device Attributes response (e.g., terminal identification i.e. DAR)
  19. string response1 = null;
  20. string response2 = null;
  21. int i = 0;
  22. // Imagine that we are expecting a DAR
  23. _parser1.ExpectResponse ("c",(s)=> response1 = s,null, false);
  24. _parser2.ExpectResponse ("c", (s) => response2 = s , null, false);
  25. // First char is Escape which we must consume incase what follows is the DAR
  26. AssertConsumed (ansiStream, ref i); // Esc
  27. for (int c = 0; c < "[<0;10;20".Length; c++)
  28. {
  29. AssertConsumed (ansiStream, ref i);
  30. }
  31. // We see the M terminator
  32. AssertReleased (ansiStream, ref i, "\u001b[<0;10;20M");
  33. // Regular user typing
  34. for (int c = 0; c < "Hello".Length; c++)
  35. {
  36. AssertIgnored (ansiStream,"Hello"[c], ref i);
  37. }
  38. // Now we have entered the actual DAR we should be consuming these
  39. for (int c = 0; c < "\u001b[0".Length; c++)
  40. {
  41. AssertConsumed (ansiStream, ref i);
  42. }
  43. // Consume the terminator 'c' and expect this to call the above event
  44. Assert.Null (response1);
  45. Assert.Null (response1);
  46. AssertConsumed (ansiStream, ref i);
  47. Assert.NotNull (response2);
  48. Assert.Equal ("\u001b[0c", response2);
  49. Assert.NotNull (response2);
  50. Assert.Equal ("\u001b[0c", response2);
  51. }
  52. [Theory]
  53. [InlineData ("\u001b[<0;10;20MHi\u001b[0c", "c", "\u001b[0c", "\u001b[<0;10;20MHi")]
  54. [InlineData ("\u001b[<1;15;25MYou\u001b[1c", "c", "\u001b[1c", "\u001b[<1;15;25MYou")]
  55. [InlineData ("\u001b[0cHi\u001b[0c", "c", "\u001b[0c", "Hi\u001b[0c")]
  56. [InlineData ("\u001b[<0;0;0MHe\u001b[3c", "c", "\u001b[3c", "\u001b[<0;0;0MHe")]
  57. [InlineData ("\u001b[<0;1;2Da\u001b[0c\u001b[1c", "c", "\u001b[0c", "\u001b[<0;1;2Da\u001b[1c")]
  58. [InlineData ("\u001b[1;1M\u001b[3cAn", "c", "\u001b[3c", "\u001b[1;1MAn")]
  59. [InlineData ("hi\u001b[2c\u001b[<5;5;5m", "c", "\u001b[2c", "hi\u001b[<5;5;5m")]
  60. [InlineData ("\u001b[3c\u001b[4c\u001b[<0;0;0MIn", "c", "\u001b[3c", "\u001b[4c\u001b[<0;0;0MIn")]
  61. [InlineData ("\u001b[<1;2;3M\u001b[0c\u001b[<1;2;3M\u001b[2c", "c", "\u001b[0c", "\u001b[<1;2;3M\u001b[<1;2;3M\u001b[2c")]
  62. [InlineData ("\u001b[<0;1;1MHi\u001b[6c\u001b[2c\u001b[<1;0;0MT", "c", "\u001b[6c", "\u001b[<0;1;1MHi\u001b[2c\u001b[<1;0;0MT")]
  63. [InlineData ("Te\u001b[<2;2;2M\u001b[7c", "c", "\u001b[7c", "Te\u001b[<2;2;2M")]
  64. [InlineData ("\u001b[0c\u001b[<0;0;0M\u001b[3c\u001b[0c\u001b[1;0MT", "c", "\u001b[0c", "\u001b[<0;0;0M\u001b[3c\u001b[0c\u001b[1;0MT")]
  65. [InlineData ("\u001b[0;0M\u001b[<0;0;0M\u001b[3cT\u001b[1c", "c", "\u001b[3c", "\u001b[0;0M\u001b[<0;0;0MT\u001b[1c")]
  66. [InlineData ("\u001b[3c\u001b[<0;0;0M\u001b[0c\u001b[<1;1;1MIn\u001b[1c", "c", "\u001b[3c", "\u001b[<0;0;0M\u001b[0c\u001b[<1;1;1MIn\u001b[1c")]
  67. [InlineData ("\u001b[<5;5;5M\u001b[7cEx\u001b[8c", "c", "\u001b[7c", "\u001b[<5;5;5MEx\u001b[8c")]
  68. // Random characters and mixed inputs
  69. [InlineData ("\u001b[<1;1;1MJJ\u001b[9c", "c", "\u001b[9c", "\u001b[<1;1;1MJJ")] // Mixed text
  70. [InlineData ("Be\u001b[0cAf", "c", "\u001b[0c", "BeAf")] // Escape in the middle of the string
  71. [InlineData ("\u001b[<0;0;0M\u001b[2cNot e", "c", "\u001b[2c", "\u001b[<0;0;0MNot e")] // Unexpected sequence followed by text
  72. [InlineData ("Just te\u001b[<0;0;0M\u001b[3c\u001b[2c\u001b[4c", "c", "\u001b[3c", "Just te\u001b[<0;0;0M\u001b[2c\u001b[4c")] // Multiple unexpected responses
  73. [InlineData ("\u001b[1;2;3M\u001b[0c\u001b[2;2M\u001b[0;0;0MTe", "c", "\u001b[0c", "\u001b[1;2;3M\u001b[2;2M\u001b[0;0;0MTe")] // Multiple commands with responses
  74. [InlineData ("\u001b[<3;3;3Mabc\u001b[4cde", "c", "\u001b[4c", "\u001b[<3;3;3Mabcde")] // Escape sequences mixed with regular text
  75. // Edge cases
  76. [InlineData ("\u001b[0c\u001b[0c\u001b[0c", "c", "\u001b[0c", "\u001b[0c\u001b[0c")] // Multiple identical responses
  77. [InlineData ("", "c", "", "")] // Empty input
  78. [InlineData ("Normal", "c", "", "Normal")] // No escape sequences
  79. [InlineData ("\u001b[<0;0;0M", "c", "", "\u001b[<0;0;0M")] // Escape sequence only
  80. [InlineData ("\u001b[1;2;3M\u001b[0c", "c", "\u001b[0c", "\u001b[1;2;3M")] // Last response consumed
  81. [InlineData ("Inpu\u001b[0c\u001b[1;0;0M", "c", "\u001b[0c", "Inpu\u001b[1;0;0M")] // Single input followed by escape
  82. [InlineData ("\u001b[2c\u001b[<5;6;7MDa", "c", "\u001b[2c", "\u001b[<5;6;7MDa")] // Multiple escape sequences followed by text
  83. [InlineData ("\u001b[0cHi\u001b[1cGo", "c", "\u001b[0c", "Hi\u001b[1cGo")] // Normal text with multiple escape sequences
  84. [InlineData ("\u001b[<1;1;1MTe", "c", "", "\u001b[<1;1;1MTe")]
  85. // Add more test cases here...
  86. public void TestInputSequences (string ansiStream, string expectedTerminator, string expectedResponse, string expectedOutput)
  87. {
  88. var swGenBatches = Stopwatch.StartNew ();
  89. int tests = 0;
  90. var permutations = GetBatchPermutations (ansiStream,5).ToArray ();
  91. swGenBatches.Stop ();
  92. var swRunTest = Stopwatch.StartNew ();
  93. foreach (var batchSet in permutations)
  94. {
  95. tIndex = 0;
  96. string response1 = string.Empty;
  97. string response2 = string.Empty;
  98. // Register the expected response with the given terminator
  99. _parser1.ExpectResponse (expectedTerminator, s => response1 = s, null, false);
  100. _parser2.ExpectResponse (expectedTerminator, s => response2 = s, null, false);
  101. // Process the input
  102. StringBuilder actualOutput1 = new StringBuilder ();
  103. StringBuilder actualOutput2 = new StringBuilder ();
  104. foreach (var batch in batchSet)
  105. {
  106. var output1 = _parser1.ProcessInput (StringToBatch (batch));
  107. actualOutput1.Append (BatchToString (output1));
  108. var output2 = _parser2.ProcessInput (batch);
  109. actualOutput2.Append (output2);
  110. }
  111. // Assert the final output minus the expected response
  112. Assert.Equal (expectedOutput, actualOutput1.ToString());
  113. Assert.Equal (expectedResponse, response1);
  114. Assert.Equal (expectedOutput, actualOutput2.ToString ());
  115. Assert.Equal (expectedResponse, response2);
  116. tests++;
  117. }
  118. output.WriteLine ($"Tested {tests} in {swRunTest.ElapsedMilliseconds} ms (gen batches took {swGenBatches.ElapsedMilliseconds} ms)" );
  119. }
  120. public static IEnumerable<object []> TestInputSequencesExact_Cases ()
  121. {
  122. yield return
  123. [
  124. "Esc Only",
  125. null,
  126. new []
  127. {
  128. new StepExpectation ('\u001b',AnsiResponseParserState.ExpectingEscapeSequence,string.Empty)
  129. }
  130. ];
  131. yield return
  132. [
  133. "Esc Hi with intermediate",
  134. 'c',
  135. new []
  136. {
  137. new StepExpectation ('\u001b',AnsiResponseParserState.ExpectingEscapeSequence,string.Empty),
  138. new StepExpectation ('H',AnsiResponseParserState.InResponse,string.Empty), // H is known terminator and not expected one so here we release both chars
  139. new StepExpectation ('\u001b',AnsiResponseParserState.ExpectingEscapeSequence,"\u001bH"),
  140. new StepExpectation ('[',AnsiResponseParserState.InResponse,string.Empty),
  141. new StepExpectation ('0',AnsiResponseParserState.InResponse,string.Empty),
  142. new StepExpectation ('c',AnsiResponseParserState.Normal,string.Empty,"\u001b[0c"), // c is expected terminator so here we swallow input and populate expected response
  143. new StepExpectation ('\u001b',AnsiResponseParserState.ExpectingEscapeSequence,string.Empty),
  144. }
  145. ];
  146. }
  147. public class StepExpectation ()
  148. {
  149. /// <summary>
  150. /// The input character to feed into the parser at this step of the test
  151. /// </summary>
  152. public char Input { get; }
  153. /// <summary>
  154. /// What should the state of the parser be after the <see cref="Input"/>
  155. /// is fed in.
  156. /// </summary>
  157. public AnsiResponseParserState ExpectedStateAfterOperation { get; }
  158. /// <summary>
  159. /// If this step should release one or more characters, put them here.
  160. /// </summary>
  161. public string ExpectedRelease { get; } = string.Empty;
  162. /// <summary>
  163. /// If this step should result in a completing of detection of ANSI response
  164. /// then put the expected full response sequence here.
  165. /// </summary>
  166. public string ExpectedAnsiResponse { get; } = string.Empty;
  167. public StepExpectation (
  168. char input,
  169. AnsiResponseParserState expectedStateAfterOperation,
  170. string expectedRelease = "",
  171. string expectedAnsiResponse = "") : this ()
  172. {
  173. Input = input;
  174. ExpectedStateAfterOperation = expectedStateAfterOperation;
  175. ExpectedRelease = expectedRelease;
  176. ExpectedAnsiResponse = expectedAnsiResponse;
  177. }
  178. }
  179. [MemberData(nameof(TestInputSequencesExact_Cases))]
  180. [Theory]
  181. public void TestInputSequencesExact (string caseName, char? terminator, IEnumerable<StepExpectation> expectedStates)
  182. {
  183. output.WriteLine ("Running test case:" + caseName);
  184. var parser = new AnsiResponseParser ();
  185. string response = null;
  186. if (terminator.HasValue)
  187. {
  188. parser.ExpectResponse (terminator.Value.ToString (),(s)=> response = s,null, false);
  189. }
  190. int step= 0;
  191. foreach (var state in expectedStates)
  192. {
  193. step++;
  194. // If we expect the response to be detected at this step
  195. if (!string.IsNullOrWhiteSpace (state.ExpectedAnsiResponse))
  196. {
  197. // Then before passing input it should be null
  198. Assert.Null (response);
  199. }
  200. var actual = parser.ProcessInput (state.Input.ToString ());
  201. Assert.Equal (state.ExpectedRelease,actual);
  202. Assert.Equal (state.ExpectedStateAfterOperation, parser.State);
  203. // If we expect the response to be detected at this step
  204. if (!string.IsNullOrWhiteSpace (state.ExpectedAnsiResponse))
  205. {
  206. // And after passing input it shuld be the expected value
  207. Assert.Equal (state.ExpectedAnsiResponse, response);
  208. }
  209. output.WriteLine ($"Step {step} passed");
  210. }
  211. }
  212. [Fact]
  213. public void ReleasesEscapeAfterTimeout ()
  214. {
  215. string input = "\u001b";
  216. int i = 0;
  217. // Esc on its own looks like it might be an esc sequence so should be consumed
  218. AssertConsumed (input,ref i);
  219. // We should know when the state changed
  220. Assert.Equal (AnsiResponseParserState.ExpectingEscapeSequence, _parser1.State);
  221. Assert.Equal (AnsiResponseParserState.ExpectingEscapeSequence, _parser2.State);
  222. Assert.Equal (DateTime.Now.Date, _parser1.StateChangedAt.Date);
  223. Assert.Equal (DateTime.Now.Date, _parser2.StateChangedAt.Date);
  224. AssertManualReleaseIs (input);
  225. }
  226. [Fact]
  227. public void TwoExcapesInARow ()
  228. {
  229. // Example user presses Esc key then a DAR comes in
  230. string input = "\u001b\u001b";
  231. int i = 0;
  232. // First Esc gets grabbed
  233. AssertConsumed (input, ref i);
  234. // Upon getting the second Esc we should release the first
  235. AssertReleased (input, ref i, "\u001b",0);
  236. // Assume 50ms or something has passed, lets force release as no new content
  237. // It should be the second escape that gets released (i.e. index 1)
  238. AssertManualReleaseIs ("\u001b",1);
  239. }
  240. [Fact]
  241. public void TestLateResponses ()
  242. {
  243. var p = new AnsiResponseParser ();
  244. string responseA = null;
  245. string responseB = null;
  246. p.ExpectResponse ("z",(r)=>responseA=r, null, false);
  247. // Some time goes by without us seeing a response
  248. p.StopExpecting ("z", false);
  249. // Send our new request
  250. p.ExpectResponse ("z", (r) => responseB = r, null, false);
  251. // Because we gave up on getting A, we should expect the response to be to our new request
  252. Assert.Empty(p.ProcessInput ("\u001b[<1;2z"));
  253. Assert.Null (responseA);
  254. Assert.Equal ("\u001b[<1;2z", responseB);
  255. // Oh looks like we got one late after all - swallow it
  256. Assert.Empty (p.ProcessInput ("\u001b[0000z"));
  257. // Do not expect late responses to be populated back to your variable
  258. Assert.Null (responseA);
  259. Assert.Equal ("\u001b[<1;2z", responseB);
  260. // We now have no outstanding requests (late or otherwise) so new ansi codes should just fall through
  261. Assert.Equal ("\u001b[111z", p.ProcessInput ("\u001b[111z"));
  262. }
  263. [Fact]
  264. public void TestPersistentResponses ()
  265. {
  266. var p = new AnsiResponseParser ();
  267. int m = 0;
  268. int M = 1;
  269. p.ExpectResponse ("m", _ => m++, null, true);
  270. p.ExpectResponse ("M", _ => M++, null, true);
  271. // Act - Feed input strings containing ANSI sequences
  272. p.ProcessInput ("\u001b[<0;10;10m"); // Should match and increment `m`
  273. p.ProcessInput ("\u001b[<0;20;20m"); // Should match and increment `m`
  274. p.ProcessInput ("\u001b[<0;30;30M"); // Should match and increment `M`
  275. p.ProcessInput ("\u001b[<0;40;40M"); // Should match and increment `M`
  276. p.ProcessInput ("\u001b[<0;50;50M"); // Should match and increment `M`
  277. // Assert - Verify that counters reflect the expected counts of each terminator
  278. Assert.Equal (2, m); // Expected two `m` responses
  279. Assert.Equal (4, M); // Expected three `M` responses plus the initial value of 1
  280. }
  281. [Fact]
  282. public void TestPersistentResponses_WithMetadata ()
  283. {
  284. var p = new AnsiResponseParser<int> ();
  285. int m = 0;
  286. var result = new List<Tuple<char,int>> ();
  287. p.ExpectResponseT ("m", (r) =>
  288. {
  289. result = r.ToList ();
  290. m++;
  291. },
  292. null, true);
  293. // Act - Feed input strings containing ANSI sequences
  294. p.ProcessInput (StringToBatch("\u001b[<0;10;10m")); // Should match and increment `m`
  295. // Prepare expected result:
  296. var expected = new List<Tuple<char, int>>
  297. {
  298. Tuple.Create('\u001b', 0), // Escape character
  299. Tuple.Create('[', 1),
  300. Tuple.Create('<', 2),
  301. Tuple.Create('0', 3),
  302. Tuple.Create(';', 4),
  303. Tuple.Create('1', 5),
  304. Tuple.Create('0', 6),
  305. Tuple.Create(';', 7),
  306. Tuple.Create('1', 8),
  307. Tuple.Create('0', 9),
  308. Tuple.Create('m', 10)
  309. };
  310. Assert.Equal (expected.Count, result.Count); // Ensure the count is as expected
  311. Assert.True (expected.SequenceEqual (result), "The result does not match the expected output."); // Check the actual content
  312. }
  313. [Fact]
  314. public void ShouldSwallowUnknownResponses_WhenDelegateSaysSo ()
  315. {
  316. // Swallow all unknown escape codes
  317. _parser1.UnexpectedResponseHandler = _ => true;
  318. _parser2.UnknownResponseHandler = _ => true;
  319. AssertReleased (
  320. "Just te\u001b[<0;0;0M\u001b[3c\u001b[2c\u001b[4cst",
  321. "Just test",
  322. 0,
  323. 1,
  324. 2,
  325. 3,
  326. 4,
  327. 5,
  328. 6,
  329. 28,
  330. 29);
  331. }
  332. [Fact]
  333. public void UnknownResponses_ParameterShouldMatch ()
  334. {
  335. // Track unknown responses passed to the UnexpectedResponseHandler
  336. var unknownResponses = new List<string> ();
  337. // Set up the UnexpectedResponseHandler to log each unknown response
  338. _parser1.UnexpectedResponseHandler = r1 =>
  339. {
  340. unknownResponses.Add (BatchToString (r1));
  341. return true; // Return true to swallow unknown responses
  342. };
  343. _parser2.UnknownResponseHandler = r2 =>
  344. {
  345. // parsers should be agreeing on what these responses are!
  346. Assert.Equal(unknownResponses.Last(),r2);
  347. return true; // Return true to swallow unknown responses
  348. };
  349. // Input with known and unknown responses
  350. AssertReleased (
  351. "Just te\u001b[<0;0;0M\u001b[3c\u001b[2c\u001b[4cst",
  352. "Just test");
  353. // Expected unknown responses (ANSI sequences that are unknown)
  354. var expectedUnknownResponses = new List<string>
  355. {
  356. "\u001b[<0;0;0M",
  357. "\u001b[3c",
  358. "\u001b[2c",
  359. "\u001b[4c"
  360. };
  361. // Assert that the UnexpectedResponseHandler was called with the correct unknown responses
  362. Assert.Equal (expectedUnknownResponses.Count, unknownResponses.Count);
  363. Assert.Equal (expectedUnknownResponses, unknownResponses);
  364. }
  365. [Fact]
  366. public void ParserDetectsMouse ()
  367. {
  368. // ANSI escape sequence for mouse down (using a generic format example)
  369. const string MOUSE_DOWN = "\u001B[<0;12;32M";
  370. // ANSI escape sequence for Device Attribute Response (e.g., Terminal identifying itself)
  371. const string DEVICE_ATTRIBUTE_RESPONSE = "\u001B[?1;2c";
  372. // ANSI escape sequence for mouse up (using a generic format example)
  373. const string MOUSE_UP = "\u001B[<0;25;50m";
  374. var parser = new AnsiResponseParser ();
  375. parser.HandleMouse = true;
  376. string? foundDar = null;
  377. List<MouseEventArgs> mouseEventArgs = new ();
  378. parser.Mouse += (s, e) => mouseEventArgs.Add (e);
  379. parser.ExpectResponse ("c", (dar) => foundDar = dar, null, false);
  380. var released = parser.ProcessInput ("a" + MOUSE_DOWN + "asdf" + DEVICE_ATTRIBUTE_RESPONSE + "bbcc" + MOUSE_UP + "sss");
  381. Assert.Equal ("aasdfbbccsss", released);
  382. Assert.Equal (2, mouseEventArgs.Count);
  383. Assert.NotNull (foundDar);
  384. Assert.Equal (DEVICE_ATTRIBUTE_RESPONSE,foundDar);
  385. Assert.True (mouseEventArgs [0].IsPressed);
  386. // Mouse positions in ANSI are 1 based so actual Terminal.Gui Screen positions are x-1,y-1
  387. Assert.Equal (11,mouseEventArgs [0].Position.X);
  388. Assert.Equal (31, mouseEventArgs [0].Position.Y);
  389. Assert.True (mouseEventArgs [1].IsReleased);
  390. Assert.Equal (24, mouseEventArgs [1].Position.X);
  391. Assert.Equal (49, mouseEventArgs [1].Position.Y);
  392. }
  393. [Fact]
  394. public void ParserDetectsKeyboard ()
  395. {
  396. // ANSI escape sequence for cursor left
  397. const string LEFT = "\u001b[D";
  398. // ANSI escape sequence for Device Attribute Response (e.g., Terminal identifying itself)
  399. const string DEVICE_ATTRIBUTE_RESPONSE = "\u001B[?1;2c";
  400. // ANSI escape sequence for cursor up (while shift held down)
  401. const string SHIFT_UP = "\u001b[1;2A";
  402. var parser = new AnsiResponseParser ();
  403. parser.HandleKeyboard = true;
  404. string? foundDar = null;
  405. List<Key> keys = new ();
  406. parser.Keyboard += (s, e) => keys.Add (e);
  407. parser.ExpectResponse ("c", (dar) => foundDar = dar, null, false);
  408. var released = parser.ProcessInput ("a" + LEFT + "asdf" + DEVICE_ATTRIBUTE_RESPONSE + "bbcc" + SHIFT_UP + "sss");
  409. Assert.Equal ("aasdfbbccsss", released);
  410. Assert.Equal (2, keys.Count);
  411. Assert.NotNull (foundDar);
  412. Assert.Equal (DEVICE_ATTRIBUTE_RESPONSE, foundDar);
  413. Assert.Equal (Key.CursorLeft,keys [0]);
  414. Assert.Equal (Key.CursorUp.WithShift, keys [1]);
  415. }
  416. public static IEnumerable<object []> ParserDetects_FunctionKeys_Cases ()
  417. {
  418. // These are VT100 escape codes for F1-4
  419. yield return
  420. [
  421. "\u001bOP",
  422. Key.F1
  423. ];
  424. yield return
  425. [
  426. "\u001bOQ",
  427. Key.F2
  428. ];
  429. yield return
  430. [
  431. "\u001bOR",
  432. Key.F3
  433. ];
  434. yield return
  435. [
  436. "\u001bOS",
  437. Key.F4
  438. ];
  439. // These are also F keys
  440. yield return [
  441. "\u001b[11~",
  442. Key.F1
  443. ];
  444. yield return [
  445. "\u001b[12~",
  446. Key.F2
  447. ];
  448. yield return [
  449. "\u001b[13~",
  450. Key.F3
  451. ];
  452. yield return [
  453. "\u001b[14~",
  454. Key.F4
  455. ];
  456. yield return [
  457. "\u001b[15~",
  458. Key.F5
  459. ];
  460. yield return [
  461. "\u001b[17~",
  462. Key.F6
  463. ];
  464. yield return [
  465. "\u001b[18~",
  466. Key.F7
  467. ];
  468. yield return [
  469. "\u001b[19~",
  470. Key.F8
  471. ];
  472. yield return [
  473. "\u001b[20~",
  474. Key.F9
  475. ];
  476. yield return [
  477. "\u001b[21~",
  478. Key.F10
  479. ];
  480. yield return [
  481. "\u001b[23~",
  482. Key.F11
  483. ];
  484. yield return [
  485. "\u001b[24~",
  486. Key.F12
  487. ];
  488. }
  489. [MemberData (nameof (ParserDetects_FunctionKeys_Cases))]
  490. [Theory]
  491. public void ParserDetects_FunctionKeys (string input, Key expectedKey)
  492. {
  493. var parser = new AnsiResponseParser ();
  494. parser.HandleKeyboard = true;
  495. List<Key> keys = new ();
  496. parser.Keyboard += (s, e) => keys.Add (e);
  497. foreach (var ch in input.ToCharArray ())
  498. {
  499. parser.ProcessInput (new (ch,1));
  500. }
  501. var k = Assert.Single (keys);
  502. Assert.Equal (k,expectedKey);
  503. }
  504. private Tuple<char, int> [] StringToBatch (string batch)
  505. {
  506. return batch.Select ((k) => Tuple.Create (k, tIndex++)).ToArray ();
  507. }
  508. public static IEnumerable<string []> GetBatchPermutations (string input, int maxDepth = 3)
  509. {
  510. // Call the recursive method to generate batches with an initial depth of 0
  511. return GenerateBatches (input, 0, maxDepth, 0);
  512. }
  513. private static IEnumerable<string []> GenerateBatches (string input, int start, int maxDepth, int currentDepth)
  514. {
  515. // If we have reached the maximum recursion depth, return no results
  516. if (currentDepth >= maxDepth)
  517. {
  518. yield break; // No more batches can be generated at this depth
  519. }
  520. // If we have reached the end of the string, return an empty list
  521. if (start >= input.Length)
  522. {
  523. yield return new string [0];
  524. yield break;
  525. }
  526. // Iterate over the input string to create batches
  527. for (int i = start + 1; i <= input.Length; i++)
  528. {
  529. // Take a batch from 'start' to 'i'
  530. string batch = input.Substring (start, i - start);
  531. // Recursively get batches from the remaining substring, increasing the depth
  532. foreach (var remainingBatches in GenerateBatches (input, i, maxDepth, currentDepth + 1))
  533. {
  534. // Combine the current batch with the remaining batches
  535. var result = new string [1 + remainingBatches.Length];
  536. result [0] = batch;
  537. Array.Copy (remainingBatches, 0, result, 1, remainingBatches.Length);
  538. yield return result;
  539. }
  540. }
  541. }
  542. private void AssertIgnored (string ansiStream,char expected, ref int i)
  543. {
  544. var c2 = ansiStream [i];
  545. var c1 = NextChar (ansiStream, ref i);
  546. // Parser does not grab this key (i.e. driver can continue with regular operations)
  547. Assert.Equal ( c1,_parser1.ProcessInput (c1));
  548. Assert.Equal (expected,c1.Single().Item1);
  549. Assert.Equal (c2, _parser2.ProcessInput (c2.ToString()).Single());
  550. Assert.Equal (expected, c2 );
  551. }
  552. private void AssertConsumed (string ansiStream, ref int i)
  553. {
  554. // Parser grabs this key
  555. var c2 = ansiStream [i];
  556. var c1 = NextChar (ansiStream, ref i);
  557. Assert.Empty (_parser1.ProcessInput(c1));
  558. Assert.Empty (_parser2.ProcessInput (c2.ToString()));
  559. }
  560. /// <summary>
  561. /// Overload that fully exhausts <paramref name="ansiStream"/> and asserts
  562. /// that the final released content across whole processing is <paramref name="expectedRelease"/>
  563. /// </summary>
  564. /// <param name="ansiStream"></param>
  565. /// <param name="expectedRelease"></param>
  566. /// <param name="expectedTValues"></param>
  567. private void AssertReleased (string ansiStream, string expectedRelease, params int [] expectedTValues)
  568. {
  569. var sb = new StringBuilder ();
  570. var tValues = new List<int> ();
  571. int i = 0;
  572. while (i < ansiStream.Length)
  573. {
  574. var c2 = ansiStream [i];
  575. var c1 = NextChar (ansiStream, ref i);
  576. var released1 = _parser1.ProcessInput (c1).ToArray ();
  577. tValues.AddRange(released1.Select (kv => kv.Item2));
  578. var released2 = _parser2.ProcessInput (c2.ToString ());
  579. // Both parsers should have same chars so release chars consistently with each other
  580. Assert.Equal (BatchToString(released1),released2);
  581. sb.Append (released2);
  582. }
  583. Assert.Equal (expectedRelease, sb.ToString());
  584. if (expectedTValues.Length > 0)
  585. {
  586. Assert.True (expectedTValues.SequenceEqual (tValues));
  587. }
  588. }
  589. /// <summary>
  590. /// Asserts that <paramref name="i"/> index of <see cref="ansiStream"/> when consumed will release
  591. /// <paramref name="expectedRelease"/>. Results in implicit increment of <paramref name="i"/>.
  592. /// <remarks>Note that this does NOT iteratively consume all the stream, only 1 char at <paramref name="i"/></remarks>
  593. /// </summary>
  594. /// <param name="ansiStream"></param>
  595. /// <param name="i"></param>
  596. /// <param name="expectedRelease"></param>
  597. /// <param name="expectedTValues"></param>
  598. private void AssertReleased (string ansiStream, ref int i, string expectedRelease, params int[] expectedTValues)
  599. {
  600. var c2 = ansiStream [i];
  601. var c1 = NextChar (ansiStream, ref i);
  602. // Parser realizes it has grabbed content that does not belong to an outstanding request
  603. // Parser returns false to indicate to continue
  604. var released1 = _parser1.ProcessInput (c1).ToArray ();
  605. Assert.Equal (expectedRelease, BatchToString (released1));
  606. if (expectedTValues.Length > 0)
  607. {
  608. Assert.True (expectedTValues.SequenceEqual (released1.Select (kv=>kv.Item2)));
  609. }
  610. Assert.Equal (expectedRelease, _parser2.ProcessInput (c2.ToString ()));
  611. }
  612. private string BatchToString (IEnumerable<Tuple<char, int>> processInput)
  613. {
  614. return new string(processInput.Select (a=>a.Item1).ToArray ());
  615. }
  616. private Tuple<char,int>[] NextChar (string ansiStream, ref int i)
  617. {
  618. return StringToBatch(ansiStream [i++].ToString());
  619. }
  620. private void AssertManualReleaseIs (string expectedRelease, params int [] expectedTValues)
  621. {
  622. // Consumer is responsible for determining this based on e.g. after 50ms
  623. var released1 = _parser1.Release ().ToArray ();
  624. Assert.Equal (expectedRelease, BatchToString (released1));
  625. if (expectedTValues.Length > 0)
  626. {
  627. Assert.True (expectedTValues.SequenceEqual (released1.Select (kv => kv.Item2)));
  628. }
  629. Assert.Equal (expectedRelease, _parser2.Release ());
  630. Assert.Equal (AnsiResponseParserState.Normal, _parser1.State);
  631. Assert.Equal (AnsiResponseParserState.Normal, _parser2.State);
  632. }
  633. }