Skip to content

Commit 0e3be7e

Browse files
committed
Add tests for ArgumentNullException-s in collections
1 parent bf31152 commit 0e3be7e

File tree

5 files changed

+103
-5
lines changed

5 files changed

+103
-5
lines changed

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,22 @@
77

88
namespace CommunityToolkit.Mvvm.UnitTests;
99

10-
public class IntGroup : List<int>, IGrouping<string, int>
10+
/// <summary>
11+
/// A simple <see cref="IGrouping{TKey, TElement}"/> implementation for <see cref="string"/> and <see cref="int"/> values.
12+
/// </summary>
13+
internal sealed class IntGroup : List<int>, IGrouping<string, int>
1114
{
15+
/// <summary>
16+
/// Creates a new <see cref="IntGroup"/> instance with the specified parameters.
17+
/// </summary>
18+
/// <param name="key">The group key.</param>
19+
/// <param name="collection">The group values.</param>
1220
public IntGroup(string key, IEnumerable<int> collection)
1321
: base(collection)
1422
{
1523
Key = key;
1624
}
1725

26+
/// <inheritdoc/>
1827
public string Key { get; }
1928
}

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

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
using System;
56
using System.Collections.Specialized;
67
using System.Linq;
78
using CommunityToolkit.Mvvm.Collections;
@@ -47,7 +48,8 @@ public void Add_ShouldRaiseEvent()
4748
bool collectionChangedEventRaised = false;
4849
int[] source = new[] { 1, 2, 3 };
4950
ObservableGroup<string, int> group = new("key", source);
50-
((INotifyCollectionChanged)group).CollectionChanged += (s, e) => collectionChangedEventRaised = true;
51+
52+
group.CollectionChanged += (s, e) => collectionChangedEventRaised = true;
5153

5254
group.Add(4);
5355

@@ -62,7 +64,8 @@ public void Update_ShouldRaiseEvent()
6264
bool collectionChangedEventRaised = false;
6365
int[] source = new[] { 1, 2, 3 };
6466
ObservableGroup<string, int> group = new("key", source);
65-
((INotifyCollectionChanged)group).CollectionChanged += (s, e) => collectionChangedEventRaised = true;
67+
68+
group.CollectionChanged += (s, e) => collectionChangedEventRaised = true;
6669

6770
group[1] = 4;
6871

@@ -77,7 +80,8 @@ public void Remove_ShouldRaiseEvent()
7780
bool collectionChangedEventRaised = false;
7881
int[] source = new[] { 1, 2, 3 };
7982
ObservableGroup<string, int>? group = new("key", source);
80-
((INotifyCollectionChanged)group).CollectionChanged += (s, e) => collectionChangedEventRaised = true;
83+
84+
group.CollectionChanged += (s, e) => collectionChangedEventRaised = true;
8185

8286
_ = group.Remove(1);
8387

@@ -92,7 +96,8 @@ public void Clear_ShouldRaiseEvent()
9296
bool collectionChangedEventRaised = false;
9397
int[] source = new[] { 1, 2, 3 };
9498
ObservableGroup<string, int>? group = new("key", source);
95-
((INotifyCollectionChanged)group).CollectionChanged += (s, e) => collectionChangedEventRaised = true;
99+
100+
group.CollectionChanged += (s, e) => collectionChangedEventRaised = true;
96101

97102
group.Clear();
98103

@@ -112,4 +117,41 @@ public void IReadOnlyObservableGroup_ShouldReturnExpectedValues(int count)
112117
Assert.AreEqual(iReadOnlyObservableGroup.Key, "key");
113118
Assert.AreEqual(iReadOnlyObservableGroup.Count, count);
114119
}
120+
121+
[TestMethod]
122+
[ExpectedException(typeof(ArgumentNullException))]
123+
public void Ctor_NullKey()
124+
{
125+
_ = new ObservableGroup<string, int>((string)null!);
126+
}
127+
128+
[TestMethod]
129+
[ExpectedException(typeof(ArgumentNullException))]
130+
public void Ctor_NullGroup()
131+
{
132+
_ = new ObservableGroup<string, int>((IGrouping<string, int>)null!);
133+
}
134+
135+
[TestMethod]
136+
[ExpectedException(typeof(ArgumentNullException))]
137+
public void Ctor_NullKeyWithNotNullElements()
138+
{
139+
_ = new ObservableGroup<string, int>(null!, new int[0]);
140+
}
141+
142+
[TestMethod]
143+
[ExpectedException(typeof(ArgumentNullException))]
144+
public void Ctor_NotNullKeyWithNullElements()
145+
{
146+
_ = new ObservableGroup<string, int>("A", null!);
147+
}
148+
149+
[TestMethod]
150+
[ExpectedException(typeof(ArgumentNullException))]
151+
public void Ctor_NullKeySetter()
152+
{
153+
ObservableGroup<string, int> group = new("A");
154+
155+
group.Key = null!;
156+
}
115157
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
using System;
56
using System.Collections.Generic;
67
using System.Linq;
78
using CommunityToolkit.Mvvm.Collections;
@@ -38,4 +39,11 @@ public void Ctor_WithGroups_ShouldHaveExpectedValues()
3839
Assert.AreEqual(groupCollection[1].Key, "B");
3940
CollectionAssert.AreEqual(groupCollection[1], new[] { 2, 4, 6 });
4041
}
42+
43+
[TestMethod]
44+
[ExpectedException(typeof(ArgumentNullException))]
45+
public void Ctor_NullCollection()
46+
{
47+
_ = new ObservableGroupedCollection<string, int>(null!);
48+
}
4149
}

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ public void Add_ShouldRaiseEvent()
5252
int[] source = new[] { 1, 2, 3 };
5353
ObservableGroup<string, int> sourceGroup = new("key", source);
5454
ReadOnlyObservableGroup<string, int> group = new(sourceGroup);
55+
5556
((INotifyCollectionChanged)group).CollectionChanged += (s, e) => collectionChangedEventRaised = true;
5657

5758
sourceGroup.Add(4);
@@ -69,6 +70,7 @@ public void Update_ShouldRaiseEvent()
6970
int[] source = new[] { 1, 2, 3 };
7071
ObservableGroup<string, int> sourceGroup = new("key", source);
7172
ReadOnlyObservableGroup<string, int> group = new(sourceGroup);
73+
7274
((INotifyCollectionChanged)group).CollectionChanged += (s, e) => collectionChangedEventRaised = true;
7375

7476
sourceGroup[1] = 4;
@@ -86,6 +88,7 @@ public void Remove_ShouldRaiseEvent()
8688
int[] source = new[] { 1, 2, 3 };
8789
ObservableGroup<string, int> sourceGroup = new("key", source);
8890
ReadOnlyObservableGroup<string, int> group = new(sourceGroup);
91+
8992
((INotifyCollectionChanged)group).CollectionChanged += (s, e) => collectionChangedEventRaised = true;
9093

9194
_ = sourceGroup.Remove(1);
@@ -103,6 +106,7 @@ public void Clear_ShouldRaiseEvent()
103106
int[] source = new[] { 1, 2, 3 };
104107
ObservableGroup<string, int> sourceGroup = new("key", source);
105108
ReadOnlyObservableGroup<string, int> group = new(sourceGroup);
109+
106110
((INotifyCollectionChanged)group).CollectionChanged += (s, e) => collectionChangedEventRaised = true;
107111

108112
sourceGroup.Clear();
@@ -125,4 +129,25 @@ public void IReadOnlyObservableGroup_ShouldReturnExpectedValues(int count)
125129
Assert.AreEqual(iReadOnlyObservableGroup.Key, "key");
126130
Assert.AreEqual(iReadOnlyObservableGroup.Count, count);
127131
}
132+
133+
[TestMethod]
134+
[ExpectedException(typeof(ArgumentNullException))]
135+
public void Ctor_NullKeyWithNotNullElements()
136+
{
137+
_ = new ReadOnlyObservableGroup<string, int>(null!, new ObservableCollection<int>());
138+
}
139+
140+
[TestMethod]
141+
[ExpectedException(typeof(ArgumentNullException))]
142+
public void Ctor_NotNullKeyWithNullElements()
143+
{
144+
_ = new ReadOnlyObservableGroup<string, int>("A", null!);
145+
}
146+
147+
[TestMethod]
148+
[ExpectedException(typeof(ArgumentNullException))]
149+
public void Ctor_NullGroup()
150+
{
151+
_ = new ReadOnlyObservableGroup<string, int>(null!);
152+
}
128153
}

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,4 +462,18 @@ private static bool IsReplaceEventValid(NotifyCollectionChangedEventArgs args, I
462462
}
463463

464464
private static bool IsResetEventValid(NotifyCollectionChangedEventArgs args) => args.Action == NotifyCollectionChangedAction.Reset && args.NewItems == null && args.OldItems == null;
465+
466+
[TestMethod]
467+
[ExpectedException(typeof(ArgumentNullException))]
468+
public void Ctor_NullCollectionWithObservableGroups()
469+
{
470+
_ = new ReadOnlyObservableGroupedCollection<string, int>((ObservableCollection<ObservableGroup<string, int>>)null!);
471+
}
472+
473+
[TestMethod]
474+
[ExpectedException(typeof(ArgumentNullException))]
475+
public void Ctor_NullCollectionWithReadOnlyObservableGroups()
476+
{
477+
_ = new ReadOnlyObservableGroupedCollection<string, int>((ObservableCollection<ReadOnlyObservableGroup<string, int>>)null!);
478+
}
465479
}

0 commit comments

Comments
 (0)