diff --git a/src/Preconditions/Check.cs b/src/Preconditions/Check.cs index d8095fd..fbe1035 100644 --- a/src/Preconditions/Check.cs +++ b/src/Preconditions/Check.cs @@ -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; /// @@ -74,33 +73,16 @@ public static string NotNullOrEmpty([NotNull] string? text, [CallerArgumentExpre return text; } - /// - /// Ensures that a collection contains at least one element. - /// - /// - /// - public static ICollection NotNullOrEmpty([NotNull] ICollection? collection, [CallerArgumentExpression("collection")] string? paramName = null) - { - NotNull(collection, paramName); - - if (collection.Count == 0) - { - ThrowArgumentException(CollectionArgumentIsEmpty, paramName); - } - - return collection; - } - /// /// Ensures that an enumerable contains at least one element. /// /// /// - public static IEnumerable NotNullOrEmpty([NotNull] IEnumerable? enumerable, [CallerArgumentExpression("enumerable")] string? paramName = null) + public static T NotNullOrEmpty([NotNull] T? enumerable, [CallerArgumentExpression("enumerable")] string? paramName = null) where T : IEnumerable { NotNull(enumerable, paramName); - if (!enumerable.Any()) + if (!enumerable.GetEnumerator().MoveNext()) { ThrowArgumentException(CollectionArgumentIsEmpty, paramName); } @@ -113,13 +95,17 @@ public static IEnumerable NotNullOrEmpty([NotNull] IEnumerable? enumera /// /// /// - public static IEnumerable HasNoNulls([NotNull] IEnumerable? enumerable, [CallerArgumentExpression("enumerable")] string? paramName = null) + public static T HasNoNulls([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; diff --git a/src/Preconditions/Preconditions.csproj b/src/Preconditions/Preconditions.csproj index 65833c6..d899dd4 100644 --- a/src/Preconditions/Preconditions.csproj +++ b/src/Preconditions/Preconditions.csproj @@ -7,7 +7,7 @@ true Preconditions.NET Philippe Lécaillon - 2.0.1 + 2.0.2 Copyright © P.Lécaillon 2022 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. logo128.png @@ -16,7 +16,7 @@ git preconditions;guard;check;argument-checks ## Bug fix -- Add missing NotNullAttribute +- NotNullOrEmpty(IEnumerable) and HasNoNulls(IEnumerable) return the same type as the one passed in parameter diff --git a/tests/Preconditions.Tests/CheckTest.cs b/tests/Preconditions.Tests/CheckTest.cs index b29b0b9..42050d5 100644 --- a/tests/Preconditions.Tests/CheckTest.cs +++ b/tests/Preconditions.Tests/CheckTest.cs @@ -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? collection = null; - - Assert.Throws(nameof(collection), () => Check.NotNullOrEmpty(collection)); - } - - [Fact] - public void NotNullOrEmpty_When_Collection_Has_No_Element_Throws_ArgumentException() - { - ICollection collection = new List(); - - Assert.Throws(nameof(collection), () => Check.NotNullOrEmpty(collection)); - } - - [Fact] - public void NotNullOrEmpty_When_Collection_Is_Not_Empty_Returns_Collection() - { - ICollection collection = new List { 1, 2, 3 }; - - Assert.Same(collection, Check.NotNullOrEmpty(collection)); - } - - [Fact] - public void Collection_Is_Considered_Not_Null_After_Calling_NotNullOrEmpty() - { - ICollection? collection = new List { 1 }; - Check.NotNullOrEmpty(collection); - collection.ToString(); - } - - #endregion - - #region Check.NotNullOrEmpty : IEnumerable + #region Check.NotNullOrEmpty [Fact] public void NotNullOrEmpty_When_Enumerable_Is_Null_Throws_ArgumentNullException() @@ -149,7 +113,7 @@ public void NotNullOrEmpty_When_Enumerable_Is_Null_Throws_ArgumentNullException( [Fact] public void NotNullOrEmpty_When_Enumerable_Has_No_Element_Throws_ArgumentException() { - IEnumerable enumerable = new List(); + var enumerable = new List(); Assert.Throws(nameof(enumerable), () => Check.NotNullOrEmpty(enumerable)); } @@ -157,7 +121,7 @@ public void NotNullOrEmpty_When_Enumerable_Has_No_Element_Throws_ArgumentExcepti [Fact] public void NotNullOrEmpty_When_Enumerable_Is_Not_Empty_Returns_Enumerable() { - IEnumerable enumerable = new List { 1, 2, 3 }; + var enumerable = new List { 1, 2, 3 }; Assert.Same(enumerable, Check.NotNullOrEmpty(enumerable)); }