Skip to content

Commit b67e5b8

Browse files
committed
Added Guard.IsTrue/False overloads with custom message
1 parent 1523b58 commit b67e5b8

File tree

3 files changed

+92
-2
lines changed

3 files changed

+92
-2
lines changed

Microsoft.Toolkit/Diagnostics/Guard.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,22 @@ public static void IsTrue([DoesNotReturnIf(false)] bool value, string name)
268268
}
269269
}
270270

271+
/// <summary>
272+
/// Asserts that the input value must be <see langword="true"/>.
273+
/// </summary>
274+
/// <param name="value">The input <see cref="bool"/> to test.</param>
275+
/// <param name="name">The name of the input parameter being tested.</param>
276+
/// <param name="message">A message to display if <paramref name="value"/> is <see langword="false"/>.</param>
277+
/// <exception cref="ArgumentException">Thrown if <paramref name="value"/> is <see langword="false"/>.</exception>
278+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
279+
public static void IsTrue([DoesNotReturnIf(false)] bool value, string name, string message)
280+
{
281+
if (!value)
282+
{
283+
ThrowHelper.ThrowArgumentExceptionForIsTrue(name, message);
284+
}
285+
}
286+
271287
/// <summary>
272288
/// Asserts that the input value must be <see langword="false"/>.
273289
/// </summary>
@@ -282,5 +298,21 @@ public static void IsFalse([DoesNotReturnIf(true)] bool value, string name)
282298
ThrowHelper.ThrowArgumentExceptionForIsFalse(name);
283299
}
284300
}
301+
302+
/// <summary>
303+
/// Asserts that the input value must be <see langword="false"/>.
304+
/// </summary>
305+
/// <param name="value">The input <see cref="bool"/> to test.</param>
306+
/// <param name="name">The name of the input parameter being tested.</param>
307+
/// <param name="message">A message to display if <paramref name="value"/> is <see langword="true"/>.</param>
308+
/// <exception cref="ArgumentException">Thrown if <paramref name="value"/> is <see langword="true"/>.</exception>
309+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
310+
public static void IsFalse([DoesNotReturnIf(true)] bool value, string name, string message)
311+
{
312+
if (value)
313+
{
314+
ThrowHelper.ThrowArgumentExceptionForIsFalse(name, message);
315+
}
316+
}
285317
}
286318
}

Microsoft.Toolkit/Diagnostics/ThrowHelper.cs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ public static void ThrowArgumentExceptionForIsReferenceNotEqualTo<T>(string name
157157
}
158158

159159
/// <summary>
160-
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.IsTrue"/> fails.
160+
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.IsTrue(bool,string)"/> fails.
161161
/// </summary>
162162
[MethodImpl(MethodImplOptions.NoInlining)]
163163
public static void ThrowArgumentExceptionForIsTrue(string name)
@@ -166,12 +166,30 @@ public static void ThrowArgumentExceptionForIsTrue(string name)
166166
}
167167

168168
/// <summary>
169-
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.IsFalse"/> fails.
169+
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.IsTrue(bool,string,string)"/> fails.
170+
/// </summary>
171+
[MethodImpl(MethodImplOptions.NoInlining)]
172+
public static void ThrowArgumentExceptionForIsTrue(string name, string message)
173+
{
174+
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} must be true, was false: {message.ToAssertString()}");
175+
}
176+
177+
/// <summary>
178+
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.IsFalse(bool,string)"/> fails.
170179
/// </summary>
171180
[MethodImpl(MethodImplOptions.NoInlining)]
172181
public static void ThrowArgumentExceptionForIsFalse(string name)
173182
{
174183
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} must be false, was true");
175184
}
185+
186+
/// <summary>
187+
/// Throws an <see cref="ArgumentException"/> when <see cref="Guard.IsFalse(bool,string,string)"/> fails.
188+
/// </summary>
189+
[MethodImpl(MethodImplOptions.NoInlining)]
190+
public static void ThrowArgumentExceptionForIsFalse(string name, string message)
191+
{
192+
ThrowArgumentException(name, $"Parameter {name.ToAssertString()} must be false, was true: {message.ToAssertString()}");
193+
}
176194
}
177195
}

UnitTests/UnitTests.Shared/Diagnostics/Test_Guard.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ public void Test_Guard_IsReferenceNotEqualTo_Fail()
216216
public void Test_Guard_IsTrue_Ok()
217217
{
218218
Guard.IsTrue(true, nameof(Test_Guard_IsTrue_Ok));
219+
Guard.IsTrue(true, nameof(Test_Guard_IsTrue_Ok), "Hello world");
219220
}
220221

221222
[TestCategory("Guard")]
@@ -226,11 +227,32 @@ public void Test_Guard_IsTrue_Fail()
226227
Guard.IsTrue(false, nameof(Test_Guard_IsTrue_Fail));
227228
}
228229

230+
[TestCategory("Guard")]
231+
[TestMethod]
232+
public void Test_Guard_IsTrue_Fail_WithMessage()
233+
{
234+
try
235+
{
236+
Guard.IsTrue(false, nameof(Test_Guard_IsTrue_Fail_WithMessage), "Hello world");
237+
}
238+
catch (ArgumentException e)
239+
{
240+
Assert.IsTrue(e.Message.Contains("\"Hello world\""));
241+
242+
return;
243+
}
244+
245+
// Compiler detects this is unreachable from attribute,
246+
// but we leave the assertion to double check that's valid
247+
Assert.Fail();
248+
}
249+
229250
[TestCategory("Guard")]
230251
[TestMethod]
231252
public void Test_Guard_IsFalse_Ok()
232253
{
233254
Guard.IsFalse(false, nameof(Test_Guard_IsFalse_Ok));
255+
Guard.IsFalse(false, nameof(Test_Guard_IsFalse_Ok), "Hello world");
234256
}
235257

236258
[TestCategory("Guard")]
@@ -241,6 +263,24 @@ public void Test_Guard_IsFalse_Fail()
241263
Guard.IsFalse(true, nameof(Test_Guard_IsFalse_Fail));
242264
}
243265

266+
[TestCategory("Guard")]
267+
[TestMethod]
268+
public void Test_Guard_IsFalse_Fail_WithMessage()
269+
{
270+
try
271+
{
272+
Guard.IsFalse(true, nameof(Test_Guard_IsFalse_Fail_WithMessage), "Hello world");
273+
}
274+
catch (ArgumentException e)
275+
{
276+
Assert.IsTrue(e.Message.Contains("\"Hello world\""));
277+
278+
return;
279+
}
280+
281+
Assert.Fail();
282+
}
283+
244284
[TestCategory("Guard")]
245285
[TestMethod]
246286
public void Test_Guard_IsLessThan_Ok()

0 commit comments

Comments
 (0)