Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,23 @@ public static string Render(TimeSpan? duration, bool wrapInParentheses = true, b
return string.Empty;
}

#if NET8_0_OR_GREATER
// Fast path for the common non-negative progress-frame case: duration < 1 hour with no milliseconds.
if (!showMilliseconds && duration.Value.Ticks >= 0 && duration.Value.Days == 0 && duration.Value.Hours == 0)
{
int seconds = duration.Value.Seconds;
int minutes = duration.Value.Minutes;

return (wrapInParentheses, minutes) switch
{
(true, 0) => $"({seconds}s)",
(true, _) => $"({minutes}m {seconds:D2}s)",
(false, 0) => $"{seconds}s",
_ => $"{minutes}m {seconds:D2}s",
};
}
#endif
Comment on lines +65 to +80

bool hasParentValue = false;

var stringBuilder = new StringBuilder();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using Microsoft.Testing.Platform.OutputDevice.Terminal;

namespace Microsoft.Testing.Platform.UnitTests;

[TestClass]
public sealed class HumanReadableDurationFormatterTests
{
[TestMethod]
public void Render_WhenDurationIsNull_ReturnsEmptyString()
=> Assert.AreEqual(string.Empty, HumanReadableDurationFormatter.Render(null));

[TestMethod]
[DataRow(0, true, "(0s)")]
[DataRow(5, true, "(5s)")]
[DataRow(65, true, "(1m 05s)")]
[DataRow(3599, true, "(59m 59s)")]
[DataRow(65, false, "1m 05s")]
Comment on lines +16 to +20
public void Render_WhenSubHourDurationDoesNotShowMilliseconds_FormatsDuration(int totalSeconds, bool wrapInParentheses, string expected)
=> Assert.AreEqual(expected, HumanReadableDurationFormatter.Render(TimeSpan.FromSeconds(totalSeconds), wrapInParentheses));

[TestMethod]
public void Render_WhenSubHourDurationShowsMilliseconds_FormatsDurationWithMilliseconds()
=> Assert.AreEqual("(1m 05s 123ms)", HumanReadableDurationFormatter.Render(TimeSpan.FromMilliseconds(65_123), showMilliseconds: true));

[TestMethod]
public void Render_WhenSubHourDurationIsNegative_PreservesExistingFormatting()
=> Assert.AreEqual("(0s)", HumanReadableDurationFormatter.Render(TimeSpan.FromMinutes(-1)));

[TestMethod]
public void Render_WhenDurationHasHours_FormatsDurationWithHours()
=> Assert.AreEqual("(1h 02m 03s)", HumanReadableDurationFormatter.Render(new TimeSpan(0, 1, 2, 3)));
}
Loading