Dim.PercentTests.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. //private readonly ITestOutputHelper _output;
  9. [Fact]
  10. public void DimFactor_Calculate_ReturnsCorrectValue ()
  11. {
  12. var dim = new DimPercent (50);
  13. var result = dim.Calculate (0, 100, null, Dimension.None);
  14. Assert.Equal (50, result);
  15. }
  16. [Fact]
  17. public void DimPercent_Equals ()
  18. {
  19. int n1 = 0;
  20. int 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 = 50;
  29. dim1 = Dim.Percent (n1);
  30. dim2 = Dim.Percent (n2);
  31. Assert.Equal (dim1, dim2);
  32. n1 = n2 = 100;
  33. dim1 = Dim.Percent (n1);
  34. dim2 = Dim.Percent (n2);
  35. Assert.Equal (dim1, dim2);
  36. n1 = n2 = 30;
  37. dim1 = Dim.Percent (n1, DimPercentMode.Position);
  38. dim2 = Dim.Percent (n2, DimPercentMode.Position);
  39. Assert.Equal (dim1, dim2);
  40. n1 = n2 = 30;
  41. dim1 = Dim.Percent (n1);
  42. dim2 = Dim.Percent (n2, DimPercentMode.Position);
  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 = 50;
  50. n2 = 150;
  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 (-1000001));
  62. //Assert.Throws<ArgumentException> (() => dim = Dim.Percent (1000001));
  63. }
  64. [Theory]
  65. [InlineData (0, DimPercentMode.ContentSize, true, 12)]
  66. [InlineData (0, DimPercentMode.ContentSize, false, 12)]
  67. [InlineData (1, DimPercentMode.ContentSize, true, 12)]
  68. [InlineData (1, DimPercentMode.ContentSize, false, 12)]
  69. [InlineData (2, DimPercentMode.ContentSize, true, 12)]
  70. [InlineData (2, DimPercentMode.ContentSize, false, 12)]
  71. [InlineData (0, DimPercentMode.Position, true, 12)]
  72. [InlineData (0, DimPercentMode.Position, false, 12)]
  73. [InlineData (1, DimPercentMode.Position, true, 12)]
  74. [InlineData (1, DimPercentMode.Position, false, 12)]
  75. [InlineData (2, DimPercentMode.Position, true, 11)]
  76. [InlineData (2, DimPercentMode.Position, false, 11)]
  77. public void DimPercent_Position (int position, DimPercentMode mode, 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, mode) : 1,
  85. Height = width ? 1 : Dim.Percent (50, mode)
  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. [Theory]
  137. [InlineData(0)]
  138. [InlineData (1)]
  139. [InlineData (50)]
  140. [InlineData (100)]
  141. [InlineData (101)]
  142. public void DimPercent_SetsValue (int percent)
  143. {
  144. Dim dim = Dim.Percent (percent);
  145. Assert.Equal ($"Percent({percent},ContentSize)", dim.ToString ());
  146. }
  147. }