Skip to content

Commit

Permalink
Add ToString implementation for SliderOption<T> (#3103)
Browse files Browse the repository at this point in the history
* Add ToString implementation for SliderOption<T>

* Add test case for SliderOption ToString with more complex class
  • Loading branch information
tznind authored Dec 31, 2023
1 parent 80ef4b5 commit 55bf533
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
6 changes: 6 additions & 0 deletions Terminal.Gui/Views/Slider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ internal void OnChanged (bool isSet)
/// Event fired when the an option has changed.
/// </summary>
public event EventHandler<SliderOptionEventArgs> Changed;

/// <summary>
/// Creates a human-readable string that represents this <see cref="SliderOption{T}"/>.
/// </summary>
public override string ToString () => "{Legend=" + Legend + ", LegendAbbr=" + LegendAbbr.ToString () + ", Data=" + Data?.ToString () + "}";

}

/// <summary>
Expand Down
34 changes: 33 additions & 1 deletion UnitTests/Views/SliderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,38 @@ public void OnChanged_Should_Raise_ChangedEvent ()
// Assert
Assert.True (eventRaised);
}

[Fact]
public void SliderOption_ToString_WhenEmpty ()
{
var sliderOption = new SliderOption<object> ();
Assert.Equal ("{Legend=, LegendAbbr=\0, Data=}", sliderOption.ToString ());
}

[Fact]
public void SliderOption_ToString_WhenPopulated_WithInt ()
{
var sliderOption = new SliderOption<int> {
Legend = "Lord flibble",
LegendAbbr = new Rune ('l'),
Data = 1,
};

Assert.Equal ("{Legend=Lord flibble, LegendAbbr=l, Data=1}", sliderOption.ToString ());
}


[Fact]
public void SliderOption_ToString_WhenPopulated_WithSizeF ()
{
var sliderOption = new SliderOption<SizeF> {
Legend = "Lord flibble",
LegendAbbr = new Rune ('l'),
Data = new SizeF(32,11),
};

Assert.Equal ("{Legend=Lord flibble, LegendAbbr=l, Data={Width=32, Height=11}}", sliderOption.ToString ());
}
}

public class SliderEventArgsTests {
Expand Down Expand Up @@ -411,7 +443,7 @@ public void Set_Should_Not_UnSetFocusedOption_When_EmptyNotAllowed ()

// Assert
Assert.False (result);
Assert.NotEmpty (slider.GetSetOptions());
Assert.NotEmpty (slider.GetSetOptions ());
}

// Add more tests for different scenarios and edge cases.
Expand Down

0 comments on commit 55bf533

Please sign in to comment.