AnsiResponseParserTests.cs 29 KB

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