Skip to content

Commit

Permalink
Merge pull request #18 from BenjaminAbt/feature/add-hasvalue
Browse files Browse the repository at this point in the history
add HasValue
  • Loading branch information
BenjaminAbt committed Jun 1, 2024
2 parents 58a61bb + 04b3c4e commit c73373a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 9 deletions.
39 changes: 30 additions & 9 deletions src/StrongOf/Strong.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,26 +30,47 @@ public static bool IsNotNull<TStrong>(TStrong? strong)
}

/// <summary>
/// Checks if the given strong string is null or empty.
/// Determines whether the specified <typeparamref name="TStrong"/> instance is <c>null</c> or empty.
/// </summary>
/// <typeparam name="TStrong">The type of the strong string.</typeparam>
/// <param name="strong">The strong string to check.</param>
/// <returns>true if the strong string is null or empty; otherwise, false.</returns>
/// <typeparam name="TStrong">The type of the strong string. Must inherit from <see cref="StrongString{TStrong}"/>.</typeparam>
/// <param name="strong">The <typeparamref name="TStrong"/> instance to check.</param>
/// <returns>
/// <c>true</c> if the <paramref name="strong"/> instance is <c>null</c> or empty; otherwise, <c>false</c>.
/// </returns>
public static bool IsNullOrEmpty<TStrong>(TStrong? strong)
where TStrong : StrongString<TStrong>
{
return strong is null || strong.IsEmpty();
}


/// <summary>
/// Determines whether the specified <typeparamref name="TStrong"/> instance has a value.
/// </summary>
/// <typeparam name="TStrong">The type of the strong string. Must inherit from <see cref="StrongString{TStrong}"/>.</typeparam>
/// <param name="strong">The <typeparamref name="TStrong"/> instance to check.</param>
/// <returns>
/// <c>true</c> if the <paramref name="strong"/> instance is not <c>null</c> and is not empty; otherwise, <c>false</c>.
/// </returns>
public static bool HasValue<TStrong>(TStrong? strong)
where TStrong : StrongString<TStrong>
{
return IsNotNullOrEmpty(strong);
}


/// <summary>
/// Checks if the given strong string is not null or empty.
/// Determines whether the specified <typeparamref name="TStrong"/> instance is not <c>null</c> and not empty.
/// </summary>
/// <typeparam name="TStrong">The type of the strong string.</typeparam>
/// <param name="strong">The strong string to check.</param>
/// <returns>true if the strong string is not null or empty; otherwise, false.</returns>
/// <typeparam name="TStrong">The type of the strong string. Must inherit from <see cref="StrongString{TStrong}"/>.</typeparam>
/// <param name="strong">The <typeparamref name="TStrong"/> instance to check.</param>
/// <returns>
/// <c>true</c> if the <paramref name="strong"/> instance is not <c>null</c> and not empty; otherwise, <c>false</c>.
/// </returns>
public static bool IsNotNullOrEmpty<TStrong>(TStrong? strong)
where TStrong : StrongString<TStrong>
where TStrong : StrongString<TStrong>
{
return strong is not null && strong.IsEmpty() is false;
}

}
5 changes: 5 additions & 0 deletions tests/StrongOf.UnitTests/StrongTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,33 +46,38 @@ public void IsNullOrEmpty_ShouldReturnTrue_WhenStrongStringIsEmpty()
{
TestStringOf strongString = new("");
Assert.True(Strong.IsNullOrEmpty(strongString));
Assert.False(Strong.HasValue(strongString));
}

[Fact]
public void IsNullOrEmpty_ShouldReturnFalse_WhenStrongStringIsNotNullOrEmpty()
{
TestStringOf strongString = new("test");
Assert.False(Strong.IsNullOrEmpty(strongString));
Assert.True(Strong.HasValue(strongString));
}

[Fact]
public void IsNotNullOrEmpty_ShouldReturnTrue_WhenStrongStringIsNotNullOrEmpty()
{
TestStringOf strongString = new("test");
Assert.True(Strong.IsNotNullOrEmpty(strongString));
Assert.True(Strong.HasValue(strongString));
}

[Fact]
public void IsNotNullOrEmpty_ShouldReturnFalse_WhenStrongStringIsNull()
{
TestStringOf? strongString = null;
Assert.False(Strong.IsNotNullOrEmpty(strongString));
Assert.False(Strong.HasValue(strongString));
}

[Fact]
public void IsNotNullOrEmpty_ShouldReturnFalse_WhenStrongStringIsEmpty()
{
TestStringOf strongString = new("");
Assert.False(Strong.IsNotNullOrEmpty(strongString));
Assert.False(Strong.HasValue(strongString));
}
}

0 comments on commit c73373a

Please sign in to comment.