From 7b04571ee9ca81130368b1dc6744055491ac70d3 Mon Sep 17 00:00:00 2001 From: Geferon Date: Sat, 6 Jul 2024 14:04:33 +0200 Subject: [PATCH] ToChangeSet now emits empty sets too (#916) Added unit test for use case as well --- .../Binding/AvaloniaDictionaryFixture.cs | 4 ++-- ...bleCollectionExtendedToChangeSetFixture.cs | 2 +- .../ObservableCollectionToChangeSetFixture.cs | 2 +- ...yObservableCollectionToChangeSetFixture.cs | 2 +- .../Issues/EmptyToChangeSetIssue.cs | 21 +++++++++++++++++++ .../Binding/ObservableCollectionEx.cs | 5 +---- 6 files changed, 27 insertions(+), 9 deletions(-) create mode 100644 src/DynamicData.Tests/Issues/EmptyToChangeSetIssue.cs diff --git a/src/DynamicData.Tests/Binding/AvaloniaDictionaryFixture.cs b/src/DynamicData.Tests/Binding/AvaloniaDictionaryFixture.cs index 12806fbd8..478fa6807 100644 --- a/src/DynamicData.Tests/Binding/AvaloniaDictionaryFixture.cs +++ b/src/DynamicData.Tests/Binding/AvaloniaDictionaryFixture.cs @@ -33,7 +33,7 @@ public void Add() _collection.Add("Someone", person); - _results.Messages.Count.Should().Be(1); + _results.Messages.Count.Should().Be(2); _results.Data.Count.Should().Be(1); _results.Data.Items[0].Should().Be(person); } @@ -86,7 +86,7 @@ public interface IAvaloniaReadOnlyDictionary /* Copied from Avalionia because an issue was raised due to compatibility issues with ToObservableChangeSet(). -There's not other way of testing it. +There's not other way of testing it. See https://github.com/AvaloniaUI/Avalonia/blob/d7c82a1a6f7eb95b2214f20a281fa0581fb7b792/src/Avalonia.Base/Collections/AvaloniaDictionary.cs#L17 */ diff --git a/src/DynamicData.Tests/Binding/ObservableCollectionExtendedToChangeSetFixture.cs b/src/DynamicData.Tests/Binding/ObservableCollectionExtendedToChangeSetFixture.cs index c971fb66e..a3d89a0d1 100644 --- a/src/DynamicData.Tests/Binding/ObservableCollectionExtendedToChangeSetFixture.cs +++ b/src/DynamicData.Tests/Binding/ObservableCollectionExtendedToChangeSetFixture.cs @@ -30,7 +30,7 @@ public void Add() { _collection.Add(1); - _results.Messages.Count.Should().Be(1); + _results.Messages.Count.Should().Be(2); _results.Data.Count.Should().Be(1); _results.Data.Items[0].Should().Be(1); } diff --git a/src/DynamicData.Tests/Binding/ObservableCollectionToChangeSetFixture.cs b/src/DynamicData.Tests/Binding/ObservableCollectionToChangeSetFixture.cs index 2816b18b4..ae492c411 100644 --- a/src/DynamicData.Tests/Binding/ObservableCollectionToChangeSetFixture.cs +++ b/src/DynamicData.Tests/Binding/ObservableCollectionToChangeSetFixture.cs @@ -28,7 +28,7 @@ public void Add() { _collection.Add(1); - _results.Messages.Count.Should().Be(1); + _results.Messages.Count.Should().BeGreaterOrEqualTo(1); _results.Data.Count.Should().Be(1); _results.Data.Items[0].Should().Be(1); } diff --git a/src/DynamicData.Tests/Binding/ReadOnlyObservableCollectionToChangeSetFixture.cs b/src/DynamicData.Tests/Binding/ReadOnlyObservableCollectionToChangeSetFixture.cs index 36e4580ce..13efc2a87 100644 --- a/src/DynamicData.Tests/Binding/ReadOnlyObservableCollectionToChangeSetFixture.cs +++ b/src/DynamicData.Tests/Binding/ReadOnlyObservableCollectionToChangeSetFixture.cs @@ -31,7 +31,7 @@ public void Add() { _collection.Add(1); - _results.Messages.Count.Should().Be(1); + _results.Messages.Count.Should().Be(2); _results.Data.Count.Should().Be(1); _results.Data.Items[0].Should().Be(1); } diff --git a/src/DynamicData.Tests/Issues/EmptyToChangeSetIssue.cs b/src/DynamicData.Tests/Issues/EmptyToChangeSetIssue.cs new file mode 100644 index 000000000..1044f5cd2 --- /dev/null +++ b/src/DynamicData.Tests/Issues/EmptyToChangeSetIssue.cs @@ -0,0 +1,21 @@ +using System.Collections.ObjectModel; +using System.Reactive; +using DynamicData.Binding; +using FluentAssertions; +using Xunit; + +namespace DynamicData.Tests.Issues +{ + public class EmptyToChangeSetIssue + { + [Fact] + public void EmptyCollectionToChangeSetBehaviour() + { + var collection = new ObservableCollection(); + + var results = collection.ToObservableChangeSet().AsAggregator(); + results.Messages.Count.Should() + .BeGreaterThan(0, "An empty collection should still have an update, even if empty."); + } + } +} diff --git a/src/DynamicData/Binding/ObservableCollectionEx.cs b/src/DynamicData/Binding/ObservableCollectionEx.cs index d7fa41362..6e6c5f25f 100644 --- a/src/DynamicData/Binding/ObservableCollectionEx.cs +++ b/src/DynamicData/Binding/ObservableCollectionEx.cs @@ -119,10 +119,7 @@ public static IObservable> ToObservableChangeSet(t { var data = new ChangeAwareList(source); - if (data.Count > 0) - { - observer.OnNext(data.CaptureChanges()); - } + observer.OnNext(data.CaptureChanges()); return source.ObserveCollectionChanges().Scan( data,