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
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ internal static class ChangeDescriptionExtensions
/// <param name="changeDescription">The change description.</param>
/// <param name="execute">The execution engine simulation the underlying operating system.</param>
/// <param name="fileSystemType">The <see cref="ChangeDescription.FileSystemType" /> must have any of the provided flags.</param>
/// <param name="changeType">The <see cref="ChangeDescription.ChangeType" /> must match this type.</param>
/// <param name="changeType">The <see cref="ChangeDescription.ChangeType" /> must have any of the provided flags.</param>
/// <param name="globPattern">The <see cref="ChangeDescription.Path" /> must match this glob pattern.</param>
/// <param name="predicate">(optional) If provided, additional filter criteria can be implemented via this predicate.</param>
/// <returns>
Expand All @@ -31,8 +31,8 @@ internal static bool Matches(this ChangeDescription changeDescription,
string globPattern,
Func<ChangeDescription, bool>? predicate = null)
{
if (changeDescription.ChangeType != changeType ||
!changeDescription.FileSystemType.HasFlag(fileSystemType))
if (!changeType.HasFlag(changeDescription.ChangeType) ||
!fileSystemType.HasFlag(changeDescription.FileSystemType))
{
return false;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System.IO;
using Testably.Abstractions.Testing.FileSystem;
using Testably.Abstractions.Testing.Helpers;

namespace Testably.Abstractions.Testing.Tests.Helpers;

public class ChangeDescriptionExtensionsTests
{
[Test]
[Arguments(WatcherChangeTypes.Created, WatcherChangeTypes.Created, true)]
[Arguments(WatcherChangeTypes.Created, WatcherChangeTypes.Changed, false)]
[Arguments(WatcherChangeTypes.Created, WatcherChangeTypes.Created | WatcherChangeTypes.Deleted,
true)]
[Arguments(WatcherChangeTypes.Deleted, WatcherChangeTypes.Created | WatcherChangeTypes.Deleted,
true)]
[Arguments(WatcherChangeTypes.Changed, WatcherChangeTypes.Created | WatcherChangeTypes.Deleted,
false)]
public async Task Matches_ChangeType_ShouldSupportCombinedFlags(
WatcherChangeTypes changeType, WatcherChangeTypes filter, bool expectedResult)
{
MockFileSystem fileSystem = new();
ChangeDescription changeDescription =
CreateChangeDescription(fileSystem, changeType, FileSystemTypes.File);

bool result = changeDescription.Matches(
fileSystem.Execute, FileSystemTypes.DirectoryOrFile, filter, "*");

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

[Test]
[Arguments(FileSystemTypes.Directory, FileSystemTypes.Directory, true)]
[Arguments(FileSystemTypes.Directory, FileSystemTypes.File, false)]
[Arguments(FileSystemTypes.Directory, FileSystemTypes.DirectoryOrFile, true)]
[Arguments(FileSystemTypes.File, FileSystemTypes.Directory, false)]
[Arguments(FileSystemTypes.File, FileSystemTypes.File, true)]
[Arguments(FileSystemTypes.File, FileSystemTypes.DirectoryOrFile, true)]
public async Task Matches_FileSystemType_ShouldSupportCombinedFlags(
FileSystemTypes fileSystemType, FileSystemTypes filter, bool expectedResult)
{
MockFileSystem fileSystem = new();
ChangeDescription changeDescription =
CreateChangeDescription(fileSystem, WatcherChangeTypes.Created, fileSystemType);

bool result = changeDescription.Matches(
fileSystem.Execute, filter, WatcherChangeTypes.Created, "*");

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

private static ChangeDescription CreateChangeDescription(MockFileSystem fileSystem,
WatcherChangeTypes changeType, FileSystemTypes fileSystemType)
=> new(changeType, fileSystemType, NotifyFilters.FileName,
fileSystem.Storage.GetLocation("foo"), null);
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.Collections.Generic;

namespace Testably.Abstractions.Testing.Tests;

public class InterceptionHandlerExtensionsTests
Expand Down Expand Up @@ -216,6 +218,22 @@ public async Task Creating_Directory_ShouldUsePredicate(bool expectedResult, str
await That(isNotified).IsEqualTo(expectedResult);
}

[Test]
[AutoArguments]
public async Task Creating_DirectoryOrFile_ShouldInterceptDirectoriesAndFiles(
string directoryPath, string filePath)
{
List<FileSystemTypes> interceptedTypes = [];
FileSystem.Intercept.Creating(FileSystemTypes.DirectoryOrFile,
c => interceptedTypes.Add(c.FileSystemType));

FileSystem.Directory.CreateDirectory(directoryPath);
FileSystem.File.WriteAllText(filePath, null);

await That(interceptedTypes).Contains(FileSystemTypes.Directory);
await That(interceptedTypes).Contains(FileSystemTypes.File);
}

[Test]
[AutoArguments]
public async Task Creating_File_OtherEvent_ShouldNotTrigger(
Expand Down Expand Up @@ -423,6 +441,24 @@ public async Task Deleting_Directory_ShouldUsePredicate(bool expectedResult, str
await That(isNotified).IsEqualTo(expectedResult);
}

[Test]
[AutoArguments]
public async Task Deleting_DirectoryOrFile_ShouldInterceptDirectoriesAndFiles(
string directoryPath, string filePath)
{
List<FileSystemTypes> interceptedTypes = [];
FileSystem.Directory.CreateDirectory(directoryPath);
FileSystem.File.WriteAllText(filePath, null);
FileSystem.Intercept.Deleting(FileSystemTypes.DirectoryOrFile,
c => interceptedTypes.Add(c.FileSystemType));

FileSystem.Directory.Delete(directoryPath);
FileSystem.File.Delete(filePath);

await That(interceptedTypes).Contains(FileSystemTypes.Directory);
await That(interceptedTypes).Contains(FileSystemTypes.File);
}

[Test]
[AutoArguments]
public async Task Deleting_File_OtherEvent_ShouldNotTrigger(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Collections.Generic;
using Testably.Abstractions.Testing.FileSystem;

namespace Testably.Abstractions.Testing.Tests;
Expand Down Expand Up @@ -257,6 +258,25 @@ public async Task OnCreated_Directory_ShouldUsePredicate(bool expectedResult, st
await That(isNotified).IsEqualTo(expectedResult);
}

[Test]
[AutoArguments]
public async Task OnCreated_DirectoryOrFile_ShouldNotifyForDirectoriesAndFiles(
string directoryPath, string filePath)
{
List<FileSystemTypes> notifiedTypes = [];

using IAwaitableCallback<ChangeDescription> onCreated = FileSystem.Notify
.OnCreated(FileSystemTypes.DirectoryOrFile,
c => notifiedTypes.Add(c.FileSystemType));
FileSystem.Directory.CreateDirectory(directoryPath);
FileSystem.File.WriteAllText(filePath, null);

onCreated.Wait(count: 2);

await That(notifiedTypes).Contains(FileSystemTypes.Directory);
await That(notifiedTypes).Contains(FileSystemTypes.File);
}

[Test]
[AutoArguments]
public async Task OnCreated_File_OtherEvent_ShouldNotTrigger(string path)
Expand Down Expand Up @@ -507,6 +527,27 @@ public async Task OnDeleted_Directory_ShouldUsePredicate(bool expectedResult, st
await That(isNotified).IsEqualTo(expectedResult);
}

[Test]
[AutoArguments]
public async Task OnDeleted_DirectoryOrFile_ShouldNotifyForDirectoriesAndFiles(
string directoryPath, string filePath)
{
List<FileSystemTypes> notifiedTypes = [];
FileSystem.Directory.CreateDirectory(directoryPath);
FileSystem.File.WriteAllText(filePath, null);

using IAwaitableCallback<ChangeDescription> onDeleted = FileSystem.Notify
.OnDeleted(FileSystemTypes.DirectoryOrFile,
c => notifiedTypes.Add(c.FileSystemType));
FileSystem.Directory.Delete(directoryPath);
FileSystem.File.Delete(filePath);

onDeleted.Wait(count: 2);

await That(notifiedTypes).Contains(FileSystemTypes.Directory);
await That(notifiedTypes).Contains(FileSystemTypes.File);
}

[Test]
[AutoArguments]
public async Task OnDeleted_File_OtherEvent_ShouldNotTrigger(string path)
Expand Down
Loading