ToAnsiTests.cs 13 KB

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