Dim.Tests.cs 7.0 KB

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