Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add assertions for HasDirectoriesMatching #42

Merged
merged 2 commits into from
Sep 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,36 +17,45 @@ internal DirectoryAssertions(IDirectoryInfo? instance)
}

/// <summary>
/// Asserts that the directory contains exactly one file matching the given <paramref name="searchPattern"/>.
/// Asserts that the current directory has at least <paramref name="minimumCount" /> directories which match the
/// <paramref name="searchPattern" />.
/// </summary>
public AndWhichConstraint<FileSystemAssertions, FileAssertions> HasSingleFileMatching(
string searchPattern = "*", string because = "", params object[] becauseArgs)
public AndConstraint<DirectoryAssertions> HasDirectoriesMatching(
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 a given file if it is null")
"You can't assert a directory having directories 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 name")
"You can't assert a directory having directories if you don't pass a proper search pattern.")
.Then
.Given(() => Subject!)
.ForCondition(directoryInfo
=> directoryInfo.GetFiles(searchPattern).Length == 1)
=> directoryInfo.GetDirectories(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 directory" : $"{minimumCount} directories")} 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()));
directoryInfo => directoryInfo.GetDirectories(searchPattern).Length);

return new AndConstraint<DirectoryAssertions>(this);
}

/// <summary>
/// Asserts that the current directory has at least one directory which matches the <paramref name="searchPattern" />.
/// </summary>
public AndConstraint<DirectoryAssertions> HasDirectoryMatching(
string searchPattern = "*", string because = "", params object[] becauseArgs)
=> HasDirectoriesMatching(searchPattern, 1, because, becauseArgs);

/// <summary>
/// Asserts that the current directory has at least one file which matches the <paramref name="searchPattern" />.
/// </summary>
Expand All @@ -69,11 +78,11 @@ public AndConstraint<DirectoryAssertions> HasFilesMatching(
.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 files if the DirectoryInfo is null.")
.Then
.ForCondition(!string.IsNullOrEmpty(searchPattern))
.FailWith(
"You can't assert a directory having files if you don't pass a proper name")
"You can't assert a directory having files if you don't pass a proper search pattern.")
.Then
.Given(() => Subject!)
.ForCondition(directoryInfo
Expand All @@ -86,4 +95,66 @@ public AndConstraint<DirectoryAssertions> HasFilesMatching(

return new AndConstraint<DirectoryAssertions>(this);
}

/// <summary>
/// Asserts that the directory contains exactly one directory matching the given <paramref name="searchPattern" />.
/// </summary>
public AndWhichConstraint<FileSystemAssertions, DirectoryAssertions> HasSingleDirectoryMatching(
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.")
.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.")
.Then
.Given(() => Subject!)
.ForCondition(directoryInfo
=> directoryInfo.GetDirectories(searchPattern).Length == 1)
.FailWith(
"Expected {context} {1} to contain exactly one directory matching {0}{reason}, but found {2}.",
_ => searchPattern,
directoryInfo => directoryInfo.Name,
directoryInfo => directoryInfo.GetDirectories(searchPattern).Length);

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

/// <summary>
/// Asserts that the directory contains exactly one file matching the given <paramref name="searchPattern" />.
/// </summary>
public AndWhichConstraint<FileSystemAssertions, FileAssertions> HasSingleFileMatching(
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 file if it 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.")
.Then
.Given(() => Subject!)
.ForCondition(directoryInfo
=> directoryInfo.GetFiles(searchPattern).Length == 1)
.FailWith(
"Expected {context} {1} to contain exactly one file matching {0}{reason}, but found {2}.",
_ => searchPattern,
directoryInfo => directoryInfo.Name,
directoryInfo => directoryInfo.GetFiles(searchPattern).Length);

return new AndWhichConstraint<FileSystemAssertions, FileAssertions>(
new FileSystemAssertions(Subject!.FileSystem),
new FileAssertions(Subject!.GetFiles(searchPattern).Single()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@ 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(
string searchPattern = "*", string because = "", params object[] becauseArgs)
{
new DirectoryAssertions(Subject).HasDirectoryMatching(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" />.
/// </summary>
Expand All @@ -24,12 +34,23 @@ public AndConstraint<DirectoryInfoAssertions> HaveFileMatching(
return new AndConstraint<DirectoryInfoAssertions>(this);
}

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

/// <summary>
/// Asserts that the directory contains exactly one file matching the given <paramref name="searchPattern" />.
/// </summary>
public AndWhichConstraint<FileSystemAssertions, FileAssertions> HaveSingleFile(
string searchPattern = "*", string because = "", params object[] becauseArgs)
{
return new DirectoryAssertions(Subject).HasSingleFileMatching(searchPattern, because, becauseArgs);
return new DirectoryAssertions(Subject).HasSingleFileMatching(searchPattern, because,
becauseArgs);
}
}
14 changes: 7 additions & 7 deletions Source/Testably.Abstractions.FluentAssertions/FileAssertions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public AndConstraint<FileAssertions> DoesNotHaveAttribute(
.BecauseOf(because, becauseArgs)
.ForCondition(Subject != null)
.FailWith(
$"You can't assert that the file does not have attribute {attribute} if it is null")
$"You can't assert that the file does not have attribute {attribute} if it is null.")
.Then
.Given(() => Subject!)
.ForCondition(fileInfo => !fileInfo.Attributes.HasFlag(attribute))
Expand All @@ -51,7 +51,7 @@ public AndConstraint<FileAssertions> HasAttribute(
.BecauseOf(because, becauseArgs)
.ForCondition(Subject != null)
.FailWith(
$"You can't assert that the file has attribute {attribute} if it is null")
$"You can't assert that the file has attribute {attribute} if it is null.")
.Then
.Given(() => Subject!)
.ForCondition(fileInfo => fileInfo.Attributes.HasFlag(attribute))
Expand All @@ -73,7 +73,7 @@ public AndConstraint<FileAssertions> HasContent(
.BecauseOf(because, becauseArgs)
.ForCondition(Subject != null)
.FailWith(
"You can't assert the content of a file if the FileInfo is null")
"You can't assert the content of a file if the FileInfo is null.")
.Then
.Given(() => Subject!)
.ForCondition(fileInfo => fileInfo.FileSystem.File
Expand All @@ -97,7 +97,7 @@ public AndConstraint<FileAssertions> HasContent(
.BecauseOf(because, becauseArgs)
.ForCondition(Subject != null)
.FailWith(
"You can't assert the content of a file if the FileInfo is null")
"You can't assert the content of a file if the FileInfo is null.")
.Then
.Given(() => Subject!)
.ForCondition(fileInfo => pattern.Matches(
Expand All @@ -121,7 +121,7 @@ public AndConstraint<FileAssertions> HasContent(
.BecauseOf(because, becauseArgs)
.ForCondition(Subject != null)
.FailWith(
"You can't assert the content of a file if the FileInfo is null")
"You can't assert the content of a file if the FileInfo is null.")
.Then
.Given(() => Subject!)
.ForCondition(fileInfo => pattern.Matches(
Expand All @@ -144,7 +144,7 @@ public AndConstraint<FileAssertions> IsNotReadOnly(
.BecauseOf(because, becauseArgs)
.ForCondition(Subject != null)
.FailWith(
"You can't assert that the file is not read-only if it is null")
"You can't assert that the file is not read-only if it is null.")
.Then
.Given(() => Subject!)
.ForCondition(fileInfo => !fileInfo.IsReadOnly)
Expand All @@ -166,7 +166,7 @@ public AndConstraint<FileAssertions> IsReadOnly(
.BecauseOf(because, becauseArgs)
.ForCondition(Subject != null)
.FailWith(
"You can't assert that the file is read-only if it is null")
"You can't assert that the file is read-only if it is null.")
.Then
.Given(() => Subject!)
.ForCondition(fileInfo => fileInfo.IsReadOnly)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public AndWhichConstraint<FileSystemAssertions, DirectoryAssertions> HaveDirecto
.WithDefaultIdentifier(Identifier)
.BecauseOf(because, becauseArgs)
.ForCondition(!string.IsNullOrEmpty(path))
.FailWith("You can't assert that a directory exists if you don't pass a proper name.")
.FailWith("You can't assert that a directory exists if you don't pass a proper path.")
.Then
.Given(() => Subject.DirectoryInfo.New(path))
.ForCondition(directoryInfo => directoryInfo.Exists)
Expand All @@ -46,7 +46,7 @@ public AndWhichConstraint<FileSystemAssertions, FileAssertions> HaveFile(
.WithDefaultIdentifier(Identifier)
.BecauseOf(because, becauseArgs)
.ForCondition(!string.IsNullOrEmpty(path))
.FailWith("You can't assert that a file exists if you don't pass a proper name.")
.FailWith("You can't assert that a file exists if you don't pass a proper path.")
.Then
.Given(() => Subject.FileInfo.New(path))
.ForCondition(fileInfo => fileInfo.Exists)
Expand All @@ -69,7 +69,7 @@ public AndWhichConstraint<FileSystemAssertions, DirectoryAssertions> NotHaveDire
.BecauseOf(because, becauseArgs)
.ForCondition(!string.IsNullOrEmpty(path))
.FailWith(
"You can't assert that a directory does not exist if you don't pass a proper name.")
"You can't assert that a directory does not exist if you don't pass a proper path.")
.Then
.Given(() => Subject.DirectoryInfo.New(path))
.ForCondition(directoryInfo => !directoryInfo.Exists)
Expand All @@ -92,7 +92,7 @@ public AndWhichConstraint<FileSystemAssertions, FileAssertions> NotHaveFile(
.BecauseOf(because, becauseArgs)
.ForCondition(!string.IsNullOrEmpty(path))
.FailWith(
"You can't assert that a file does not exist if you don't pass a proper name.")
"You can't assert that a file does not exist if you don't pass a proper path.")
.Then
.Given(() => Subject.FileInfo.New(path))
.ForCondition(fileInfo => !fileInfo.Exists)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public AndConstraint<TFileSystemInfo> Exist(
.WithDefaultIdentifier(Identifier)
.BecauseOf(because, becauseArgs)
.ForCondition(Subject != null)
.FailWith("You can't assert that the {context} exists if it is null")
.FailWith("You can't assert that the {context} exists if it is null.")
.Then
.Given(() => Subject!)
.ForCondition(fileSystemInfo => fileSystemInfo.Exists)
Expand Down
Loading