Dim.PercentTests.cs 4.8 KB

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