ToAnsiTests.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. using System.Text;
  2. using UnitTests;
  3. using Xunit.Abstractions;
  4. namespace 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. [Theory]
  36. [InlineData (true, "\u001b[31m", "\u001b[34m")]
  37. [InlineData (false, "\u001b[38;2;255;0;0m", "\u001b[38;2;0;0;255")]
  38. public void ToAnsi_With_Colors (bool force16Colors, string expectedRed, string expectedBue)
  39. {
  40. IDriver driver = CreateFakeDriver (10, 2);
  41. driver.Force16Colors = force16Colors;
  42. // Set red foreground
  43. driver.CurrentAttribute = new Attribute (Color.Red, Color.Black);
  44. driver.AddStr ("Red");
  45. driver.Move (0, 1);
  46. // Set blue foreground
  47. driver.CurrentAttribute = new Attribute (Color.Blue, Color.Black);
  48. driver.AddStr ("Blue");
  49. string ansi = driver.ToAnsi ();
  50. Assert.True (driver.Force16Colors == force16Colors);
  51. // Should contain ANSI color codes
  52. Assert.Contains (expectedRed, ansi); // Red foreground
  53. Assert.Contains (expectedBue, ansi); // Blue foreground
  54. Assert.Contains ("Red", ansi);
  55. Assert.Contains ("Blue", ansi);
  56. }
  57. [Theory (Skip = "Uses Application.")]
  58. [InlineData (false, "\u001b[48;2;")]
  59. [InlineData (true, "\u001b[41m")]
  60. public void ToAnsi_With_Background_Colors (bool force16Colors, string expected)
  61. {
  62. IDriver driver = CreateFakeDriver (10, 2);
  63. driver.Force16Colors = force16Colors;
  64. // Set background color
  65. driver.CurrentAttribute = new (Color.White, Color.Red);
  66. driver.AddStr ("WhiteOnRed");
  67. string ansi = driver.ToAnsi ();
  68. /*
  69. The ANSI escape sequence for red background (8-color) is ESC[41m — where ESC is \x1b (or \u001b).
  70. Examples:
  71. • C# string: "\u001b[41m" or "\x1b[41m"
  72. • Reset (clear attributes): "\u001b[0m"
  73. Notes:
  74. • Bright/red background (16-color bright variant) uses ESC[101m ("\u001b[101m").
  75. • For 24-bit RGB background use ESC[48;2;<r>;<g>;<b>m, e.g. "\u001b[48;2;255;0;0m" for pure red.
  76. */
  77. Assert.True (driver.Force16Colors == force16Colors);
  78. // Should contain ANSI background color code
  79. Assert.Contains (expected, ansi); // Red background
  80. Assert.Contains ("WhiteOnRed", ansi);
  81. }
  82. [Fact]
  83. public void ToAnsi_With_Text_Styles ()
  84. {
  85. IDriver driver = CreateFakeDriver (10, 3);
  86. // Bold text
  87. driver.CurrentAttribute = new Attribute (Color.White, Color.Black, TextStyle.Bold);
  88. driver.AddStr ("Bold");
  89. driver.Move (0, 1);
  90. // Italic text
  91. driver.CurrentAttribute = new Attribute (Color.White, Color.Black, TextStyle.Italic);
  92. driver.AddStr ("Italic");
  93. driver.Move (0, 2);
  94. // Underline text
  95. driver.CurrentAttribute = new Attribute (Color.White, Color.Black, TextStyle.Underline);
  96. driver.AddStr ("Underline");
  97. string ansi = driver.ToAnsi ();
  98. // Should contain ANSI style codes
  99. Assert.Contains ("\u001b[1m", ansi); // Bold
  100. Assert.Contains ("\u001b[3m", ansi); // Italic
  101. Assert.Contains ("\u001b[4m", ansi); // Underline
  102. }
  103. [Fact]
  104. public void ToAnsi_With_Wide_Characters ()
  105. {
  106. IDriver driver = CreateFakeDriver (10, 2);
  107. // Add a wide character (Chinese character)
  108. driver.AddStr ("??");
  109. driver.Move (0, 1);
  110. driver.AddStr ("??");
  111. string ansi = driver.ToAnsi ();
  112. Assert.Contains ("??", ansi);
  113. Assert.Contains ("??", ansi);
  114. }
  115. [Fact]
  116. public void ToAnsi_With_Unicode_Characters ()
  117. {
  118. IDriver driver = CreateFakeDriver (10, 2);
  119. // Add various Unicode characters
  120. driver.AddStr ("???"); // Greek letters
  121. driver.Move (0, 1);
  122. driver.AddStr ("???"); // Emoji
  123. string ansi = driver.ToAnsi ();
  124. Assert.Contains ("???", ansi);
  125. Assert.Contains ("???", ansi);
  126. }
  127. [Theory]
  128. [InlineData (true, "\u001b[31m", "\u001b[34m")]
  129. [InlineData (false, "\u001b[38;2;", "\u001b[48;2;")]
  130. public void ToAnsi_Attribute_Changes_Within_Line (bool force16Colors, string expectedRed, string expectedBlue)
  131. {
  132. IDriver driver = CreateFakeDriver (20, 1);
  133. driver.Force16Colors = force16Colors;
  134. driver.AddStr ("Normal");
  135. driver.CurrentAttribute = new Attribute (Color.Red, Color.Black);
  136. driver.AddStr ("Red");
  137. driver.CurrentAttribute = new Attribute (Color.Blue, Color.Black);
  138. driver.AddStr ("Blue");
  139. string ansi = driver.ToAnsi ();
  140. Assert.True (driver.Force16Colors == force16Colors);
  141. // Should contain color changes within the line
  142. Assert.Contains ("Normal", ansi);
  143. Assert.Contains (expectedRed, ansi); // Red
  144. Assert.Contains (expectedBlue, ansi); // Blue
  145. }
  146. [Fact]
  147. public void ToAnsi_Large_Buffer ()
  148. {
  149. // Test with a larger buffer to stress performance
  150. IDriver driver = CreateFakeDriver (200, 50);
  151. // Fill with some content
  152. for (int row = 0; row < 50; row++)
  153. {
  154. driver.Move (0, row);
  155. driver.CurrentAttribute = new Attribute ((ColorName16)(row % 16), Color.Black);
  156. driver.AddStr ($"Row {row:D2} content");
  157. }
  158. string ansi = driver.ToAnsi ();
  159. // Should contain all rows
  160. Assert.Contains ("Row 00", ansi);
  161. Assert.Contains ("Row 49", ansi);
  162. // Should have proper newlines (50 content lines + 50 newlines)
  163. Assert.Equal (50, ansi.Count (c => c == '\n'));
  164. }
  165. [Fact]
  166. public void ToAnsi_RGB_Colors ()
  167. {
  168. IDriver driver = CreateFakeDriver (10, 1);
  169. // Use RGB colors (when not forcing 16 colors)
  170. driver.Force16Colors = false;
  171. try
  172. {
  173. driver.CurrentAttribute = new Attribute (new Color (255, 0, 0), new Color (0, 255, 0));
  174. driver.AddStr ("RGB");
  175. string ansi = driver.ToAnsi ();
  176. // Should contain RGB color codes
  177. Assert.Contains ("\u001b[38;2;255;0;0m", ansi); // Red foreground RGB
  178. Assert.Contains ("\u001b[48;2;0;255;0m", ansi); // Green background RGB
  179. }
  180. finally
  181. {
  182. driver.Force16Colors = true; // Reset
  183. }
  184. }
  185. [Fact]
  186. public void ToAnsi_Force16Colors ()
  187. {
  188. IDriver driver = CreateFakeDriver (10, 1);
  189. // Force 16 colors
  190. driver.Force16Colors = true;
  191. driver.CurrentAttribute = new Attribute (Color.Red, Color.Blue);
  192. driver.AddStr ("16Color");
  193. string ansi = driver.ToAnsi ();
  194. // Should contain 16-color codes, not RGB
  195. Assert.Contains ("\u001b[31m", ansi); // Red foreground (16-color)
  196. Assert.Contains ("\u001b[44m", ansi); // Blue background (16-color)
  197. Assert.DoesNotContain ("\u001b[38;2;", ansi); // No RGB codes
  198. }
  199. [Theory]
  200. [InlineData (true, "\u001b[31m", "\u001b[32m", "\u001b[34m", "\u001b[33m", "\u001b[35m", "\u001b[36m")]
  201. [InlineData (false, "\u001b[38;2;255;0;0m", "\u001b[38;2;0;128;0m", "\u001b[38;2;0;0;255", "\u001b[38;2;255;255;0m", "\u001b[38;2;255;0;255m", "\u001b[38;2;0;255;255m")]
  202. public void ToAnsi_Multiple_Attributes_Per_Line (
  203. bool force16Colors,
  204. string expectedRed,
  205. string expectedGreen,
  206. string expectedBlue,
  207. string expectedYellow,
  208. string expectedMagenta,
  209. string expectedCyan
  210. )
  211. {
  212. IDriver driver = CreateFakeDriver (50, 1);
  213. driver.Force16Colors = force16Colors;
  214. // Create a line with many attribute changes
  215. string [] colors = { "Red", "Green", "Blue", "Yellow", "Magenta", "Cyan" };
  216. foreach (string colorName in colors)
  217. {
  218. Color fg = colorName switch
  219. {
  220. "Red" => Color.Red,
  221. "Green" => Color.Green,
  222. "Blue" => Color.Blue,
  223. "Yellow" => Color.Yellow,
  224. "Magenta" => Color.Magenta,
  225. "Cyan" => Color.Cyan,
  226. _ => Color.White
  227. };
  228. driver.CurrentAttribute = new (fg, Color.Black);
  229. driver.AddStr (colorName);
  230. }
  231. string ansi = driver.ToAnsi ();
  232. Assert.True (driver.Force16Colors == force16Colors);
  233. // Should contain multiple color codes
  234. Assert.Contains (expectedRed, ansi); // Red
  235. Assert.Contains (expectedGreen, ansi); // Green
  236. Assert.Contains (expectedBlue, ansi); // Blue
  237. Assert.Contains (expectedYellow, ansi); // Yellow
  238. Assert.Contains (expectedMagenta, ansi); // Magenta
  239. Assert.Contains (expectedCyan, ansi); // Cyan
  240. }
  241. [Fact]
  242. public void ToAnsi_Special_Characters ()
  243. {
  244. IDriver driver = CreateFakeDriver (20, 1);
  245. // Test backslash character
  246. driver.AddStr ("Backslash:");
  247. driver.AddRune ('\\');
  248. string ansi = driver.ToAnsi ();
  249. Assert.Contains ("Backslash:", ansi);
  250. Assert.Contains ("\\", ansi);
  251. }
  252. [Fact]
  253. public void ToAnsi_Buffer_Boundary_Conditions ()
  254. {
  255. // Test with minimum buffer size
  256. IDriver driver = CreateFakeDriver (1, 1);
  257. driver.AddStr ("X");
  258. string ansi = driver.ToAnsi ();
  259. Assert.Contains ("X", ansi);
  260. Assert.Contains ("\n", ansi);
  261. // Test with very wide buffer
  262. driver = CreateFakeDriver (1000, 1);
  263. driver.AddStr ("Wide");
  264. ansi = driver.ToAnsi ();
  265. Assert.Contains ("Wide", ansi);
  266. Assert.True (ansi.Length > 1000); // Should have many spaces
  267. }
  268. [Fact]
  269. public void ToAnsi_Empty_Lines ()
  270. {
  271. IDriver driver = CreateFakeDriver (10, 3);
  272. // Only write to first and third lines
  273. driver.AddStr ("First");
  274. driver.Move (0, 2);
  275. driver.AddStr ("Third");
  276. string ansi = driver.ToAnsi ();
  277. string [] lines = ansi.Split ('\n');
  278. Assert.Equal (4, lines.Length); // 3 content lines + 1 empty line at end
  279. Assert.Contains ("First", lines [0]);
  280. Assert.Contains ("Third", lines [2]);
  281. }
  282. [Fact]
  283. public void ToAnsi_Performance_Stress_Test ()
  284. {
  285. // Create a large buffer and fill it completely
  286. const int width = 200;
  287. const int height = 100;
  288. IDriver driver = CreateFakeDriver (width, height);
  289. // Fill every cell with different content and colors
  290. for (int row = 0; row < height; row++)
  291. {
  292. for (int col = 0; col < width; col++)
  293. {
  294. driver.Move (col, row);
  295. driver.CurrentAttribute = new Attribute ((ColorName16)((row + col) % 16), Color.Black);
  296. driver.AddRune ((char)('A' + ((row + col) % 26)));
  297. }
  298. }
  299. // This should complete in reasonable time and not throw
  300. string ansi = driver.ToAnsi ();
  301. Assert.NotNull (ansi);
  302. Assert.True (ansi.Length > width * height); // Should contain all characters plus ANSI codes
  303. }
  304. }