Pos.AnchorEndTests.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. using Xunit.Abstractions;
  2. using static Terminal.Gui.Pos;
  3. namespace Terminal.Gui.LayoutTests;
  4. public class PosAnchorEndTests ()
  5. {
  6. [Fact]
  7. public void PosAnchorEnd_Constructor ()
  8. {
  9. var posAnchorEnd = new PosAnchorEnd (10);
  10. Assert.NotNull (posAnchorEnd);
  11. }
  12. [Theory]
  13. [InlineData (0, 0, true)]
  14. [InlineData (10, 10, true)]
  15. [InlineData (0, 10, false)]
  16. [InlineData (10, 1, false)]
  17. public void PosAnchorEnd_Equals (int offset1, int offset2, bool expectedEquals)
  18. {
  19. var posAnchorEnd1 = new PosAnchorEnd (offset1);
  20. var posAnchorEnd2 = new PosAnchorEnd (offset2);
  21. Assert.Equal (expectedEquals, posAnchorEnd1.Equals (posAnchorEnd2));
  22. Assert.Equal (expectedEquals, posAnchorEnd2.Equals (posAnchorEnd1));
  23. }
  24. [Fact]
  25. public void PosAnchorEnd_ToString ()
  26. {
  27. var posAnchorEnd = new PosAnchorEnd (10);
  28. var expectedString = "AnchorEnd(10)";
  29. Assert.Equal (expectedString, posAnchorEnd.ToString ());
  30. }
  31. [Fact]
  32. public void PosAnchorEnd_GetAnchor ()
  33. {
  34. var posAnchorEnd = new PosAnchorEnd (10);
  35. var width = 50;
  36. var expectedAnchor = width - 10;
  37. Assert.Equal (expectedAnchor, posAnchorEnd.GetAnchor (width));
  38. }
  39. [Fact]
  40. public void PosAnchorEnd_CreatesCorrectInstance ()
  41. {
  42. var pos = Pos.AnchorEnd (10);
  43. Assert.IsType<PosAnchorEnd> (pos);
  44. }
  45. [Fact]
  46. public void PosAnchorEnd_Negative_Throws ()
  47. {
  48. Pos pos;
  49. int n = -1;
  50. Assert.Throws<ArgumentOutOfRangeException> (() => pos = Pos.AnchorEnd (n));
  51. }
  52. [Theory]
  53. [InlineData (0)]
  54. [InlineData (1)]
  55. public void PosAnchorEnd_SetsValue_GetAnchor_Is_Negative (int offset)
  56. {
  57. Pos pos = Pos.AnchorEnd (offset);
  58. Assert.Equal (offset, -pos.GetAnchor (0));
  59. }
  60. [Theory]
  61. [InlineData (0, 0, 25)]
  62. [InlineData (0, 10, 25)]
  63. [InlineData (1, 10, 24)]
  64. [InlineData (10, 10, 15)]
  65. [InlineData (20, 10, 5)]
  66. [InlineData (25, 10, 0)]
  67. [InlineData (26, 10, -1)]
  68. public void PosAnchorEnd_With_Offset_PositionsViewOffsetFromRight (int offset, int width, int expectedXPosition)
  69. {
  70. // Arrange
  71. var superView = new View { Width = 25, Height = 25 };
  72. var view = new View
  73. {
  74. X = Pos.AnchorEnd (offset),
  75. Width = width,
  76. Height = 1
  77. };
  78. superView.Add (view);
  79. superView.BeginInit ();
  80. superView.EndInit ();
  81. // Act
  82. superView.LayoutSubViews ();
  83. // Assert
  84. Assert.Equal (expectedXPosition, view.Frame.X);
  85. }
  86. // UseDimForOffset tests
  87. [Fact]
  88. public void PosAnchorEnd_UseDimForOffset_CreatesCorrectInstance ()
  89. {
  90. var pos = Pos.AnchorEnd ();
  91. Assert.IsType<PosAnchorEnd> (pos);
  92. Assert.True (((PosAnchorEnd)pos).UseDimForOffset);
  93. }
  94. [Fact]
  95. public void PosAnchorEnd_UseDimForOffset_SetsValue_GetAnchor_Is_Negative ()
  96. {
  97. Pos pos = Pos.AnchorEnd ();
  98. Assert.Equal (-10, -pos.GetAnchor (10));
  99. }
  100. [Theory]
  101. [InlineData (0, 25)]
  102. [InlineData (10, 15)]
  103. [InlineData (9, 16)]
  104. [InlineData (11, 14)]
  105. [InlineData (25, 0)]
  106. [InlineData (26, -1)]
  107. public void PosAnchorEnd_UseDimForOffset_PositionsViewOffsetByDim (int dim, int expectedXPosition)
  108. {
  109. // Arrange
  110. var superView = new View { Width = 25, Height = 25 };
  111. var view = new View
  112. {
  113. X = Pos.AnchorEnd (),
  114. Width = dim,
  115. Height = 1
  116. };
  117. superView.Add (view);
  118. superView.BeginInit ();
  119. superView.EndInit ();
  120. // Act
  121. superView.LayoutSubViews ();
  122. // Assert
  123. Assert.Equal (expectedXPosition, view.Frame.X);
  124. }
  125. [Theory]
  126. [InlineData (0, 25)]
  127. [InlineData (10, 23)]
  128. [InlineData (50, 13)]
  129. [InlineData (100, 0)]
  130. public void PosAnchorEnd_UseDimForOffset_DimPercent_PositionsViewOffsetByDim (int percent, int expectedXPosition)
  131. {
  132. // Arrange
  133. var superView = new View { Width = 25, Height = 25 };
  134. var view = new View
  135. {
  136. X = Pos.AnchorEnd (),
  137. Width = Dim.Percent (percent),
  138. Height = 1
  139. };
  140. superView.Add (view);
  141. superView.BeginInit ();
  142. superView.EndInit ();
  143. // Act
  144. superView.LayoutSubViews ();
  145. // Assert
  146. Assert.Equal (expectedXPosition, view.Frame.X);
  147. }
  148. [Fact]
  149. public void PosAnchorEnd_Calculate_ReturnsExpectedValue ()
  150. {
  151. var posAnchorEnd = new PosAnchorEnd (5);
  152. var result = posAnchorEnd.Calculate (10, new DimAbsolute (2), null, Dimension.None);
  153. Assert.Equal (5, result);
  154. }
  155. [Fact]
  156. public void PosAnchorEnd_MinusOne_Combine_Works ()
  157. {
  158. var pos = AnchorEnd () - 1;
  159. var result = pos.Calculate (10, new DimAbsolute (2), null, Dimension.None);
  160. Assert.Equal (7, result);
  161. }
  162. }