Pos.PercentTests.cs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. using Xunit.Abstractions;
  2. using static Terminal.Gui.Pos;
  3. namespace Terminal.Gui.LayoutTests;
  4. public class PosPercentTests (ITestOutputHelper output)
  5. {
  6. private readonly ITestOutputHelper _output = output;
  7. [Theory]
  8. [InlineData (50, 10, 2, 5)]
  9. [InlineData (50, 10, 10, 5)]
  10. [InlineData (50, 10, 11, 5)]
  11. [InlineData (50, 10, 12, 5)]
  12. [InlineData (50, 19, 20, 9)]
  13. public void PosPercent_Calculate_ReturnsExpectedValue (int percent, int superviewDimension, int width, int expectedX)
  14. {
  15. var posPercent = new PosPercent (percent);
  16. int result = posPercent.Calculate (superviewDimension, new DimAbsolute (width), null!, Dimension.Width);
  17. Assert.Equal (expectedX, result);
  18. }
  19. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  20. // TODO: A new test that calls SetRelativeLayout directly is needed.
  21. [Theory]
  22. [InlineData (true)]
  23. [InlineData (false)]
  24. public void PosPercent_PlusOne (bool testHorizontal)
  25. {
  26. var container = new View { Width = 100, Height = 100 };
  27. var view = new View
  28. {
  29. X = testHorizontal ? Percent (50) + Percent (10) + 1 : 1,
  30. Y = testHorizontal ? 1 : Percent (50) + Percent (10) + 1,
  31. Width = 10,
  32. Height = 10
  33. };
  34. container.Add (view);
  35. var top = new Toplevel ();
  36. top.Add (container);
  37. top.LayoutSubviews ();
  38. Assert.Equal (100, container.Frame.Width);
  39. Assert.Equal (100, container.Frame.Height);
  40. if (testHorizontal)
  41. {
  42. Assert.Equal (61, view.Frame.X);
  43. Assert.Equal (1, view.Frame.Y);
  44. }
  45. else
  46. {
  47. Assert.Equal (1, view.Frame.X);
  48. Assert.Equal (61, view.Frame.Y);
  49. }
  50. top.Dispose ();
  51. }
  52. [Fact]
  53. public void PosPercent_SetsValue ()
  54. {
  55. var f = 0;
  56. Pos pos = Percent (f);
  57. Assert.Equal ($"Percent({f})", pos.ToString ());
  58. f = 50;
  59. pos = Percent (f);
  60. Assert.Equal ($"Percent({f})", pos.ToString ());
  61. f = 100;
  62. pos = Percent (f);
  63. Assert.Equal ($"Percent({f})", pos.ToString ());
  64. }
  65. [Fact]
  66. public void PosPercent_ThrowsOnIvalid ()
  67. {
  68. Pos pos = Percent (0);
  69. Assert.Throws<ArgumentException> (() => pos = Percent (-1));
  70. //Assert.Throws<ArgumentException> (() => pos = Pos.Percent (101));
  71. //Assert.Throws<ArgumentException> (() => pos = Pos.Percent (1000001));
  72. }
  73. }