fix: avoid duplicate contexts in collection expectations#641
Conversation
When combining multiple collection expectations with multiple failures, the context contained one collection context for every failure.
There was a problem hiding this comment.
Pull Request Overview
This PR ensures that when multiple collection expectations fail in a single assertion chain, the collection context is only included once. It adds tests across various collection types to verify the behavior and updates the context-addition logic to prevent duplicate contexts.
- Added
WithMultipleFailures_ShouldIncludeCollectionOnlyOncetests for ReadOnlyDictionary, Dictionary, Enumerable, ImmutableArray, and AsyncEnumerable scenarios. - Modified
CollectionHelpersto only add a "Collection" or "Dictionary" context if it hasn't been added already.
Reviewed Changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| Tests/...ThatReadOnlyDictionary.ContainsKey.Tests.cs | Added multi-failure test for IReadOnlyDictionary.ContainsKey |
| Tests/...ThatDictionary.ContainsKey.Tests.cs | Added multi-failure test for IDictionary.ContainsKey |
| Tests/...ThatEnumerable.Contains.Tests.cs | Added multi-failure test for IEnumerable.Contains |
| Tests/...Contains.ImmutableTests.cs | Added multi-failure test for ImmutableArray.Contains |
| Tests/...Contains.EnumerableTests.cs | Added multi-failure test for non-generic IEnumerable.Contains |
| Tests/...ThatAsyncEnumerable.Contains.Tests.cs | Added multi-failure test for IAsyncEnumerable.Contains |
| Source/aweXpect/.../CollectionHelpers.cs | Wrapped context updates in conditional blocks to avoid duplicates |
Comments suppressed due to low confidence (6)
Source/aweXpect/That/Collections/CollectionHelpers.cs:41
- [nitpick] The conditional context-addition logic is duplicated across multiple overloads. Consider extracting a shared helper method to reduce repetition and improve maintainability.
internal static ExpectationBuilder AddCollectionContext<TItem>(this ExpectationBuilder expectationBuilder,
Source/aweXpect/That/Collections/CollectionHelpers.cs:64
- The lambda passed to
UpdateContextsuses a block but never returns the modifiedcontextslist, leading to a compile error or unexpected behavior. Addreturn contexts;before the closing brace.
});
Source/aweXpect/That/Collections/CollectionHelpers.cs:100
- The lambda block in
AddCollectionContext(this ExpectationBuilder, object)does not return the updatedcontexts. You need toreturn contexts;at the end of the block.
});
Source/aweXpect/That/Collections/CollectionHelpers.cs:124
- Missing
return contexts;in theUpdateContextsblock for the.NET 8+overload. Without it, the updated contexts won't be passed back.
});
Source/aweXpect/That/Collections/CollectionHelpers.cs:147
- In the generic dictionary overload, the
UpdateContextslambda block fails to return the modifiedcontexts. Please addreturn contexts;.
});
Source/aweXpect/That/Collections/CollectionHelpers.cs:169
- The
UpdateContextsblock in the second IDictionary overload doesn't returncontexts. Insertreturn contexts;to fix functionality.
});
|
Test Results 14 files - 24 14 suites - 24 2m 43s ⏱️ -33s Results for commit 213ec22. ± Comparison against base commit d832baa. This pull request removes 1379 and adds 1356 tests. Note that renamed tests count towards both. |
🚀 Benchmark ResultsDetails
|
👽 Mutation ResultsaweXpectDetails
The final mutation score is 89.89%Coverage Thresholds: high:80 low:60 break:0aweXpect.CoreDetails
The final mutation score is NaN%Coverage Thresholds: high:80 low:60 break:0 |
…tion expectations (#641) by Valentin Breuß
…tion expectations (#641) by Valentin Breuß
|
This is addressed in release v2.17.0. |

When combining multiple collection expectations with multiple failures, the context contained one collection context for every failure.