Skip to content

Commit

Permalink
feat: Added ThrowIfNullOrEmpty(IEnumerable<T>) (#133)
Browse files Browse the repository at this point in the history
* feat: Added `ThrowIfNullOrEmpty(IEnumerable`1)`

* docs: Added XML Summary to ThrowIfNullOrEmpty method
  • Loading branch information
samtrion authored Apr 26, 2024
1 parent 1426b4e commit bbc1151
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 6 deletions.
4 changes: 1 addition & 3 deletions src/NetEvolve.Arguments/Argument_ThrowArgumentException.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#if !NET8_0_OR_GREATER
namespace NetEvolve.Arguments;
namespace NetEvolve.Arguments;

using System;
using System.Diagnostics;
Expand All @@ -17,4 +16,3 @@ private static void ThrowArgumentException(string? paramName, string? message =
throw new ArgumentException(message, paramName);
}
}
#endif
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#if !NET8_0_OR_GREATER
namespace NetEvolve.Arguments;
namespace NetEvolve.Arguments;

using System;
using System.Diagnostics;
Expand All @@ -17,4 +16,3 @@ private static void ThrowArgumentNullException(string? paramName, string? messag
throw new ArgumentNullException(paramName, message);
}
}
#endif
26 changes: 26 additions & 0 deletions src/NetEvolve.Arguments/Argument_ThrowIfNullOrEmpty.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
namespace NetEvolve.Arguments;

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Runtime.CompilerServices;

public static partial class Argument
Expand Down Expand Up @@ -38,4 +40,28 @@ public static void ThrowIfNullOrEmpty(
}
#endif
}

/// <summary>Throws an exception if <paramref name="argument"/> is null or empty.</summary>
/// <param name="argument">The string argument to validate as non-null and non-empty.</param>
/// <param name="paramName">The name of the parameter with which <paramref name="argument"/> corresponds.</param>
/// <exception cref="ArgumentNullException"><paramref name="argument"/> is null.</exception>
/// <exception cref="ArgumentException"><paramref name="argument"/> is empty.</exception>
[DebuggerStepThrough]
[StackTraceHidden]
[MethodImpl(MethodImplOptions.NoInlining)]
public static void ThrowIfNullOrEmpty<T>(
[NotNull] IEnumerable<T> argument,
[CallerArgumentExpression(nameof(argument))] string? paramName = null
)
{
if (argument is null)
{
ThrowArgumentNullException(paramName);
}

if (argument.TryGetNonEnumeratedCount(out var count) && count == 0)
{
ThrowArgumentException(paramName);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace NetEvolve.Arguments.Tests.Unit;

using System;
using System.Collections.Generic;
using Xunit;

public sealed partial class ArgumentTests
Expand Down Expand Up @@ -43,4 +44,75 @@ public void ThrowIfNullOrEmpty_WhenArgumentIsNotEmpty_ReturnsArgument()
// Assert
Assert.True(true);
}

[Fact]
public void ThrowIfNullOrEmpty_WhenIEnumerableNull_ThrowsArgumentNullException()
{
// Arrange
IEnumerable<string>? argument = null;

// Act
void Act() => Argument.ThrowIfNullOrEmpty(argument);

// Assert
_ = Assert.Throws<ArgumentNullException>(nameof(argument), Act);
}

[Theory]
[MemberData(nameof(ThrowIfNullOrEmptyEnumerableData))]
public void ThrowIfNullOrEmpty_WhenIEnumerableIsEmpty_ThrowsArgumentException(
IEnumerable<string> argument
)
{
// Act
void Act() => Argument.ThrowIfNullOrEmpty(argument);

// Assert
_ = Assert.Throws<ArgumentException>(nameof(argument), Act);
}

[Theory]
[MemberData(nameof(ThrowIfNullOrEmptyEnumerableWithData))]
public void ThrowIfNullOrEmpty_WhenIEnumerableIsNotEmpty_ReturnsArgument(
IEnumerable<string> argument
)
{
// Act
Argument.ThrowIfNullOrEmpty(argument);

// Assert
Assert.True(true);
}

public static TheoryData<IEnumerable<string>> ThrowIfNullOrEmptyEnumerableData
{
get
{
var data = new TheoryData<IEnumerable<string>>
{
Array.Empty<string>(),
new List<string>(),
new HashSet<string>()
};

return data;
}
}

public static TheoryData<IEnumerable<string>> ThrowIfNullOrEmptyEnumerableWithData
{
get
{
var data = new TheoryData<IEnumerable<string>>
{
#pragma warning disable CA1861 // Avoid constant arrays as arguments
new[] { "argument" },
#pragma warning restore CA1861 // Avoid constant arrays as arguments
new List<string> { "argument" },
new HashSet<string> { "argument" }
};

return data;
}
}
}

0 comments on commit bbc1151

Please sign in to comment.