Skip to content
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 @@ -22,10 +22,14 @@ public AndConstraint<DirectoryAssertions> HasFileMatching(
=> HasFilesMatching(searchPattern, 1, because, becauseArgs);

/// <summary>
/// Asserts that the current directory has at least <paramref name="minimumCount"/> files which match the <paramref name="searchPattern" />.
/// 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)
string searchPattern = "*",
int minimumCount = 1,
string because = "",
params object[] becauseArgs)
{
Execute.Assertion
.WithDefaultIdentifier(Identifier)
Expand All @@ -39,10 +43,12 @@ public AndConstraint<DirectoryAssertions> HasFilesMatching(
"You can't assert a directory having files if you don't pass a proper name")
.Then
.Given(() => Subject!)
.ForCondition(directoryInfo => directoryInfo.GetFiles(searchPattern).Length > 0)
.ForCondition(directoryInfo
=> directoryInfo.GetFiles(searchPattern).Length >= minimumCount)
.FailWith(
$"Expected {{context}} {{1}} to contain at least {(minimumCount == 1 ? "one file" : $"{minimumCount} files")} matching {{0}}{{reason}}, but none was found.",
_ => searchPattern, directoryInfo => directoryInfo.Name);
$"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 AndConstraint<DirectoryAssertions>(this);
}
Expand Down
2 changes: 1 addition & 1 deletion Source/Testably.Abstractions.FluentAssertions/Match.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ public static Match Wildcard(string pattern, bool ignoreCase = false)

private sealed class WildcardMatch : Match
{
private readonly string _originalPattern;
private readonly bool _ignoreCase;
private readonly string _originalPattern;
private readonly string _pattern;

internal WildcardMatch(string pattern, bool ignoreCase)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,37 @@ public void HasFileMatching_WithoutMatchingFile_ShouldThrow(
.Be(
$"Expected directory \"{directoryName}\" to contain at least one file matching \"{fileName}\" {because}, but none was found.");
}

[Theory]
[InlineAutoData(3, 5)]
[InlineAutoData(1, 2)]
public void HasFilesMatching_WithoutTooLittleFiles_ShouldThrow(
int matchingCount,
int expectedCount,
string directoryName,
string fileNamePrefix,
string because)
{
MockFileSystem fileSystem = new();
fileSystem.Initialize()
.WithSubdirectory(directoryName);
for (int i = 0; i < matchingCount; i++)
{
fileSystem.File.WriteAllText(
fileSystem.Path.Combine(directoryName, $"{fileNamePrefix}-{i}.txt"),
"some content");
}

DirectoryAssertions? sut = fileSystem.Should().HaveDirectory(directoryName).Which;

Exception? exception = Record.Exception(() =>
{
sut.HasFilesMatching($"{fileNamePrefix}*", expectedCount, because);
});

exception.Should().NotBeNull();
exception!.Message.Should()
.Be(
$"Expected directory \"{directoryName}\" to contain at least {expectedCount} files matching \"{fileNamePrefix}*\" {because}, but only {matchingCount} were found.");
}
}