-
Notifications
You must be signed in to change notification settings - Fork 0
Add floating-point assertions, numeric comparison assertions, Contain for enumerables, and async action assertions
#195
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
20f782b
Initial plan
Copilot 7b2c644
Add FloatingPointAssertions, numeric comparison methods for IntegerAs…
Copilot fd1d985
Add AsyncActionAssertions with ThrowAsync/NotThrowAsync, Should(Func<…
Copilot be0d89e
Add precision to floating-point comparison methods; expand async acti…
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
149 changes: 149 additions & 0 deletions
149
src/MrKWatkins.Assertions.Tests/AsyncActionAssertionsTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
123 changes: 123 additions & 0 deletions
123
src/MrKWatkins.Assertions.Tests/FloatingPointAssertionsTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.