TopologicalSortTests.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. using Xunit.Abstractions;
  2. namespace Terminal.Gui.LayoutTests;
  3. public class TopologicalSortTests (ITestOutputHelper output)
  4. {
  5. private readonly ITestOutputHelper _output = output;
  6. [Fact]
  7. public void TopologicalSort_Missing_Add ()
  8. {
  9. var root = new View ();
  10. var sub1 = new View ();
  11. root.Add (sub1);
  12. var sub2 = new View ();
  13. sub1.Width = Dim.Width (sub2);
  14. Assert.Throws<LayoutException> (() => root.LayoutSubviews ());
  15. sub2.Width = Dim.Width (sub1);
  16. Assert.Throws<LayoutException> (() => root.LayoutSubviews ());
  17. root.Dispose ();
  18. sub1.Dispose ();
  19. sub2.Dispose ();
  20. }
  21. [Fact]
  22. public void TopologicalSort_Recursive_Ref_Does_Not_Throw ()
  23. {
  24. var root = new View ();
  25. var sub1 = new View ();
  26. root.Add (sub1);
  27. var sub2 = new View ();
  28. root.Add (sub2);
  29. sub2.Width = Dim.Width (sub2);
  30. Exception exception = Record.Exception (root.LayoutSubviews);
  31. Assert.Null (exception);
  32. root.Dispose ();
  33. sub1.Dispose ();
  34. sub2.Dispose ();
  35. }
  36. [Fact]
  37. public void TopologicalSort_Throws_If_SuperView_Refs_SubView ()
  38. {
  39. var top = new View ();
  40. var superView = new View ();
  41. top.Add (superView);
  42. var subView = new View ();
  43. superView.Y = Pos.Top (subView);
  44. superView.Add (subView);
  45. Assert.Throws<LayoutException> (() => top.LayoutSubviews ());
  46. superView.Dispose ();
  47. }
  48. [Fact]
  49. public void TopologicalSort_View_Not_Added_Throws ()
  50. {
  51. var top = new View { Width = 80, Height = 50 };
  52. var super = new View { Width = Dim.Width (top) - 2, Height = Dim.Height (top) - 2 };
  53. top.Add (super);
  54. var sub = new View ();
  55. super.Add (sub);
  56. var v1 = new View { Width = Dim.Width (super) - 2, Height = Dim.Height (super) - 2 };
  57. var v2 = new View { Width = Dim.Width (v1) - 2, Height = Dim.Height (v1) - 2 };
  58. sub.Add (v1);
  59. // v2 not added to sub; should cause exception on Layout since it's referenced by sub.
  60. sub.Width = Dim.Fill () - Dim.Width (v2);
  61. sub.Height = Dim.Fill () - Dim.Height (v2);
  62. Assert.Throws<LayoutException> (() => top.Layout ());
  63. }
  64. }