diff --git a/src/CommunityToolkit/Xamarin.CommunityToolkit.UnitTests/ObjectModel/ObservableRangeCollection_Tests.cs b/src/CommunityToolkit/Xamarin.CommunityToolkit.UnitTests/ObjectModel/ObservableRangeCollection_Tests.cs index 9b98b339b..beba839f1 100644 --- a/src/CommunityToolkit/Xamarin.CommunityToolkit.UnitTests/ObjectModel/ObservableRangeCollection_Tests.cs +++ b/src/CommunityToolkit/Xamarin.CommunityToolkit.UnitTests/ObjectModel/ObservableRangeCollection_Tests.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Generic; using System.Collections.Specialized; using Xamarin.CommunityToolkit.ObjectModel; @@ -18,12 +19,7 @@ public void AddRange() { Assert.Equal(NotifyCollectionChangedAction.Add, e.Action); Assert.Null(e.OldItems); - Assert.Equal(toAdd.Length, e.NewItems.Count); - - for (var i = 0; i < toAdd.Length; i++) - { - Assert.Equal(toAdd[i], (int)e.NewItems[i]); - } + Assert.Equal(toAdd, e.NewItems); }; collection.AddRange(toAdd); } @@ -130,7 +126,6 @@ public void RemoveRangeRemoveFact() } }; collection.RemoveRange(toRemove, NotifyCollectionChangedAction.Remove); - } [Fact] @@ -180,5 +175,38 @@ public void RemoveRange_should_NOT_mutate_collection_when_source_data_is_not_pre // the collection should not be modified if the source items are not found Assert.Equal(6, collection.Count); } + + class CollectionWrapper + { + public readonly ObservableRangeCollection Collection = new ObservableRangeCollection(); + public ObservableRangeCollection NullCollection; + } + + [Fact] + public void AddCollection() + { + var toAdd = new[] { 3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9, 7, 9, 3, 2, 3 }; + + var wrapper = new CollectionWrapper() + { + Collection = { toAdd } + }; + + Assert.Equal(toAdd, wrapper.Collection); + } + + [Fact] + public void AddToNullCollection() + { + var toAdd = new[] { 3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9, 7, 9, 3, 2, 3 }; + + Assert.Throws(() => + { + var wrapper = new CollectionWrapper() + { + NullCollection = { toAdd } + }; + }); + } } } \ No newline at end of file diff --git a/src/CommunityToolkit/Xamarin.CommunityToolkit/ObjectModel/Extensions/INotifyPropertyChangedEx.shared.cs b/src/CommunityToolkit/Xamarin.CommunityToolkit/ObjectModel/Extensions/INotifyPropertyChangedExtension.shared.cs similarity index 92% rename from src/CommunityToolkit/Xamarin.CommunityToolkit/ObjectModel/Extensions/INotifyPropertyChangedEx.shared.cs rename to src/CommunityToolkit/Xamarin.CommunityToolkit/ObjectModel/Extensions/INotifyPropertyChangedExtension.shared.cs index c5934cae9..b465d366c 100644 --- a/src/CommunityToolkit/Xamarin.CommunityToolkit/ObjectModel/Extensions/INotifyPropertyChangedEx.shared.cs +++ b/src/CommunityToolkit/Xamarin.CommunityToolkit/ObjectModel/Extensions/INotifyPropertyChangedExtension.shared.cs @@ -3,7 +3,7 @@ namespace Xamarin.CommunityToolkit.ObjectModel.Extensions { - public static class INotifyPropertyChangedEx + public static class INotifyPropertyChangedExtension { public static void WeakSubscribe(this INotifyPropertyChanged target, T subscriber, Action action) { diff --git a/src/CommunityToolkit/Xamarin.CommunityToolkit/ObjectModel/Extensions/ObservableRangeCollectionExtension.shared.cs b/src/CommunityToolkit/Xamarin.CommunityToolkit/ObjectModel/Extensions/ObservableRangeCollectionExtension.shared.cs new file mode 100644 index 000000000..6385ec22b --- /dev/null +++ b/src/CommunityToolkit/Xamarin.CommunityToolkit/ObjectModel/Extensions/ObservableRangeCollectionExtension.shared.cs @@ -0,0 +1,20 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; + +namespace Xamarin.CommunityToolkit.ObjectModel +{ + public static class ObservableRangeCollectionExtension + { + /// + /// To be used in collection initializer + /// + [EditorBrowsable(EditorBrowsableState.Never)] + public static void Add(this ObservableRangeCollection source, IEnumerable collection) + { + _ = source ?? throw new ArgumentNullException(nameof(source)); + + source.AddRange(collection); + } + } +}