Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add HasValue #18

Merged
merged 1 commit into from
Jun 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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));
}
}