2
0
Tig 1 жил өмнө
parent
commit
6c117e0f0a

+ 1 - 1
Terminal.Gui/Text/TextFormatter.cs

@@ -175,7 +175,7 @@ public class TextFormatter
             bool textWasNull = _text is null && value != null;
             _text = EnableNeedsFormat (value);
 
-//            if (/*(Alignment != TextAlignment.Justified && VerticalAlignment != VerticalTextAlignment.Justified) ||*/ (textWasNull && Size.IsEmpty))
+            if (AutoSize || (textWasNull && Size.IsEmpty))
             {
                 Size = CalcRect (0, 0, _text, Direction, TabWidth).Size;
             }

+ 123 - 6
UnitTests/Text/TextFormatterTests.cs

@@ -164,7 +164,7 @@ public class TextFormatterTests
     public void CalcRect_Horizontal_Width_Correct (string text, TextDirection textDirection)
     {
         // The width is the number of columns in the text
-        Assert.Equal (new Size ( text.GetColumns (), 1), TextFormatter.CalcRect (0, 0, text, textDirection).Size);
+        Assert.Equal (new Size (text.GetColumns (), 1), TextFormatter.CalcRect (0, 0, text, textDirection).Size);
     }
 
     [Theory]
@@ -177,7 +177,7 @@ public class TextFormatterTests
     public void CalcRect_Vertical_Height_Correct (string text, TextDirection textDirection)
     {
         // The height is based both the number of lines and the number of wide chars
-        Assert.Equal (new Size (1 + text.GetColumns() - text.Length, text.Length), TextFormatter.CalcRect (0, 0, text, textDirection).Size);
+        Assert.Equal (new Size (1 + text.GetColumns () - text.Length, text.Length), TextFormatter.CalcRect (0, 0, text, textDirection).Size);
     }
 
     [Theory]
@@ -2246,7 +2246,7 @@ ssb
 
         tf.Direction = TextDirection.TopBottom_LeftRight;
 
-        if (autoSize && textAlignment != TextAlignment.Justified)
+        if (autoSize/* && textAlignment != TextAlignment.Justified*/)
         {
             Assert.Equal (2, tf.Size.Width);
             Assert.Equal (2, tf.Size.Height);
@@ -2280,7 +2280,7 @@ ssb
 
         tf.Direction = TextDirection.LeftRight_TopBottom;
 
-        if (autoSize && textAlignment != VerticalTextAlignment.Justified)
+        if (autoSize/* && textAlignment != VerticalTextAlignment.Justified*/)
         {
             Assert.Equal (4, tf.Size.Width);
             Assert.Equal (1, tf.Size.Height);
@@ -2350,7 +2350,7 @@ ssb
 
         tf.Size = new (1, 1);
 
-        if (autoSize && textAlignment != TextAlignment.Justified)
+        if (autoSize)
         {
             Assert.Equal (4, tf.Size.Width);
             Assert.Equal (1, tf.Size.Height);
@@ -2384,7 +2384,7 @@ ssb
 
         tf.Size = new (1, 1);
 
-        if (autoSize && textAlignment != VerticalTextAlignment.Justified)
+        if (autoSize)
         {
             Assert.Equal (2, tf.Size.Width);
             Assert.Equal (2, tf.Size.Height);
@@ -2408,6 +2408,8 @@ ssb
         Assert.Equal (1, tf.Size.Height);
         tf.Text = "你你";
 
+        Assert.Equal (autoSize, tf.AutoSize);
+
         if (autoSize)
         {
             if (textDirection == TextDirection.LeftRight_TopBottom)
@@ -3324,4 +3326,119 @@ ssb
                     );
         Assert.Equal (resultLines, wrappedLines);
     }
+
+    [SetupFakeDriver]
+    [Theory]
+    [InlineData ("A", 0, "")]
+    [InlineData ("A", 1, "A")]
+    [InlineData ("A", 2, "A")]
+    [InlineData ("AB", 1, "A")]
+    [InlineData ("AB", 2, "AB")]
+    [InlineData ("ABC", 3, "ABC")]
+    [InlineData ("ABC", 4, "ABC")]
+    [InlineData ("ABC", 6, "ABC")]
+    public void Draw_Horizontal_Left (string text, int width, string expectedText)
+
+    {
+        TextFormatter tf = new ()
+        {
+            Size = new Size (width, 1),
+
+            Text = text,
+            Alignment = TextAlignment.Left
+        };
+        tf.Draw (new Rectangle (0, 0, width, 1), Attribute.Default, Attribute.Default);
+
+        TestHelpers.AssertDriverContentsWithFrameAre (expectedText, _output);
+    }
+
+    [SetupFakeDriver]
+    [Theory]
+    [InlineData ("A", 0, false, "")]
+    [InlineData ("A", 1, false, "A")]
+    [InlineData ("A", 2, false, " A")]
+    [InlineData ("AB", 1, false, "A")]
+    [InlineData ("AB", 2, false, "AB")]
+    [InlineData ("ABC", 3, false, "ABC")]
+    [InlineData ("ABC", 4, false, " ABC")]
+    [InlineData ("ABC", 6, false, "   ABC")]
+
+    [InlineData ("A", 0, true, "")]
+    [InlineData ("A", 1, true, "A")]
+    [InlineData ("A", 2, true, " A")]
+    [InlineData ("AB", 1, true, "")] // BUGBUG: This is wrong, it should be "A"
+    [InlineData ("AB", 2, true, "AB")]
+    [InlineData ("ABC", 3, true, "ABC")]
+    [InlineData ("ABC", 4, true, " ABC")]
+    [InlineData ("ABC", 6, true, "   ABC")]
+    public void Draw_Horizontal_Right (string text, int width, bool autoSize, string expectedText)
+    {
+        TextFormatter tf = new ()
+        {
+            Text = text,
+            Alignment = TextAlignment.Right,
+            AutoSize = autoSize,
+        };
+
+        if (!autoSize)
+        {
+            tf.Size = new Size (width, 1);
+        }
+
+        tf.Draw (new Rectangle (Point.Empty, new (width, 1)), Attribute.Default, Attribute.Default);
+        TestHelpers.AssertDriverContentsWithFrameAre (expectedText, _output);
+    }
+
+    [SetupFakeDriver]
+    [Theory]
+    [InlineData ("A", 0, "")]
+    [InlineData ("A", 1, "A")]
+    [InlineData ("A", 2, "A")]
+    [InlineData ("A", 3, " A")]
+    [InlineData ("AB", 1, "A")]
+    [InlineData ("AB", 2, "AB")]
+    [InlineData ("ABC", 3, "ABC")]
+    [InlineData ("ABC", 4, "ABC")]
+    [InlineData ("ABC", 5, " ABC")]
+    [InlineData ("ABC", 6, " ABC")]
+    [InlineData ("ABC", 9, "   ABC")]
+    public void Draw_Horizontal_Centered (string text, int width, string expectedText)
+    {
+        TextFormatter tf = new ()
+        {
+            Size = new Size (width, 1),
+            Text = text,
+            Alignment = TextAlignment.Centered
+        };
+        tf.Draw (new Rectangle (0, 0, width, 1), Attribute.Default, Attribute.Default);
+
+        TestHelpers.AssertDriverContentsWithFrameAre (expectedText, _output);
+    }
+
+    [SetupFakeDriver]
+    [Theory]
+    [InlineData ("A", 0, "")]
+    [InlineData ("A", 1, "A")]
+    [InlineData ("A", 2, "A")]
+    [InlineData ("A B", 3, "A B")]
+    [InlineData ("A B", 1, "A")]
+    [InlineData ("A B", 2, "A")]
+    [InlineData ("A B", 3, "A B")]
+    [InlineData ("A B", 4, "A  B")]
+    [InlineData ("A B", 5, "A   B")]
+    [InlineData ("A B", 6, "A    B")]
+    [InlineData ("A B", 10, "A        B")]
+    [InlineData ("ABC ABC", 10, "ABC    ABC")]
+    public void Draw_Horizontal_Justified (string text, int width, string expectedText)
+    {
+        TextFormatter tf = new ()
+        {
+            Size = new Size (width, 1),
+            Text = text,
+            Alignment = TextAlignment.Justified,
+        };
+        tf.Draw (new Rectangle (0, 0, width, 1), Attribute.Default, Attribute.Default);
+
+        TestHelpers.AssertDriverContentsWithFrameAre (expectedText, _output);
+    }
 }

+ 5 - 65
UnitTests/View/Text/AutoSizeTrueTests.cs

@@ -2687,18 +2687,18 @@ Y
         Assert.Equal (new (2, 11), verticalView.GetSizeNeededForTextWithoutHotKey ());
         Assert.Equal (verticalView.Frame.Size, verticalView.GetSizeNeededForTextWithoutHotKey ());
 
-        text = "Say He_llo 你";
+        text = "012345678你";
         horizontalView.Text = text;
         verticalView.Text = text;
 
         Assert.True (horizontalView.AutoSize);
-        Assert.Equal (new (0, 0, 12, 1), horizontalView.Frame);
-        Assert.Equal (new (12, 1), horizontalView.GetSizeNeededForTextWithoutHotKey ());
+        Assert.Equal (new (0, 0, 11, 1), horizontalView.Frame);
+        Assert.Equal (new (11, 1), horizontalView.GetSizeNeededForTextWithoutHotKey ());
         Assert.Equal (horizontalView.Frame.Size, horizontalView.GetSizeNeededForTextWithoutHotKey ());
 
         Assert.True (verticalView.AutoSize);
-        Assert.Equal (new (0, 0, 2, 11), verticalView.Frame);
-        Assert.Equal (new (2, 11), verticalView.GetSizeNeededForTextWithoutHotKey ());
+        Assert.Equal (new (0, 0, 2, 10), verticalView.Frame);
+        Assert.Equal (new (2, 10), verticalView.GetSizeNeededForTextWithoutHotKey ());
         Assert.Equal (verticalView.Frame.Size, verticalView.GetSizeNeededForTextWithoutHotKey ());
     }
 
@@ -2716,65 +2716,6 @@ Y
         Assert.Equal (new (0, 0, 20, 1), view.Frame);
     }
 
-    [Fact]
-    [AutoInitShutdown]
-    public void Setting_Frame_Dont_Respect_AutoSize_True_On_Layout_Absolute ()
-    {
-        var view1 = new View { Frame = new (0, 0, 10, 0), Text = "Say Hello view1 你", AutoSize = true };
-
-        var viewTopBottom_LeftRight = new View
-        {
-            Frame = new (0, 0, 0, 10),
-            Text = "Say Hello view2 你",
-            AutoSize = true,
-            TextDirection =
-                TextDirection.TopBottom_LeftRight
-        };
-        var top = new Toplevel ();
-        top.Add (view1, viewTopBottom_LeftRight);
-
-        RunState rs = Application.Begin (top);
-
-        Assert.True (view1.AutoSize);
-        Assert.Equal (LayoutStyle.Absolute, view1.LayoutStyle);
-        Assert.Equal (new (0, 0, 18, 1), view1.Frame);
-        Assert.Equal ("Absolute(0)", view1.X.ToString ());
-        Assert.Equal ("Absolute(0)", view1.Y.ToString ());
-        Assert.Equal ("Absolute(18)", view1.Width.ToString ());
-        Assert.Equal ("Absolute(1)", view1.Height.ToString ());
-
-        Assert.True (viewTopBottom_LeftRight.AutoSize);
-        Assert.Equal (LayoutStyle.Absolute, viewTopBottom_LeftRight.LayoutStyle);
-        Assert.Equal (new (0, 0, 18, 17), viewTopBottom_LeftRight.Frame);
-        Assert.Equal ("Absolute(0)", viewTopBottom_LeftRight.X.ToString ());
-        Assert.Equal ("Absolute(0)", viewTopBottom_LeftRight.Y.ToString ());
-        Assert.Equal ("Absolute(18)", viewTopBottom_LeftRight.Width.ToString ());
-        Assert.Equal ("Absolute(17)", viewTopBottom_LeftRight.Height.ToString ());
-
-        view1.Frame = new (0, 0, 25, 4);
-        var firstIteration = false;
-        Application.RunIteration (ref rs, ref firstIteration);
-
-        Assert.True (view1.AutoSize);
-        Assert.Equal (LayoutStyle.Absolute, view1.LayoutStyle);
-        Assert.Equal (new (0, 0, 25, 4), view1.Frame);
-        Assert.Equal ("Absolute(0)", view1.X.ToString ());
-        Assert.Equal ("Absolute(0)", view1.Y.ToString ());
-        Assert.Equal ("Absolute(25)", view1.Width.ToString ());
-        Assert.Equal ("Absolute(4)", view1.Height.ToString ());
-
-        viewTopBottom_LeftRight.Frame = new (0, 0, 1, 25);
-        Application.RunIteration (ref rs, ref firstIteration);
-
-        Assert.True (viewTopBottom_LeftRight.AutoSize);
-        Assert.Equal (LayoutStyle.Absolute, viewTopBottom_LeftRight.LayoutStyle);
-        Assert.Equal (new (0, 0, 2, 25), viewTopBottom_LeftRight.Frame);
-        Assert.Equal ("Absolute(0)", viewTopBottom_LeftRight.X.ToString ());
-        Assert.Equal ("Absolute(0)", viewTopBottom_LeftRight.Y.ToString ());
-        Assert.Equal ("Absolute(2)", viewTopBottom_LeftRight.Width.ToString ());
-        Assert.Equal ("Absolute(25)", viewTopBottom_LeftRight.Height.ToString ());
-        Application.End (rs);
-    }
 
     //[Fact]
     //[AutoInitShutdown]
@@ -2900,7 +2841,6 @@ Y
             Assert.Equal (expectedSize, lblLeft.TextFormatter.Size);
             Assert.Equal (expectedSize, lblCenter.TextFormatter.Size);
             Assert.Equal (expectedSize, lblRight.TextFormatter.Size);
-            expectedSize = new (width, 1);
             Assert.Equal (expectedSize, lblJust.TextFormatter.Size);
         }
         else