Skip to content

Commit 6621f4c

Browse files
committed
refactor: refactor commands tests
1 parent 0634d0f commit 6621f4c

9 files changed

+276
-332
lines changed

tests/Atypical.VirtualFileSystem.UnitTests/SystemOperations/Commands/VirtualFileSystem_MethodCreateDirectory_Tests.cs

Lines changed: 40 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -2,63 +2,62 @@ namespace VirtualFileSystem.UnitTests.SystemOperations.Commands;
22

33
public class VirtualFileSystem_MethodCreateDirectory_Tests : VirtualFileSystemTestsBase
44
{
5+
private readonly IVirtualFileSystem _vfs = CreateVFS();
6+
private VFSDirectoryPath _directoryPath = new("dir1");
7+
8+
private void Act()
9+
=> _vfs.CreateDirectory(_directoryPath);
10+
511
[Fact]
612
public void CreateDirectory_creates_a_directory()
713
{
8-
// Arrange
9-
var vfs = CreateVFS();
10-
VFSDirectoryPath directoryPath = new("dir1");
11-
1214
// Act
13-
vfs.CreateDirectory(directoryPath);
15+
Act();
1416

1517
// 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(new VFSDirectoryPath("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));
18+
_vfs.IsEmpty.Should().BeFalse();
19+
_vfs.Index.RawIndex.Should().NotBeEmpty();
20+
_vfs.Index.RawIndex.Should().HaveCount(1);
21+
_vfs.Index.RawIndex.Should().ContainKey(new VFSDirectoryPath("vfs://dir1"));
22+
_vfs.Root.IsDirectory.Should().BeTrue();
23+
_vfs.Root.IsFile.Should().BeFalse();
24+
_vfs.Root.Path.Value.Should().Be("vfs://");
25+
_vfs.Root.CreationTime.Should().BeCloseTo(DateTime.Now, TimeSpan.FromHours(1));
2426
}
2527

2628
[Fact]
2729
public void CreateDirectory_creates_a_directory_and_its_parents()
2830
{
2931
// Arrange
30-
var vfs = CreateVFS();
31-
VFSDirectoryPath directoryPath = new("dir1/dir2/dir3");
32+
_directoryPath = new("dir1/dir2/dir3");
3233

3334
// Act
34-
vfs.CreateDirectory(directoryPath);
35+
Act();
3536

3637
// Assert
37-
vfs.Index.RawIndex.Should().NotBeEmpty();
38-
vfs.Index.RawIndex.Should().HaveCount(3); // dir1 + dir2 + dir3
39-
vfs.Index.RawIndex.Should().ContainKey(directoryPath);
40-
vfs.Index.RawIndex.Should().ContainKey(new VFSDirectoryPath("vfs://dir1"));
41-
vfs.Index.RawIndex.Should().ContainKey(new VFSDirectoryPath("vfs://dir1/dir2"));
42-
vfs.Index.RawIndex.Should().ContainKey(new VFSDirectoryPath("vfs://dir1/dir2/dir3"));
38+
_vfs.Index.RawIndex.Should().NotBeEmpty();
39+
_vfs.Index.RawIndex.Should().HaveCount(3); // dir1 + dir2 + dir3
40+
_vfs.Index.RawIndex.Should().ContainKey(_directoryPath);
41+
_vfs.Index.RawIndex.Should().ContainKey(new VFSDirectoryPath("vfs://dir1"));
42+
_vfs.Index.RawIndex.Should().ContainKey(new VFSDirectoryPath("vfs://dir1/dir2"));
43+
_vfs.Index.RawIndex.Should().ContainKey(new VFSDirectoryPath("vfs://dir1/dir2/dir3"));
4344

44-
vfs.Index[new VFSDirectoryPath("vfs://dir1")].Should().BeAssignableTo<IDirectoryNode>();
45-
vfs.Index[new VFSDirectoryPath("vfs://dir1/dir2")].Should().BeAssignableTo<IDirectoryNode>();
46-
vfs.Index[new VFSDirectoryPath("vfs://dir1/dir2/dir3")].Should().BeAssignableTo<IDirectoryNode>();
45+
_vfs.Index[new VFSDirectoryPath("vfs://dir1")].Should().BeAssignableTo<IDirectoryNode>();
46+
_vfs.Index[new VFSDirectoryPath("vfs://dir1/dir2")].Should().BeAssignableTo<IDirectoryNode>();
47+
_vfs.Index[new VFSDirectoryPath("vfs://dir1/dir2/dir3")].Should().BeAssignableTo<IDirectoryNode>();
4748

48-
vfs.Index[new VFSDirectoryPath("vfs://dir1")].As<IDirectoryNode>().Directories.Should().NotBeEmpty();
49-
vfs.Index[new VFSDirectoryPath("vfs://dir1")].As<IDirectoryNode>().Directories.Should().HaveCount(1);
49+
_vfs.Index[new VFSDirectoryPath("vfs://dir1")].As<IDirectoryNode>().Directories.Should().NotBeEmpty();
50+
_vfs.Index[new VFSDirectoryPath("vfs://dir1")].As<IDirectoryNode>().Directories.Should().HaveCount(1);
5051
}
5152

5253
[Fact]
5354
public void CreateDirectory_throws_an_exception_if_the_directory_already_exists()
5455
{
5556
// Arrange
56-
var vfs = CreateVFS();
57-
var directoryPath = new VFSDirectoryPath("dir1");
58-
vfs.CreateDirectory(directoryPath);
57+
Act();
5958

6059
// Act
61-
Action action = () => vfs.CreateDirectory(directoryPath);
60+
var action = Act;
6261

6362
// Assert
6463
action.Should()
@@ -69,11 +68,8 @@ public void CreateDirectory_throws_an_exception_if_the_directory_already_exists(
6968
[Fact]
7069
public void CreateDirectory_throws_an_exception_if_the_path_is_the_root_directory()
7170
{
72-
// Arrange
73-
var vfs = CreateVFS();
74-
7571
// Act
76-
Action action = () => vfs.CreateDirectory(new VFSRootPath());
72+
Action action = () => _vfs.CreateDirectory(new VFSRootPath());
7773

7874
// Assert
7975
action.Should()
@@ -85,18 +81,16 @@ public void CreateDirectory_throws_an_exception_if_the_path_is_the_root_director
8581
public void CreateDirectory_raises_a_DirectoryCreated_event()
8682
{
8783
// Arrange
88-
var vfs = CreateVFS();
89-
VFSDirectoryPath directoryPath = new("dir1");
90-
bool eventRaised = false;
84+
var eventRaised = false;
9185

92-
vfs.DirectoryCreated += (args) =>
86+
_vfs.DirectoryCreated += args =>
9387
{
9488
eventRaised = true;
95-
args.Path.Should().Be(directoryPath);
89+
args.Path.Should().Be(_directoryPath);
9690
};
9791

9892
// Act
99-
vfs.CreateDirectory(directoryPath);
93+
Act();
10094

10195
// Assert
10296
eventRaised.Should().BeTrue();
@@ -105,19 +99,15 @@ public void CreateDirectory_raises_a_DirectoryCreated_event()
10599
[Fact]
106100
public void CreateDirectory_adds_a_change_to_the_ChangeHistory()
107101
{
108-
// Arrange
109-
var vfs = CreateVFS();
110-
var directoryPath = new VFSDirectoryPath("dir1");
111-
112102
// Act
113-
vfs.CreateDirectory(directoryPath);
103+
Act();
114104

115105
// Retrieve the change from the UndoStack
116-
var change = vfs.ChangeHistory.UndoStack.First();
106+
var change = _vfs.ChangeHistory.UndoStack.First();
117107

118108
// Assert
119-
vfs.ChangeHistory.UndoStack.Should().ContainEquivalentOf(change);
120-
vfs.ChangeHistory.UndoStack.Should().HaveCount(1);
121-
vfs.ChangeHistory.RedoStack.Should().BeEmpty();
109+
_vfs.ChangeHistory.UndoStack.Should().ContainEquivalentOf(change);
110+
_vfs.ChangeHistory.UndoStack.Should().HaveCount(1);
111+
_vfs.ChangeHistory.RedoStack.Should().BeEmpty();
122112
}
123113
}

tests/Atypical.VirtualFileSystem.UnitTests/SystemOperations/Commands/VirtualFileSystem_MethodCreateFile_Tests.cs

Lines changed: 36 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,57 +2,57 @@ namespace VirtualFileSystem.UnitTests.SystemOperations.Commands;
22

33
public class VirtualFileSystem_MethodCreateFile_Tests : VirtualFileSystemTestsBase
44
{
5+
private readonly IVirtualFileSystem _vfs = CreateVFS();
6+
private VFSFilePath _filePath = new("file.txt");
7+
8+
private void Act()
9+
=> _vfs.CreateFile(_filePath);
10+
511
[Fact]
612
public void CreateFile_creates_a_file()
713
{
8-
// Arrange
9-
var vfs = CreateVFS();
10-
VFSFilePath filePath = new("file.txt");
11-
1214
// Act
13-
vfs.CreateFile(filePath);
15+
Act();
1416

1517
// 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(new VFSFilePath("vfs://file.txt"));
20-
vfs.Root.Files.Should().NotBeEmpty();
21-
vfs.Root.Files.Should().HaveCount(1);
18+
_vfs.IsEmpty.Should().BeFalse();
19+
_vfs.Index.RawIndex.Should().NotBeEmpty();
20+
_vfs.Index.RawIndex.Should().HaveCount(1);
21+
_vfs.Index.RawIndex.Should().ContainKey(new VFSFilePath("vfs://file.txt"));
22+
_vfs.Root.Files.Should().NotBeEmpty();
23+
_vfs.Root.Files.Should().HaveCount(1);
2224
}
2325

2426
[Fact]
2527
public void CreateFile_creates_a_file_and_its_parents()
2628
{
2729
// Arrange
28-
var vfs = CreateVFS();
29-
VFSFilePath filePath = new("dir1/dir2/dir3/file.txt");
30+
_filePath = new("dir1/dir2/dir3/file.txt");
3031

3132
// Act
32-
vfs.CreateFile(filePath);
33+
Act();
3334

3435
// 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(new VFSDirectoryPath("vfs://dir1"));
39-
vfs.Index.RawIndex.Should().ContainKey(new VFSDirectoryPath("vfs://dir1/dir2"));
40-
vfs.Index.RawIndex.Should().ContainKey(new VFSDirectoryPath("vfs://dir1/dir2/dir3"));
41-
vfs.Index.RawIndex.Should().ContainKey(new VFSFilePath("vfs://dir1/dir2/dir3/file.txt"));
42-
vfs.Root.Directories.Should().NotBeEmpty();
43-
vfs.Root.Directories.Should().HaveCount(1);
36+
_vfs.IsEmpty.Should().BeFalse();
37+
_vfs.Index.RawIndex.Should().NotBeEmpty();
38+
_vfs.Index.RawIndex.Should().HaveCount(4); // dir1 + dir2 + dir3 + file.txt
39+
_vfs.Index.RawIndex.Should().ContainKey(new VFSDirectoryPath("vfs://dir1"));
40+
_vfs.Index.RawIndex.Should().ContainKey(new VFSDirectoryPath("vfs://dir1/dir2"));
41+
_vfs.Index.RawIndex.Should().ContainKey(new VFSDirectoryPath("vfs://dir1/dir2/dir3"));
42+
_vfs.Index.RawIndex.Should().ContainKey(new VFSFilePath("vfs://dir1/dir2/dir3/file.txt"));
43+
_vfs.Root.Directories.Should().NotBeEmpty();
44+
_vfs.Root.Directories.Should().HaveCount(1);
4445
}
4546

4647
[Fact]
4748
public void CreateFile_throws_an_exception_if_the_file_already_exists()
4849
{
4950
// Arrange
50-
var vfs = CreateVFS();
51-
var filePath = new VFSFilePath("dir1/dir2/dir3/file.txt");
52-
vfs.CreateFile(filePath);
51+
_filePath = new VFSFilePath("dir1/dir2/dir3/file.txt");
52+
Act();
5353

5454
// Act
55-
Action action = () => vfs.CreateFile(filePath);
55+
var action = Act;
5656

5757
// Assert
5858
action.Should()
@@ -64,18 +64,16 @@ public void CreateFile_throws_an_exception_if_the_file_already_exists()
6464
public void CreateFile_raises_a_FileCreated_event()
6565
{
6666
// Arrange
67-
var vfs = CreateVFS();
68-
VFSFilePath filePath = new("file.txt");
69-
bool eventRaised = false;
67+
var eventRaised = false;
7068

71-
vfs.FileCreated += args =>
69+
_vfs.FileCreated += args =>
7270
{
7371
eventRaised = true;
74-
args.Path.Should().Be(filePath);
72+
args.Path.Should().Be(_filePath);
7573
};
7674

7775
// Act
78-
vfs.CreateFile(filePath);
76+
Act();
7977

8078
// Assert
8179
eventRaised.Should().BeTrue();
@@ -84,19 +82,15 @@ public void CreateFile_raises_a_FileCreated_event()
8482
[Fact]
8583
public void CreateFile_adds_a_change_to_the_ChangeHistory()
8684
{
87-
// Arrange
88-
var vfs = CreateVFS();
89-
var filePath = new VFSFilePath("file.txt");
90-
9185
// Act
92-
vfs.CreateFile(filePath);
86+
Act();
9387

9488
// Retrieve the change from the UndoStack
95-
var change = vfs.ChangeHistory.UndoStack.First();
89+
var change = _vfs.ChangeHistory.UndoStack.First();
9690

9791
// Assert
98-
vfs.ChangeHistory.UndoStack.Should().ContainEquivalentOf(change);
99-
vfs.ChangeHistory.UndoStack.Should().HaveCount(1);
100-
vfs.ChangeHistory.RedoStack.Should().BeEmpty();
92+
_vfs.ChangeHistory.UndoStack.Should().ContainEquivalentOf(change);
93+
_vfs.ChangeHistory.UndoStack.Should().HaveCount(1);
94+
_vfs.ChangeHistory.RedoStack.Should().BeEmpty();
10195
}
10296
}

0 commit comments

Comments
 (0)