ソースを参照

Added primitive AnchorEnd tests

Tig 1 年間 前
コミット
05d3399f35
1 ファイル変更135 行追加70 行削除
  1. 135 70
      UnitTests/View/Layout/AnchorEndTests.cs

+ 135 - 70
UnitTests/View/Layout/AnchorEndTests.cs

@@ -1,84 +1,64 @@
 using Xunit.Abstractions;
+using static Terminal.Gui.Pos;
 
 namespace Terminal.Gui.ViewTests;
 
 public class AnchorEndTests (ITestOutputHelper output)
 {
     [Fact]
-    public void AnchorEnd_Equal ()
+    public void PosAnchorEnd_Constructor ()
     {
-        var n1 = 0;
-        var n2 = 0;
+        var posAnchorEnd = new PosAnchorEnd (10);
+        Assert.NotNull (posAnchorEnd);
+    }
 
-        Pos pos1 = Pos.AnchorEnd (n1);
-        Pos pos2 = Pos.AnchorEnd (n2);
-        Assert.Equal (pos1, pos2);
+    [Theory]
+    [InlineData (0, 0, true)]
+    [InlineData (10, 10, true)]
+    [InlineData (0, 10, false)]
+    [InlineData (10, 1, false)]
+    public void PosAnchorEnd_Equals (int offset1, int offset2, bool expectedEquals)
+    {
+        var posAnchorEnd1 = new PosAnchorEnd (offset1);
+        var posAnchorEnd2 = new PosAnchorEnd (offset2);
 
-        // Test inequality
-        n2 = 5;
-        pos2 = Pos.AnchorEnd (n2);
-        Assert.NotEqual (pos1, pos2);
+        Assert.Equal (expectedEquals, posAnchorEnd1.Equals (posAnchorEnd2));
+        Assert.Equal (expectedEquals, posAnchorEnd2.Equals (posAnchorEnd1));
     }
 
-    // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
-    // TODO: A new test that calls SetRelativeLayout directly is needed.
     [Fact]
-    [AutoInitShutdown]
-    public void AnchorEnd_Equal_Inside_Window ()
+    public void PosAnchorEnd_GetHashCode ()
     {
-        var viewWidth = 10;
-        var viewHeight = 1;
-
-        var tv = new TextView
-        {
-            X = Pos.AnchorEnd (viewWidth), Y = Pos.AnchorEnd (viewHeight), Width = viewWidth, Height = viewHeight
-        };
-
-        var win = new Window ();
-
-        win.Add (tv);
-
-        Toplevel top = new ();
-        top.Add (win);
-        RunState rs = Application.Begin (top);
+        var posAnchorEnd = new PosAnchorEnd (10);
+        var expectedHashCode = 10.GetHashCode ();
 
-        Assert.Equal (new (0, 0, 80, 25), top.Frame);
-        Assert.Equal (new (0, 0, 80, 25), win.Frame);
-        Assert.Equal (new (68, 22, 10, 1), tv.Frame);
-        Application.End (rs);
+        Assert.Equal (expectedHashCode, posAnchorEnd.GetHashCode ());
     }
 
-    // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
-    // TODO: A new test that calls SetRelativeLayout directly is needed.
     [Fact]
-    [AutoInitShutdown]
-    public void AnchorEnd_Equal_Inside_Window_With_MenuBar_And_StatusBar_On_Toplevel ()
+    public void PosAnchorEnd_ToString ()
     {
-        var viewWidth = 10;
-        var viewHeight = 1;
-
-        var tv = new TextView
-        {
-            X = Pos.AnchorEnd (viewWidth), Y = Pos.AnchorEnd (viewHeight), Width = viewWidth, Height = viewHeight
-        };
+        var posAnchorEnd = new PosAnchorEnd (10);
+        var expectedString = "AnchorEnd(10)";
 
-        var win = new Window ();
-
-        win.Add (tv);
+        Assert.Equal (expectedString, posAnchorEnd.ToString ());
+    }
 
-        var menu = new MenuBar ();
-        var status = new StatusBar ();
-        Toplevel top = new ();
-        top.Add (win, menu, status);
-        RunState rs = Application.Begin (top);
+    [Fact]
+    public void PosAnchorEnd_Anchor ()
+    {
+        var posAnchorEnd = new PosAnchorEnd (10);
+        var width = 50;
+        var expectedAnchor = width - 10;
 
-        Assert.Equal (new (0, 0, 80, 25), top.Frame);
-        Assert.Equal (new (0, 0, 80, 1), menu.Frame);
-        Assert.Equal (new (0, 24, 80, 1), status.Frame);
-        Assert.Equal (new (0, 1, 80, 23), win.Frame);
-        Assert.Equal (new (68, 20, 10, 1), tv.Frame);
+        Assert.Equal (expectedAnchor, posAnchorEnd.Anchor (width));
+    }
 
-        Application.End (rs);
+    [Fact]
+    public void AnchorEnd_CreatesCorrectInstance ()
+    {
+        var pos = Pos.AnchorEnd (10);
+        Assert.IsType<PosAnchorEnd> (pos);
     }
 
     [Fact]
@@ -89,16 +69,40 @@ public class AnchorEndTests (ITestOutputHelper output)
         Assert.Throws<ArgumentException> (() => pos = Pos.AnchorEnd (n));
     }
 
-    [Fact]
-    public void AnchorEnd_SetsValue ()
+    [Theory]
+    [InlineData (0)]
+    [InlineData (1)]
+    public void AnchorEnd_SetsValue_Anchor_Is_Negative (int offset)
+    {
+        Pos pos = Pos.AnchorEnd (offset);
+        Assert.Equal (offset, -pos.Anchor (0));
+    }
+
+    [Theory]
+    [InlineData (0, 0, 25)]
+    [InlineData (0, 10, 25)]
+    [InlineData (1, 10, 24)]
+    [InlineData (10, 10, 15)]
+    [InlineData (20, 10, 5)]
+    public void AnchorEnd_With_Offset_PositionsViewOffsetFromRight (int offset, int width, int expectedXPosition)
     {
-        var n = 0;
-        Pos pos = Pos.AnchorEnd (0);
-        Assert.Equal ($"AnchorEnd({n})", pos.ToString ());
+        // Arrange
+        var superView = new View { Width = 25, Height = 25 };
+        var view = new View
+        {
+            X = Pos.AnchorEnd (offset),
+            Width = width,
+            Height = 1
+        };
+        superView.Add (view);
+        superView.BeginInit ();
+        superView.EndInit ();
 
-        n = 5;
-        pos = Pos.AnchorEnd (n);
-        Assert.Equal ($"AnchorEnd({n})", pos.ToString ());
+        // Act
+        superView.LayoutSubviews ();
+
+        // Assert
+        Assert.Equal (expectedXPosition, view.Frame.X);
     }
 
     // This test used to be Dialog_In_Window_With_TextField_And_Button_AnchorEnd in DialogTests.
@@ -117,7 +121,7 @@ public class AnchorEndTests (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.Function (Btn_Width) };
 
         var view = new View
         {
@@ -149,11 +153,72 @@ public class AnchorEndTests (ITestOutputHelper output)
 
         var expected = $@"
 ┌────────────────┐
-│012345678 {
-    b
-}│
+│012345678 {b}│
 └────────────────┘
 ";
         _ = TestHelpers.AssertDriverContentsWithFrameAre (expected, output);
     }
+
+
+    // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
+    // TODO: A new test that calls SetRelativeLayout directly is needed.
+    [Fact]
+    [AutoInitShutdown]
+    public void AnchorEnd_Equal_Inside_Window ()
+    {
+        var viewWidth = 10;
+        var viewHeight = 1;
+
+        var tv = new TextView
+        {
+            X = Pos.AnchorEnd (viewWidth), Y = Pos.AnchorEnd (viewHeight), Width = viewWidth, Height = viewHeight
+        };
+
+        var win = new Window ();
+
+        win.Add (tv);
+
+        Toplevel top = new ();
+        top.Add (win);
+        RunState rs = Application.Begin (top);
+
+        Assert.Equal (new (0, 0, 80, 25), top.Frame);
+        Assert.Equal (new (0, 0, 80, 25), win.Frame);
+        Assert.Equal (new (68, 22, 10, 1), tv.Frame);
+        Application.End (rs);
+    }
+
+    // TODO: This actually a SetRelativeLayout/LayoutSubViews test and should be moved
+    // TODO: A new test that calls SetRelativeLayout directly is needed.
+    [Fact]
+    [AutoInitShutdown]
+    public void AnchorEnd_Equal_Inside_Window_With_MenuBar_And_StatusBar_On_Toplevel ()
+    {
+        var viewWidth = 10;
+        var viewHeight = 1;
+
+        var tv = new TextView
+        {
+            X = Pos.AnchorEnd (viewWidth), Y = Pos.AnchorEnd (viewHeight), Width = viewWidth, Height = viewHeight
+        };
+
+        var win = new Window ();
+
+        win.Add (tv);
+
+        var menu = new MenuBar ();
+        var status = new StatusBar ();
+        Toplevel top = new ();
+        top.Add (win, menu, status);
+        RunState rs = Application.Begin (top);
+
+        Assert.Equal (new (0, 0, 80, 25), top.Frame);
+        Assert.Equal (new (0, 0, 80, 1), menu.Frame);
+        Assert.Equal (new (0, 24, 80, 1), status.Frame);
+        Assert.Equal (new (0, 1, 80, 23), win.Frame);
+        Assert.Equal (new (68, 20, 10, 1), tv.Frame);
+
+        Application.End (rs);
+    }
+
 }