fix: Contains for collections with duplicates in expected#642
Conversation
Ensure that the following test succeeds:
```csharp
[Fact]
public async Task WhenExpectedContainsDuplicateButMissingItems_ShouldFail()
{
int[] collection = [1, 2, 1, 3, 12, 2, 2,];
async Task Act()
=> await That(collection).Contains([1, 2, 1, 1, 2,]);
await That(Act).Throws<XunitException>()
.WithMessage("""
Expected that collection
contains collection [1, 2, 1, 1, 2,] in order,
but it
contained item 3 at index 3 instead of 1 and
contained item 12 at index 4 instead of 2
Collection:
[1, 2, 1, 3, 12, 2, 2]
Expected:
[1, 2, 1, 1, 2]
""");
}
```
There was a problem hiding this comment.
Pull Request Overview
This PR fixes the behavior of the Contains assertion when the expected collection includes duplicates yet some expected items are missing. The changes improve the error messages by including detailed information about mismatches and adjust the underlying options for duplicate handling.
- Added a new test to verify failure when duplicates in the expected collection are partially missing.
- Updated multiple test files to refine error message output with precise mismatch information.
- Modified the CollectionMatchOptions to remove a conditional check that filtered out duplicate discrepancies.
Reviewed Changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| Tests/aweXpect.Tests/Collections/ThatEnumerable.Contains.CollectionTests.cs | Added a new test and updated error message formatting for duplicate checks. |
| Tests/aweXpect.Tests/Collections/ThatEnumerable.Contains.CollectionImmutableTests.cs | Updated error messages to include item mismatch details. |
| Tests/aweXpect.Tests/Collections/ThatEnumerable.Contains.CollectionEnumerableTests.cs | Updated error messages to include item mismatch details. |
| Tests/aweXpect.Tests/Collections/ThatAsyncEnumerable.Contains.CollectionTests.cs | Updated error messages to include item mismatch details. |
| Source/aweXpect.Core/Options/CollectionMatchOptions.cs | Removed a duplicate filtering condition to improve error reporting on mismatches. |
Comments suppressed due to low confidence (1)
Source/aweXpect.Core/Options/CollectionMatchOptions.cs:166
- Removing the duplicate check now causes all mismatches to be reported, including for collections without duplicates. Verify that this behavior is aligned with the desired user experience, and consider adding tests for non-duplicate scenarios to confirm the change.
}
Test Results 4 files - 34 4 suites - 34 20s ⏱️ - 2m 23s Results for commit 9c8d809. ± Comparison against base commit 56b575b. This pull request removes 12783 and adds 54 tests. Note that renamed tests count towards both.♻️ This comment has been updated with latest results. |
|
🚀 Benchmark ResultsDetails
|
👽 Mutation ResultsaweXpectDetails
The final mutation score is NaN%Coverage Thresholds: high:80 low:60 break:0aweXpect.CoreDetails
The final mutation score is 100.00%Coverage Thresholds: high:80 low:60 break:0 |
|
This is addressed in release v2.17.0. |



Fixes the behavior of the Contains assertion when the expected collection includes duplicates yet some expected items are missing.
Ensure that the following test succeeds: