Skip to content

Commit

Permalink
Use file scoped namepsaces (#763)
Browse files Browse the repository at this point in the history
  • Loading branch information
Romfos authored Dec 25, 2023
1 parent 8b2beb2 commit f470b5d
Show file tree
Hide file tree
Showing 312 changed files with 15,426 additions and 15,736 deletions.
1 change: 1 addition & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ dotnet_style_qualification_for_field = false:suggestion
dotnet_style_qualification_for_property = false:suggestion
dotnet_style_qualification_for_method = false:suggestion
dotnet_style_qualification_for_event = false:suggestion
csharp_style_namespace_declarations=file_scoped:warning

# ReSharper properties
resharper_int_align_switch_expressions = true
Expand Down
323 changes: 161 additions & 162 deletions src/NSubstitute/Arg.cs

Large diffs are not rendered by default.

195 changes: 97 additions & 98 deletions src/NSubstitute/Callback.cs
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);
}
}
}
117 changes: 58 additions & 59 deletions src/NSubstitute/Callbacks/ConfiguredCallback.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,74 +3,73 @@
// Disable nullability for client API, so it does not affect clients.
#nullable disable annotations

namespace NSubstitute.Callbacks
namespace NSubstitute.Callbacks;

public class ConfiguredCallback : EndCallbackChain
{
public class ConfiguredCallback : EndCallbackChain
internal ConfiguredCallback() { }

/// <summary>
/// Perform this action once in chain of called callbacks.
/// </summary>
public ConfiguredCallback Then(Action<CallInfo> doThis)
{
internal ConfiguredCallback() { }
AddCallback(doThis);
return this;
}

/// <summary>
/// Perform this action once in chain of called callbacks.
/// </summary>
public ConfiguredCallback Then(Action<CallInfo> doThis)
{
AddCallback(doThis);
return this;
}
/// <summary>
/// Keep doing this action after the other callbacks have run.
/// </summary>
public EndCallbackChain ThenKeepDoing(Action<CallInfo> doThis)
{
SetKeepDoing(doThis);
return this;
}

/// <summary>
/// Keep doing this action after the other callbacks have run.
/// </summary>
public EndCallbackChain ThenKeepDoing(Action<CallInfo> doThis)
{
SetKeepDoing(doThis);
return this;
}
/// <summary>
/// Keep throwing this exception after the other callbacks have run.
/// </summary>
public EndCallbackChain ThenKeepThrowing<TException>(Func<CallInfo, TException> throwThis) where TException : Exception =>
ThenKeepDoing(ToCallback(throwThis));

/// <summary>
/// Keep throwing this exception after the other callbacks have run.
/// </summary>
public EndCallbackChain ThenKeepThrowing<TException>(Func<CallInfo, TException> throwThis) where TException : Exception =>
ThenKeepDoing(ToCallback(throwThis));
/// <summary>
/// Keep throwing this exception after the other callbacks have run.
/// </summary>
public EndCallbackChain ThenKeepThrowing<TException>(TException throwThis) where TException : Exception =>
ThenKeepThrowing(info => throwThis);

/// <summary>
/// Keep throwing this exception after the other callbacks have run.
/// </summary>
public EndCallbackChain ThenKeepThrowing<TException>(TException throwThis) where TException : Exception =>
ThenKeepThrowing(info => throwThis);
/// <summary>
/// Throw exception returned by function once when called in a chain of callbacks.
/// </summary>
/// <typeparam name="TException">The type of the exception</typeparam>
/// <param name="throwThis">Produce the exception to throw for a CallInfo</param>
public ConfiguredCallback ThenThrow<TException>(Func<CallInfo, TException> throwThis) where TException : Exception
{
AddCallback(ToCallback(throwThis));
return this;
}

/// <summary>
/// Throw exception returned by function once when called in a chain of callbacks.
/// </summary>
/// <typeparam name="TException">The type of the exception</typeparam>
/// <param name="throwThis">Produce the exception to throw for a CallInfo</param>
public ConfiguredCallback ThenThrow<TException>(Func<CallInfo, TException> throwThis) where TException : Exception
{
AddCallback(ToCallback(throwThis));
return this;
}
/// <summary>
/// Throw this exception once when called in a chain of callbacks.
/// </summary>
/// <typeparam name="TException">The type of the exception</typeparam>
/// <param name="exception">The exception to throw</param>
public ConfiguredCallback ThenThrow<TException>(TException exception) where TException : Exception =>
ThenThrow(_ => exception);
}

/// <summary>
/// Throw this exception once when called in a chain of callbacks.
/// </summary>
/// <typeparam name="TException">The type of the exception</typeparam>
/// <param name="exception">The exception to throw</param>
public ConfiguredCallback ThenThrow<TException>(TException exception) where TException : Exception =>
ThenThrow(_ => exception);
}
public class EndCallbackChain : Callback
{
internal EndCallbackChain() { }

public class EndCallbackChain : Callback
/// <summary>
/// Perform the given action for every call.
/// </summary>
/// <param name="doThis">The action to perform for every call</param>
public Callback AndAlways(Action<CallInfo> doThis)
{
internal EndCallbackChain() { }

/// <summary>
/// Perform the given action for every call.
/// </summary>
/// <param name="doThis">The action to perform for every call</param>
public Callback AndAlways(Action<CallInfo> doThis)
{
SetAlwaysDo(doThis);
return this;
}
SetAlwaysDo(doThis);
return this;
}
}
Loading

0 comments on commit f470b5d

Please sign in to comment.