Pos.PercentTests.cs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. using Microsoft.VisualStudio.TestPlatform.Utilities;
  2. using Xunit.Abstractions;
  3. using static Terminal.Gui.Dim;
  4. using static Terminal.Gui.Pos;
  5. namespace Terminal.Gui.PosDimTests;
  6. public class PosPercentTests (ITestOutputHelper output)
  7. {
  8. private readonly ITestOutputHelper _output = output;
  9. // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
  10. // TODO: A new test that calls SetRelativeLayout directly is needed.
  11. [Theory]
  12. [AutoInitShutdown]
  13. [InlineData (true)]
  14. [InlineData (false)]
  15. public void PosPercent_PlusOne (bool testHorizontal)
  16. {
  17. var container = new View { Width = 100, Height = 100 };
  18. var view = new View
  19. {
  20. X = testHorizontal ? Pos.Percent (50) + Pos.Percent (10) + 1 : 1,
  21. Y = testHorizontal ? 1 : Pos.Percent (50) + Pos.Percent (10) + 1,
  22. Width = 10,
  23. Height = 10
  24. };
  25. container.Add (view);
  26. var top = new Toplevel ();
  27. top.Add (container);
  28. top.LayoutSubviews ();
  29. Assert.Equal (100, container.Frame.Width);
  30. Assert.Equal (100, container.Frame.Height);
  31. if (testHorizontal)
  32. {
  33. Assert.Equal (61, view.Frame.X);
  34. Assert.Equal (1, view.Frame.Y);
  35. }
  36. else
  37. {
  38. Assert.Equal (1, view.Frame.X);
  39. Assert.Equal (61, view.Frame.Y);
  40. }
  41. }
  42. [Fact]
  43. public void PosPercent_SetsValue ()
  44. {
  45. float f = 0;
  46. Pos pos = Pos.Percent (f);
  47. Assert.Equal ($"Factor({f / 100:0.###})", pos.ToString ());
  48. f = 0.5F;
  49. pos = Pos.Percent (f);
  50. Assert.Equal ($"Factor({f / 100:0.###})", pos.ToString ());
  51. f = 100;
  52. pos = Pos.Percent (f);
  53. Assert.Equal ($"Factor({f / 100:0.###})", pos.ToString ());
  54. }
  55. [Fact]
  56. public void PosPercent_ThrowsOnIvalid ()
  57. {
  58. Pos pos = Pos.Percent (0);
  59. Assert.Throws<ArgumentException> (() => pos = Pos.Percent (-1));
  60. Assert.Throws<ArgumentException> (() => pos = Pos.Percent (101));
  61. Assert.Throws<ArgumentException> (() => pos = Pos.Percent (100.0001F));
  62. Assert.Throws<ArgumentException> (() => pos = Pos.Percent (1000001));
  63. }
  64. }