DimTests.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.IO;
  5. using System.Linq;
  6. using Terminal.Gui;
  7. using Xunit;
  8. // Alais Console to MockConsole so we don't accidentally use Console
  9. using Console = Terminal.Gui.FakeConsole;
  10. namespace Terminal.Gui {
  11. public class DimTests {
  12. [Fact]
  13. public void New_Works ()
  14. {
  15. var dim = new Dim ();
  16. Assert.Equal ("Terminal.Gui.Dim", dim.ToString ());
  17. }
  18. [Fact]
  19. public void Sized_SetsValue ()
  20. {
  21. var dim = Dim.Sized (0);
  22. Assert.Equal ("Dim.Absolute(0)", dim.ToString ());
  23. int testVal = 5;
  24. dim = Dim.Sized (testVal);
  25. Assert.Equal ($"Dim.Absolute({testVal})", dim.ToString ());
  26. testVal = -1;
  27. dim = Dim.Sized (testVal);
  28. Assert.Equal ($"Dim.Absolute({testVal})", dim.ToString ());
  29. }
  30. [Fact]
  31. public void Sized_Equals ()
  32. {
  33. int n1 = 0;
  34. int n2 = 0;
  35. var dim1 = Dim.Sized (n1);
  36. var dim2 = Dim.Sized (n2);
  37. Assert.Equal (dim1, dim2);
  38. n1 = n2 = 1;
  39. dim1 = Dim.Sized (n1);
  40. dim2 = Dim.Sized (n2);
  41. Assert.Equal (dim1, dim2);
  42. n1 = n2 = -1;
  43. dim1 = Dim.Sized (n1);
  44. dim2 = Dim.Sized (n2);
  45. Assert.Equal (dim1, dim2);
  46. n1 = 0;
  47. n2 = 1;
  48. dim1 = Dim.Sized (n1);
  49. dim2 = Dim.Sized (n2);
  50. Assert.NotEqual (dim1, dim2);
  51. }
  52. [Fact]
  53. public void Width_SetsValue ()
  54. {
  55. var dim = Dim.Width (null);
  56. Assert.Throws<NullReferenceException> (() => dim.ToString ());
  57. var testVal = Rect.Empty;
  58. testVal = Rect.Empty;
  59. dim = Dim.Width (new View (testVal));
  60. Assert.Equal ($"DimView(side=Width, target=View()({{X={testVal.X},Y={testVal.Y},Width={testVal.Width},Height={testVal.Height}}}))", dim.ToString ());
  61. testVal = new Rect (1, 2, 3, 4);
  62. dim = Dim.Width (new View (testVal));
  63. Assert.Equal ($"DimView(side=Width, target=View()({{X={testVal.X},Y={testVal.Y},Width={testVal.Width},Height={testVal.Height}}}))", dim.ToString ());
  64. }
  65. [Fact]
  66. public void Width_Equals ()
  67. {
  68. var testRect1 = Rect.Empty;
  69. var view1 = new View (testRect1);
  70. var testRect2 = Rect.Empty;
  71. var view2 = new View (testRect2);
  72. var dim1 = Dim.Width (view1);
  73. var dim2 = Dim.Width (view1);
  74. // BUGBUG: Dim.Width should support Equals() and this should change to Euqal.
  75. Assert.NotEqual (dim1, dim2);
  76. dim2 = Dim.Width (view2);
  77. Assert.NotEqual (dim1, dim2);
  78. testRect1 = new Rect (0, 1, 2, 3);
  79. view1 = new View (testRect1);
  80. testRect2 = new Rect (0, 1, 2, 3);
  81. dim1 = Dim.Width (view1);
  82. dim2 = Dim.Width (view1);
  83. // BUGBUG: Dim.Width should support Equals() and this should change to Euqal.
  84. Assert.NotEqual (dim1, dim2);
  85. testRect1 = new Rect (0, -1, -2, -3);
  86. view1 = new View (testRect1);
  87. testRect2 = new Rect (0, -1, -2, -3);
  88. dim1 = Dim.Width (view1);
  89. dim2 = Dim.Width (view1);
  90. // BUGBUG: Dim.Width should support Equals() and this should change to Euqal.
  91. Assert.NotEqual (dim1, dim2);
  92. testRect1 = new Rect (0, -1, -2, -3);
  93. view1 = new View (testRect1);
  94. testRect2 = Rect.Empty;
  95. view2 = new View (testRect2);
  96. dim1 = Dim.Width (view1);
  97. dim2 = Dim.Width (view2);
  98. Assert.NotEqual (dim1, dim2);
  99. }
  100. [Fact]
  101. public void Height_SetsValue ()
  102. {
  103. var dim = Dim.Height (null);
  104. Assert.Throws<NullReferenceException> (() => dim.ToString ());
  105. var testVal = Rect.Empty;
  106. testVal = Rect.Empty;
  107. dim = Dim.Height (new View (testVal));
  108. Assert.Equal ($"DimView(side=Height, target=View()({{X={testVal.X},Y={testVal.Y},Width={testVal.Width},Height={testVal.Height}}}))", dim.ToString ());
  109. testVal = new Rect (1, 2, 3, 4);
  110. dim = Dim.Height (new View (testVal));
  111. Assert.Equal ($"DimView(side=Height, target=View()({{X={testVal.X},Y={testVal.Y},Width={testVal.Width},Height={testVal.Height}}}))", dim.ToString ());
  112. }
  113. // TODO: Other Dim.Height tests (e.g. Equal?)
  114. [Fact]
  115. public void Fill_SetsValue ()
  116. {
  117. var testMargin = 0;
  118. var dim = Dim.Fill ();
  119. Assert.Equal ($"Dim.Fill(margin={testMargin})", dim.ToString ());
  120. testMargin = 0;
  121. dim = Dim.Fill (testMargin);
  122. Assert.Equal ($"Dim.Fill(margin={testMargin})", dim.ToString ());
  123. testMargin = 5;
  124. dim = Dim.Fill (testMargin);
  125. Assert.Equal ($"Dim.Fill(margin={testMargin})", dim.ToString ());
  126. }
  127. [Fact]
  128. public void Fill_Equal ()
  129. {
  130. var margin1 = 0;
  131. var margin2 = 0;
  132. var dim1 = Dim.Fill (margin1);
  133. var dim2 = Dim.Fill (margin2);
  134. Assert.Equal (dim1, dim2);
  135. }
  136. [Fact]
  137. public void Percent_SetsValue ()
  138. {
  139. float f = 0;
  140. var dim = Dim.Percent (f);
  141. Assert.Equal ($"Dim.Factor({f / 100:0.###})", dim.ToString ());
  142. f = 0.5F;
  143. dim = Dim.Percent (f);
  144. Assert.Equal ($"Dim.Factor({f / 100:0.###})", dim.ToString ());
  145. f = 100;
  146. dim = Dim.Percent (f);
  147. Assert.Equal ($"Dim.Factor({f / 100:0.###})", dim.ToString ());
  148. }
  149. [Fact]
  150. public void Percent_Equals ()
  151. {
  152. float n1 = 0;
  153. float n2 = 0;
  154. var dim1 = Dim.Percent (n1);
  155. var dim2 = Dim.Percent (n2);
  156. Assert.Equal (dim1, dim2);
  157. n1 = n2 = 1;
  158. dim1 = Dim.Percent (n1);
  159. dim2 = Dim.Percent (n2);
  160. Assert.Equal (dim1, dim2);
  161. n1 = n2 = 0.5f;
  162. dim1 = Dim.Percent (n1);
  163. dim2 = Dim.Percent (n2);
  164. Assert.Equal (dim1, dim2);
  165. n1 = n2 = 100f;
  166. dim1 = Dim.Percent (n1);
  167. dim2 = Dim.Percent (n2);
  168. Assert.Equal (dim1, dim2);
  169. n1 = 0;
  170. n2 = 1;
  171. dim1 = Dim.Percent (n1);
  172. dim2 = Dim.Percent (n2);
  173. Assert.NotEqual (dim1, dim2);
  174. n1 = 0.5f;
  175. n2 = 1.5f;
  176. dim1 = Dim.Percent (n1);
  177. dim2 = Dim.Percent (n2);
  178. Assert.NotEqual (dim1, dim2);
  179. }
  180. [Fact]
  181. public void Percent_ThrowsOnIvalid ()
  182. {
  183. var dim = Dim.Percent (0);
  184. Assert.Throws<ArgumentException> (() => dim = Dim.Percent (-1));
  185. Assert.Throws<ArgumentException> (() => dim = Dim.Percent (101));
  186. Assert.Throws<ArgumentException> (() => dim = Dim.Percent (100.0001F));
  187. Assert.Throws<ArgumentException> (() => dim = Dim.Percent (1000001));
  188. }
  189. // TODO: Test operators
  190. }
  191. }