Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 52 additions & 33 deletions src/DynamicData.Tests/List/FilterFixture.WithPredicateState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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());


Expand All @@ -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;
}

Expand All @@ -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());
Expand Down
3 changes: 3 additions & 0 deletions src/DynamicData/List/Internal/Filter.WithPredicateState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,9 @@ protected override void PerformAdd(ItemChange<T> change)
Item = change.Current
});

if (!isIncluded)
return;

for (var i = change.CurrentIndex + 1; i < ItemStates.Count; ++i)
{
var otherItemState = ItemStates[i];
Expand Down
Loading