Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions learnings.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<Func<T, object?>>` would cause CS8620 for existing `Expression<Func<T, object>>` 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.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<object?> left,
Func<object?> right,
Expand All @@ -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)));
Expand Down
31 changes: 31 additions & 0 deletions src/SharpAssert.Runtime/Features/Shared/NativeOperators.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
33 changes: 33 additions & 0 deletions src/SharpAssert.Tests/Features/Async/AsyncBinaryFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<long> NextId() { await Task.Yield(); return 101L; }

Assert(await NextId() == 101);
}

[Test]
public async Task Should_order_different_number_types_by_value()
{
async Task<long> NextId() { await Task.Yield(); return 101L; }

Assert(await NextId() > 100);
}

[Test]
public async Task Should_compare_objects_with_their_equals()
{
async Task<Point> 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()
{
Expand Down Expand Up @@ -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);
}
}