diff --git a/Tests/aweXpect.Testably.Tests/DirectoryInfo.IsEmpty.Tests.cs b/Tests/aweXpect.Testably.Tests/DirectoryInfo.IsEmpty.Tests.cs index 91716d1..72c6e6e 100644 --- a/Tests/aweXpect.Testably.Tests/DirectoryInfo.IsEmpty.Tests.cs +++ b/Tests/aweXpect.Testably.Tests/DirectoryInfo.IsEmpty.Tests.cs @@ -30,6 +30,25 @@ but it was not """); } + [Fact] + public async Task WhenDirectoryDoesNotExist_ShouldFail() + { + MockFileSystem fileSystem = new(); + IDirectoryInfo dirInfo = fileSystem.DirectoryInfo.New("missing"); + + async Task Act() + { + await That(dirInfo).IsEmpty(); + } + + await That(Act).ThrowsException() + .WithMessage(""" + Expected that dirInfo + is empty, + but it did not exist + """); + } + [Fact] public async Task WhenDirectoryIsEmpty_ShouldSucceed() { diff --git a/Tests/aweXpect.Testably.Tests/FileInfo.HasContent.Binary.Negated.Tests.cs b/Tests/aweXpect.Testably.Tests/FileInfo.HasContent.Binary.Negated.Tests.cs new file mode 100644 index 0000000..e4deb9e --- /dev/null +++ b/Tests/aweXpect.Testably.Tests/FileInfo.HasContent.Binary.Negated.Tests.cs @@ -0,0 +1,59 @@ +using System.IO.Abstractions; +using System.Text; +using Testably.Abstractions.Testing; + +namespace aweXpect.Testably.Tests; + +public sealed partial class FileInfo +{ + public sealed partial class HasContent + { + public sealed class BinaryNegated + { + public sealed class Tests + { + [Fact] + public async Task WhenContentIsDifferent_ShouldSucceed() + { + byte[] content = Encoding.UTF8.GetBytes("baz"); + byte[] expected = Encoding.UTF8.GetBytes("bar"); + string path = "foo.txt"; + MockFileSystem fileSystem = new(); + // ReSharper disable once MethodHasAsyncOverload + fileSystem.File.WriteAllBytes(path, content); + IFileInfo fileInfo = fileSystem.FileInfo.New("foo.txt"); + + async Task Act() + { + await That(fileInfo).DoesNotComplyWith(it => it.HasContent(expected)); + } + + await That(Act).DoesNotThrow(); + } + + [Fact] + public async Task WhenContentMatches_ShouldFail() + { + byte[] content = Encoding.UTF8.GetBytes("baz"); + string path = "foo.txt"; + MockFileSystem fileSystem = new(); + // ReSharper disable once MethodHasAsyncOverload + fileSystem.File.WriteAllBytes(path, content); + IFileInfo fileInfo = fileSystem.FileInfo.New("foo.txt"); + + async Task Act() + { + await That(fileInfo).DoesNotComplyWith(it => it.HasContent(content)); + } + + await That(Act).ThrowsException() + .WithMessage(""" + Expected that fileInfo + has content different from content, + but it did match + """); + } + } + } + } +} diff --git a/Tests/aweXpect.Testably.Tests/FileInfo.HasContent.Binary.Plural.Tests.cs b/Tests/aweXpect.Testably.Tests/FileInfo.HasContent.Binary.Plural.Tests.cs new file mode 100644 index 0000000..8267df0 --- /dev/null +++ b/Tests/aweXpect.Testably.Tests/FileInfo.HasContent.Binary.Plural.Tests.cs @@ -0,0 +1,119 @@ +using System.IO; +using System.Text; +using Testably.Abstractions.Testing; + +namespace aweXpect.Testably.Tests; + +public sealed partial class FileInfo +{ + public sealed partial class HasContent + { + public sealed class BinaryPlural + { + public sealed class Tests + { + [Fact] + public async Task WhenContentDiffers_ShouldFail() + { + byte[] expected = Encoding.UTF8.GetBytes("bar"); + MockFileSystem sut = new(); + sut.Initialize().WithSubdirectory("foo").Initialized(d => d + .WithFile("bar.txt").Which(f => f.HasStringContent("baz"))); + + async Task Act() + { + await That(sut).HasDirectory("foo") + .WithFiles(files => files.All().ComplyWith(file => file.HasContent(expected))); + } + + await That(Act).ThrowsException() + .WithMessage($""" + Expected that sut + has directory 'foo' whose files have content equal to expected for all items, + but not all were + + Not matching items: + [ + foo{Path.DirectorySeparatorChar}bar.txt, + (… and maybe others) + ] + + Collection: + [ + foo{Path.DirectorySeparatorChar}bar.txt + ] + """).IgnoringNewlineStyle(); + } + + [Fact] + public async Task WhenContentMatches_ShouldSucceed() + { + byte[] content = Encoding.UTF8.GetBytes("baz"); + MockFileSystem sut = new(); + sut.Initialize().WithSubdirectory("foo").Initialized(d => d + .WithFile("bar.txt").Which(f => f.HasStringContent("baz"))); + + async Task Act() + { + await That(sut).HasDirectory("foo") + .WithFiles(files => files.All().ComplyWith(file => file.HasContent(content))); + } + + await That(Act).DoesNotThrow(); + } + + [Fact] + public async Task WhenNegated_WhenContentDiffers_ShouldSucceed() + { + byte[] expected = Encoding.UTF8.GetBytes("bar"); + MockFileSystem sut = new(); + sut.Initialize().WithSubdirectory("foo").Initialized(d => d + .WithFile("bar.txt").Which(f => f.HasStringContent("baz"))); + + async Task Act() + { + await That(sut).HasDirectory("foo") + .WithFiles(files => files.All().ComplyWith(file + => file.DoesNotComplyWith(it => it.HasContent(expected)))); + } + + await That(Act).DoesNotThrow(); + } + + [Fact] + public async Task WhenNegated_WhenContentMatches_ShouldFail() + { + byte[] content = Encoding.UTF8.GetBytes("baz"); + MockFileSystem sut = new(); + sut.Initialize().WithSubdirectory("foo").Initialized(d => d + .WithFile("bar.txt").Which(f => f.HasStringContent("baz"))); + + async Task Act() + { + await That(sut).HasDirectory("foo") + .WithFiles(files => files.All().ComplyWith(file + => file.DoesNotComplyWith(it => it.HasContent(content)))); + } + + await That(Act).ThrowsException() + .WithMessage($""" + Expected that sut + has directory 'foo' whose files have content different from content for all items, + but not all were + + Not matching items: + [ + foo{Path.DirectorySeparatorChar}bar.txt, + (… and maybe others) + ] + + Collection: + [ + foo{Path.DirectorySeparatorChar}bar.txt + ] + """).IgnoringNewlineStyle(); + } + } + } + } +} diff --git a/Tests/aweXpect.Testably.Tests/FileInfo.HasContent.SameAs.Tests.cs b/Tests/aweXpect.Testably.Tests/FileInfo.HasContent.SameAs.Tests.cs index db235d0..aadf028 100644 --- a/Tests/aweXpect.Testably.Tests/FileInfo.HasContent.SameAs.Tests.cs +++ b/Tests/aweXpect.Testably.Tests/FileInfo.HasContent.SameAs.Tests.cs @@ -43,6 +43,33 @@ Expected that fileInfo """); } + [Fact] + public async Task WhenExpectedFileDoesNotExist_ShouldFail() + { + MockFileSystem fileSystem = new(); + string path = "foo.txt"; + string expectedPath = "bar.txt"; + string fullExpectedPath = fileSystem.Path.GetFullPath(expectedPath); + // ReSharper disable once MethodHasAsyncOverload + fileSystem.File.WriteAllText(path, "baz"); + IFileInfo fileInfo = fileSystem.FileInfo.New("foo.txt"); + + async Task Act() + { + await That(fileInfo).HasContent().SameAs(expectedPath); + } + + await That(Act).ThrowsException() + .WithMessage($""" + Expected that fileInfo + has the same content as file '{fullExpectedPath}', + but it did not contain any file at '{fullExpectedPath}' + + File content: + baz + """); + } + [Fact] public async Task WhenContentMatches_ShouldSucceed() { diff --git a/Tests/aweXpect.Testably.Tests/FileInfo.HasContent.Tests.cs b/Tests/aweXpect.Testably.Tests/FileInfo.HasContent.Tests.cs index 5a6a68a..b2fe859 100644 --- a/Tests/aweXpect.Testably.Tests/FileInfo.HasContent.Tests.cs +++ b/Tests/aweXpect.Testably.Tests/FileInfo.HasContent.Tests.cs @@ -113,6 +113,26 @@ async Task Act() await That(Act).DoesNotThrow(); } + + [Fact] + public async Task WhenFileDoesNotExist_ShouldFail() + { + byte[] expected = Encoding.UTF8.GetBytes("bar"); + MockFileSystem fileSystem = new(); + IFileInfo fileInfo = fileSystem.FileInfo.New("foo.txt"); + + async Task Act() + { + await That(fileInfo).HasContent(expected); + } + + await That(Act).ThrowsException() + .WithMessage(""" + Expected that fileInfo + has content equal to expected, + but it did not exist + """); + } } public sealed class AsWildcardTests diff --git a/Tests/aweXpect.Testably.Tests/FileInfo.HasLength.Negated.Tests.cs b/Tests/aweXpect.Testably.Tests/FileInfo.HasLength.Negated.Tests.cs new file mode 100644 index 0000000..f0410bc --- /dev/null +++ b/Tests/aweXpect.Testably.Tests/FileInfo.HasLength.Negated.Tests.cs @@ -0,0 +1,65 @@ +using System.IO.Abstractions; +using System.Text; +using Testably.Abstractions.Testing; + +namespace aweXpect.Testably.Tests; + +public sealed partial class FileInfo +{ + public sealed class HasLengthNegated + { + public sealed class Tests + { + [Fact] + public async Task WhenLengthDiffers_ShouldSucceed() + { + MockFileSystem fileSystem = new(); + // ReSharper disable once MethodHasAsyncOverload + fileSystem.File.WriteAllBytes("foo.txt", Encoding.UTF8.GetBytes("baz")); + IFileInfo fileInfo = fileSystem.FileInfo.New("foo.txt"); + + async Task Act() + { + await That(fileInfo).DoesNotComplyWith(it => it.HasLength(5)); + } + + await That(Act).DoesNotThrow(); + } + + [Fact] + public async Task WhenLengthMatches_ShouldFail() + { + MockFileSystem fileSystem = new(); + // ReSharper disable once MethodHasAsyncOverload + fileSystem.File.WriteAllBytes("foo.txt", Encoding.UTF8.GetBytes("baz")); + IFileInfo fileInfo = fileSystem.FileInfo.New("foo.txt"); + + async Task Act() + { + await That(fileInfo).DoesNotComplyWith(it => it.HasLength(3)); + } + + await That(Act).ThrowsException() + .WithMessage(""" + Expected that fileInfo + does not have length 3, + but it did + """); + } + + [Fact] + public async Task WhenFileDoesNotExist_ShouldSucceed() + { + MockFileSystem fileSystem = new(); + IFileInfo fileInfo = fileSystem.FileInfo.New("missing.txt"); + + async Task Act() + { + await That(fileInfo).DoesNotComplyWith(it => it.HasLength(0)); + } + + await That(Act).DoesNotThrow(); + } + } + } +} diff --git a/Tests/aweXpect.Testably.Tests/FileInfo.IsReadOnly.Tests.cs b/Tests/aweXpect.Testably.Tests/FileInfo.IsReadOnly.Tests.cs index ae111d1..7e3782c 100644 --- a/Tests/aweXpect.Testably.Tests/FileInfo.IsReadOnly.Tests.cs +++ b/Tests/aweXpect.Testably.Tests/FileInfo.IsReadOnly.Tests.cs @@ -31,6 +31,25 @@ but it was not """); } + [Fact] + public async Task WhenFileDoesNotExist_ShouldFail() + { + MockFileSystem fileSystem = new(); + IFileInfo fileInfo = fileSystem.FileInfo.New("foo.txt"); + + async Task Act() + { + await That(fileInfo).IsReadOnly(); + } + + await That(Act).ThrowsException() + .WithMessage(""" + Expected that fileInfo + is read-only, + but it did not exist + """); + } + [Fact] public async Task WhenFileIsReadOnly_ShouldSucceed() { diff --git a/Tests/aweXpect.Testably.Tests/FileSystem.HasDirectory.WithDirectories.Tests.cs b/Tests/aweXpect.Testably.Tests/FileSystem.HasDirectory.WithDirectories.Tests.cs index 39661b6..fbcee57 100644 --- a/Tests/aweXpect.Testably.Tests/FileSystem.HasDirectory.WithDirectories.Tests.cs +++ b/Tests/aweXpect.Testably.Tests/FileSystem.HasDirectory.WithDirectories.Tests.cs @@ -1,4 +1,5 @@ -using Testably.Abstractions.Testing; +using System.IO; +using Testably.Abstractions.Testing; namespace aweXpect.Testably.Tests; @@ -38,6 +39,58 @@ but found only 2 """).AsWildcard(); } + [Fact] + public async Task AllAreEmpty_WhenSubdirectoryIsNotEmpty_ShouldFail() + { + string path = "foo"; + MockFileSystem sut = new(); + sut.Initialize().WithSubdirectory(path).Initialized(d => d + .WithSubdirectory("directory1").Initialized(s => s + .WithFile("bar.txt"))); + + async Task Act() + { + await That(sut).HasDirectory(path) + .WithDirectories(dirs => dirs.All().ComplyWith(dir => dir.IsEmpty())); + } + + await That(Act).ThrowsException() + .WithMessage($""" + Expected that sut + has directory '{path}' whose subdirectories is empty for all items, + but not all were + + Not matching items: + [ + foo{Path.DirectorySeparatorChar}directory1, + (… and maybe others) + ] + + Collection: + [ + foo{Path.DirectorySeparatorChar}directory1 + ] + """).IgnoringNewlineStyle(); + } + + [Fact] + public async Task AllAreEmpty_WhenSubdirectoriesAreEmpty_ShouldSucceed() + { + string path = "foo"; + MockFileSystem sut = new(); + sut.Initialize().WithSubdirectory(path).Initialized(d => d + .WithSubdirectory("directory1") + .WithSubdirectory("directory2")); + + async Task Act() + { + await That(sut).HasDirectory(path) + .WithDirectories(dirs => dirs.All().ComplyWith(dir => dir.IsEmpty())); + } + + await That(Act).DoesNotThrow(); + } + [Fact] public async Task WhenItemCountMatches_ShouldSucceed() { diff --git a/Tests/aweXpect.Testably.Tests/FileSystem.HasFile.WithContent.SameAs.Tests.cs b/Tests/aweXpect.Testably.Tests/FileSystem.HasFile.WithContent.SameAs.Tests.cs index d0b12d0..652cd41 100644 --- a/Tests/aweXpect.Testably.Tests/FileSystem.HasFile.WithContent.SameAs.Tests.cs +++ b/Tests/aweXpect.Testably.Tests/FileSystem.HasFile.WithContent.SameAs.Tests.cs @@ -43,6 +43,32 @@ Expected that sut """); } + [Fact] + public async Task WhenExpectedFileDoesNotExist_ShouldFail() + { + MockFileSystem sut = new(); + string path = "foo.txt"; + string expectedPath = "bar.txt"; + string fullExpectedPath = sut.Path.GetFullPath(expectedPath); + // ReSharper disable once MethodHasAsyncOverload + sut.File.WriteAllText(path, "baz"); + + async Task Act() + { + await That(sut).HasFile(path).WithContent().SameAs(expectedPath); + } + + await That(Act).ThrowsException() + .WithMessage($""" + Expected that sut + has file '{path}' with the same content as file '{fullExpectedPath}', + but it did not contain any file at '{fullExpectedPath}' + + File content: + baz + """); + } + [Fact] public async Task WhenContentMatches_ShouldSucceed() { diff --git a/Tests/aweXpect.Testably.Tests/FileSystem.HasFile.WithCreationTime.Tests.cs b/Tests/aweXpect.Testably.Tests/FileSystem.HasFile.WithCreationTime.Tests.cs index a2538c2..ef16ce7 100644 --- a/Tests/aweXpect.Testably.Tests/FileSystem.HasFile.WithCreationTime.Tests.cs +++ b/Tests/aweXpect.Testably.Tests/FileSystem.HasFile.WithCreationTime.Tests.cs @@ -96,6 +96,46 @@ await That(sut).HasFile(path).WithCreationTime(expectedTime) await That(Act).DoesNotThrow(); } + [Fact] + public async Task WhenNegated_WhenCreationTimeMatches_ShouldFail() + { + MockFileSystem sut = new(); + DateTime expectedTime = CurrentTime().ToLocalTime(); + string path = "foo.txt"; + sut.File.WriteAllText(path, ""); + sut.File.SetCreationTime(path, expectedTime); + + async Task Act() + { + await That(sut).DoesNotComplyWith(it => it.HasFile(path).WithCreationTime(expectedTime)); + } + + await That(Act).ThrowsException() + .WithMessage($""" + Expected that sut + does not have file '{path}' with creation time not equal to {Formatter.Format(expectedTime)}, + but it did and it was {Formatter.Format(expectedTime)} + """); + } + + [Fact] + public async Task WhenNegated_WhenCreationTimeDiffers_ShouldSucceed() + { + MockFileSystem sut = new(); + DateTime expectedTime = CurrentTime().ToLocalTime(); + DateTime actualTime = expectedTime.AddSeconds(1); + string path = "foo.txt"; + sut.File.WriteAllText(path, ""); + sut.File.SetCreationTime(path, actualTime); + + async Task Act() + { + await That(sut).DoesNotComplyWith(it => it.HasFile(path).WithCreationTime(expectedTime)); + } + + await That(Act).DoesNotThrow(); + } + [Fact] public async Task WhenCreationTimeIsUnspecified_ShouldSucceed() {