Skip to content

fix!: treat IsSameAs as a comparison for a null subject - #1068

Merged
vbreuss merged 2 commits into
mainfrom
fix/null-philosophy-is-same-as
Sep 13, 2026
Merged

vbreuss merged 2 commits into
mainfrom
fix/null-philosophy-is-same-as

Conversation

@vbreuss

@vbreuss vbreuss commented Sep 13, 2026

Copy link
Copy Markdown
Member

Expect.That((object?)null).IsSameAs(null) failed with "refers to , but it was ", although ReferenceEquals(null, null) is true. The constraint was built on WithNotNullValue, which fails for a null subject whatever it is compared with.

Identity is a comparison against a value the caller supplies, so null is an ordinary value on both sides, exactly as for IsEqualTo. WithEqualToValue expresses that: for a null subject, IsSameAs(null) and IsNotSameAs(other) now succeed, while IsSameAs(other) and IsNotSameAs(null) still fail.

This is the one change in this series that loosens behaviour rather than tightening it, which is why it is separate. Both expectations lose their [GuaranteesNotNull] marking, because a null subject can now satisfy them: a right answer is worth more than a suppressed nullability warning.

BREAKING CHANGE:
IsSameAs(null) and IsNotSameAs(other) now succeed for a null subject instead of failing, and neither expectation proves the subject to be not null for the analyzer any more.

@vbreuss vbreuss self-assigned this Sep 13, 2026
@vbreuss vbreuss added bug Something isn't working breaking change The changes require a new major version labels Sep 13, 2026
`Expect.That((object?)null).IsSameAs(null)` failed with "refers to <null>,
but it was <null>", although `ReferenceEquals(null, null)` is true. The
constraint was built on `WithNotNullValue`, which fails for a `null` subject
whatever it is compared with.

Identity is a comparison against a value the caller supplies, so `null` is an
ordinary value on both sides, exactly as for `IsEqualTo`. `WithEqualToValue`
expresses that: for a `null` subject, `IsSameAs(null)` and
`IsNotSameAs(other)` now succeed, while `IsSameAs(other)` and
`IsNotSameAs(null)` still fail.

This is the one change in this series that loosens behaviour rather than
tightening it, which is why it is separate. Both expectations lose their
`[GuaranteesNotNull]` marking, because a `null` subject can now satisfy them:
a right answer is worth more than a suppressed nullability warning.

BREAKING CHANGE: `IsSameAs(null)` and `IsNotSameAs(other)` now succeed for a
`null` subject instead of failing, and neither expectation proves the subject
to be not `null` for the analyzer any more.
`IsSameAs` and `IsNotSameAs` no longer carry `[GuaranteesNotNull]`.

The regenerated files also pick up `ThatVersion`, which #1061 added without
updating the snapshots - the approval tests are red on `main` until this
lands.
@vbreuss
vbreuss force-pushed the fix/null-philosophy-is-same-as branch from 05bd214 to 56a1397 Compare September 13, 2026 07:15
@github-actions

github-actions Bot commented Sep 13, 2026 •

Copy link
Copy Markdown
Contributor

🚀 Benchmark Results

Details

BenchmarkDotNet v0.15.8, Linux Ubuntu 24.04.5 LTS (Noble Numbat)
AMD EPYC 7763 2.45GHz, 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-v3

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

Method Mean Error StdDev Gen0 Gen1 Allocated
Bool_aweXpect 266.1 ns 1.77 ns 1.38 ns 0.0415 - 696 B
Bool_FluentAssertions 244.0 ns 5.90 ns 5.52 ns 0.0567 - 952 B
Equivalency_aweXpect 315,247.1 ns 3,274.39 ns 3,062.86 ns 20.0195 0.4883 335444 B
Equivalency_FluentAssertions 2,819,945.4 ns 23,041.44 ns 21,552.97 ns 289.0625 46.8750 4841651 B
Int_GreaterThan_aweXpect 278.9 ns 5.69 ns 5.32 ns 0.0515 - 864 B
Int_GreaterThan_FluentAssertions 248.1 ns 4.66 ns 4.36 ns 0.0730 - 1224 B
ItemsCount_AtLeast_aweXpect 501.2 ns 8.02 ns 7.50 ns 0.0811 - 1360 B
ItemsCount_AtLeast_FluentAssertions 503.3 ns 8.53 ns 7.98 ns 0.1192 - 2008 B
String_aweXpect 491.3 ns 6.88 ns 6.44 ns 0.0668 - 1128 B
String_FluentAssertions 1,277.8 ns 16.94 ns 14.15 ns 0.2346 - 3944 B
StringArray_aweXpect 2,033.0 ns 22.11 ns 20.69 ns 0.1564 - 2624 B
StringArray_FluentAssertions 1,355.1 ns 36.77 ns 34.40 ns 0.2480 - 4152 B
StringArrayInAnyOrder_aweXpect 2,594.3 ns 13.85 ns 12.96 ns 0.1678 - 2816 B
StringArrayInAnyOrder_FluentAssertions 27,038.1 ns 215.58 ns 201.66 ns 1.9836 0.0610 33471 B

@github-actions

Copy link
Copy Markdown
Contributor

👽 Mutation Results

Mutation testing badge

aweXpect

Details
File Score Killed Survived Timeout No Coverage Ignored Compile Errors Runtime Errors Total Detected Total Undetected Total Mutants
That/Objects/ThatObject.IsSameAs.cs 100.00% 16 0 0 0 5 6 0 16 0 27

The final mutation score is 100.00%

Coverage Thresholds: high:80 low:60 break:0

aweXpect.Core

Details
File Score Killed Survived Timeout No Coverage Ignored Compile Errors Runtime Errors Total Detected Total Undetected Total Mutants

The final mutation score is NaN%

Coverage Thresholds: high:80 low:60 break:0

@github-actions

Copy link
Copy Markdown
Contributor

Test Results

     23 files   -  27       23 suites   - 27   10m 37s ⏱️ +55s
 20 681 tests  -  36   20 679 ✅  -  36  2 💤 ±0  0 ❌ ±0 
107 642 runs   - 120  107 640 ✅  - 120  2 💤 ±0  0 ❌ ±0 

Results for commit 56a1397. ± Comparison against base commit 80d7a63.

This pull request removes 3236 and adds 3200 tests. Note that renamed tests count towards both.
aweXpect.Core.Tests.Core.Exceptions.FailExceptionTests ‑ InnerException_ShouldBeSet(message: "message49156500-fccc-4de1-8ae3-7e8a6a18082c")
aweXpect.Core.Tests.Core.Exceptions.FailExceptionTests ‑ InnerException_ShouldBeSet(message: "messageb887b5ce-d5a5-4656-94d4-9e7e6274cc2a")
aweXpect.Core.Tests.Core.Exceptions.FailExceptionTests ‑ InnerException_ShouldBeSet(message: "messagec0c34fd9-64ed-4a04-ac45-b9d975230437")
aweXpect.Core.Tests.Core.Exceptions.FailExceptionTests ‑ InnerException_ShouldBeSet(message: "messagecbd42c1d-1c27-4a71-8d30-a88be3bafdfb")
aweXpect.Core.Tests.Core.Exceptions.FailExceptionTests ‑ InnerException_ShouldBeSet(message: "messageeab1bcb2-a7d6-42bd-abdb-d021195c9efb")
aweXpect.Core.Tests.Core.Exceptions.FailExceptionTests ‑ InnerException_ShouldBeSet(message: "messagefe923ffd-49ef-4748-85d2-50613cb7b6c8")
aweXpect.Core.Tests.Core.Exceptions.FailExceptionTests ‑ Message_ShouldBeSet(message: "message027c9856-77e5-46f5-8471-49c40bcd4d3c")
aweXpect.Core.Tests.Core.Exceptions.FailExceptionTests ‑ Message_ShouldBeSet(message: "message731012ec-4109-483c-96b8-242061f1d969")
aweXpect.Core.Tests.Core.Exceptions.FailExceptionTests ‑ Message_ShouldBeSet(message: "message84740aa4-79bb-49c6-b156-b50b87be6118")
aweXpect.Core.Tests.Core.Exceptions.FailExceptionTests ‑ Message_ShouldBeSet(message: "messagea74795aa-63ac-472f-aabe-c2387be38753")
…
aweXpect.Core.Tests.Core.Exceptions.FailExceptionTests ‑ InnerException_ShouldBeSet(message: "message11eabcc4-6425-4fc7-8f60-8f918ae34661")
aweXpect.Core.Tests.Core.Exceptions.FailExceptionTests ‑ InnerException_ShouldBeSet(message: "message16330d8c-d9d5-493e-93a5-02f6b0274d8b")
aweXpect.Core.Tests.Core.Exceptions.FailExceptionTests ‑ InnerException_ShouldBeSet(message: "message357ed3a1-96db-491d-9520-e2ac8f95eaae")
aweXpect.Core.Tests.Core.Exceptions.FailExceptionTests ‑ InnerException_ShouldBeSet(message: "message6b577b17-5ebb-4df1-ae8a-60288251c434")
aweXpect.Core.Tests.Core.Exceptions.FailExceptionTests ‑ InnerException_ShouldBeSet(message: "messagee104c630-2a9b-4033-bfb1-b49007639794")
aweXpect.Core.Tests.Core.Exceptions.FailExceptionTests ‑ InnerException_ShouldBeSet(message: "messageeb2363d6-d609-4c6c-9fac-788a265afeaf")
aweXpect.Core.Tests.Core.Exceptions.FailExceptionTests ‑ Message_ShouldBeSet(message: "message1a9fdaca-81f2-4327-b475-5bccffaedf0a")
aweXpect.Core.Tests.Core.Exceptions.FailExceptionTests ‑ Message_ShouldBeSet(message: "message3ed12093-174e-4d0c-a31a-5f7c80dc7189")
aweXpect.Core.Tests.Core.Exceptions.FailExceptionTests ‑ Message_ShouldBeSet(message: "message5331eaaa-afdf-4b2e-93a2-081bd9ca0aee")
aweXpect.Core.Tests.Core.Exceptions.FailExceptionTests ‑ Message_ShouldBeSet(message: "message84af0a07-ffc1-4cb8-874d-a19efe3f1e58")
…

@vbreuss
vbreuss merged commit dfb520f into main Sep 13, 2026
19 of 20 checks passed
@vbreuss
vbreuss deleted the fix/null-philosophy-is-same-as branch September 13, 2026 10:59
@sonarqubecloud

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

breaking change The changes require a new major version bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant