Skip to content

fix: do not throw when the subject of IsEquatableTo is null - #1047

Merged
vbreuss merged 2 commits into
mainfrom
fix/is-equatable-to-null-subject
Sep 12, 2026
Merged

vbreuss merged 2 commits into
mainfrom
fix/is-equatable-to-null-subject

Conversation

@vbreuss

@vbreuss vbreuss commented Sep 12, 2026

Copy link
Copy Markdown
Member

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<IEquatable<T>> instead of WithValue<TEquatable>, 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.

`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<IEquatable<T>>`
instead of `WithValue<TEquatable>`, 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.
@vbreuss vbreuss self-assigned this Sep 12, 2026
Copilot AI lite review requested due to automatic review settings September 12, 2026 14:14
@vbreuss vbreuss added the bug Something isn't working label Sep 12, 2026

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@vbreuss
vbreuss enabled auto-merge (squash) September 12, 2026 14:15
@vbreuss
vbreuss disabled auto-merge September 12, 2026 14:16
`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.
Copilot AI review requested due to automatic review settings September 12, 2026 14:21

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@github-actions

github-actions Bot commented Sep 12, 2026 •

Copy link
Copy Markdown
Contributor

🚀 Benchmark Results

Details

BenchmarkDotNet v0.15.8, Linux Ubuntu 24.04.5 LTS (Noble Numbat)
AMD EPYC 9V74 2.60GHz, 1 CPU, 4 logical and 2 physical cores
.NET SDK 10.0.303
[Host] : .NET 8.0.31 (8.0.31, 8.0.3126.42015), X64 RyuJIT x86-64-v4

Job=InProcess Toolchain=InProcessEmitToolchain IterationCount=15
LaunchCount=1 WarmupCount=10

Method Mean Error StdDev Gen0 Gen1 Allocated
Bool_aweXpect 186.4 ns 0.60 ns 0.47 ns 0.0415 - 696 B
Bool_FluentAssertions 190.8 ns 0.61 ns 0.51 ns 0.0567 - 952 B
Equivalency_aweXpect 229,947.3 ns 2,728.46 ns 2,552.21 ns 20.0195 0.9766 335442 B
Equivalency_FluentAssertions 1,824,193.1 ns 44,328.44 ns 41,464.85 ns 289.0625 50.7813 4841648 B
Int_GreaterThan_aweXpect 210.0 ns 0.49 ns 0.38 ns 0.0515 - 864 B
Int_GreaterThan_FluentAssertions 189.0 ns 0.49 ns 0.41 ns 0.0730 - 1224 B
ItemsCount_AtLeast_aweXpect 375.8 ns 0.88 ns 0.83 ns 0.0811 - 1360 B
ItemsCount_AtLeast_FluentAssertions 365.1 ns 2.56 ns 2.39 ns 0.1197 - 2008 B
String_aweXpect 358.5 ns 1.93 ns 1.81 ns 0.0672 - 1128 B
String_FluentAssertions 902.7 ns 9.76 ns 9.13 ns 0.2356 0.0010 3944 B
StringArray_aweXpect 1,475.7 ns 3.40 ns 3.18 ns 0.1564 - 2624 B
StringArray_FluentAssertions 975.2 ns 5.15 ns 4.81 ns 0.2480 - 4152 B
StringArrayInAnyOrder_aweXpect 1,979.5 ns 2.07 ns 1.94 ns 0.1678 - 2816 B
StringArrayInAnyOrder_FluentAssertions 15,280.3 ns 134.83 ns 112.59 ns 1.9836 0.0610 33471 B

@vbreuss
vbreuss enabled auto-merge (squash) September 12, 2026 14:25
@sonarqubecloud

Copy link
Copy Markdown

@github-actions

Copy link
Copy Markdown
Contributor

Test Results

     23 files   - 27       23 suites   - 27   9m 9s ⏱️ +6s
 20 238 tests  - 25   20 236 ✅  - 25  2 💤 ±0  0 ❌ ±0 
104 627 runs   - 84  104 625 ✅  - 84  2 💤 ±0  0 ❌ ±0 

Results for commit 294ba5a. ± Comparison against base commit 6b0b96a.

@vbreuss
vbreuss merged commit ef652c1 into main Sep 12, 2026
13 of 14 checks passed
@vbreuss
vbreuss deleted the fix/is-equatable-to-null-subject branch September 12, 2026 14:30
github-actions Bot added a commit that referenced this pull request Sep 12, 2026
…IsEquatableTo` is `null` (#1047) by Valentin Breuß
github-actions Bot added a commit that referenced this pull request Sep 12, 2026
…IsEquatableTo` is `null` (#1047) by Valentin Breuß
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants