feat: support .Within tolerance on numeric comparison expectations#943
Conversation
There was a problem hiding this comment.
Pull request overview
This PR extends aweXpect’s numeric .Within(tolerance) option (previously available on equality/one-of expectations) to comparison and range expectations, enabling tolerant comparisons like IsGreaterThan(5).Within(1).
Changes:
- Add tolerance-aware result types (
NumberToleranceResult/NullableNumberToleranceResult) toIsGreaterThan,IsGreaterThanOrEqualTo,IsLessThan,IsLessThanOrEqualTo,IsBetween, andIsNotBetween. - Implement tolerance-aware comparison/range logic via new helpers on
NumberTolerance<TNumber>. - Add/extend unit tests for the new
.Within(...)behavior across several numeric types.
Reviewed changes
Copilot reviewed 16 out of 16 changed files in this pull request and generated 10 comments.
Show a summary per file
| File | Description |
|---|---|
| Tests/aweXpect.Tests/Numbers/ThatNumber.IsLessThanOrEqualTo.WithinTests.cs | New tests for .Within(...) on IsLessThanOrEqualTo. |
| Tests/aweXpect.Tests/Numbers/ThatNumber.IsLessThanOrEqualTo.Tests.cs | Make nested test class partial to allow additional test files. |
| Tests/aweXpect.Tests/Numbers/ThatNumber.IsLessThan.WithinTests.cs | New tests for .Within(...) on IsLessThan (incl. strict vs tolerant behavior). |
| Tests/aweXpect.Tests/Numbers/ThatNumber.IsLessThan.Tests.cs | Make nested test class partial to allow additional test files. |
| Tests/aweXpect.Tests/Numbers/ThatNumber.IsGreaterThanOrEqualTo.WithinTests.cs | New tests for .Within(...) on IsGreaterThanOrEqualTo. |
| Tests/aweXpect.Tests/Numbers/ThatNumber.IsGreaterThanOrEqualTo.Tests.cs | Make nested test class partial to allow additional test files. |
| Tests/aweXpect.Tests/Numbers/ThatNumber.IsGreaterThan.WithinTests.cs | New tests for .Within(...) on IsGreaterThan (incl. strict vs tolerant behavior). |
| Tests/aweXpect.Tests/Numbers/ThatNumber.IsGreaterThan.Tests.cs | Make nested test class partial to allow additional test files. |
| Tests/aweXpect.Tests/Numbers/ThatNumber.IsBetween.WithinTests.cs | New tests for widening range semantics with .Within(...) on IsBetween/IsNotBetween. |
| Tests/aweXpect.Tests/Numbers/ThatNumber.IsBetween.Tests.cs | Make nested test class partial to allow additional test files. |
| Source/aweXpect/That/Numbers/ThatNumber.IsLessThanOrEqualTo.cs | Change return types to tolerance-capable results and route evaluation through NumberTolerance. |
| Source/aweXpect/That/Numbers/ThatNumber.IsLessThan.cs | Change return types to tolerance-capable results and route evaluation through NumberTolerance. |
| Source/aweXpect/That/Numbers/ThatNumber.IsGreaterThanOrEqualTo.cs | Change return types to tolerance-capable results and route evaluation through NumberTolerance. |
| Source/aweXpect/That/Numbers/ThatNumber.IsGreaterThan.cs | Change return types to tolerance-capable results and route evaluation through NumberTolerance. |
| Source/aweXpect/That/Numbers/ThatNumber.IsBetween.cs | Make IsBetween/IsNotBetween produce tolerance-capable results and apply tolerance to range bounds. |
| Source/aweXpect.Core/Options/NumberTolerance.cs | Add tolerance-aware comparison and range helper methods. |
Test Results 23 files - 27 23 suites - 27 7m 47s ⏱️ +29s Results for commit 061a167. ± Comparison against base commit 6c19073. This pull request removes 3102 and adds 3305 tests. Note that renamed tests count towards both.♻️ This comment has been updated with latest results. |
Extend the existing .Within(tolerance) mechanism — previously only available on IsEqualTo / IsNotEqualTo / IsOneOf — to ThatNumber's comparison and range methods: IsGreaterThan, IsGreaterThanOrEqualTo, IsLessThan, IsLessThanOrEqualTo, IsBetween, and IsNotBetween. When .Within(t) is set, strict comparisons become inclusive at the new boundary (e.g. IsGreaterThan(5).Within(1) accepts actual >= 4); without .Within, the original strict behavior is preserved. Closes #894
Guards `NumberTolerance` comparison helpers against `OverflowException` from checked difference lambdas (e.g. `sbyte.MinValue` vs `sbyte.MaxValue`) via a shared `TryCalculateDifference` helper, aligns the netstandard2.0 nullable `IsBetween` constraint with the NET8+ variant by inheriting from `WithEqualToValue`, and expands `Within` test coverage (nullable overloads, expected-NaN, overflow-safe sbyte, OrEqualTo parity, float/long failure paths).
80d3ac0 to
2d04373
Compare
🚀 Benchmark ResultsDetails
|
👽 Mutation ResultsaweXpectDetails
The final mutation score is 87.95%Coverage Thresholds: high:80 low:60 break:0aweXpect.CoreDetails
The final mutation score is NaN%Coverage Thresholds: high:80 low:60 break:0 |
.Within tolerance on numeric comparison expectations
0828fd3 to
061a167
Compare
|
…ric comparison expectations (#943) by Valentin Breuß
…ric comparison expectations (#943) by Valentin Breuß
|
This is addressed in release v2.32.0. |



Extend the existing .Within(tolerance) mechanism — previously only available on IsEqualTo / IsNotEqualTo / IsOneOf — to ThatNumber's comparison and range methods: IsGreaterThan, IsGreaterThanOrEqualTo, IsLessThan, IsLessThanOrEqualTo, IsBetween, and IsNotBetween.
When .Within(t) is set, strict comparisons become inclusive at the new boundary (e.g. IsGreaterThan(5).Within(1) accepts actual >= 4); without .Within, the original strict behavior is preserved.
.Withintolerance also for other numeric expectations #894