Skip to content

Commit 7c58521

Browse files
authored
feat: add assertion for NotExist (#50)
Add assertions for `IFileInfo` and `IDirectoryInfo` to `NotExist`, which checks, that the `Exists` flag is `false`.
1 parent 5be500d commit 7c58521

File tree

2 files changed

+152
-4
lines changed

2 files changed

+152
-4
lines changed

Source/Testably.Abstractions.FluentAssertions/FileSystemInfoAssertions.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,25 @@ public AndConstraint<TFileSystemInfo> Exist(
3636

3737
return new AndConstraint<TFileSystemInfo>(Subject!);
3838
}
39+
40+
/// <summary>
41+
/// Asserts that the current file or directory does not exist.
42+
/// </summary>
43+
public AndConstraint<TFileSystemInfo> NotExist(
44+
string because = "", params object[] becauseArgs)
45+
{
46+
Execute.Assertion
47+
.WithDefaultIdentifier(Identifier)
48+
.BecauseOf(because, becauseArgs)
49+
.ForCondition(Subject != null)
50+
.FailWith("You can't assert that the {context} does not exist if it is null.")
51+
.Then
52+
.Given(() => Subject!)
53+
.ForCondition(fileSystemInfo => !fileSystemInfo.Exists)
54+
.FailWith(
55+
"Expected {context} {0} not to exist{reason}, but it did.",
56+
fileSystemInfo => fileSystemInfo.Name);
57+
58+
return new AndConstraint<TFileSystemInfo>(Subject!);
59+
}
3960
}

Tests/Testably.Abstractions.FluentAssertions.Tests/FileSystemInfoAssertionsTests.cs

Lines changed: 131 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public void Exist_ForDirectoryInfo_Null_ShouldThrow(string because)
2727

2828
[Theory]
2929
[AutoData]
30-
public void Exist_ForDirectoryInfo_WithCorrectDirectory_ShouldNotThrow(string directoryName)
30+
public void Exist_ForDirectoryInfo_WithExistingDirectory_ShouldNotThrow(string directoryName)
3131
{
3232
MockFileSystem fileSystem = new();
3333
fileSystem.Initialize()
@@ -40,7 +40,7 @@ public void Exist_ForDirectoryInfo_WithCorrectDirectory_ShouldNotThrow(string di
4040

4141
[Theory]
4242
[AutoData]
43-
public void Exist_ForDirectoryInfo_WithoutCorrectDirectory_ShouldThrow(
43+
public void Exist_ForDirectoryInfo_WithoutExistingDirectory_ShouldThrow(
4444
string directoryName,
4545
string because)
4646
{
@@ -97,7 +97,7 @@ public void Exist_ForFileInfo_Null_ShouldThrow(string because)
9797

9898
[Theory]
9999
[AutoData]
100-
public void Exist_ForFileInfo_WithCorrectFile_ShouldNotThrow(string fileName)
100+
public void Exist_ForFileInfo_WithExistingFile_ShouldNotThrow(string fileName)
101101
{
102102
MockFileSystem fileSystem = new();
103103
fileSystem.Initialize()
@@ -110,7 +110,7 @@ public void Exist_ForFileInfo_WithCorrectFile_ShouldNotThrow(string fileName)
110110

111111
[Theory]
112112
[AutoData]
113-
public void Exist_ForFileInfo_WithoutCorrectFile_ShouldThrow(
113+
public void Exist_ForFileInfo_WithoutExistingFile_ShouldThrow(
114114
string fileName,
115115
string because)
116116
{
@@ -148,4 +148,131 @@ public void Exist_ForFileInfo_WithSameDirectory_ShouldThrow(
148148
exception!.Message.Should()
149149
.Be($"Expected file \"{fileName}\" to exist {because}, but it did not.");
150150
}
151+
152+
[Theory]
153+
[AutoData]
154+
public void NotExist_ForDirectoryInfo_Null_ShouldThrow(string because)
155+
{
156+
IDirectoryInfo? sut = null;
157+
158+
Exception? exception = Record.Exception(() =>
159+
{
160+
sut.Should().NotExist(because);
161+
});
162+
163+
exception.Should().NotBeNull();
164+
exception!.Message.Should().Contain("null");
165+
exception.Message.Should().NotContain(because);
166+
}
167+
168+
[Theory]
169+
[AutoData]
170+
public void NotExist_ForDirectoryInfo_WithExistingDirectory_ShouldThrow(
171+
string directoryName,
172+
string because)
173+
{
174+
MockFileSystem fileSystem = new();
175+
fileSystem.Initialize()
176+
.WithSubdirectories(directoryName);
177+
178+
IDirectoryInfo sut = fileSystem.DirectoryInfo.New(directoryName);
179+
180+
Exception? exception = Record.Exception(() =>
181+
{
182+
sut.Should().NotExist(because);
183+
});
184+
185+
exception.Should().NotBeNull();
186+
exception!.Message.Should()
187+
.Be($"Expected directory \"{directoryName}\" not to exist {because}, but it did.");
188+
}
189+
190+
[Theory]
191+
[AutoData]
192+
public void NotExist_ForDirectoryInfo_WithoutExistingDirectory_ShouldNotThrow(
193+
string directoryName)
194+
{
195+
MockFileSystem fileSystem = new();
196+
fileSystem.Initialize();
197+
IDirectoryInfo sut = fileSystem.DirectoryInfo.New(directoryName);
198+
199+
sut.Should().NotExist();
200+
}
201+
202+
[Theory]
203+
[AutoData]
204+
public void NotExist_ForDirectoryInfo_WithSameFile_ShouldNotThrow(
205+
string fileName,
206+
string because)
207+
{
208+
MockFileSystem fileSystem = new();
209+
fileSystem.Initialize();
210+
fileSystem.File.WriteAllText(fileName, "some content");
211+
IDirectoryInfo sut = fileSystem.DirectoryInfo.New(fileName);
212+
213+
sut.Should().NotExist(because);
214+
}
215+
216+
[Theory]
217+
[AutoData]
218+
public void NotExist_ForFileInfo_Null_ShouldThrow(string because)
219+
{
220+
IFileInfo? sut = null;
221+
222+
Exception? exception = Record.Exception(() =>
223+
{
224+
sut.Should().NotExist(because);
225+
});
226+
227+
exception.Should().NotBeNull();
228+
exception!.Message.Should().Contain("null");
229+
exception.Message.Should().NotContain(because);
230+
}
231+
232+
[Theory]
233+
[AutoData]
234+
public void NotExist_ForFileInfo_WithExistingFile_ShouldThrow(
235+
string fileName,
236+
string because)
237+
{
238+
MockFileSystem fileSystem = new();
239+
fileSystem.Initialize()
240+
.WithFile(fileName);
241+
IFileInfo sut = fileSystem.FileInfo.New(fileName);
242+
243+
Exception? exception = Record.Exception(() =>
244+
{
245+
sut.Should().NotExist(because);
246+
});
247+
248+
exception.Should().NotBeNull();
249+
exception!.Message.Should()
250+
.Be($"Expected file \"{fileName}\" not to exist {because}, but it did.");
251+
}
252+
253+
[Theory]
254+
[AutoData]
255+
public void NotExist_ForFileInfo_WithoutExistingFile_ShouldNotThrow(string fileName)
256+
{
257+
MockFileSystem fileSystem = new();
258+
fileSystem.Initialize();
259+
260+
IFileInfo sut = fileSystem.FileInfo.New(fileName);
261+
262+
sut.Should().NotExist();
263+
}
264+
265+
[Theory]
266+
[AutoData]
267+
public void NotExist_ForFileInfo_WithSameDirectory_ShouldNotThrow(
268+
string fileName,
269+
string because)
270+
{
271+
MockFileSystem fileSystem = new();
272+
fileSystem.Initialize();
273+
fileSystem.Directory.CreateDirectory(fileName);
274+
IFileInfo sut = fileSystem.FileInfo.New(fileName);
275+
276+
sut.Should().NotExist(because);
277+
}
151278
}

0 commit comments

Comments
 (0)