Skip to content
This repository was archived by the owner on May 1, 2024. It is now read-only.

Add "Add(IEnumerable<T> collection)" to ObservableRangeCollection<T> #891

Merged
merged 8 commits into from
Feb 16, 2021
Merged
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
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using Xamarin.CommunityToolkit.ObjectModel;
Expand All @@ -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);
}
Expand Down Expand Up @@ -130,7 +126,6 @@ public void RemoveRangeRemoveFact()
}
};
collection.RemoveRange(toRemove, NotifyCollectionChangedAction.Remove);

}

[Fact]
Expand Down Expand Up @@ -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<T>
{
public readonly ObservableRangeCollection<T> Collection = new ObservableRangeCollection<T>();
public ObservableRangeCollection<T> 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<int>()
{
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<ArgumentNullException>(() =>
{
var wrapper = new CollectionWrapper<int>()
{
NullCollection = { toAdd }
};
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace Xamarin.CommunityToolkit.ObjectModel.Extensions
{
public static class INotifyPropertyChangedEx
public static class INotifyPropertyChangedExtension
{
public static void WeakSubscribe<T>(this INotifyPropertyChanged target, T subscriber, Action<T, object, EventArgs> action)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;

namespace Xamarin.CommunityToolkit.ObjectModel
{
public static class ObservableRangeCollectionExtension
{
/// <summary>
/// To be used in collection initializer
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public static void Add<T>(this ObservableRangeCollection<T> source, IEnumerable<T> collection)
{
_ = source ?? throw new ArgumentNullException(nameof(source));

source.AddRange(collection);
}
}
}