Skip to content

Commit 0a92932

Browse files
committed
Added collection IsNullOrEmpty extension
1 parent b9fe264 commit 0a92932

File tree

2 files changed

+76
-4
lines changed

2 files changed

+76
-4
lines changed

src/MADE.Collections/CollectionExtensions.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,5 +445,16 @@ public static void SortDescending<T, TKey>(this ObservableCollection<T> source,
445445
idx++;
446446
}
447447
}
448+
449+
/// <summary>Indicates whether the specified collection is <see langword="null" /> or empty (containing no items).</summary>
450+
/// <param name="source">The collection to test.</param>
451+
/// <typeparam name="T">The type of item in the collection.</typeparam>
452+
/// <returns>
453+
/// <see langword="true" /> if the <paramref name="source" /> parameter is <see langword="null" /> or empty (containing no items); otherwise, <see langword="false" />.
454+
/// </returns>
455+
public static bool IsNullOrEmpty<T>(this IEnumerable<T> source)
456+
{
457+
return source is null || !source.Any();
458+
}
448459
}
449460
}

tests/MADE.Collections.Tests/Tests/CollectionExtensionsTests.cs

Lines changed: 65 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,69 @@ public void ShouldReturnFalseForInvalidCases(Collection<int> expected, Collectio
225225
};
226226
}
227227

228+
public class WhenValidatingIfCollectionIsNullOrEmpty
229+
{
230+
[TestCaseSource(nameof(ValidEnumerableCases))]
231+
public void ShouldReturnTrueIfEnumerableIsNullOrEmpty(IEnumerable<int> collection)
232+
{
233+
// Act
234+
bool isEmpty = collection.IsNullOrEmpty();
235+
236+
// Assert
237+
isEmpty.ShouldBeTrue();
238+
}
239+
240+
[TestCaseSource(nameof(ValidDictionaryCases))]
241+
public void ShouldReturnTrueIfDictionaryIsNullOrEmpty(Dictionary<int, string> collection)
242+
{
243+
// Act
244+
bool isEmpty = collection.IsNullOrEmpty();
245+
246+
// Assert
247+
isEmpty.ShouldBeTrue();
248+
}
249+
250+
[TestCaseSource(nameof(InvalidEnumerableCases))]
251+
public void ShouldReturnFalseIfEnumerableIsNotNullOrEmpty(IEnumerable<int> collection)
252+
{
253+
// Act
254+
bool isEmpty = collection.IsNullOrEmpty();
255+
256+
// Assert
257+
isEmpty.ShouldBeFalse();
258+
}
259+
260+
[TestCaseSource(nameof(InvalidDictionaryCases))]
261+
public void ShouldReturnFalseIfDictionaryIsNotNullOrEmpty(Dictionary<int, string> collection)
262+
{
263+
// Act
264+
bool isEmpty = collection.IsNullOrEmpty();
265+
266+
// Assert
267+
isEmpty.ShouldBeFalse();
268+
}
269+
270+
private static object[] ValidEnumerableCases =
271+
{
272+
new object[] {null}, new object[] {new ObservableCollection<int>()},
273+
};
274+
275+
private static object[] ValidDictionaryCases =
276+
{
277+
new object[] {null}, new object[] {new Dictionary<int, string>()},
278+
};
279+
280+
private static object[] InvalidEnumerableCases =
281+
{
282+
new object[] {new ObservableCollection<int> {1, 2, 3}},
283+
};
284+
285+
private static object[] InvalidDictionaryCases =
286+
{
287+
new object[] {new Dictionary<int, string> {{1, "A"}, {2, "B"}, {3, "C"}}},
288+
};
289+
}
290+
228291
public class WhenSortingObservableCollections
229292
{
230293
[Test]
@@ -294,10 +357,8 @@ public void ShouldSortDescendingByComplexType()
294357
// Assert
295358
collection.ShouldBe(new ComplexObject[]
296359
{
297-
new() {Id = 1, Name = "James Croft"},
298-
new() {Id = 3, Name = "Guy Wilmer"},
299-
new() {Id = 0, Name = "Ben Hartley"},
300-
new() {Id = 2, Name = "Adam Llewellyn"},
360+
new() {Id = 1, Name = "James Croft"}, new() {Id = 3, Name = "Guy Wilmer"},
361+
new() {Id = 0, Name = "Ben Hartley"}, new() {Id = 2, Name = "Adam Llewellyn"},
301362
});
302363
}
303364

0 commit comments

Comments
 (0)