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
16 changes: 15 additions & 1 deletion Source/Testably.Abstractions.Testing/MockFileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public MockFileSystem(Func<MockFileSystemOptions, MockFileSystemOptions> options
using IDisposable release = FileSystemRegistration.Ignore();
RandomSystem =
new MockRandomSystem(initialization.RandomProvider ?? RandomProvider.Default());
TimeSystem = new MockTimeSystem(TimeProvider.Now());
TimeSystem = initialization.TimeSystem ?? new MockTimeSystem(TimeProvider.Now());
_pathMock = new PathMock(this);
_storage = new InMemoryStorage(this);
ChangeHandler = new ChangeHandler(this);
Expand Down Expand Up @@ -275,6 +275,11 @@ public class MockFileSystemOptions
/// </summary>
internal IRandomProvider? RandomProvider { get; private set; }

/// <summary>
/// The <see cref="ITimeSystem" /> to use within the <see cref="MockFileSystem" />.
/// </summary>
internal ITimeSystem? TimeSystem { get; private set; }

/// <summary>
/// The simulated operating system.
/// </summary>
Expand Down Expand Up @@ -317,5 +322,14 @@ public MockFileSystemOptions UseRandomProvider(IRandomProvider randomProvider)
RandomProvider = randomProvider;
return this;
}

/// <summary>
/// Use the given <paramref name="timeSystem" /> within the <see cref="MockFileSystem" />.
/// </summary>
public MockFileSystemOptions UseTimeSystem(ITimeSystem timeSystem)
{
TimeSystem = timeSystem;
return this;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public async Task UseCurrentDirectory_WithPath_ShouldUsePathCurrentDirectory(str
[AutoData]
public async Task UseRandomProvider_ShouldUseFixedRandomValue(int fixedRandomValue)
{
MockFileSystem fileSystem = new(i => i
MockFileSystem fileSystem = new(options => options
.UseRandomProvider(RandomProvider.Generate(
intGenerator: new RandomProvider.Generator<int>(() => fixedRandomValue))));

Expand All @@ -139,6 +139,21 @@ public async Task UseRandomProvider_ShouldUseFixedRandomValue(int fixedRandomVal
await That(results).All().AreEqualTo(fixedRandomValue);
}

[Theory]
[AutoData]
public async Task UseTimeSystem_ShouldUseProvidedTimeSystem(int offsetSeconds)
{
DateTime simulatedNow = DateTime.Now.AddSeconds(offsetSeconds);
MockTimeSystem timeSystem = new(TimeProvider.Use(simulatedNow));
MockFileSystem fileSystem = new(options => options
.UseTimeSystem(timeSystem));

fileSystem.File.WriteAllText("foo", "some text");
DateTime result = fileSystem.File.GetCreationTime("foo");

await That(result).IsEqualTo(simulatedNow);
}

#region Helpers

#if CAN_SIMULATE_OTHER_OS
Expand Down
Loading