Skip to content

Commit

Permalink
Merge pull request #34 from Atypical-Consulting/tests
Browse files Browse the repository at this point in the history
Add some unit tests
  • Loading branch information
phmatray authored Mar 11, 2023
2 parents 1672c22 + d88cb70 commit 1a6e11e
Show file tree
Hide file tree
Showing 12 changed files with 168 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -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<IDirectoryNode,bool>) Method

Finds all directory nodes that match the specified predicate.

```csharp
System.Collections.Generic.IEnumerable<Atypical.VirtualFileSystem.Core.Contracts.IDirectoryNode> SelectDirectories(System.Func<Atypical.VirtualFileSystem.Core.Contracts.IDirectoryNode,bool> predicate);
```
#### Parameters

<a name='Atypical.VirtualFileSystem.Core.Contracts.IVirtualFileSystem.SelectDirectories(System.Func_Atypical.VirtualFileSystem.Core.Contracts.IDirectoryNode,bool_).predicate'></a>

`predicate` [System.Func&lt;](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')[&gt;](https://docs.microsoft.com/en-us/dotnet/api/System.Func-2 'System.Func`2')

The predicate.

#### Returns
[System.Collections.Generic.IEnumerable&lt;](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')[&gt;](https://docs.microsoft.com/en-us/dotnet/api/System.Collections.Generic.IEnumerable-1 'System.Collections.Generic.IEnumerable`1')
The directory nodes.
1 change: 1 addition & 0 deletions docs/api/IVirtualFileSystem.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.<br/>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.<br/>This is the case if the root directory is empty. |
| [SelectDirectories(Func&lt;IDirectoryNode,bool&gt;)](IVirtualFileSystem.SelectDirectories(Func_IDirectoryNode,bool_).md 'Atypical.VirtualFileSystem.Core.Contracts.IVirtualFileSystem.SelectDirectories(System.Func<Atypical.VirtualFileSystem.Core.Contracts.IDirectoryNode,bool>)') | 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.<br/>The path must be absolute.<br/>If the directory node does not exist, this method returns `false`<br/>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.<br/>The path must be absolute.<br/>If the directory node does not exist, this method returns `false`<br/>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.<br/>The path must be absolute. |
Expand Down
23 changes: 23 additions & 0 deletions docs/api/VFS.SelectDirectories(Func_IDirectoryNode,bool_).md
Original file line number Diff line number Diff line change
@@ -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<IDirectoryNode,bool>) Method

Finds all directory nodes that match the specified predicate.

```csharp
public System.Collections.Generic.IEnumerable<Atypical.VirtualFileSystem.Core.Contracts.IDirectoryNode> SelectDirectories(System.Func<Atypical.VirtualFileSystem.Core.Contracts.IDirectoryNode,bool> predicate);
```
#### Parameters

<a name='Atypical.VirtualFileSystem.Core.VFS.SelectDirectories(System.Func_Atypical.VirtualFileSystem.Core.Contracts.IDirectoryNode,bool_).predicate'></a>

`predicate` [System.Func&lt;](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')[&gt;](https://docs.microsoft.com/en-us/dotnet/api/System.Func-2 'System.Func`2')

The predicate.

Implements [SelectDirectories(Func&lt;IDirectoryNode,bool&gt;)](IVirtualFileSystem.SelectDirectories(Func_IDirectoryNode,bool_).md 'Atypical.VirtualFileSystem.Core.Contracts.IVirtualFileSystem.SelectDirectories(System.Func<Atypical.VirtualFileSystem.Core.Contracts.IDirectoryNode,bool>)')

#### Returns
[System.Collections.Generic.IEnumerable&lt;](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')[&gt;](https://docs.microsoft.com/en-us/dotnet/api/System.Collections.Generic.IEnumerable-1 'System.Collections.Generic.IEnumerable`1')
The directory nodes.
1 change: 1 addition & 0 deletions docs/api/VFS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.<br/>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.<br/>This is the case if the root directory is empty. |
| [SelectDirectories(Func&lt;IDirectoryNode,bool&gt;)](VFS.SelectDirectories(Func_IDirectoryNode,bool_).md 'Atypical.VirtualFileSystem.Core.VFS.SelectDirectories(System.Func<Atypical.VirtualFileSystem.Core.Contracts.IDirectoryNode,bool>)') | 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.<br/>The path must be absolute.<br/>If the directory node does not exist, this method returns `false`<br/>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.<br/>The path must be absolute.<br/>If the directory node does not exist, this method returns `false`<br/>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`. |
Expand Down
2 changes: 2 additions & 0 deletions docs/api/VirtualFileSystem.md
Original file line number Diff line number Diff line change
Expand Up @@ -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&lt;IDirectoryNode,bool&gt;)](VFS.SelectDirectories(Func_IDirectoryNode,bool_).md 'Atypical.VirtualFileSystem.Core.VFS.SelectDirectories(System.Func<Atypical.VirtualFileSystem.Core.Contracts.IDirectoryNode,bool>)')** `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.
Expand Down Expand Up @@ -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&lt;IDirectoryNode,bool&gt;)](IVirtualFileSystem.SelectDirectories(Func_IDirectoryNode,bool_).md 'Atypical.VirtualFileSystem.Core.Contracts.IVirtualFileSystem.SelectDirectories(System.Func<Atypical.VirtualFileSystem.Core.Contracts.IDirectoryNode,bool>)')** `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`
Expand Down
2 changes: 2 additions & 0 deletions docs/links
Original file line number Diff line number Diff line change
Expand Up @@ -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<IDirectoryNode,bool>)
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)
Expand Down Expand Up @@ -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<IDirectoryNode,bool>)
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)
Expand Down
3 changes: 0 additions & 3 deletions src/Atypical.VirtualFileSystem.Core/Abstractions/VFSPath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ public abstract record VFSPath
/// </summary>
public static readonly Regex VFSPathRegex = new(VFSPathRegexPattern, RegexOptions.Compiled);



/// <summary>
/// Creates a new instance of <see cref="VFSPath" />.
/// </summary>
Expand Down Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,14 @@ public interface IVirtualFileSystem
/// <returns>The directory nodes.</returns>
IEnumerable<IDirectoryNode> FindDirectories();

/// <summary>
/// Finds all directory nodes that match the specified predicate.
/// </summary>
/// <param name="predicate">The predicate.</param>
/// <returns>The directory nodes.</returns>
IEnumerable<IDirectoryNode> SelectDirectories(
Func<IDirectoryNode, bool> predicate);

/// <summary>
/// Finds all directory nodes that match the specified regular expression.
/// The regular expression must be relative to the root directory.
Expand Down
16 changes: 2 additions & 14 deletions src/Atypical.VirtualFileSystem.Core/VFS.Commands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -27,9 +24,10 @@ public IVirtualFileSystem CreateDirectory(string directoryPath)
/// <inheritdoc cref="IVirtualFileSystem.DeleteDirectory(VFSDirectoryPath)" />
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)
Expand All @@ -55,9 +53,6 @@ public IVirtualFileSystem DeleteDirectory(string directoryPath)
/// <inheritdoc cref="IVirtualFileSystem.CreateFile(VFSFilePath,string?)" />
public IVirtualFileSystem CreateFile(VFSFilePath filePath, string? content = null)
{
if (filePath.Parent == null)
ThrowCannotCreateDirectoryWithoutParent();

var file = new FileNode(filePath, content);
this.AddToIndex(file);

Expand Down Expand Up @@ -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);
}
}
4 changes: 2 additions & 2 deletions src/Atypical.VirtualFileSystem.Core/VFS.Queries.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ public IEnumerable<IDirectoryNode> FindDirectories()
/// <inheritdoc cref="IVirtualFileSystem.FindDirectories(Regex)" />
public IEnumerable<IDirectoryNode> FindDirectories(
Regex regexPattern)
=> FindDirectories()
.Where(f => regexPattern.IsMatch(f.Path.Value));
=> SelectDirectories(f => regexPattern.IsMatch(f.Path.Value));

/// <inheritdoc cref="IVirtualFileSystem.SelectDirectories(Func{IDirectoryNode,bool})" />
public IEnumerable<IDirectoryNode> SelectDirectories(
Func<IDirectoryNode, bool> predicate)
=> FindDirectories()
Expand Down
Loading

0 comments on commit 1a6e11e

Please sign in to comment.