diff --git a/src/CommunityToolkit.Diagnostics/Guard.String.cs b/src/CommunityToolkit.Diagnostics/Guard.String.cs index 8b11eb6e3..e0b9419de 100644 --- a/src/CommunityToolkit.Diagnostics/Guard.String.cs +++ b/src/CommunityToolkit.Diagnostics/Guard.String.cs @@ -49,11 +49,11 @@ public static void IsNotNullOrEmpty([NotNull] string? text, [CallerArgumentExpre } /// - /// Asserts that the input instance must be or whitespace. + /// Asserts that the input instance must be , empty or consists only of white-space characters. /// /// The input instance to test. /// The name of the input parameter being tested. - /// Thrown if is neither nor whitespace. + /// Thrown if is neither , nor empty, nor whitespace. [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void IsNullOrWhiteSpace(string? text, [CallerArgumentExpression(nameof(text))] string name = "") { @@ -66,12 +66,12 @@ public static void IsNullOrWhiteSpace(string? text, [CallerArgumentExpression(na } /// - /// Asserts that the input instance must not be or whitespace. + /// Asserts that the input instance must not be , empty or consists only of white-space characters. /// /// The input instance to test. /// The name of the input parameter being tested. /// Thrown if is . - /// Thrown if is whitespace. + /// Thrown if is empty or whitespace. [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void IsNotNullOrWhiteSpace([NotNull] string? text, [CallerArgumentExpression(nameof(text))] string name = "") { @@ -118,15 +118,15 @@ public static void IsNotEmpty(string text, [CallerArgumentExpression(nameof(text } /// - /// Asserts that the input instance must be whitespace. + /// Asserts that the input instance must consists only of white-space characters. /// /// The input instance to test. /// The name of the input parameter being tested. - /// Thrown if is neither nor whitespace. + /// Thrown if has some not white-space characters. [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void IsWhiteSpace(string text, [CallerArgumentExpression(nameof(text))] string name = "") { - if (string.IsNullOrWhiteSpace(text)) + if (!string.IsNullOrEmpty(text) && string.IsNullOrWhiteSpace(text)) { return; } @@ -135,15 +135,15 @@ public static void IsWhiteSpace(string text, [CallerArgumentExpression(nameof(te } /// - /// Asserts that the input instance must not be or whitespace. + /// Asserts that the input instance must not consists only of white-space characters. /// /// The input instance to test. /// The name of the input parameter being tested. - /// Thrown if is or whitespace. + /// Thrown if has only white-space characters. [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void IsNotWhiteSpace(string text, [CallerArgumentExpression(nameof(text))] string name = "") { - if (!string.IsNullOrWhiteSpace(text)) + if (string.IsNullOrEmpty(text) || !string.IsNullOrWhiteSpace(text)) { return; } diff --git a/src/CommunityToolkit.Diagnostics/Internals/Guard.String.ThrowHelper.cs b/src/CommunityToolkit.Diagnostics/Internals/Guard.String.ThrowHelper.cs index 7c8818069..70d1bb17f 100644 --- a/src/CommunityToolkit.Diagnostics/Internals/Guard.String.ThrowHelper.cs +++ b/src/CommunityToolkit.Diagnostics/Internals/Guard.String.ThrowHelper.cs @@ -94,7 +94,7 @@ public static void ThrowArgumentExceptionForIsNotEmpty(string name) /// Throws an when fails. /// [DoesNotReturn] - public static void ThrowArgumentExceptionForIsWhiteSpace(string text, string name) + public static void ThrowArgumentExceptionForIsWhiteSpace(string? text, string name) { throw new ArgumentException($"Parameter {AssertString(name)} (string) must be whitespace, was {AssertString(text)}.", name); } @@ -103,7 +103,7 @@ public static void ThrowArgumentExceptionForIsWhiteSpace(string text, string nam /// Throws an when fails. /// [DoesNotReturn] - public static void ThrowArgumentExceptionForIsNotWhiteSpace(string text, string name) + public static void ThrowArgumentExceptionForIsNotWhiteSpace(string? text, string name) { throw new ArgumentException($"Parameter {AssertString(name)} (string) must not be whitespace, was {AssertString(text)}.", name); } diff --git a/tests/CommunityToolkit.Diagnostics.UnitTests/Test_Guard.cs b/tests/CommunityToolkit.Diagnostics.UnitTests/Test_Guard.cs index 482df6fe8..0b225aba0 100644 --- a/tests/CommunityToolkit.Diagnostics.UnitTests/Test_Guard.cs +++ b/tests/CommunityToolkit.Diagnostics.UnitTests/Test_Guard.cs @@ -224,6 +224,50 @@ public void Test_Guard_IsNotNullOrWhiteSpace_Empty() { Guard.IsNotNullOrWhiteSpace(" ", nameof(Test_Guard_IsNotNullOrWhiteSpace_Empty)); } + + [TestMethod] + public void Test_Guard_IsWhiteSpace_Ok() + { + Guard.IsWhiteSpace(" ", nameof(Test_Guard_IsWhiteSpace_Ok)); + Guard.IsWhiteSpace("\t", nameof(Test_Guard_IsWhiteSpace_Ok)); + } + + [TestMethod] + [ExpectedException(typeof(ArgumentException))] + public void Test_Guard_IsWhiteSpace_Null() + { + Guard.IsWhiteSpace(null, nameof(Test_Guard_IsWhiteSpace_Null)); + } + + [TestMethod] + [ExpectedException(typeof(ArgumentException))] + public void Test_Guard_IsWhiteSpace_Empty() + { + Guard.IsWhiteSpace(string.Empty, nameof(Test_Guard_IsWhiteSpace_Empty)); + } + + [TestMethod] + [ExpectedException(typeof(ArgumentException))] + public void Test_Guard_IsWhiteSpace_NotWhiteSpace() + { + Guard.IsWhiteSpace("foo", nameof(Test_Guard_IsWhiteSpace_NotWhiteSpace)); + } + + [TestMethod] + public void Test_Guard_IsNotWhiteSpace_Ok() + { + Guard.IsNotWhiteSpace(null, nameof(Test_Guard_IsNotWhiteSpace_Ok)); + Guard.IsNotWhiteSpace("foo", nameof(Test_Guard_IsNotWhiteSpace_Ok)); + Guard.IsNotWhiteSpace(string.Empty, nameof(Test_Guard_IsNotWhiteSpace_Ok)); + } + + [TestMethod] + [ExpectedException(typeof(ArgumentException))] + public void Test_Guard_IsNotWhiteSpace_WhiteSpace() + { + Guard.IsNotWhiteSpace(" ", nameof(Test_Guard_IsNotWhiteSpace_WhiteSpace)); + Guard.IsNotWhiteSpace("\t", nameof(Test_Guard_IsNotWhiteSpace_WhiteSpace)); + } [TestMethod] public void Test_Guard_IsEqualTo_Ok()