forked from nsubstitute/NSubstitute
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWhenCalled.cs
More file actions
101 lines (88 loc) · 3.41 KB
/
WhenCalled.cs
File metadata and controls
101 lines (88 loc) · 3.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
using System.Threading.Tasks;
using NSubstitute.Routing;
namespace NSubstitute.Core;
public class WhenCalled<T>(ISubstitutionContext context, T substitute, Action<T> call, MatchArgs matchArgs)
{
private readonly ICallRouter _callRouter = context.GetCallRouterFor(substitute!);
private readonly IThreadLocalContext _threadContext = context.ThreadContext;
private readonly IRouteFactory _routeFactory = context.RouteFactory;
/// <summary>
/// Perform this action when called.
/// </summary>
/// <param name="callbackWithArguments"></param>
public void Do(Action<CallInfo> callbackWithArguments)
{
_threadContext.SetNextRoute(_callRouter, x => _routeFactory.DoWhenCalled(x, callbackWithArguments, matchArgs));
call(substitute);
}
/// <summary>
/// Perform this action when called.
/// </summary>
/// <param name="callbackWithArguments"></param>
public void Do(Func<CallInfo, Task> callbackWithArguments)
{
Do(callInfo => callbackWithArguments(callInfo).GetAwaiter().GetResult());
}
/// <summary>
/// Perform this configured callback when called.
/// </summary>
/// <param name="callback"></param>
public void Do(Callback callback)
{
_threadContext.SetNextRoute(_callRouter, x => _routeFactory.DoWhenCalled(x, callback.Call, matchArgs));
call(substitute);
}
/// <summary>
/// Do not call the base implementation on future calls. For use with partial substitutes.
/// </summary>
public void DoNotCallBase()
{
_threadContext.SetNextRoute(_callRouter, x => _routeFactory.DoNotCallBase(x, matchArgs));
call(substitute);
}
/// <summary>
/// Call the base implementation of future calls. For use with non-partial class substitutes.
/// </summary>
public void CallBase()
{
_threadContext.SetNextRoute(_callRouter, x => _routeFactory.CallBase(x, matchArgs));
call(substitute);
}
/// <summary>
/// Throw the specified exception when called.
/// </summary>
public void Throw(Exception exception) =>
Do(ci => throw exception);
/// <summary>
/// Throw an exception of the given type when called.
/// </summary>
public TException Throw<TException>() where TException : Exception, new()
{
var exception = new TException();
Do(_ => throw exception!);
return exception;
}
/// <summary>
/// Throw an exception generated by the specified function when called.
/// </summary>
public void Throw(Func<CallInfo, Exception> createException) =>
Do(ci => throw createException(ci));
/// <summary>
/// Throws the specified exception when called.
/// </summary>
/// Prefer <see cref="Throw(System.Exception)" /> for readability.
public void Throws(Exception exception) =>
Throw(exception);
/// <summary>
/// Throws an exception of the given type when called.
/// </summary>
/// Prefer <see cref="Throw{TException}" /> for readability.
public TException Throws<TException>() where TException : Exception, new()
=> Throw<TException>();
/// <summary>
/// Throws an exception generated by the specified function when called.
/// </summary>
/// Prefer <see cref="Throw(System.Func{CallInfo, System.Exception})" /> for readability.
public void Throws(Func<CallInfo, Exception> createException) =>
Throw(createException);
}