From 04b3c4e3704808d33e6af1fb4128438ca7f2e6e6 Mon Sep 17 00:00:00 2001 From: BEN ABT Date: Sat, 1 Jun 2024 18:15:26 +0200 Subject: [PATCH] add HasValue --- src/StrongOf/Strong.cs | 39 +++++++++++++++++++------ tests/StrongOf.UnitTests/StrongTests.cs | 5 ++++ 2 files changed, 35 insertions(+), 9 deletions(-) diff --git a/src/StrongOf/Strong.cs b/src/StrongOf/Strong.cs index b80af68..f214264 100644 --- a/src/StrongOf/Strong.cs +++ b/src/StrongOf/Strong.cs @@ -30,26 +30,47 @@ public static bool IsNotNull(TStrong? strong) } /// - /// Checks if the given strong string is null or empty. + /// Determines whether the specified instance is null or empty. /// - /// The type of the strong string. - /// The strong string to check. - /// true if the strong string is null or empty; otherwise, false. + /// The type of the strong string. Must inherit from . + /// The instance to check. + /// + /// true if the instance is null or empty; otherwise, false. + /// public static bool IsNullOrEmpty(TStrong? strong) where TStrong : StrongString { return strong is null || strong.IsEmpty(); } + + /// + /// Determines whether the specified instance has a value. + /// + /// The type of the strong string. Must inherit from . + /// The instance to check. + /// + /// true if the instance is not null and is not empty; otherwise, false. + /// + public static bool HasValue(TStrong? strong) + where TStrong : StrongString + { + return IsNotNullOrEmpty(strong); + } + + /// - /// Checks if the given strong string is not null or empty. + /// Determines whether the specified instance is not null and not empty. /// - /// The type of the strong string. - /// The strong string to check. - /// true if the strong string is not null or empty; otherwise, false. + /// The type of the strong string. Must inherit from . + /// The instance to check. + /// + /// true if the instance is not null and not empty; otherwise, false. + /// public static bool IsNotNullOrEmpty(TStrong? strong) - where TStrong : StrongString + where TStrong : StrongString { return strong is not null && strong.IsEmpty() is false; } + } diff --git a/tests/StrongOf.UnitTests/StrongTests.cs b/tests/StrongOf.UnitTests/StrongTests.cs index bfd8748..efc752a 100644 --- a/tests/StrongOf.UnitTests/StrongTests.cs +++ b/tests/StrongOf.UnitTests/StrongTests.cs @@ -46,6 +46,7 @@ public void IsNullOrEmpty_ShouldReturnTrue_WhenStrongStringIsEmpty() { TestStringOf strongString = new(""); Assert.True(Strong.IsNullOrEmpty(strongString)); + Assert.False(Strong.HasValue(strongString)); } [Fact] @@ -53,6 +54,7 @@ public void IsNullOrEmpty_ShouldReturnFalse_WhenStrongStringIsNotNullOrEmpty() { TestStringOf strongString = new("test"); Assert.False(Strong.IsNullOrEmpty(strongString)); + Assert.True(Strong.HasValue(strongString)); } [Fact] @@ -60,6 +62,7 @@ public void IsNotNullOrEmpty_ShouldReturnTrue_WhenStrongStringIsNotNullOrEmpty() { TestStringOf strongString = new("test"); Assert.True(Strong.IsNotNullOrEmpty(strongString)); + Assert.True(Strong.HasValue(strongString)); } [Fact] @@ -67,6 +70,7 @@ public void IsNotNullOrEmpty_ShouldReturnFalse_WhenStrongStringIsNull() { TestStringOf? strongString = null; Assert.False(Strong.IsNotNullOrEmpty(strongString)); + Assert.False(Strong.HasValue(strongString)); } [Fact] @@ -74,5 +78,6 @@ public void IsNotNullOrEmpty_ShouldReturnFalse_WhenStrongStringIsEmpty() { TestStringOf strongString = new(""); Assert.False(Strong.IsNotNullOrEmpty(strongString)); + Assert.False(Strong.HasValue(strongString)); } }