Quellcode durchsuchen

Fixed DimAuto and tests

Tig vor 11 Monaten
Ursprung
Commit
a8f929bed9
2 geänderte Dateien mit 61 neuen und 10 gelöschten Zeilen
  1. 19 10
      Terminal.Gui/View/Layout/DimAuto.cs
  2. 42 0
      UnitTests/View/Layout/Dim.AutoTests.cs

+ 19 - 10
Terminal.Gui/View/Layout/DimAuto.cs

@@ -17,25 +17,30 @@ namespace Terminal.Gui;
 /// </remarks>
 public class DimAuto : Dim
 {
-    private readonly Dim? _maximumContentDim;
-
-    private readonly Dim? _minimumContentDim;
-
-    private readonly DimAutoStyle _style;
-
-    /// <inheritdoc/>
+    /// <inheritdoc />
     public override bool Equals (object? other)
     {
+        if (ReferenceEquals (this, other))
+        {
+            return true;
+        }
+
         if (other is not DimAuto auto)
         {
             return false;
         }
 
-        return auto.MinimumContentDim == MinimumContentDim && auto.MaximumContentDim == MaximumContentDim && auto.Style == Style;
+        return EqualityComparer<Dim?>.Default.Equals (MinimumContentDim, auto.MinimumContentDim) &&
+               EqualityComparer<Dim?>.Default.Equals (MaximumContentDim, auto.MaximumContentDim) &&
+               Style == auto.Style;
     }
 
-    /// <inheritdoc/>
-    public override int GetHashCode () { return HashCode.Combine (MinimumContentDim, MaximumContentDim, Style); }
+    /// <inheritdoc />
+    public override int GetHashCode ()
+    {
+        return HashCode.Combine (MinimumContentDim, MaximumContentDim, Style);
+    }
+    private readonly Dim? _maximumContentDim;
 
     /// <summary>
     ///     Gets the maximum dimension the View's ContentSize will be fit to. NOT CURRENTLY SUPPORTED.
@@ -48,6 +53,8 @@ public class DimAuto : Dim
         init => _maximumContentDim = value;
     }
 
+    private readonly Dim? _minimumContentDim;
+
     /// <summary>
     ///     Gets the minimum dimension the View's ContentSize will be constrained to.
     /// </summary>
@@ -59,6 +66,8 @@ public class DimAuto : Dim
         init => _minimumContentDim = value;
     }
 
+    private readonly DimAutoStyle _style;
+
     /// <summary>
     ///     Gets the style of the DimAuto.
     /// </summary>

+ 42 - 0
UnitTests/View/Layout/Dim.AutoTests.cs

@@ -216,8 +216,50 @@ public partial class DimAutoTests (ITestOutputHelper output)
             MinimumContentDim = 1,
             Style = DimAutoStyle.Auto
         };
+
+        var c = new DimAuto
+        {
+            MaximumContentDim = 2,
+            MinimumContentDim = 1,
+            Style = DimAutoStyle.Auto
+        };
+
+        var d = new DimAuto
+        {
+            MaximumContentDim = null,
+            MinimumContentDim = 1,
+            Style = DimAutoStyle.Content
+        };
+
+        var e = new DimAuto
+        {
+            MaximumContentDim = null,
+            MinimumContentDim = 2,
+            Style = DimAutoStyle.Auto
+        };
+
+        // Test equality with same values
         Assert.True (a.Equals (b));
         Assert.True (a.GetHashCode () == b.GetHashCode ());
+
+        // Test inequality with different MaximumContentDim
+        Assert.False (a.Equals (c));
+        Assert.False (a.GetHashCode () == c.GetHashCode ());
+
+        // Test inequality with different Style
+        Assert.False (a.Equals (d));
+        Assert.False (a.GetHashCode () == d.GetHashCode ());
+
+        // Test inequality with different MinimumContentDim
+        Assert.False (a.Equals (e));
+        Assert.False (a.GetHashCode () == e.GetHashCode ());
+
+        // Test equality with self
+        Assert.True (a.Equals (a));
+        Assert.True (a.GetHashCode () == a.GetHashCode ());
+
+        // Test inequality with null
+        Assert.False (a.Equals (null));
     }
 
     [Fact]