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
8 changes: 4 additions & 4 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@
<ItemGroup>
<PackageVersion Include="System.Memory" Version="4.6.3" />
<PackageVersion Include="System.Net.Http" Version="4.3.4"/>
<PackageVersion Include="aweXpect" Version="2.40.0"/>
<PackageVersion Include="aweXpect" Version="3.0.0"/>
<PackageVersion Include="aweXpect.Chronology" Version="1.1.0"/>
<PackageVersion Include="aweXpect.Testably" Version="0.15.0"/>
<PackageVersion Include="aweXpect.Mockolate" Version="3.2.0"/>
<PackageVersion Include="Mockolate" Version="3.5.2"/>
<PackageVersion Include="aweXpect.Testably" Version="2.0.0"/>
<PackageVersion Include="aweXpect.Mockolate" Version="3.3.0"/>
<PackageVersion Include="Mockolate" Version="3.6.0"/>
<PackageVersion Include="TUnit.Engine" Version="1.72.16"/>
<PackageVersion Include="Polyfill" Version="9.24.1"/>
<PackageVersion Include="PublicApiGenerator" Version="11.5.4"/>
Expand Down
62 changes: 40 additions & 22 deletions Tests/Helpers/Testably.Abstractions.TestHelpers/AssertionHelpers.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
#if NET8_0_OR_GREATER
using System.Diagnostics.CodeAnalysis;
#endif
using aweXpect.Core;
using aweXpect.Core;
using aweXpect.Core.Constraints;
using aweXpect.Core.Extending;
using aweXpect.Core.Sources;
using aweXpect.Delegates;
using aweXpect.Formatting;
using aweXpect.Results;
using System;
using System.IO;
using System.Text;
Expand All @@ -15,16 +14,49 @@ namespace Testably.Abstractions.TestHelpers;

public static class AssertionHelpers
{
/// <summary>
/// Adds the <paramref name="expectations" /> to the <paramref name="result" /> only when the
/// <paramref name="condition" /> is <see langword="true" />.
/// </summary>
public static AndOrResult<TType, TThat, TSelf> AndIf<TType, TThat, TSelf>(
this AndOrResult<TType, TThat, TSelf> result,
bool condition,
Func<TThat, Expectation> expectations)
where TSelf : AndOrResult<TType, TThat, TSelf>
{
if (condition)
{
expectations(result.And);
}

return result;
}

/// <inheritdoc cref="AndIf{TType, TThat, TSelf}" />
public static ThatDelegateThrows<TException> AndIf<TException>(
this ThatDelegateThrows<TException> result,
bool condition,
Func<IThatDelegateThrows<TException>, Expectation> expectations)
where TException : Exception?
{
if (condition)
{
expectations(result);
}

return result;
}

/// <summary>
/// Verifies that the <paramref name="delegate" /> throws either a <see cref="FileNotFoundException" /> or
/// a <see cref="DirectoryNotFoundException" /> with their corresponding HResult values.
/// </summary>
public static ThatDelegateThrows<Exception> ThrowsAFileOrDirectoryNotFoundException(
this ThatDelegate @delegate)
this ThatDelegate.WithoutValue @delegate)
{
ThrowsOption? throwOptions = new();
return new ThatDelegateThrows<Exception>(
@delegate.ExpectationBuilder.AddConstraint((it, grammars)
@delegate.Get().ExpectationBuilder.AddConstraint((it, grammars)
=> new DelegateIsNotNullWithinTimeoutConstraint(it, grammars, throwOptions))
.ForWhich<DelegateValue, Exception?>(d => d.Exception)
.AddConstraint((it, grammars)
Expand Down Expand Up @@ -88,14 +120,7 @@ public override void AppendResult(StringBuilder stringBuilder, string? indentati
public override ConstraintResult Negate()
=> this;

#if NET8_0_OR_GREATER
public override bool TryGetValue<TValue>([NotNullWhen(true)] out TValue? value)
where TValue : default
#else
#pragma warning disable CS8765
public override bool TryGetValue<TValue>(out TValue? value) where TValue : default
#pragma warning restore CS8765
#endif
public override bool TryGetStoredValue<TValue>(out TValue? value) where TValue : default
{
value = default;
return false;
Expand Down Expand Up @@ -184,14 +209,7 @@ public override ConstraintResult Negate()
return this;
}

#if NET8_0_OR_GREATER
public override bool TryGetValue<TValue>([NotNullWhen(true)] out TValue? value)
where TValue : default
#else
#pragma warning disable CS8765
public override bool TryGetValue<TValue>(out TValue? value) where TValue : default
#pragma warning restore CS8765
#endif
public override bool TryGetStoredValue<TValue>(out TValue? value) where TValue : default
{
if (_actual is TValue typedValue)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ public async Task CompressedLength_WithNoCompression_ShouldBeFileLength()
IZipArchive archive = FileSystem.ZipArchive.New(stream, ZipArchiveMode.Read);

await That(archive.Entries.Single())
.For(x => x.Length, l => l.IsEqualTo(9)).And
.For(x => x.CompressedLength, l => l.IsEqualTo(9));
.Whose(x => x.Length, l => l.IsEqualTo(9)).And
.Whose(x => x.CompressedLength, l => l.IsEqualTo(9));
}

[Test]
Expand All @@ -106,8 +106,8 @@ public async Task CompressedLength_WithOptimalCompressionLevel_ShouldBeLessThanF
IZipArchive archive = FileSystem.ZipArchive.New(stream, ZipArchiveMode.Read);

await That(archive.Entries.Single())
.For(x => x.Length, l => l.IsEqualTo(9)).And
.For(x => x.CompressedLength, l => l.IsLessThan(9));
.Whose(x => x.Length, l => l.IsEqualTo(9)).And
.Whose(x => x.CompressedLength, l => l.IsLessThan(9));
}

#if FEATURE_COMPRESSION_ADVANCED
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ await FileSystem.ZipFile
FileSystem.ZipFile.Open("destination.zip", ZipArchiveMode.Read);

await That(archive.Entries).HasSingle()
.Which.For(x => x.FullName, f => f.IsEqualTo("bar/"));
.Which.Whose(x => x.FullName, f => f.IsEqualTo("bar/"));
}

[Test]
Expand Down Expand Up @@ -68,7 +68,7 @@ await FileSystem.ZipFile
FileSystem.ZipFile.Open("destination.zip", ZipArchiveMode.Read);

await That(archive.Entries).HasSingle()
.Which.For(x => x.FullName, f => f.IsEqualTo("foo/"));
.Which.Whose(x => x.FullName, f => f.IsEqualTo("foo/"));
}

[Test]
Expand Down Expand Up @@ -115,7 +115,7 @@ await FileSystem.ZipFile
FileSystem.ZipFile.Open("destination.zip", ZipArchiveMode.Read);

await That(archive.Entries).HasSingle()
.Which.For(x => x.FullName, f => f.IsEqualTo("foo/test.txt"));
.Which.Whose(x => x.FullName, f => f.IsEqualTo("foo/test.txt"));
}

[Test]
Expand All @@ -138,7 +138,7 @@ await FileSystem.ZipFile.CreateFromDirectoryAsync("foo", "destination.zip",
.Open("destination.zip", ZipArchiveMode.Read, encoding);

await That(archive.Entries).HasSingle()
.Which.For(x => x.FullName, f => f.IsEqualTo("test.txt"));
.Which.Whose(x => x.FullName, f => f.IsEqualTo("test.txt"));
}

[Test]
Expand Down Expand Up @@ -199,7 +199,7 @@ await FileSystem.ZipFile
using IZipArchive archive = FileSystem.ZipArchive.New(stream, ZipArchiveMode.Read);

await That(archive.Entries).HasSingle()
.Which.For(x => x.FullName, f => f.IsEqualTo("bar/"));
.Which.Whose(x => x.FullName, f => f.IsEqualTo("bar/"));
}

[Test]
Expand Down Expand Up @@ -238,7 +238,7 @@ await FileSystem.ZipFile
using IZipArchive archive = FileSystem.ZipArchive.New(stream, ZipArchiveMode.Read);

await That(archive.Entries).HasSingle()
.Which.For(x => x.FullName, f => f.IsEqualTo("foo/"));
.Which.Whose(x => x.FullName, f => f.IsEqualTo("foo/"));
}

[Test]
Expand Down Expand Up @@ -286,7 +286,7 @@ await FileSystem.ZipFile
using IZipArchive archive = FileSystem.ZipArchive.New(stream, ZipArchiveMode.Read);

await That(archive.Entries).HasSingle()
.Which.For(x => x.FullName, f => f.IsEqualTo("foo/test.txt"));
.Which.Whose(x => x.FullName, f => f.IsEqualTo("foo/test.txt"));
}

[Test]
Expand Down Expand Up @@ -343,7 +343,7 @@ await FileSystem.ZipFile.CreateFromDirectoryAsync("foo", stream,
FileSystem.ZipArchive.New(stream, ZipArchiveMode.Read, true, encoding);

await That(archive.Entries).HasSingle()
.Which.For(x => x.FullName, f => f.IsEqualTo("test.txt"));
.Which.Whose(x => x.FullName, f => f.IsEqualTo("test.txt"));
}

[Test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public async Task
FileSystem.ZipFile.Open("destination.zip", ZipArchiveMode.Read);

await That(archive.Entries).HasSingle()
.Which.For(x => x.FullName, f => f.IsEqualTo("bar/"));
.Which.Whose(x => x.FullName, f => f.IsEqualTo("bar/"));
}

[Test]
Expand Down Expand Up @@ -64,7 +64,7 @@ public async Task
FileSystem.ZipFile.Open("destination.zip", ZipArchiveMode.Read);

await That(archive.Entries).HasSingle()
.Which.For(x => x.FullName, f => f.IsEqualTo("foo/"));
.Which.Whose(x => x.FullName, f => f.IsEqualTo("foo/"));
}

[Test]
Expand Down Expand Up @@ -110,7 +110,7 @@ public async Task CreateFromDirectory_IncludeBaseDirectory_ShouldPrependDirector
FileSystem.ZipFile.Open("destination.zip", ZipArchiveMode.Read);

await That(archive.Entries).HasSingle()
.Which.For(x => x.FullName, f => f.IsEqualTo("foo/test.txt"));
.Which.Whose(x => x.FullName, f => f.IsEqualTo("foo/test.txt"));
}

#if FEATURE_COMPRESSION_OVERWRITE
Expand All @@ -134,7 +134,7 @@ public async Task CreateFromDirectory_Overwrite_WithEncoding_ShouldOverwriteFile
.Open("destination.zip", ZipArchiveMode.Read, encoding);

await That(archive.Entries).HasSingle()
.Which.For(x => x.FullName, f => f.IsEqualTo("test.txt"));
.Which.Whose(x => x.FullName, f => f.IsEqualTo("test.txt"));
}
#endif

Expand Down Expand Up @@ -197,7 +197,7 @@ public async Task
using IZipArchive archive = FileSystem.ZipArchive.New(stream, ZipArchiveMode.Read);

await That(archive.Entries).HasSingle()
.Which.For(x => x.FullName, f => f.IsEqualTo("bar/"));
.Which.Whose(x => x.FullName, f => f.IsEqualTo("bar/"));
}
#endif

Expand Down Expand Up @@ -238,7 +238,7 @@ public async Task
using IZipArchive archive = FileSystem.ZipArchive.New(stream, ZipArchiveMode.Read);

await That(archive.Entries).HasSingle()
.Which.For(x => x.FullName, f => f.IsEqualTo("foo/"));
.Which.Whose(x => x.FullName, f => f.IsEqualTo("foo/"));
}
#endif

Expand Down Expand Up @@ -289,7 +289,7 @@ public async Task
using IZipArchive archive = FileSystem.ZipArchive.New(stream, ZipArchiveMode.Read);

await That(archive.Entries).HasSingle()
.Which.For(x => x.FullName, f => f.IsEqualTo("foo/test.txt"));
.Which.Whose(x => x.FullName, f => f.IsEqualTo("foo/test.txt"));
}
#endif

Expand Down Expand Up @@ -351,7 +351,7 @@ public async Task CreateFromDirectory_WithStream_Overwrite_WithEncoding_ShouldOv
FileSystem.ZipArchive.New(stream, ZipArchiveMode.Read, true, encoding);

await That(archive.Entries).HasSingle()
.Which.For(x => x.FullName, f => f.IsEqualTo("test.txt"));
.Which.Whose(x => x.FullName, f => f.IsEqualTo("test.txt"));
}
#endif

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ void Act() =>
_ = fileSystem.Directory.EnumerateDirectories(path);

await That(Act).Throws<UnauthorizedAccessException>()
.WithMessageContaining($"'{sut.FullName}'").And
.WithMessage().Containing($"'{sut.FullName}'").And
.WithHResult(-2147024891);
}

Expand All @@ -42,7 +42,7 @@ void Act() =>
_ = fileSystem.Directory.EnumerateFiles(path);

await That(Act).Throws<UnauthorizedAccessException>()
.WithMessageContaining($"'{sut.FullName}'").And
.WithMessage().Containing($"'{sut.FullName}'").And
.WithHResult(-2147024891);
}

Expand All @@ -63,7 +63,7 @@ void Act() =>
_ = fileSystem.Directory.EnumerateFileSystemEntries(path);

await That(Act).Throws<UnauthorizedAccessException>()
.WithMessageContaining($"'{sut.FullName}'").And
.WithMessage().Containing($"'{sut.FullName}'").And
.WithHResult(-2147024891);
}
}
16 changes: 8 additions & 8 deletions Tests/Testably.Abstractions.Testing.Tests/RandomProviderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public async Task Default_ShouldReturnRandomGuid()
results.Add(randomProvider.GetGuid());
}

await That(results).AreAllUnique();
await That(results).All().AreUnique();
}

[Test]
Expand All @@ -34,7 +34,7 @@ public async Task Default_ShouldReturnRandomNumbers()
results.Add(randomProvider.GetRandom(i).Next());
}

await That(results).AreAllUnique();
await That(results).All().AreUnique();
}

[Test]
Expand Down Expand Up @@ -178,7 +178,7 @@ public async Task GenerateRandom_Next_WithoutGenerator_ShouldReturnRandomValues(
results.Add(random.Next());
}

await That(results).AreAllUnique();
await That(results).All().AreUnique();
}

[Test]
Expand Down Expand Up @@ -241,7 +241,7 @@ public async Task GenerateRandom_NextBytes_Span_WithoutGenerator_ShouldReturnRan
results.Add(buffer);
}

await That(results).AreAllUnique();
await That(results).All().AreUnique();
}
#endif

Expand Down Expand Up @@ -288,7 +288,7 @@ public async Task GenerateRandom_NextBytes_WithoutGenerator_ShouldReturnRandomVa
results.Add(buffer);
}

await That(results).AreAllUnique();
await That(results).All().AreUnique();
}

[Test]
Expand Down Expand Up @@ -349,7 +349,7 @@ public async Task GenerateRandom_NextDouble_WithoutGenerator_ShouldReturnRandomV
results.Add(random.NextDouble());
}

await That(results).AreAllUnique();
await That(results).All().AreUnique();
}

#if FEATURE_RANDOM_ADVANCED
Expand Down Expand Up @@ -455,7 +455,7 @@ public async Task GenerateRandom_NextInt64_WithoutGenerator_ShouldReturnRandomVa
results.Add(random.NextInt64());
}

await That(results).AreAllUnique();
await That(results).All().AreUnique();
}
#endif

Expand Down Expand Up @@ -495,7 +495,7 @@ public async Task GenerateRandom_NextSingle_WithoutGenerator_ShouldReturnRandomV
results.Add(random.NextSingle());
}

await That(results).AreAllUnique();
await That(results).All().AreUnique();
}
#endif

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public async Task Random_ShouldReturnRandomDateTime()
results.Add(TimeProviderFactory.Random().Create(_ => { }).Read());
});

await That(results).AreAllUnique();
await That(results).All().AreUnique();
}

[Test]
Expand Down
Loading
Loading