Skip to content

Commit

Permalink
Implement Stopwatch.ToString() (#87812)
Browse files Browse the repository at this point in the history
* 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 <returns> tag is somewhat redundant since the <summary> tag
adequately describes what's going on here, but since the convention
is to have both tags, I have used the <returns> 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 #87812 (comment)

---------

Co-authored-by: Eric Mutta <ericm@creditregistry.com>
  • Loading branch information
ericmutta and Eric Mutta committed Jun 22, 2023
1 parent 426d18a commit b7b7279
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,14 @@ public void Restart()
_isRunning = true;
}

/// <summary>
/// Returns the <see cref="Elapsed"/> time as a string.
/// </summary>
/// <returns>
/// Elapsed time string in the same format used by <see cref="TimeSpan.ToString()"/>.
/// </returns>
public override string ToString() => Elapsed.ToString();

public bool IsRunning
{
get { return _isRunning; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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);
Expand All @@ -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);
}
Expand Down Expand Up @@ -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()
{
Expand Down
1 change: 1 addition & 0 deletions src/libraries/System.Runtime/ref/System.Runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down

0 comments on commit b7b7279

Please sign in to comment.