Skip to content

Commit c096438

Browse files
committed
Add unit tests for new generic combinations
1 parent 5d7f2a7 commit c096438

File tree

2 files changed

+67
-4
lines changed

2 files changed

+67
-4
lines changed

CommunityToolkit.Diagnostics/Guard.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ public static void IsNull<T>(T? value, [CallerArgumentExpression("value")] strin
4040
/// <param name="value">The input value to test.</param>
4141
/// <param name="name">The name of the input parameter being tested.</param>
4242
/// <exception cref="ArgumentException">Thrown if <paramref name="value"/> is not <see langword="null"/>.</exception>
43-
/// <remarks>The method is generic to avoid boxing the parameters, if they are value types.</remarks>
4443
[MethodImpl(MethodImplOptions.AggressiveInlining)]
4544
public static void IsNull<T>(T? value, [CallerArgumentExpression("value")] string name = "")
4645
where T : struct
@@ -78,7 +77,6 @@ public static void IsNotNull<T>([NotNull] T? value, [CallerArgumentExpression("v
7877
/// <param name="value">The input value to test.</param>
7978
/// <param name="name">The name of the input parameter being tested.</param>
8079
/// <exception cref="ArgumentNullException">Thrown if <paramref name="value"/> is <see langword="null"/>.</exception>
81-
/// <remarks>The method is generic to avoid boxing the parameters, if they are value types.</remarks>
8280
[MethodImpl(MethodImplOptions.AggressiveInlining)]
8381
public static void IsNotNull<T>([NotNull] T? value, [CallerArgumentExpression("value")] string name = "")
8482
where T : struct

tests/CommunityToolkit.Diagnostics.UnitTests/Test_Guard.cs

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ public void Test_Guard_IsNull_Ok()
1515
{
1616
Guard.IsNull<object>(null, nameof(Test_Guard_IsNull_Ok));
1717
Guard.IsNull<int>(null, nameof(Test_Guard_IsNull_Ok));
18+
19+
static void Test<T>(T? obj)
20+
{
21+
Guard.IsNull(obj, nameof(Test_Guard_IsNull_Ok));
22+
}
23+
24+
Test<string>(null);
25+
Test<int?>(null);
1826
}
1927

2028
[TestMethod]
@@ -28,14 +36,47 @@ public void Test_Guard_IsNull_ClassFail()
2836
[ExpectedException(typeof(ArgumentException))]
2937
public void Test_Guard_IsNull_StructFail()
3038
{
31-
Guard.IsNull<int>(7, nameof(Test_Guard_IsNull_StructFail));
39+
Guard.IsNull(7, nameof(Test_Guard_IsNull_StructFail));
40+
}
41+
42+
[TestMethod]
43+
[ExpectedException(typeof(ArgumentException))]
44+
public void Test_Guard_IsNull_GenericClassFail()
45+
{
46+
static void Test<T>(T? obj)
47+
{
48+
Guard.IsNull(obj, nameof(Test_Guard_IsNull_GenericClassFail));
49+
}
50+
51+
Test("Hi!");
52+
}
53+
54+
[TestMethod]
55+
[ExpectedException(typeof(ArgumentException))]
56+
public void Test_Guard_IsNull_GenericStructFail()
57+
{
58+
static void Test<T>(T? obj)
59+
{
60+
Guard.IsNull(obj, nameof(Test_Guard_IsNull_GenericStructFail));
61+
}
62+
63+
Test(42);
3264
}
3365

3466
[TestMethod]
3567
public void Test_Guard_IsNotNull_Ok()
3668
{
3769
Guard.IsNotNull(new object(), nameof(Test_Guard_IsNotNull_Ok));
38-
Guard.IsNotNull<int>(7, nameof(Test_Guard_IsNotNull_Ok));
70+
Guard.IsNotNull(7, nameof(Test_Guard_IsNotNull_Ok));
71+
72+
static void Test<T>(T? obj)
73+
{
74+
Guard.IsNotNull(obj, nameof(Test_Guard_IsNotNull_Ok));
75+
}
76+
77+
Test("Hi!");
78+
Test(42);
79+
Test<int?>(42);
3980
}
4081

4182
[TestMethod]
@@ -52,6 +93,30 @@ public void Test_Guard_IsNotNull_StructFail()
5293
Guard.IsNotNull<int>(null, nameof(Test_Guard_IsNotNull_StructFail));
5394
}
5495

96+
[TestMethod]
97+
[ExpectedException(typeof(ArgumentNullException))]
98+
public void Test_Guard_IsNotNull_GenericClassFail()
99+
{
100+
static void Test<T>(T? obj)
101+
{
102+
Guard.IsNotNull(obj, nameof(Test_Guard_IsNotNull_GenericClassFail));
103+
}
104+
105+
Test<string>(null);
106+
}
107+
108+
[TestMethod]
109+
[ExpectedException(typeof(ArgumentNullException))]
110+
public void Test_Guard_IsNotNull_GenericStructFail()
111+
{
112+
static void Test<T>(T? obj)
113+
{
114+
Guard.IsNotNull(obj, nameof(Test_Guard_IsNotNull_GenericClassFail));
115+
}
116+
117+
Test<int?>(null);
118+
}
119+
55120
[TestMethod]
56121
public void Test_Guard_IsOfT_Ok()
57122
{

0 commit comments

Comments
 (0)