Dim.Tests.cs 7.0 KB

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