From 52e2f0c5d1fcd339a0462d5ceb4fdf46c8da3f0a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Valentin=20Breu=C3=9F?= Date: Sat, 12 Sep 2026 14:55:18 +0200 Subject: [PATCH 1/2] fix: do not throw when the subject of `IsEquatableTo` is `null` `IsEquatableTo` called `actual.Equals(expected)` without a null check, so a `null` subject aborted the evaluation with a `NullReferenceException` (surfaced as `InvalidOperationException: Error evaluating ... constraint`) instead of failing the expectation. A `null` subject now fails `IsEquatableTo` and satisfies `IsNotEquatableTo`, consistent with `IsEqualTo`/`IsNotEqualTo`. The constraint also derived from `ConstraintResult.WithValue>` instead of `WithValue`, which made `TryGetValue` report a type mismatch whenever the constraint succeeded with a `null` subject. Every other constraint passes its own subject type, so the type argument is corrected. --- .../That/ThatGeneric.IsEquatableTo.cs | 4 +-- .../ThatGeneric.IsEquatableTo.Tests.cs | 36 +++++++++++++++++++ .../ThatGeneric.IsNotEquatableTo.Tests.cs | 36 +++++++++++++++++++ 3 files changed, 74 insertions(+), 2 deletions(-) diff --git a/Source/aweXpect/That/ThatGeneric.IsEquatableTo.cs b/Source/aweXpect/That/ThatGeneric.IsEquatableTo.cs index bea9730af3..7b4d90c9b5 100644 --- a/Source/aweXpect/That/ThatGeneric.IsEquatableTo.cs +++ b/Source/aweXpect/That/ThatGeneric.IsEquatableTo.cs @@ -36,14 +36,14 @@ private sealed class IsEquatableToConstraint( string it, ExpectationGrammars grammars, T expected) - : ConstraintResult.WithValue>(grammars), + : ConstraintResult.WithValue(grammars), IValueConstraint where TEquatable : IEquatable { public ConstraintResult IsMetBy(TEquatable actual) { Actual = actual; - Outcome = actual.Equals(expected) ? Outcome.Success : Outcome.Failure; + Outcome = actual is not null && actual.Equals(expected) ? Outcome.Success : Outcome.Failure; return this; } diff --git a/Tests/aweXpect.Tests/ThatGeneric.IsEquatableTo.Tests.cs b/Tests/aweXpect.Tests/ThatGeneric.IsEquatableTo.Tests.cs index 16bd04bf0c..2913783e31 100644 --- a/Tests/aweXpect.Tests/ThatGeneric.IsEquatableTo.Tests.cs +++ b/Tests/aweXpect.Tests/ThatGeneric.IsEquatableTo.Tests.cs @@ -67,6 +67,22 @@ but it was ThatGeneric.IsEquatableTo.Wrapper { } """); } + + [Fact] + public async Task WhenSubjectIsNull_ShouldFail() + { + NullableWrapper subject = null!; + + async Task Act() + => await That(subject).IsEquatableTo(1L); + + await That(Act).Throws() + .WithMessage(""" + Expected that subject + is equatable to 1, + but it was + """); + } } public sealed class NegatedTests @@ -132,6 +148,26 @@ async Task Act() await That(Act).DoesNotThrow(); } + + [Fact] + public async Task WhenSubjectIsNull_ShouldSucceed() + { + NullableWrapper subject = null!; + + async Task Act() + => await That(subject).DoesNotComplyWith(it => it.IsEquatableTo(1L)); + + await That(Act).DoesNotThrow(); + } + } + + private sealed class NullableWrapper(long value) : IEquatable + { + public long Value { get; } = value; + + /// + public bool Equals(long other) + => Value == other; } private readonly struct Wrapper(long value) diff --git a/Tests/aweXpect.Tests/ThatGeneric.IsNotEquatableTo.Tests.cs b/Tests/aweXpect.Tests/ThatGeneric.IsNotEquatableTo.Tests.cs index 0ea1560c41..0bca685d42 100644 --- a/Tests/aweXpect.Tests/ThatGeneric.IsNotEquatableTo.Tests.cs +++ b/Tests/aweXpect.Tests/ThatGeneric.IsNotEquatableTo.Tests.cs @@ -67,6 +67,17 @@ async Task Act() await That(Act).DoesNotThrow(); } + + [Fact] + public async Task WhenSubjectIsNull_ShouldSucceed() + { + NullableWrapper subject = null!; + + async Task Act() + => await That(subject).IsNotEquatableTo(1L); + + await That(Act).DoesNotThrow(); + } } public sealed class NegatedTests @@ -132,6 +143,31 @@ but it was ThatGeneric.IsNotEquatableTo.Wrapper { } """); } + + [Fact] + public async Task WhenSubjectIsNull_ShouldFail() + { + NullableWrapper subject = null!; + + async Task Act() + => await That(subject).DoesNotComplyWith(it => it.IsNotEquatableTo(1L)); + + await That(Act).Throws() + .WithMessage(""" + Expected that subject + is equatable to 1, + but it was + """); + } + } + + private sealed class NullableWrapper(long value) : IEquatable + { + public long Value { get; } = value; + + /// + public bool Equals(long other) + => Value == other; } private readonly struct Wrapper(long value) From 294ba5a69cb00ce3edf9a4489531e8712e01c59e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Valentin=20Breu=C3=9F?= Date: Sat, 12 Sep 2026 16:21:52 +0200 Subject: [PATCH 2/2] fix: report the negated result of `IsEquatableTo` without "in" MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `IsEquatableToConstraint.AppendNegatedResult` appended " was in " instead of " was ", so a failing `IsNotEquatableTo` read "but it was in Wrapper { … }". It was the only occurrence of that wording in the library; `IsOneOf`, which it appears to have been copied from, does not use it either. The override now delegates to `AppendNormalResult`, as 153 of the 250 negated-result overrides in `Source/aweXpect/That` already do. --- Source/aweXpect/That/ThatGeneric.IsEquatableTo.cs | 5 +---- Tests/aweXpect.Tests/ThatGeneric.IsEquatableTo.Tests.cs | 4 ++-- Tests/aweXpect.Tests/ThatGeneric.IsNotEquatableTo.Tests.cs | 4 ++-- 3 files changed, 5 insertions(+), 8 deletions(-) diff --git a/Source/aweXpect/That/ThatGeneric.IsEquatableTo.cs b/Source/aweXpect/That/ThatGeneric.IsEquatableTo.cs index 7b4d90c9b5..2bf3812aff 100644 --- a/Source/aweXpect/That/ThatGeneric.IsEquatableTo.cs +++ b/Source/aweXpect/That/ThatGeneric.IsEquatableTo.cs @@ -66,9 +66,6 @@ protected override void AppendNegatedExpectation(StringBuilder stringBuilder, st } protected override void AppendNegatedResult(StringBuilder stringBuilder, string? indentation = null) - { - stringBuilder.Append(it).Append(" was in "); - Formatter.Format(stringBuilder, Actual); - } + => AppendNormalResult(stringBuilder, indentation); } } diff --git a/Tests/aweXpect.Tests/ThatGeneric.IsEquatableTo.Tests.cs b/Tests/aweXpect.Tests/ThatGeneric.IsEquatableTo.Tests.cs index 2913783e31..619adf5064 100644 --- a/Tests/aweXpect.Tests/ThatGeneric.IsEquatableTo.Tests.cs +++ b/Tests/aweXpect.Tests/ThatGeneric.IsEquatableTo.Tests.cs @@ -99,7 +99,7 @@ await That(Act).Throws() .WithMessage(""" Expected that subject is not equatable to 1, - but it was in ThatGeneric.IsEquatableTo.Wrapper { + but it was ThatGeneric.IsEquatableTo.Wrapper { Value = 1 } """); @@ -120,7 +120,7 @@ Expected that subject is not equatable to ThatGeneric.IsEquatableTo.Wrapper { Value = 1 }, - but it was in ThatGeneric.IsEquatableTo.Wrapper { + but it was ThatGeneric.IsEquatableTo.Wrapper { Value = 1 } """); diff --git a/Tests/aweXpect.Tests/ThatGeneric.IsNotEquatableTo.Tests.cs b/Tests/aweXpect.Tests/ThatGeneric.IsNotEquatableTo.Tests.cs index 0bca685d42..a546a384c1 100644 --- a/Tests/aweXpect.Tests/ThatGeneric.IsNotEquatableTo.Tests.cs +++ b/Tests/aweXpect.Tests/ThatGeneric.IsNotEquatableTo.Tests.cs @@ -18,7 +18,7 @@ await That(Act).Throws() .WithMessage(""" Expected that subject is not equatable to 1, - but it was in ThatGeneric.IsNotEquatableTo.Wrapper { + but it was ThatGeneric.IsNotEquatableTo.Wrapper { Value = 1 } """); @@ -39,7 +39,7 @@ Expected that subject is not equatable to ThatGeneric.IsNotEquatableTo.Wrapper { Value = 1 }, - but it was in ThatGeneric.IsNotEquatableTo.Wrapper { + but it was ThatGeneric.IsNotEquatableTo.Wrapper { Value = 1 } """);