Skip to content

Commit

Permalink
Fixed wrong message with NullOrEmpty or NullOrWhiteSpace checks (#175)
Browse files Browse the repository at this point in the history
* NullOrEmpty Clause: Pass the custom message to the Null check.

This ensures that both Null and Empty checks throw the exception with
the correct custom message set by the API consumer.

* NullOrEmpty Clause: Pass the custom message to the Null check.

    This ensures that both Null and Empty checks throw the exception with
    the correct custom message set by the API consumer.
  • Loading branch information
Miljankg authored Jan 7, 2022
1 parent cc16c24 commit 54b8c2e
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 6 deletions.
8 changes: 4 additions & 4 deletions src/GuardClauses/GuardClauseExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public static T Null<T>([JetBrainsNotNull] this IGuardClause guardClause, [NotNu
/// <exception cref="ArgumentException"></exception>
public static string NullOrEmpty([JetBrainsNotNull] this IGuardClause guardClause, [NotNull, JetBrainsNotNull][ValidatedNotNull] string? input, [JetBrainsNotNull][JetBrainsInvokerParameterName] string parameterName, string? message = null)
{
Guard.Against.Null(input, parameterName);
Guard.Against.Null(input, parameterName, message);
if (input == string.Empty)
{
throw new ArgumentException(message ?? $"Required input {parameterName} was empty.", parameterName);
Expand All @@ -77,7 +77,7 @@ public static string NullOrEmpty([JetBrainsNotNull] this IGuardClause guardClaus
/// <exception cref="ArgumentException"></exception>
public static Guid NullOrEmpty([JetBrainsNotNull] this IGuardClause guardClause, [NotNull, JetBrainsNotNull][ValidatedNotNull] Guid? input, [JetBrainsNotNull][JetBrainsInvokerParameterName] string parameterName, string? message = null)
{
Guard.Against.Null(input, parameterName);
Guard.Against.Null(input, parameterName, message);
if (input == Guid.Empty)
{
throw new ArgumentException(message ?? $"Required input {parameterName} was empty.", parameterName);
Expand All @@ -99,7 +99,7 @@ public static Guid NullOrEmpty([JetBrainsNotNull] this IGuardClause guardClause,
/// <exception cref="ArgumentException"></exception>
public static IEnumerable<T> NullOrEmpty<T>([JetBrainsNotNull] this IGuardClause guardClause, [NotNull, JetBrainsNotNull][ValidatedNotNull] IEnumerable<T>? input, [JetBrainsNotNull][JetBrainsInvokerParameterName] string parameterName, string? message = null)
{
Guard.Against.Null(input, parameterName);
Guard.Against.Null(input, parameterName, message);
if (!input.Any())
{
throw new ArgumentException(message ?? $"Required input {parameterName} was empty.", parameterName);
Expand All @@ -121,7 +121,7 @@ public static IEnumerable<T> NullOrEmpty<T>([JetBrainsNotNull] this IGuardClause
/// <exception cref="ArgumentException"></exception>
public static string NullOrWhiteSpace([JetBrainsNotNull] this IGuardClause guardClause, [NotNull, JetBrainsNotNull][ValidatedNotNull] string? input, [JetBrainsNotNull][JetBrainsInvokerParameterName] string parameterName, string? message = null)
{
Guard.Against.NullOrEmpty(input, parameterName);
Guard.Against.NullOrEmpty(input, parameterName, message);
if (String.IsNullOrWhiteSpace(input))
{
throw new ArgumentException(message ?? $"Required input {parameterName} was empty.", parameterName);
Expand Down
2 changes: 1 addition & 1 deletion test/GuardClauses.UnitTests/GuardAgainstNullOrEmpty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public void ErrorMessageMatchesExpectedWhenInputIsEmpty(string customMessage, st

[Theory]
[InlineData(null, "Value cannot be null. (Parameter 'parameterName')")]
[InlineData("Value must be correct", "Value cannot be null. (Parameter 'parameterName')")]
[InlineData("Value must be correct", "Value must be correct (Parameter 'parameterName')")]
public void ErrorMessageMatchesExpectedWhenInputIsNull(string customMessage, string expectedMessage)
{
string? nullString = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public void ErrorMessageMatchesExpectedWhenInputIsWhiteSpace(string customMessag

[Theory]
[InlineData(null, "Value cannot be null. (Parameter 'parameterName')")]
[InlineData("Value is null", "Value cannot be null. (Parameter 'parameterName')")]
[InlineData("Value is null", "Value is null (Parameter 'parameterName')")]
public void ErrorMessageMatchesExpectedWhenInputIsNull(string customMessage, string expectedMessage)
{
string? nullString = null;
Expand Down

0 comments on commit 54b8c2e

Please sign in to comment.