DimTests.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. // FIXED: Dim.Width should support Equals() and this should change to Equal.
  75. Assert.Equal (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. // FIXED: Dim.Width should support Equals() and this should change to Equal.
  84. Assert.Equal (dim1, dim2);
  85. Assert.Throws<ArgumentException> (() => new Rect (0, -1, -2, -3));
  86. testRect1 = new Rect (0, -1, 2, 3);
  87. view1 = new View (testRect1);
  88. testRect2 = new Rect (0, -1, 2, 3);
  89. dim1 = Dim.Width (view1);
  90. dim2 = Dim.Width (view1);
  91. // FIXED: Dim.Width should support Equals() and this should change to Equal.
  92. Assert.Equal (dim1, dim2);
  93. testRect1 = new Rect (0, -1, 2, 3);
  94. view1 = new View (testRect1);
  95. testRect2 = Rect.Empty;
  96. view2 = new View (testRect2);
  97. dim1 = Dim.Width (view1);
  98. dim2 = Dim.Width (view2);
  99. Assert.NotEqual (dim1, dim2);
  100. }
  101. [Fact]
  102. public void Height_SetsValue ()
  103. {
  104. var dim = Dim.Height (null);
  105. Assert.Throws<NullReferenceException> (() => dim.ToString ());
  106. var testVal = Rect.Empty;
  107. testVal = Rect.Empty;
  108. dim = Dim.Height (new View (testVal));
  109. Assert.Equal ($"DimView(side=Height, target=View()({{X={testVal.X},Y={testVal.Y},Width={testVal.Width},Height={testVal.Height}}}))", dim.ToString ());
  110. testVal = new Rect (1, 2, 3, 4);
  111. dim = Dim.Height (new View (testVal));
  112. Assert.Equal ($"DimView(side=Height, target=View()({{X={testVal.X},Y={testVal.Y},Width={testVal.Width},Height={testVal.Height}}}))", dim.ToString ());
  113. }
  114. // TODO: Other Dim.Height tests (e.g. Equal?)
  115. [Fact]
  116. public void Fill_SetsValue ()
  117. {
  118. var testMargin = 0;
  119. var dim = Dim.Fill ();
  120. Assert.Equal ($"Dim.Fill(margin={testMargin})", dim.ToString ());
  121. testMargin = 0;
  122. dim = Dim.Fill (testMargin);
  123. Assert.Equal ($"Dim.Fill(margin={testMargin})", dim.ToString ());
  124. testMargin = 5;
  125. dim = Dim.Fill (testMargin);
  126. Assert.Equal ($"Dim.Fill(margin={testMargin})", dim.ToString ());
  127. }
  128. [Fact]
  129. public void Fill_Equal ()
  130. {
  131. var margin1 = 0;
  132. var margin2 = 0;
  133. var dim1 = Dim.Fill (margin1);
  134. var dim2 = Dim.Fill (margin2);
  135. Assert.Equal (dim1, dim2);
  136. }
  137. [Fact]
  138. public void Percent_SetsValue ()
  139. {
  140. float f = 0;
  141. var dim = Dim.Percent (f);
  142. Assert.Equal ($"Dim.Factor(factor={f / 100:0.###}, remaining={false})", dim.ToString ());
  143. f = 0.5F;
  144. dim = Dim.Percent (f);
  145. Assert.Equal ($"Dim.Factor(factor={f / 100:0.###}, remaining={false})", dim.ToString ());
  146. f = 100;
  147. dim = Dim.Percent (f);
  148. Assert.Equal ($"Dim.Factor(factor={f / 100:0.###}, remaining={false})", dim.ToString ());
  149. }
  150. [Fact]
  151. public void Percent_Equals ()
  152. {
  153. float n1 = 0;
  154. float n2 = 0;
  155. var dim1 = Dim.Percent (n1);
  156. var dim2 = Dim.Percent (n2);
  157. Assert.Equal (dim1, dim2);
  158. n1 = n2 = 1;
  159. dim1 = Dim.Percent (n1);
  160. dim2 = Dim.Percent (n2);
  161. Assert.Equal (dim1, dim2);
  162. n1 = n2 = 0.5f;
  163. dim1 = Dim.Percent (n1);
  164. dim2 = Dim.Percent (n2);
  165. Assert.Equal (dim1, dim2);
  166. n1 = n2 = 100f;
  167. dim1 = Dim.Percent (n1);
  168. dim2 = Dim.Percent (n2);
  169. Assert.Equal (dim1, dim2);
  170. n1 = n2 = 0.3f;
  171. dim1 = Dim.Percent (n1, true);
  172. dim2 = Dim.Percent (n2, true);
  173. Assert.Equal (dim1, dim2);
  174. n1 = n2 = 0.3f;
  175. dim1 = Dim.Percent (n1);
  176. dim2 = Dim.Percent (n2, true);
  177. Assert.NotEqual (dim1, dim2);
  178. n1 = 0;
  179. n2 = 1;
  180. dim1 = Dim.Percent (n1);
  181. dim2 = Dim.Percent (n2);
  182. Assert.NotEqual (dim1, dim2);
  183. n1 = 0.5f;
  184. n2 = 1.5f;
  185. dim1 = Dim.Percent (n1);
  186. dim2 = Dim.Percent (n2);
  187. Assert.NotEqual (dim1, dim2);
  188. }
  189. [Fact]
  190. public void Percent_ThrowsOnIvalid ()
  191. {
  192. var dim = Dim.Percent (0);
  193. Assert.Throws<ArgumentException> (() => dim = Dim.Percent (-1));
  194. Assert.Throws<ArgumentException> (() => dim = Dim.Percent (101));
  195. Assert.Throws<ArgumentException> (() => dim = Dim.Percent (100.0001F));
  196. Assert.Throws<ArgumentException> (() => dim = Dim.Percent (1000001));
  197. }
  198. // TODO: Test operators
  199. }
  200. }