Skip to content

Commit d1e75b9

Browse files
committed
Add ObservableGroupedCollectionExtensions tests, bug fixes
1 parent dd2a72d commit d1e75b9

File tree

2 files changed

+115
-24
lines changed

2 files changed

+115
-24
lines changed

CommunityToolkit.Mvvm/Collections/ObservableGroupedCollectionExtensions.cs

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -584,25 +584,24 @@ public static ObservableGroup<TKey, TElement> InsertItem<TKey, TElement>(this Ob
584584
{
585585
group = source.InsertGroup(key, new[] { item });
586586
}
587-
else
587+
else if (group.TryGetList(out List<TElement>? list))
588588
{
589-
if (group.TryGetList(out List<TElement>? list))
590-
{
591-
int index = 0;
589+
int index = 0;
592590

593-
foreach (TElement element in list)
591+
foreach (TElement element in list)
592+
{
593+
if (Comparer<TElement>.Default.Compare(item, element) < 0)
594594
{
595-
if (Comparer<TElement>.Default.Compare(item, element) < 0)
596-
{
597-
break;
598-
}
599-
600-
index++;
595+
break;
601596
}
602597

603-
group.Insert(index, item);
598+
index++;
604599
}
605600

601+
group.Insert(index, item);
602+
}
603+
else
604+
{
606605
[MethodImpl(MethodImplOptions.NoInlining)]
607606
static void InsertItemFallback(ObservableCollection<TElement> source, TElement item)
608607
{
@@ -658,25 +657,24 @@ public static ObservableGroup<TKey, TElement> InsertItem<TKey, TElement>(
658657
{
659658
group = source.InsertGroup(key, keyComparer, new[] { item });
660659
}
661-
else
660+
else if (group.TryGetList(out List<TElement>? list))
662661
{
663-
if (group.TryGetList(out List<TElement>? list))
664-
{
665-
int index = 0;
662+
int index = 0;
666663

667-
foreach (TElement element in list)
664+
foreach (TElement element in list)
665+
{
666+
if (itemComparer.Compare(item, element) < 0)
668667
{
669-
if (itemComparer.Compare(item, element) < 0)
670-
{
671-
break;
672-
}
673-
674-
index++;
668+
break;
675669
}
676670

677-
group.Insert(index, item);
671+
index++;
678672
}
679673

674+
group.Insert(index, item);
675+
}
676+
else
677+
{
680678
[MethodImpl(MethodImplOptions.NoInlining)]
681679
static void InsertItemFallback(ObservableCollection<TElement> source, TElement item, IComparer<TElement> comparer)
682680
{

tests/CommunityToolkit.Mvvm.UnitTests/Collections/Test_ObservableGroupedCollectionExtensions.cs

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// See the LICENSE file in the project root for more information.
44

55
using System;
6+
using System.Collections.Generic;
67
using CommunityToolkit.Mvvm.Collections;
78
using Microsoft.VisualStudio.TestTools.UnitTesting;
89

@@ -148,6 +149,38 @@ public void Test_ObservableGroupedCollectionExtensions_AddGroup_WithParamsCollec
148149
CollectionAssert.AreEqual(groupedCollection, new[] { addedGroup });
149150
}
150151

152+
[TestMethod]
153+
public void Test_ObservableGroupedCollectionExtensions_InsertGroup()
154+
{
155+
ObservableGroupedCollection<string, int> groupedCollection = new();
156+
157+
ObservableGroup<string, int> a = groupedCollection.InsertGroup("A", new[] { 1, 2, 3 });
158+
ObservableGroup<string, int> d = groupedCollection.InsertGroup("D", new[] { 1, 2, 3 });
159+
ObservableGroup<string, int> e = groupedCollection.InsertGroup("E", new[] { 1, 2, 3 });
160+
ObservableGroup<string, int> b = groupedCollection.InsertGroup("B", new[] { 1, 2, 3 });
161+
ObservableGroup<string, int> z = groupedCollection.InsertGroup("Z", new[] { 1, 2, 3 });
162+
ObservableGroup<string, int> c = groupedCollection.InsertGroup("C", new[] { 1, 2, 3 });
163+
164+
CollectionAssert.AllItemsAreNotNull(new[] { a, d, e, b, z, c });
165+
CollectionAssert.AreEqual(new[] { a, b, c, d, e, z }, groupedCollection);
166+
}
167+
168+
[TestMethod]
169+
public void Test_ObservableGroupedCollectionExtensions_InsertGroup_WithGrouping()
170+
{
171+
ObservableGroupedCollection<string, int> groupedCollection = new();
172+
173+
ObservableGroup<string, int> a = groupedCollection.InsertGroup(new IntGroup("A", new[] { 1, 2, 3 }));
174+
ObservableGroup<string, int> d = groupedCollection.InsertGroup(new IntGroup("D", new[] { 1, 2, 3 }));
175+
ObservableGroup<string, int> e = groupedCollection.InsertGroup(new IntGroup("E", new[] { 1, 2, 3 }));
176+
ObservableGroup<string, int> b = groupedCollection.InsertGroup(new IntGroup("B", new[] { 1, 2, 3 }));
177+
ObservableGroup<string, int> z = groupedCollection.InsertGroup(new IntGroup("Z", new[] { 1, 2, 3 }));
178+
ObservableGroup<string, int> c = groupedCollection.InsertGroup(new IntGroup("C", new[] { 1, 2, 3 }));
179+
180+
CollectionAssert.AllItemsAreNotNull(new[] { a, d, e, b, z, c });
181+
CollectionAssert.AreEqual(new[] { a, b, c, d, e, z }, groupedCollection);
182+
}
183+
151184
[TestMethod]
152185
public void Test_ObservableGroupedCollectionExtensions_AddItem_WhenTargetGroupDoesNotExists_ShouldCreateAndAddNewGroup()
153186
{
@@ -223,6 +256,66 @@ public void Test_ObservableGroupedCollectionExtensions_AddItem_WhenSeveralTarget
223256
CollectionAssert.AreEqual(groupedCollection[3], new[] { 10, 11 });
224257
}
225258

259+
[TestMethod]
260+
public void Test_ObservableGroupedCollectionExtensions_InsertItem()
261+
{
262+
ObservableGroupedCollection<string, int> groupedCollection = new();
263+
264+
ObservableGroup<string, int> group1 = groupedCollection.InsertItem("A", 1);
265+
ObservableGroup<string, int> group2 = groupedCollection.InsertItem("A", 2);
266+
ObservableGroup<string, int> group6 = groupedCollection.InsertItem("A", 6);
267+
ObservableGroup<string, int> group4 = groupedCollection.InsertItem("A", 4);
268+
ObservableGroup<string, int> group3 = groupedCollection.InsertItem("A", 3);
269+
ObservableGroup<string, int> group99 = groupedCollection.InsertItem("A", 99);
270+
ObservableGroup<string, int> group8 = groupedCollection.InsertItem("B", 8);
271+
ObservableGroup<string, int> group7 = groupedCollection.InsertItem("B", 7);
272+
273+
Assert.AreEqual(2, groupedCollection.Count);
274+
CollectionAssert.AllItemsAreNotNull(new[] { group1, group2, group6, group4, group3, group99, group8, group7 });
275+
276+
Assert.AreSame(group1, group2);
277+
Assert.AreSame(group1, group6);
278+
Assert.AreSame(group1, group4);
279+
Assert.AreSame(group1, group3);
280+
Assert.AreSame(group1, group2);
281+
Assert.AreSame(group1, group99);
282+
Assert.AreNotSame(group1, group8);
283+
Assert.AreSame(group8, group7);
284+
285+
CollectionAssert.AreEqual(new[] { 1, 2, 3, 4, 6, 99 }, group1);
286+
CollectionAssert.AreEqual(new[] { 7, 8 }, group8);
287+
}
288+
289+
[TestMethod]
290+
public void Test_ObservableGroupedCollectionExtensions_InsertItem_WithComparer()
291+
{
292+
ObservableGroupedCollection<string, int> groupedCollection = new();
293+
294+
ObservableGroup<string, int> group1 = groupedCollection.InsertItem("A", Comparer<string>.Default, 1, Comparer<int>.Default);
295+
ObservableGroup<string, int> group2 = groupedCollection.InsertItem("A", Comparer<string>.Default, 2, Comparer<int>.Default);
296+
ObservableGroup<string, int> group6 = groupedCollection.InsertItem("A", Comparer<string>.Default, 6, Comparer<int>.Default);
297+
ObservableGroup<string, int> group4 = groupedCollection.InsertItem("A", Comparer<string>.Default, 4, Comparer<int>.Default);
298+
ObservableGroup<string, int> group3 = groupedCollection.InsertItem("A", Comparer<string>.Default, 3, Comparer<int>.Default);
299+
ObservableGroup<string, int> group99 = groupedCollection.InsertItem("A", Comparer<string>.Default, 99, Comparer<int>.Default);
300+
ObservableGroup<string, int> group8 = groupedCollection.InsertItem("B", Comparer<string>.Default, 8, Comparer<int>.Default);
301+
ObservableGroup<string, int> group7 = groupedCollection.InsertItem("B", Comparer<string>.Default, 7, Comparer<int>.Default);
302+
303+
Assert.AreEqual(2, groupedCollection.Count);
304+
CollectionAssert.AllItemsAreNotNull(new[] { group1, group2, group6, group4, group3, group99, group8, group7 });
305+
306+
Assert.AreSame(group1, group2);
307+
Assert.AreSame(group1, group6);
308+
Assert.AreSame(group1, group4);
309+
Assert.AreSame(group1, group3);
310+
Assert.AreSame(group1, group2);
311+
Assert.AreSame(group1, group99);
312+
Assert.AreNotSame(group1, group8);
313+
Assert.AreSame(group8, group7);
314+
315+
CollectionAssert.AreEqual(new[] { 1, 2, 3, 4, 6, 99 }, group1);
316+
CollectionAssert.AreEqual(new[] { 7, 8 }, group8);
317+
}
318+
226319
[TestMethod]
227320
public void Test_ObservableGroupedCollectionExtensions_RemoveGroup_WhenGroupDoesNotExists_ShouldDoNothing()
228321
{

0 commit comments

Comments
 (0)