Fixes #3951. Add DimFuncWithView with a View dependency

This commit is contained in:
BDisp
2025-07-25 03:06:49 +01:00
parent f9202c5594
commit b6d0aefd9b
27 changed files with 166 additions and 36 deletions

View File

@@ -263,6 +263,7 @@ public partial class DimAutoTests
view.BeginInit ();
view.EndInit ();
view.Layout ();
// subview should be centered in the parent view + 1
Assert.Equal ((view.Viewport.Width - subview.Frame.Width) / 2 + 1, subview.Frame.X);
@@ -338,6 +339,7 @@ public partial class DimAutoTests
view.BeginInit ();
view.EndInit ();
view.Layout ();
// subview should be at the end of the view
Assert.Equal (view.Viewport.Width - subview.Frame.Width, subview.Frame.X);
@@ -392,6 +394,7 @@ public partial class DimAutoTests
view.BeginInit ();
view.EndInit ();
view.Layout ();
// subview should be at the end of the view
Assert.Equal (view.Viewport.Width - subview.Frame.Width, subview.Frame.X);
@@ -446,6 +449,7 @@ public partial class DimAutoTests
view.BeginInit ();
view.EndInit ();
view.Layout ();
// subview should be at the end of the view
Assert.Equal (view.Viewport.Width - subview.Frame.Width, subview.Frame.X);
@@ -508,6 +512,7 @@ public partial class DimAutoTests
view.BeginInit ();
view.EndInit ();
view.Layout ();
// subview should be at the end of the view
Assert.Equal (view.Viewport.Width - subview.Frame.Width, subview.Frame.X);
@@ -572,11 +577,13 @@ public partial class DimAutoTests
view.BeginInit ();
view.EndInit ();
view.Layout ();
// subview should be at the end of the view
Assert.Equal (view.Viewport.Width - subview.Frame.Width, subview.Frame.X);
Assert.Equal (view.Viewport.Height - subview.Frame.Height, subview.Frame.Y);
}
[Theory]
[InlineData (0, 10, 0, 10, 10, 2)]
[InlineData (0, 5, 0, 5, 5, 3)] // max width of 5 should cause wordwrap at 5 giving a height of 2 + 1

View File

@@ -0,0 +1,58 @@
using Xunit.Abstractions;
using static Terminal.Gui.ViewBase.Dim;
namespace Terminal.Gui.LayoutTests;
public class DimFuncWithViewTests (ITestOutputHelper output)
{
private readonly ITestOutputHelper _output = output;
[Fact]
public void DimFuncWithView_Equal ()
{
Func<View, int> f1 = v => v.Frame.Width;
Func<View, int> f2 = v => v.Frame.Width;
View view1 = new ();
View view2 = new ();
Dim dim1 = FuncWithView (f1, view1);
Dim dim2 = FuncWithView (f1, view1);
Assert.Equal (dim1, dim2);
dim2 = FuncWithView (f2, view2);
Assert.NotEqual (dim1, dim2);
view2.Width = 1;
Assert.NotEqual (dim1, dim2);
Assert.Equal (1, f2 (view2));
}
[Fact]
public void DimFuncWithView_SetsValue ()
{
View view = new () { Text = "Test" };
Dim dim = FuncWithView (v => v.Text.Length, view);
Assert.Equal ("DimFuncWithView(4)", dim.ToString ());
view.Text = "New Test";
Assert.Equal ("DimFuncWithView(8)", dim.ToString ());
view.Text = "";
Assert.Equal ("DimFuncWithView(0)", dim.ToString ());
}
[Fact]
public void DimFuncWithView_Calculate_ReturnsCorrectValue ()
{
View view = new () { Width = 10 };
var dim = new DimFuncWithView (v => v.Frame.Width, view);
int result = dim.Calculate (0, 100, view, Dimension.None);
Assert.Equal (10, result);
}
[Fact]
public void DimFuncWithView_Throws_ArgumentNullException_If_View_Is_Null ()
{
Assert.Throws<ArgumentNullException> (() => FuncWithView (v => 0, null));
}
}

View File

@@ -376,24 +376,24 @@ public class SetLayoutTests : GlobalTestSetup
Assert.Equal (0, layoutStartedCount);
Assert.Equal (0, layoutCompleteCount);
superView.EndInit ();
Assert.Equal (1, borderLayoutStartedCount);
Assert.Equal (1, borderLayoutCompleteCount);
Assert.Equal (2, layoutStartedCount);
Assert.Equal (2, layoutCompleteCount);
superView.EndInit (); // Only sets NeedsLayout
Assert.Equal (0, borderLayoutStartedCount);
Assert.Equal (0, borderLayoutCompleteCount);
Assert.Equal (0, layoutStartedCount);
Assert.Equal (0, layoutCompleteCount);
superView.LayoutSubViews ();
Assert.Equal (1, borderLayoutStartedCount);
Assert.Equal (1, borderLayoutCompleteCount);
Assert.Equal (3, layoutStartedCount);
Assert.Equal (3, layoutCompleteCount);
Assert.Equal (0, borderLayoutStartedCount); // No Border
Assert.Equal (0, borderLayoutCompleteCount); // No Border
Assert.Equal (1, layoutStartedCount);
Assert.Equal (1, layoutCompleteCount);
superView.SetNeedsLayout ();
superView.LayoutSubViews ();
Assert.Equal (1, borderLayoutStartedCount);
Assert.Equal (1, borderLayoutCompleteCount);
Assert.Equal (4, layoutStartedCount);
Assert.Equal (4, layoutCompleteCount);
Assert.Equal (0, borderLayoutStartedCount); // No Border
Assert.Equal (0, borderLayoutCompleteCount); // No Border
Assert.Equal (2, layoutStartedCount);
Assert.Equal (2, layoutCompleteCount);
superView.Dispose ();
}
@@ -443,8 +443,8 @@ public class SetLayoutTests : GlobalTestSetup
superView.BeginInit ();
superView.EndInit ();
superView.LayoutSubViews ();
Assert.Equal (3, layoutStartedRaised);
Assert.Equal (3, layoutCompleteRaised);
Assert.Equal (2, layoutStartedRaised);
Assert.Equal (2, layoutCompleteRaised);
}
[Fact]

View File

@@ -409,6 +409,7 @@ public class SetRelativeLayoutTests
// BUGBUG: IsInitialized need to be true before calculate
view.BeginInit ();
view.EndInit ();
view.Layout ();
view.SetRelativeLayout (screen);
Assert.Equal (27, view.Frame.X);
Assert.Equal (0, view.Frame.Y);

View File

@@ -170,6 +170,7 @@ public class TextTests ()
};
view.BeginInit ();
view.EndInit ();
view.Layout ();
Assert.Equal (new (0, 0, 10, 1), view.Frame);
Assert.Equal (new (0, 0, 10, 1), view.Viewport);