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
149 changes: 149 additions & 0 deletions src/MrKWatkins.Assertions.Tests/AsyncActionAssertionsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
namespace MrKWatkins.Assertions.Tests;

public sealed class AsyncActionAssertionsTests
{
[Test]
public async Task ThrowAsync()
{
Func<Task> doesNotThrow = () => Task.CompletedTask;
Func<Task> doesNotThrowAsync = async () => await Task.Yield();

var exception = new InvalidOperationException("Test");
Func<Task> throws = () => throw exception;
Func<Task> throwsAsync = async () => { await Task.Yield(); throw exception; };

Func<Task> throwsWrongException = () => throw new NotSupportedException("Wrong");
Func<Task> throwsWrongExceptionAsync = async () => { await Task.Yield(); throw new NotSupportedException("Wrong"); };

await Assert.That(() => throws.Should().ThrowAsync<InvalidOperationException>()).ThrowsNothing();
await Assert.That(() => throwsAsync.Should().ThrowAsync<InvalidOperationException>()).ThrowsNothing();

await Assert.That(() => doesNotThrow.Should().ThrowAsync<InvalidOperationException>())
.Throws<AssertionException>().WithMessage("Function should throw an InvalidOperationException.");
await Assert.That(() => doesNotThrowAsync.Should().ThrowAsync<InvalidOperationException>())
.Throws<AssertionException>().WithMessage("Function should throw an InvalidOperationException.");

await Assert.That(() => throwsWrongException.Should().ThrowAsync<InvalidOperationException>())
.Throws<AssertionException>().WithMessage("Function should throw an InvalidOperationException but threw a NotSupportedException with message \"Wrong\".");
await Assert.That(() => throwsWrongExceptionAsync.Should().ThrowAsync<InvalidOperationException>())
.Throws<AssertionException>().WithMessage("Function should throw an InvalidOperationException but threw a NotSupportedException with message \"Wrong\".");
}

[Test]
public async Task ThrowAsync_Chain()
{
var exception = new InvalidOperationException("Test");
Func<Task> throws = () => throw exception;
Func<Task> throwsAsync = async () => { await Task.Yield(); throw exception; };

var chain = await throws.Should().ThrowAsync<InvalidOperationException>().ConfigureAwait(false);
await Assert.That(chain.Exception).IsSameReferenceAs(exception);
await Assert.That(chain.That).IsSameReferenceAs(exception);

chain = await throwsAsync.Should().ThrowAsync<InvalidOperationException>().ConfigureAwait(false);
await Assert.That(chain.Exception).IsSameReferenceAs(exception);
await Assert.That(chain.That).IsSameReferenceAs(exception);
}

[Test]
public async Task ThrowAsync_String()
{
Func<Task> doesNotThrow = () => Task.CompletedTask;
Func<Task> doesNotThrowAsync = async () => await Task.Yield();

var exception = new InvalidOperationException("Test");
Func<Task> throws = () => throw exception;
Func<Task> throwsAsync = async () => { await Task.Yield(); throw exception; };

Func<Task> throwsWrongException = () => throw new NotSupportedException("Wrong");
Func<Task> throwsWrongExceptionAsync = async () => { await Task.Yield(); throw new NotSupportedException("Wrong"); };

await Assert.That(() => throws.Should().ThrowAsync<InvalidOperationException>("Test")).ThrowsNothing();
await Assert.That(() => throwsAsync.Should().ThrowAsync<InvalidOperationException>("Test")).ThrowsNothing();

await Assert.That(() => throws.Should().ThrowAsync<InvalidOperationException>("Wrong Message"))
.Throws<AssertionException>().WithMessage("Value should have Message \"Wrong Message\" but was \"Test\".");
await Assert.That(() => throwsAsync.Should().ThrowAsync<InvalidOperationException>("Wrong Message"))
.Throws<AssertionException>().WithMessage("Value should have Message \"Wrong Message\" but was \"Test\".");

await Assert.That(() => doesNotThrow.Should().ThrowAsync<InvalidOperationException>("Test"))
.Throws<AssertionException>().WithMessage("Function should throw an InvalidOperationException.");
await Assert.That(() => doesNotThrowAsync.Should().ThrowAsync<InvalidOperationException>("Test"))
.Throws<AssertionException>().WithMessage("Function should throw an InvalidOperationException.");

await Assert.That(() => throwsWrongException.Should().ThrowAsync<InvalidOperationException>("Test"))
.Throws<AssertionException>().WithMessage("Function should throw an InvalidOperationException but threw a NotSupportedException with message \"Wrong\".");
await Assert.That(() => throwsWrongExceptionAsync.Should().ThrowAsync<InvalidOperationException>("Test"))
.Throws<AssertionException>().WithMessage("Function should throw an InvalidOperationException but threw a NotSupportedException with message \"Wrong\".");
}

[Test]
public async Task ThrowAsync_String_Chain()
{
var exception = new InvalidOperationException("Test");
Func<Task> throws = () => throw exception;
Func<Task> throwsAsync = async () => { await Task.Yield(); throw exception; };

var chain = await throws.Should().ThrowAsync<InvalidOperationException>("Test").ConfigureAwait(false);
await Assert.That(chain.Exception).IsSameReferenceAs(exception);
await Assert.That(chain.That).IsSameReferenceAs(exception);

chain = await throwsAsync.Should().ThrowAsync<InvalidOperationException>("Test").ConfigureAwait(false);
await Assert.That(chain.Exception).IsSameReferenceAs(exception);
await Assert.That(chain.That).IsSameReferenceAs(exception);
}

[Test]
public async Task NotThrowAsync()
{
Func<Task> doesNotThrow = () => Task.CompletedTask;
Func<Task> doesNotThrowAsync = async () => await Task.Yield();

var exception = new InvalidOperationException("Test");
Func<Task> throws = () => throw exception;
Func<Task> throwsAsync = async () => { await Task.Yield(); throw exception; };

await Assert.That(() => doesNotThrow.Should().NotThrowAsync()).ThrowsNothing();
await Assert.That(() => doesNotThrowAsync.Should().NotThrowAsync()).ThrowsNothing();

var actualException = await Assert.That(() => throws.Should().NotThrowAsync())
.Throws<AssertionException>()
.WithMessage("Function should not throw but threw an InvalidOperationException with message \"Test\".");
await Assert.That(actualException!.InnerException).IsSameReferenceAs(exception);

actualException = await Assert.That(() => throwsAsync.Should().NotThrowAsync())
.Throws<AssertionException>()
.WithMessage("Function should not throw but threw an InvalidOperationException with message \"Test\".");
await Assert.That(actualException!.InnerException).IsSameReferenceAs(exception);
}

[Test]
public async Task Awaiting_InvokingExtensions()
{
var value = new TestClass();

await Assert.That(() => value.Awaiting(v => v.ThrowAsync()).Should().ThrowAsync<InvalidOperationException>())
.ThrowsNothing();

await Assert.That(() => value.Awaiting(v => v.NotThrowAsync()).Should().NotThrowAsync())
.ThrowsNothing();
}

[Test]
public async Task Awaiting_InvokingExtensions_WithReturn()
{
var value = new TestClass();

await Assert.That(() => value.Awaiting(v => v.ThrowWithReturnAsync()).Should().ThrowAsync<InvalidOperationException>())
.ThrowsNothing();
}

private sealed class TestClass
{
public Task ThrowAsync() => throw new InvalidOperationException();

public async Task NotThrowAsync() => await Task.Yield();

public Task<string> ThrowWithReturnAsync() => throw new InvalidOperationException();
}
}
29 changes: 29 additions & 0 deletions src/MrKWatkins.Assertions.Tests/EnumerableAssertionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,4 +168,33 @@ public async Task NotSequenceEqual_Chain()
await Assert.That(chain.Value).IsEqualTo(value);
await Assert.That(chain.And.Value).IsEqualTo(value);
}

[Test]
public async Task Contain_Null()
{
IEnumerable<int> nullEnumerable = null!;

await Assert.That(() => nullEnumerable.Should().Contain(1)).Throws<AssertionException>()
.WithMessage("Value should not be null.");
}

[Test]
public async Task Contain()
{
var value = new List<int> { 1, 2, 3 };

await Assert.That(() => value.Should().Contain(2)).ThrowsNothing();
await Assert.That(() => value.Should().Contain(5)).Throws<AssertionException>()
.WithMessage("Value should contain 5 but did not.");
}

[Test]
public async Task Contain_Chain()
{
var value = new List<int> { 1, 2, 3 };

var chain = value.Should().Contain(2);
await Assert.That(chain.Value).IsEqualTo(value);
await Assert.That(chain.And.Value).IsEqualTo(value);
}
}
123 changes: 123 additions & 0 deletions src/MrKWatkins.Assertions.Tests/FloatingPointAssertionsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
namespace MrKWatkins.Assertions.Tests;

public sealed class FloatingPointAssertionsTests
{
[Test]
public async Task BeApproximately()
{
const double value = 17.5;

await Assert.That(() => value.Should().BeApproximately(17.5, 0.1)).ThrowsNothing();
await Assert.That(() => value.Should().BeApproximately(17.45, 0.1)).ThrowsNothing();
await Assert.That(() => value.Should().BeApproximately(17.55, 0.1)).ThrowsNothing();
await Assert.That(() => value.Should().BeApproximately(17.6, 0.1)).Throws<AssertionException>()
.WithMessage("Value should be approximately 17.6 (±0.1) but was 17.5.");
await Assert.That(() => value.Should().BeApproximately(17.4, 0.1)).Throws<AssertionException>()
.WithMessage("Value should be approximately 17.4 (±0.1) but was 17.5.");
}

[Test]
public async Task BeApproximately_Float()
{
const float value = 17.5f;

await Assert.That(() => value.Should().BeApproximately(17.5f, 0.1f)).ThrowsNothing();
await Assert.That(() => value.Should().BeApproximately(17.6f, 0.05f)).Throws<AssertionException>();
}

[Test]
public async Task BeApproximately_Chain()
{
const double value = 17.5;

var chain = value.Should().BeApproximately(17.5, 0.1);
await Assert.That(chain.Value).IsEqualTo(value);
await Assert.That(chain.And.Value).IsEqualTo(value);
}

[Test]
public async Task BeLessThan()
{
const double value = 5.0;

await Assert.That(() => value.Should().BeLessThan(10.0, 0.001)).ThrowsNothing();
await Assert.That(() => value.Should().BeLessThan(5.0, 0.001)).Throws<AssertionException>()
.WithMessage("Value should be less than 5 (±0.001) but was 5.");
await Assert.That(() => value.Should().BeLessThan(1.0, 0.001)).Throws<AssertionException>()
.WithMessage("Value should be less than 1 (±0.001) but was 5.");
}

[Test]
public async Task BeLessThan_Chain()
{
const double value = 5.0;

var chain = value.Should().BeLessThan(10.0, 0.001);
await Assert.That(chain.Value).IsEqualTo(value);
await Assert.That(chain.And.Value).IsEqualTo(value);
}

[Test]
public async Task BeLessThanOrEqualTo()
{
const double value = 5.0;

await Assert.That(() => value.Should().BeLessThanOrEqualTo(10.0, 0.001)).ThrowsNothing();
await Assert.That(() => value.Should().BeLessThanOrEqualTo(5.0, 0.001)).ThrowsNothing();
await Assert.That(() => value.Should().BeLessThanOrEqualTo(1.0, 0.001)).Throws<AssertionException>()
.WithMessage("Value should be less than or equal to 1 (±0.001) but was 5.");
}

[Test]
public async Task BeLessThanOrEqualTo_Chain()
{
const double value = 5.0;

var chain = value.Should().BeLessThanOrEqualTo(5.0, 0.001);
await Assert.That(chain.Value).IsEqualTo(value);
await Assert.That(chain.And.Value).IsEqualTo(value);
}

[Test]
public async Task BeGreaterThan()
{
const double value = 5.0;

await Assert.That(() => value.Should().BeGreaterThan(1.0, 0.001)).ThrowsNothing();
await Assert.That(() => value.Should().BeGreaterThan(5.0, 0.001)).Throws<AssertionException>()
.WithMessage("Value should be greater than 5 (±0.001) but was 5.");
await Assert.That(() => value.Should().BeGreaterThan(10.0, 0.001)).Throws<AssertionException>()
.WithMessage("Value should be greater than 10 (±0.001) but was 5.");
}

[Test]
public async Task BeGreaterThan_Chain()
{
const double value = 5.0;

var chain = value.Should().BeGreaterThan(1.0, 0.001);
await Assert.That(chain.Value).IsEqualTo(value);
await Assert.That(chain.And.Value).IsEqualTo(value);
}

[Test]
public async Task BeGreaterThanOrEqualTo()
{
const double value = 5.0;

await Assert.That(() => value.Should().BeGreaterThanOrEqualTo(1.0, 0.001)).ThrowsNothing();
await Assert.That(() => value.Should().BeGreaterThanOrEqualTo(5.0, 0.001)).ThrowsNothing();
await Assert.That(() => value.Should().BeGreaterThanOrEqualTo(10.0, 0.001)).Throws<AssertionException>()
.WithMessage("Value should be greater than or equal to 10 (±0.001) but was 5.");
}

[Test]
public async Task BeGreaterThanOrEqualTo_Chain()
{
const double value = 5.0;

var chain = value.Should().BeGreaterThanOrEqualTo(5.0, 0.001);
await Assert.That(chain.Value).IsEqualTo(value);
await Assert.That(chain.And.Value).IsEqualTo(value);
}
}
80 changes: 80 additions & 0 deletions src/MrKWatkins.Assertions.Tests/IntegerAssertionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -270,4 +270,84 @@ public async Task ChainWithInheritedMethods()
await Assert.That(() => value.Should().BePositive().And.NotEqual(0)).ThrowsNothing();
await Assert.That(() => value.Should().NotBeZero().And.Equal(42)).ThrowsNothing();
}

[Test]
public async Task BeLessThan()
{
const int value = 5;

await Assert.That(() => value.Should().BeLessThan(10)).ThrowsNothing();
await Assert.That(() => value.Should().BeLessThan(5)).Throws<AssertionException>().WithMessage("Value should be less than 5 but was 5.");
await Assert.That(() => value.Should().BeLessThan(1)).Throws<AssertionException>().WithMessage("Value should be less than 1 but was 5.");
}

[Test]
public async Task BeLessThan_Chain()
{
const int value = 5;

var chain = value.Should().BeLessThan(10);
await Assert.That(chain.Value).IsEqualTo(value);
await Assert.That(chain.And.Value).IsEqualTo(value);
}

[Test]
public async Task BeLessThanOrEqualTo()
{
const int value = 5;

await Assert.That(() => value.Should().BeLessThanOrEqualTo(10)).ThrowsNothing();
await Assert.That(() => value.Should().BeLessThanOrEqualTo(5)).ThrowsNothing();
await Assert.That(() => value.Should().BeLessThanOrEqualTo(1)).Throws<AssertionException>().WithMessage("Value should be less than or equal to 1 but was 5.");
}

[Test]
public async Task BeLessThanOrEqualTo_Chain()
{
const int value = 5;

var chain = value.Should().BeLessThanOrEqualTo(5);
await Assert.That(chain.Value).IsEqualTo(value);
await Assert.That(chain.And.Value).IsEqualTo(value);
}

[Test]
public async Task BeGreaterThan()
{
const int value = 5;

await Assert.That(() => value.Should().BeGreaterThan(1)).ThrowsNothing();
await Assert.That(() => value.Should().BeGreaterThan(5)).Throws<AssertionException>().WithMessage("Value should be greater than 5 but was 5.");
await Assert.That(() => value.Should().BeGreaterThan(10)).Throws<AssertionException>().WithMessage("Value should be greater than 10 but was 5.");
}

[Test]
public async Task BeGreaterThan_Chain()
{
const int value = 5;

var chain = value.Should().BeGreaterThan(1);
await Assert.That(chain.Value).IsEqualTo(value);
await Assert.That(chain.And.Value).IsEqualTo(value);
}

[Test]
public async Task BeGreaterThanOrEqualTo()
{
const int value = 5;

await Assert.That(() => value.Should().BeGreaterThanOrEqualTo(1)).ThrowsNothing();
await Assert.That(() => value.Should().BeGreaterThanOrEqualTo(5)).ThrowsNothing();
await Assert.That(() => value.Should().BeGreaterThanOrEqualTo(10)).Throws<AssertionException>().WithMessage("Value should be greater than or equal to 10 but was 5.");
}

[Test]
public async Task BeGreaterThanOrEqualTo_Chain()
{
const int value = 5;

var chain = value.Should().BeGreaterThanOrEqualTo(5);
await Assert.That(chain.Value).IsEqualTo(value);
await Assert.That(chain.And.Value).IsEqualTo(value);
}
}
Loading
Loading