Skip to content

Commit

Permalink
refactor: simplify assertion names (#44)
Browse files Browse the repository at this point in the history
Simplify and consolidate the assertion names, e.g.
- `HasSingleDirectory` -> `HasDirectory`
- `HasDirectoriesMatching` -> `HasDirectories`
- `HasSingleFile` -> `HasFile`
- `HasFilesMatching` -> `HasFiles`
  • Loading branch information
vbreuss authored Sep 1, 2023
1 parent 93fa106 commit 5da01b6
Show file tree
Hide file tree
Showing 5 changed files with 228 additions and 228 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ This library is an extension to [FluentAssertions](https://github.com/fluentasse
or
```csharp
IDirectoryInfo directoryInfo = fileSystem.DirectoryInfo.New(".");
directoryInfo.Should().HaveSingleDirectory("foo");
directoryInfo.Should().HaveDirectory("foo");
```

3. Verify, that the file "foo.txt" has text content "bar":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ internal DirectoryAssertions(IDirectoryInfo? instance)
/// Asserts that the current directory has at least <paramref name="minimumCount" /> directories which match the
/// <paramref name="searchPattern" />.
/// </summary>
public AndConstraint<DirectoryAssertions> HasDirectoriesMatching(
string searchPattern = "*",
int minimumCount = 1,
public AndConstraint<DirectoryAssertions> HasDirectories(
string searchPattern,
int minimumCount,
string because = "",
params object[] becauseArgs)
{
Expand Down Expand Up @@ -52,109 +52,109 @@ public AndConstraint<DirectoryAssertions> HasDirectoriesMatching(
/// <summary>
/// Asserts that the current directory has at least one directory which matches the <paramref name="searchPattern" />.
/// </summary>
public AndConstraint<DirectoryAssertions> HasDirectoryMatching(
public AndConstraint<DirectoryAssertions> HasDirectories(
string searchPattern = "*", string because = "", params object[] becauseArgs)
=> HasDirectoriesMatching(searchPattern, 1, because, becauseArgs);
=> HasDirectories(searchPattern, 1, because, becauseArgs);

/// <summary>
/// Asserts that the current directory has at least one file which matches the <paramref name="searchPattern" />.
/// Asserts that the directory contains exactly one directory matching the given <paramref name="searchPattern" />.
/// </summary>
public AndConstraint<DirectoryAssertions> HasFileMatching(
public AndWhichConstraint<FileSystemAssertions, DirectoryAssertions> HasDirectory(
string searchPattern = "*", string because = "", params object[] becauseArgs)
=> HasFilesMatching(searchPattern, 1, because, becauseArgs);

/// <summary>
/// Asserts that the current directory has at least <paramref name="minimumCount" /> files which match the
/// <paramref name="searchPattern" />.
/// </summary>
public AndConstraint<DirectoryAssertions> HasFilesMatching(
string searchPattern = "*",
int minimumCount = 1,
string because = "",
params object[] becauseArgs)
{
Execute.Assertion
.WithDefaultIdentifier(Identifier)
.BecauseOf(because, becauseArgs)
.ForCondition(Subject != null)
.FailWith(
"You can't assert a directory having files if the DirectoryInfo is null.")
"You can't assert a directory having a given directory if it is null.")
.Then
.ForCondition(!string.IsNullOrEmpty(searchPattern))
.FailWith(
"You can't assert a directory having files if you don't pass a proper search pattern.")
"You can't assert a directory having a given directory if you don't pass a proper search pattern.")
.Then
.Given(() => Subject!)
.ForCondition(directoryInfo
=> directoryInfo.GetFiles(searchPattern).Length >= minimumCount)
=> directoryInfo.GetDirectories(searchPattern).Length == 1)
.FailWith(
$"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.",
"Expected {context} {1} to contain exactly one directory matching {0}{reason}, but found {2}.",
_ => searchPattern,
directoryInfo => directoryInfo.Name,
directoryInfo => directoryInfo.GetFiles(searchPattern).Length);
directoryInfo => directoryInfo.GetDirectories(searchPattern).Length);

return new AndConstraint<DirectoryAssertions>(this);
return new AndWhichConstraint<FileSystemAssertions, DirectoryAssertions>(
new FileSystemAssertions(Subject!.FileSystem),
new DirectoryAssertions(Subject!.GetDirectories(searchPattern).Single()));
}

/// <summary>
/// Asserts that the directory contains exactly one directory matching the given <paramref name="searchPattern" />.
/// Asserts that the directory contains exactly one file matching the given <paramref name="searchPattern" />.
/// </summary>
public AndWhichConstraint<FileSystemAssertions, DirectoryAssertions> HasSingleDirectoryMatching(
public AndWhichConstraint<FileSystemAssertions, FileAssertions> HasFile(
string searchPattern = "*", string because = "", params object[] becauseArgs)
{
Execute.Assertion
.WithDefaultIdentifier(Identifier)
.BecauseOf(because, becauseArgs)
.ForCondition(Subject != null)
.FailWith(
"You can't assert a directory having a given directory if it is null.")
"You can't assert a directory having a given file if it is null.")
.Then
.ForCondition(!string.IsNullOrEmpty(searchPattern))
.FailWith(
"You can't assert a directory having a given directory if you don't pass a proper search pattern.")
"You can't assert a directory having a given file if you don't pass a proper search pattern.")
.Then
.Given(() => Subject!)
.ForCondition(directoryInfo
=> directoryInfo.GetDirectories(searchPattern).Length == 1)
=> directoryInfo.GetFiles(searchPattern).Length == 1)
.FailWith(
"Expected {context} {1} to contain exactly one directory matching {0}{reason}, but found {2}.",
"Expected {context} {1} to contain exactly one file matching {0}{reason}, but found {2}.",
_ => searchPattern,
directoryInfo => directoryInfo.Name,
directoryInfo => directoryInfo.GetDirectories(searchPattern).Length);
directoryInfo => directoryInfo.GetFiles(searchPattern).Length);

return new AndWhichConstraint<FileSystemAssertions, DirectoryAssertions>(
return new AndWhichConstraint<FileSystemAssertions, FileAssertions>(
new FileSystemAssertions(Subject!.FileSystem),
new DirectoryAssertions(Subject!.GetDirectories(searchPattern).Single()));
new FileAssertions(Subject!.GetFiles(searchPattern).Single()));
}

/// <summary>
/// Asserts that the directory contains exactly one file matching the given <paramref name="searchPattern" />.
/// Asserts that the current directory has at least one file which matches the <paramref name="searchPattern" />.
/// </summary>
public AndWhichConstraint<FileSystemAssertions, FileAssertions> HasSingleFileMatching(
public AndConstraint<DirectoryAssertions> HasFiles(
string searchPattern = "*", string because = "", params object[] becauseArgs)
=> HasFiles(searchPattern, 1, because, becauseArgs);

/// <summary>
/// Asserts that the current directory has at least <paramref name="minimumCount" /> files which match the
/// <paramref name="searchPattern" />.
/// </summary>
public AndConstraint<DirectoryAssertions> HasFiles(
string searchPattern,
int minimumCount,
string because = "",
params object[] becauseArgs)
{
Execute.Assertion
.WithDefaultIdentifier(Identifier)
.BecauseOf(because, becauseArgs)
.ForCondition(Subject != null)
.FailWith(
"You can't assert a directory having a given file if it is null.")
"You can't assert a directory having files if the DirectoryInfo is null.")
.Then
.ForCondition(!string.IsNullOrEmpty(searchPattern))
.FailWith(
"You can't assert a directory having a given file if you don't pass a proper search pattern.")
"You can't assert a directory having files if you don't pass a proper search pattern.")
.Then
.Given(() => Subject!)
.ForCondition(directoryInfo
=> directoryInfo.GetFiles(searchPattern).Length == 1)
=> directoryInfo.GetFiles(searchPattern).Length >= minimumCount)
.FailWith(
"Expected {context} {1} to contain exactly one file matching {0}{reason}, but found {2}.",
$"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.",
_ => searchPattern,
directoryInfo => directoryInfo.Name,
directoryInfo => directoryInfo.GetFiles(searchPattern).Length);

return new AndWhichConstraint<FileSystemAssertions, FileAssertions>(
new FileSystemAssertions(Subject!.FileSystem),
new FileAssertions(Subject!.GetFiles(searchPattern).Single()));
return new AndConstraint<DirectoryAssertions>(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,40 +17,40 @@ internal DirectoryInfoAssertions(IDirectoryInfo? instance)
/// <summary>
/// Asserts that the current directory has at least one directory which matches the <paramref name="searchPattern" />.
/// </summary>
public AndConstraint<DirectoryInfoAssertions> HaveDirectoryMatching(
public AndConstraint<DirectoryInfoAssertions> HaveDirectories(
string searchPattern = "*", string because = "", params object[] becauseArgs)
{
new DirectoryAssertions(Subject).HasDirectoryMatching(searchPattern, because, becauseArgs);
new DirectoryAssertions(Subject).HasDirectories(searchPattern, because, becauseArgs);
return new AndConstraint<DirectoryInfoAssertions>(this);
}

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

/// <summary>
/// Asserts that the directory contains exactly one directory matching the given <paramref name="searchPattern" />.
/// Asserts that the directory contains exactly one file matching the given <paramref name="searchPattern" />.
/// </summary>
public AndWhichConstraint<FileSystemAssertions, DirectoryAssertions> HaveSingleDirectory(
public AndWhichConstraint<FileSystemAssertions, FileAssertions> HaveFile(
string searchPattern = "*", string because = "", params object[] becauseArgs)
{
return new DirectoryAssertions(Subject).HasSingleDirectoryMatching(searchPattern, because,
return new DirectoryAssertions(Subject).HasFile(searchPattern, because,
becauseArgs);
}

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

0 comments on commit 5da01b6

Please sign in to comment.