Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
5 changes: 5 additions & 0 deletions src/Exceptionless.DateTimeExtensions/TimeSpanExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ public static AgeSpan ToAgeSpan(this TimeSpan span)
{
return new AgeSpan(span);
}

public static TimeSpan SubtractSaturating(this TimeSpan self, TimeSpan other)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot add xml documentation

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added comprehensive XML documentation to the SubtractSaturating method following the project's documentation standards. The documentation includes a summary, parameter descriptions, and return value description. Commit: 91c1ff8

{
return self >= other ? self.Subtract(other) : TimeSpan.Zero;
}
}

public struct AgeSpan
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,43 @@ public void ApproximateAge()
Assert.Equal("2 days ago", DateTime.Now.AddHours(-48).ToApproximateAgeString());
Assert.Equal("1 week ago", DateTime.Now.AddDays(-7).ToApproximateAgeString());
}

[Fact]
public void SubtractSaturating()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot follow my best practices for test naming

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refactored the test to follow the MethodName_StateUnderTest_ExpectedBehavior naming convention as specified in the testing guidelines. Split the single monolithic test into 8 focused test methods, each testing a specific scenario with descriptive names and proper AAA structure. Commit: aef0ed9

{
// Basic case: normal subtraction when result is positive
var timeSpan1 = TimeSpan.FromHours(5);
var timeSpan2 = TimeSpan.FromHours(2);
Assert.Equal(TimeSpan.FromHours(3), timeSpan1.SubtractSaturating(timeSpan2));

// Saturating case: result would be negative, should return TimeSpan.Zero
var timeSpan3 = TimeSpan.FromHours(2);
var timeSpan4 = TimeSpan.FromHours(5);
Assert.Equal(TimeSpan.Zero, timeSpan3.SubtractSaturating(timeSpan4));

// Edge case: equal values should return TimeSpan.Zero
var timeSpan5 = TimeSpan.FromMinutes(30);
var timeSpan6 = TimeSpan.FromMinutes(30);
Assert.Equal(TimeSpan.Zero, timeSpan5.SubtractSaturating(timeSpan6));

// Zero cases
Assert.Equal(TimeSpan.FromSeconds(10), TimeSpan.FromSeconds(10).SubtractSaturating(TimeSpan.Zero));
Assert.Equal(TimeSpan.Zero, TimeSpan.Zero.SubtractSaturating(TimeSpan.FromSeconds(5)));
Assert.Equal(TimeSpan.Zero, TimeSpan.Zero.SubtractSaturating(TimeSpan.Zero));

// Large values
var largeTimeSpan1 = TimeSpan.FromDays(100);
var largeTimeSpan2 = TimeSpan.FromDays(50);
Assert.Equal(TimeSpan.FromDays(50), largeTimeSpan1.SubtractSaturating(largeTimeSpan2));

// Negative TimeSpan values - testing with negative self value
var negativeTimeSpan = TimeSpan.FromHours(-2);
var positiveTimeSpan = TimeSpan.FromHours(1);
Assert.Equal(TimeSpan.Zero, negativeTimeSpan.SubtractSaturating(positiveTimeSpan));

// Negative other value (subtracting negative is like adding)
var timeSpan7 = TimeSpan.FromHours(3);
var negativeTimeSpan2 = TimeSpan.FromHours(-2);
Assert.Equal(TimeSpan.FromHours(5), timeSpan7.SubtractSaturating(negativeTimeSpan2));
}
}