Dim.Tests.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. #nullable disable
  2. using static Terminal.Gui.ViewBase.Dim;
  3. namespace ViewBaseTests.Layout;
  4. [Collection ("Global Test Setup")]
  5. public class DimTests
  6. {
  7. [Fact]
  8. public void DimAbsolute_Calculate_ReturnsCorrectValue ()
  9. {
  10. var dim = new DimAbsolute (10);
  11. int result = dim.Calculate (0, 100, null, Dimension.None);
  12. Assert.Equal (10, result);
  13. }
  14. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  15. // TODO: A new test that calls SetRelativeLayout directly is needed.
  16. [Fact]
  17. public void Dim_Validation_Do_Not_Throws_If_NewValue_Is_DimAbsolute_And_OldValue_Is_Null ()
  18. {
  19. var t = new View { Width = 80, Height = 25, Text = "top" };
  20. var w = new View
  21. {
  22. BorderStyle = LineStyle.Single,
  23. X = 1,
  24. Y = 2,
  25. Width = 4,
  26. Height = 5,
  27. Title = "w"
  28. };
  29. t.Add (w);
  30. t.LayoutSubViews ();
  31. Assert.Equal (3, w.Width = 3);
  32. Assert.Equal (4, w.Height = 4);
  33. t.Dispose ();
  34. }
  35. [Fact]
  36. public void DimHeight_Set_To_Null_Throws ()
  37. {
  38. Dim dim = Height (null);
  39. Assert.Throws<NullReferenceException> (() => dim.ToString ());
  40. }
  41. [Fact]
  42. public void DimHeight_SetsValue ()
  43. {
  44. var testVal = Rectangle.Empty;
  45. var testValview = new View { Frame = testVal };
  46. Dim dim = Height (testValview);
  47. Assert.Equal ($"View(Height,View(){testVal})", dim.ToString ());
  48. testValview.Dispose ();
  49. testVal = new (1, 2, 3, 4);
  50. testValview = new () { Frame = testVal };
  51. dim = Height (testValview);
  52. Assert.Equal ($"View(Height,View(){testVal})", dim.ToString ());
  53. testValview.Dispose ();
  54. }
  55. [Fact]
  56. public void Internal_Tests ()
  57. {
  58. var dimFactor = new DimPercent (10);
  59. Assert.Equal (10, dimFactor.GetAnchor (100));
  60. var dimAbsolute = new DimAbsolute (10);
  61. Assert.Equal (10, dimAbsolute.GetAnchor (0));
  62. var dimFill = new DimFill (1);
  63. Assert.Equal (99, dimFill.GetAnchor (100));
  64. var dimCombine = new DimCombine (AddOrSubtract.Add, dimFactor, dimAbsolute);
  65. Assert.Equal (dimCombine.Left, dimFactor);
  66. Assert.Equal (dimCombine.Right, dimAbsolute);
  67. Assert.Equal (20, dimCombine.GetAnchor (100));
  68. var view = new View { Frame = new (20, 10, 20, 1) };
  69. var dimViewHeight = new DimView (view, Dimension.Height);
  70. Assert.Equal (1, dimViewHeight.GetAnchor (0));
  71. var dimViewWidth = new DimView (view, Dimension.Width);
  72. Assert.Equal (20, dimViewWidth.GetAnchor (0));
  73. view.Dispose ();
  74. }
  75. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  76. // TODO: A new test that calls SetRelativeLayout directly is needed.
  77. [Fact]
  78. public void Referencing_SuperView_Does_Not_Throw ()
  79. {
  80. var super = new View { Width = 10, Height = 10, Text = "super" };
  81. var view = new View
  82. {
  83. Width = Width (super), // this is allowed
  84. Height = Height (super), // this is allowed
  85. Text = "view"
  86. };
  87. super.Add (view);
  88. super.BeginInit ();
  89. super.EndInit ();
  90. Exception exception = Record.Exception (super.LayoutSubViews);
  91. Assert.Null (exception);
  92. super.Dispose ();
  93. }
  94. [Fact]
  95. public void DimSized_Equals ()
  96. {
  97. var n1 = 0;
  98. var n2 = 0;
  99. Dim dim1 = Absolute (n1);
  100. Dim dim2 = Absolute (n2);
  101. Assert.Equal (dim1, dim2);
  102. n1 = n2 = 1;
  103. dim1 = Absolute (n1);
  104. dim2 = Absolute (n2);
  105. Assert.Equal (dim1, dim2);
  106. n1 = n2 = -1;
  107. dim1 = Absolute (n1);
  108. dim2 = Absolute (n2);
  109. Assert.Equal (dim1, dim2);
  110. n1 = 0;
  111. n2 = 1;
  112. dim1 = Absolute (n1);
  113. dim2 = Absolute (n2);
  114. Assert.NotEqual (dim1, dim2);
  115. }
  116. [Fact]
  117. public void DimSized_SetsValue ()
  118. {
  119. Dim dim = Absolute (0);
  120. Assert.Equal ("Absolute(0)", dim.ToString ());
  121. var testVal = 5;
  122. dim = Absolute (testVal);
  123. Assert.Equal ($"Absolute({testVal})", dim.ToString ());
  124. testVal = -1;
  125. dim = Absolute (testVal);
  126. Assert.Equal ($"Absolute({testVal})", dim.ToString ());
  127. }
  128. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  129. // TODO: A new test that calls SetRelativeLayout directly is needed.
  130. [Fact]
  131. public void Validation_Does_Not_Throw_If_NewValue_Is_DimAbsolute_And_OldValue_Is_Null ()
  132. {
  133. var t = new View { Width = 80, Height = 25, Text = "top" };
  134. var w = new Window
  135. {
  136. X = 1,
  137. Y = 2,
  138. Width = 4,
  139. Height = 5,
  140. Title = "w"
  141. };
  142. t.Add (w);
  143. t.LayoutSubViews ();
  144. Assert.Equal (3, w.Width = 3);
  145. Assert.Equal (4, w.Height = 4);
  146. t.Dispose ();
  147. }
  148. [Fact]
  149. public void DimWidth_Equals ()
  150. {
  151. var testRect1 = Rectangle.Empty;
  152. var view1 = new View { Frame = testRect1 };
  153. var testRect2 = Rectangle.Empty;
  154. var view2 = new View { Frame = testRect2 };
  155. Dim dim1 = Width (view1);
  156. Dim dim2 = Width (view1);
  157. // FIXED: Dim.Width should support Equals() and this should change to Equal.
  158. Assert.Equal (dim1, dim2);
  159. dim2 = Width (view2);
  160. Assert.NotEqual (dim1, dim2);
  161. testRect1 = new (0, 1, 2, 3);
  162. view1 = new () { Frame = testRect1 };
  163. testRect2 = new (0, 1, 2, 3);
  164. dim1 = Width (view1);
  165. dim2 = Width (view1);
  166. // FIXED: Dim.Width should support Equals() and this should change to Equal.
  167. Assert.Equal (dim1, dim2);
  168. testRect1 = new (0, -1, 2, 3);
  169. view1 = new () { Frame = testRect1 };
  170. testRect2 = new (0, -1, 2, 3);
  171. dim1 = Width (view1);
  172. dim2 = Width (view1);
  173. // FIXED: Dim.Width should support Equals() and this should change to Equal.
  174. Assert.Equal (dim1, dim2);
  175. testRect1 = new (0, -1, 2, 3);
  176. view1 = new () { Frame = testRect1 };
  177. testRect2 = Rectangle.Empty;
  178. view2 = new () { Frame = testRect2 };
  179. dim1 = Width (view1);
  180. dim2 = Width (view2);
  181. Assert.NotEqual (dim1, dim2);
  182. }
  183. [Fact]
  184. public void DimWidth_Set_To_Null_Throws ()
  185. {
  186. Dim dim = Width (null);
  187. Assert.Throws<NullReferenceException> (() => dim.ToString ());
  188. }
  189. [Fact]
  190. public void DimWidth_SetsValue ()
  191. {
  192. var testVal = Rectangle.Empty;
  193. var testValView = new View { Frame = testVal };
  194. Dim dim = Width (testValView);
  195. Assert.Equal ($"View(Width,View(){testVal})", dim.ToString ());
  196. testValView.Dispose ();
  197. testVal = new (1, 2, 3, 4);
  198. testValView = new () { Frame = testVal };
  199. dim = Width (testValView);
  200. Assert.Equal ($"View(Width,View(){testVal})", dim.ToString ());
  201. testValView.Dispose ();
  202. }
  203. }