Dim.PercentTests.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 DimPercent_Invalid_Throws ()
  56. {
  57. Dim dim = Dim.Percent (0);
  58. Assert.Throws<ArgumentOutOfRangeException> (() => dim = Dim.Percent (-1));
  59. //Assert.Throws<ArgumentException> (() => dim = Dim.Percent (101));
  60. Assert.Throws<ArgumentOutOfRangeException> (() => dim = Dim.Percent (-1000001));
  61. //Assert.Throws<ArgumentException> (() => dim = Dim.Percent (1000001));
  62. }
  63. [Theory]
  64. [InlineData (0, DimPercentMode.ContentSize, true, 12)]
  65. [InlineData (0, DimPercentMode.ContentSize, false, 12)]
  66. [InlineData (1, DimPercentMode.ContentSize, true, 12)]
  67. [InlineData (1, DimPercentMode.ContentSize, false, 12)]
  68. [InlineData (2, DimPercentMode.ContentSize, true, 12)]
  69. [InlineData (2, DimPercentMode.ContentSize, false, 12)]
  70. [InlineData (0, DimPercentMode.Position, true, 12)]
  71. [InlineData (0, DimPercentMode.Position, false, 12)]
  72. [InlineData (1, DimPercentMode.Position, true, 12)]
  73. [InlineData (1, DimPercentMode.Position, false, 12)]
  74. [InlineData (2, DimPercentMode.Position, true, 11)]
  75. [InlineData (2, DimPercentMode.Position, false, 11)]
  76. public void DimPercent_Position (int position, DimPercentMode mode, bool width, int expected)
  77. {
  78. var super = new View { Width = 25, Height = 25 };
  79. var view = new View
  80. {
  81. X = width ? position : 0,
  82. Y = width ? 0 : position,
  83. Width = width ? Dim.Percent (50, mode) : 1,
  84. Height = width ? 1 : Dim.Percent (50, mode)
  85. };
  86. super.Add (view);
  87. super.BeginInit ();
  88. super.EndInit ();
  89. super.LayoutSubviews ();
  90. Assert.Equal (25, super.Frame.Width);
  91. Assert.Equal (25, super.Frame.Height);
  92. if (width)
  93. {
  94. Assert.Equal (expected, view.Frame.Width);
  95. Assert.Equal (1, view.Frame.Height);
  96. }
  97. else
  98. {
  99. Assert.Equal (1, view.Frame.Width);
  100. Assert.Equal (expected, view.Frame.Height);
  101. }
  102. }
  103. [Theory]
  104. [InlineData (0, true)]
  105. [InlineData (0, false)]
  106. [InlineData (50, true)]
  107. [InlineData (50, false)]
  108. public void DimPercent_PlusOne (int startingDistance, bool testHorizontal)
  109. {
  110. var super = new View { Width = 100, Height = 100 };
  111. var view = new View
  112. {
  113. X = testHorizontal ? startingDistance : 0,
  114. Y = testHorizontal ? 0 : startingDistance,
  115. Width = testHorizontal ? Dim.Percent (50) + 1 : 1,
  116. Height = testHorizontal ? 1 : Dim.Percent (50) + 1
  117. };
  118. super.Add (view);
  119. super.BeginInit ();
  120. super.EndInit ();
  121. super.LayoutSubviews ();
  122. Assert.Equal (100, super.Frame.Width);
  123. Assert.Equal (100, super.Frame.Height);
  124. if (testHorizontal)
  125. {
  126. Assert.Equal (51, view.Frame.Width);
  127. Assert.Equal (1, view.Frame.Height);
  128. }
  129. else
  130. {
  131. Assert.Equal (1, view.Frame.Width);
  132. Assert.Equal (51, view.Frame.Height);
  133. }
  134. }
  135. [Theory]
  136. [InlineData(0)]
  137. [InlineData (1)]
  138. [InlineData (50)]
  139. [InlineData (100)]
  140. [InlineData (101)]
  141. public void DimPercent_SetsValue (int percent)
  142. {
  143. Dim dim = Dim.Percent (percent);
  144. Assert.Equal ($"Percent({percent},ContentSize)", dim.ToString ());
  145. }
  146. }