From 00daccfcf18895ca9e3e9f8b13c6e422b59c41ae Mon Sep 17 00:00:00 2001 From: Tig Date: Thu, 16 May 2024 06:50:19 -0700 Subject: [PATCH] Renamed Pos.Function->Func to align iwth C# Func type. Pos API doc fixes and code cleanup. --- Terminal.Gui/View/Layout/Pos.cs | 4 +- Terminal.Gui/Views/FileDialog.cs | 4 +- UICatalog/Scenarios/Adornments.cs | 2 +- UnitTests/View/Layout/Dim.FuncTests.cs | 12 +++-- UnitTests/View/Layout/Pos.AnchorEndTests.cs | 2 +- UnitTests/View/Layout/Pos.FuncTests.cs | 45 +++++++++++++++++++ UnitTests/View/Layout/Pos.Tests.cs | 8 ++-- .../View/Layout/SetRelativeLayoutTests.cs | 2 +- UnitTests/Views/LabelTests.cs | 2 +- UnitTests/Views/TileViewTests.cs | 2 +- 10 files changed, 63 insertions(+), 20 deletions(-) create mode 100644 UnitTests/View/Layout/Pos.FuncTests.cs diff --git a/Terminal.Gui/View/Layout/Pos.cs b/Terminal.Gui/View/Layout/Pos.cs index a457b0c2c..32294991b 100644 --- a/Terminal.Gui/View/Layout/Pos.cs +++ b/Terminal.Gui/View/Layout/Pos.cs @@ -51,7 +51,7 @@ public enum Side /// /// /// -/// +/// /// /// /// Creates a object that computes the position by executing the provided @@ -220,7 +220,7 @@ public class Pos /// /// The function to be executed. /// The returned from the function. - public static Pos Function (Func function) { return new PosFunc (function); } + public static Pos Func (Func function) { return new PosFunc (function); } /// Creates a percentage object /// The percent object. diff --git a/Terminal.Gui/Views/FileDialog.cs b/Terminal.Gui/Views/FileDialog.cs index 8c68c657a..3be8ba4ef 100644 --- a/Terminal.Gui/Views/FileDialog.cs +++ b/Terminal.Gui/Views/FileDialog.cs @@ -68,7 +68,7 @@ public class FileDialog : Dialog _btnOk = new Button { - Y = Pos.AnchorEnd (1), X = Pos.Function (CalculateOkButtonPosX), IsDefault = true, Text = Style.OkButtonText + Y = Pos.AnchorEnd (1), X = Pos.Func (CalculateOkButtonPosX), IsDefault = true, Text = Style.OkButtonText }; _btnOk.Accept += (s, e) => Accept (true); @@ -457,7 +457,7 @@ public class FileDialog : Dialog if (Style.FlipOkCancelButtonLayoutOrder) { - _btnCancel.X = Pos.Function (CalculateOkButtonPosX); + _btnCancel.X = Pos.Func (CalculateOkButtonPosX); _btnOk.X = Pos.Right (_btnCancel) + 1; // Flip tab order too for consistency diff --git a/UICatalog/Scenarios/Adornments.cs b/UICatalog/Scenarios/Adornments.cs index c9c6e363a..c47f2af83 100644 --- a/UICatalog/Scenarios/Adornments.cs +++ b/UICatalog/Scenarios/Adornments.cs @@ -224,7 +224,7 @@ public class Adornments : Scenario _leftEdit = new () { - X = Pos.Left (_topEdit) - Pos.Function (() => _topEdit.Digits) - 2, Y = Pos.Bottom (_topEdit) + X = Pos.Left (_topEdit) - Pos.Func (() => _topEdit.Digits) - 2, Y = Pos.Bottom (_topEdit) }; _leftEdit.ValueChanging += Left_ValueChanging; diff --git a/UnitTests/View/Layout/Dim.FuncTests.cs b/UnitTests/View/Layout/Dim.FuncTests.cs index efb0c57c0..1ff1d450f 100644 --- a/UnitTests/View/Layout/Dim.FuncTests.cs +++ b/UnitTests/View/Layout/Dim.FuncTests.cs @@ -7,19 +7,18 @@ public class DimFuncTests (ITestOutputHelper output) { private readonly ITestOutputHelper _output = output; - [Fact] public void DimFunc_Equal () { Func f1 = () => 0; Func f2 = () => 0; - Dim dim1 = Dim.Func (f1); - Dim dim2 = Dim.Func (f2); + Dim dim1 = Func (f1); + Dim dim2 = Func (f2); Assert.Equal (dim1, dim2); f2 = () => 1; - dim2 = Dim.Func (f2); + dim2 = Func (f2); Assert.NotEqual (dim1, dim2); } @@ -27,7 +26,7 @@ public class DimFuncTests (ITestOutputHelper output) public void DimFunc_SetsValue () { var text = "Test"; - Dim dim = Dim.Func (() => text.Length); + Dim dim = Func (() => text.Length); Assert.Equal ("DimFunc(4)", dim.ToString ()); text = "New Test"; @@ -37,12 +36,11 @@ public class DimFuncTests (ITestOutputHelper output) Assert.Equal ("DimFunc(0)", dim.ToString ()); } - [Fact] public void DimFunc_Calculate_ReturnsCorrectValue () { var dim = new DimFunc (() => 10); - var result = dim.Calculate (0, 100, null, Dimension.None); + int result = dim.Calculate (0, 100, null, Dimension.None); Assert.Equal (10, result); } } diff --git a/UnitTests/View/Layout/Pos.AnchorEndTests.cs b/UnitTests/View/Layout/Pos.AnchorEndTests.cs index 16bba2cc6..59901b714 100644 --- a/UnitTests/View/Layout/Pos.AnchorEndTests.cs +++ b/UnitTests/View/Layout/Pos.AnchorEndTests.cs @@ -195,7 +195,7 @@ public class PosAnchorEndTests (ITestOutputHelper output) int Btn_Width () { return btn?.Viewport.Width ?? 0; } - btn = new () { Text = "Ok", X = Pos.AnchorEnd (0) - Pos.Function (Btn_Width) }; + btn = new () { Text = "Ok", X = Pos.AnchorEnd (0) - Pos.Func (Btn_Width) }; var view = new View { diff --git a/UnitTests/View/Layout/Pos.FuncTests.cs b/UnitTests/View/Layout/Pos.FuncTests.cs new file mode 100644 index 000000000..ad9d332e2 --- /dev/null +++ b/UnitTests/View/Layout/Pos.FuncTests.cs @@ -0,0 +1,45 @@ +using Xunit.Abstractions; + +namespace Terminal.Gui.PosDimTests; + +public class PosFuncTests (ITestOutputHelper output) +{ + private readonly ITestOutputHelper _output = output; + + [Fact] + public void PosFunc_Equal () + { + Func f1 = () => 0; + Func f2 = () => 0; + + Pos pos1 = Pos.Func (f1); + Pos pos2 = Pos.Func (f2); + Assert.Equal (pos1, pos2); + + f2 = () => 1; + pos2 = Pos.Func (f2); + Assert.NotEqual (pos1, pos2); + } + + [Fact] + public void PosFunc_SetsValue () + { + var text = "Test"; + Pos pos = Pos.Func (() => text.Length); + Assert.Equal ("PosFunc(4)", pos.ToString ()); + + text = "New Test"; + Assert.Equal ("PosFunc(8)", pos.ToString ()); + + text = ""; + Assert.Equal ("PosFunc(0)", pos.ToString ()); + } + + [Fact] + public void PosFunc_Calculate_ReturnsCorrectValue () + { + var pos = new PosFunc (() => 10); + int result = pos.Calculate (0, 100, null, Dimension.None); + Assert.Equal (10, result); + } +} diff --git a/UnitTests/View/Layout/Pos.Tests.cs b/UnitTests/View/Layout/Pos.Tests.cs index 8e8079cbd..e4728e343 100644 --- a/UnitTests/View/Layout/Pos.Tests.cs +++ b/UnitTests/View/Layout/Pos.Tests.cs @@ -185,12 +185,12 @@ public class PosTests (ITestOutputHelper output) Func f1 = () => 0; Func f2 = () => 0; - Pos pos1 = Pos.Function (f1); - Pos pos2 = Pos.Function (f2); + Pos pos1 = Pos.Func (f1); + Pos pos2 = Pos.Func (f2); Assert.Equal (pos1, pos2); f2 = () => 1; - pos2 = Pos.Function (f2); + pos2 = Pos.Func (f2); Assert.NotEqual (pos1, pos2); } @@ -198,7 +198,7 @@ public class PosTests (ITestOutputHelper output) public void PosFunction_SetsValue () { var text = "Test"; - Pos pos = Pos.Function (() => text.Length); + Pos pos = Pos.Func (() => text.Length); Assert.Equal ("PosFunc(4)", pos.ToString ()); text = "New Test"; diff --git a/UnitTests/View/Layout/SetRelativeLayoutTests.cs b/UnitTests/View/Layout/SetRelativeLayoutTests.cs index f5931a5c4..20fb45be8 100644 --- a/UnitTests/View/Layout/SetRelativeLayoutTests.cs +++ b/UnitTests/View/Layout/SetRelativeLayoutTests.cs @@ -403,7 +403,7 @@ public class SetRelativeLayoutTests Width = Auto (DimAutoStyle.Text), Height = Auto (DimAutoStyle.Text) }; - view.X = Pos.AnchorEnd (0) - Pos.Function (GetViewWidth); + view.X = Pos.AnchorEnd (0) - Pos.Func (GetViewWidth); int GetViewWidth () { return view.Frame.Width; } diff --git a/UnitTests/Views/LabelTests.cs b/UnitTests/Views/LabelTests.cs index 21caa06af..938b3a788 100644 --- a/UnitTests/Views/LabelTests.cs +++ b/UnitTests/Views/LabelTests.cs @@ -93,7 +93,7 @@ public class LabelTests public void AutoSize_Stays_True_AnchorEnd () { var label = new Label { Y = Pos.Center (), Text = "Say Hello 你" }; - label.X = Pos.AnchorEnd (0) - Pos.Function (() => label.TextFormatter.Text.GetColumns ()); + label.X = Pos.AnchorEnd (0) - Pos.Func (() => label.TextFormatter.Text.GetColumns ()); var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () }; win.Add (label); diff --git a/UnitTests/Views/TileViewTests.cs b/UnitTests/Views/TileViewTests.cs index 269b10a51..861b7e0b9 100644 --- a/UnitTests/Views/TileViewTests.cs +++ b/UnitTests/Views/TileViewTests.cs @@ -1663,7 +1663,7 @@ public class TileViewTests var ex = Assert.Throws (() => tileView.SetSplitterPos (0, Pos.Right (tileView))); Assert.Equal ("Only Percent and Absolute values are supported. Passed value was PosView", ex.Message); - ex = Assert.Throws (() => tileView.SetSplitterPos (0, Pos.Function (() => 1))); + ex = Assert.Throws (() => tileView.SetSplitterPos (0, Pos.Func (() => 1))); Assert.Equal ("Only Percent and Absolute values are supported. Passed value was PosFunc", ex.Message); // Also not allowed because this results in a PosCombine