Skip to content

fix!: reject a null or empty needle in the dictionary key and value expectations - #1075

Merged
vbreuss merged 1 commit into
mainfrom
fix/dictionary-argument-validation
Sep 13, 2026
Merged

vbreuss merged 1 commit into
mainfrom
fix/dictionary-argument-validation

Conversation

@vbreuss

@vbreuss vbreuss commented Sep 13, 2026 •

Copy link
Copy Markdown
Member

ContainsKeys, ContainsValues, DoesNotContainKeys and DoesNotContainValues take params, so calling one with no arguments was the default call and asserted nothing at all: the empty needle made every key trivially present and every unwanted key trivially absent, so the expectation passed whatever the dictionary held.

A null needle was worse than unvalidated: it surfaced as InvalidOperationException wrapping a NullReferenceException from inside the constraint, rather than as an ArgumentNullException naming the parameter. The single ThrowIfNullOrEmpty guard fixes both, and covers all sixteen overloads - each expectation exists twice per dictionary type, for the interface and the concrete class, across ThatDictionary and ThatReadOnlyDictionary.

The null subject was never affected: these constraints derive from WithNotNullValue, which already forces a failure when the subject is null, so their GuaranteesNotNull markings were and remain correct.

… expectations

`ContainsKeys`, `ContainsValues`, `DoesNotContainKeys` and `DoesNotContainValues` take `params`, so calling one with no arguments was the default call and asserted nothing at all: the empty needle made every key trivially present and every unwanted key trivially absent, so the expectation passed whatever the dictionary held. #1069 already rejected exactly that for `Contains`, `StartsWith` and `EndsWith`, and leaving the dictionary family out would ship a major in which one half of the surface validates its argument and the other half silently accepts a no-op.

A `null` needle was worse than unvalidated: it surfaced as `InvalidOperationException` wrapping a `NullReferenceException` from inside the constraint, rather than as an `ArgumentNullException` naming the parameter. The single `ThrowIfNullOrEmpty` guard fixes both, and covers all sixteen overloads - each expectation exists twice per dictionary type, for the interface and the concrete class, across `ThatDictionary` and `ThatReadOnlyDictionary`.

The `null` subject was never affected: these constraints derive from `WithNotNullValue`, which already forces a failure when the subject is `null`, so their `GuaranteesNotNull` markings were and remain correct.
@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
@sonarqubecloud

Copy link
Copy Markdown

@github-actions

Copy link
Copy Markdown
Contributor

Test Results

     23 files   - 27       23 suites   - 27   10m 58s ⏱️ +20s
 20 700 tests  - 17   20 698 ✅  - 17  2 💤 ±0  0 ❌ ±0 
107 762 runs   - 12  107 760 ✅  - 12  2 💤 ±0  0 ❌ ±0 

Results for commit f5c348a. ± Comparison against base commit f2e2904.

This pull request removes 3236 and adds 3219 tests. Note that renamed tests count towards both.
aweXpect.Core.Tests.Core.Exceptions.FailExceptionTests ‑ InnerException_ShouldBeSet(message: "message4af6e32f-094e-4681-be18-1644af47cf77")
aweXpect.Core.Tests.Core.Exceptions.FailExceptionTests ‑ InnerException_ShouldBeSet(message: "message661b76f5-98f3-42ba-86e8-f15d543bb069")
aweXpect.Core.Tests.Core.Exceptions.FailExceptionTests ‑ InnerException_ShouldBeSet(message: "message68bfc668-1a3b-4f3e-a6bd-b54a284a1db0")
aweXpect.Core.Tests.Core.Exceptions.FailExceptionTests ‑ InnerException_ShouldBeSet(message: "message9c7bc8fa-7819-41fb-8d20-1a9f4166100a")
aweXpect.Core.Tests.Core.Exceptions.FailExceptionTests ‑ InnerException_ShouldBeSet(message: "messageddda8caf-3b5b-440b-960d-fd3af5e5dfdd")
aweXpect.Core.Tests.Core.Exceptions.FailExceptionTests ‑ InnerException_ShouldBeSet(message: "messagee835dff1-6ab3-434f-8e26-b2e67fdd7cce")
aweXpect.Core.Tests.Core.Exceptions.FailExceptionTests ‑ Message_ShouldBeSet(message: "message3e6a8e58-2cf2-4db5-987f-9e08aa9c8efe")
aweXpect.Core.Tests.Core.Exceptions.FailExceptionTests ‑ Message_ShouldBeSet(message: "message40e94ebb-d7b8-484c-9c20-c1ebe7bba385")
aweXpect.Core.Tests.Core.Exceptions.FailExceptionTests ‑ Message_ShouldBeSet(message: "message47ff2a5d-bf12-42c3-aed8-d896c56c9a55")
aweXpect.Core.Tests.Core.Exceptions.FailExceptionTests ‑ Message_ShouldBeSet(message: "message582f9b46-6350-466f-bde8-3e9c436a6926")
…
aweXpect.Core.Tests.Core.Exceptions.FailExceptionTests ‑ InnerException_ShouldBeSet(message: "message2dc7ad88-25c0-4e04-8d3f-3d2a70c0b0f4")
aweXpect.Core.Tests.Core.Exceptions.FailExceptionTests ‑ InnerException_ShouldBeSet(message: "message81fc1e0e-29fd-42b1-be89-8800fd765389")
aweXpect.Core.Tests.Core.Exceptions.FailExceptionTests ‑ InnerException_ShouldBeSet(message: "message9153bb3c-e301-40dc-84db-a067ad060f5a")
aweXpect.Core.Tests.Core.Exceptions.FailExceptionTests ‑ InnerException_ShouldBeSet(message: "messagea8c8881b-59df-45bb-b735-fcba615ec192")
aweXpect.Core.Tests.Core.Exceptions.FailExceptionTests ‑ InnerException_ShouldBeSet(message: "messageefc61bba-4d46-4c13-b751-03cff33ef503")
aweXpect.Core.Tests.Core.Exceptions.FailExceptionTests ‑ InnerException_ShouldBeSet(message: "messagef18394d7-62e5-473b-ba13-23acd8cb0291")
aweXpect.Core.Tests.Core.Exceptions.FailExceptionTests ‑ Message_ShouldBeSet(message: "message3daf3353-0057-4b8e-8353-f33a5a48b275")
aweXpect.Core.Tests.Core.Exceptions.FailExceptionTests ‑ Message_ShouldBeSet(message: "message9303ac18-7e1f-4a69-8a7e-4b26d63aefb5")
aweXpect.Core.Tests.Core.Exceptions.FailExceptionTests ‑ Message_ShouldBeSet(message: "messageb88a6d89-0e01-4861-9bbe-6d58cefb5155")
aweXpect.Core.Tests.Core.Exceptions.FailExceptionTests ‑ Message_ShouldBeSet(message: "messageee8d7d76-b0c8-4f99-b594-cfb539a287ef")
…

@github-actions

Copy link
Copy Markdown
Contributor

🚀 Benchmark Results

Details

BenchmarkDotNet v0.15.8, Linux Ubuntu 24.04.5 LTS (Noble Numbat)
AMD EPYC 7763 3.15GHz, 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 278.2 ns 4.67 ns 4.37 ns 0.0415 - 696 B
Bool_FluentAssertions 256.0 ns 1.08 ns 0.84 ns 0.0567 - 952 B
Equivalency_aweXpect 306,574.0 ns 850.97 ns 754.36 ns 20.0195 0.4883 335444 B
Equivalency_FluentAssertions 2,803,255.6 ns 44,137.71 ns 41,286.44 ns 289.0625 46.8750 4841651 B
Int_GreaterThan_aweXpect 284.0 ns 3.19 ns 2.99 ns 0.0515 - 864 B
Int_GreaterThan_FluentAssertions 277.1 ns 12.06 ns 11.28 ns 0.0730 - 1224 B
ItemsCount_AtLeast_aweXpect 492.3 ns 11.88 ns 11.11 ns 0.0811 - 1360 B
ItemsCount_AtLeast_FluentAssertions 508.1 ns 7.53 ns 7.04 ns 0.1192 - 2008 B
String_aweXpect 483.9 ns 11.06 ns 10.35 ns 0.0668 - 1128 B
String_FluentAssertions 1,157.0 ns 9.44 ns 8.83 ns 0.2346 - 3944 B
StringArray_aweXpect 1,890.3 ns 7.53 ns 7.04 ns 0.1564 - 2624 B
StringArray_FluentAssertions 1,261.6 ns 23.79 ns 22.25 ns 0.2480 - 4152 B
StringArrayInAnyOrder_aweXpect 2,575.7 ns 33.81 ns 31.62 ns 0.1678 - 2816 B
StringArrayInAnyOrder_FluentAssertions 26,362.4 ns 154.06 ns 136.57 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/Collections/ThatDictionary.ContainsKeys.cs 96.77% 30 0 0 1 13 16 0 30 1 60
That/Collections/ThatDictionary.ContainsValues.cs 100.00% 29 0 0 0 13 10 0 29 0 52
That/Collections/ThatReadOnlyDictionary.ContainsKeys.cs 100.00% 30 0 0 0 13 15 0 30 0 58
That/Collections/ThatReadOnlyDictionary.ContainsValues.cs 100.00% 29 0 0 0 13 10 0 29 0 52

The final mutation score is 99.16%

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

@vbreuss
vbreuss merged commit 9547e82 into main Sep 13, 2026
13 checks passed
@vbreuss
vbreuss deleted the fix/dictionary-argument-validation branch September 13, 2026 14:12
github-actions Bot added a commit that referenced this pull request Sep 13, 2026
…n the dictionary key and value expectations (#1075) by Valentin Breuß
github-actions Bot added a commit that referenced this pull request Sep 13, 2026
…n the dictionary key and value expectations (#1075) by Valentin Breuß
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