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

Commit e38a6e7

Browse files
Add "Add(IEnumerable<T> collection)" to ObservableRangeCollection<T> (#891)
* Create ObservableRangeCollectionEx.shared.cs * Add test * Update src/CommunityToolkit/Xamarin.CommunityToolkit/ObjectModel/Extensions/ObservableRangeCollectionEx.shared.cs * Resolve comments * Add AddToNullCollection test * Fix WeakSubscribe parameter type * Revert "Fix WeakSubscribe parameter type" This reverts commit 221ee0f. Co-authored-by: Andrei <[email protected]>
1 parent 3f7bc8c commit e38a6e7

File tree

3 files changed

+56
-8
lines changed

3 files changed

+56
-8
lines changed

src/CommunityToolkit/Xamarin.CommunityToolkit.UnitTests/ObjectModel/ObservableRangeCollection_Tests.cs

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using System.Collections.Generic;
23
using System.Collections.Specialized;
34
using Xamarin.CommunityToolkit.ObjectModel;
@@ -18,12 +19,7 @@ public void AddRange()
1819
{
1920
Assert.Equal(NotifyCollectionChangedAction.Add, e.Action);
2021
Assert.Null(e.OldItems);
21-
Assert.Equal(toAdd.Length, e.NewItems.Count);
22-
23-
for (var i = 0; i < toAdd.Length; i++)
24-
{
25-
Assert.Equal(toAdd[i], (int)e.NewItems[i]);
26-
}
22+
Assert.Equal(toAdd, e.NewItems);
2723
};
2824
collection.AddRange(toAdd);
2925
}
@@ -130,7 +126,6 @@ public void RemoveRangeRemoveFact()
130126
}
131127
};
132128
collection.RemoveRange(toRemove, NotifyCollectionChangedAction.Remove);
133-
134129
}
135130

136131
[Fact]
@@ -180,5 +175,38 @@ public void RemoveRange_should_NOT_mutate_collection_when_source_data_is_not_pre
180175
// the collection should not be modified if the source items are not found
181176
Assert.Equal(6, collection.Count);
182177
}
178+
179+
class CollectionWrapper<T>
180+
{
181+
public readonly ObservableRangeCollection<T> Collection = new ObservableRangeCollection<T>();
182+
public ObservableRangeCollection<T> NullCollection;
183+
}
184+
185+
[Fact]
186+
public void AddCollection()
187+
{
188+
var toAdd = new[] { 3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9, 7, 9, 3, 2, 3 };
189+
190+
var wrapper = new CollectionWrapper<int>()
191+
{
192+
Collection = { toAdd }
193+
};
194+
195+
Assert.Equal(toAdd, wrapper.Collection);
196+
}
197+
198+
[Fact]
199+
public void AddToNullCollection()
200+
{
201+
var toAdd = new[] { 3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9, 7, 9, 3, 2, 3 };
202+
203+
Assert.Throws<ArgumentNullException>(() =>
204+
{
205+
var wrapper = new CollectionWrapper<int>()
206+
{
207+
NullCollection = { toAdd }
208+
};
209+
});
210+
}
183211
}
184212
}

src/CommunityToolkit/Xamarin.CommunityToolkit/ObjectModel/Extensions/INotifyPropertyChangedEx.shared.cs renamed to src/CommunityToolkit/Xamarin.CommunityToolkit/ObjectModel/Extensions/INotifyPropertyChangedExtension.shared.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
namespace Xamarin.CommunityToolkit.ObjectModel.Extensions
55
{
6-
public static class INotifyPropertyChangedEx
6+
public static class INotifyPropertyChangedExtension
77
{
88
public static void WeakSubscribe<T>(this INotifyPropertyChanged target, T subscriber, Action<T, object, EventArgs> action)
99
{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.ComponentModel;
4+
5+
namespace Xamarin.CommunityToolkit.ObjectModel
6+
{
7+
public static class ObservableRangeCollectionExtension
8+
{
9+
/// <summary>
10+
/// To be used in collection initializer
11+
/// </summary>
12+
[EditorBrowsable(EditorBrowsableState.Never)]
13+
public static void Add<T>(this ObservableRangeCollection<T> source, IEnumerable<T> collection)
14+
{
15+
_ = source ?? throw new ArgumentNullException(nameof(source));
16+
17+
source.AddRange(collection);
18+
}
19+
}
20+
}

0 commit comments

Comments
 (0)