LineCanvasTests.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. using Terminal.Gui.Graphs;
  2. using Xunit;
  3. using Xunit.Abstractions;
  4. namespace Terminal.Gui.CoreTests {
  5. public class LineCanvasTests {
  6. readonly ITestOutputHelper output;
  7. public LineCanvasTests (ITestOutputHelper output)
  8. {
  9. this.output = output;
  10. }
  11. [Fact, AutoInitShutdown]
  12. public void TestLineCanvas_Dot ()
  13. {
  14. var v = GetCanvas (out var canvas);
  15. canvas.AddLine (new Point (0, 0), 0, Orientation.Horizontal, BorderStyle.Single);
  16. v.Redraw (v.Bounds);
  17. string looksLike =
  18. @"
  19. .";
  20. TestHelpers.AssertDriverContentsAre (looksLike, output);
  21. }
  22. [InlineData (BorderStyle.Single)]
  23. [InlineData (BorderStyle.Rounded)]
  24. [Theory, AutoInitShutdown]
  25. public void TestLineCanvas_Horizontal (BorderStyle style)
  26. {
  27. var v = GetCanvas (out var canvas);
  28. canvas.AddLine (new Point (0, 0), 1, Orientation.Horizontal, style);
  29. v.Redraw (v.Bounds);
  30. string looksLike =
  31. @"
  32. ──";
  33. TestHelpers.AssertDriverContentsAre (looksLike, output);
  34. }
  35. [Fact, AutoInitShutdown]
  36. public void TestLineCanvas_Horizontal_Double ()
  37. {
  38. var v = GetCanvas (out var canvas);
  39. canvas.AddLine (new Point (0, 0), 1, Orientation.Horizontal, BorderStyle.Double);
  40. v.Redraw (v.Bounds);
  41. string looksLike =
  42. @"
  43. ══";
  44. TestHelpers.AssertDriverContentsAre (looksLike, output);
  45. }
  46. [InlineData (BorderStyle.Single)]
  47. [InlineData (BorderStyle.Rounded)]
  48. [Theory, AutoInitShutdown]
  49. public void TestLineCanvas_Vertical (BorderStyle style)
  50. {
  51. var v = GetCanvas (out var canvas);
  52. canvas.AddLine (new Point (0, 0), 1, Orientation.Vertical, style);
  53. v.Redraw (v.Bounds);
  54. string looksLike =
  55. @"
  56. │";
  57. TestHelpers.AssertDriverContentsAre (looksLike, output);
  58. }
  59. [Fact, AutoInitShutdown]
  60. public void TestLineCanvas_Vertical_Double ()
  61. {
  62. var v = GetCanvas (out var canvas);
  63. canvas.AddLine (new Point (0, 0), 1, Orientation.Vertical, BorderStyle.Double);
  64. v.Redraw (v.Bounds);
  65. string looksLike =
  66. @"
  67. ║";
  68. TestHelpers.AssertDriverContentsAre (looksLike, output);
  69. }
  70. /// <summary>
  71. /// This test demonstrates that corners are only drawn when lines overlap.
  72. /// Not when they terminate adjacent to one another.
  73. /// </summary>
  74. [Fact, AutoInitShutdown]
  75. public void TestLineCanvas_Corner_NoOverlap ()
  76. {
  77. var v = GetCanvas (out var canvas);
  78. canvas.AddLine (new Point (0, 0), 1, Orientation.Horizontal, BorderStyle.Single);
  79. canvas.AddLine (new Point (0, 1), 1, Orientation.Vertical, BorderStyle.Single);
  80. v.Redraw (v.Bounds);
  81. string looksLike =
  82. @"
  83. ──
  84. │";
  85. TestHelpers.AssertDriverContentsAre (looksLike, output);
  86. }
  87. /// <summary>
  88. /// This test demonstrates how to correctly trigger a corner. By
  89. /// overlapping the lines in the same cell
  90. /// </summary>
  91. [Fact, AutoInitShutdown]
  92. public void TestLineCanvas_Corner_Correct ()
  93. {
  94. var v = GetCanvas (out var canvas);
  95. canvas.AddLine (new Point (0, 0), 1, Orientation.Horizontal, BorderStyle.Single);
  96. canvas.AddLine (new Point (0, 0), 2, Orientation.Vertical, BorderStyle.Single);
  97. v.Redraw (v.Bounds);
  98. string looksLike =
  99. @"
  100. ┌─
  101. │";
  102. TestHelpers.AssertDriverContentsAre (looksLike, output);
  103. }
  104. [Fact, AutoInitShutdown]
  105. public void TestLineCanvas_Window ()
  106. {
  107. var v = GetCanvas (out var canvas);
  108. // outer box
  109. canvas.AddLine (new Point (0, 0), 9, Orientation.Horizontal, BorderStyle.Single);
  110. canvas.AddLine (new Point (9, 0), 4, Orientation.Vertical, BorderStyle.Single);
  111. canvas.AddLine (new Point (9, 4), -9, Orientation.Horizontal, BorderStyle.Single);
  112. canvas.AddLine (new Point (0, 4), -4, Orientation.Vertical, BorderStyle.Single);
  113. canvas.AddLine (new Point (5, 0), 4, Orientation.Vertical, BorderStyle.Single);
  114. canvas.AddLine (new Point (0, 2), 9, Orientation.Horizontal, BorderStyle.Single);
  115. v.Redraw (v.Bounds);
  116. string looksLike =
  117. @"
  118. ┌────┬───┐
  119. │ │ │
  120. ├────┼───┤
  121. │ │ │
  122. └────┴───┘";
  123. TestHelpers.AssertDriverContentsAre (looksLike, output);
  124. }
  125. [Fact, AutoInitShutdown]
  126. public void TestLineCanvas_PositiveLocation ()
  127. {
  128. var x = 1;
  129. var y = 1;
  130. var v = GetCanvas (out var canvas, x, y);
  131. // outer box
  132. canvas.AddLine (new Point (x, y), 9, Orientation.Horizontal, BorderStyle.Single);
  133. canvas.AddLine (new Point (x + 9, y + 0), 4, Orientation.Vertical, BorderStyle.Single);
  134. canvas.AddLine (new Point (x + 9, y + 4), -9, Orientation.Horizontal, BorderStyle.Single);
  135. canvas.AddLine (new Point (x + 0, y + 4), -4, Orientation.Vertical, BorderStyle.Single);
  136. canvas.AddLine (new Point (x + 5, y + 0), 4, Orientation.Vertical, BorderStyle.Single);
  137. canvas.AddLine (new Point (x + 0, y + 2), 9, Orientation.Horizontal, BorderStyle.Single);
  138. v.Redraw (v.Bounds);
  139. string looksLike =
  140. @"
  141. ┌────┬───┐
  142. │ │ │
  143. ├────┼───┤
  144. │ │ │
  145. └────┴───┘";
  146. TestHelpers.AssertDriverContentsAre (looksLike, output);
  147. }
  148. /// <summary>
  149. /// Demonstrates when <see cref="BorderStyle.Rounded"/> corners are used. Notice how
  150. /// not all lines declare rounded. If there are 1+ lines intersecting and a corner is
  151. /// to be used then if any of them are rounded a rounded corner is used.
  152. /// </summary>
  153. [Fact, AutoInitShutdown]
  154. public void TestLineCanvas_Window_Rounded ()
  155. {
  156. var v = GetCanvas (out var canvas);
  157. // outer box
  158. canvas.AddLine (new Point (0, 0), 9, Orientation.Horizontal, BorderStyle.Rounded);
  159. // BorderStyle.Single is ignored because corner overlaps with the above line which is Rounded
  160. // this results in a rounded corner being used.
  161. canvas.AddLine (new Point (9, 0), 4, Orientation.Vertical, BorderStyle.Single);
  162. canvas.AddLine (new Point (9, 4), -9, Orientation.Horizontal, BorderStyle.Rounded);
  163. canvas.AddLine (new Point (0, 4), -4, Orientation.Vertical, BorderStyle.Single);
  164. // These lines say rounded but they will result in the T sections which are never rounded.
  165. canvas.AddLine (new Point (5, 0), 4, Orientation.Vertical, BorderStyle.Rounded);
  166. canvas.AddLine (new Point (0, 2), 9, Orientation.Horizontal, BorderStyle.Rounded);
  167. v.Redraw (v.Bounds);
  168. string looksLike =
  169. @"
  170. ╭────┬───╮
  171. │ │ │
  172. ├────┼───┤
  173. │ │ │
  174. ╰────┴───╯";
  175. TestHelpers.AssertDriverContentsAre (looksLike, output);
  176. }
  177. [Fact, AutoInitShutdown]
  178. public void TestLineCanvas_Window_Double ()
  179. {
  180. var v = GetCanvas (out var canvas);
  181. // outer box
  182. canvas.AddLine (new Point (0, 0), 9, Orientation.Horizontal, BorderStyle.Double);
  183. canvas.AddLine (new Point (9, 0), 4, Orientation.Vertical, BorderStyle.Double);
  184. canvas.AddLine (new Point (9, 4), -9, Orientation.Horizontal, BorderStyle.Double);
  185. canvas.AddLine (new Point (0, 4), -4, Orientation.Vertical, BorderStyle.Double);
  186. canvas.AddLine (new Point (5, 0), 4, Orientation.Vertical, BorderStyle.Double);
  187. canvas.AddLine (new Point (0, 2), 9, Orientation.Horizontal, BorderStyle.Double);
  188. v.Redraw (v.Bounds);
  189. string looksLike =
  190. @"
  191. ╔════╦═══╗
  192. ║ ║ ║
  193. ╠════╬═══╣
  194. ║ ║ ║
  195. ╚════╩═══╝";
  196. TestHelpers.AssertDriverContentsAre (looksLike, output);
  197. }
  198. [Theory, AutoInitShutdown]
  199. [InlineData (BorderStyle.Single)]
  200. [InlineData (BorderStyle.Rounded)]
  201. public void TestLineCanvas_Window_DoubleTop_SingleSides (BorderStyle thinStyle)
  202. {
  203. var v = GetCanvas (out var canvas);
  204. // outer box
  205. canvas.AddLine (new Point (0, 0), 9, Orientation.Horizontal, BorderStyle.Double);
  206. canvas.AddLine (new Point (9, 0), 4, Orientation.Vertical, thinStyle);
  207. canvas.AddLine (new Point (9, 4), -9, Orientation.Horizontal, BorderStyle.Double);
  208. canvas.AddLine (new Point (0, 4), -4, Orientation.Vertical, thinStyle);
  209. canvas.AddLine (new Point (5, 0), 4, Orientation.Vertical, thinStyle);
  210. canvas.AddLine (new Point (0, 2), 9, Orientation.Horizontal, BorderStyle.Double);
  211. v.Redraw (v.Bounds);
  212. string looksLike =
  213. @"
  214. ╒════╤═══╕
  215. │ │ │
  216. ╞════╪═══╡
  217. │ │ │
  218. ╘════╧═══╛
  219. ";
  220. TestHelpers.AssertDriverContentsAre (looksLike, output);
  221. }
  222. [Theory, AutoInitShutdown]
  223. [InlineData (BorderStyle.Single)]
  224. [InlineData (BorderStyle.Rounded)]
  225. public void TestLineCanvas_Window_SingleTop_DoubleSides (BorderStyle thinStyle)
  226. {
  227. var v = GetCanvas (out var canvas);
  228. // outer box
  229. canvas.AddLine (new Point (0, 0), 9, Orientation.Horizontal, thinStyle);
  230. canvas.AddLine (new Point (9, 0), 4, Orientation.Vertical, BorderStyle.Double);
  231. canvas.AddLine (new Point (9, 4), -9, Orientation.Horizontal, thinStyle);
  232. canvas.AddLine (new Point (0, 4), -4, Orientation.Vertical, BorderStyle.Double);
  233. canvas.AddLine (new Point (5, 0), 4, Orientation.Vertical, BorderStyle.Double);
  234. canvas.AddLine (new Point (0, 2), 9, Orientation.Horizontal, thinStyle);
  235. v.Redraw (v.Bounds);
  236. string looksLike =
  237. @"
  238. ╓────╥───╖
  239. ║ ║ ║
  240. ╟────╫───╢
  241. ║ ║ ║
  242. ╙────╨───╜
  243. ";
  244. TestHelpers.AssertDriverContentsAre (looksLike, output);
  245. }
  246. private View GetCanvas (out LineCanvas canvas, int x = 0, int y = 0)
  247. {
  248. var v = new View {
  249. Width = 10,
  250. Height = 5,
  251. Bounds = new Rect (x, y, 10, 5)
  252. };
  253. var canvasCopy = canvas = new LineCanvas ();
  254. v.DrawContentComplete += (r) => canvasCopy.Draw (v, v.Bounds);
  255. return v;
  256. }
  257. }
  258. }