Add dedicated IntegerAssertions type with cross-type comparisons#190
Add dedicated IntegerAssertions type with cross-type comparisons#190MrKWatkins merged 8 commits intomainfrom
Conversation
- Updated NumericExtensions.Equal and NotEqual to accept different numeric types - Moved from single generic constraint to dual generic constraints (T and TOther) - Both methods now use T.CreateChecked to convert values for comparison - All existing numeric assertion methods (BeZero, BePositive, etc.) remain unchanged - Replaced NumericExtensionsTests with NumericAssertionsTests - All 349 tests pass including new cross-type comparison tests Co-authored-by: MrKWatkins <345796+MrKWatkins@users.noreply.github.com>
Updated comment to accurately reflect that these are extension methods on ObjectAssertions, not a separate NumericAssertions class. Co-authored-by: MrKWatkins <345796+MrKWatkins@users.noreply.github.com>
- Added exception handling for OverflowException in Equal and NotEqual methods - For Equal: throws AssertionException with descriptive message when conversion overflows - For NotEqual: returns success when expected value can't be represented in target type - Added XML documentation remarks explaining overflow behavior - Added tests for overflow scenarios (byte with 300) - All 351 tests pass Co-authored-by: MrKWatkins <345796+MrKWatkins@users.noreply.github.com>
- Fixed XML documentation for NotEqual to clarify it succeeds (not throws) on overflow - Changed unreachable throw to return default in Equal method - All 351 tests pass Co-authored-by: MrKWatkins <345796+MrKWatkins@users.noreply.github.com>
- Changed return default to return default! with clearer comment - All 351 tests pass Co-authored-by: MrKWatkins <345796+MrKWatkins@users.noreply.github.com>
| @@ -7,14 +7,50 @@ namespace MrKWatkins.Assertions; | |||
| /// </summary> | |||
| public static class NumericExtensions | |||
There was a problem hiding this comment.
I said I wanted a NumericAssertions type, rather than extensions. See StringAssertions or ExceptionAssertions for an example.
There was a problem hiding this comment.
Refactored to use a dedicated NumericAssertions<T> type following the pattern of StringAssertions and ExceptionAssertions. Created specific Should() overloads for all built-in numeric types (byte, sbyte, short, ushort, int, uint, long, ulong, float, double, decimal, nint, nuint) to properly route numeric values to the NumericAssertions<T> class. Commit: bfb4ebf
- Created NumericAssertions<T> class inheriting from ObjectAssertions<T> - Created NumericAssertionsChain<T> struct for fluent chaining - Moved all numeric assertion methods from NumericExtensions to NumericAssertions - Added specific Should() overloads for all built-in numeric types (byte, sbyte, short, ushort, int, uint, long, ulong, float, double, decimal, nint, nuint) - Removed NumericExtensions.cs as functionality moved to NumericAssertions - All 351 tests pass Co-authored-by: MrKWatkins <345796+MrKWatkins@users.noreply.github.com>
| /// </summary> | ||
| /// <typeparam name="T">The numeric type of the value being asserted on.</typeparam> | ||
| /// <param name="value">The value to assert on.</param> | ||
| public sealed class NumericAssertions<T>(T value) : ObjectAssertions<T>(value) |
There was a problem hiding this comment.
Can we rename to IntegerAssertions and use IBinaryInteger for the constraint instead?
There was a problem hiding this comment.
Renamed to IntegerAssertions<T> with IBinaryInteger<T> constraint. This excludes float, double, and decimal types, focusing only on integer types. Removed the Should() overloads for non-integer types and updated all references. Commit: 23dae4b
…straint - Renamed NumericAssertions<T> to IntegerAssertions<T> - Renamed NumericAssertionsChain<T> to IntegerAssertionsChain<T> - Changed constraint from INumberBase<T> to IBinaryInteger<T> - Removed Should() overloads for float, double, and decimal (non-integer types) - Updated all references in tests and documentation - Removed Equal_FloatingPoint test as it's no longer applicable - All 350 tests pass Co-authored-by: MrKWatkins <345796+MrKWatkins@users.noreply.github.com>
Integer assertions previously required exact type matching, causing
someByte.Should().Equal(0)to fail since0is anint. This PR adds a dedicatedIntegerAssertions<T>type following the pattern ofStringAssertionsandExceptionAssertions.Changes
IntegerAssertions<T>class: Dedicated assertion type inheriting fromObjectAssertions<T>withwhere T : struct, IBinaryInteger<T>constraintIntegerAssertionsChain<T>struct: Enables fluent chaining with.AndpropertyNumericExtensionstoIntegerAssertions<T>TandTOther), both constrained tostruct, IBinaryInteger<T>T.CreateChecked(expected)to convert comparison values at assertion timeEqual: ThrowsAssertionExceptionwith descriptive message when conversion overflowsNotEqual: Succeeds when expected value cannot be represented in target type (values cannot be equal)IntegerAssertions<T>NumericExtensions.cs: All functionality moved to the new classNote: This implementation uses
IBinaryInteger<T>constraint to support only integer types, excluding floating-point types (float, double, decimal) which have different mathematical properties.Example
All 350 tests pass.
Original prompt
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.