Skip to content

Commit

Permalink
Merge pull request #37 from Atypical-Consulting/simplify-vfspath
Browse files Browse the repository at this point in the history
Remove the regex from the VFSPath class
  • Loading branch information
phmatray authored Mar 16, 2023
2 parents bbe9b1f + 92850c2 commit 23efa11
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 33 deletions.
13 changes: 0 additions & 13 deletions docs/api/VFSPath.VFSPathRegex.md

This file was deleted.

4 changes: 0 additions & 4 deletions docs/api/VFSPath.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@ Implements [System.IEquatable<](https://docs.microsoft.com/en-us/dotnet/api/S
| :--- | :--- |
| [VFSPath(string)](VFSPath.VFSPath(string).md 'Atypical.VirtualFileSystem.Core.Abstractions.VFSPath.VFSPath(string)') | Creates a new instance of [VFSPath](VFSPath.md 'Atypical.VirtualFileSystem.Core.Abstractions.VFSPath'). |

| Fields | |
| :--- | :--- |
| [VFSPathRegex](VFSPath.VFSPathRegex.md 'Atypical.VirtualFileSystem.Core.Abstractions.VFSPath.VFSPathRegex') | Regex for matching a valid file system path. |

| Properties | |
| :--- | :--- |
| [Depth](VFSPath.Depth.md 'Atypical.VirtualFileSystem.Core.Abstractions.VFSPath.Depth') | Gets the depth of the file system entry.<br/>The root directory has a depth of 0.<br/>The depth of a file is the depth of its parent directory plus one.<br/>The depth of a directory is the depth of its parent directory plus one. |
Expand Down
1 change: 0 additions & 1 deletion docs/api/VirtualFileSystem.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@
- **[Path](VFSNode.Path.md 'Atypical.VirtualFileSystem.Core.Abstractions.VFSNode.Path')** `Property` Gets the creation time of the node.
- **[VFSPath](VFSPath.md 'Atypical.VirtualFileSystem.Core.Abstractions.VFSPath')** `Class` Represents a file system entry (file or directory) in the virtual file system.
- **[VFSPath(string)](VFSPath.VFSPath(string).md 'Atypical.VirtualFileSystem.Core.Abstractions.VFSPath.VFSPath(string)')** `Constructor` Creates a new instance of [VFSPath](VFSPath.md 'Atypical.VirtualFileSystem.Core.Abstractions.VFSPath').
- **[VFSPathRegex](VFSPath.VFSPathRegex.md 'Atypical.VirtualFileSystem.Core.Abstractions.VFSPath.VFSPathRegex')** `Field` Regex for matching a valid file system path.
- **[Depth](VFSPath.Depth.md 'Atypical.VirtualFileSystem.Core.Abstractions.VFSPath.Depth')** `Property` Gets the depth of the file system entry.
The root directory has a depth of 0.
The depth of a file is the depth of its parent directory plus one.
Expand Down
1 change: 0 additions & 1 deletion docs/links
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,6 @@ P:Atypical.VirtualFileSystem.Core.Abstractions.VFSNode.IsFile|VFSNode.IsFile.md|
M:Atypical.VirtualFileSystem.Core.Abstractions.VFSNode.#ctor(Atypical.VirtualFileSystem.Core.Abstractions.VFSPath)|VFSNode.VFSNode(VFSPath).md|VFSNode(VFSPath)
N:Atypical.VirtualFileSystem.Core.Abstractions|VirtualFileSystem.md#Atypical.VirtualFileSystem.Core.Abstractions|Atypical.VirtualFileSystem.Core.Abstractions
T:Atypical.VirtualFileSystem.Core.Abstractions.VFSNode|VFSNode.md|VFSNode
F:Atypical.VirtualFileSystem.Core.Abstractions.VFSPath.VFSPathRegex|VFSPath.VFSPathRegex.md|VFSPathRegex
P:Atypical.VirtualFileSystem.Core.Abstractions.VFSPath.Value|VFSPath.Value.md|Value
P:Atypical.VirtualFileSystem.Core.Abstractions.VFSPath.Parent|VFSPath.Parent.md|Parent
P:Atypical.VirtualFileSystem.Core.Abstractions.VFSPath.HasParent|VFSPath.HasParent.md|HasParent
Expand Down
17 changes: 3 additions & 14 deletions src/Atypical.VirtualFileSystem.Core/Abstractions/VFSPath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,6 @@ namespace Atypical.VirtualFileSystem.Core.Abstractions;
/// </summary>
public abstract record VFSPath
{
/// <summary>
/// Regex pattern for matching a valid file system path.
/// </summary>
private const string VFSPathRegexPattern =
@$"^{ROOT_PATH}(?<path>([a-zA-Z0-9_\-\.]+{DIRECTORY_SEPARATOR})*[a-zA-Z0-9_\-\.]+)$";

/// <summary>
/// Regex for matching a valid file system path.
/// </summary>
public static readonly Regex VFSPathRegex = new(VFSPathRegexPattern, RegexOptions.Compiled);

/// <summary>
/// Creates a new instance of <see cref="VFSPath" />.
/// </summary>
Expand All @@ -31,7 +20,7 @@ public abstract record VFSPath
public VFSPath(string path)
{
// ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract
if (path is null)
if (string.IsNullOrWhiteSpace(path))
ThrowArgumentHasInvalidFormat(string.Empty);

var vfsPath = CleanVFSPathInput(path);
Expand All @@ -46,9 +35,9 @@ public VFSPath(string path)
if (vfsPath.Contains($".{DIRECTORY_SEPARATOR}"))
ThrowArgumentHasRelativePathSegment(vfsPath);

if (!VFSPathRegex.IsMatch(vfsPath))
if (vfsPath.Split(DIRECTORY_SEPARATOR + DIRECTORY_SEPARATOR).Length > 2)
ThrowArgumentHasInvalidFormat(vfsPath);

Value = vfsPath;

// set parent path
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,24 @@ public class Constructor
// - The path must not contain any relative path segments (..).
// - The path must not contain any consecutive slashes (//).
// - The path must not end with a slash (/).

[Theory]
[InlineData(@"AddChild.md", @"vfs://AddChild.md")]
[InlineData(@"AddChild(IDirectoryNode).md", @"vfs://AddChild(IDirectoryNode).md")]
[InlineData(@"DirectoryNode.AddChild(IDirectoryNode).md", @"vfs://DirectoryNode.AddChild(IDirectoryNode).md")]
[InlineData(@"api/AddChild.md", @"vfs://api/AddChild.md")]
[InlineData(@"api/AddChild(IDirectoryNode).md", @"vfs://api/AddChild(IDirectoryNode).md")]
[InlineData(@"api/DirectoryNode.AddChild(IDirectoryNode).md", @"vfs://api/DirectoryNode.AddChild(IDirectoryNode).md")]
public void Constructor_create_instance_when_path_is_valid(string path, string expectedPath)
{
// Act
var vfsPath = new VFSFilePath(path);

// Assert
vfsPath.Value.Should().NotBeNull();
vfsPath.Value.Should().Be(expectedPath);
}

[Fact]
public void Constructor_create_instance_when_path_is_valid_and_starts_with_dot()
{
Expand Down

0 comments on commit 23efa11

Please sign in to comment.