Skip to content
Merged
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
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.Utc.
/// </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.Utc)
: now;

_description = description;
}

Expand Down
18 changes: 17 additions & 1 deletion 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 Expand Up @@ -102,7 +118,7 @@ public async Task ToString_WithFixedContainer_ShouldContainTimeProvider()
string result = timeSystem.ToString();

await That(result).Contains("Fixed");
await That(result).Contains($"{now.ToUniversalTime()}Z");
await That(result).Contains($"{now}Z");
}

[Fact]
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_ShouldConvertToUtcDateTime()
{
DateTime unspecifiedTime = TimeTestHelper.GetRandomTime(DateTimeKind.Unspecified);
ITimeProvider timeProvider = TimeProvider.Use(unspecifiedTime);
DateTime result = timeProvider.Read();
await That(result).IsEqualTo(DateTime.SpecifyKind(unspecifiedTime, DateTimeKind.Utc));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public async Task OnDateTimeRead_MultipleCallbacks_ShouldAllBeCalled()
[Fact]
public async Task OnDateTimeRead_Today_ShouldExecuteCallbackWithCorrectParameter()
{
DateTime expectedTime = TimeTestHelper.GetRandomTime().Date;
DateTime expectedTime = TimeTestHelper.GetRandomTime(DateTimeKind.Local).Date;
MockTimeSystem timeSystem = new(expectedTime);
DateTime? receivedTime = null;

Expand Down
Loading