Skip to content

Commit b1c446b

Browse files
committed
refactor: split vfs tests
1 parent 17ba67f commit b1c446b

19 files changed

+923
-904
lines changed

tests/Atypical.VirtualFileSystem.UnitTests/SystemOperations/VirtualFileSystemTests.cs

Lines changed: 0 additions & 904 deletions
This file was deleted.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright (c) 2022-2023, Atypical Consulting SRL
2+
// All rights reserved... but seriously, we're open to sharing if you ask nicely!
3+
//
4+
// This source code is licensed under the BSD-style license found in the
5+
// LICENSE file in the root directory of this source tree.
6+
7+
namespace VirtualFileSystem.UnitTests.SystemOperations;
8+
9+
public abstract class VirtualFileSystemTestsBase
10+
{
11+
protected static IVirtualFileSystem CreateVFS()
12+
=> new VirtualFileSystemFactory().CreateFileSystem();
13+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
namespace VirtualFileSystem.UnitTests.SystemOperations;
2+
3+
public class VirtualFileSystem_Constructor_Tests : VirtualFileSystemTestsBase
4+
{
5+
[Fact]
6+
public void Constructor_creates_a_new_file_system()
7+
{
8+
// Act
9+
var vfs = CreateVFS();
10+
11+
// Assert
12+
vfs.Should().NotBeNull();
13+
vfs.IsEmpty.Should().BeTrue();
14+
vfs.Root.IsDirectory.Should().BeTrue();
15+
vfs.Root.IsFile.Should().BeFalse();
16+
vfs.Root.Path.Value.Should().Be("vfs://");
17+
vfs.Root.CreationTime.Should().BeCloseTo(DateTime.Now, TimeSpan.FromHours(1));
18+
}
19+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
namespace VirtualFileSystem.UnitTests.SystemOperations;
2+
3+
public class VirtualFileSystem_MethodCreateDirectory_Tests : VirtualFileSystemTestsBase
4+
{
5+
[Fact]
6+
public void CreateDirectory_creates_a_directory()
7+
{
8+
// Arrange
9+
var vfs = CreateVFS();
10+
VFSDirectoryPath directoryPath = new("dir1");
11+
12+
// Act
13+
vfs.CreateDirectory(directoryPath);
14+
15+
// Assert
16+
vfs.IsEmpty.Should().BeFalse();
17+
vfs.Index.RawIndex.Should().NotBeEmpty();
18+
vfs.Index.RawIndex.Should().HaveCount(1);
19+
vfs.Index.RawIndex.Should().ContainKey("vfs://dir1");
20+
vfs.Root.IsDirectory.Should().BeTrue();
21+
vfs.Root.IsFile.Should().BeFalse();
22+
vfs.Root.Path.Value.Should().Be("vfs://");
23+
vfs.Root.CreationTime.Should().BeCloseTo(DateTime.Now, TimeSpan.FromHours(1));
24+
}
25+
26+
[Fact]
27+
public void CreateDirectory_creates_a_directory_and_its_parents()
28+
{
29+
// Arrange
30+
var vfs = CreateVFS();
31+
VFSDirectoryPath directoryPath = new("dir1/dir2/dir3");
32+
33+
// Act
34+
vfs.CreateDirectory(directoryPath);
35+
36+
// Assert
37+
vfs.Index.RawIndex.Should().NotBeEmpty();
38+
vfs.Index.RawIndex.Should().HaveCount(3); // dir1 + dir2 + dir3
39+
vfs.Index.RawIndex.Should().ContainKey(directoryPath.Value);
40+
vfs.Index.RawIndex.Should().ContainKey("vfs://dir1");
41+
vfs.Index.RawIndex.Should().ContainKey("vfs://dir1/dir2");
42+
vfs.Index.RawIndex.Should().ContainKey("vfs://dir1/dir2/dir3");
43+
44+
vfs.Index["vfs://dir1"].Should().BeAssignableTo<IDirectoryNode>();
45+
vfs.Index["vfs://dir1/dir2"].Should().BeAssignableTo<IDirectoryNode>();
46+
vfs.Index["vfs://dir1/dir2/dir3"].Should().BeAssignableTo<IDirectoryNode>();
47+
48+
vfs.Index["vfs://dir1"].As<IDirectoryNode>().Directories.Should().NotBeEmpty();
49+
vfs.Index["vfs://dir1"].As<IDirectoryNode>().Directories.Should().HaveCount(1);
50+
}
51+
52+
[Fact]
53+
public void CreateDirectory_throws_an_exception_if_the_directory_already_exists()
54+
{
55+
// Arrange
56+
var vfs = CreateVFS();
57+
var directoryPath = new VFSDirectoryPath("dir1");
58+
vfs.CreateDirectory(directoryPath);
59+
60+
// Act
61+
Action action = () => vfs.CreateDirectory(directoryPath);
62+
63+
// Assert
64+
action.Should()
65+
.Throw<VirtualFileSystemException>()
66+
.WithMessage($"The node 'vfs://dir1' already exists in the index.");
67+
}
68+
69+
[Fact]
70+
public void CreateDirectory_throws_an_exception_if_the_path_is_the_root_directory()
71+
{
72+
// Arrange
73+
var vfs = CreateVFS();
74+
75+
// Act
76+
Action action = () => vfs.CreateDirectory(new VFSRootPath());
77+
78+
// Assert
79+
action.Should()
80+
.Throw<VirtualFileSystemException>()
81+
.WithMessage("Cannot create the root directory.");
82+
}
83+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
namespace VirtualFileSystem.UnitTests.SystemOperations;
2+
3+
public class VirtualFileSystem_MethodCreateFile_Tests : VirtualFileSystemTestsBase
4+
{
5+
[Fact]
6+
public void CreateFile_creates_a_file()
7+
{
8+
// Arrange
9+
var vfs = CreateVFS();
10+
VFSFilePath filePath = new("file.txt");
11+
12+
// Act
13+
vfs.CreateFile(filePath);
14+
15+
// Assert
16+
vfs.IsEmpty.Should().BeFalse();
17+
vfs.Index.RawIndex.Should().NotBeEmpty();
18+
vfs.Index.RawIndex.Should().HaveCount(1);
19+
vfs.Index.RawIndex.Should().ContainKey("vfs://file.txt");
20+
vfs.Root.Files.Should().NotBeEmpty();
21+
vfs.Root.Files.Should().HaveCount(1);
22+
}
23+
24+
[Fact]
25+
public void CreateFile_creates_a_file_and_its_parents()
26+
{
27+
// Arrange
28+
var vfs = CreateVFS();
29+
VFSFilePath filePath = new("dir1/dir2/dir3/file.txt");
30+
31+
// Act
32+
vfs.CreateFile(filePath);
33+
34+
// Assert
35+
vfs.IsEmpty.Should().BeFalse();
36+
vfs.Index.RawIndex.Should().NotBeEmpty();
37+
vfs.Index.RawIndex.Should().HaveCount(4); // dir1 + dir2 + dir3 + file.txt
38+
vfs.Index.RawIndex.Should().ContainKey("vfs://dir1");
39+
vfs.Index.RawIndex.Should().ContainKey("vfs://dir1/dir2");
40+
vfs.Index.RawIndex.Should().ContainKey("vfs://dir1/dir2/dir3");
41+
vfs.Index.RawIndex.Should().ContainKey("vfs://dir1/dir2/dir3/file.txt");
42+
vfs.Root.Directories.Should().NotBeEmpty();
43+
vfs.Root.Directories.Should().HaveCount(1);
44+
}
45+
46+
[Fact]
47+
public void CreateFile_throws_an_exception_if_the_file_already_exists()
48+
{
49+
// Arrange
50+
var vfs = CreateVFS();
51+
var filePath = new VFSFilePath("dir1/dir2/dir3/file.txt");
52+
vfs.CreateFile(filePath);
53+
54+
// Act
55+
Action action = () => vfs.CreateFile(filePath);
56+
57+
// Assert
58+
action.Should()
59+
.Throw<VirtualFileSystemException>()
60+
.WithMessage("The node 'vfs://dir1/dir2/dir3/file.txt' already exists in the index.");
61+
}
62+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
namespace VirtualFileSystem.UnitTests.SystemOperations;
2+
3+
public class VirtualFileSystem_MethodDeleteDirectory_Tests : VirtualFileSystemTestsBase
4+
{
5+
[Fact]
6+
public void DeleteDirectory_deletes_a_directory()
7+
{
8+
// Arrange
9+
var vfs = CreateVFS();
10+
VFSDirectoryPath directoryPath = new("dir1");
11+
vfs.CreateDirectory(directoryPath);
12+
13+
// Act
14+
vfs.DeleteDirectory(directoryPath);
15+
16+
// Assert
17+
vfs.IsEmpty.Should().BeTrue();
18+
}
19+
20+
[Fact]
21+
public void DeleteDirectory_deletes_a_directory_and_its_children()
22+
{
23+
// Arrange
24+
var vfs = CreateVFS();
25+
VFSDirectoryPath directoryPath = new("dir1/dir2/dir3");
26+
vfs.CreateDirectory(directoryPath);
27+
28+
// Act
29+
VFSDirectoryPath ancestorPath = new("dir1");
30+
vfs.DeleteDirectory(ancestorPath);
31+
32+
// Assert
33+
vfs.IsEmpty.Should().BeTrue();
34+
}
35+
36+
[Fact]
37+
public void DeleteDirectory_throws_an_exception_if_the_directory_does_not_exist()
38+
{
39+
// Arrange
40+
var vfs = CreateVFS();
41+
VFSDirectoryPath directoryPath = new("dir1");
42+
43+
// Act
44+
Action action = () => vfs.DeleteDirectory(directoryPath);
45+
46+
// Assert
47+
action.Should()
48+
.Throw<VirtualFileSystemException>()
49+
.WithMessage("The directory 'vfs://dir1' does not exist in the index.");
50+
}
51+
52+
[Fact]
53+
public void DeleteDirectory_throws_an_exception_if_the_path_is_the_root_directory()
54+
{
55+
// Arrange
56+
var vfs = CreateVFS();
57+
VFSDirectoryPath rootPath = new("vfs://");
58+
59+
// Act
60+
Action action = () => vfs.DeleteDirectory(rootPath);
61+
62+
// Assert
63+
action.Should()
64+
.Throw<VirtualFileSystemException>()
65+
.WithMessage("Cannot delete the root directory.");
66+
}
67+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
namespace VirtualFileSystem.UnitTests.SystemOperations;
2+
3+
public class VirtualFileSystem_MethodDeleteFile_Tests : VirtualFileSystemTestsBase
4+
{
5+
[Fact]
6+
public void DeleteFile_deletes_a_file()
7+
{
8+
// Arrange
9+
var vfs = CreateVFS();
10+
var filePath = new VFSFilePath("dir1/dir2/dir3/file.txt");
11+
vfs.CreateFile(filePath);
12+
13+
// Act
14+
vfs.DeleteFile(filePath);
15+
16+
// Assert
17+
vfs.Index.Count.Should().Be(3); // dir1, dir2, dir3
18+
}
19+
20+
[Fact]
21+
public void DeleteFile_throws_an_exception_if_the_file_does_not_exist()
22+
{
23+
// Arrange
24+
var vfs = CreateVFS();
25+
26+
// Act
27+
Action action = () => vfs.DeleteFile(new VFSFilePath("dir1/dir2/dir3/file.txt"));
28+
29+
// Assert
30+
action.Should()
31+
.Throw<VirtualFileSystemException>()
32+
.WithMessage("The file 'vfs://dir1/dir2/dir3/file.txt' does not exist in the index.");
33+
}
34+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using System.Text.RegularExpressions;
2+
3+
namespace VirtualFileSystem.UnitTests.SystemOperations;
4+
5+
public class VirtualFileSystem_MethodFindDirectories_Tests : VirtualFileSystemTestsBase
6+
{
7+
[Fact]
8+
public void FindDirectories_returns_all_directories()
9+
{
10+
// Arrange
11+
var vfs = CreateVFS();
12+
vfs.CreateDirectory(new VFSDirectoryPath("dir1"));
13+
vfs.CreateDirectory(new VFSDirectoryPath("dir2"));
14+
vfs.CreateDirectory(new VFSDirectoryPath("dir3"));
15+
16+
// Act
17+
var directories = vfs.Directories.ToList();
18+
19+
// Assert
20+
directories.Should().NotBeEmpty();
21+
directories.Should().HaveCount(3); // dir1 + dir2 + dir3
22+
directories.Should().Contain(d => d.Path.Value == "vfs://dir1");
23+
directories.Should().Contain(d => d.Path.Value == "vfs://dir2");
24+
directories.Should().Contain(d => d.Path.Value == "vfs://dir3");
25+
}
26+
27+
[Fact]
28+
public void FindDirectories_returns_all_directories_matching_a_pattern()
29+
{
30+
// Arrange
31+
var vfs = CreateVFS();
32+
vfs.CreateDirectory(new VFSDirectoryPath("dir1"));
33+
vfs.CreateDirectory(new VFSDirectoryPath("dir2"));
34+
vfs.CreateDirectory(new VFSDirectoryPath("dir3"));
35+
36+
// Act
37+
var regexPattern = new Regex("dir1");
38+
var directories = vfs.FindDirectories(regexPattern).ToList();
39+
40+
// Assert
41+
directories.Should().NotBeEmpty();
42+
directories.Should().HaveCount(1);
43+
directories.Should().Contain(d => d.Path.Value == "vfs://dir1");
44+
}
45+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
using System.Text.RegularExpressions;
2+
3+
namespace VirtualFileSystem.UnitTests.SystemOperations;
4+
5+
public class VirtualFileSystem_MethodFindFiles_Tests : VirtualFileSystemTestsBase
6+
{
7+
[Fact]
8+
public void FindFiles_returns_all_files()
9+
{
10+
// Arrange
11+
var vfs = CreateVFS();
12+
vfs.CreateDirectory(new VFSDirectoryPath("dir1"));
13+
vfs.CreateDirectory(new VFSDirectoryPath("dir2"));
14+
vfs.CreateDirectory(new VFSDirectoryPath("dir3"));
15+
vfs.CreateFile(new VFSFilePath("dir1/file1.txt"));
16+
vfs.CreateFile(new VFSFilePath("dir2/file2.txt"));
17+
vfs.CreateFile(new VFSFilePath("dir3/file3.txt"));
18+
19+
// Act
20+
var files = vfs.Files.ToList();
21+
22+
// Assert
23+
files.Should().NotBeEmpty();
24+
files.Should().HaveCount(3);
25+
files.Should().Contain(f => f.Path.Value == "vfs://dir1/file1.txt");
26+
files.Should().Contain(f => f.Path.Value == "vfs://dir2/file2.txt");
27+
files.Should().Contain(f => f.Path.Value == "vfs://dir3/file3.txt");
28+
}
29+
30+
[Fact]
31+
public void FindFiles_with_valid_data_returns_a_list_of_files_with_content_and_name()
32+
{
33+
// Arrange
34+
var vfs = CreateVFS();
35+
vfs.CreateFile(new VFSFilePath("file1.txt"), "content1");
36+
vfs.CreateFile(new VFSFilePath("file2.txt"), "content2");
37+
vfs.CreateFile(new VFSFilePath("file3.txt"), "content3");
38+
39+
var regex = new Regex(@"file\d.txt");
40+
41+
// Act
42+
var files = vfs.FindFiles(regex).ToList();
43+
44+
// Assert
45+
files.Should().NotBeNull();
46+
files.Count.Should().Be(3);
47+
files[0].Name.Should().Be("file1.txt");
48+
files[0].Content.Should().Be("content1");
49+
files[1].Name.Should().Be("file2.txt");
50+
files[1].Content.Should().Be("content2");
51+
files[2].Name.Should().Be("file3.txt");
52+
files[2].Content.Should().Be("content3");
53+
// Assert Index
54+
vfs.Index.Count.Should().Be(3); // file1, file2, file3
55+
}
56+
}

0 commit comments

Comments
 (0)