LineCanvasTests.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. namespace Terminal.Gui.DrawingTests;
  2. /// <summary>
  3. /// Pure unit tests for <see cref="LineCanvas"/> that don't require Application.Driver or View context.
  4. /// These tests focus on properties and behavior that don't depend on glyph rendering.
  5. ///
  6. /// Note: Tests that verify rendered output (ToString()) cannot be parallelized because LineCanvas
  7. /// depends on Application.Driver for glyph resolution and configuration. Those tests remain in UnitTests.
  8. /// </summary>
  9. public class LineCanvasTests : UnitTests.Parallelizable.ParallelizableBase
  10. {
  11. #region Basic API Tests
  12. [Fact]
  13. public void Empty_Canvas_ToString_Returns_EmptyString ()
  14. {
  15. var canvas = new LineCanvas ();
  16. Assert.Equal (string.Empty, canvas.ToString ());
  17. }
  18. [Fact]
  19. public void Clear_Removes_All_Lines ()
  20. {
  21. var canvas = new LineCanvas ();
  22. canvas.AddLine (new (0, 0), 5, Orientation.Horizontal, LineStyle.Single);
  23. canvas.AddLine (new (0, 0), 3, Orientation.Vertical, LineStyle.Single);
  24. canvas.Clear ();
  25. Assert.Empty (canvas.Lines);
  26. Assert.Equal (Rectangle.Empty, canvas.Bounds);
  27. Assert.Equal (string.Empty, canvas.ToString ());
  28. }
  29. [Fact]
  30. public void Lines_Property_Returns_ReadOnly_Collection ()
  31. {
  32. var canvas = new LineCanvas ();
  33. canvas.AddLine (new (0, 0), 5, Orientation.Horizontal, LineStyle.Single);
  34. Assert.Single (canvas.Lines);
  35. Assert.IsAssignableFrom<IReadOnlyCollection<StraightLine>> (canvas.Lines);
  36. }
  37. [Fact]
  38. public void AddLine_Adds_Line_To_Collection ()
  39. {
  40. var canvas = new LineCanvas ();
  41. Assert.Empty (canvas.Lines);
  42. canvas.AddLine (new (0, 0), 5, Orientation.Horizontal, LineStyle.Single);
  43. Assert.Single (canvas.Lines);
  44. canvas.AddLine (new (0, 0), 3, Orientation.Vertical, LineStyle.Single);
  45. Assert.Equal (2, canvas.Lines.Count);
  46. }
  47. [Fact]
  48. public void Constructor_With_Lines_Creates_Canvas_With_Lines ()
  49. {
  50. var lines = new[]
  51. {
  52. new StraightLine (new (0, 0), 5, Orientation.Horizontal, LineStyle.Single),
  53. new StraightLine (new (0, 0), 3, Orientation.Vertical, LineStyle.Single)
  54. };
  55. var canvas = new LineCanvas (lines);
  56. Assert.Equal (2, canvas.Lines.Count);
  57. }
  58. #endregion
  59. #region Bounds Tests - Tests for Bounds property
  60. [Theory]
  61. [InlineData (0, 0, 0, 0, 0, 1, 1)]
  62. [InlineData (0, 0, 1, 0, 0, 1, 1)]
  63. [InlineData (0, 0, 2, 0, 0, 2, 2)]
  64. [InlineData (0, 0, 3, 0, 0, 3, 3)]
  65. [InlineData (0, 0, -1, 0, 0, 1, 1)]
  66. [InlineData (0, 0, -2, -1, -1, 2, 2)]
  67. [InlineData (0, 0, -3, -2, -2, 3, 3)]
  68. public void Viewport_H_And_V_Lines_Both_Positive (
  69. int x,
  70. int y,
  71. int length,
  72. int expectedX,
  73. int expectedY,
  74. int expectedWidth,
  75. int expectedHeight
  76. )
  77. {
  78. var canvas = new LineCanvas ();
  79. canvas.AddLine (new (x, y), length, Orientation.Horizontal, LineStyle.Single);
  80. canvas.AddLine (new (x, y), length, Orientation.Vertical, LineStyle.Single);
  81. Assert.Equal (new (expectedX, expectedY, expectedWidth, expectedHeight), canvas.Bounds);
  82. }
  83. [Theory]
  84. [InlineData (0, 0, 0, 0, 0, 1, 1)]
  85. [InlineData (0, 0, 1, 0, 0, 1, 1)]
  86. [InlineData (0, 0, 2, 0, 0, 2, 1)]
  87. [InlineData (0, 0, 3, 0, 0, 3, 1)]
  88. [InlineData (0, 0, -1, 0, 0, 1, 1)]
  89. [InlineData (0, 0, -2, -1, 0, 2, 1)]
  90. [InlineData (0, 0, -3, -2, 0, 3, 1)]
  91. public void Viewport_H_Line (
  92. int x,
  93. int y,
  94. int length,
  95. int expectedX,
  96. int expectedY,
  97. int expectedWidth,
  98. int expectedHeight
  99. )
  100. {
  101. var canvas = new LineCanvas ();
  102. canvas.AddLine (new (x, y), length, Orientation.Horizontal, LineStyle.Single);
  103. Assert.Equal (new (expectedX, expectedY, expectedWidth, expectedHeight), canvas.Bounds);
  104. }
  105. [Fact]
  106. public void Bounds_Specific_Coordinates ()
  107. {
  108. var canvas = new LineCanvas ();
  109. canvas.AddLine (new (5, 5), 3, Orientation.Horizontal, LineStyle.Single);
  110. Assert.Equal (new (5, 5, 3, 1), canvas.Bounds);
  111. }
  112. [Fact]
  113. public void Bounds_Empty_Canvas_Returns_Empty_Rectangle ()
  114. {
  115. var canvas = new LineCanvas ();
  116. Assert.Equal (Rectangle.Empty, canvas.Bounds);
  117. }
  118. [Fact]
  119. public void Bounds_Single_Point_Zero_Length ()
  120. {
  121. var canvas = new LineCanvas ();
  122. canvas.AddLine (new (5, 5), 0, Orientation.Horizontal, LineStyle.Single);
  123. Assert.Equal (new (5, 5, 1, 1), canvas.Bounds);
  124. }
  125. [Fact]
  126. public void Bounds_Horizontal_Line ()
  127. {
  128. var canvas = new LineCanvas ();
  129. canvas.AddLine (new (2, 3), 5, Orientation.Horizontal, LineStyle.Single);
  130. Assert.Equal (new (2, 3, 5, 1), canvas.Bounds);
  131. }
  132. [Fact]
  133. public void Bounds_Vertical_Line ()
  134. {
  135. var canvas = new LineCanvas ();
  136. canvas.AddLine (new (2, 3), 5, Orientation.Vertical, LineStyle.Single);
  137. Assert.Equal (new (2, 3, 1, 5), canvas.Bounds);
  138. }
  139. [Fact]
  140. public void Bounds_Multiple_Lines_Returns_Union ()
  141. {
  142. var canvas = new LineCanvas ();
  143. canvas.AddLine (new (0, 0), 5, Orientation.Horizontal, LineStyle.Single);
  144. canvas.AddLine (new (0, 0), 3, Orientation.Vertical, LineStyle.Single);
  145. Assert.Equal (new (0, 0, 5, 3), canvas.Bounds);
  146. }
  147. [Fact]
  148. public void Bounds_Negative_Length_Line ()
  149. {
  150. var canvas = new LineCanvas ();
  151. canvas.AddLine (new (5, 5), -3, Orientation.Horizontal, LineStyle.Single);
  152. // Line from (5,5) going left 3 positions: includes points 3, 4, 5 (width 3, X starts at 3)
  153. Assert.Equal (new (3, 5, 3, 1), canvas.Bounds);
  154. }
  155. [Fact]
  156. public void Bounds_Complex_Box ()
  157. {
  158. var canvas = new LineCanvas ();
  159. // top
  160. canvas.AddLine (new (0, 0), 3, Orientation.Horizontal, LineStyle.Single);
  161. // left
  162. canvas.AddLine (new (0, 0), 2, Orientation.Vertical, LineStyle.Single);
  163. // right
  164. canvas.AddLine (new (2, 0), 2, Orientation.Vertical, LineStyle.Single);
  165. // bottom
  166. canvas.AddLine (new (0, 2), 3, Orientation.Horizontal, LineStyle.Single);
  167. Assert.Equal (new (0, 0, 3, 3), canvas.Bounds);
  168. }
  169. #endregion
  170. #region Exclusion Tests
  171. [Fact]
  172. public void ClearExclusions_Clears_Exclusion_Region ()
  173. {
  174. var canvas = new LineCanvas ();
  175. canvas.AddLine (new (0, 0), 5, Orientation.Horizontal, LineStyle.Single);
  176. var region = new Region (new Rectangle (0, 0, 2, 1));
  177. canvas.Exclude (region);
  178. canvas.ClearExclusions ();
  179. // After clearing exclusions, GetMap should return all points
  180. var map = canvas.GetMap ();
  181. Assert.Equal (5, map.Count);
  182. }
  183. [Fact]
  184. public void Exclude_Removes_Points_From_Map ()
  185. {
  186. var canvas = new LineCanvas ();
  187. canvas.AddLine (new (0, 0), 5, Orientation.Horizontal, LineStyle.Single);
  188. var region = new Region (new Rectangle (0, 0, 2, 1));
  189. canvas.Exclude (region);
  190. var map = canvas.GetMap ();
  191. // Should have 5 - 2 = 3 points (excluding the first 2)
  192. Assert.Equal (3, map.Count);
  193. }
  194. #endregion
  195. #region Fill Property Tests
  196. [Fact]
  197. public void Fill_Property_Can_Be_Set ()
  198. {
  199. var foregroundFill = new SolidFill (new Color (255, 0));
  200. var backgroundFill = new SolidFill (new Color (0, 0));
  201. var fillPair = new FillPair (foregroundFill, backgroundFill);
  202. var canvas = new LineCanvas { Fill = fillPair };
  203. Assert.Equal (fillPair, canvas.Fill);
  204. }
  205. [Fact]
  206. public void Fill_Property_Defaults_To_Null ()
  207. {
  208. var canvas = new LineCanvas ();
  209. Assert.Null (canvas.Fill);
  210. }
  211. #endregion
  212. }