From 19daee8ab7ed037e34fa7cd464b587078c65550d Mon Sep 17 00:00:00 2001 From: Roland Pheasant Date: Mon, 23 May 2022 20:07:35 +0100 Subject: [PATCH] Connect Predicate (#599) * ChangeAwareList.AddRange not specify index in change args. Fixes #372 * Add unit test to illustrate that connect predicate works as expected and has historically been fixed. Fixes #471 --- src/DynamicData.Tests/Cache/FilterFixture.cs | 1 + .../Cache/SourceCacheFixture.cs | 31 +++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/src/DynamicData.Tests/Cache/FilterFixture.cs b/src/DynamicData.Tests/Cache/FilterFixture.cs index 51dbf7567..f2c7a3974 100644 --- a/src/DynamicData.Tests/Cache/FilterFixture.cs +++ b/src/DynamicData.Tests/Cache/FilterFixture.cs @@ -127,6 +127,7 @@ public void Clear() _results.Data.Count.Should().Be(0, "Should be nothing cached"); } + public void Dispose() { _source.Dispose(); diff --git a/src/DynamicData.Tests/Cache/SourceCacheFixture.cs b/src/DynamicData.Tests/Cache/SourceCacheFixture.cs index adbca902b..d1182d4e8 100644 --- a/src/DynamicData.Tests/Cache/SourceCacheFixture.cs +++ b/src/DynamicData.Tests/Cache/SourceCacheFixture.cs @@ -1,4 +1,5 @@ using System; +using System.Linq; using System.Reactive.Linq; using DynamicData.Tests.Domain; @@ -157,4 +158,34 @@ public void EmptyChangesWithFilter() change.Should().NotBeNull(); change!.Count.Should().Be(0); } + + + + [Fact] + public void StaticFilterRemove() + { + var cache = new SourceCache(x => x.Id); + + var above5 = cache.Connect(x => x.Value > 5).AsObservableCache(); + var below5 = cache.Connect(x => x.Value <= 5).AsObservableCache(); + + cache.AddOrUpdate(Enumerable.Range(1,10).Select(i=> new SomeObject(i,i))); + + + above5.Items.Should().BeEquivalentTo(Enumerable.Range(6, 5).Select(i => new SomeObject(i, i))); + below5.Items.Should().BeEquivalentTo(Enumerable.Range(1, 5).Select(i => new SomeObject(i, i))); + + //should move from above 5 to below 5 + cache.AddOrUpdate(new SomeObject(6,-1)); + + above5.Count.Should().Be(4); + below5.Count.Should().Be(6); + + + above5.Items.Should().BeEquivalentTo(Enumerable.Range(7, 4).Select(i => new SomeObject(i, i))); + below5.Items.Should().BeEquivalentTo(Enumerable.Range(1, 6).Select(i => new SomeObject(i, i == 6 ? -1 : i))); + } + + public record class SomeObject(int Id, int Value); + }