Dim.PercentTests.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. using System.Globalization;
  2. using System.Text;
  3. using Xunit.Abstractions;
  4. using static Terminal.Gui.Dim;
  5. namespace Terminal.Gui.LayoutTests;
  6. public class DimPercentTests
  7. {
  8. [Fact]
  9. public void DimFactor_Calculate_ReturnsCorrectValue ()
  10. {
  11. var dim = new DimPercent (50);
  12. var result = dim.Calculate (0, 100, null, Dimension.None);
  13. Assert.Equal (50, result);
  14. }
  15. [Fact]
  16. public void DimPercent_Equals ()
  17. {
  18. int n1 = 0;
  19. int n2 = 0;
  20. Dim dim1 = Dim.Percent (n1);
  21. Dim dim2 = Dim.Percent (n2);
  22. Assert.Equal (dim1, dim2);
  23. n1 = n2 = 1;
  24. dim1 = Dim.Percent (n1);
  25. dim2 = Dim.Percent (n2);
  26. Assert.Equal (dim1, dim2);
  27. n1 = n2 = 50;
  28. dim1 = Dim.Percent (n1);
  29. dim2 = Dim.Percent (n2);
  30. Assert.Equal (dim1, dim2);
  31. n1 = n2 = 100;
  32. dim1 = Dim.Percent (n1);
  33. dim2 = Dim.Percent (n2);
  34. Assert.Equal (dim1, dim2);
  35. n1 = n2 = 30;
  36. dim1 = Dim.Percent (n1, DimPercentMode.Position);
  37. dim2 = Dim.Percent (n2, DimPercentMode.Position);
  38. Assert.Equal (dim1, dim2);
  39. n1 = n2 = 30;
  40. dim1 = Dim.Percent (n1);
  41. dim2 = Dim.Percent (n2, DimPercentMode.Position);
  42. Assert.NotEqual (dim1, dim2);
  43. n1 = 0;
  44. n2 = 1;
  45. dim1 = Dim.Percent (n1);
  46. dim2 = Dim.Percent (n2);
  47. Assert.NotEqual (dim1, dim2);
  48. n1 = 50;
  49. n2 = 150;
  50. dim1 = Dim.Percent (n1);
  51. dim2 = Dim.Percent (n2);
  52. Assert.NotEqual (dim1, dim2);
  53. }
  54. [Fact]
  55. public void TestEquality ()
  56. {
  57. var a = Dim.Percent (32);
  58. var b = Dim.Percent (32);
  59. Assert.True (a.Equals (b));
  60. Assert.True (a.GetHashCode () == b.GetHashCode ());
  61. }
  62. [Fact]
  63. public void DimPercent_Invalid_Throws ()
  64. {
  65. Dim dim = Dim.Percent (0);
  66. Assert.Throws<ArgumentOutOfRangeException> (() => dim = Dim.Percent (-1));
  67. //Assert.Throws<ArgumentException> (() => dim = Dim.Percent (101));
  68. Assert.Throws<ArgumentOutOfRangeException> (() => dim = Dim.Percent (-1000001));
  69. //Assert.Throws<ArgumentException> (() => dim = Dim.Percent (1000001));
  70. }
  71. [Theory]
  72. [InlineData (0, DimPercentMode.ContentSize, true, 12)]
  73. [InlineData (0, DimPercentMode.ContentSize, false, 12)]
  74. [InlineData (1, DimPercentMode.ContentSize, true, 12)]
  75. [InlineData (1, DimPercentMode.ContentSize, false, 12)]
  76. [InlineData (2, DimPercentMode.ContentSize, true, 12)]
  77. [InlineData (2, DimPercentMode.ContentSize, false, 12)]
  78. [InlineData (0, DimPercentMode.Position, true, 12)]
  79. [InlineData (0, DimPercentMode.Position, false, 12)]
  80. [InlineData (1, DimPercentMode.Position, true, 12)]
  81. [InlineData (1, DimPercentMode.Position, false, 12)]
  82. [InlineData (2, DimPercentMode.Position, true, 11)]
  83. [InlineData (2, DimPercentMode.Position, false, 11)]
  84. public void DimPercent_Position (int position, DimPercentMode mode, bool width, int expected)
  85. {
  86. var super = new View { Width = 25, Height = 25 };
  87. var view = new View
  88. {
  89. X = width ? position : 0,
  90. Y = width ? 0 : position,
  91. Width = width ? Dim.Percent (50, mode) : 1,
  92. Height = width ? 1 : Dim.Percent (50, mode)
  93. };
  94. super.Add (view);
  95. super.BeginInit ();
  96. super.EndInit ();
  97. super.LayoutSubviews ();
  98. Assert.Equal (25, super.Frame.Width);
  99. Assert.Equal (25, super.Frame.Height);
  100. if (width)
  101. {
  102. Assert.Equal (expected, view.Frame.Width);
  103. Assert.Equal (1, view.Frame.Height);
  104. }
  105. else
  106. {
  107. Assert.Equal (1, view.Frame.Width);
  108. Assert.Equal (expected, view.Frame.Height);
  109. }
  110. }
  111. [Theory]
  112. [InlineData (0, true)]
  113. [InlineData (0, false)]
  114. [InlineData (50, true)]
  115. [InlineData (50, false)]
  116. public void DimPercent_PlusOne (int startingDistance, bool testHorizontal)
  117. {
  118. var super = new View { Width = 100, Height = 100 };
  119. var view = new View
  120. {
  121. X = testHorizontal ? startingDistance : 0,
  122. Y = testHorizontal ? 0 : startingDistance,
  123. Width = testHorizontal ? Dim.Percent (50) + 1 : 1,
  124. Height = testHorizontal ? 1 : Dim.Percent (50) + 1
  125. };
  126. super.Add (view);
  127. super.BeginInit ();
  128. super.EndInit ();
  129. super.LayoutSubviews ();
  130. Assert.Equal (100, super.Frame.Width);
  131. Assert.Equal (100, super.Frame.Height);
  132. if (testHorizontal)
  133. {
  134. Assert.Equal (51, view.Frame.Width);
  135. Assert.Equal (1, view.Frame.Height);
  136. }
  137. else
  138. {
  139. Assert.Equal (1, view.Frame.Width);
  140. Assert.Equal (51, view.Frame.Height);
  141. }
  142. }
  143. [Theory]
  144. [InlineData (0)]
  145. [InlineData (1)]
  146. [InlineData (50)]
  147. [InlineData (100)]
  148. [InlineData (101)]
  149. public void DimPercent_SetsValue (int percent)
  150. {
  151. Dim dim = Dim.Percent (percent);
  152. Assert.Equal ($"Percent({percent},ContentSize)", dim.ToString ());
  153. }
  154. }