-
Notifications
You must be signed in to change notification settings - Fork 263
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
312 changed files
with
15,426 additions
and
15,736 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,127 +1,126 @@ | ||
using System.Collections.Concurrent; | ||
using NSubstitute.Callbacks; | ||
using NSubstitute.Callbacks; | ||
using NSubstitute.Core; | ||
using System.Collections.Concurrent; | ||
|
||
// Disable nullability for client API, so it does not affect clients. | ||
#nullable disable annotations | ||
|
||
namespace NSubstitute | ||
namespace NSubstitute; | ||
|
||
/// <summary> | ||
/// Perform this chain of callbacks and/or always callback when called. | ||
/// </summary> | ||
public class Callback | ||
{ | ||
/// <summary> | ||
/// Perform this chain of callbacks and/or always callback when called. | ||
/// Perform as first in chain of callback when called. | ||
/// </summary> | ||
public class Callback | ||
/// <param name="doThis"></param> | ||
/// <returns></returns> | ||
public static ConfiguredCallback First(Action<CallInfo> doThis) | ||
{ | ||
/// <summary> | ||
/// Perform as first in chain of callback when called. | ||
/// </summary> | ||
/// <param name="doThis"></param> | ||
/// <returns></returns> | ||
public static ConfiguredCallback First(Action<CallInfo> doThis) | ||
{ | ||
return new ConfiguredCallback().Then(doThis); | ||
} | ||
return new ConfiguredCallback().Then(doThis); | ||
} | ||
|
||
/// <summary> | ||
/// Perform this action always when callback is called. | ||
/// </summary> | ||
/// <param name="doThis"></param> | ||
/// <returns></returns> | ||
public static Callback Always(Action<CallInfo> doThis) | ||
{ | ||
return new ConfiguredCallback().AndAlways(doThis); | ||
} | ||
/// <summary> | ||
/// Perform this action always when callback is called. | ||
/// </summary> | ||
/// <param name="doThis"></param> | ||
/// <returns></returns> | ||
public static Callback Always(Action<CallInfo> doThis) | ||
{ | ||
return new ConfiguredCallback().AndAlways(doThis); | ||
} | ||
|
||
/// <summary> | ||
/// Throw exception returned by function as first callback in chain of callback when called. | ||
/// </summary> | ||
/// <param name="throwThis"></param> | ||
/// <returns></returns> | ||
public static ConfiguredCallback FirstThrow<TException>(Func<CallInfo, TException> throwThis) where TException : Exception | ||
{ | ||
return new ConfiguredCallback().ThenThrow(throwThis); | ||
} | ||
/// <summary> | ||
/// Throw exception returned by function as first callback in chain of callback when called. | ||
/// </summary> | ||
/// <param name="throwThis"></param> | ||
/// <returns></returns> | ||
public static ConfiguredCallback FirstThrow<TException>(Func<CallInfo, TException> throwThis) where TException : Exception | ||
{ | ||
return new ConfiguredCallback().ThenThrow(throwThis); | ||
} | ||
|
||
/// <summary> | ||
/// Throw this exception as first callback in chain of callback when called. | ||
/// </summary> | ||
/// <param name="exception"></param> | ||
/// <returns></returns> | ||
public static ConfiguredCallback FirstThrow<TException>(TException exception) where TException : Exception | ||
{ | ||
return new ConfiguredCallback().ThenThrow(info => exception); | ||
} | ||
/// <summary> | ||
/// Throw this exception as first callback in chain of callback when called. | ||
/// </summary> | ||
/// <param name="exception"></param> | ||
/// <returns></returns> | ||
public static ConfiguredCallback FirstThrow<TException>(TException exception) where TException : Exception | ||
{ | ||
return new ConfiguredCallback().ThenThrow(info => exception); | ||
} | ||
|
||
/// <summary> | ||
/// Throw exception returned by function always when callback is called. | ||
/// </summary> | ||
/// <typeparam name="TException">The type of the exception.</typeparam> | ||
/// <param name="throwThis">The throw this.</param> | ||
/// <returns></returns> | ||
public static Callback AlwaysThrow<TException>(Func<CallInfo, TException> throwThis) where TException : Exception | ||
{ | ||
return new ConfiguredCallback().AndAlways(ToCallback(throwThis)); | ||
} | ||
/// <summary> | ||
/// Throw exception returned by function always when callback is called. | ||
/// </summary> | ||
/// <typeparam name="TException">The type of the exception.</typeparam> | ||
/// <param name="throwThis">The throw this.</param> | ||
/// <returns></returns> | ||
public static Callback AlwaysThrow<TException>(Func<CallInfo, TException> throwThis) where TException : Exception | ||
{ | ||
return new ConfiguredCallback().AndAlways(ToCallback(throwThis)); | ||
} | ||
|
||
/// <summary> | ||
/// Throw this exception always when callback is called. | ||
/// </summary> | ||
/// <typeparam name="TException">The type of the exception.</typeparam> | ||
/// <param name="exception">The exception.</param> | ||
/// <returns></returns> | ||
public static Callback AlwaysThrow<TException>(TException exception) where TException : Exception | ||
{ | ||
return AlwaysThrow(_ => exception); | ||
} | ||
/// <summary> | ||
/// Throw this exception always when callback is called. | ||
/// </summary> | ||
/// <typeparam name="TException">The type of the exception.</typeparam> | ||
/// <param name="exception">The exception.</param> | ||
/// <returns></returns> | ||
public static Callback AlwaysThrow<TException>(TException exception) where TException : Exception | ||
{ | ||
return AlwaysThrow(_ => exception); | ||
} | ||
|
||
protected static Action<CallInfo> ToCallback<TException>(Func<CallInfo, TException> throwThis) | ||
where TException : notnull, Exception | ||
{ | ||
return ci => { if (throwThis != null) throw throwThis(ci); }; | ||
} | ||
protected static Action<CallInfo> ToCallback<TException>(Func<CallInfo, TException> throwThis) | ||
where TException : notnull, Exception | ||
{ | ||
return ci => { if (throwThis != null) throw throwThis(ci); }; | ||
} | ||
|
||
internal Callback() { } | ||
private readonly ConcurrentQueue<Action<CallInfo>> callbackQueue = new ConcurrentQueue<Action<CallInfo>>(); | ||
private Action<CallInfo> alwaysDo = x => { }; | ||
private Action<CallInfo> keepDoing = x => { }; | ||
internal Callback() { } | ||
private readonly ConcurrentQueue<Action<CallInfo>> callbackQueue = new ConcurrentQueue<Action<CallInfo>>(); | ||
private Action<CallInfo> alwaysDo = x => { }; | ||
private Action<CallInfo> keepDoing = x => { }; | ||
|
||
protected void AddCallback(Action<CallInfo> doThis) | ||
{ | ||
callbackQueue.Enqueue(doThis); | ||
} | ||
protected void AddCallback(Action<CallInfo> doThis) | ||
{ | ||
callbackQueue.Enqueue(doThis); | ||
} | ||
|
||
protected void SetAlwaysDo(Action<CallInfo> always) | ||
protected void SetAlwaysDo(Action<CallInfo> always) | ||
{ | ||
alwaysDo = always ?? (_ => { }); | ||
} | ||
|
||
protected void SetKeepDoing(Action<CallInfo> keep) | ||
{ | ||
keepDoing = keep ?? (_ => { }); | ||
} | ||
|
||
public void Call(CallInfo callInfo) | ||
{ | ||
try | ||
{ | ||
alwaysDo = always ?? (_ => { }); | ||
CallFromStack(callInfo); | ||
} | ||
|
||
protected void SetKeepDoing(Action<CallInfo> keep) | ||
finally | ||
{ | ||
keepDoing = keep ?? (_ => { }); | ||
alwaysDo(callInfo); | ||
} | ||
} | ||
|
||
public void Call(CallInfo callInfo) | ||
private void CallFromStack(CallInfo callInfo) | ||
{ | ||
if (callbackQueue.TryDequeue(out var callback)) | ||
{ | ||
try | ||
{ | ||
CallFromStack(callInfo); | ||
} | ||
finally | ||
{ | ||
alwaysDo(callInfo); | ||
} | ||
callback(callInfo); | ||
} | ||
|
||
private void CallFromStack(CallInfo callInfo) | ||
else | ||
{ | ||
if (callbackQueue.TryDequeue(out var callback)) | ||
{ | ||
callback(callInfo); | ||
} | ||
else | ||
{ | ||
keepDoing(callInfo); | ||
} | ||
keepDoing(callInfo); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.