Skip to content

Commit

Permalink
Use safer approach for calculating the time span
Browse files Browse the repository at this point in the history
  • Loading branch information
IT-VBFK committed Nov 28, 2022
1 parent 35444f6 commit d2ef03e
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public static TimeSpan Ticks(this long ticks)
/// </summary>
internal static TimeSpan AddTicks(this TimeSpan timeSpan, int ticks)
{
return TimeSpan.FromTicks(timeSpan.Ticks + ticks);
return timeSpan + ticks.Ticks();
}

/// <summary>
Expand Down
4 changes: 2 additions & 2 deletions Src/FluentAssertions/Primitives/TimeOnlyAssertions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ public AndConstraint<TAssertions> Be(TimeOnly? expected, string because = "", pa
}

TimeOnly minimumValue = nearbyTime.Add(-precision);
TimeOnly maximumValue = nearbyTime.Add((precision == TimeSpan.MaxValue) ? precision : precision.AddTicks(1));
TimeOnly maximumValue = nearbyTime.Add(precision.AddTicks(1));

TimeSpan? difference = (Subject != null)
? MinimumDifference(Subject.Value, nearbyTime)
Expand Down Expand Up @@ -208,7 +208,7 @@ public AndConstraint<TAssertions> Be(TimeOnly? expected, string because = "", pa
}

TimeOnly minimumValue = distantTime.Add(-precision);
TimeOnly maximumValue = distantTime.Add((precision == TimeSpan.MaxValue) ? precision : precision.AddTicks(1));
TimeOnly maximumValue = distantTime.Add(precision.AddTicks(1));

Execute.Assertion
.BecauseOf(because, becauseArgs)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,8 +294,11 @@ public void A_precision_of_maximum_time_span_does_not_overflow()
TimeOnly time = new TimeOnly(23, 59, 0);
TimeOnly nearbyTime = new TimeOnly(0, 1, 0);

// Act / Assert
time.Should().BeCloseTo(nearbyTime, TimeSpan.MaxValue);
// Act
Actionn act = () => time.Should().BeCloseTo(nearbyTime, TimeSpan.MaxValue);

// Assert
act.Should().Throw<OverflowException>();
}

[Fact]
Expand Down Expand Up @@ -464,7 +467,7 @@ public void A_precision_of_maximum_time_span_does_not_overflow()
Action act = () => time.Should().NotBeCloseTo(nearbyTime, TimeSpan.MaxValue);

// Assert
act.Should().Throw<XunitException>();
act.Should().Throw<OverflowException>();
}

[Fact]
Expand Down

0 comments on commit d2ef03e

Please sign in to comment.