-
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 2 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
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)).ThrowsNothing(); | ||
| await Assert.That(() => value.Should().BeLessThan(5.0)).Throws<AssertionException>() | ||
| .WithMessage("Value should be less than 5 but was 5."); | ||
| await Assert.That(() => value.Should().BeLessThan(1.0)).Throws<AssertionException>() | ||
| .WithMessage("Value should be less than 1 but was 5."); | ||
| } | ||
|
|
||
| [Test] | ||
| public async Task BeLessThan_Chain() | ||
| { | ||
| const double value = 5.0; | ||
|
|
||
| var chain = value.Should().BeLessThan(10.0); | ||
| 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)).ThrowsNothing(); | ||
| await Assert.That(() => value.Should().BeLessThanOrEqualTo(5.0)).ThrowsNothing(); | ||
| await Assert.That(() => value.Should().BeLessThanOrEqualTo(1.0)).Throws<AssertionException>() | ||
| .WithMessage("Value should be less than or equal to 1 but was 5."); | ||
| } | ||
|
|
||
| [Test] | ||
| public async Task BeLessThanOrEqualTo_Chain() | ||
| { | ||
| const double value = 5.0; | ||
|
|
||
| var chain = value.Should().BeLessThanOrEqualTo(5.0); | ||
| 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)).ThrowsNothing(); | ||
| await Assert.That(() => value.Should().BeGreaterThan(5.0)).Throws<AssertionException>() | ||
| .WithMessage("Value should be greater than 5 but was 5."); | ||
| await Assert.That(() => value.Should().BeGreaterThan(10.0)).Throws<AssertionException>() | ||
| .WithMessage("Value should be greater than 10 but was 5."); | ||
| } | ||
|
|
||
| [Test] | ||
| public async Task BeGreaterThan_Chain() | ||
| { | ||
| const double value = 5.0; | ||
|
|
||
| var chain = value.Should().BeGreaterThan(1.0); | ||
| 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)).ThrowsNothing(); | ||
| await Assert.That(() => value.Should().BeGreaterThanOrEqualTo(5.0)).ThrowsNothing(); | ||
| await Assert.That(() => value.Should().BeGreaterThanOrEqualTo(10.0)).Throws<AssertionException>() | ||
| .WithMessage("Value should be greater than or equal to 10 but was 5."); | ||
| } | ||
|
|
||
| [Test] | ||
| public async Task BeGreaterThanOrEqualTo_Chain() | ||
| { | ||
| const double value = 5.0; | ||
|
|
||
| var chain = value.Should().BeGreaterThanOrEqualTo(5.0); | ||
| 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
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
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,73 @@ | ||
| using System.Numerics; | ||
|
|
||
| namespace MrKWatkins.Assertions; | ||
|
|
||
| /// <summary> | ||
| /// Provides assertions for floating-point values. | ||
| /// </summary> | ||
| /// <typeparam name="T">The floating-point type of the value being asserted on.</typeparam> | ||
| /// <param name="value">The value to assert on.</param> | ||
| public sealed class FloatingPointAssertions<T>(T value) : ObjectAssertions<T>(value) | ||
| where T : struct, IFloatingPoint<T> | ||
| { | ||
| /// <summary> | ||
| /// Asserts that the floating-point value is approximately equal to the expected value within the specified precision. | ||
| /// </summary> | ||
| /// <param name="expected">The expected value.</param> | ||
| /// <param name="precision">The maximum allowed difference between the value and the expected value.</param> | ||
| /// <returns>A <see cref="FloatingPointAssertionsChain{T}" /> for chaining further assertions.</returns> | ||
| public FloatingPointAssertionsChain<T> BeApproximately(T expected, T precision) | ||
| { | ||
| Verify.That(T.Abs(Value - expected) <= precision, $"Value should be approximately {expected} (±{precision}) but was {Value}."); | ||
|
|
||
| return new FloatingPointAssertionsChain<T>(this); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Asserts that the floating-point value is less than the expected value. | ||
| /// </summary> | ||
| /// <param name="expected">The value the floating-point number should be less than.</param> | ||
| /// <returns>A <see cref="FloatingPointAssertionsChain{T}" /> for chaining further assertions.</returns> | ||
| public FloatingPointAssertionsChain<T> BeLessThan(T expected) | ||
| { | ||
| Verify.That(Value < expected, $"Value should be less than {expected} but was {Value}."); | ||
|
|
||
| return new FloatingPointAssertionsChain<T>(this); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Asserts that the floating-point value is less than or equal to the expected value. | ||
| /// </summary> | ||
| /// <param name="expected">The value the floating-point number should be less than or equal to.</param> | ||
| /// <returns>A <see cref="FloatingPointAssertionsChain{T}" /> for chaining further assertions.</returns> | ||
| public FloatingPointAssertionsChain<T> BeLessThanOrEqualTo(T expected) | ||
| { | ||
| Verify.That(Value <= expected, $"Value should be less than or equal to {expected} but was {Value}."); | ||
|
|
||
| return new FloatingPointAssertionsChain<T>(this); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Asserts that the floating-point value is greater than the expected value. | ||
| /// </summary> | ||
| /// <param name="expected">The value the floating-point number should be greater than.</param> | ||
| /// <returns>A <see cref="FloatingPointAssertionsChain{T}" /> for chaining further assertions.</returns> | ||
| public FloatingPointAssertionsChain<T> BeGreaterThan(T expected) | ||
| { | ||
| Verify.That(Value > expected, $"Value should be greater than {expected} but was {Value}."); | ||
|
|
||
| return new FloatingPointAssertionsChain<T>(this); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Asserts that the floating-point value is greater than or equal to the expected value. | ||
| /// </summary> | ||
| /// <param name="expected">The value the floating-point number should be greater than or equal to.</param> | ||
| /// <returns>A <see cref="FloatingPointAssertionsChain{T}" /> for chaining further assertions.</returns> | ||
| public FloatingPointAssertionsChain<T> BeGreaterThanOrEqualTo(T expected) | ||
| { | ||
| Verify.That(Value >= expected, $"Value should be greater than or equal to {expected} but was {Value}."); | ||
|
|
||
| return new FloatingPointAssertionsChain<T>(this); | ||
| } | ||
| } | ||
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,22 @@ | ||
| using System.Numerics; | ||
|
|
||
| namespace MrKWatkins.Assertions; | ||
|
|
||
| /// <summary> | ||
| /// Enables chaining of assertions on a floating-point value after a successful assertion. | ||
| /// </summary> | ||
| /// <typeparam name="T">The floating-point type of the value being asserted on.</typeparam> | ||
| /// <param name="floatingPointAssertions">The assertions object to chain from.</param> | ||
| public readonly struct FloatingPointAssertionsChain<T>(FloatingPointAssertions<T> floatingPointAssertions) | ||
| where T : struct, IFloatingPoint<T> | ||
| { | ||
| /// <summary> | ||
| /// Gets the assertions object for chaining further assertions. | ||
| /// </summary> | ||
| public FloatingPointAssertions<T> And { get; } = floatingPointAssertions; | ||
|
|
||
| /// <summary> | ||
| /// Gets the floating-point value being asserted on. | ||
| /// </summary> | ||
| public T Value => And.Value; | ||
| } |
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.