Skip to content

Commit e9344a6

Browse files
committed
Add unit tests for new interpolated Guard APIs
1 parent 086249c commit e9344a6

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

tests/CommunityToolkit.Diagnostics.UnitTests/Test_Guard.cs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,33 @@ public void Test_Guard_IsTrue_Fail_WithMessage()
311311
Assert.Fail();
312312
}
313313

314+
#if NET6_0_OR_GREATER
315+
[TestMethod]
316+
public void Test_Guard_IsTrue_WithHandler_Ok()
317+
{
318+
Guard.IsTrue(true, nameof(Test_Guard_IsTrue_Ok), $"This is an interpolated message: {DateTime.Now.Year}, {"hello".AsSpan()}");
319+
}
320+
321+
[TestMethod]
322+
public void Test_Guard_IsTrue_WithHandler_Fail()
323+
{
324+
try
325+
{
326+
Guard.IsTrue(false, nameof(Test_Guard_IsTrue_WithHandler_Fail), $"This is an interpolated message: {DateTime.Now.Year}, {"hello".AsSpan()}");
327+
}
328+
catch (ArgumentException e)
329+
{
330+
Assert.IsTrue(e.Message.Contains($"This is an interpolated message: {DateTime.Now.Year}, {"hello".AsSpan()}"));
331+
332+
return;
333+
}
334+
335+
// Compiler detects this is unreachable from attribute,
336+
// but we leave the assertion to double check that's valid
337+
Assert.Fail();
338+
}
339+
#endif
340+
314341
[TestMethod]
315342
public void Test_Guard_IsFalse_Ok()
316343
{
@@ -342,6 +369,33 @@ public void Test_Guard_IsFalse_Fail_WithMessage()
342369
Assert.Fail();
343370
}
344371

372+
#if NET6_0_OR_GREATER
373+
[TestMethod]
374+
public void Test_Guard_IsFalse_WithHandler_Ok()
375+
{
376+
Guard.IsFalse(false, nameof(Test_Guard_IsFalse_WithHandler_Ok), $"This is an interpolated message: {DateTime.Now.Year}, {"hello".AsSpan()}");
377+
}
378+
379+
[TestMethod]
380+
public void Test_Guard_IsFalse_WithHandler_Fail()
381+
{
382+
try
383+
{
384+
Guard.IsFalse(true, nameof(Test_Guard_IsFalse_WithHandler_Fail), $"This is an interpolated message: {DateTime.Now.Year}, {"hello".AsSpan()}");
385+
}
386+
catch (ArgumentException e)
387+
{
388+
Assert.IsTrue(e.Message.Contains($"This is an interpolated message: {DateTime.Now.Year}, {"hello".AsSpan()}"));
389+
390+
return;
391+
}
392+
393+
// Compiler detects this is unreachable from attribute,
394+
// but we leave the assertion to double check that's valid
395+
Assert.Fail();
396+
}
397+
#endif
398+
345399
[TestMethod]
346400
public void Test_Guard_IsLessThan_Ok()
347401
{

0 commit comments

Comments
 (0)