From b9b853a5c394c40e59239fc2192fdd36f3da08c3 Mon Sep 17 00:00:00 2001 From: Tig Date: Thu, 17 Oct 2024 15:54:41 -0600 Subject: [PATCH] Fixing unit tests 3 --- Terminal.Gui/Application/Application.Run.cs | 2 +- Terminal.Gui/View/Layout/Pos.cs | 12 +-- Terminal.Gui/View/View.Drawing.cs | 4 +- Terminal.Gui/View/View.Layout.cs | 9 +- Terminal.Gui/Views/ColorBar.cs | 4 +- UICatalog/Scenarios/TileViewNesting.cs | 3 - UICatalog/UICatalog.cs | 2 +- UnitTests/View/Layout/Dim.CombineTests.cs | 31 ------- UnitTests/View/Layout/Pos.ViewTests.cs | 23 ++---- .../{LayoutTests.cs => SetLayoutTests.cs} | 59 +------------ UnitTests/View/Layout/TopologicalSortTests.cs | 82 +++++++++++++++++++ 11 files changed, 105 insertions(+), 126 deletions(-) rename UnitTests/View/Layout/{LayoutTests.cs => SetLayoutTests.cs} (92%) create mode 100644 UnitTests/View/Layout/TopologicalSortTests.cs diff --git a/Terminal.Gui/Application/Application.Run.cs b/Terminal.Gui/Application/Application.Run.cs index fa35743f8..4ba25fb7d 100644 --- a/Terminal.Gui/Application/Application.Run.cs +++ b/Terminal.Gui/Application/Application.Run.cs @@ -202,7 +202,7 @@ public static partial class Application // Run (Begin, Run, End, Stop) if (PositionCursor ()) { - Driver.UpdateCursor (); + Driver?.UpdateCursor (); } NotifyNewRunState?.Invoke (toplevel, new (rs)); diff --git a/Terminal.Gui/View/Layout/Pos.cs b/Terminal.Gui/View/Layout/Pos.cs index b8f47538e..4691b88bf 100644 --- a/Terminal.Gui/View/Layout/Pos.cs +++ b/Terminal.Gui/View/Layout/Pos.cs @@ -251,22 +251,22 @@ public abstract record Pos /// Creates a object that tracks the Top (Y) position of the specified . /// The that depends on the other view. /// The that will be tracked. - public static Pos Top (View? view) { return new PosView (view, Side.Top); } + public static Pos Top (View view) { return new PosView (view, Side.Top); } /// Creates a object that tracks the Top (Y) position of the specified . /// The that depends on the other view. /// The that will be tracked. - public static Pos Y (View? view) { return new PosView (view, Side.Top); } + public static Pos Y (View view) { return new PosView (view, Side.Top); } /// Creates a object that tracks the Left (X) position of the specified . /// The that depends on the other view. /// The that will be tracked. - public static Pos Left (View? view) { return new PosView (view, Side.Left); } + public static Pos Left (View view) { return new PosView (view, Side.Left); } /// Creates a object that tracks the Left (X) position of the specified . /// The that depends on the other view. /// The that will be tracked. - public static Pos X (View? view) { return new PosView (view, Side.Left); } + public static Pos X (View view) { return new PosView (view, Side.Left); } /// /// Creates a object that tracks the Bottom (Y+Height) coordinate of the specified @@ -274,7 +274,7 @@ public abstract record Pos /// /// The that depends on the other view. /// The that will be tracked. - public static Pos Bottom (View? view) { return new PosView (view, Side.Bottom); } + public static Pos Bottom (View view) { return new PosView (view, Side.Bottom); } /// /// Creates a object that tracks the Right (X+Width) coordinate of the specified @@ -282,7 +282,7 @@ public abstract record Pos /// /// The that depends on the other view. /// The that will be tracked. - public static Pos Right (View? view) { return new PosView (view, Side.Right); } + public static Pos Right (View view) { return new PosView (view, Side.Right); } #endregion static Pos creation methods diff --git a/Terminal.Gui/View/View.Drawing.cs b/Terminal.Gui/View/View.Drawing.cs index bb2fa7b27..d73cbaff8 100644 --- a/Terminal.Gui/View/View.Drawing.cs +++ b/Terminal.Gui/View/View.Drawing.cs @@ -83,7 +83,7 @@ public partial class View // Drawing APIs { if (Move (col, row)) { - Driver.AddRune (rune); + Driver?.AddRune (rune); } } @@ -195,7 +195,7 @@ public partial class View // Drawing APIs /// /// /// The view will only be drawn if it is visible, and has any of , , - /// or set. + /// or set. /// /// /// Always use (view-relative) when calling , NOT diff --git a/Terminal.Gui/View/View.Layout.cs b/Terminal.Gui/View/View.Layout.cs index 362236b09..98ad5c566 100644 --- a/Terminal.Gui/View/View.Layout.cs +++ b/Terminal.Gui/View/View.Layout.cs @@ -20,8 +20,7 @@ public partial class View // Layout APIs /// /// /// If does not have a or it's SuperView is not - /// the position will be bound by the and - /// . + /// the position will be bound by . /// /// The View that is to be moved. /// The target x location. @@ -48,7 +47,7 @@ public partial class View // Layout APIs if (viewToMove is not Toplevel || viewToMove?.SuperView is null || viewToMove == Application.Top || viewToMove?.SuperView == Application.Top) { - maxDimension = Driver.Cols; + maxDimension = Application.Screen.Width; superView = Application.Top; } else @@ -135,7 +134,7 @@ public partial class View // Layout APIs if (viewToMove?.SuperView is null || viewToMove == Application.Top || viewToMove?.SuperView == Application.Top) { - maxDimension = statusVisible ? Driver.Rows - 1 : Driver.Rows; + maxDimension = statusVisible ? Application.Screen.Height - 1 : Application.Screen.Height; } else { @@ -591,7 +590,7 @@ public partial class View // Layout APIs } } - catch (Exception e) + catch (Exception _) { // A Dim/PosFunc threw indicating it could not calculate (typically because a dependent View was not laid out). return false; diff --git a/Terminal.Gui/Views/ColorBar.cs b/Terminal.Gui/Views/ColorBar.cs index 581606d17..d6eb0147d 100644 --- a/Terminal.Gui/Views/ColorBar.cs +++ b/Terminal.Gui/Views/ColorBar.cs @@ -91,8 +91,8 @@ internal abstract class ColorBar : View, IColorBar if (!string.IsNullOrWhiteSpace (Text)) { Move (0, 0); - Driver.SetAttribute (HasFocus ? GetFocusColor () : GetNormalColor ()); - Driver.AddStr (Text); + Driver?.SetAttribute (HasFocus ? GetFocusColor () : GetNormalColor ()); + Driver?.AddStr (Text); // TODO: is there a better method than this? this is what it is in TableView xOffset = Text.EnumerateRunes ().Sum (c => c.GetColumns ()); diff --git a/UICatalog/Scenarios/TileViewNesting.cs b/UICatalog/Scenarios/TileViewNesting.cs index 9882865ec..3654c2007 100644 --- a/UICatalog/Scenarios/TileViewNesting.cs +++ b/UICatalog/Scenarios/TileViewNesting.cs @@ -12,7 +12,6 @@ public class TileViewNesting : Scenario private CheckBox _cbHorizontal; private CheckBox _cbTitles; private CheckBox _cbUseLabels; - private bool _loaded; private TextField _textField; private int _viewsCreated; private int _viewsToCreate; @@ -70,8 +69,6 @@ public class TileViewNesting : Scenario top.Add (menu); top.Add (win); - top.Loaded += (s, e) => _loaded = true; - Application.Run (top); top.Dispose (); Application.Shutdown (); diff --git a/UICatalog/UICatalog.cs b/UICatalog/UICatalog.cs index 4a0b42514..dad4091d0 100644 --- a/UICatalog/UICatalog.cs +++ b/UICatalog/UICatalog.cs @@ -1068,7 +1068,7 @@ public class UICatalogApp if (ShVersion is { }) { - ShVersion.Title = $"{RuntimeEnvironment.OperatingSystem} {RuntimeEnvironment.OperatingSystemVersion}, {Driver.GetVersionInfo ()}"; + ShVersion.Title = $"{RuntimeEnvironment.OperatingSystem} {RuntimeEnvironment.OperatingSystemVersion}, {Driver!.GetVersionInfo ()}"; } if (_selectedScenario != null) diff --git a/UnitTests/View/Layout/Dim.CombineTests.cs b/UnitTests/View/Layout/Dim.CombineTests.cs index 6dd68c41e..5ad9ab350 100644 --- a/UnitTests/View/Layout/Dim.CombineTests.cs +++ b/UnitTests/View/Layout/Dim.CombineTests.cs @@ -18,35 +18,4 @@ public class DimCombineTests (ITestOutputHelper output) Assert.Equal (30, result); } - - // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved - // TODO: A new test that calls SetRelativeLayout directly is needed. - [Fact] - [TestRespondersDisposed] - public void DimCombine_View_Not_Added_Throws () - { - var t = new View { Width = 80, Height = 50 }; - - var super = new View { Width = Dim.Width (t) - 2, Height = Dim.Height (t) - 2 }; - t.Add (super); - - var sub = new View (); - super.Add (sub); - - var v1 = new View { Width = Dim.Width (super) - 2, Height = Dim.Height (super) - 2 }; - var v2 = new View { Width = Dim.Width (v1) - 2, Height = Dim.Height (v1) - 2 }; - sub.Add (v1); - - // v2 not added to sub; should cause exception on Layout since it's referenced by sub. - sub.Width = Dim.Fill () - Dim.Width (v2); - sub.Height = Dim.Fill () - Dim.Height (v2); - - t.BeginInit (); - t.EndInit (); - - Assert.Throws (() => t.LayoutSubviews ()); - t.Dispose (); - v2.Dispose (); - } - } diff --git a/UnitTests/View/Layout/Pos.ViewTests.cs b/UnitTests/View/Layout/Pos.ViewTests.cs index e6d04a3d7..8ad4efb07 100644 --- a/UnitTests/View/Layout/Pos.ViewTests.cs +++ b/UnitTests/View/Layout/Pos.ViewTests.cs @@ -256,23 +256,12 @@ public class PosViewTests (ITestOutputHelper output) [Fact] public void PosView_Side_SetToNull_Throws () { - Pos pos = Left (null); - Assert.Throws (() => pos.ToString ()); - - pos = X (null); - Assert.Throws (() => pos.ToString ()); - - pos = Top (null); - Assert.Throws (() => pos.ToString ()); - - pos = Y (null); - Assert.Throws (() => pos.ToString ()); - - pos = Bottom (null); - Assert.Throws (() => pos.ToString ()); - - pos = Right (null); - Assert.Throws (() => pos.ToString ()); + Assert.Throws (() => X (null)); + Assert.Throws (() => Y (null)); + Assert.Throws (() => Left (null)); + Assert.Throws (() => Right (null)); + Assert.Throws (() => Bottom (null)); + Assert.Throws (() => Top (null)); } // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved diff --git a/UnitTests/View/Layout/LayoutTests.cs b/UnitTests/View/Layout/SetLayoutTests.cs similarity index 92% rename from UnitTests/View/Layout/LayoutTests.cs rename to UnitTests/View/Layout/SetLayoutTests.cs index 99cc976eb..024be1b3e 100644 --- a/UnitTests/View/Layout/LayoutTests.cs +++ b/UnitTests/View/Layout/SetLayoutTests.cs @@ -2,7 +2,7 @@ namespace Terminal.Gui.LayoutTests; -public class LayoutTests (ITestOutputHelper output) +public class SetLayoutTests (ITestOutputHelper output) { private readonly ITestOutputHelper _output = output; @@ -173,42 +173,6 @@ public class LayoutTests (ITestOutputHelper output) super.Dispose (); } - [Fact] - public void TopologicalSort_Missing_Add () - { - var root = new View (); - var sub1 = new View (); - root.Add (sub1); - var sub2 = new View (); - sub1.Width = Dim.Width (sub2); - - Assert.Throws (() => root.LayoutSubviews ()); - - sub2.Width = Dim.Width (sub1); - - Assert.Throws (() => root.LayoutSubviews ()); - root.Dispose (); - sub1.Dispose (); - sub2.Dispose (); - } - - [Fact] - public void TopologicalSort_Recursive_Ref () - { - var root = new View (); - var sub1 = new View (); - root.Add (sub1); - var sub2 = new View (); - root.Add (sub2); - sub2.Width = Dim.Width (sub2); - - Exception exception = Record.Exception (root.LayoutSubviews); - Assert.Null (exception); - root.Dispose (); - sub1.Dispose (); - sub2.Dispose (); - } - [Fact] public void LayoutSubviews_Uses_ContentSize () { @@ -695,25 +659,4 @@ public class LayoutTests (ITestOutputHelper output) Assert.Equal (19, v2.Frame.Height); t.Dispose (); } - - - /// This is an intentionally obtuse test. See https://github.com/gui-cs/Terminal.Gui/issues/2461 - [Fact] - [TestRespondersDisposed] - public void Throw_If_SuperView_Refs_SubView () - { - var superView = new View { Width = 80, Height = 25 }; - - var subViewThatRefsSuperView = new View () - { - Width = Dim.Width (superView), - Height = Dim.Height (superView) - }; - - superView.Add (subViewThatRefsSuperView); - - Assert.Throws (() => superView.LayoutSubviews ()); - superView.Dispose (); - } - } diff --git a/UnitTests/View/Layout/TopologicalSortTests.cs b/UnitTests/View/Layout/TopologicalSortTests.cs new file mode 100644 index 000000000..00c1c0277 --- /dev/null +++ b/UnitTests/View/Layout/TopologicalSortTests.cs @@ -0,0 +1,82 @@ +using Xunit.Abstractions; + +namespace Terminal.Gui.LayoutTests; + +public class TopologicalSortTests (ITestOutputHelper output) +{ + private readonly ITestOutputHelper _output = output; + + [Fact] + public void TopologicalSort_Missing_Add () + { + var root = new View (); + var sub1 = new View (); + root.Add (sub1); + var sub2 = new View (); + sub1.Width = Dim.Width (sub2); + + Assert.Throws (() => root.LayoutSubviews ()); + + sub2.Width = Dim.Width (sub1); + + Assert.Throws (() => root.LayoutSubviews ()); + root.Dispose (); + sub1.Dispose (); + sub2.Dispose (); + } + + [Fact] + public void TopologicalSort_Recursive_Ref_Does_Not_Throw () + { + var root = new View (); + var sub1 = new View (); + root.Add (sub1); + var sub2 = new View (); + root.Add (sub2); + sub2.Width = Dim.Width (sub2); + + Exception exception = Record.Exception (root.LayoutSubviews); + Assert.Null (exception); + root.Dispose (); + sub1.Dispose (); + sub2.Dispose (); + } + + + [Fact] + public void TopologicalSort_Throws_If_SuperView_Refs_SubView () + { + var top = new View (); + var superView = new View (); + top.Add (superView); + + var subView = new View (); + superView.Y = Pos.Top (subView); + superView.Add (subView); + + Assert.Throws (() => top.LayoutSubviews ()); + superView.Dispose (); + } + + [Fact] + public void TopologicalSort_View_Not_Added_Throws () + { + var top = new View { Width = 80, Height = 50 }; + + var super = new View { Width = Dim.Width (top) - 2, Height = Dim.Height (top) - 2 }; + top.Add (super); + + var sub = new View (); + super.Add (sub); + + var v1 = new View { Width = Dim.Width (super) - 2, Height = Dim.Height (super) - 2 }; + var v2 = new View { Width = Dim.Width (v1) - 2, Height = Dim.Height (v1) - 2 }; + sub.Add (v1); + + // v2 not added to sub; should cause exception on Layout since it's referenced by sub. + sub.Width = Dim.Fill () - Dim.Width (v2); + sub.Height = Dim.Fill () - Dim.Height (v2); + + Assert.Throws (() => top.Layout ()); + } +}