ToAnsiTests.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. using System.Text;
  2. using UnitTests;
  3. using Xunit.Abstractions;
  4. namespace UnitTests_Parallelizable.DriverTests;
  5. /// <summary>
  6. /// Tests for the ToAnsi functionality that generates ANSI escape sequences from buffer contents.
  7. /// </summary>
  8. public class ToAnsiTests : FakeDriverBase
  9. {
  10. [Fact]
  11. public void ToAnsi_Empty_Buffer ()
  12. {
  13. IDriver driver = CreateFakeDriver (10, 5);
  14. string ansi = driver.ToAnsi ();
  15. // Empty buffer should have newlines for each row
  16. Assert.Contains ("\n", ansi);
  17. // Should have 5 newlines (one per row)
  18. Assert.Equal (5, ansi.Count (c => c == '\n'));
  19. }
  20. [Fact]
  21. public void ToAnsi_Simple_Text ()
  22. {
  23. IDriver driver = CreateFakeDriver (10, 3);
  24. driver.AddStr ("Hello");
  25. driver.Move (0, 1);
  26. driver.AddStr ("World");
  27. string ansi = driver.ToAnsi ();
  28. // Should contain the text
  29. Assert.Contains ("Hello", ansi);
  30. Assert.Contains ("World", ansi);
  31. // Should have proper structure with newlines
  32. string[] lines = ansi.Split (['\r', '\n'], StringSplitOptions.RemoveEmptyEntries);
  33. Assert.Equal (3, lines.Length);
  34. }
  35. [Fact]
  36. public void ToAnsi_With_Colors ()
  37. {
  38. IDriver driver = CreateFakeDriver (10, 2);
  39. // Set red foreground
  40. driver.CurrentAttribute = new Attribute (Color.Red, Color.Black);
  41. driver.AddStr ("Red");
  42. driver.Move (0, 1);
  43. // Set blue foreground
  44. driver.CurrentAttribute = new Attribute (Color.Blue, Color.Black);
  45. driver.AddStr ("Blue");
  46. string ansi = driver.ToAnsi ();
  47. // Should contain ANSI color codes
  48. Assert.Contains ("\u001b[31m", ansi); // Red foreground
  49. Assert.Contains ("\u001b[34m", ansi); // Blue foreground
  50. Assert.Contains ("Red", ansi);
  51. Assert.Contains ("Blue", ansi);
  52. }
  53. [Fact]
  54. public void ToAnsi_With_Background_Colors ()
  55. {
  56. IDriver driver = CreateFakeDriver (10, 2);
  57. // Set background color
  58. driver.CurrentAttribute = new Attribute (Color.White, Color.Red);
  59. driver.AddStr ("WhiteOnRed");
  60. string ansi = driver.ToAnsi ();
  61. // Should contain ANSI background color code
  62. Assert.Contains ("\u001b[41m", ansi); // Red background
  63. Assert.Contains ("WhiteOnRed", ansi);
  64. }
  65. [Fact]
  66. public void ToAnsi_With_Text_Styles ()
  67. {
  68. IDriver driver = CreateFakeDriver (10, 3);
  69. // Bold text
  70. driver.CurrentAttribute = new Attribute (Color.White, Color.Black, TextStyle.Bold);
  71. driver.AddStr ("Bold");
  72. driver.Move (0, 1);
  73. // Italic text
  74. driver.CurrentAttribute = new Attribute (Color.White, Color.Black, TextStyle.Italic);
  75. driver.AddStr ("Italic");
  76. driver.Move (0, 2);
  77. // Underline text
  78. driver.CurrentAttribute = new Attribute (Color.White, Color.Black, TextStyle.Underline);
  79. driver.AddStr ("Underline");
  80. string ansi = driver.ToAnsi ();
  81. // Should contain ANSI style codes
  82. Assert.Contains ("\u001b[1m", ansi); // Bold
  83. Assert.Contains ("\u001b[3m", ansi); // Italic
  84. Assert.Contains ("\u001b[4m", ansi); // Underline
  85. }
  86. [Fact]
  87. public void ToAnsi_With_Wide_Characters ()
  88. {
  89. IDriver driver = CreateFakeDriver (10, 2);
  90. // Add a wide character (Chinese character)
  91. driver.AddStr ("??");
  92. driver.Move (0, 1);
  93. driver.AddStr ("??");
  94. string ansi = driver.ToAnsi ();
  95. Assert.Contains ("??", ansi);
  96. Assert.Contains ("??", ansi);
  97. }
  98. [Fact]
  99. public void ToAnsi_With_Unicode_Characters ()
  100. {
  101. IDriver driver = CreateFakeDriver (10, 2);
  102. // Add various Unicode characters
  103. driver.AddStr ("???"); // Greek letters
  104. driver.Move (0, 1);
  105. driver.AddStr ("???"); // Emoji
  106. string ansi = driver.ToAnsi ();
  107. Assert.Contains ("???", ansi);
  108. Assert.Contains ("???", ansi);
  109. }
  110. [Fact]
  111. public void ToAnsi_Attribute_Changes_Within_Line ()
  112. {
  113. IDriver driver = CreateFakeDriver (20, 1);
  114. driver.AddStr ("Normal");
  115. driver.CurrentAttribute = new Attribute (Color.Red, Color.Black);
  116. driver.AddStr ("Red");
  117. driver.CurrentAttribute = new Attribute (Color.Blue, Color.Black);
  118. driver.AddStr ("Blue");
  119. string ansi = driver.ToAnsi ();
  120. // Should contain color changes within the line
  121. Assert.Contains ("Normal", ansi);
  122. Assert.Contains ("\u001b[31m", ansi); // Red
  123. Assert.Contains ("\u001b[34m", ansi); // Blue
  124. }
  125. [Fact]
  126. public void ToAnsi_Large_Buffer ()
  127. {
  128. // Test with a larger buffer to stress performance
  129. IDriver driver = CreateFakeDriver (200, 50);
  130. // Fill with some content
  131. for (int row = 0; row < 50; row++)
  132. {
  133. driver.Move (0, row);
  134. driver.CurrentAttribute = new Attribute ((ColorName16)(row % 16), Color.Black);
  135. driver.AddStr ($"Row {row:D2} content");
  136. }
  137. string ansi = driver.ToAnsi ();
  138. // Should contain all rows
  139. Assert.Contains ("Row 00", ansi);
  140. Assert.Contains ("Row 49", ansi);
  141. // Should have proper newlines (50 content lines + 50 newlines)
  142. Assert.Equal (50, ansi.Count (c => c == '\n'));
  143. }
  144. [Fact]
  145. public void ToAnsi_RGB_Colors ()
  146. {
  147. IDriver driver = CreateFakeDriver (10, 1);
  148. // Use RGB colors (when not forcing 16 colors)
  149. Application.Force16Colors = false;
  150. try
  151. {
  152. driver.CurrentAttribute = new Attribute (new Color (255, 0, 0), new Color (0, 255, 0));
  153. driver.AddStr ("RGB");
  154. string ansi = driver.ToAnsi ();
  155. // Should contain RGB color codes
  156. Assert.Contains ("\u001b[38;2;255;0;0m", ansi); // Red foreground RGB
  157. Assert.Contains ("\u001b[48;2;0;255;0m", ansi); // Green background RGB
  158. }
  159. finally
  160. {
  161. Application.Force16Colors = true; // Reset
  162. }
  163. }
  164. [Fact]
  165. public void ToAnsi_Force16Colors ()
  166. {
  167. IDriver driver = CreateFakeDriver (10, 1);
  168. // Force 16 colors
  169. Application.Force16Colors = true;
  170. driver.CurrentAttribute = new Attribute (Color.Red, Color.Blue);
  171. driver.AddStr ("16Color");
  172. string ansi = driver.ToAnsi ();
  173. // Should contain 16-color codes, not RGB
  174. Assert.Contains ("\u001b[31m", ansi); // Red foreground (16-color)
  175. Assert.Contains ("\u001b[44m", ansi); // Blue background (16-color)
  176. Assert.DoesNotContain ("\u001b[38;2;", ansi); // No RGB codes
  177. }
  178. [Fact]
  179. public void ToAnsi_Multiple_Attributes_Per_Line ()
  180. {
  181. IDriver driver = CreateFakeDriver (50, 1);
  182. // Create a line with many attribute changes
  183. string[] colors = { "Red", "Green", "Blue", "Yellow", "Magenta", "Cyan" };
  184. foreach (string colorName in colors)
  185. {
  186. Color fg = colorName switch
  187. {
  188. "Red" => Color.Red,
  189. "Green" => Color.Green,
  190. "Blue" => Color.Blue,
  191. "Yellow" => Color.Yellow,
  192. "Magenta" => Color.Magenta,
  193. "Cyan" => Color.Cyan,
  194. _ => Color.White
  195. };
  196. driver.CurrentAttribute = new Attribute (fg, Color.Black);
  197. driver.AddStr (colorName);
  198. }
  199. string ansi = driver.ToAnsi ();
  200. // Should contain multiple color codes
  201. Assert.Contains ("\u001b[31m", ansi); // Red
  202. Assert.Contains ("\u001b[32m", ansi); // Green
  203. Assert.Contains ("\u001b[34m", ansi); // Blue
  204. Assert.Contains ("\u001b[33m", ansi); // Yellow
  205. Assert.Contains ("\u001b[35m", ansi); // Magenta
  206. Assert.Contains ("\u001b[36m", ansi); // Cyan
  207. }
  208. [Fact]
  209. public void ToAnsi_Special_Characters ()
  210. {
  211. IDriver driver = CreateFakeDriver (20, 1);
  212. // Test backslash character
  213. driver.AddStr ("Backslash:");
  214. driver.AddRune ('\\');
  215. string ansi = driver.ToAnsi ();
  216. Assert.Contains ("Backslash:", ansi);
  217. Assert.Contains ("\\", ansi);
  218. }
  219. [Fact]
  220. public void ToAnsi_Buffer_Boundary_Conditions ()
  221. {
  222. // Test with minimum buffer size
  223. IDriver driver = CreateFakeDriver (1, 1);
  224. driver.AddStr ("X");
  225. string ansi = driver.ToAnsi ();
  226. Assert.Contains ("X", ansi);
  227. Assert.Contains ("\n", ansi);
  228. // Test with very wide buffer
  229. driver = CreateFakeDriver (1000, 1);
  230. driver.AddStr ("Wide");
  231. ansi = driver.ToAnsi ();
  232. Assert.Contains ("Wide", ansi);
  233. Assert.True (ansi.Length > 1000); // Should have many spaces
  234. }
  235. [Fact]
  236. public void ToAnsi_Empty_Lines ()
  237. {
  238. IDriver driver = CreateFakeDriver (10, 3);
  239. // Only write to first and third lines
  240. driver.AddStr ("First");
  241. driver.Move (0, 2);
  242. driver.AddStr ("Third");
  243. string ansi = driver.ToAnsi ();
  244. string[] lines = ansi.Split ('\n');
  245. Assert.Equal (4, lines.Length); // 3 content lines + 1 empty line at end
  246. Assert.Contains ("First", lines[0]);
  247. Assert.Contains ("Third", lines[2]);
  248. }
  249. [Fact]
  250. public void ToAnsi_Performance_Stress_Test ()
  251. {
  252. // Create a large buffer and fill it completely
  253. const int width = 200;
  254. const int height = 100;
  255. IDriver driver = CreateFakeDriver (width, height);
  256. // Fill every cell with different content and colors
  257. for (int row = 0; row < height; row++)
  258. {
  259. for (int col = 0; col < width; col++)
  260. {
  261. driver.Move (col, row);
  262. driver.CurrentAttribute = new Attribute ((ColorName16)((row + col) % 16), Color.Black);
  263. driver.AddRune ((char)('A' + ((row + col) % 26)));
  264. }
  265. }
  266. // This should complete in reasonable time and not throw
  267. string ansi = driver.ToAnsi ();
  268. Assert.NotNull (ansi);
  269. Assert.True (ansi.Length > width * height); // Should contain all characters plus ANSI codes
  270. }
  271. }