Skip to content

Commit d91ca3b

Browse files
committed
Fix tests
1 parent 721fbdc commit d91ca3b

8 files changed

Lines changed: 103 additions & 73 deletions

File tree

tests/ImageSort.UnitTests/Actions/MoveActionTests.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ public class MoveActionTests
1111
[Fact(DisplayName = "File gets moved correctly and caller gets notified of change.")]
1212
public void FileGetsMovedCorrectly()
1313
{
14-
const string oldPath = @"C:\SomeFile.png";
15-
const string newFolder = @"C:\SomeOtherFolder\SomeOtherFolderAsWell\";
16-
const string newPath = newFolder + "SomeFile.png";
14+
var oldPath = Path.GetFullPath("SomeFile.png");
15+
var newFolder = Path.GetFullPath("SomeOtherFolder");
16+
var newPath = Path.Combine(newFolder, "SomeFile.png");
1717

1818
var notifedOfAction = false;
1919
var notifiedOfReversion = false;
@@ -45,10 +45,10 @@ public void FileGetsMovedCorrectly()
4545
[Fact(DisplayName = "Handles file or directory not existing correctly.")]
4646
public void HandlesFileOrDirectoryNotExisting()
4747
{
48-
const string existingDirectory = @"C:\SomeRealDirectory";
49-
const string existingFile = @"C:\SomeRealFile.tif";
50-
const string fakeDirectory = @"C:\DirectoryThatDoesntExist";
51-
const string fakeFile = @"C:\SomeFakeFile.gif";
48+
var existingDirectory = Path.GetFullPath("SomeRealDirectory");
49+
var existingFile = Path.GetFullPath("SomeRealFile.tif");
50+
var fakeDirectory = Path.GetFullPath("DirectoryThatDoesntExist");
51+
var fakeFile = Path.GetFullPath("SomeFakeFile.gif");
5252

5353
var fsMock = Substitute.For<IFileSystem>();
5454

tests/ImageSort.UnitTests/Actions/RenameActionTests.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ public class RenameActionTests
1111
[Fact(DisplayName = "Can rename files and undo")]
1212
public void CanRenameFilesAndUndo()
1313
{
14-
const string oldPath = @"C:\my-image.png";
14+
var oldPath = Path.GetFullPath("my-image.png");
1515
const string newFileName = "my-renamed-image";
16-
const string newPath = @"C:\my-renamed-image.png";
16+
var newPath = Path.Combine(Path.GetDirectoryName(oldPath), "my-renamed-image.png");
1717

1818
var canAct = false;
1919
var canRevert = false;
@@ -41,12 +41,12 @@ public void CanRenameFilesAndUndo()
4141
[Fact(DisplayName = "Throws when the file doesn't exist or the renamed path is already used.")]
4242
public void ThrowsWhenFileDoesNotExistOrNewPathIsAlreadyUsed()
4343
{
44-
const string oldPath = @"C:\my-image.png";
45-
const string invalidOldPath = @"C:\invalid.gif";
44+
var oldPath = Path.GetFullPath("my-image.png");
45+
var invalidOldPath = Path.GetFullPath("invalid.gif");
4646
const string newFileName = "my-renamed-image";
47-
const string newPath = @"C:\my-renamed-image.png";
47+
var newPath = Path.Combine(Path.GetDirectoryName(oldPath), "my-renamed-image.png");
4848
const string alreadyExistingName = @"already-exists";
49-
const string alreadyExistingPath = @"C:\already-exists.png";
49+
var alreadyExistingPath = Path.Combine(Path.GetDirectoryName(oldPath), "already-exists.png");
5050

5151
var fsMock = Substitute.For<IFileSystem>();
5252

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System.Runtime.CompilerServices;
2+
using ReactiveUI;
3+
using ReactiveUI.Builder;
4+
5+
namespace ImageSort.UnitTests;
6+
7+
public static class ModuleInitializer
8+
{
9+
[ModuleInitializer]
10+
public static void Initialize()
11+
{
12+
RxAppBuilder.CreateReactiveUIBuilder()
13+
.WithCoreServices()
14+
.BuildApp();
15+
}
16+
}

tests/ImageSort.UnitTests/ViewModels/FolderTreeItemViewModelTests.cs

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,19 @@ namespace ImageSort.UnitTests.ViewModels;
1515
public class FolderTreeItemViewModelTests
1616
{
1717
[Fact(DisplayName = "Obtains the child folders of the current folder correctly")]
18-
public void ObtainsChildrenCorrectly()
18+
public async Task ObtainsChildrenCorrectly()
1919
{
20-
const string path = @"C:\current folder";
20+
var path = Path.GetFullPath("current_folder");
2121

2222
var resultingPaths =
2323
new[]
2424
{
25-
@"\folder 1",
26-
@"\folder 2",
27-
@"\folder 3"
25+
"folder 1",
26+
"folder 2",
27+
"folder 3"
2828
}
29-
.Select(sub => path + sub); // make the (mock) subfolders absolute paths.
29+
.Select(sub => Path.Combine(path, sub))
30+
.ToArray(); // make the (mock) subfolders absolute paths.
3031

3132
var fsMock = Substitute.For<IFileSystem>();
3233

@@ -38,18 +39,21 @@ public void ObtainsChildrenCorrectly()
3839
IsVisible = true
3940
};
4041

41-
fsMock.Received().GetSubFolders(path);
42+
while (folderTreeItem.Children.Count == 0)
43+
{
44+
await Task.Delay(1);
45+
}
4246

43-
while (folderTreeItem.Children.Count == 0) {}
47+
fsMock.Received().GetSubFolders(path);
4448

45-
Assert.Equal(resultingPaths, folderTreeItem.Children.Select(vm => vm.Path).ToArray());
49+
Assert.Equal(resultingPaths.OrderBy(p => p), folderTreeItem.Children.Select(vm => vm.Path).OrderBy(p => p));
4650
}
4751

4852
[Fact(DisplayName =
4953
"Handles an tried access to an unauthorized file (UnauthorizedAccessException) gracefully.")]
5054
public void HandlesUnauthorizedAccessExceptionGracefully()
5155
{
52-
const string pathToUnauthorisedFolder = @"C:\UnauthorizedFolder";
56+
var pathToUnauthorisedFolder = Path.GetFullPath("UnauthorizedFolder");
5357

5458
var fsMock = Substitute.For<IFileSystem>();
5559

@@ -64,12 +68,12 @@ public void HandlesUnauthorizedAccessExceptionGracefully()
6468
[Fact(DisplayName = "Can create folders and adds them to the children.")]
6569
public async Task CanCreateFolders()
6670
{
67-
const string currentFolder = @"C:\current_folder";
71+
var currentFolder = Path.GetFullPath("current_folder");
6872
var subfolders = new[]
6973
{
7074
"sub1", "sub2", "sub3"
7175
}.Select(s => Path.Combine(currentFolder, s));
72-
const string addedFolder = currentFolder + @"\new_ sub";
76+
var addedFolder = Path.Combine(currentFolder, "new_sub");
7377
var result = new List<string>();
7478
result.AddRange(subfolders);
7579
result.Add(addedFolder);
@@ -85,9 +89,9 @@ public async Task CanCreateFolders()
8589
IsVisible = true
8690
};
8791

88-
await folderTreeItem.CreateFolder.Execute(addedFolder);
92+
await folderTreeItem.CreateFolder.Execute("new_sub");
8993
// verify that no second folder is created when a folder already exists
90-
await folderTreeItem.CreateFolder.Execute(addedFolder);
94+
await folderTreeItem.CreateFolder.Execute("new_sub");
9195

9296
fsMock.Received().CreateFolder(addedFolder);
9397

@@ -97,16 +101,16 @@ public async Task CanCreateFolders()
97101
[Fact(DisplayName = "Do not load subfolders when not visible")]
98102
public void DoNotLoadSubfoldersWhenNotVisible()
99103
{
100-
const string path = @"C:\current folder";
104+
var path = Path.GetFullPath("current_folder");
101105

102106
var resultingPaths =
103107
new[]
104108
{
105-
@"\folder 1",
106-
@"\folder 2",
107-
@"\folder 3"
109+
"folder 1",
110+
"folder 2",
111+
"folder 3"
108112
}
109-
.Select(sub => path + sub); // make the (mock) subfolders absolute paths.
113+
.Select(sub => Path.Combine(path, sub)); // make the (mock) subfolders absolute paths.
110114

111115
var fsMock = Substitute.For<IFileSystem>();
112116

tests/ImageSort.UnitTests/ViewModels/FoldersViewModelTests.cs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.IO;
23
using System.Linq;
34
using System.Reactive.Linq;
45
using System.Threading.Tasks;
@@ -10,7 +11,7 @@
1011
namespace ImageSort.UnitTests.ViewModels;
1112
public class FoldersViewModelTests
1213
{
13-
private const string MockPath = @"C:\SomePath\";
14+
private static readonly string MockPath = Path.GetFullPath(@"C:\SomePath\");
1415
private readonly IFileSystem fileSystemMock;
1516

1617
public FoldersViewModelTests()
@@ -43,7 +44,7 @@ public void CurrentFolderCanBeChanged()
4344
[Fact(DisplayName = "Can prompt user to pin folders.")]
4445
public async Task CanPinFolders()
4546
{
46-
const string mockPathToPin = @"C:\SomeOtherPath\";
47+
var mockPathToPin = Path.GetFullPath(@"C:\SomeOtherPath\");
4748

4849
var fsMock = Substitute.For<IFileSystem>();
4950

@@ -65,7 +66,7 @@ public async Task CanPinFolders()
6566
[Fact(DisplayName = "Handles user canceling gracefully.")]
6667
public async Task HandlesCancelingGracefully()
6768
{
68-
const string mockPathToPin = @"C:\SomeOtherPath\";
69+
var mockPathToPin = Path.GetFullPath(@"C:\SomeOtherPath\");
6970

7071
var foldersVM = new FoldersViewModel
7172
{
@@ -81,7 +82,7 @@ public async Task HandlesCancelingGracefully()
8182
[Fact(DisplayName = "Can pin the selected folder.")]
8283
public async Task CanPinSelected()
8384
{
84-
const string mockPathToPin = @"C:\SomeOtherPath\";
85+
var mockPathToPin = Path.GetFullPath(@"C:\SomeOtherPath\");
8586

8687
var mockToPin = CreateMock(mockPathToPin);
8788

@@ -101,7 +102,7 @@ public async Task CanPinSelected()
101102
[Fact(DisplayName = "Can unpin the selected folder.")]
102103
public async Task CanUnpinSelected()
103104
{
104-
const string mockPathToPin = @"C:\SomeOtherPath\";
105+
var mockPathToPin = Path.GetFullPath(@"C:\SomeOtherPath\");
105106

106107
var fsMock = Substitute.For<IFileSystem>();
107108

@@ -161,7 +162,7 @@ public async Task MovesSelectedFolderUp()
161162
CurrentFolder = CreateMock(MockPath)
162163
};
163164

164-
var pinnedFolders = new[] { @"C:\folder 1", @"C:\folder 2", @"C:\folder 3" };
165+
var pinnedFolders = new[] { Path.GetFullPath("folder 1"), Path.GetFullPath("folder 2"), Path.GetFullPath("folder 3") };
165166

166167
foreach (var pinnedFolder in pinnedFolders)
167168
{
@@ -181,6 +182,8 @@ public async Task MovesSelectedFolderUp()
181182

182183
await foldersVM.MoveSelectedPinnedFolderUp.Execute();
183184

185+
await Task.Delay(1); // allow bound collection to update
186+
184187
Assert.Equal(selected, foldersVM.PinnedFolders.ElementAt(0));
185188
}
186189

@@ -192,7 +195,7 @@ public async Task MovesSelectedFolderDown()
192195
CurrentFolder = CreateMock(MockPath)
193196
};
194197

195-
var pinnedFolders = new[] { @"C:\folder 1", @"C:\folder 2", @"C:\folder 3" };
198+
var pinnedFolders = new[] { Path.GetFullPath("folder 1"), Path.GetFullPath("folder 2"), Path.GetFullPath("folder 3") };
196199

197200
foreach (var pinnedFolder in pinnedFolders)
198201
{
@@ -212,6 +215,8 @@ public async Task MovesSelectedFolderDown()
212215

213216
await foldersVM.MoveSelectedPinnedFolderDown.Execute();
214217

218+
await Task.Delay(1); // allow bound collection to update
219+
215220
Assert.Equal(selected, foldersVM.PinnedFolders.ElementAt(2));
216221
}
217222
}

tests/ImageSort.UnitTests/ViewModels/ImagesViewModelTests.cs

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
24
using System.Linq;
35
using System.Reactive;
46
using System.Reactive.Linq;
@@ -15,9 +17,9 @@ public class ImagesViewModelTests
1517
[Fact(DisplayName = "Gets the files correctly, ignoring unsupported files.")]
1618
public void GetTheFilesCorrectly()
1719
{
18-
const string basePath = @"C:\";
20+
var basePath = Path.GetFullPath(@"C:\");
1921
var allFiles = new[] {"image.png", "some.gif", "cat.mp4", "somethingwrong.exe"}
20-
.Select(f => basePath + f);
22+
.Select(f => Path.Combine(basePath, f));
2123

2224
var expectedFiles = allFiles.Where(f =>
2325
f.EndsWith(".png", StringComparison.OrdinalIgnoreCase)
@@ -40,9 +42,9 @@ public void GetTheFilesCorrectly()
4042
[Fact(DisplayName = "Selected image is accessible by index and gives out the correct path.")]
4143
public void SelectedImageWorksCorrectly()
4244
{
43-
const string basePath = @"C:\";
45+
var basePath = Path.GetFullPath(@"C:\");
4446
var allFiles = new[] {"image.png", "some.gif"}
45-
.Select(f => basePath + f);
47+
.Select(f => Path.Combine(basePath, f));
4648

4749
var fsMock = Substitute.For<IFileSystem>();
4850

@@ -65,9 +67,9 @@ public void SelectedImageWorksCorrectly()
6567
[Fact(DisplayName = "Can remove and add images externally")]
6668
public void CanRemoveAndAddImagesExternally()
6769
{
68-
const string basePath = @"C:\";
70+
var basePath = Path.GetFullPath(@"C:\");
6971
var allFiles = new[] {"image.png", "some.gif"}
70-
.Select(f => basePath + f);
72+
.Select(f => Path.Combine(basePath, f));
7173

7274
var fsMock = Substitute.For<IFileSystem>();
7375

@@ -94,9 +96,9 @@ public void CanRemoveAndAddImagesExternally()
9496
[Fact(DisplayName = "Search filter works")]
9597
public void SearchFilterWorks()
9698
{
97-
const string basePath = @"C:\";
99+
var basePath = Path.GetFullPath("search_test");
98100
var allFiles = new[] {"image.png", "some.gif"}
99-
.Select(f => basePath + f);
101+
.Select(f => Path.Combine(basePath, f));
100102

101103
var fsMock = Substitute.For<IFileSystem>();
102104

@@ -107,7 +109,7 @@ public void SearchFilterWorks()
107109
CurrentFolder = basePath
108110
};
109111

110-
imagesVM.SearchTerm = "image";
112+
imagesVM.SearchTerm = "image.png";
111113

112114
Assert.DoesNotContain(allFiles.ElementAt(1), imagesVM.Images);
113115

@@ -117,12 +119,12 @@ public void SearchFilterWorks()
117119
[Fact(DisplayName = "Can rename images")]
118120
public async Task CanRenameImages()
119121
{
120-
const string basePath = @"C:\";
121-
const string oldFilePath = basePath + "image.png";
122+
var basePath = Path.GetFullPath("rename_test");
123+
var oldFilePath = Path.Combine(basePath, "image.png");
122124
const string newFileName = "other_image";
123125
var invalidFileNames = new[] {@"image\ima", "im/age", "imag\n", "imag\t"};
124126
var promptedFileName = newFileName;
125-
const string newFilePath = basePath + newFileName + ".png";
127+
var newFilePath = Path.Combine(basePath, newFileName + ".png");
126128
var allFiles = new[] {oldFilePath};
127129
var allFilesResulting = new[] {newFilePath};
128130

@@ -160,15 +162,15 @@ public async Task CanRenameImages()
160162

161163
await Task.Delay(1);
162164

163-
Assert.Equal(allFilesResulting, imagesVM.Images);
165+
Assert.Equal(allFilesResulting, imagesVM.Images.ToArray());
164166

165167
foreach (var invalidFileName in invalidFileNames)
166168
{
167169
promptedFileName = invalidFileName;
168170

169171
await imagesVM.RenameImage.Execute();
170172

171-
Assert.Equal(allFilesResulting, imagesVM.Images);
173+
Assert.Equal(allFilesResulting, imagesVM.Images.ToArray());
172174
}
173175

174176
Assert.True(notifiesUserOfError);

0 commit comments

Comments
 (0)