From c751895c7f3add9d4879daf73776e6978a13e89d Mon Sep 17 00:00:00 2001 From: Jake Meiergerd Date: Sat, 5 Dec 2026 22:47:49 -0600 Subject: [PATCH] Fixed that the default backing implementation of the dynamic `.Filter()` operator did not properly limit index re-calculations, when processing an `Add` change, to only run when dealing with an included item, resulting in downstream index corruption when inserting excluded items. --- .../List/FilterFixture.WithPredicateState.cs | 85 ++++++++++++------- .../Internal/Filter.WithPredicateState.cs | 3 + 2 files changed, 55 insertions(+), 33 deletions(-) diff --git a/src/DynamicData.Tests/List/FilterFixture.WithPredicateState.cs b/src/DynamicData.Tests/List/FilterFixture.WithPredicateState.cs index de41e5abe..dcf1fc263 100644 --- a/src/DynamicData.Tests/List/FilterFixture.WithPredicateState.cs +++ b/src/DynamicData.Tests/List/FilterFixture.WithPredicateState.cs @@ -62,12 +62,31 @@ public void ChangesAreMadeAfterInitialPredicateState_ItemsAreFiltered(ListFilter ShouldBeValid(results, EnumerateFilteredItems()); + // Test Insert, with an included item, for already-populated list + var item9 = new Item() { Id = 9, IsIncluded = true }; + source.Insert( + index: 2, + item: item9); + + results.RecordedChangeSets.Skip(2).Count().Should().Be(1, "one source operation was performed, with one included item added"); + ShouldBeValid(results, EnumerateFilteredItems()); + + + // Test Insert, with an excluded item, for already-populated list + var item10 = new Item() { Id = 10, IsIncluded = false }; + source.Insert( + index: 1, + item: item10); + + results.RecordedChangeSets.Skip(3).Should().BeEmpty("one source operation was performed, but no included items were affected"); + + // Test Refresh, with no item mutations. source.Refresh(Enumerable.Range(0, source.Count)); - results.RecordedChangeSets.Skip(2).Count().Should().Be(1, "one source operation was performed, with all included items affected"); - results.RecordedChangeSets.Skip(2).First().Select(static change => change.Reason).Should().AllBeEquivalentTo(ListChangeReason.Refresh, "all included items should have been refreshed"); - results.RecordedChangeSets.Skip(2).First().Select(static change => change.Item.Current).Should().BeEquivalentTo(EnumerateFilteredItems(), "all included items should have been refreshed"); + results.RecordedChangeSets.Skip(3).Count().Should().Be(1, "one source operation was performed, with all included items affected"); + results.RecordedChangeSets.Skip(3).First().Select(static change => change.Reason).Should().AllBeEquivalentTo(ListChangeReason.Refresh, "all included items should have been refreshed"); + results.RecordedChangeSets.Skip(3).First().Select(static change => change.Item.Current).Should().BeEquivalentTo(EnumerateFilteredItems(), "all included items should have been refreshed"); ShouldBeValid(results, EnumerateFilteredItems()); @@ -78,85 +97,85 @@ public void ChangesAreMadeAfterInitialPredicateState_ItemsAreFiltered(ListFilter item6.IsIncluded = !item6.IsIncluded; source.Refresh(Enumerable.Range(0, source.Count)); - results.RecordedChangeSets.Skip(3).Count().Should().Be(1, "one source operation was performed, with items being included and excluded"); + results.RecordedChangeSets.Skip(4).Count().Should().Be(1, "one source operation was performed, with items being included and excluded"); ShouldBeValid(results, EnumerateFilteredItems()); // Test Remove, with an included item - source.RemoveAt(3); + source.RemoveAt(5); - results.RecordedChangeSets.Skip(4).Count().Should().Be(1, "one source operation was performed, with one included item affected"); + results.RecordedChangeSets.Skip(5).Count().Should().Be(1, "one source operation was performed, with one included item affected"); ShouldBeValid(results, EnumerateFilteredItems()); // Test Remove, with an excluded item - source.RemoveAt(3); + source.RemoveAt(5); - results.RecordedChangeSets.Skip(5).Should().BeEmpty("one source operation was performed, but no included items were affected"); + results.RecordedChangeSets.Skip(6).Should().BeEmpty("one source operation was performed, but no included items were affected"); // Test Remove, with both included and excluded items - source.RemoveRange(index: 2, count: 2); + source.RemoveRange(index: 4, count: 2); - results.RecordedChangeSets.Skip(5).Count().Should().Be(1, "one source operation was performed, with one included item affected"); + results.RecordedChangeSets.Skip(6).Count().Should().Be(1, "one source operation was performed, with one included item affected"); ShouldBeValid(results, EnumerateFilteredItems()); // Test Replace, not affecting filtering - var item9 = new Item() { Id = 9, IsIncluded = false }; - var item10 = new Item() { Id = 10, IsIncluded = true }; + var item11 = new Item() { Id = 11, IsIncluded = false }; + var item12 = new Item() { Id = 12, IsIncluded = true }; source.Edit(updater => { - updater.Replace(item7, item9); - updater.Replace(item8, item10); + updater.Replace(item7, item11); + updater.Replace(item8, item12); }); - results.RecordedChangeSets.Skip(6).Count().Should().Be(1, "one source operation was performed, with one included item affected"); + results.RecordedChangeSets.Skip(7).Count().Should().Be(1, "one source operation was performed, with one included item affected"); ShouldBeValid(results, EnumerateFilteredItems()); // Test Replace, affecting filtering - var item11 = new Item() { Id = 11, IsIncluded = true }; - var item12 = new Item() { Id = 12, IsIncluded = false }; + var item13 = new Item() { Id = 13, IsIncluded = true }; + var item14 = new Item() { Id = 14, IsIncluded = false }; source.Edit(updater => { - updater.Replace(item9, item11); - updater.Replace(item10, item12); + updater.Replace(item11, item13); + updater.Replace(item12, item14); }); - results.RecordedChangeSets.Skip(7).Count().Should().Be(1, "one source operation was performed, with one included item affected"); + results.RecordedChangeSets.Skip(8).Count().Should().Be(1, "one source operation was performed, with one included item affected"); ShouldBeValid(results, EnumerateFilteredItems()); // Test Move of an included item, relative to another included item - var item13 = new Item() { Id = 13, IsIncluded = true }; - source.Add(item13); - source.Move(2, 4); + var item15 = new Item() { Id = 15, IsIncluded = true }; + source.Add(item15); + source.Move(4, 6); switch (filterPolicy) { case ListFilterPolicy.CalculateDiff: - results.RecordedChangeSets.Skip(8).Count().Should().Be(2, "two source operations were performed"); + results.RecordedChangeSets.Skip(9).Count().Should().Be(2, "two source operations were performed"); break; case ListFilterPolicy.ClearAndReplace: - results.RecordedChangeSets.Skip(8).Count().Should().Be(1, "two source operations were performed, one of which was a move, which are not propagated, as ordering is not preserved"); + results.RecordedChangeSets.Skip(9).Count().Should().Be(1, "two source operations were performed, one of which was a move, which are not propagated, as ordering is not preserved"); break; } ShouldBeValid(results, EnumerateFilteredItems()); // Test Move of an excluded item - source.Move(4, 2); + source.Move(6, 4); switch (filterPolicy) { case ListFilterPolicy.CalculateDiff: - results.RecordedChangeSets.Skip(10).Count().Should().Be(1, "one source operation was performed"); + results.RecordedChangeSets.Skip(11).Count().Should().Be(1, "one source operation was performed"); break; case ListFilterPolicy.ClearAndReplace: - results.RecordedChangeSets.Skip(9).Should().BeEmpty("one source operation was performed, a move, which are not propagated, as ordering is not preserved"); + results.RecordedChangeSets.Skip(10).Should().BeEmpty("one source operation was performed, a move, which are not propagated, as ordering is not preserved"); break; } @@ -167,28 +186,28 @@ public void ChangesAreMadeAfterInitialPredicateState_ItemsAreFiltered(ListFilter switch (filterPolicy) { case ListFilterPolicy.CalculateDiff: - results.RecordedChangeSets.Skip(11).Count().Should().Be(1, "one source operation was performed, with all included items affected"); + results.RecordedChangeSets.Skip(12).Count().Should().Be(1, "one source operation was performed, with all included items affected"); break; case ListFilterPolicy.ClearAndReplace: - results.RecordedChangeSets.Skip(9).Count().Should().Be(1, "one source operation was performed, with all included items affected"); + results.RecordedChangeSets.Skip(10).Count().Should().Be(1, "one source operation was performed, with all included items affected"); break; } ShouldBeValid(results, EnumerateFilteredItems()); // Test Clear, with only excluded items - source.Add(new Item() { Id = 14, IsIncluded = false }); + source.Add(new Item() { Id = 16, IsIncluded = false }); source.Clear(); switch (filterPolicy) { case ListFilterPolicy.CalculateDiff: - results.RecordedChangeSets.Skip(12).Should().BeEmpty("two source operations were performed, and neither affected included items"); + results.RecordedChangeSets.Skip(13).Should().BeEmpty("two source operations were performed, and neither affected included items"); break; case ListFilterPolicy.ClearAndReplace: - results.RecordedChangeSets.Skip(10).Should().BeEmpty("two source operations were performed, and neither affected included items"); + results.RecordedChangeSets.Skip(11).Should().BeEmpty("two source operations were performed, and neither affected included items"); break; } ShouldBeValid(results, EnumerateFilteredItems()); diff --git a/src/DynamicData/List/Internal/Filter.WithPredicateState.cs b/src/DynamicData/List/Internal/Filter.WithPredicateState.cs index 61ae3a840..13c722160 100644 --- a/src/DynamicData/List/Internal/Filter.WithPredicateState.cs +++ b/src/DynamicData/List/Internal/Filter.WithPredicateState.cs @@ -433,6 +433,9 @@ protected override void PerformAdd(ItemChange change) Item = change.Current }); + if (!isIncluded) + return; + for (var i = change.CurrentIndex + 1; i < ItemStates.Count; ++i) { var otherItemState = ItemStates[i];