From b7b72799e0b6878261f10db8cb3333de553805f2 Mon Sep 17 00:00:00 2001 From: Eric Mutta Date: Thu, 22 Jun 2023 14:52:48 +0300 Subject: [PATCH] Implement Stopwatch.ToString() (#87812) * Implement Stopwatch.ToString() For the tests, I added a few one-liners to the existing tests where the Stopwatch.Elapsed property was easy to check. I also added the OverridesToString() test to check for cases where the ToString() method is called implicitly. This confirms that people can write "Console.WriteLine(sw)" where "sw" is an instance of the Stopwatch class, and get the elapsed time printed. Fix #87449 * Fix typo in Stopwatch.cs Fix typo in the comment within the OverridesToString() test. * Add XML docs for the ToString() method. The tag is somewhat redundant since the tag adequately describes what's going on here, but since the convention is to have both tags, I have used the tag to clarify that the returned string renders the elapsed time using the same format as TimeSpan.ToString(). * Add ToString() override for Stopwatch class. See https://github.com/dotnet/runtime/pull/87812#discussion_r1235940302 --------- Co-authored-by: Eric Mutta --- .../src/System/Diagnostics/Stopwatch.cs | 8 +++++++ .../tests/System/Diagnostics/Stopwatch.cs | 22 +++++++++++++++++++ .../System.Runtime/ref/System.Runtime.cs | 1 + 3 files changed, 31 insertions(+) diff --git a/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Stopwatch.cs b/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Stopwatch.cs index d6212cd0bcf78..8c27526555092 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Stopwatch.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Diagnostics/Stopwatch.cs @@ -89,6 +89,14 @@ public void Restart() _isRunning = true; } + /// + /// Returns the time as a string. + /// + /// + /// Elapsed time string in the same format used by . + /// + public override string ToString() => Elapsed.ToString(); + public bool IsRunning { get { return _isRunning; } diff --git a/src/libraries/System.Runtime.Extensions/tests/System/Diagnostics/Stopwatch.cs b/src/libraries/System.Runtime.Extensions/tests/System/Diagnostics/Stopwatch.cs index 3ddb805e35552..d50906d17a27f 100644 --- a/src/libraries/System.Runtime.Extensions/tests/System/Diagnostics/Stopwatch.cs +++ b/src/libraries/System.Runtime.Extensions/tests/System/Diagnostics/Stopwatch.cs @@ -29,6 +29,7 @@ public static void ConstructStartAndStop() Stopwatch watch = new Stopwatch(); Assert.False(watch.IsRunning); Assert.Equal(TimeSpan.Zero, watch.Elapsed); + Assert.Equal(TimeSpan.Zero.ToString(), watch.ToString()); Assert.Equal(0, watch.ElapsedTicks); Assert.Equal(0, watch.ElapsedMilliseconds); watch.Start(); @@ -38,6 +39,7 @@ public static void ConstructStartAndStop() watch.Stop(); Assert.False(watch.IsRunning); + Assert.Equal(watch.Elapsed.ToString(), watch.ToString()); var e1 = watch.Elapsed; Sleep(s_defaultSleepTimeMs); @@ -64,6 +66,7 @@ public static void StartNewAndReset() watch.Reset(); Assert.False(watch.IsRunning); Assert.Equal(TimeSpan.Zero, watch.Elapsed); + Assert.Equal(TimeSpan.Zero.ToString(), watch.ToString()); Assert.Equal(0, watch.ElapsedTicks); Assert.Equal(0, watch.ElapsedMilliseconds); } @@ -96,6 +99,25 @@ public static void StartNewAndRestart() } } + [Fact] + public static void OverridesToString() + { + // In this test we use string interpolation with a Stopwatch instance to trigger + // a call to the overridden ToString() method which should return the elapsed time + // as a string. + + Stopwatch watch = new Stopwatch(); + Assert.Equal(TimeSpan.Zero, watch.Elapsed); + Assert.Equal($"Elapsed = {watch.Elapsed}", $"Elapsed = {watch}"); + + watch.Start(); + Sleep(s_defaultSleepTimeMs); + watch.Stop(); + + Assert.True(watch.Elapsed > TimeSpan.Zero); + Assert.Equal($"Elapsed = {watch.Elapsed}", $"Elapsed = {watch}"); + } + [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsDebuggerTypeProxyAttributeSupported))] public static void DebuggerAttributesValid() { diff --git a/src/libraries/System.Runtime/ref/System.Runtime.cs b/src/libraries/System.Runtime/ref/System.Runtime.cs index 09c4ad4dc6c0d..d0b807ee15d0a 100644 --- a/src/libraries/System.Runtime/ref/System.Runtime.cs +++ b/src/libraries/System.Runtime/ref/System.Runtime.cs @@ -8197,6 +8197,7 @@ public void Restart() { } public void Start() { } public static System.Diagnostics.Stopwatch StartNew() { throw null; } public void Stop() { } + public override string ToString() { throw null; } } public sealed partial class UnreachableException : System.Exception {