diff --git a/CHANGELOG.md b/CHANGELOG.md index f2e1ed8..267cfff 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## Unreleased +- Fixed awaited comparisons of numbers with different types, such as `Assert(await GetLongAsync() == 101)`. Equality failed with two equal values in the message, and `<`, `>` threw `ArgumentException`. Numbers now compare by value, as in compiled C#; other values still compare with `Equals`. + ## 2.0.0 - **Breaking change:** Restored C# short-circuit evaluation for assertion `&&` operators, including async, dynamic, local-function, and pattern expressions. Conditions after the first false operand no longer run. Null and type guards keep their native behavior. diff --git a/learnings.md b/learnings.md index b74249c..69af0d7 100644 --- a/learnings.md +++ b/learnings.md @@ -100,6 +100,7 @@ This document is organized by topic to consolidate key learnings about the proje - `scripts/test-nunit-output.sh` checks an intentional failure outside the solution with NUnit 4.6.1 and adapter 6.3.0; a nonzero test exit alone is not enough to prove diagnostics. - DateTimeOffset equivalency needs a native `==` comparer: the dependency's built-in comparer compares local clock values and ignores sub-millisecond differences. Put explicit custom comparers before the default so `EqualsExact` can compare offsets. +- Awaited binary comparisons receive boxed operands: `Equals` and `Comparer.Default` treat a boxed `long` and `int` as different types. Use native operators for two numbers, and keep `Equals` for other values to match the synchronous path, where `==` on a class with its own `Equals` passes. - Nullable equivalency selectors use generic overloads. The legacy non-generic signatures use nullable-oblivious annotations: `Expression>` would cause CS8620 for existing `Expression>` variables. Keep nullable warnings enabled and test both forms. For a generic selector, unwrap only conversion nodes; a unary operation such as `-item.Id` is not a member selector. diff --git a/src/SharpAssert.Runtime/Features/Async/AsyncExpressionAnalyzer.cs b/src/SharpAssert.Runtime/Features/Async/AsyncExpressionAnalyzer.cs index 1d598fa..d1907e4 100644 --- a/src/SharpAssert.Runtime/Features/Async/AsyncExpressionAnalyzer.cs +++ b/src/SharpAssert.Runtime/Features/Async/AsyncExpressionAnalyzer.cs @@ -7,7 +7,15 @@ namespace SharpAssert.Features.Async; class AsyncExpressionAnalyzer { - static bool EvaluateBinaryComparison(BinaryOp op, object? leftValue, object? rightValue) => op switch + static bool EvaluateBinaryComparison(BinaryOp op, object? leftValue, object? rightValue) => + IsNumber(leftValue) && IsNumber(rightValue) + ? NativeOperators.Evaluate(op, leftValue, rightValue) + : EvaluateWithEquals(op, leftValue, rightValue); + + static bool IsNumber(object? value) => + value is sbyte or byte or short or ushort or int or uint or long or ulong or float or double or decimal; + + static bool EvaluateWithEquals(BinaryOp op, object? leftValue, object? rightValue) => op switch { BinaryOp.Eq => Equals(leftValue, rightValue), BinaryOp.Ne => !Equals(leftValue, rightValue), diff --git a/src/SharpAssert.Runtime/Features/Dynamic/DynamicExpressionAnalyzer.cs b/src/SharpAssert.Runtime/Features/Dynamic/DynamicExpressionAnalyzer.cs index 2b62407..90498a3 100644 --- a/src/SharpAssert.Runtime/Features/Dynamic/DynamicExpressionAnalyzer.cs +++ b/src/SharpAssert.Runtime/Features/Dynamic/DynamicExpressionAnalyzer.cs @@ -6,28 +6,6 @@ namespace SharpAssert.Features.Dynamic; static class DynamicExpressionAnalyzer { - static bool EvaluateDynamicBinaryComparison(BinaryOp op, object? leftValue, object? rightValue) - { - try - { - return op switch - { - BinaryOp.Eq => (dynamic?)leftValue == (dynamic?)rightValue, - BinaryOp.Ne => (dynamic?)leftValue != (dynamic?)rightValue, - BinaryOp.Lt => (dynamic?)leftValue < (dynamic?)rightValue, - BinaryOp.Le => (dynamic?)leftValue <= (dynamic?)rightValue, - BinaryOp.Gt => (dynamic?)leftValue > (dynamic?)rightValue, - BinaryOp.Ge => (dynamic?)leftValue >= (dynamic?)rightValue, - _ => false - }; - } - catch - { - // If dynamic operation fails, fall back to false (comparison failed) - return false; - } - } - public static AssertionEvaluationResult AnalyzeBinary( Func left, Func right, @@ -37,7 +15,7 @@ public static AssertionEvaluationResult AnalyzeBinary( var leftValue = left(); var rightValue = right(); - var comparisonResult = EvaluateDynamicBinaryComparison(op, leftValue, rightValue); + var comparisonResult = NativeOperators.Evaluate(op, leftValue, rightValue); if (comparisonResult) return new AssertionEvaluationResult(context, new ValueEvaluationResult(context.Expression, true, typeof(bool))); diff --git a/src/SharpAssert.Runtime/Features/Shared/NativeOperators.cs b/src/SharpAssert.Runtime/Features/Shared/NativeOperators.cs new file mode 100644 index 0000000..bedf6e2 --- /dev/null +++ b/src/SharpAssert.Runtime/Features/Shared/NativeOperators.cs @@ -0,0 +1,31 @@ +// ABOUTME: Evaluates comparison operators on boxed values with C# operator semantics. +// ABOUTME: Uses dynamic binding, so numbers of different types and user-defined operators behave as in compiled code. + +using SharpAssert.Core; + +namespace SharpAssert.Features.Shared; + +static class NativeOperators +{ + public static bool Evaluate(BinaryOp op, object? leftValue, object? rightValue) + { + try + { + return op switch + { + BinaryOp.Eq => (dynamic?)leftValue == (dynamic?)rightValue, + BinaryOp.Ne => (dynamic?)leftValue != (dynamic?)rightValue, + BinaryOp.Lt => (dynamic?)leftValue < (dynamic?)rightValue, + BinaryOp.Le => (dynamic?)leftValue <= (dynamic?)rightValue, + BinaryOp.Gt => (dynamic?)leftValue > (dynamic?)rightValue, + BinaryOp.Ge => (dynamic?)leftValue >= (dynamic?)rightValue, + _ => false + }; + } + catch + { + // If dynamic operation fails, fall back to false (comparison failed) + return false; + } + } +} diff --git a/src/SharpAssert.Tests/Features/Async/AsyncBinaryFixture.cs b/src/SharpAssert.Tests/Features/Async/AsyncBinaryFixture.cs index 5356ff3..0777e1f 100644 --- a/src/SharpAssert.Tests/Features/Async/AsyncBinaryFixture.cs +++ b/src/SharpAssert.Tests/Features/Async/AsyncBinaryFixture.cs @@ -36,6 +36,30 @@ public async Task Should_handle_mixed_async_sync() await AssertFailsAsync(async () => Assert(await GetAsyncValue() == 24), expected); } + [Test] + public async Task Should_compare_different_number_types_by_value() + { + async Task NextId() { await Task.Yield(); return 101L; } + + Assert(await NextId() == 101); + } + + [Test] + public async Task Should_order_different_number_types_by_value() + { + async Task NextId() { await Task.Yield(); return 101L; } + + Assert(await NextId() > 100); + } + + [Test] + public async Task Should_compare_objects_with_their_equals() + { + async Task Origin() { await Task.Yield(); return new Point(0, 0); } + + Assert(await Origin() == new Point(0, 0)); + } + [Test] public async Task Should_evaluate_in_source_order() { @@ -86,4 +110,13 @@ public void Should_render_async_comparison() static BinaryComparison.DefaultComparisonResult Comparison(object? left, object? right) => new(Operand(left), Operand(right)); + + class Point(int x, int y) + { + public int X { get; } = x; + public int Y { get; } = y; + + public override bool Equals(object? other) => other is Point point && point.X == X && point.Y == Y; + public override int GetHashCode() => HashCode.Combine(X, Y); + } }