Skip to content

Commit 5fa96da

Browse files
authored
Merge pull request #692 from Stedoss/value-type-nulls
Add support for `ReturnsNull` calls for nullable value types
2 parents 8d00402 + 3946fe8 commit 5fa96da

File tree

3 files changed

+95
-0
lines changed

3 files changed

+95
-0
lines changed

src/NSubstitute/Extensions/ReturnsExtensions.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,18 @@ public static ConfiguredCall ReturnsNull<T>(this T value) where T : class =>
2020
public static ConfiguredCall ReturnsNullForAnyArgs<T>(this T value) where T : class =>
2121
value.ReturnsForAnyArgs(default(T));
2222

23+
/// <summary>
24+
/// Set null as returned value for this call.
25+
/// </summary>
26+
public static ConfiguredCall ReturnsNull<T>(this T? value) where T : struct =>
27+
value.Returns(default(T?));
28+
29+
/// <summary>
30+
/// Set null as returned value for this call made with any arguments.
31+
/// </summary>
32+
public static ConfiguredCall ReturnsNullForAnyArgs<T>(this T? value) where T : struct =>
33+
value.ReturnsForAnyArgs(default(T?));
34+
2335
/// <summary>
2436
/// Set null as returned value for this call.
2537
/// </summary>
@@ -46,5 +58,29 @@ public static ConfiguredCall ReturnsNullForAnyArgs<T>(this Task<T> value) where
4658
/// <returns></returns>
4759
public static ConfiguredCall ReturnsNullForAnyArgs<T>(this ValueTask<T> value) where T : class =>
4860
value.ReturnsForAnyArgs(default(T));
61+
62+
/// <summary>
63+
/// Set null as returned value for this call.
64+
/// </summary>
65+
public static ConfiguredCall ReturnsNull<T>(this Task<T?> value) where T : struct =>
66+
value.Returns(default(T?));
67+
68+
/// <summary>
69+
/// Set null as returned value for this call made with any arguments.
70+
/// </summary>
71+
public static ConfiguredCall ReturnsNullForAnyArgs<T>(this Task<T?> value) where T : struct =>
72+
value.ReturnsForAnyArgs(default(T?));
73+
74+
/// <summary>
75+
/// Set null as returned value for this call.
76+
/// </summary>
77+
public static ConfiguredCall ReturnsNull<T>(this ValueTask<T?> value) where T : struct =>
78+
value.Returns(default(T?));
79+
80+
/// <summary>
81+
/// Set null as returned value for this call made with any arguments.
82+
/// </summary>
83+
public static ConfiguredCall ReturnsNullForAnyArgs<T>(this ValueTask<T?> value) where T : struct =>
84+
value.ReturnsForAnyArgs(default(T?));
4985
}
5086
}

tests/NSubstitute.Acceptance.Specs/Infrastructure/ISomething.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ public interface ISomething
1616
int MethodWithRefParameter(int arg1, ref int arg2);
1717
int MethodWithMultipleRefParameters(int arg1, ref int arg2, ref int arg3);
1818
int MethodWithOutParameter(int arg1, out int arg2);
19+
int? NullableCount();
20+
int? NullableWithParams(int i, string s);
1921

2022
object this[string key] { get; set; }
2123
System.Threading.Tasks.Task Async();
@@ -26,12 +28,16 @@ public interface ISomething
2628
System.Threading.Tasks.Task<string> SayAsync(string s);
2729
System.Threading.Tasks.Task<SomeClass> SomeActionAsync();
2830
System.Threading.Tasks.Task<SomeClass> SomeActionWithParamsAsync(int i, string s);
31+
System.Threading.Tasks.Task<int?> NullableCountAsync();
32+
System.Threading.Tasks.Task<int?> NullableWithParamsAsync(int i, string s);
2933

3034
System.Threading.Tasks.ValueTask<int> CountValueTaskAsync();
3135
System.Threading.Tasks.ValueTask<string> EchoValueTaskAsync(int i);
3236
System.Threading.Tasks.ValueTask<int> AnythingValueTaskAsync(object stuff);
3337
System.Threading.Tasks.ValueTask<string> SayValueTaskAsync(string s);
3438
System.Threading.Tasks.ValueTask<SomeClass> SomeActionValueTaskAsync();
3539
System.Threading.Tasks.ValueTask<SomeClass> SomeActionWithParamsValueTaskAsync(int i, string s);
40+
System.Threading.Tasks.ValueTask<int?> NullableCountValueTaskAsync();
41+
System.Threading.Tasks.ValueTask<int?> NullableCountValueTaskWithParamsAsync(int i, string s);
3642
}
3743
}

tests/NSubstitute.Acceptance.Specs/ReturningResults.cs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,59 @@ public void Return_multiple_ValueTask_async_results_from_funcs()
330330
Assert.That(_something.CountValueTaskAsync().Result, Is.EqualTo(3), "Fourth return");
331331
}
332332

333+
[Test]
334+
public void Returns_null_for_nullable_value_type()
335+
{
336+
_something.NullableCount().ReturnsNull();
337+
338+
Assert.That(_something.NullableCount(), Is.Null);
339+
}
340+
341+
[Test]
342+
public void Returns_null_for_any_args_when_nullable_value_type()
343+
{
344+
_something.NullableWithParams(2, "test").ReturnsNullForAnyArgs();
345+
346+
Assert.That(_something.NullableWithParams(123, "something else"), Is.Null);
347+
}
348+
349+
[Test]
350+
public void Returns_null_for_task_of_null_value_type()
351+
{
352+
_something.NullableCountAsync().ReturnsNull();
353+
354+
Assert.That(_something.NullableCountAsync(), Is.TypeOf<Task<int?>>());
355+
Assert.That(_something.NullableCountAsync().Result, Is.Null);
356+
}
357+
358+
[Test]
359+
public void Returns_null_for_any_args_when_task_of_null_value_type()
360+
{
361+
_something.NullableWithParamsAsync(2, "test").ReturnsNullForAnyArgs();
362+
363+
Assert.That(_something.NullableWithParamsAsync(123, "something else"), Is.TypeOf<Task<int?>>());
364+
Assert.That(_something.NullableWithParamsAsync(123, "something else").Result, Is.Null);
365+
}
366+
367+
[Test]
368+
public void Returns_null_for_TaskValue_for_null_value_type()
369+
{
370+
_something.NullableCountValueTaskAsync().ReturnsNull();
371+
372+
Assert.That(_something.NullableCountValueTaskAsync(), Is.TypeOf<ValueTask<int?>>());
373+
Assert.That(_something.NullableCountValueTaskAsync().Result, Is.Null);
374+
}
375+
376+
[Test]
377+
public void Returns_null_for_any_args_when_TaskValue_is_of_null_value_type()
378+
{
379+
_something.NullableCountValueTaskWithParamsAsync(2, "test").ReturnsNullForAnyArgs();
380+
381+
Assert.That(_something.NullableCountValueTaskWithParamsAsync(123, "something else"),
382+
Is.TypeOf<ValueTask<int?>>());
383+
Assert.That(_something.NullableCountValueTaskWithParamsAsync(123, "something else").Result, Is.Null);
384+
}
385+
333386
[SetUp]
334387
public void SetUp()
335388
{

0 commit comments

Comments
 (0)