From 92c6af52042bbdd028aa1215be1e5b97591f9e0c Mon Sep 17 00:00:00 2001 From: Gabriele Messina Date: Sat, 18 Mar 2023 15:07:38 +0100 Subject: [PATCH 1/5] Fix Guard IsWhiteSpace and IsNotWhiteSpace Methods --- src/CommunityToolkit.Diagnostics/Guard.String.cs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/CommunityToolkit.Diagnostics/Guard.String.cs b/src/CommunityToolkit.Diagnostics/Guard.String.cs index 8b11eb6e3..33ebc75ea 100644 --- a/src/CommunityToolkit.Diagnostics/Guard.String.cs +++ b/src/CommunityToolkit.Diagnostics/Guard.String.cs @@ -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 exclusively 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 (text != null && 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 exclusively 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 (text == null || !string.IsNullOrWhiteSpace(text)) { return; } From 97fc6c1bf1c29d9e53812749a16ad9adbdcd5ea5 Mon Sep 17 00:00:00 2001 From: Gabriele Messina Date: Sat, 18 Mar 2023 15:25:05 +0100 Subject: [PATCH 2/5] Add tests for Guard IsWhiteSpace and IsNotWhiteSpace methods --- .../Test_Guard.cs | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/tests/CommunityToolkit.Diagnostics.UnitTests/Test_Guard.cs b/tests/CommunityToolkit.Diagnostics.UnitTests/Test_Guard.cs index 482df6fe8..1fcb0927c 100644 --- a/tests/CommunityToolkit.Diagnostics.UnitTests/Test_Guard.cs +++ b/tests/CommunityToolkit.Diagnostics.UnitTests/Test_Guard.cs @@ -224,6 +224,48 @@ 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(string.Empty, 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_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)); + } + + [TestMethod] + [ExpectedException(typeof(ArgumentException))] + public void Test_Guard_IsNotWhiteSpace_WhiteSpace() + { + Guard.IsNotWhiteSpace(" ", nameof(Test_Guard_IsNotWhiteSpace_WhiteSpace)); + } + + [TestMethod] + [ExpectedException(typeof(ArgumentException))] + public void Test_Guard_IsNotWhiteSpace_Empty() + { + Guard.IsNotWhiteSpace(string.Empty, nameof(Test_Guard_IsNotWhiteSpace_Empty)); + } [TestMethod] public void Test_Guard_IsEqualTo_Ok() From 86e66280ed8e5d77b1c778a443b7bdbb86bde142 Mon Sep 17 00:00:00 2001 From: Gabriele Messina Date: Mon, 20 Mar 2023 23:37:40 +0100 Subject: [PATCH 3/5] Fix possible null reference argument ThrowArgumentExceptionForIsWhiteSpace and ThrowArgumentExceptionForIsNotWhiteSpace --- .../Internals/Guard.String.ThrowHelper.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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); } From 4f61a8aaff40ac6835dbf76d7ae69c645e8adc10 Mon Sep 17 00:00:00 2001 From: Gabriele Messina Date: Sun, 16 Apr 2023 17:21:01 +0200 Subject: [PATCH 4/5] Fix Guard.Is(Not)WhiteSpace checking for string.empty, updated tests --- .../Guard.String.cs | 8 ++++---- .../Test_Guard.cs | 18 ++++++++++-------- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/src/CommunityToolkit.Diagnostics/Guard.String.cs b/src/CommunityToolkit.Diagnostics/Guard.String.cs index 33ebc75ea..df234552d 100644 --- a/src/CommunityToolkit.Diagnostics/Guard.String.cs +++ b/src/CommunityToolkit.Diagnostics/Guard.String.cs @@ -118,7 +118,7 @@ public static void IsNotEmpty(string text, [CallerArgumentExpression(nameof(text } /// - /// Asserts that the input instance must consists exclusively of white-space characters. + /// 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. @@ -126,7 +126,7 @@ public static void IsNotEmpty(string text, [CallerArgumentExpression(nameof(text [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void IsWhiteSpace(string text, [CallerArgumentExpression(nameof(text))] string name = "") { - if (text != null && string.IsNullOrWhiteSpace(text)) + if (!string.IsNullOrEmpty(text) && string.IsNullOrWhiteSpace(text)) { return; } @@ -135,7 +135,7 @@ public static void IsWhiteSpace(string text, [CallerArgumentExpression(nameof(te } /// - /// Asserts that the input instance must not consists exclusively of white-space characters. + /// 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. @@ -143,7 +143,7 @@ public static void IsWhiteSpace(string text, [CallerArgumentExpression(nameof(te [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void IsNotWhiteSpace(string text, [CallerArgumentExpression(nameof(text))] string name = "") { - if (text == null || !string.IsNullOrWhiteSpace(text)) + if (string.IsNullOrEmpty(text) || !string.IsNullOrWhiteSpace(text)) { return; } diff --git a/tests/CommunityToolkit.Diagnostics.UnitTests/Test_Guard.cs b/tests/CommunityToolkit.Diagnostics.UnitTests/Test_Guard.cs index 1fcb0927c..2af543f65 100644 --- a/tests/CommunityToolkit.Diagnostics.UnitTests/Test_Guard.cs +++ b/tests/CommunityToolkit.Diagnostics.UnitTests/Test_Guard.cs @@ -229,7 +229,7 @@ public void Test_Guard_IsNotNullOrWhiteSpace_Empty() public void Test_Guard_IsWhiteSpace_Ok() { Guard.IsWhiteSpace(" ", nameof(Test_Guard_IsWhiteSpace_Ok)); - Guard.IsWhiteSpace(string.Empty, nameof(Test_Guard_IsWhiteSpace_Ok)); + Guard.IsWhiteSpace("\t", nameof(Test_Guard_IsWhiteSpace_Ok)); } [TestMethod] @@ -239,6 +239,13 @@ 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() @@ -251,6 +258,7 @@ 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] @@ -258,13 +266,7 @@ public void Test_Guard_IsNotWhiteSpace_Ok() public void Test_Guard_IsNotWhiteSpace_WhiteSpace() { Guard.IsNotWhiteSpace(" ", nameof(Test_Guard_IsNotWhiteSpace_WhiteSpace)); - } - - [TestMethod] - [ExpectedException(typeof(ArgumentException))] - public void Test_Guard_IsNotWhiteSpace_Empty() - { - Guard.IsNotWhiteSpace(string.Empty, nameof(Test_Guard_IsNotWhiteSpace_Empty)); + Guard.IsNotWhiteSpace("\t", nameof(Test_Guard_IsNotWhiteSpace_WhiteSpace)); } [TestMethod] From aa5bc48d3827910a14a4340f17b460dd52f7e4c9 Mon Sep 17 00:00:00 2001 From: Gabriele Messina Date: Sun, 16 Apr 2023 17:22:49 +0200 Subject: [PATCH 5/5] Fix Guard.Is(Not)NullOrWhiteSpace documentation --- src/CommunityToolkit.Diagnostics/Guard.String.cs | 8 ++++---- .../CommunityToolkit.Diagnostics.UnitTests/Test_Guard.cs | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/CommunityToolkit.Diagnostics/Guard.String.cs b/src/CommunityToolkit.Diagnostics/Guard.String.cs index df234552d..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 = "") { diff --git a/tests/CommunityToolkit.Diagnostics.UnitTests/Test_Guard.cs b/tests/CommunityToolkit.Diagnostics.UnitTests/Test_Guard.cs index 2af543f65..0b225aba0 100644 --- a/tests/CommunityToolkit.Diagnostics.UnitTests/Test_Guard.cs +++ b/tests/CommunityToolkit.Diagnostics.UnitTests/Test_Guard.cs @@ -238,7 +238,7 @@ public void Test_Guard_IsWhiteSpace_Null() { Guard.IsWhiteSpace(null, nameof(Test_Guard_IsWhiteSpace_Null)); } - + [TestMethod] [ExpectedException(typeof(ArgumentException))] public void Test_Guard_IsWhiteSpace_Empty()