Skip to content

Commit

Permalink
Fix wrong type returned by NotNullOrEmpty() and HasNoNulls()
Browse files Browse the repository at this point in the history
  • Loading branch information
Philippe Lecaillon committed Feb 16, 2022
1 parent 65ce462 commit ed3f78a
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 65 deletions.
34 changes: 10 additions & 24 deletions src/Preconditions/Check.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
namespace Preconditions
{
using System;
using System.Collections.Generic;
using System.Collections;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;

/// <summary>
Expand Down Expand Up @@ -74,33 +73,16 @@ public static string NotNullOrEmpty([NotNull] string? text, [CallerArgumentExpre
return text;
}

/// <summary>
/// Ensures that a collection contains at least one element.
/// </summary>
/// <exception cref="ArgumentNullException"/>
/// <exception cref="ArgumentException"/>
public static ICollection<T> NotNullOrEmpty<T>([NotNull] ICollection<T>? collection, [CallerArgumentExpression("collection")] string? paramName = null)
{
NotNull(collection, paramName);

if (collection.Count == 0)
{
ThrowArgumentException(CollectionArgumentIsEmpty, paramName);
}

return collection;
}

/// <summary>
/// Ensures that an enumerable contains at least one element.
/// </summary>
/// <exception cref="ArgumentNullException"/>
/// <exception cref="ArgumentException"/>
public static IEnumerable<T> NotNullOrEmpty<T>([NotNull] IEnumerable<T>? enumerable, [CallerArgumentExpression("enumerable")] string? paramName = null)
public static T NotNullOrEmpty<T>([NotNull] T? enumerable, [CallerArgumentExpression("enumerable")] string? paramName = null) where T : IEnumerable
{
NotNull(enumerable, paramName);

if (!enumerable.Any())
if (!enumerable.GetEnumerator().MoveNext())
{
ThrowArgumentException(CollectionArgumentIsEmpty, paramName);
}
Expand All @@ -113,13 +95,17 @@ public static IEnumerable<T> NotNullOrEmpty<T>([NotNull] IEnumerable<T>? enumera
/// </summary>
/// <exception cref="ArgumentNullException"/>
/// <exception cref="ArgumentException"/>
public static IEnumerable<T> HasNoNulls<T>([NotNull] IEnumerable<T>? enumerable, [CallerArgumentExpression("enumerable")] string? paramName = null)
public static T HasNoNulls<T>([NotNull] T? enumerable, [CallerArgumentExpression("enumerable")] string? paramName = null) where T : IEnumerable
{
NotNull(enumerable, paramName);

if (enumerable.Any(e => e is null))

foreach (var item in enumerable)
{
ThrowArgumentException(CollectionArgumentHasNullElement, paramName);
if (item is null)
{
ThrowArgumentException(CollectionArgumentHasNullElement, paramName);
}
}

return enumerable;
Expand Down
4 changes: 2 additions & 2 deletions src/Preconditions/Preconditions.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<PackageId>Preconditions.NET</PackageId>
<Authors>Philippe Lécaillon</Authors>
<Version>2.0.1</Version>
<Version>2.0.2</Version>
<Copyright>Copyright © P.Lécaillon 2022</Copyright>
<Description>Preconditions provide convenience static methods that help to check that a method or a constructor is invoked with proper parameter or not. In other words it checks the pre-conditions.</Description>
<PackageIcon>logo128.png</PackageIcon>
Expand All @@ -16,7 +16,7 @@
<RepositoryType>git</RepositoryType>
<PackageTags>preconditions;guard;check;argument-checks</PackageTags>
<PackageReleaseNotes>## Bug fix
- Add missing NotNullAttribute</PackageReleaseNotes>
- NotNullOrEmpty(IEnumerable) and HasNoNulls(IEnumerable) return the same type as the one passed in parameter</PackageReleaseNotes>
</PropertyGroup>

<ItemGroup>
Expand Down
42 changes: 3 additions & 39 deletions tests/Preconditions.Tests/CheckTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,43 +100,7 @@ public void Text_Is_Considered_Not_Null_After_Calling_NotNullOrEmpty()

#endregion

#region Check.NotNullOrEmpty : ICollection

[Fact]
public void NotNullOrEmpty_When_Collection_Is_Null_Throws_ArgumentNullException()
{
ICollection<int>? collection = null;

Assert.Throws<ArgumentNullException>(nameof(collection), () => Check.NotNullOrEmpty(collection));
}

[Fact]
public void NotNullOrEmpty_When_Collection_Has_No_Element_Throws_ArgumentException()
{
ICollection<int> collection = new List<int>();

Assert.Throws<ArgumentException>(nameof(collection), () => Check.NotNullOrEmpty(collection));
}

[Fact]
public void NotNullOrEmpty_When_Collection_Is_Not_Empty_Returns_Collection()
{
ICollection<int> collection = new List<int> { 1, 2, 3 };

Assert.Same(collection, Check.NotNullOrEmpty(collection));
}

[Fact]
public void Collection_Is_Considered_Not_Null_After_Calling_NotNullOrEmpty()
{
ICollection<int>? collection = new List<int> { 1 };
Check.NotNullOrEmpty(collection);
collection.ToString();
}

#endregion

#region Check.NotNullOrEmpty : IEnumerable
#region Check.NotNullOrEmpty

[Fact]
public void NotNullOrEmpty_When_Enumerable_Is_Null_Throws_ArgumentNullException()
Expand All @@ -149,15 +113,15 @@ public void NotNullOrEmpty_When_Enumerable_Is_Null_Throws_ArgumentNullException(
[Fact]
public void NotNullOrEmpty_When_Enumerable_Has_No_Element_Throws_ArgumentException()
{
IEnumerable<int> enumerable = new List<int>();
var enumerable = new List<int>();

Assert.Throws<ArgumentException>(nameof(enumerable), () => Check.NotNullOrEmpty(enumerable));
}

[Fact]
public void NotNullOrEmpty_When_Enumerable_Is_Not_Empty_Returns_Enumerable()
{
IEnumerable<int> enumerable = new List<int> { 1, 2, 3 };
var enumerable = new List<int> { 1, 2, 3 };

Assert.Same(enumerable, Check.NotNullOrEmpty(enumerable));
}
Expand Down

0 comments on commit ed3f78a

Please sign in to comment.