LineCanvasTests.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. /// <summary>
  126. /// Demonstrates when <see cref="BorderStyle.Rounded"/> corners are used. Notice how
  127. /// not all lines declare rounded. If there are 1+ lines intersecting and a corner is
  128. /// to be used then if any of them are rounded a rounded corner is used.
  129. /// </summary>
  130. [Fact, AutoInitShutdown]
  131. public void TestLineCanvas_Window_Rounded ()
  132. {
  133. var v = GetCanvas (out var canvas);
  134. // outer box
  135. canvas.AddLine (new Point (0, 0), 9, Orientation.Horizontal, BorderStyle.Rounded);
  136. // BorderStyle.Single is ignored because corner overlaps with the above line which is Rounded
  137. // this results in a rounded corner being used.
  138. canvas.AddLine (new Point (9, 0), 4, Orientation.Vertical, BorderStyle.Single);
  139. canvas.AddLine (new Point (9, 4), -9, Orientation.Horizontal, BorderStyle.Rounded);
  140. canvas.AddLine (new Point (0, 4), -4, Orientation.Vertical, BorderStyle.Single);
  141. // These lines say rounded but they will result in the T sections which are never rounded.
  142. canvas.AddLine (new Point (5, 0), 4, Orientation.Vertical, BorderStyle.Rounded);
  143. canvas.AddLine (new Point (0, 2), 9, Orientation.Horizontal, BorderStyle.Rounded);
  144. v.Redraw (v.Bounds);
  145. string looksLike =
  146. @"
  147. ╭────┬───╮
  148. │ │ │
  149. ├────┼───┤
  150. │ │ │
  151. ╰────┴───╯";
  152. TestHelpers.AssertDriverContentsAre (looksLike, output);
  153. }
  154. [Fact, AutoInitShutdown]
  155. public void TestLineCanvas_Window_Double ()
  156. {
  157. var v = GetCanvas (out var canvas);
  158. // outer box
  159. canvas.AddLine (new Point (0, 0), 9, Orientation.Horizontal, BorderStyle.Double);
  160. canvas.AddLine (new Point (9, 0), 4, Orientation.Vertical, BorderStyle.Double);
  161. canvas.AddLine (new Point (9, 4), -9, Orientation.Horizontal, BorderStyle.Double);
  162. canvas.AddLine (new Point (0, 4), -4, Orientation.Vertical, BorderStyle.Double);
  163. canvas.AddLine (new Point (5, 0), 4, Orientation.Vertical, BorderStyle.Double);
  164. canvas.AddLine (new Point (0, 2), 9, Orientation.Horizontal, BorderStyle.Double);
  165. v.Redraw (v.Bounds);
  166. string looksLike =
  167. @"
  168. ╔════╦═══╗
  169. ║ ║ ║
  170. ╠════╬═══╣
  171. ║ ║ ║
  172. ╚════╩═══╝";
  173. TestHelpers.AssertDriverContentsAre (looksLike, output);
  174. }
  175. [Theory, AutoInitShutdown]
  176. [InlineData(BorderStyle.Single)]
  177. [InlineData(BorderStyle.Rounded)]
  178. public void TestLineCanvas_Window_DoubleTop_SingleSides (BorderStyle thinStyle)
  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, thinStyle);
  184. canvas.AddLine (new Point (9, 4), -9, Orientation.Horizontal, BorderStyle.Double);
  185. canvas.AddLine (new Point (0, 4), -4, Orientation.Vertical, thinStyle);
  186. canvas.AddLine (new Point (5, 0), 4, Orientation.Vertical,thinStyle);
  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. ";
  197. TestHelpers.AssertDriverContentsAre (looksLike, output);
  198. }
  199. [Theory, AutoInitShutdown]
  200. [InlineData(BorderStyle.Single)]
  201. [InlineData(BorderStyle.Rounded)]
  202. public void TestLineCanvas_Window_SingleTop_DoubleSides (BorderStyle thinStyle)
  203. {
  204. var v = GetCanvas (out var canvas);
  205. // outer box
  206. canvas.AddLine (new Point (0, 0), 9, Orientation.Horizontal, thinStyle);
  207. canvas.AddLine (new Point (9, 0), 4, Orientation.Vertical, BorderStyle.Double);
  208. canvas.AddLine (new Point (9, 4), -9, Orientation.Horizontal,thinStyle);
  209. canvas.AddLine (new Point (0, 4), -4, Orientation.Vertical, BorderStyle.Double);
  210. canvas.AddLine (new Point (5, 0), 4, Orientation.Vertical, BorderStyle.Double);
  211. canvas.AddLine (new Point (0, 2), 9, Orientation.Horizontal, thinStyle);
  212. v.Redraw (v.Bounds);
  213. string looksLike =
  214. @"
  215. ╓────╥───╖
  216. ║ ║ ║
  217. ╟────╫───╢
  218. ║ ║ ║
  219. ╙────╨───╜
  220. ";
  221. TestHelpers.AssertDriverContentsAre (looksLike, output);
  222. }
  223. private View GetCanvas (out LineCanvas canvas)
  224. {
  225. var v = new View {
  226. Width = 10,
  227. Height = 5,
  228. Bounds = new Rect (0, 0, 10, 5)
  229. };
  230. var canvasCopy = canvas = new LineCanvas ();
  231. v.DrawContentComplete += (r)=> canvasCopy.Draw (v, v.Bounds);
  232. return v;
  233. }
  234. }
  235. }