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();
watch.Stop();
Assert.False(watch.IsRunning);
+ Assert.Equal(watch.Elapsed.ToString(), watch.ToString());
var e1 = watch.Elapsed;
Sleep(s_defaultSleepTimeMs);
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);
}
}
}
+ [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()
{