From d88cb70b4be8196129e4535e3fc31e2a73717525 Mon Sep 17 00:00:00 2001 From: Philippe Matray Date: Sat, 11 Mar 2023 20:55:50 +0100 Subject: [PATCH] Add some unit tests --- ...tDirectories(Func_IDirectoryNode,bool_).md | 21 +++++++ docs/api/IVirtualFileSystem.md | 1 + ...tDirectories(Func_IDirectoryNode,bool_).md | 23 ++++++++ docs/api/VFS.md | 1 + docs/api/VirtualFileSystem.md | 2 + docs/links | 2 + .../Abstractions/VFSPath.cs | 3 - .../Contracts/IVirtualFileSystem.cs | 8 +++ .../VFS.Commands.cs | 16 +----- .../VFS.Queries.cs | 4 +- .../Models/VirtualFileSystemTests.cs | 55 +++++++++++++++++++ .../ValueObjects/VFSPathTests.cs | 51 +++++++++++++++++ 12 files changed, 168 insertions(+), 19 deletions(-) create mode 100644 docs/api/IVirtualFileSystem.SelectDirectories(Func_IDirectoryNode,bool_).md create mode 100644 docs/api/VFS.SelectDirectories(Func_IDirectoryNode,bool_).md create mode 100644 tests/Atypical.VirtualFileSystem.UnitTests/ValueObjects/VFSPathTests.cs diff --git a/docs/api/IVirtualFileSystem.SelectDirectories(Func_IDirectoryNode,bool_).md b/docs/api/IVirtualFileSystem.SelectDirectories(Func_IDirectoryNode,bool_).md new file mode 100644 index 0000000..33bb071 --- /dev/null +++ b/docs/api/IVirtualFileSystem.SelectDirectories(Func_IDirectoryNode,bool_).md @@ -0,0 +1,21 @@ +#### [Atypical.VirtualFileSystem.Core](VirtualFileSystem.md 'VirtualFileSystem') +### [Atypical.VirtualFileSystem.Core.Contracts](VirtualFileSystem.md#Atypical.VirtualFileSystem.Core.Contracts 'Atypical.VirtualFileSystem.Core.Contracts').[IVirtualFileSystem](IVirtualFileSystem.md 'Atypical.VirtualFileSystem.Core.Contracts.IVirtualFileSystem') + +## IVirtualFileSystem.SelectDirectories(Func) Method + +Finds all directory nodes that match the specified predicate. + +```csharp +System.Collections.Generic.IEnumerable SelectDirectories(System.Func predicate); +``` +#### Parameters + + + +`predicate` [System.Func<](https://docs.microsoft.com/en-us/dotnet/api/System.Func-2 'System.Func`2')[IDirectoryNode](IDirectoryNode.md 'Atypical.VirtualFileSystem.Core.Contracts.IDirectoryNode')[,](https://docs.microsoft.com/en-us/dotnet/api/System.Func-2 'System.Func`2')[System.Boolean](https://docs.microsoft.com/en-us/dotnet/api/System.Boolean 'System.Boolean')[>](https://docs.microsoft.com/en-us/dotnet/api/System.Func-2 'System.Func`2') + +The predicate. + +#### Returns +[System.Collections.Generic.IEnumerable<](https://docs.microsoft.com/en-us/dotnet/api/System.Collections.Generic.IEnumerable-1 'System.Collections.Generic.IEnumerable`1')[IDirectoryNode](IDirectoryNode.md 'Atypical.VirtualFileSystem.Core.Contracts.IDirectoryNode')[>](https://docs.microsoft.com/en-us/dotnet/api/System.Collections.Generic.IEnumerable-1 'System.Collections.Generic.IEnumerable`1') +The directory nodes. \ No newline at end of file diff --git a/docs/api/IVirtualFileSystem.md b/docs/api/IVirtualFileSystem.md index 6cb4d91..72e1c39 100644 --- a/docs/api/IVirtualFileSystem.md +++ b/docs/api/IVirtualFileSystem.md @@ -39,6 +39,7 @@ Derived | [GetFile(string)](IVirtualFileSystem.GetFile(string).md 'Atypical.VirtualFileSystem.Core.Contracts.IVirtualFileSystem.GetFile(string)') | Gets a file node by its path.
The path must be absolute. | | [GetRootPath()](IVirtualFileSystem.GetRootPath().md 'Atypical.VirtualFileSystem.Core.Contracts.IVirtualFileSystem.GetRootPath()') | Gets the path of the root directory. | | [IsEmpty()](IVirtualFileSystem.IsEmpty().md 'Atypical.VirtualFileSystem.Core.Contracts.IVirtualFileSystem.IsEmpty()') | Indicates whether the file system is empty.
This is the case if the root directory is empty. | +| [SelectDirectories(Func<IDirectoryNode,bool>)](IVirtualFileSystem.SelectDirectories(Func_IDirectoryNode,bool_).md 'Atypical.VirtualFileSystem.Core.Contracts.IVirtualFileSystem.SelectDirectories(System.Func)') | Finds all directory nodes that match the specified predicate. | | [TryGetDirectory(VFSDirectoryPath, IDirectoryNode)](IVirtualFileSystem.TryGetDirectory(VFSDirectoryPath,IDirectoryNode).md 'Atypical.VirtualFileSystem.Core.Contracts.IVirtualFileSystem.TryGetDirectory(Atypical.VirtualFileSystem.Core.ValueObjects.VFSDirectoryPath, Atypical.VirtualFileSystem.Core.Contracts.IDirectoryNode)') | Try to get a directory node by its path.
The path must be absolute.
If the directory node does not exist, this method returns `false`
and [directory](IVirtualFileSystem.TryGetDirectory(VFSDirectoryPath,IDirectoryNode).md#Atypical.VirtualFileSystem.Core.Contracts.IVirtualFileSystem.TryGetDirectory(Atypical.VirtualFileSystem.Core.ValueObjects.VFSDirectoryPath,Atypical.VirtualFileSystem.Core.Contracts.IDirectoryNode).directory 'Atypical.VirtualFileSystem.Core.Contracts.IVirtualFileSystem.TryGetDirectory(Atypical.VirtualFileSystem.Core.ValueObjects.VFSDirectoryPath, Atypical.VirtualFileSystem.Core.Contracts.IDirectoryNode).directory') is set to `null`. | | [TryGetDirectory(string, IDirectoryNode)](IVirtualFileSystem.TryGetDirectory(string,IDirectoryNode).md 'Atypical.VirtualFileSystem.Core.Contracts.IVirtualFileSystem.TryGetDirectory(string, Atypical.VirtualFileSystem.Core.Contracts.IDirectoryNode)') | Try to get a directory node by its path.
The path must be absolute.
If the directory node does not exist, this method returns `false`
and [directory](IVirtualFileSystem.TryGetDirectory(string,IDirectoryNode).md#Atypical.VirtualFileSystem.Core.Contracts.IVirtualFileSystem.TryGetDirectory(string,Atypical.VirtualFileSystem.Core.Contracts.IDirectoryNode).directory 'Atypical.VirtualFileSystem.Core.Contracts.IVirtualFileSystem.TryGetDirectory(string, Atypical.VirtualFileSystem.Core.Contracts.IDirectoryNode).directory') is set to `null`. | | [TryGetFile(VFSFilePath, IFileNode)](IVirtualFileSystem.TryGetFile(VFSFilePath,IFileNode).md 'Atypical.VirtualFileSystem.Core.Contracts.IVirtualFileSystem.TryGetFile(Atypical.VirtualFileSystem.Core.ValueObjects.VFSFilePath, Atypical.VirtualFileSystem.Core.Contracts.IFileNode)') | Try to get a file node by its path.
The path must be absolute. | diff --git a/docs/api/VFS.SelectDirectories(Func_IDirectoryNode,bool_).md b/docs/api/VFS.SelectDirectories(Func_IDirectoryNode,bool_).md new file mode 100644 index 0000000..f39f61a --- /dev/null +++ b/docs/api/VFS.SelectDirectories(Func_IDirectoryNode,bool_).md @@ -0,0 +1,23 @@ +#### [Atypical.VirtualFileSystem.Core](VirtualFileSystem.md 'VirtualFileSystem') +### [Atypical.VirtualFileSystem.Core](VirtualFileSystem.md#Atypical.VirtualFileSystem.Core 'Atypical.VirtualFileSystem.Core').[VFS](VFS.md 'Atypical.VirtualFileSystem.Core.VFS') + +## VFS.SelectDirectories(Func) Method + +Finds all directory nodes that match the specified predicate. + +```csharp +public System.Collections.Generic.IEnumerable SelectDirectories(System.Func predicate); +``` +#### Parameters + + + +`predicate` [System.Func<](https://docs.microsoft.com/en-us/dotnet/api/System.Func-2 'System.Func`2')[IDirectoryNode](IDirectoryNode.md 'Atypical.VirtualFileSystem.Core.Contracts.IDirectoryNode')[,](https://docs.microsoft.com/en-us/dotnet/api/System.Func-2 'System.Func`2')[System.Boolean](https://docs.microsoft.com/en-us/dotnet/api/System.Boolean 'System.Boolean')[>](https://docs.microsoft.com/en-us/dotnet/api/System.Func-2 'System.Func`2') + +The predicate. + +Implements [SelectDirectories(Func<IDirectoryNode,bool>)](IVirtualFileSystem.SelectDirectories(Func_IDirectoryNode,bool_).md 'Atypical.VirtualFileSystem.Core.Contracts.IVirtualFileSystem.SelectDirectories(System.Func)') + +#### Returns +[System.Collections.Generic.IEnumerable<](https://docs.microsoft.com/en-us/dotnet/api/System.Collections.Generic.IEnumerable-1 'System.Collections.Generic.IEnumerable`1')[IDirectoryNode](IDirectoryNode.md 'Atypical.VirtualFileSystem.Core.Contracts.IDirectoryNode')[>](https://docs.microsoft.com/en-us/dotnet/api/System.Collections.Generic.IEnumerable-1 'System.Collections.Generic.IEnumerable`1') +The directory nodes. \ No newline at end of file diff --git a/docs/api/VFS.md b/docs/api/VFS.md index b2d61d7..e39628a 100644 --- a/docs/api/VFS.md +++ b/docs/api/VFS.md @@ -45,6 +45,7 @@ Implements [IVirtualFileSystem](IVirtualFileSystem.md 'Atypical.VirtualFileSyste | [GetFile(string)](VFS.GetFile(string).md 'Atypical.VirtualFileSystem.Core.VFS.GetFile(string)') | Gets a file node by its path.
The path must be absolute. | | [GetRootPath()](VFS.GetRootPath().md 'Atypical.VirtualFileSystem.Core.VFS.GetRootPath()') | Gets the path of the root directory. | | [IsEmpty()](VFS.IsEmpty().md 'Atypical.VirtualFileSystem.Core.VFS.IsEmpty()') | Indicates whether the file system is empty.
This is the case if the root directory is empty. | +| [SelectDirectories(Func<IDirectoryNode,bool>)](VFS.SelectDirectories(Func_IDirectoryNode,bool_).md 'Atypical.VirtualFileSystem.Core.VFS.SelectDirectories(System.Func)') | Finds all directory nodes that match the specified predicate. | | [ToString()](VFS.ToString().md 'Atypical.VirtualFileSystem.Core.VFS.ToString()') | Returns the index as an ASCII tree. | | [TryGetDirectory(VFSDirectoryPath, IDirectoryNode)](VFS.TryGetDirectory(VFSDirectoryPath,IDirectoryNode).md 'Atypical.VirtualFileSystem.Core.VFS.TryGetDirectory(Atypical.VirtualFileSystem.Core.ValueObjects.VFSDirectoryPath, Atypical.VirtualFileSystem.Core.Contracts.IDirectoryNode)') | Try to get a directory node by its path.
The path must be absolute.
If the directory node does not exist, this method returns `false`
and [directory](VFS.TryGetDirectory(VFSDirectoryPath,IDirectoryNode).md#Atypical.VirtualFileSystem.Core.VFS.TryGetDirectory(Atypical.VirtualFileSystem.Core.ValueObjects.VFSDirectoryPath,Atypical.VirtualFileSystem.Core.Contracts.IDirectoryNode).directory 'Atypical.VirtualFileSystem.Core.VFS.TryGetDirectory(Atypical.VirtualFileSystem.Core.ValueObjects.VFSDirectoryPath, Atypical.VirtualFileSystem.Core.Contracts.IDirectoryNode).directory') is set to `null`. | | [TryGetDirectory(string, IDirectoryNode)](VFS.TryGetDirectory(string,IDirectoryNode).md 'Atypical.VirtualFileSystem.Core.VFS.TryGetDirectory(string, Atypical.VirtualFileSystem.Core.Contracts.IDirectoryNode)') | Try to get a directory node by its path.
The path must be absolute.
If the directory node does not exist, this method returns `false`
and [directory](VFS.TryGetDirectory(string,IDirectoryNode).md#Atypical.VirtualFileSystem.Core.VFS.TryGetDirectory(string,Atypical.VirtualFileSystem.Core.Contracts.IDirectoryNode).directory 'Atypical.VirtualFileSystem.Core.VFS.TryGetDirectory(string, Atypical.VirtualFileSystem.Core.Contracts.IDirectoryNode).directory') is set to `null`. | diff --git a/docs/api/VirtualFileSystem.md b/docs/api/VirtualFileSystem.md index a653b3f..d16aac9 100644 --- a/docs/api/VirtualFileSystem.md +++ b/docs/api/VirtualFileSystem.md @@ -46,6 +46,7 @@ - **[GetRootPath()](VFS.GetRootPath().md 'Atypical.VirtualFileSystem.Core.VFS.GetRootPath()')** `Method` Gets the path of the root directory. - **[IsEmpty()](VFS.IsEmpty().md 'Atypical.VirtualFileSystem.Core.VFS.IsEmpty()')** `Method` Indicates whether the file system is empty. This is the case if the root directory is empty. + - **[SelectDirectories(Func<IDirectoryNode,bool>)](VFS.SelectDirectories(Func_IDirectoryNode,bool_).md 'Atypical.VirtualFileSystem.Core.VFS.SelectDirectories(System.Func)')** `Method` Finds all directory nodes that match the specified predicate. - **[ToString()](VFS.ToString().md 'Atypical.VirtualFileSystem.Core.VFS.ToString()')** `Method` Returns the index as an ASCII tree. - **[TryGetDirectory(VFSDirectoryPath, IDirectoryNode)](VFS.TryGetDirectory(VFSDirectoryPath,IDirectoryNode).md 'Atypical.VirtualFileSystem.Core.VFS.TryGetDirectory(Atypical.VirtualFileSystem.Core.ValueObjects.VFSDirectoryPath, Atypical.VirtualFileSystem.Core.Contracts.IDirectoryNode)')** `Method` Try to get a directory node by its path. The path must be absolute. @@ -175,6 +176,7 @@ - **[GetRootPath()](IVirtualFileSystem.GetRootPath().md 'Atypical.VirtualFileSystem.Core.Contracts.IVirtualFileSystem.GetRootPath()')** `Method` Gets the path of the root directory. - **[IsEmpty()](IVirtualFileSystem.IsEmpty().md 'Atypical.VirtualFileSystem.Core.Contracts.IVirtualFileSystem.IsEmpty()')** `Method` Indicates whether the file system is empty. This is the case if the root directory is empty. + - **[SelectDirectories(Func<IDirectoryNode,bool>)](IVirtualFileSystem.SelectDirectories(Func_IDirectoryNode,bool_).md 'Atypical.VirtualFileSystem.Core.Contracts.IVirtualFileSystem.SelectDirectories(System.Func)')** `Method` Finds all directory nodes that match the specified predicate. - **[TryGetDirectory(VFSDirectoryPath, IDirectoryNode)](IVirtualFileSystem.TryGetDirectory(VFSDirectoryPath,IDirectoryNode).md 'Atypical.VirtualFileSystem.Core.Contracts.IVirtualFileSystem.TryGetDirectory(Atypical.VirtualFileSystem.Core.ValueObjects.VFSDirectoryPath, Atypical.VirtualFileSystem.Core.Contracts.IDirectoryNode)')** `Method` Try to get a directory node by its path. The path must be absolute. If the directory node does not exist, this method returns `false` diff --git a/docs/links b/docs/links index 8bda2ed..60b162b 100644 --- a/docs/links +++ b/docs/links @@ -19,6 +19,7 @@ M:Atypical.VirtualFileSystem.Core.VFS.TryGetDirectory(Atypical.VirtualFileSystem M:Atypical.VirtualFileSystem.Core.VFS.TryGetDirectory(System.String,Atypical.VirtualFileSystem.Core.Contracts.IDirectoryNode@)|VFS.TryGetDirectory(string,IDirectoryNode).md|TryGetDirectory(string, IDirectoryNode) M:Atypical.VirtualFileSystem.Core.VFS.FindDirectories|VFS.FindDirectories().md|FindDirectories() M:Atypical.VirtualFileSystem.Core.VFS.FindDirectories(System.Text.RegularExpressions.Regex)|VFS.FindDirectories(Regex).md|FindDirectories(Regex) +M:Atypical.VirtualFileSystem.Core.VFS.SelectDirectories(System.Func{Atypical.VirtualFileSystem.Core.Contracts.IDirectoryNode,System.Boolean})|VFS.SelectDirectories(Func_IDirectoryNode,bool_).md|SelectDirectories(Func) M:Atypical.VirtualFileSystem.Core.VFS.GetFile(Atypical.VirtualFileSystem.Core.ValueObjects.VFSFilePath)|VFS.GetFile(VFSFilePath).md|GetFile(VFSFilePath) M:Atypical.VirtualFileSystem.Core.VFS.GetFile(System.String)|VFS.GetFile(string).md|GetFile(string) M:Atypical.VirtualFileSystem.Core.VFS.TryGetFile(Atypical.VirtualFileSystem.Core.ValueObjects.VFSFilePath,Atypical.VirtualFileSystem.Core.Contracts.IFileNode@)|VFS.TryGetFile(VFSFilePath,IFileNode).md|TryGetFile(VFSFilePath, IFileNode) @@ -102,6 +103,7 @@ M:Atypical.VirtualFileSystem.Core.Contracts.IVirtualFileSystem.CreateDirectory(S M:Atypical.VirtualFileSystem.Core.Contracts.IVirtualFileSystem.DeleteDirectory(Atypical.VirtualFileSystem.Core.ValueObjects.VFSDirectoryPath)|IVirtualFileSystem.DeleteDirectory(VFSDirectoryPath).md|DeleteDirectory(VFSDirectoryPath) M:Atypical.VirtualFileSystem.Core.Contracts.IVirtualFileSystem.DeleteDirectory(System.String)|IVirtualFileSystem.DeleteDirectory(string).md|DeleteDirectory(string) M:Atypical.VirtualFileSystem.Core.Contracts.IVirtualFileSystem.FindDirectories|IVirtualFileSystem.FindDirectories().md|FindDirectories() +M:Atypical.VirtualFileSystem.Core.Contracts.IVirtualFileSystem.SelectDirectories(System.Func{Atypical.VirtualFileSystem.Core.Contracts.IDirectoryNode,System.Boolean})|IVirtualFileSystem.SelectDirectories(Func_IDirectoryNode,bool_).md|SelectDirectories(Func) M:Atypical.VirtualFileSystem.Core.Contracts.IVirtualFileSystem.FindDirectories(System.Text.RegularExpressions.Regex)|IVirtualFileSystem.FindDirectories(Regex).md|FindDirectories(Regex) M:Atypical.VirtualFileSystem.Core.Contracts.IVirtualFileSystem.GetFile(Atypical.VirtualFileSystem.Core.ValueObjects.VFSFilePath)|IVirtualFileSystem.GetFile(VFSFilePath).md|GetFile(VFSFilePath) M:Atypical.VirtualFileSystem.Core.Contracts.IVirtualFileSystem.GetFile(System.String)|IVirtualFileSystem.GetFile(string).md|GetFile(string) diff --git a/src/Atypical.VirtualFileSystem.Core/Abstractions/VFSPath.cs b/src/Atypical.VirtualFileSystem.Core/Abstractions/VFSPath.cs index 3571af6..9910598 100644 --- a/src/Atypical.VirtualFileSystem.Core/Abstractions/VFSPath.cs +++ b/src/Atypical.VirtualFileSystem.Core/Abstractions/VFSPath.cs @@ -22,8 +22,6 @@ public abstract record VFSPath /// public static readonly Regex VFSPathRegex = new(VFSPathRegexPattern, RegexOptions.Compiled); - - /// /// Creates a new instance of . /// @@ -130,7 +128,6 @@ public string Name { public virtual bool Equals(VFSPath? other) { if (ReferenceEquals(null, other)) return false; - if (ReferenceEquals(this, other)) return true; return Value == other.Value; } diff --git a/src/Atypical.VirtualFileSystem.Core/Contracts/IVirtualFileSystem.cs b/src/Atypical.VirtualFileSystem.Core/Contracts/IVirtualFileSystem.cs index 2d69493..f25388e 100644 --- a/src/Atypical.VirtualFileSystem.Core/Contracts/IVirtualFileSystem.cs +++ b/src/Atypical.VirtualFileSystem.Core/Contracts/IVirtualFileSystem.cs @@ -114,6 +114,14 @@ public interface IVirtualFileSystem /// The directory nodes. IEnumerable FindDirectories(); + /// + /// Finds all directory nodes that match the specified predicate. + /// + /// The predicate. + /// The directory nodes. + IEnumerable SelectDirectories( + Func predicate); + /// /// Finds all directory nodes that match the specified regular expression. /// The regular expression must be relative to the root directory. diff --git a/src/Atypical.VirtualFileSystem.Core/VFS.Commands.cs b/src/Atypical.VirtualFileSystem.Core/VFS.Commands.cs index 82cc8f4..0658975 100644 --- a/src/Atypical.VirtualFileSystem.Core/VFS.Commands.cs +++ b/src/Atypical.VirtualFileSystem.Core/VFS.Commands.cs @@ -8,9 +8,6 @@ public IVirtualFileSystem CreateDirectory(VFSDirectoryPath directoryPath) if (directoryPath.IsRoot) ThrowCannotCreateRootDirectory(); - if (directoryPath.Parent == null) - ThrowCannotCreateDirectoryWithoutParent(); - var directory = new DirectoryNode(directoryPath); this.AddToIndex(directory); @@ -27,9 +24,10 @@ public IVirtualFileSystem CreateDirectory(string directoryPath) /// public IVirtualFileSystem DeleteDirectory(VFSDirectoryPath directoryPath) { + // cannot delete the root directory if (directoryPath.IsRoot) ThrowCannotDeleteRootDirectory(); - + // try to get the directory var found = this.TryGetDirectory(directoryPath, out _); if (!found) @@ -55,9 +53,6 @@ public IVirtualFileSystem DeleteDirectory(string directoryPath) /// public IVirtualFileSystem CreateFile(VFSFilePath filePath, string? content = null) { - if (filePath.Parent == null) - ThrowCannotCreateDirectoryWithoutParent(); - var file = new FileNode(filePath, content); this.AddToIndex(file); @@ -116,11 +111,4 @@ private static void ThrowCannotCreateRootDirectory() const string message = "Cannot create the root directory."; throw new VFSException(message); } - - [DoesNotReturn] - private static void ThrowCannotCreateDirectoryWithoutParent() - { - const string message = "Cannot create a directory without a parent."; - throw new VFSException(message); - } } \ No newline at end of file diff --git a/src/Atypical.VirtualFileSystem.Core/VFS.Queries.cs b/src/Atypical.VirtualFileSystem.Core/VFS.Queries.cs index b88185a..62af56c 100644 --- a/src/Atypical.VirtualFileSystem.Core/VFS.Queries.cs +++ b/src/Atypical.VirtualFileSystem.Core/VFS.Queries.cs @@ -48,9 +48,9 @@ public IEnumerable FindDirectories() /// public IEnumerable FindDirectories( Regex regexPattern) - => FindDirectories() - .Where(f => regexPattern.IsMatch(f.Path.Value)); + => SelectDirectories(f => regexPattern.IsMatch(f.Path.Value)); + /// public IEnumerable SelectDirectories( Func predicate) => FindDirectories() diff --git a/tests/Atypical.VirtualFileSystem.UnitTests/Models/VirtualFileSystemTests.cs b/tests/Atypical.VirtualFileSystem.UnitTests/Models/VirtualFileSystemTests.cs index 3bd7dc7..41d82ec 100644 --- a/tests/Atypical.VirtualFileSystem.UnitTests/Models/VirtualFileSystemTests.cs +++ b/tests/Atypical.VirtualFileSystem.UnitTests/Models/VirtualFileSystemTests.cs @@ -212,6 +212,21 @@ public void CreateDirectory_throws_an_exception_if_the_path_is_not_a_directory() .Throw() .WithMessage("The directory path 'vfs://dir1/dir2/dir3/file.txt' contains a file extension."); } + + [Fact] + public void CreateDirectory_throws_an_exception_if_the_path_is_the_root_directory() + { + // Arrange + IVirtualFileSystem vfs = new VFS(); + + // Act + Action action = () => vfs.CreateDirectory("vfs://"); + + // Assert + action.Should() + .Throw() + .WithMessage("Cannot create the root directory."); + } } public class MethodDeleteDirectory @@ -261,6 +276,21 @@ public void DeleteDirectory_throws_an_exception_if_the_directory_does_not_exist( .Throw() .WithMessage("The directory 'vfs://dir1' does not exist in the index."); } + + [Fact] + public void DeleteDirectory_throws_an_exception_if_the_path_is_the_root_directory() + { + // Arrange + IVirtualFileSystem vfs = new VFS(); + + // Act + Action action = () => vfs.DeleteDirectory("vfs://"); + + // Assert + action.Should() + .Throw() + .WithMessage("Cannot delete the root directory."); + } } public class MethodFindDirectories @@ -305,6 +335,31 @@ public void FindDirectories_returns_all_directories_matching_a_pattern() } } + public class MethodSelectDirectories + { + [Fact] + public void SelectDirectories_returns_all_directories() + { + // Arrange + IVirtualFileSystem vfs = new VFS(); + vfs.CreateDirectory("dir1"); + vfs.CreateDirectory("dir2"); + vfs.CreateDirectory("dir3"); + + // Act + var directories = vfs + .SelectDirectories(x => x.IsDirectory) + .ToList(); + + // Assert + directories.Should().NotBeEmpty(); + directories.Should().HaveCount(3); // dir1 + dir2 + dir3 + directories.Should().Contain(d => d.Path.Value == "vfs://dir1"); + directories.Should().Contain(d => d.Path.Value == "vfs://dir2"); + directories.Should().Contain(d => d.Path.Value == "vfs://dir3"); + } + } + public class MethodGetFile { [Fact] diff --git a/tests/Atypical.VirtualFileSystem.UnitTests/ValueObjects/VFSPathTests.cs b/tests/Atypical.VirtualFileSystem.UnitTests/ValueObjects/VFSPathTests.cs new file mode 100644 index 0000000..2f32b1c --- /dev/null +++ b/tests/Atypical.VirtualFileSystem.UnitTests/ValueObjects/VFSPathTests.cs @@ -0,0 +1,51 @@ +using Atypical.VirtualFileSystem.Core.Abstractions; + +namespace VirtualFileSystem.UnitTests.ValueObjects; + +public class VFSPathTests +{ + public class Equality + { + [Fact] + public void Equals_returns_true_when_paths_are_equal() + { + // Arrange + VFSPath path1 = new VFSDirectoryPath("/path/to/file"); + VFSPath path2 = new VFSDirectoryPath("/path/to/file"); + + // Act + bool result = path1.Equals(path2); + + // Assert + Assert.True(result); + } + + [Fact] + public void Equals_returns_true_when_paths_are_the_same() + { + // Arrange + VFSPath path1 = new VFSDirectoryPath("/path/to/file"); + VFSPath path2 = path1; + + // Act + bool result = path1.Equals(path2); + + // Assert + Assert.True(result); + } + + [Fact] + public void Equals_returns_false_when_paths_are_not_equal() + { + // Arrange + VFSPath path1 = new VFSDirectoryPath("/path/to/file"); + VFSPath path2 = new VFSDirectoryPath("/path/to/directory"); + + // Act + bool result = path1.Equals(path2); + + // Assert + Assert.False(result); + } + } +} \ No newline at end of file