Skip to content

Commit 5da01b6

Browse files
authored
refactor: simplify assertion names (#44)
Simplify and consolidate the assertion names, e.g. - `HasSingleDirectory` -> `HasDirectory` - `HasDirectoriesMatching` -> `HasDirectories` - `HasSingleFile` -> `HasFile` - `HasFilesMatching` -> `HasFiles`
1 parent 93fa106 commit 5da01b6

File tree

5 files changed

+228
-228
lines changed

5 files changed

+228
-228
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ This library is an extension to [FluentAssertions](https://github.com/fluentasse
2929
or
3030
```csharp
3131
IDirectoryInfo directoryInfo = fileSystem.DirectoryInfo.New(".");
32-
directoryInfo.Should().HaveSingleDirectory("foo");
32+
directoryInfo.Should().HaveDirectory("foo");
3333
```
3434

3535
3. Verify, that the file "foo.txt" has text content "bar":

Source/Testably.Abstractions.FluentAssertions/DirectoryAssertions.cs

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ internal DirectoryAssertions(IDirectoryInfo? instance)
2020
/// Asserts that the current directory has at least <paramref name="minimumCount" /> directories which match the
2121
/// <paramref name="searchPattern" />.
2222
/// </summary>
23-
public AndConstraint<DirectoryAssertions> HasDirectoriesMatching(
24-
string searchPattern = "*",
25-
int minimumCount = 1,
23+
public AndConstraint<DirectoryAssertions> HasDirectories(
24+
string searchPattern,
25+
int minimumCount,
2626
string because = "",
2727
params object[] becauseArgs)
2828
{
@@ -52,109 +52,109 @@ public AndConstraint<DirectoryAssertions> HasDirectoriesMatching(
5252
/// <summary>
5353
/// Asserts that the current directory has at least one directory which matches the <paramref name="searchPattern" />.
5454
/// </summary>
55-
public AndConstraint<DirectoryAssertions> HasDirectoryMatching(
55+
public AndConstraint<DirectoryAssertions> HasDirectories(
5656
string searchPattern = "*", string because = "", params object[] becauseArgs)
57-
=> HasDirectoriesMatching(searchPattern, 1, because, becauseArgs);
57+
=> HasDirectories(searchPattern, 1, because, becauseArgs);
5858

5959
/// <summary>
60-
/// Asserts that the current directory has at least one file which matches the <paramref name="searchPattern" />.
60+
/// Asserts that the directory contains exactly one directory matching the given <paramref name="searchPattern" />.
6161
/// </summary>
62-
public AndConstraint<DirectoryAssertions> HasFileMatching(
62+
public AndWhichConstraint<FileSystemAssertions, DirectoryAssertions> HasDirectory(
6363
string searchPattern = "*", string because = "", params object[] becauseArgs)
64-
=> HasFilesMatching(searchPattern, 1, because, becauseArgs);
65-
66-
/// <summary>
67-
/// Asserts that the current directory has at least <paramref name="minimumCount" /> files which match the
68-
/// <paramref name="searchPattern" />.
69-
/// </summary>
70-
public AndConstraint<DirectoryAssertions> HasFilesMatching(
71-
string searchPattern = "*",
72-
int minimumCount = 1,
73-
string because = "",
74-
params object[] becauseArgs)
7564
{
7665
Execute.Assertion
7766
.WithDefaultIdentifier(Identifier)
7867
.BecauseOf(because, becauseArgs)
7968
.ForCondition(Subject != null)
8069
.FailWith(
81-
"You can't assert a directory having files if the DirectoryInfo is null.")
70+
"You can't assert a directory having a given directory if it is null.")
8271
.Then
8372
.ForCondition(!string.IsNullOrEmpty(searchPattern))
8473
.FailWith(
85-
"You can't assert a directory having files if you don't pass a proper search pattern.")
74+
"You can't assert a directory having a given directory if you don't pass a proper search pattern.")
8675
.Then
8776
.Given(() => Subject!)
8877
.ForCondition(directoryInfo
89-
=> directoryInfo.GetFiles(searchPattern).Length >= minimumCount)
78+
=> directoryInfo.GetDirectories(searchPattern).Length == 1)
9079
.FailWith(
91-
$"Expected {{context}} {{1}} to contain at least {(minimumCount == 1 ? "one file" : $"{minimumCount} files")} matching {{0}}{{reason}}, but {(minimumCount == 1 ? "none was" : "only {2} were")} found.",
80+
"Expected {context} {1} to contain exactly one directory matching {0}{reason}, but found {2}.",
9281
_ => searchPattern,
9382
directoryInfo => directoryInfo.Name,
94-
directoryInfo => directoryInfo.GetFiles(searchPattern).Length);
83+
directoryInfo => directoryInfo.GetDirectories(searchPattern).Length);
9584

96-
return new AndConstraint<DirectoryAssertions>(this);
85+
return new AndWhichConstraint<FileSystemAssertions, DirectoryAssertions>(
86+
new FileSystemAssertions(Subject!.FileSystem),
87+
new DirectoryAssertions(Subject!.GetDirectories(searchPattern).Single()));
9788
}
9889

9990
/// <summary>
100-
/// Asserts that the directory contains exactly one directory matching the given <paramref name="searchPattern" />.
91+
/// Asserts that the directory contains exactly one file matching the given <paramref name="searchPattern" />.
10192
/// </summary>
102-
public AndWhichConstraint<FileSystemAssertions, DirectoryAssertions> HasSingleDirectoryMatching(
93+
public AndWhichConstraint<FileSystemAssertions, FileAssertions> HasFile(
10394
string searchPattern = "*", string because = "", params object[] becauseArgs)
10495
{
10596
Execute.Assertion
10697
.WithDefaultIdentifier(Identifier)
10798
.BecauseOf(because, becauseArgs)
10899
.ForCondition(Subject != null)
109100
.FailWith(
110-
"You can't assert a directory having a given directory if it is null.")
101+
"You can't assert a directory having a given file if it is null.")
111102
.Then
112103
.ForCondition(!string.IsNullOrEmpty(searchPattern))
113104
.FailWith(
114-
"You can't assert a directory having a given directory if you don't pass a proper search pattern.")
105+
"You can't assert a directory having a given file if you don't pass a proper search pattern.")
115106
.Then
116107
.Given(() => Subject!)
117108
.ForCondition(directoryInfo
118-
=> directoryInfo.GetDirectories(searchPattern).Length == 1)
109+
=> directoryInfo.GetFiles(searchPattern).Length == 1)
119110
.FailWith(
120-
"Expected {context} {1} to contain exactly one directory matching {0}{reason}, but found {2}.",
111+
"Expected {context} {1} to contain exactly one file matching {0}{reason}, but found {2}.",
121112
_ => searchPattern,
122113
directoryInfo => directoryInfo.Name,
123-
directoryInfo => directoryInfo.GetDirectories(searchPattern).Length);
114+
directoryInfo => directoryInfo.GetFiles(searchPattern).Length);
124115

125-
return new AndWhichConstraint<FileSystemAssertions, DirectoryAssertions>(
116+
return new AndWhichConstraint<FileSystemAssertions, FileAssertions>(
126117
new FileSystemAssertions(Subject!.FileSystem),
127-
new DirectoryAssertions(Subject!.GetDirectories(searchPattern).Single()));
118+
new FileAssertions(Subject!.GetFiles(searchPattern).Single()));
128119
}
129120

130121
/// <summary>
131-
/// Asserts that the directory contains exactly one file matching the given <paramref name="searchPattern" />.
122+
/// Asserts that the current directory has at least one file which matches the <paramref name="searchPattern" />.
132123
/// </summary>
133-
public AndWhichConstraint<FileSystemAssertions, FileAssertions> HasSingleFileMatching(
124+
public AndConstraint<DirectoryAssertions> HasFiles(
134125
string searchPattern = "*", string because = "", params object[] becauseArgs)
126+
=> HasFiles(searchPattern, 1, because, becauseArgs);
127+
128+
/// <summary>
129+
/// Asserts that the current directory has at least <paramref name="minimumCount" /> files which match the
130+
/// <paramref name="searchPattern" />.
131+
/// </summary>
132+
public AndConstraint<DirectoryAssertions> HasFiles(
133+
string searchPattern,
134+
int minimumCount,
135+
string because = "",
136+
params object[] becauseArgs)
135137
{
136138
Execute.Assertion
137139
.WithDefaultIdentifier(Identifier)
138140
.BecauseOf(because, becauseArgs)
139141
.ForCondition(Subject != null)
140142
.FailWith(
141-
"You can't assert a directory having a given file if it is null.")
143+
"You can't assert a directory having files if the DirectoryInfo is null.")
142144
.Then
143145
.ForCondition(!string.IsNullOrEmpty(searchPattern))
144146
.FailWith(
145-
"You can't assert a directory having a given file if you don't pass a proper search pattern.")
147+
"You can't assert a directory having files if you don't pass a proper search pattern.")
146148
.Then
147149
.Given(() => Subject!)
148150
.ForCondition(directoryInfo
149-
=> directoryInfo.GetFiles(searchPattern).Length == 1)
151+
=> directoryInfo.GetFiles(searchPattern).Length >= minimumCount)
150152
.FailWith(
151-
"Expected {context} {1} to contain exactly one file matching {0}{reason}, but found {2}.",
153+
$"Expected {{context}} {{1}} to contain at least {(minimumCount == 1 ? "one file" : $"{minimumCount} files")} matching {{0}}{{reason}}, but {(minimumCount == 1 ? "none was" : "only {2} were")} found.",
152154
_ => searchPattern,
153155
directoryInfo => directoryInfo.Name,
154156
directoryInfo => directoryInfo.GetFiles(searchPattern).Length);
155157

156-
return new AndWhichConstraint<FileSystemAssertions, FileAssertions>(
157-
new FileSystemAssertions(Subject!.FileSystem),
158-
new FileAssertions(Subject!.GetFiles(searchPattern).Single()));
158+
return new AndConstraint<DirectoryAssertions>(this);
159159
}
160160
}

Source/Testably.Abstractions.FluentAssertions/DirectoryInfoAssertions.cs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,40 +17,40 @@ internal DirectoryInfoAssertions(IDirectoryInfo? instance)
1717
/// <summary>
1818
/// Asserts that the current directory has at least one directory which matches the <paramref name="searchPattern" />.
1919
/// </summary>
20-
public AndConstraint<DirectoryInfoAssertions> HaveDirectoryMatching(
20+
public AndConstraint<DirectoryInfoAssertions> HaveDirectories(
2121
string searchPattern = "*", string because = "", params object[] becauseArgs)
2222
{
23-
new DirectoryAssertions(Subject).HasDirectoryMatching(searchPattern, because, becauseArgs);
23+
new DirectoryAssertions(Subject).HasDirectories(searchPattern, because, becauseArgs);
2424
return new AndConstraint<DirectoryInfoAssertions>(this);
2525
}
2626

2727
/// <summary>
28-
/// Asserts that the current directory has at least one file which matches the <paramref name="searchPattern" />.
28+
/// Asserts that the directory contains exactly one directory matching the given <paramref name="searchPattern" />.
2929
/// </summary>
30-
public AndConstraint<DirectoryInfoAssertions> HaveFileMatching(
30+
public AndWhichConstraint<FileSystemAssertions, DirectoryAssertions> HaveDirectory(
3131
string searchPattern = "*", string because = "", params object[] becauseArgs)
3232
{
33-
new DirectoryAssertions(Subject).HasFileMatching(searchPattern, because, becauseArgs);
34-
return new AndConstraint<DirectoryInfoAssertions>(this);
33+
return new DirectoryAssertions(Subject).HasDirectory(searchPattern, because,
34+
becauseArgs);
3535
}
3636

3737
/// <summary>
38-
/// Asserts that the directory contains exactly one directory matching the given <paramref name="searchPattern" />.
38+
/// Asserts that the directory contains exactly one file matching the given <paramref name="searchPattern" />.
3939
/// </summary>
40-
public AndWhichConstraint<FileSystemAssertions, DirectoryAssertions> HaveSingleDirectory(
40+
public AndWhichConstraint<FileSystemAssertions, FileAssertions> HaveFile(
4141
string searchPattern = "*", string because = "", params object[] becauseArgs)
4242
{
43-
return new DirectoryAssertions(Subject).HasSingleDirectoryMatching(searchPattern, because,
43+
return new DirectoryAssertions(Subject).HasFile(searchPattern, because,
4444
becauseArgs);
4545
}
4646

4747
/// <summary>
48-
/// Asserts that the directory contains exactly one file matching the given <paramref name="searchPattern" />.
48+
/// Asserts that the current directory has at least one file which matches the <paramref name="searchPattern" />.
4949
/// </summary>
50-
public AndWhichConstraint<FileSystemAssertions, FileAssertions> HaveSingleFile(
50+
public AndConstraint<DirectoryInfoAssertions> HaveFiles(
5151
string searchPattern = "*", string because = "", params object[] becauseArgs)
5252
{
53-
return new DirectoryAssertions(Subject).HasSingleFileMatching(searchPattern, because,
54-
becauseArgs);
53+
new DirectoryAssertions(Subject).HasFiles(searchPattern, because, becauseArgs);
54+
return new AndConstraint<DirectoryInfoAssertions>(this);
5555
}
5656
}

0 commit comments

Comments
 (0)