From a8f929bed929b1047e5ff379236b67a22cd1ebd2 Mon Sep 17 00:00:00 2001 From: Tig Date: Tue, 13 Aug 2024 14:12:32 -0600 Subject: [PATCH] Fixed DimAuto and tests --- Terminal.Gui/View/Layout/DimAuto.cs | 29 ++++++++++++------ UnitTests/View/Layout/Dim.AutoTests.cs | 42 ++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 10 deletions(-) diff --git a/Terminal.Gui/View/Layout/DimAuto.cs b/Terminal.Gui/View/Layout/DimAuto.cs index 6bd5f4d0f..05ac21d42 100644 --- a/Terminal.Gui/View/Layout/DimAuto.cs +++ b/Terminal.Gui/View/Layout/DimAuto.cs @@ -17,25 +17,30 @@ namespace Terminal.Gui; /// public class DimAuto : Dim { - private readonly Dim? _maximumContentDim; - - private readonly Dim? _minimumContentDim; - - private readonly DimAutoStyle _style; - - /// + /// 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.Default.Equals (MinimumContentDim, auto.MinimumContentDim) && + EqualityComparer.Default.Equals (MaximumContentDim, auto.MaximumContentDim) && + Style == auto.Style; } - /// - public override int GetHashCode () { return HashCode.Combine (MinimumContentDim, MaximumContentDim, Style); } + /// + public override int GetHashCode () + { + return HashCode.Combine (MinimumContentDim, MaximumContentDim, Style); + } + private readonly Dim? _maximumContentDim; /// /// 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; + /// /// Gets the minimum dimension the View's ContentSize will be constrained to. /// @@ -59,6 +66,8 @@ public class DimAuto : Dim init => _minimumContentDim = value; } + private readonly DimAutoStyle _style; + /// /// Gets the style of the DimAuto. /// diff --git a/UnitTests/View/Layout/Dim.AutoTests.cs b/UnitTests/View/Layout/Dim.AutoTests.cs index 401a6bb61..7ac704846 100644 --- a/UnitTests/View/Layout/Dim.AutoTests.cs +++ b/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]