#nullable enable using Xunit.Abstractions; namespace UnitTests.ViewsTests; public class LabelTests (ITestOutputHelper output) { [Fact] [AutoInitShutdown] public void Text_Set_With_AnchorEnd_Works () { var label = new Label { Y = Pos.Center (), Text = "Say Hello 你" }; label.X = Pos.AnchorEnd (0) - Pos.Func (_ => label.TextFormatter.Text.GetColumns ()); var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () }; win.Add (label); var top = new Runnable (); top.Add (win); Application.Begin (top); Application.Driver!.SetScreenSize (30, 5); AutoInitShutdownAttribute.RunIteration (); var expected = @" ┌────────────────────────────┐ │ │ │ Say Hello 你│ │ │ └────────────────────────────┘ "; DriverAssert.AssertDriverContentsWithFrameAre (expected, output); label.Text = "Say Hello 你 changed"; AutoInitShutdownAttribute.RunIteration (); expected = @" ┌────────────────────────────┐ │ │ │ Say Hello 你 changed│ │ │ └────────────────────────────┘ "; DriverAssert.AssertDriverContentsWithFrameAre (expected, output); top.Dispose (); } [Fact] [AutoInitShutdown] public void Set_Text_With_Center () { var label = new Label { X = Pos.Center (), Y = Pos.Center (), Text = "Say Hello 你" }; var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () }; win.Add (label); var top = new Runnable (); top.Add (win); Application.Begin (top); Application.Driver!.SetScreenSize (30, 5); Application.LayoutAndDraw (); var expected = @" ┌────────────────────────────┐ │ │ │ Say Hello 你 │ │ │ └────────────────────────────┘ "; DriverAssert.AssertDriverContentsWithFrameAre (expected, output); label.Text = "Say Hello 你 changed"; AutoInitShutdownAttribute.RunIteration (); expected = @" ┌────────────────────────────┐ │ │ │ Say Hello 你 changed │ │ │ └────────────────────────────┘ "; DriverAssert.AssertDriverContentsWithFrameAre (expected, output); top.Dispose (); } [Fact] [AutoInitShutdown] public void Label_Draw_Fill_Remaining () { Size tfSize = new (80, 1); Label label = new () { Text = "This label needs to be cleared before rewritten.", Width = tfSize.Width, Height = tfSize.Height }; TextFormatter tf1 = new () { Direction = TextDirection.LeftRight_TopBottom, ConstrainToSize = tfSize }; tf1.Text = "This TextFormatter (tf1) without fill will not be cleared on rewritten."; TextFormatter tf2 = new () { Direction = TextDirection.LeftRight_TopBottom, ConstrainToSize = tfSize, FillRemaining = true }; tf2.Text = "This TextFormatter (tf2) with fill will be cleared on rewritten."; Runnable top = new (); top.Add (label); SessionToken sessionToken = Application.Begin (top); AutoInitShutdownAttribute.RunIteration (); Assert.False (label.TextFormatter.FillRemaining); Assert.False (tf1.FillRemaining); Assert.True (tf2.FillRemaining); AutoInitShutdownAttribute.RunIteration (); tf1.Draw ( Application.Driver, new (new (0, 1), tfSize), label.GetAttributeForRole (VisualRole.Normal), label.GetAttributeForRole (VisualRole.HotNormal)); tf2.Draw ( Application.Driver, new (new (0, 2), tfSize), label.GetAttributeForRole (VisualRole.Normal), label.GetAttributeForRole (VisualRole.HotNormal)); DriverAssert.AssertDriverContentsWithFrameAre ( @" This label needs to be cleared before rewritten. This TextFormatter (tf1) without fill will not be cleared on rewritten. This TextFormatter (tf2) with fill will be cleared on rewritten. ", output ); Assert.False (label.NeedsDraw); Assert.False (label.NeedsLayout); Assert.False (label.SubViewNeedsDraw); label.Text = "This label is rewritten."; Assert.True (label.NeedsDraw); Assert.True (label.NeedsLayout); //Assert.False (label.SubViewNeedsDraw); label.Draw (); tf1.Text = "This TextFormatter (tf1) is rewritten."; tf1.Draw ( Application.Driver, new (new (0, 1), tfSize), label.GetAttributeForRole (VisualRole.Normal), label.GetAttributeForRole (VisualRole.HotNormal)); tf2.Text = "This TextFormatter (tf2) is rewritten."; tf2.Draw ( Application.Driver, new (new (0, 2), tfSize), label.GetAttributeForRole (VisualRole.Normal), label.GetAttributeForRole (VisualRole.HotNormal)); DriverAssert.AssertDriverContentsWithFrameAre ( @" This label is rewritten. This TextFormatter (tf1) is rewritten.will not be cleared on rewritten. This TextFormatter (tf2) is rewritten. ", output ); top.Dispose (); } [Fact] [AutoInitShutdown] public void Label_Draw_Horizontal_Simple_Runes () { var label = new Label { Text = "Demo Simple Text" }; var top = new Runnable (); top.Add (label); Application.Begin (top); AutoInitShutdownAttribute.RunIteration (); Assert.Equal (new (0, 0, 16, 1), label.Frame); var expected = @" Demo Simple Text "; Rectangle pos = DriverAssert.AssertDriverContentsWithFrameAre (expected, output); Assert.Equal (new (0, 0, 16, 1), pos); top.Dispose (); } [Fact] [AutoInitShutdown] public void Label_Draw_Vertical_Simple_Text () { var label = new Label { TextDirection = TextDirection.TopBottom_LeftRight, Text = "Demo Simple Text" }; var top = new Runnable (); top.Add (label); Application.Begin (top); AutoInitShutdownAttribute.RunIteration (); Assert.NotNull (label.Width); Assert.NotNull (label.Height); var expected = @" D e m o S i m p l e T e x t "; Rectangle pos = DriverAssert.AssertDriverContentsWithFrameAre (expected, output); Assert.Equal (new (0, 0, 1, 16), pos); top.Dispose (); } [Fact] [AutoInitShutdown] public void Label_Draw_Vertical_Wide_Runes () { var label = new Label { TextDirection = TextDirection.TopBottom_LeftRight, Text = "デモエムポンズ" }; var top = new Runnable (); top.Add (label); Application.Begin (top); AutoInitShutdownAttribute.RunIteration (); var expected = @" デ モ エ ム ポ ン ズ "; Rectangle pos = DriverAssert.AssertDriverContentsWithFrameAre (expected, output); Assert.Equal (new (0, 0, 2, 7), pos); top.Dispose (); } [Fact] [AutoInitShutdown] public void Update_Only_On_Or_After_Initialize () { var label = new Label { X = Pos.Center (), Y = Pos.Center (), Text = "Say Hello 你" }; var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () }; win.Add (label); var top = new Runnable (); top.Add (win); Assert.False (label.IsInitialized); Application.Begin (top); Application.Driver!.SetScreenSize (30, 5); Application.LayoutAndDraw (); Assert.True (label.IsInitialized); Assert.Equal ("Say Hello 你", label.Text); Assert.Equal ("Say Hello 你", label.TextFormatter.Text); Assert.Equal (new (0, 0, 12, 1), label.Viewport); var expected = @" ┌────────────────────────────┐ │ │ │ Say Hello 你 │ │ │ └────────────────────────────┘ "; Rectangle pos = DriverAssert.AssertDriverContentsWithFrameAre (expected, output); Assert.Equal (new (0, 0, 30, 5), pos); top.Dispose (); } [Fact] [AutoInitShutdown] public void Update_Parameterless_Only_On_Or_After_Initialize () { var label = new Label { X = Pos.Center (), Y = Pos.Center (), Text = "Say Hello 你" }; var win = new Window { Width = Dim.Fill (), Height = Dim.Fill () }; win.Add (label); var top = new Runnable (); top.Add (win); Assert.False (label.IsInitialized); Application.Begin (top); Application.Driver!.SetScreenSize (30, 5); Application.LayoutAndDraw (); Assert.True (label.IsInitialized); Assert.Equal ("Say Hello 你", label.Text); Assert.Equal ("Say Hello 你", label.TextFormatter.Text); Assert.Equal (new (0, 0, 12, 1), label.Viewport); var expected = @" ┌────────────────────────────┐ │ │ │ Say Hello 你 │ │ │ └────────────────────────────┘ "; Rectangle pos = DriverAssert.AssertDriverContentsWithFrameAre (expected, output); Assert.Equal (new (0, 0, 30, 5), pos); top.Dispose (); } [Fact] [SetupFakeApplication] public void Full_Border () { var label = new Label { Driver = Application.Driver, BorderStyle = LineStyle.Single, Text = "Test" }; label.BeginInit (); label.EndInit (); label.SetRelativeLayout (Application.Screen.Size); Assert.Equal (new (0, 0, 4, 1), label.Viewport); Assert.Equal (new (0, 0, 6, 3), label.Frame); label.Draw (); DriverAssert.AssertDriverContentsWithFrameAre ( @" ┌┤Te├┐ │Test│ └────┘", output ); label.Dispose (); } // These tests were formally in AutoSizetrue.cs. They are (poor) Label tests. private readonly string [] _expecteds = new string [21] { @" ┌────────────────────┐ │View with long text │ │ │ └────────────────────┘", @" ┌────────────────────┐ │View with long text │ │Label 0 │ │Label 0 │ └────────────────────┘", @" ┌────────────────────┐ │View with long text │ │Label 0 │ │Label 1 │ │Label 1 │ └────────────────────┘", @" ┌────────────────────┐ │View with long text │ │Label 0 │ │Label 1 │ │Label 2 │ │Label 2 │ └────────────────────┘", @" ┌────────────────────┐ │View with long text │ │Label 0 │ │Label 1 │ │Label 2 │ │Label 3 │ │Label 3 │ └────────────────────┘", @" ┌────────────────────┐ │View with long text │ │Label 0 │ │Label 1 │ │Label 2 │ │Label 3 │ │Label 4 │ │Label 4 │ └────────────────────┘", @" ┌────────────────────┐ │View with long text │ │Label 0 │ │Label 1 │ │Label 2 │ │Label 3 │ │Label 4 │ │Label 5 │ │Label 5 │ └────────────────────┘", @" ┌────────────────────┐ │View with long text │ │Label 0 │ │Label 1 │ │Label 2 │ │Label 3 │ │Label 4 │ │Label 5 │ │Label 6 │ │Label 6 │ └────────────────────┘", @" ┌────────────────────┐ │View with long text │ │Label 0 │ │Label 1 │ │Label 2 │ │Label 3 │ │Label 4 │ │Label 5 │ │Label 6 │ │Label 7 │ │Label 7 │ └────────────────────┘", @" ┌────────────────────┐ │View with long text │ │Label 0 │ │Label 1 │ │Label 2 │ │Label 3 │ │Label 4 │ │Label 5 │ │Label 6 │ │Label 7 │ │Label 8 │ │Label 8 │ └────────────────────┘", @" ┌────────────────────┐ │View with long text │ │Label 0 │ │Label 1 │ │Label 2 │ │Label 3 │ │Label 4 │ │Label 5 │ │Label 6 │ │Label 7 │ │Label 8 │ │Label 9 │ │Label 9 │ └────────────────────┘", @" ┌────────────────────┐ │View with long text │ │Label 0 │ │Label 1 │ │Label 2 │ │Label 3 │ │Label 4 │ │Label 5 │ │Label 6 │ │Label 7 │ │Label 8 │ │Label 9 │ │Label 10 │ │Label 10 │ └────────────────────┘", @" ┌────────────────────┐ │View with long text │ │Label 0 │ │Label 1 │ │Label 2 │ │Label 3 │ │Label 4 │ │Label 5 │ │Label 6 │ │Label 7 │ │Label 8 │ │Label 9 │ │Label 10 │ │Label 11 │ │Label 11 │ └────────────────────┘", @" ┌────────────────────┐ │View with long text │ │Label 0 │ │Label 1 │ │Label 2 │ │Label 3 │ │Label 4 │ │Label 5 │ │Label 6 │ │Label 7 │ │Label 8 │ │Label 9 │ │Label 10 │ │Label 11 │ │Label 12 │ │Label 12 │ └────────────────────┘", @" ┌────────────────────┐ │View with long text │ │Label 0 │ │Label 1 │ │Label 2 │ │Label 3 │ │Label 4 │ │Label 5 │ │Label 6 │ │Label 7 │ │Label 8 │ │Label 9 │ │Label 10 │ │Label 11 │ │Label 12 │ │Label 13 │ │Label 13 │ └────────────────────┘", @" ┌────────────────────┐ │View with long text │ │Label 0 │ │Label 1 │ │Label 2 │ │Label 3 │ │Label 4 │ │Label 5 │ │Label 6 │ │Label 7 │ │Label 8 │ │Label 9 │ │Label 10 │ │Label 11 │ │Label 12 │ │Label 13 │ │Label 14 │ │Label 14 │ └────────────────────┘", @" ┌────────────────────┐ │View with long text │ │Label 0 │ │Label 1 │ │Label 2 │ │Label 3 │ │Label 4 │ │Label 5 │ │Label 6 │ │Label 7 │ │Label 8 │ │Label 9 │ │Label 10 │ │Label 11 │ │Label 12 │ │Label 13 │ │Label 14 │ │Label 15 │ │Label 15 │ └────────────────────┘", @" ┌────────────────────┐ │View with long text │ │Label 0 │ │Label 1 │ │Label 2 │ │Label 3 │ │Label 4 │ │Label 5 │ │Label 6 │ │Label 7 │ │Label 8 │ │Label 9 │ │Label 10 │ │Label 11 │ │Label 12 │ │Label 13 │ │Label 14 │ │Label 15 │ │Label 16 │ │Label 16 │ └────────────────────┘", @" ┌────────────────────┐ │View with long text │ │Label 0 │ │Label 1 │ │Label 2 │ │Label 3 │ │Label 4 │ │Label 5 │ │Label 6 │ │Label 7 │ │Label 8 │ │Label 9 │ │Label 10 │ │Label 11 │ │Label 12 │ │Label 13 │ │Label 14 │ │Label 15 │ │Label 16 │ │Label 17 │ │Label 17 │ └────────────────────┘", @" ┌────────────────────┐ │View with long text │ │Label 0 │ │Label 1 │ │Label 2 │ │Label 3 │ │Label 4 │ │Label 5 │ │Label 6 │ │Label 7 │ │Label 8 │ │Label 9 │ │Label 10 │ │Label 11 │ │Label 12 │ │Label 13 │ │Label 14 │ │Label 15 │ │Label 16 │ │Label 17 │ │Label 18 │ │Label 18 │ └────────────────────┘", @" ┌────────────────────┐ │View with long text │ │Label 0 │ │Label 1 │ │Label 2 │ │Label 3 │ │Label 4 │ │Label 5 │ │Label 6 │ │Label 7 │ │Label 8 │ │Label 9 │ │Label 10 │ │Label 11 │ │Label 12 │ │Label 13 │ │Label 14 │ │Label 15 │ │Label 16 │ │Label 17 │ │Label 18 │ │Label 19 │ │Label 19 │ └────────────────────┘" }; // TODO: This is a Label test. Move to label tests if there's not already a test for this. [Fact] [AutoInitShutdown] public void AnchorEnd_Better_Than_Bottom_Equal_Inside_Window () { var win = new Window (); var label = new Label { Text = "This should be the last line.", //Width = Dim.Fill (), X = 0, // keep unit test focused; don't use Center here Y = Pos.AnchorEnd (1) }; win.Add (label); Runnable top = new (); top.Add (win); SessionToken rs = Application.Begin (top); Application.Driver!.SetScreenSize (40, 10); Application.LayoutAndDraw (); Assert.Equal (29, label.Text.Length); Assert.Equal (new (0, 0, 40, 10), top.Frame); Assert.Equal (new (0, 0, 40, 10), win.Frame); Assert.Equal (new (0, 7, 29, 1), label.Frame); var expected = @" ┌──────────────────────────────────────┐ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │This should be the last line. │ └──────────────────────────────────────┘ " ; DriverAssert.AssertDriverContentsWithFrameAre (expected, output); Application.End (rs); top.Dispose (); } // TODO: This is a Label test. Move to label tests if there's not already a test for this. [Fact] [AutoInitShutdown] public void Bottom_Equal_Inside_Window () { var win = new Window (); var label = new Label { Text = "This should be the last line.", //Width = Dim.Fill (), X = 0, Y = Pos.Bottom (win) - 3 // two lines top and bottom borders more one line above the bottom border }; win.Add (label); Runnable top = new (); top.Add (win); SessionToken rs = Application.Begin (top); Application.Driver!.SetScreenSize (40, 10); Application.LayoutAndDraw (); Assert.Equal (new (0, 0, 40, 10), top.Frame); Assert.Equal (new (0, 0, 40, 10), win.Frame); Assert.Equal (new (0, 7, 29, 1), label.Frame); var expected = @" ┌──────────────────────────────────────┐ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │This should be the last line. │ └──────────────────────────────────────┘ "; DriverAssert.AssertDriverContentsWithFrameAre (expected, output); Application.End (rs); top.Dispose (); } // TODO: This is a Dim test. Move to Dim tests. [Fact] [AutoInitShutdown] public void Dim_Subtract_Operator_With_Text () { Runnable top = new (); var view = new View { Text = "View with long text", X = 0, Y = 0, Width = 20, Height = 1 }; var field = new TextField { X = 0, Y = Pos.Bottom (view), Width = 20 }; var count = 20; List