Explorar el Código

Add ToString implementation for SliderOption<T> (#3103)

* Add ToString implementation for SliderOption<T>

* Add test case for SliderOption ToString with more complex class
Thomas Nind hace 1 año
padre
commit
55bf53336a
Se han modificado 2 ficheros con 39 adiciones y 1 borrados
  1. 6 0
      Terminal.Gui/Views/Slider.cs
  2. 33 1
      UnitTests/Views/SliderTests.cs

+ 6 - 0
Terminal.Gui/Views/Slider.cs

@@ -86,6 +86,12 @@ public class SliderOption<T> {
 	/// 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>

+ 33 - 1
UnitTests/Views/SliderTests.cs

@@ -53,6 +53,38 @@ public class SliderOptionTests {
 		// 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 {
@@ -411,7 +443,7 @@ public class SliderTests {
 
 		// Assert
 		Assert.False (result);
-		Assert.NotEmpty (slider.GetSetOptions());
+		Assert.NotEmpty (slider.GetSetOptions ());
 	}
 
 	// Add more tests for different scenarios and edge cases.