From 1a79cb8aa9531b1e5546ac64185b1ad9f9b7dbd9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Valentin=20Breu=C3=9F?= Date: Tue, 8 Jul 2025 12:56:46 +0200 Subject: [PATCH 1/2] fix: collection contains with many additional items When verifying that a collection contains another collection and it has many additional items, it now fails incorrectly. --- ...MatchOptions.SameOrderCollectionMatcher.cs | 6 +- ...able.Contains.CollectionEnumerableTests.cs | 78 ++++++++++++------- 2 files changed, 53 insertions(+), 31 deletions(-) diff --git a/Source/aweXpect.Core/Options/CollectionMatchOptions.SameOrderCollectionMatcher.cs b/Source/aweXpect.Core/Options/CollectionMatchOptions.SameOrderCollectionMatcher.cs index 1d51f9a79..2ea500bfe 100644 --- a/Source/aweXpect.Core/Options/CollectionMatchOptions.SameOrderCollectionMatcher.cs +++ b/Source/aweXpect.Core/Options/CollectionMatchOptions.SameOrderCollectionMatcher.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Generic; using System.Linq; -using System.Text.RegularExpressions; using aweXpect.Core; using aweXpect.Core.Helpers; @@ -60,6 +59,11 @@ public bool Verify(string it, T value, IOptionsEquality options, int maximum _index++; error = null; + if (_equivalenceRelations.HasFlag(EquivalenceRelations.Contains)) + { + return _incorrectItems.Count + _missingItems.Count > 2 * maximumNumber; + } + return _additionalItems.Count + _incorrectItems.Count + _missingItems.Count > 2 * maximumNumber; } diff --git a/Tests/aweXpect.Tests/Collections/ThatEnumerable.Contains.CollectionEnumerableTests.cs b/Tests/aweXpect.Tests/Collections/ThatEnumerable.Contains.CollectionEnumerableTests.cs index 2c758a838..d8effae04 100644 --- a/Tests/aweXpect.Tests/Collections/ThatEnumerable.Contains.CollectionEnumerableTests.cs +++ b/Tests/aweXpect.Tests/Collections/ThatEnumerable.Contains.CollectionEnumerableTests.cs @@ -16,7 +16,7 @@ public sealed class EnumerableInSameOrderTests public async Task CompletelyDifferentCollections_ShouldFail() { IEnumerable subject = Enumerable.Range(1, 11); - IEnumerable expected = Enumerable.Range(100, 11); + IEnumerable expected = Enumerable.Range(100, 21); async Task Act() => await That(subject).Contains(expected); @@ -73,7 +73,7 @@ await That(Act).Throws() Expected that subject contains collection expected in order, but it lacked all 3 expected items - + Collection: [] @@ -100,7 +100,7 @@ await That(Act).Throws() Expected that subject contains collection expected in order, but it lacked all 10 expected items - + Collection: [ 1, @@ -184,7 +184,7 @@ contained item "e" at index 4 instead of "y" and "x", "y", "z" - + Collection: [ "a", @@ -220,7 +220,7 @@ await That(Act).Throws() Expected that subject contains collection expected in order, but it lacked all 6 expected items - + Collection: [ "b", @@ -401,6 +401,24 @@ async Task Act() await That(Act).DoesNotThrow(); } + [Fact] + public async Task WithManyAdditionalItems_ShouldSucceed() + { + List values = [42, 5, 18, 23,]; + List results = Enumerable.Range(1, 50).SelectMany(_ => values).ToList(); + + await That(results).Contains(values); + } + + [Fact] + public async Task WithManyAdditionalItemsAtTheBeginning_ShouldSucceed() + { + List values = [42, 5, 18, 23,]; + List results = [..Enumerable.Range(100, 50), ..values, 101, 102, 103,]; + + await That(results).Contains(values); + } + [Fact] public async Task WithMissingItem_ShouldFail() { @@ -545,7 +563,7 @@ await That(Act).Throws() Expected that subject contains collection expected in order ignoring duplicates, but it lacked all 3 unique expected items - + Collection: [] @@ -572,7 +590,7 @@ await That(Act).Throws() Expected that subject contains collection expected in order ignoring duplicates, but it lacked all 2 unique expected items - + Collection: [] @@ -599,7 +617,7 @@ await That(Act).Throws() Expected that subject contains collection expected in order ignoring duplicates, but it lacked all 10 unique expected items - + Collection: [ 1, @@ -650,7 +668,7 @@ contained item "e" at index 4 instead of "y" and "x", "y", "z" - + Collection: [ "a", @@ -686,7 +704,7 @@ await That(Act).Throws() Expected that subject contains collection expected in order ignoring duplicates, but it lacked all 5 unique expected items - + Collection: [ "b", @@ -967,7 +985,7 @@ await That(Act).Throws() Expected that subject contains collection expected in any order, but it lacked all 3 expected items - + Collection: [] @@ -994,7 +1012,7 @@ await That(Act).Throws() Expected that subject contains collection expected in any order, but it lacked all 10 expected items - + Collection: [ 1, @@ -1381,7 +1399,7 @@ await That(Act).Throws() Expected that subject contains collection expected in any order ignoring duplicates, but it lacked all 3 unique expected items - + Collection: [] @@ -1410,7 +1428,7 @@ await That(Act).Throws() Expected that subject contains collection expected in any order ignoring duplicates, but it lacked all 2 unique expected items - + Collection: [] @@ -1437,7 +1455,7 @@ await That(Act).Throws() Expected that subject contains collection expected in any order ignoring duplicates, but it lacked all 10 unique expected items - + Collection: [ 1, @@ -1726,7 +1744,7 @@ public sealed class EnumerableProperlyInSameOrderTests public async Task CompletelyDifferentCollections_ShouldFail() { IEnumerable subject = Enumerable.Range(1, 11); - IEnumerable expected = Enumerable.Range(100, 11); + IEnumerable expected = Enumerable.Range(100, 21); async Task Act() => await That(subject).Contains(expected).Properly(); @@ -1785,7 +1803,7 @@ Expected that subject but it did not contain any additional items and lacked all 3 expected items - + Collection: [] @@ -1812,7 +1830,7 @@ await That(Act).Throws() Expected that subject contains collection expected and at least one additional item in order, but it lacked all 10 expected items - + Collection: [ 1, @@ -1864,7 +1882,7 @@ did not contain any additional items and "x", "y", "z" - + Collection: [ "a", @@ -1902,7 +1920,7 @@ Expected that subject but it did not contain any additional items and lacked all 6 expected items - + Collection: [ "b", @@ -2256,7 +2274,7 @@ Expected that subject but it did not contain any additional items and lacked all 3 unique expected items - + Collection: [] @@ -2285,7 +2303,7 @@ Expected that subject but it did not contain any additional items and lacked all 2 unique expected items - + Collection: [] @@ -2312,7 +2330,7 @@ await That(Act).Throws() Expected that subject contains collection expected and at least one additional item in order ignoring duplicates, but it lacked all 10 unique expected items - + Collection: [ 1, @@ -2363,7 +2381,7 @@ contained item "e" at index 4 instead of "y" and "x", "y", "z" - + Collection: [ "a", @@ -2401,7 +2419,7 @@ Expected that subject but it did not contain any additional items and lacked all 5 unique expected items - + Collection: [ "b", @@ -2808,7 +2826,7 @@ Expected that subject but it did not contain any additional items and lacked all 3 expected items - + Collection: [] @@ -2835,7 +2853,7 @@ await That(Act).Throws() Expected that subject contains collection expected and at least one additional item in any order, but it lacked all 10 expected items - + Collection: [ 1, @@ -3272,7 +3290,7 @@ Expected that subject but it did not contain any additional items and lacked all 3 unique expected items - + Collection: [] @@ -3303,7 +3321,7 @@ Expected that subject but it did not contain any additional items and lacked all 2 unique expected items - + Collection: [] @@ -3330,7 +3348,7 @@ await That(Act).Throws() Expected that subject contains collection expected and at least one additional item in any order ignoring duplicates, but it lacked all 10 unique expected items - + Collection: [ 1, From 5bcdbb3da10ff552d27609559883ecffe86ba165 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Valentin=20Breu=C3=9F?= Date: Tue, 8 Jul 2025 13:02:35 +0200 Subject: [PATCH 2/2] Fix review issues and disable tests temporarily --- ...ollectionMatchOptions.SameOrderCollectionMatcher.cs | 10 ++++++---- ...hatEnumerable.Contains.CollectionEnumerableTests.cs | 4 ++-- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/Source/aweXpect.Core/Options/CollectionMatchOptions.SameOrderCollectionMatcher.cs b/Source/aweXpect.Core/Options/CollectionMatchOptions.SameOrderCollectionMatcher.cs index 2ea500bfe..06e98be08 100644 --- a/Source/aweXpect.Core/Options/CollectionMatchOptions.SameOrderCollectionMatcher.cs +++ b/Source/aweXpect.Core/Options/CollectionMatchOptions.SameOrderCollectionMatcher.cs @@ -59,12 +59,14 @@ public bool Verify(string it, T value, IOptionsEquality options, int maximum _index++; error = null; - if (_equivalenceRelations.HasFlag(EquivalenceRelations.Contains)) + int errorThreshold = 2 * maximumNumber; + int errorCount = _incorrectItems.Count + _missingItems.Count; + if (!_equivalenceRelations.HasFlag(EquivalenceRelations.Contains)) { - return _incorrectItems.Count + _missingItems.Count > 2 * maximumNumber; + errorCount += _additionalItems.Count; } - - return _additionalItems.Count + _incorrectItems.Count + _missingItems.Count > 2 * maximumNumber; + + return errorCount > errorThreshold; } #pragma warning disable S3776 // https://rules.sonarsource.com/csharp/RSPEC-3776 diff --git a/Tests/aweXpect.Tests/Collections/ThatEnumerable.Contains.CollectionEnumerableTests.cs b/Tests/aweXpect.Tests/Collections/ThatEnumerable.Contains.CollectionEnumerableTests.cs index d8effae04..a3df5bb9f 100644 --- a/Tests/aweXpect.Tests/Collections/ThatEnumerable.Contains.CollectionEnumerableTests.cs +++ b/Tests/aweXpect.Tests/Collections/ThatEnumerable.Contains.CollectionEnumerableTests.cs @@ -401,7 +401,7 @@ async Task Act() await That(Act).DoesNotThrow(); } - [Fact] + [Fact(Skip = "TODO: Enable after updating aweXpect.Core")] public async Task WithManyAdditionalItems_ShouldSucceed() { List values = [42, 5, 18, 23,]; @@ -410,7 +410,7 @@ public async Task WithManyAdditionalItems_ShouldSucceed() await That(results).Contains(values); } - [Fact] + [Fact(Skip = "TODO: Enable after updating aweXpect.Core")] public async Task WithManyAdditionalItemsAtTheBeginning_ShouldSucceed() { List values = [42, 5, 18, 23,];