Skip to content

Commit 7b6c62f

Browse files
committed
Feat: IsNullableValueType
1 parent 52bd4c6 commit 7b6c62f

File tree

2 files changed

+79
-2
lines changed

2 files changed

+79
-2
lines changed

src/CodeOfChaos.Extensions/ReflectionHelpers.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,10 @@ public static bool IsNullableReferenceType(this ParameterInfo parameter) {
2929
return nullabilityFlag == 2;
3030

3131
}
32+
33+
// WHY IS THIS SO EASY!
34+
public static bool IsNullableValueType(this ParameterInfo parameter) {
35+
return Nullable.GetUnderlyingType(parameter.ParameterType) != null;
36+
}
37+
3238
}

tests/Tests.CodeOfChaos.Extensions/ReflectionHelperTests.cs

Lines changed: 73 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ private class TestClass
1616
public static void MethodWithNullability(
1717
string nonNullable,
1818
string? nullable,
19-
int valueType)
20-
{ }
19+
int valueType,
20+
int? nullableValueType
21+
) { }
2122
}
2223

2324
// -----------------------------------------------------------------------------------------------------------------
@@ -64,5 +65,75 @@ public async Task IsNullableReferenceType_ShouldReturnFalse_ForValueType()
6465
// Assert
6566
await Assert.That(result).IsFalse().Because("Expected parameter 'valueType' to be recognized as a non-nullable reference type.");
6667
}
68+
69+
[Test]
70+
public async Task IsNullableReferenceType_ShouldReturnFalse_ForNullableValueType()
71+
{
72+
// Arrange
73+
MethodInfo method = typeof(TestClass).GetMethod(nameof(TestClass.MethodWithNullability))!;
74+
ParameterInfo nullableValueTypeParameter = method.GetParameters().First(p => p.Name == "nullableValueType");
75+
76+
// Act
77+
bool result = nullableValueTypeParameter.IsNullableReferenceType();
78+
79+
// Assert
80+
await Assert.That(result).IsFalse().Because("Expected parameter 'nullableInt' to be recognized as not a nullable reference type (it's a nullable value type).");
81+
}
82+
83+
[Test]
84+
public async Task IsNullableValueType_ShouldReturnFalse_ForReferenceType()
85+
{
86+
// Arrange
87+
MethodInfo method = typeof(TestClass).GetMethod(nameof(TestClass.MethodWithNullability))!;
88+
ParameterInfo referenceTypeParameter = method.GetParameters().First(p => p.Name == "nonNullable");
89+
90+
// Act
91+
bool result = referenceTypeParameter.IsNullableValueType();
92+
93+
// Assert
94+
await Assert.That(result).IsFalse().Because("Expected parameter 'nonNullable' to not be a nullable value type (it's a reference type).");
95+
}
96+
97+
[Test]
98+
public async Task IsNullableValueType_ShouldReturnFalse_ForNonNullableValueType()
99+
{
100+
// Arrange
101+
MethodInfo method = typeof(TestClass).GetMethod(nameof(TestClass.MethodWithNullability))!;
102+
ParameterInfo valueTypeParameter = method.GetParameters().First(p => p.Name == "valueType");
103+
104+
// Act
105+
bool result = valueTypeParameter.IsNullableValueType();
106+
107+
// Assert
108+
await Assert.That(result).IsFalse().Because("Expected parameter 'valueType' to not be a nullable value type (it's a non-nullable value type).");
109+
}
110+
111+
[Test]
112+
public async Task IsNullableValueType_ShouldReturnTrue_ForNullableValueType()
113+
{
114+
// Arrange
115+
MethodInfo method = typeof(TestClass).GetMethod(nameof(TestClass.MethodWithNullability))!;
116+
ParameterInfo nullableValueTypeParameter = method.GetParameters().First(p => p.Name == "nullableValueType");
117+
118+
// Act
119+
bool result = nullableValueTypeParameter.IsNullableValueType();
120+
121+
// Assert
122+
await Assert.That(result).IsTrue().Because("Expected parameter 'nullableValueType' to be recognized as a nullable value type.");
123+
}
124+
125+
[Test]
126+
public async Task IsNullableValueType_ShouldReturnFalse_ForNullableReferenceType()
127+
{
128+
// Arrange
129+
MethodInfo method = typeof(TestClass).GetMethod(nameof(TestClass.MethodWithNullability))!;
130+
ParameterInfo nullableParameter = method.GetParameters().First(p => p.Name == "nullable");
131+
132+
// Act
133+
bool result = nullableParameter.IsNullableValueType();
134+
135+
// Assert
136+
await Assert.That(result).IsFalse().Because("Expected parameter 'nullable' to not be a nullable value type (it's a nullable reference type).");
137+
}
67138

68139
}

0 commit comments

Comments
 (0)