CellTests.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. using System.Text;
  2. using Xunit.Abstractions;
  3. namespace Terminal.Gui.DrawingTests;
  4. public class CellTests (ITestOutputHelper output)
  5. {
  6. [Fact]
  7. public void Constructor_Defaults ()
  8. {
  9. var c = new Cell ();
  10. Assert.True (c is { });
  11. Assert.Equal (0, c.Rune.Value);
  12. Assert.Null (c.Attribute);
  13. }
  14. [Fact]
  15. public void Equals_False ()
  16. {
  17. var c1 = new Cell ();
  18. var c2 = new Cell
  19. {
  20. Rune = new ('a'), Attribute = new (Color.Red)
  21. };
  22. Assert.False (c1.Equals (c2));
  23. Assert.False (c2.Equals (c1));
  24. c1.Rune = new ('a');
  25. c1.Attribute = new ();
  26. Assert.Equal (c1.Rune, c2.Rune);
  27. Assert.False (c1.Equals (c2));
  28. Assert.False (c2.Equals (c1));
  29. }
  30. [Fact]
  31. public void Equals_True ()
  32. {
  33. var c1 = new Cell ();
  34. var c2 = new Cell ();
  35. Assert.True (c1.Equals (c2));
  36. Assert.True (c2.Equals (c1));
  37. c1.Rune = new ('a');
  38. c1.Attribute = new ();
  39. c2.Rune = new ('a');
  40. c2.Attribute = new ();
  41. Assert.True (c1.Equals (c2));
  42. Assert.True (c2.Equals (c1));
  43. }
  44. [Fact]
  45. [AutoInitShutdown (configLocation: ConfigurationManager.ConfigLocations.DefaultOnly)]
  46. public void Cell_LoadCells_InheritsPreviousAttribute ()
  47. {
  48. List<Cell> cells = [];
  49. foreach (KeyValuePair<string, ColorScheme> color in Colors.ColorSchemes)
  50. {
  51. string csName = color.Key;
  52. foreach (Rune rune in csName.EnumerateRunes ())
  53. {
  54. cells.Add (new() { Rune = rune, Attribute = color.Value.Normal });
  55. }
  56. cells.Add (new() { Rune = (Rune)'\n', Attribute = color.Value.Focus });
  57. }
  58. TextView tv = CreateTextView ();
  59. tv.Load (cells);
  60. var top = new Toplevel ();
  61. top.Add (tv);
  62. RunState rs = Application.Begin (top);
  63. Assert.True (tv.InheritsPreviousAttribute);
  64. var expectedText = @"
  65. TopLevel
  66. Base
  67. Dialog
  68. Menu
  69. Error ";
  70. TestHelpers.AssertDriverContentsWithFrameAre (expectedText, output);
  71. Attribute [] attributes =
  72. {
  73. // 0
  74. Colors.ColorSchemes ["TopLevel"].Normal,
  75. // 1
  76. Colors.ColorSchemes ["Base"].Normal,
  77. // 2
  78. Colors.ColorSchemes ["Dialog"].Normal,
  79. // 3
  80. Colors.ColorSchemes ["Menu"].Normal,
  81. // 4
  82. Colors.ColorSchemes ["Error"].Normal,
  83. // 5
  84. tv.ColorScheme!.Focus
  85. };
  86. var expectedColor = @"
  87. 0000000055
  88. 1111555555
  89. 2222225555
  90. 3333555555
  91. 4444455555";
  92. TestHelpers.AssertDriverAttributesAre (expectedColor, Application.Driver, attributes);
  93. tv.WordWrap = true;
  94. Application.Refresh ();
  95. TestHelpers.AssertDriverContentsWithFrameAre (expectedText, output);
  96. TestHelpers.AssertDriverAttributesAre (expectedColor, Application.Driver, attributes);
  97. tv.CursorPosition = new (6, 2);
  98. tv.SelectionStartColumn = 0;
  99. tv.SelectionStartRow = 0;
  100. Assert.Equal ($"TopLevel{Environment.NewLine}Base{Environment.NewLine}Dialog", tv.SelectedText);
  101. tv.Copy ();
  102. tv.Selecting = false;
  103. tv.CursorPosition = new (2, 4);
  104. tv.Paste ();
  105. Application.Refresh ();
  106. expectedText = @"
  107. TopLevel
  108. Base
  109. Dialog
  110. Menu
  111. ErTopLevel
  112. Base
  113. Dialogror ";
  114. TestHelpers.AssertDriverContentsWithFrameAre (expectedText, output);
  115. expectedColor = @"
  116. 0000000055
  117. 1111555555
  118. 2222225555
  119. 3333555555
  120. 4444444444
  121. 4444555555
  122. 4444444445";
  123. TestHelpers.AssertDriverAttributesAre (expectedColor, Application.Driver, attributes);
  124. tv.Undo ();
  125. tv.CursorPosition = new (0, 3);
  126. tv.SelectionStartColumn = 0;
  127. tv.SelectionStartRow = 0;
  128. Assert.Equal (
  129. $"TopLevel{Environment.NewLine}Base{Environment.NewLine}Dialog{Environment.NewLine}",
  130. tv.SelectedText
  131. );
  132. tv.Copy ();
  133. tv.Selecting = false;
  134. tv.CursorPosition = new (2, 4);
  135. tv.Paste ();
  136. Application.Refresh ();
  137. expectedText = @"
  138. TopLevel
  139. Base
  140. Dialog
  141. Menu
  142. ErTopLevel
  143. Base
  144. Dialog
  145. ror ";
  146. TestHelpers.AssertDriverContentsWithFrameAre (expectedText, output);
  147. expectedColor = @"
  148. 0000000055
  149. 1111555555
  150. 2222225555
  151. 3333555555
  152. 4444444444
  153. 4444555555
  154. 4444445555
  155. 4445555555";
  156. TestHelpers.AssertDriverAttributesAre (expectedColor, Application.Driver, attributes);
  157. Application.End (rs);
  158. top.Dispose ();
  159. }
  160. [Fact]
  161. public void Cell_LoadCells_Without_ColorScheme_Is_Never_Null ()
  162. {
  163. List<Cell> cells = new ()
  164. {
  165. new() { Rune = new ('T') },
  166. new() { Rune = new ('e') },
  167. new() { Rune = new ('s') },
  168. new() { Rune = new ('t') }
  169. };
  170. TextView tv = CreateTextView ();
  171. var top = new Toplevel ();
  172. top.Add (tv);
  173. tv.Load (cells);
  174. for (var i = 0; i < tv.Lines; i++)
  175. {
  176. List<Cell> line = tv.GetLine (i);
  177. foreach (Cell c in line)
  178. {
  179. Assert.NotNull (c.Attribute);
  180. }
  181. }
  182. }
  183. [Fact]
  184. [AutoInitShutdown]
  185. public void CellEventArgs_WordWrap_True ()
  186. {
  187. var eventCount = 0;
  188. List<List<Cell>> text =
  189. [
  190. Cell.ToCells (
  191. "This is the first line.".ToRunes ()
  192. ),
  193. Cell.ToCells (
  194. "This is the second line.".ToRunes ()
  195. )
  196. ];
  197. TextView tv = CreateTextView ();
  198. tv.DrawNormalColor += _textView_DrawColor;
  199. tv.DrawReadOnlyColor += _textView_DrawColor;
  200. tv.DrawSelectionColor += _textView_DrawColor;
  201. tv.DrawUsedColor += _textView_DrawColor;
  202. void _textView_DrawColor (object sender, CellEventArgs e)
  203. {
  204. Assert.Equal (e.Line [e.Col], text [e.UnwrappedPosition.Row] [e.UnwrappedPosition.Col]);
  205. eventCount++;
  206. }
  207. tv.Text = $"{Cell.ToString (text [0])}\n{Cell.ToString (text [1])}\n";
  208. Assert.False (tv.WordWrap);
  209. var top = new Toplevel ();
  210. top.Add (tv);
  211. Application.Begin (top);
  212. TestHelpers.AssertDriverContentsWithFrameAre (
  213. @"
  214. This is the first line.
  215. This is the second line.",
  216. output
  217. );
  218. tv.Width = 10;
  219. tv.Height = 25;
  220. tv.WordWrap = true;
  221. Application.Refresh ();
  222. TestHelpers.AssertDriverContentsWithFrameAre (
  223. @"
  224. This is
  225. the
  226. first
  227. line.
  228. This is
  229. the
  230. second
  231. line. ",
  232. output
  233. );
  234. Assert.Equal (eventCount, (text [0].Count + text [1].Count) * 2);
  235. top.Dispose ();
  236. }
  237. [Fact]
  238. public void ToString_Override ()
  239. {
  240. var c1 = new Cell ();
  241. var c2 = new Cell
  242. {
  243. Rune = new ('a'), Attribute = new (Color.Red)
  244. };
  245. Assert.Equal ("[\0, ]", c1.ToString ());
  246. Assert.Equal (
  247. "[a, [Red,Red]]",
  248. c2.ToString ()
  249. );
  250. }
  251. // TODO: Move the tests below to View or Color - they test ColorScheme, not Cell primitives.
  252. private TextView CreateTextView () { return new() { Width = 30, Height = 10 }; }
  253. }