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
13 changes: 13 additions & 0 deletions Examples/.idea/.idea.Examples/.idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Examples/.idea/.idea.Examples/.idea/indexLayout.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,39 +1,44 @@
using AutoFixture.Xunit2;
using FluentAssertions;
using System;
using aweXpect;
using System.IO;
using System.Threading.Tasks;
using Testably.Abstractions.Testing;
using Xunit;

namespace Testably.Abstractions.Examples.AccessControlLists.Tests;

public class AccessControlListTests
{
#region Test Setup

public MockFileSystem FileSystem { get; }

public AccessControlListTests()
{
FileSystem = new MockFileSystem();
}

public MockFileSystem FileSystem { get; }
#endregion

[Theory]
[AutoData]
public void ReadAllText_DeniedPath_ShouldThrowIOException(string grantedPath, string deniedPath)
[InlineData("granted", "denied")]
public async Task ReadAllText_DeniedPath_ShouldThrowIOException(string grantedPath,
string deniedPath)
{
FileSystem.File.WriteAllText(grantedPath, "foo");
FileSystem.File.WriteAllText(deniedPath, "bar");
FileSystem.WithAccessControlStrategy(new CustomAccessControlStrategy(
path => path.Contains(grantedPath)));
FileSystem.WithAccessControlStrategy(
new CustomAccessControlStrategy(path => path.Contains(grantedPath)));

string result = FileSystem.File.ReadAllText(grantedPath);
result.Should().Be("foo");
await Expect.That(result).IsEqualTo("foo");

Exception? exception = Record.Exception(() =>
void Act()
{
FileSystem.File.ReadAllText(deniedPath);
});
exception.Should().BeOfType<IOException>()
.Which.Message.Should()
.Be($"Access to the path '{FileSystem.Path.GetFullPath(deniedPath)}' is denied.");
}

await Expect.That(Act).Throws<IOException>()
.WithMessage(
$"Access to the path '{FileSystem.Path.GetFullPath(deniedPath)}' is denied.");
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
using FluentAssertions;
using aweXpect;
using Microsoft.Extensions.DependencyInjection;
using System.IO.Abstractions;
using System.Threading.Tasks;
using Xunit;

namespace Testably.Abstractions.Examples.Configuration.DependencyInjection.Tests;

public class DependencyInjectionTests
{
[Fact]
public void
public async Task
DependencyInjection_Microsoft_ShouldAllowRegistrationAndCreationOfInstances()
{
ServiceProvider services = new ServiceCollection()
Expand All @@ -17,8 +18,8 @@ public void
.AddSingleton<ITimeSystem, RealTimeSystem>()
.BuildServiceProvider();

services.GetService<IFileSystem>().Should().BeOfType<RealFileSystem>();
services.GetService<IRandomSystem>().Should().BeOfType<RealRandomSystem>();
services.GetService<ITimeSystem>().Should().BeOfType<RealTimeSystem>();
await Expect.That(services.GetService<IFileSystem>()).Is<RealFileSystem>();
await Expect.That(services.GetService<IRandomSystem>()).Is<RealRandomSystem>();
await Expect.That(services.GetService<ITimeSystem>()).Is<RealTimeSystem>();
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
using AutoFixture.Xunit2;
using FluentAssertions;
using System;
using aweXpect;
using System.IO;
using System.IO.Abstractions;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using Testably.Abstractions.RandomSystem;
using Testably.Abstractions.Testing;
using Xunit;
Expand All @@ -18,18 +16,16 @@ public class InitializationTests
/// - a randomly named directory
/// </summary>
[Theory]
[AutoData]
public void InitializeFileSystemInSpecifiedCurrentDirectory(string currentDirectory)
[InlineData("foo")]
public async Task InitializeFileSystemInSpecifiedCurrentDirectory(string currentDirectory)
{
MockFileSystem fileSystem = new();
string expectedDirectory = fileSystem.Path.GetFullPath(currentDirectory);

fileSystem.InitializeIn(currentDirectory)
.WithASubdirectory();

fileSystem.Directory.GetCurrentDirectory()
.Should()
.Be(expectedDirectory);
await Expect.That(fileSystem.Directory.GetCurrentDirectory()).IsEqualTo(expectedDirectory);
}

/// <summary>
Expand All @@ -39,7 +35,7 @@ public void InitializeFileSystemInSpecifiedCurrentDirectory(string currentDirect
/// - a file named "bar.txt"
/// </summary>
[Fact]
public void InitializeFileSystemInTheRootDirectory()
public async Task InitializeFileSystemInTheRootDirectory()
{
MockFileSystem fileSystem = new();
fileSystem.InitializeIn("base-directory")
Expand All @@ -49,9 +45,9 @@ public void InitializeFileSystemInTheRootDirectory()
.WithAFile())
.WithFile("bar.txt");

fileSystem.File.Exists("bar.txt").Should().BeTrue();
fileSystem.Directory.Exists("foo").Should().BeTrue();
fileSystem.Directory.GetDirectories(".").Length.Should().Be(2);
await Expect.That(fileSystem.File.Exists("bar.txt")).IsTrue();
await Expect.That(fileSystem.Directory.Exists("foo")).IsTrue();
await Expect.That(fileSystem.Directory.GetDirectories(".")).HasCount(2);
}

/// <summary>
Expand All @@ -60,20 +56,20 @@ public void InitializeFileSystemInTheRootDirectory()
/// UNC servers (or additional drives under windows) can be added if required.
/// </summary>
[Fact]
public void InitializeFileSystemWithUncDrive()
public async Task InitializeFileSystemWithUncDrive()
{
MockFileSystem fileSystem = new();
var initialDriveCount = fileSystem.DriveInfo.GetDrives().Length;
int initialDriveCount = fileSystem.DriveInfo.GetDrives().Length;

fileSystem.WithUncDrive(@"//unc-server");

fileSystem.DriveInfo.GetDrives().Should().HaveCount(initialDriveCount);
await Expect.That(fileSystem.DriveInfo.GetDrives()).HasCount(initialDriveCount);
IDriveInfo drive = fileSystem.DriveInfo.New(@"//unc-server");
drive.IsReady.Should().BeTrue();
await Expect.That(drive.IsReady).IsTrue();

if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
fileSystem.DriveInfo.GetDrives().Should().HaveCount(initialDriveCount);
await Expect.That(fileSystem.DriveInfo.GetDrives()).HasCount(initialDriveCount);
}
}

Expand All @@ -84,7 +80,7 @@ public void InitializeFileSystemWithUncDrive()
/// <see cref="IOException" />, when the limit is breached.
/// </summary>
[Fact]
public void LimitAvailableSpaceOnDrives()
public async Task LimitAvailableSpaceOnDrives()
{
MockFileSystem fileSystem = new();
IRandom random = fileSystem.RandomSystem.Random.Shared;
Expand All @@ -96,16 +92,16 @@ public void LimitAvailableSpaceOnDrives()
// Limit the main drive to 200 bytes
fileSystem.WithDrive(drive => drive.SetTotalSize(200));
IDriveInfo mainDrive = fileSystem.GetDefaultDrive();
mainDrive.AvailableFreeSpace.Should().Be(200);
await Expect.That(mainDrive.AvailableFreeSpace).IsEqualTo(200);

fileSystem.File.WriteAllBytes("foo", firstFileContent);
mainDrive.AvailableFreeSpace.Should().Be(1);
await Expect.That(mainDrive.AvailableFreeSpace).IsEqualTo(1);

Exception? exception = Record.Exception(() =>
void Act()
{
fileSystem.File.WriteAllBytes("bar", secondFileContent);
});
}

exception.Should().BeOfType<IOException>();
await Expect.That(Act).Throws<IOException>();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using AutoFixture.Xunit2;
using FluentAssertions;
using aweXpect;
using System;
using System.Threading.Tasks;
using Testably.Abstractions.Testing;
using Xunit;

Expand All @@ -11,22 +11,22 @@ public class InterceptionTests
/// <summary>
/// Intercepting allows callbacks to be invoked before the change in the file system is performed.
/// </summary>
[Theory]
[AutoData]
public void Intercept(Exception customException)
[Fact]
public async Task Intercept()
{
Exception customException = new ApplicationException("bar");
MockFileSystem fileSystem = new();
fileSystem.Intercept.Creating(FileSystemTypes.File,
_ => throw customException);

fileSystem.Directory.CreateDirectory("foo");

Exception exception = Record.Exception(() =>
Exception? exception = Record.Exception(() =>
{
fileSystem.File.Create("foo/bar.txt");
});

exception.Should().Be(customException);
fileSystem.File.Exists("foo/bar.txt").Should().BeFalse();
await Expect.That(exception).IsSameAs(customException);
await Expect.That(fileSystem.File.Exists("foo/bar.txt")).IsFalse();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using FluentAssertions;
using aweXpect;
using System.Threading;
using System.Threading.Tasks;
using Testably.Abstractions.Testing;
using Xunit;

Expand All @@ -11,7 +12,7 @@ public class NotificationTests
/// Notifications allow reacting to an event after it occurred.
/// </summary>
[Fact]
public void Notify_ManualWait()
public async Task Notify_ManualWait()
{
ManualResetEventSlim ms = new();
bool isNotified = false;
Expand All @@ -26,16 +27,16 @@ public void Notify_ManualWait()
fileSystem.Directory.CreateDirectory("foo");
fileSystem.File.Create("foo/bar.txt");

ms.Wait();
fileSystem.File.Exists("foo/bar.txt").Should().BeTrue();
isNotified.Should().BeTrue();
ms.Wait(TestContext.Current.CancellationToken);
await Expect.That(fileSystem.File.Exists("foo/bar.txt")).IsTrue();
await Expect.That(isNotified).IsTrue();
}

/// <summary>
/// Notifications allow reacting to an event after it occurred.
/// </summary>
[Fact]
public void Notify_UseAwaitableCallback()
public async Task Notify_UseAwaitableCallback()
{
bool isNotified = false;
MockFileSystem fileSystem = new();
Expand All @@ -53,7 +54,7 @@ public void Notify_UseAwaitableCallback()
// If a timeout is provided, this will throw a TimeoutException if no event was triggered within 1000ms
.Wait(timeout: 1000);

fileSystem.File.Exists("foo/bar.txt").Should().BeTrue();
isNotified.Should().BeTrue();
await Expect.That(fileSystem.File.Exists("foo/bar.txt")).IsTrue();
await Expect.That(isNotified).IsTrue();
}
}
3 changes: 2 additions & 1 deletion Examples/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="AutoFixture.Xunit3" />
<PackageReference Include="aweXpect" />
<PackageReference Include="aweXpect.Testably" />
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="xunit.v3" />
<PackageReference Include="xunit.runner.visualstudio">
Expand Down
Loading
Loading