Skip to content

Commit 44ad0cc

Browse files
Merge pull request #4327 from Sergio0694/bugfix/incorrect-notnullorempty-exception
Fixed exception type for Guard.IsNotNullOr[Empty|WhiteSpace]
2 parents 7113a60 + f4c75d2 commit 44ad0cc

File tree

3 files changed

+92
-5
lines changed

3 files changed

+92
-5
lines changed

Microsoft.Toolkit.Diagnostics/Guard.String.cs

+4-2
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ public static void IsNullOrEmpty(string? text, string name)
3737
/// </summary>
3838
/// <param name="text">The input <see cref="string"/> instance to test.</param>
3939
/// <param name="name">The name of the input parameter being tested.</param>
40-
/// <exception cref="ArgumentException">Thrown if <paramref name="text"/> is <see langword="null"/> or empty.</exception>
40+
/// <exception cref="ArgumentNullException">Thrown if <paramref name="text"/> is <see langword="null"/>.</exception>
41+
/// <exception cref="ArgumentException">Thrown if <paramref name="text"/> is empty.</exception>
4142
[MethodImpl(MethodImplOptions.AggressiveInlining)]
4243
public static void IsNotNullOrEmpty([NotNull] string? text, string name)
4344
{
@@ -89,7 +90,8 @@ public static void IsNullOrWhitespace(string? text, string name)
8990
/// </summary>
9091
/// <param name="text">The input <see cref="string"/> instance to test.</param>
9192
/// <param name="name">The name of the input parameter being tested.</param>
92-
/// <exception cref="ArgumentException">Thrown if <paramref name="text"/> is <see langword="null"/> or whitespace.</exception>
93+
/// <exception cref="ArgumentNullException">Thrown if <paramref name="text"/> is <see langword="null"/>.</exception>
94+
/// <exception cref="ArgumentException">Thrown if <paramref name="text"/> is whitespace.</exception>
9395
[MethodImpl(MethodImplOptions.AggressiveInlining)]
9496
public static void IsNotNullOrWhiteSpace([NotNull] string? text, string name)
9597
{

Microsoft.Toolkit.Diagnostics/Internals/Guard.String.ThrowHelper.cs

+26-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
using System;
66
using System.Diagnostics.CodeAnalysis;
7+
using System.Runtime.CompilerServices;
78

89
namespace Microsoft.Toolkit.Diagnostics
910
{
@@ -27,12 +28,23 @@ public static void ThrowArgumentExceptionForIsNullOrEmpty(string? text, string n
2728
}
2829

2930
/// <summary>
30-
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.IsNotNullOrEmpty"/> fails.
31+
/// Throws an <see cref="ArgumentNullException"/> or <see cref="ArgumentException"/> when <see cref="Guard.IsNotNullOrEmpty"/> fails.
3132
/// </summary>
3233
[DoesNotReturn]
3334
public static void ThrowArgumentExceptionForIsNotNullOrEmpty(string? text, string name)
3435
{
35-
throw new ArgumentException($"Parameter {AssertString(name)} (string) must not be null or empty, was {(text is null ? "null" : "empty")}", name);
36+
[MethodImpl(MethodImplOptions.NoInlining)]
37+
static Exception GetException(string? text, string name)
38+
{
39+
if (text is null)
40+
{
41+
return new ArgumentNullException(name, $"Parameter {AssertString(name)} (string) must not be null or empty, was null");
42+
}
43+
44+
return new ArgumentException($"Parameter {AssertString(name)} (string) must not be null or empty, was empty", name);
45+
}
46+
47+
throw GetException(text, name);
3648
}
3749

3850
/// <summary>
@@ -50,7 +62,18 @@ public static void ThrowArgumentExceptionForIsNullOrWhiteSpace(string? text, str
5062
[DoesNotReturn]
5163
public static void ThrowArgumentExceptionForIsNotNullOrWhiteSpace(string? text, string name)
5264
{
53-
throw new ArgumentException($"Parameter {AssertString(name)} (string) must not be null or whitespace, was {(text is null ? "null" : "whitespace")}", name);
65+
[MethodImpl(MethodImplOptions.NoInlining)]
66+
static Exception GetException(string? text, string name)
67+
{
68+
if (text is null)
69+
{
70+
return new ArgumentNullException(name, $"Parameter {AssertString(name)} (string) must not be null or whitespace, was null");
71+
}
72+
73+
return new ArgumentException($"Parameter {AssertString(name)} (string) must not be null or whitespace, was whitespace", name);
74+
}
75+
76+
throw GetException(text, name);
5477
}
5578

5679
/// <summary>

UnitTests/UnitTests.Shared/Diagnostics/Test_Guard.cs

+62
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,68 @@ public void Test_Guard_IsAssignableToType_Fail()
121121
Guard.IsAssignableToType(7, typeof(string), nameof(Test_Guard_IsAssignableToType_Fail));
122122
}
123123

124+
[TestCategory("Guard")]
125+
[TestMethod]
126+
public void Test_Guard_IsNullOrEmpty_Ok()
127+
{
128+
Guard.IsNullOrEmpty(null, nameof(Test_Guard_IsNullOrEmpty_Ok));
129+
Guard.IsNullOrEmpty(string.Empty, nameof(Test_Guard_IsNullOrEmpty_Ok));
130+
}
131+
132+
[TestCategory("Guard")]
133+
[TestMethod]
134+
[ExpectedException(typeof(ArgumentException))]
135+
public void Test_Guard_IsNullOrEmpty_Fail()
136+
{
137+
Guard.IsNullOrEmpty("Hello", nameof(Test_Guard_IsNullOrEmpty_Fail));
138+
}
139+
140+
[TestCategory("Guard")]
141+
[TestMethod]
142+
public void Test_Guard_IsNotNullOrEmpty_Ok()
143+
{
144+
Guard.IsNotNullOrEmpty("Hello", nameof(Test_Guard_IsNotNullOrEmpty_Ok));
145+
}
146+
147+
[TestCategory("Guard")]
148+
[TestMethod]
149+
[ExpectedException(typeof(ArgumentNullException))]
150+
public void Test_Guard_IsNotNullOrEmpty_Null()
151+
{
152+
Guard.IsNotNullOrEmpty(null, nameof(Test_Guard_IsNotNullOrEmpty_Null));
153+
}
154+
155+
[TestCategory("Guard")]
156+
[TestMethod]
157+
[ExpectedException(typeof(ArgumentException))]
158+
public void Test_Guard_IsNotNullOrEmpty_Empty()
159+
{
160+
Guard.IsNotNullOrEmpty(string.Empty, nameof(Test_Guard_IsNotNullOrEmpty_Empty));
161+
}
162+
163+
[TestCategory("Guard")]
164+
[TestMethod]
165+
public void Test_Guard_IsNotNullOrWhiteSpace_Ok()
166+
{
167+
Guard.IsNotNullOrWhiteSpace("Hello", nameof(Test_Guard_IsNotNullOrWhiteSpace_Ok));
168+
}
169+
170+
[TestCategory("Guard")]
171+
[TestMethod]
172+
[ExpectedException(typeof(ArgumentNullException))]
173+
public void Test_Guard_IsNotNullOrWhiteSpace_Null()
174+
{
175+
Guard.IsNotNullOrWhiteSpace(null, nameof(Test_Guard_IsNotNullOrWhiteSpace_Null));
176+
}
177+
178+
[TestCategory("Guard")]
179+
[TestMethod]
180+
[ExpectedException(typeof(ArgumentException))]
181+
public void Test_Guard_IsNotNullOrWhiteSpace_Empty()
182+
{
183+
Guard.IsNotNullOrWhiteSpace(" ", nameof(Test_Guard_IsNotNullOrWhiteSpace_Empty));
184+
}
185+
124186
[TestCategory("Guard")]
125187
[TestMethod]
126188
public void Test_Guard_IsEqualTo_Ok()

0 commit comments

Comments
 (0)