Skip to content

Commit 66e4ef5

Browse files
committed
added IsNullOrEmpty for collections
1 parent 4c316a8 commit 66e4ef5

2 files changed

Lines changed: 77 additions & 33 deletions

File tree

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using System.Collections.Generic;
2+
using FluentAssertions;
3+
using Xunit;
4+
5+
namespace Light.GuardClauses.Tests.CollectionAssertions
6+
{
7+
public static class IsNullOrEmptyTests
8+
{
9+
[Fact]
10+
public static void CollectionNull()
11+
{
12+
var collection = (IEnumerable<int>) null;
13+
14+
// ReSharper disable once ConditionIsAlwaysTrueOrFalse
15+
var result = collection.IsNullOrEmpty();
16+
17+
// ReSharper disable once ConditionIsAlwaysTrueOrFalse
18+
result.Should().BeTrue();
19+
}
20+
21+
[Fact]
22+
public static void CollectionEmpty()
23+
{
24+
var emptyCollection = new int[0];
25+
26+
var result = emptyCollection.IsNullOrEmpty();
27+
28+
result.Should().BeTrue();
29+
}
30+
31+
[Fact]
32+
public static void CollectionNotEmpty()
33+
{
34+
var collection = new[] { "Foo", "Bar" };
35+
36+
var result = collection.IsNullOrEmpty();
37+
38+
result.Should().BeFalse();
39+
}
40+
}
41+
}

Code/Light.GuardClauses/Check.EnumerableAssertions.cs

Lines changed: 36 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,17 @@ public static TCollection MustHaveCount<TCollection>(this TCollection parameter,
5353
return parameter;
5454
}
5555

56-
56+
/// <summary>
57+
/// Checks if the specified collection is null or empty.
58+
/// </summary>
59+
/// <param name="collection">The collection to be checked.</param>
60+
/// <returns>True if the collection is null or empty, else false.</returns>
61+
#if (NETSTANDARD2_0 || NETSTANDARD1_0 || NET45 || SILVERLIGHT)
62+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
63+
#endif
64+
[ContractAnnotation("collection:null => false")]
65+
public static bool IsNullOrEmpty<TCollection>(this TCollection collection) where TCollection : class, IEnumerable =>
66+
collection == null || collection.Count() == 0;
5767

5868
/// <summary>
5969
/// Ensures that the collection is not null or empty, or otherwise throws an <see cref="EmptyCollectionException"/>.
@@ -201,6 +211,27 @@ public static TCollection MustNotContain<TCollection, TItem>(this TCollection pa
201211
return parameter;
202212
}
203213

214+
/// <summary>
215+
/// Checks if the given <paramref name="item" /> is one of the specified <paramref name="items" />.
216+
/// </summary>
217+
/// <param name="item">The item to be checked.</param>
218+
/// <param name="items">The collection that might contain the <paramref name="item" />.</param>
219+
/// <exception cref="ArgumentNullException">Thrown when <paramref name="items" /> is null.</exception>
220+
#if (NETSTANDARD2_0 || NETSTANDARD1_0 || NET45 || SILVERLIGHT)
221+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
222+
#endif
223+
[ContractAnnotation("items:null => halt")]
224+
public static bool IsOneOf<TItem, TCollection>(this TItem item, TCollection items) where TCollection : class, IEnumerable<TItem>
225+
{
226+
if (items is ICollection<TItem> collection)
227+
return collection.Contains(item);
228+
229+
if (items is string @string && item is char character)
230+
return @string.IndexOf(character) != -1;
231+
232+
return items.MustNotBeNull(nameof(items)).Contains(item);
233+
}
234+
204235
/// <summary>
205236
/// Ensures that the value is one of the specified items, or otherwise throws a <see cref="ValueIsNotOneOfException"/>.
206237
/// </summary>
@@ -216,14 +247,7 @@ public static TCollection MustNotContain<TCollection, TItem>(this TCollection pa
216247
[ContractAnnotation("items:null => halt")]
217248
public static TItem MustBeOneOf<TItem, TCollection>(this TItem parameter, TCollection items, string parameterName = null, string message = null) where TCollection : class, IEnumerable<TItem>
218249
{
219-
if (items is ICollection<TItem> collection)
220-
{
221-
if (!collection.Contains(parameter))
222-
Throw.ValueNotOneOf(parameter, items, parameterName, message);
223-
return parameter;
224-
}
225-
226-
if (!items.MustNotBeNull(nameof(items)).Contains(parameter))
250+
if (!parameter.IsOneOf(items))
227251
Throw.ValueNotOneOf(parameter, items, parameterName, message);
228252
return parameter;
229253
}
@@ -242,14 +266,7 @@ public static TItem MustBeOneOf<TItem, TCollection>(this TItem parameter, TColle
242266
[ContractAnnotation("items:null => halt")]
243267
public static TItem MustBeOneOf<TItem, TCollection>(this TItem parameter, TCollection items, Func<TItem, TCollection, Exception> exceptionFactory) where TCollection : class, IEnumerable<TItem>
244268
{
245-
if (items is ICollection<TItem> collection)
246-
{
247-
if (!collection.Contains(parameter))
248-
Throw.CustomException(exceptionFactory, parameter, items);
249-
return parameter;
250-
}
251-
252-
if (!items.MustNotBeNull(nameof(items)).Contains(parameter))
269+
if (!parameter.IsOneOf(items))
253270
Throw.CustomException(exceptionFactory, parameter, items);
254271
return parameter;
255272
}
@@ -269,14 +286,7 @@ public static TItem MustBeOneOf<TItem, TCollection>(this TItem parameter, TColle
269286
[ContractAnnotation("items:null => halt")]
270287
public static TItem MustNotBeOneOf<TItem, TCollection>(this TItem parameter, TCollection items, string parameterName = null, string message = null) where TCollection : class, IEnumerable<TItem>
271288
{
272-
if (items is ICollection<TItem> collection)
273-
{
274-
if (collection.Contains(parameter))
275-
Throw.ValueIsOneOf(parameter, items, parameterName, message);
276-
return parameter;
277-
}
278-
279-
if (items.MustNotBeNull(nameof(items)).Contains(parameter))
289+
if (parameter.IsOneOf(items))
280290
Throw.ValueIsOneOf(parameter, items, parameterName, message);
281291
return parameter;
282292
}
@@ -295,14 +305,7 @@ public static TItem MustNotBeOneOf<TItem, TCollection>(this TItem parameter, TCo
295305
[ContractAnnotation("items:null => halt")]
296306
public static TItem MustNotBeOneOf<TItem, TCollection>(this TItem parameter, TCollection items, Func<TItem, TCollection, Exception> exceptionFactory) where TCollection : class, IEnumerable<TItem>
297307
{
298-
if (items is ICollection<TItem> collection)
299-
{
300-
if (collection.Contains(parameter))
301-
Throw.CustomException(exceptionFactory, parameter, items);
302-
return parameter;
303-
}
304-
305-
if (items.MustNotBeNull(nameof(items)).Contains(parameter))
308+
if (parameter.IsOneOf(items))
306309
Throw.CustomException(exceptionFactory, parameter, items);
307310
return parameter;
308311
}

0 commit comments

Comments
 (0)