LineTests.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. namespace Terminal.Gui.ViewsTests;
  2. public class LineTests
  3. {
  4. [Fact]
  5. public void Line_DefaultConstructor_Horizontal ()
  6. {
  7. var line = new Line ();
  8. Assert.Equal (Orientation.Horizontal, line.Orientation);
  9. Assert.Equal (Dim.Fill (), line.Width);
  10. Assert.Equal (LineStyle.Single, line.Style);
  11. Assert.True (line.SuperViewRendersLineCanvas);
  12. Assert.False (line.CanFocus);
  13. line.Layout ();
  14. Assert.Equal (1, line.Frame.Height);
  15. }
  16. [Fact]
  17. public void Line_Horizontal_FillsWidth ()
  18. {
  19. var line = new Line { Orientation = Orientation.Horizontal };
  20. var container = new View { Width = 50, Height = 10 };
  21. container.Add (line);
  22. container.Layout ();
  23. Assert.Equal (50, line.Frame.Width);
  24. Assert.Equal (1, line.Frame.Height);
  25. }
  26. [Fact]
  27. public void Line_Vertical_FillsHeight ()
  28. {
  29. var line = new Line { Orientation = Orientation.Vertical };
  30. var container = new View { Width = 50, Height = 10 };
  31. container.Add (line);
  32. container.Layout ();
  33. Assert.Equal (1, line.Frame.Width);
  34. Assert.Equal (10, line.Frame.Height);
  35. }
  36. [Fact]
  37. public void Line_ChangeOrientation_UpdatesDimensions ()
  38. {
  39. var line = new Line { Orientation = Orientation.Horizontal };
  40. var container = new View { Width = 50, Height = 20 };
  41. container.Add (line);
  42. container.Layout ();
  43. Assert.Equal (50, line.Frame.Width);
  44. Assert.Equal (1, line.Frame.Height);
  45. // Change to vertical
  46. line.Orientation = Orientation.Vertical;
  47. container.Layout ();
  48. Assert.Equal (1, line.Frame.Width);
  49. Assert.Equal (20, line.Frame.Height);
  50. }
  51. [Fact]
  52. public void Line_Style_CanBeSet ()
  53. {
  54. var line = new Line { Style = LineStyle.Double };
  55. Assert.Equal (LineStyle.Double, line.Style);
  56. }
  57. [Theory]
  58. [InlineData (LineStyle.Single)]
  59. [InlineData (LineStyle.Double)]
  60. [InlineData (LineStyle.Heavy)]
  61. [InlineData (LineStyle.Rounded)]
  62. [InlineData (LineStyle.Dashed)]
  63. [InlineData (LineStyle.Dotted)]
  64. public void Line_SupportsDifferentLineStyles (LineStyle style)
  65. {
  66. var line = new Line { Style = style };
  67. Assert.Equal (style, line.Style);
  68. }
  69. [Fact]
  70. public void Line_DrawsCalled_Successfully ()
  71. {
  72. var app = new Window ();
  73. var line = new Line { Y = 1, Width = 10 };
  74. app.Add (line);
  75. app.BeginInit ();
  76. app.EndInit ();
  77. app.Layout ();
  78. // Just verify the line can be drawn without errors
  79. Exception exception = Record.Exception (() => app.Draw ());
  80. Assert.Null (exception);
  81. }
  82. [Fact]
  83. public void Line_WithBorder_DrawsSuccessfully ()
  84. {
  85. var app = new Window { Width = 20, Height = 10, BorderStyle = LineStyle.Single };
  86. // Add a line that intersects with the window border
  87. var line = new Line { X = 5, Y = 0, Height = Dim.Fill (), Orientation = Orientation.Vertical };
  88. app.Add (line);
  89. app.BeginInit ();
  90. app.EndInit ();
  91. app.Layout ();
  92. // Just verify the line and border can be drawn together without errors
  93. Exception exception = Record.Exception (() => app.Draw ());
  94. Assert.Null (exception);
  95. }
  96. [Fact]
  97. public void Line_MultipleIntersecting_DrawsSuccessfully ()
  98. {
  99. var app = new Window { Width = 30, Height = 15 };
  100. // Create intersecting lines
  101. var hLine = new Line { X = 5, Y = 5, Width = 15, Style = LineStyle.Single };
  102. var vLine = new Line
  103. {
  104. X = 12, Y = 2, Height = 8, Orientation = Orientation.Vertical, Style = LineStyle.Single
  105. };
  106. app.Add (hLine, vLine);
  107. app.BeginInit ();
  108. app.EndInit ();
  109. app.Layout ();
  110. // Just verify multiple intersecting lines can be drawn without errors
  111. Exception exception = Record.Exception (() => app.Draw ());
  112. Assert.Null (exception);
  113. }
  114. [Fact]
  115. public void Line_ExplicitWidthAndHeight_RespectValues ()
  116. {
  117. var line = new Line { Width = 10, Height = 1 };
  118. var container = new View { Width = 50, Height = 20 };
  119. container.Add (line);
  120. container.Layout ();
  121. Assert.Equal (10, line.Frame.Width);
  122. Assert.Equal (1, line.Frame.Height);
  123. }
  124. [Fact]
  125. public void Line_VerticalWithExplicitHeight_RespectValues ()
  126. {
  127. var line = new Line { Orientation = Orientation.Vertical };
  128. // Set height AFTER orientation to avoid it being reset
  129. line.Width = 1;
  130. line.Height = 8;
  131. var container = new View { Width = 50, Height = 20 };
  132. container.Add (line);
  133. container.Layout ();
  134. Assert.Equal (1, line.Frame.Width);
  135. Assert.Equal (8, line.Frame.Height);
  136. }
  137. [Fact]
  138. public void Line_SuperViewRendersLineCanvas_IsTrue ()
  139. {
  140. var line = new Line ();
  141. Assert.True (line.SuperViewRendersLineCanvas);
  142. }
  143. [Fact]
  144. public void Line_CannotFocus ()
  145. {
  146. var line = new Line ();
  147. Assert.False (line.CanFocus);
  148. }
  149. [Fact]
  150. public void Line_ImplementsIOrientation ()
  151. {
  152. var line = new Line ();
  153. Assert.IsAssignableFrom<IOrientation> (line);
  154. }
  155. [Fact]
  156. public void Line_Length_Get_ReturnsCorrectDimension ()
  157. {
  158. var line = new Line { Width = 20, Height = 1 };
  159. // For horizontal, Length should be Width
  160. line.Orientation = Orientation.Horizontal;
  161. Assert.Equal (line.Width, line.Length);
  162. Assert.Equal (1, line.Height.GetAnchor (0));
  163. // For vertical, Length should be Height
  164. line.Orientation = Orientation.Vertical;
  165. Assert.Equal (line.Height, line.Length);
  166. Assert.Equal (1, line.Width.GetAnchor (0));
  167. }
  168. [Fact]
  169. public void Line_OrientationChange_SwapsDimensions ()
  170. {
  171. var line = new Line ();
  172. var container = new View { Width = 50, Height = 20 };
  173. container.Add (line);
  174. // Start horizontal with custom dimensions
  175. line.Orientation = Orientation.Horizontal;
  176. line.Width = 30;
  177. line.Height = 1;
  178. container.Layout ();
  179. Assert.Equal (30, line.Frame.Width);
  180. Assert.Equal (1, line.Frame.Height);
  181. // Change to vertical - dimensions should swap
  182. line.Orientation = Orientation.Vertical;
  183. container.Layout ();
  184. Assert.Equal (1, line.Frame.Width);
  185. Assert.Equal (30, line.Frame.Height); // Width became Height
  186. }
  187. [Fact]
  188. public void Line_Dimensions_WorkSameAsInitializers ()
  189. {
  190. // Object initializers work same as sequential assignment
  191. // Test: new Line { Width = 15, Orientation = Orientation.Horizontal }
  192. // Expected: Width=15, Height=1
  193. Line line = new () { Width = 15, Orientation = Orientation.Horizontal };
  194. Assert.Equal (15, line.Width.GetAnchor (0));
  195. Assert.Equal (1, line.Height.GetAnchor (0));
  196. Assert.Equal (line.Length, line.Width); // Length should be Width for horizontal
  197. line = new ();
  198. line.Width = 15;
  199. line.Orientation = Orientation.Horizontal;
  200. Assert.Equal (15, line.Width.GetAnchor (0));
  201. Assert.Equal (1, line.Height.GetAnchor (0));
  202. Assert.Equal (line.Length, line.Width); // Length should be Width for horizontal
  203. // Test: new Line { Height = 9, Orientation = Orientation.Vertical }
  204. // Expected: Width=1, Height=9
  205. line = new() { Height = 9, Orientation = Orientation.Vertical };
  206. Assert.Equal (1, line.Width.GetAnchor (0));
  207. Assert.Equal (9, line.Height.GetAnchor (0));
  208. Assert.Equal (line.Length, line.Height); // Length should be Height for vertical
  209. line = new ();
  210. line.Height = 9;
  211. line.Orientation = Orientation.Vertical;
  212. Assert.Equal (1, line.Width.GetAnchor (0));
  213. Assert.Equal (9, line.Height.GetAnchor (0));
  214. Assert.Equal (line.Length, line.Height); // Length should be Height for vertical
  215. }
  216. }