Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 3 additions & 0 deletions Source/Testably.Abstractions.Testing/TimeProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ public static ITimeProvider Random()
/// <summary>
/// Initializes the <see cref="MockTimeSystem.TimeProvider" /> with the specified <paramref name="time" />.
/// </summary>
/// <remarks>
/// If the <paramref name="time" /> has Kind DateTimeKind.Unspecified it will be treated as if it had Kind DateTimeKind.Local.
/// </remarks>
public static ITimeProvider Use(DateTime time)
{
return new TimeProviderMock(time, "Fixed");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ internal sealed class TimeProviderMock : ITimeProvider

public TimeProviderMock(DateTime now, string description)
{
_now = now;
_now = now.Kind == DateTimeKind.Unspecified
? DateTime.SpecifyKind(now, DateTimeKind.Local)
: now;

_description = description;
}

Expand Down
16 changes: 16 additions & 0 deletions Tests/Testably.Abstractions.Testing.Tests/MockTimeSystemTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,22 @@ async Task Act()
await That(Act).ThrowsExactly<ArgumentOutOfRangeException>().WithParamName("delay");
}

[Theory]
[InlineData(DateTimeKind.Local)]
[InlineData(DateTimeKind.Unspecified)]
[InlineData(DateTimeKind.Utc)]
public async Task DifferenceBetweenDateTimeNowAndDateTimeUtcNow_ShouldBeLocalTimeZoneOffsetFromUtc(DateTimeKind dateTimeKind)
{
DateTime now = TimeTestHelper.GetRandomTime(DateTimeKind.Local);

var expectedDifference = TimeZoneInfo.Local.GetUtcOffset(now);

MockTimeSystem timeSystem = new(DateTime.SpecifyKind(now, dateTimeKind));
var actualDifference = timeSystem.DateTime.Now - timeSystem.DateTime.UtcNow;

await That(actualDifference).IsEqualTo(expectedDifference);
}

[Fact]
public async Task Sleep_Infinite_ShouldNotThrowException()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,13 @@ public async Task Use_ShouldReturnFixedDateTime()
await That(result1).IsEqualTo(now);
await That(result2).IsEqualTo(now);
}

[Fact]
public async Task Use_UnspecifiedKind_ShouldConvertToLocalDateTime()
{
DateTime unspecifiedTime = TimeTestHelper.GetRandomTime(DateTimeKind.Unspecified);
ITimeProvider timeProvider = TimeProvider.Use(unspecifiedTime);
DateTime result1 = timeProvider.Read();
await That(result1).IsEqualTo(DateTime.SpecifyKind(unspecifiedTime, DateTimeKind.Local));
}
}
Loading