Skip to content

Commit

Permalink
Remove ability to reset persistent vias
Browse files Browse the repository at this point in the history
  • Loading branch information
devodo committed Mar 17, 2023
1 parent 52b55da commit d4398cb
Show file tree
Hide file tree
Showing 14 changed files with 48 additions and 142 deletions.
8 changes: 4 additions & 4 deletions src/DivertR/Diverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,17 +130,17 @@ public IDiverter Strict(string? name = null)
}

/// <inheritdoc />
public IDiverter ResetAll(bool includePersistent = false)
public IDiverter ResetAll()
{
RedirectSet.ResetAll(includePersistent);
RedirectSet.ResetAll();

return this;
}

/// <inheritdoc />
public IDiverter Reset(string? name = null, bool includePersistent = false)
public IDiverter Reset(string? name = null)
{
RedirectSet.Reset(name, includePersistent);
RedirectSet.Reset(name);

return this;
}
Expand Down
8 changes: 0 additions & 8 deletions src/DivertR/IActionRedirectCall.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,6 @@

namespace DivertR
{
public interface IActionRedirectCall : IRedirectCall
{
new void CallNext();
new void CallNext(CallArguments args);
new void CallRoot();
new void CallRoot(CallArguments args);
}

public interface IActionRedirectCall<TTarget> : IRedirectCall<TTarget> where TTarget : class?
{
}
Expand Down
6 changes: 2 additions & 4 deletions src/DivertR/IDiverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,16 +113,14 @@ public interface IDiverter
/// <summary>
/// Reset all <see cref="IRedirect"/> instances in the underlying <see cref="IRedirectSet"/>.
/// </summary>
/// <param name="includePersistent">Optionally also reset persistent <see cref="IVia"/> registrations.</param>
/// <returns>This <see cref="IDiverter"/> instance.</returns>
IDiverter ResetAll(bool includePersistent = false);
IDiverter ResetAll();

/// <summary>
/// Reset an <see cref="IRedirect"/> group in the underlying <see cref="IRedirectSet"/> set with name equal to <paramref name="name"/>.
/// </summary>
/// <param name="name">The <see cref="RedirectId.Name" /> of the <see cref="IRedirect"/> group.</param>
/// <param name="includePersistent">Optionally also reset persistent <see cref="IVia"/> registrations.</param>
/// <returns>This <see cref="IDiverter"/> instance.</returns>
IDiverter Reset(string? name = null, bool includePersistent = false);
IDiverter Reset(string? name = null);
}
}
6 changes: 2 additions & 4 deletions src/DivertR/IRedirect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,8 @@ public interface IRedirect
/// <summary>
/// Reset the Redirect's <see cref="IRedirectRepository" /> to its initial state.
/// </summary>
/// <param name="includePersistent">Optionally also reset persistent <see cref="IVia"/>s.</param>
/// <returns>This Redirect instance.</returns>
IRedirect Reset(bool includePersistent = false);
IRedirect Reset();

/// <summary>
/// Set strict mode on the Redirect.
Expand Down Expand Up @@ -139,9 +138,8 @@ public interface IRedirect<TTarget> : IRedirect where TTarget : class?
/// <summary>
/// Reset the Redirect's <see cref="IRedirectRepository" /> to its initial state.
/// </summary>
/// <param name="includePersistent">Optionally also reset persistent <see cref="IVia"/>s.</param>
/// <returns>This Redirect instance.</returns>
new IRedirect<TTarget> Reset(bool includePersistent = false);
new IRedirect<TTarget> Reset();

/// <summary>
/// Set strict mode on the Redirect.
Expand Down
12 changes: 4 additions & 8 deletions src/DivertR/IRedirectRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,33 +41,29 @@ IRedirectPlan RedirectPlan
/// <summary>
/// Reset the <see cref="RedirectPlan" /> to its initial state.
/// </summary>
/// <param name="includePersistent">Optionally also reset persistent <see cref="IVia"/>s.</param>
/// <returns>This <see cref="IRedirectRepository"/> instance.</returns>
IRedirectRepository Reset(bool includePersistent = false);
IRedirectRepository Reset();

/// <summary>
/// Reset the <see cref="RedirectPlan" /> and atomically insert the given <paramref name="via"/>.
/// </summary>
/// <param name="via">The Via instance to insert after reset.</param>
/// <param name="includePersistent">Optionally also reset persistent <see cref="IVia"/>s.</param>
/// <returns>This <see cref="IRedirectRepository"/> instance.</returns>
IRedirectRepository ResetAndInsert(IVia via, bool includePersistent = false);
IRedirectRepository ResetAndInsert(IVia via);

/// <summary>
/// Resets the <see cref="RedirectPlan" /> and atomically insert the given <paramref name="via"/>.
/// </summary>
/// <param name="via">The Via instance to insert after reset.</param>
/// <param name="viaOptions">The Via options.</param>
/// <param name="includePersistent">Optionally also reset persistent <see cref="IVia"/>s.</param>
/// <returns>This <see cref="IRedirectRepository"/> instance.</returns>
IRedirectRepository ResetAndInsert(IVia via, ViaOptions viaOptions, bool includePersistent = false);
IRedirectRepository ResetAndInsert(IVia via, ViaOptions viaOptions);

/// <summary>
/// Reset the <see cref="RedirectPlan" /> and atomically insert the given <paramref name="configuredVia"/>.
/// </summary>
/// <param name="configuredVia">The configured Via instance to insert after reset.</param>
/// <param name="includePersistent">Optionally also reset persistent <see cref="IVia"/>s.</param>
/// <returns>This <see cref="IRedirectRepository"/> instance.</returns>
IRedirectRepository ResetAndInsert(IConfiguredVia configuredVia, bool includePersistent = false);
IRedirectRepository ResetAndInsert(IConfiguredVia configuredVia);
}
}
6 changes: 2 additions & 4 deletions src/DivertR/IRedirectSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,14 @@ public interface IRedirectSet
/// Reset an <see cref="IRedirect"/> group in this set with name equal to <paramref name="name"/>.
/// </summary>
/// <param name="name">The <see cref="RedirectId.Name" /> of the <see cref="IRedirect"/> group.</param>
/// <param name="includePersistent">Optionally also reset persistent <see cref="IVia"/> registrations.</param>
/// <returns>This <see cref="IRedirectSet"/> instance.</returns>
IRedirectSet Reset(string? name = null, bool includePersistent = false);
IRedirectSet Reset(string? name = null);

/// <summary>
/// Reset all <see cref="IRedirect"/> instances in this set.
/// </summary>
/// <param name="includePersistent">Optionally also reset persistent <see cref="IVia"/> registrations.</param>
/// <returns>This <see cref="IRedirectSet"/> instance.</returns>
IRedirectSet ResetAll(bool includePersistent = false);
IRedirectSet ResetAll();

/// <summary>
/// Enable strict mode on an <see cref="IRedirect"/> group in this set with name equal to <paramref name="name"/>.
Expand Down
6 changes: 2 additions & 4 deletions src/DivertR/ISpy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,8 @@ public interface ISpy : IRedirect
/// <summary>
/// Reset the Spy's <see cref="IRedirectRepository" /> to its initial state.
/// </summary>
/// <param name="includePersistent">Optionally also reset persistent <see cref="IVia"/>s.</param>
/// <returns>This Spy instance.</returns>
new ISpy Reset(bool includePersistent = false);
new ISpy Reset();

/// <summary>
/// Set strict mode on the Spy.
Expand Down Expand Up @@ -76,9 +75,8 @@ public interface ISpy<TTarget> : IRedirect<TTarget>, ISpy where TTarget : class?
/// <summary>
/// Reset the Spy's <see cref="IRedirectRepository" /> to its initial state.
/// </summary>
/// <param name="includePersistent">Optionally also reset persistent <see cref="IVia"/>s.</param>
/// <returns>This Spy instance.</returns>
new ISpy<TTarget> Reset(bool includePersistent = false);
new ISpy<TTarget> Reset();

/// <summary>
/// Set strict mode on the Spy.
Expand Down
31 changes: 13 additions & 18 deletions src/DivertR/Internal/RedirectRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,16 @@ namespace DivertR.Internal
internal class RedirectRepository : IRedirectRepository
{
private readonly ConcurrentStack<RedirectPlan> _planStack = new();
private RedirectPlan _persistentPlan = Internal.RedirectPlan.Empty;
private RedirectPlan _persistentPlan;
private readonly object _lockObject = new();

public RedirectRepository()
public RedirectRepository() : this(Internal.RedirectPlan.Empty)
{
}

public RedirectRepository(RedirectPlan initialPlan)
{
_persistentPlan = initialPlan;
_planStack.Push(_persistentPlan);
}

Expand Down Expand Up @@ -70,43 +75,33 @@ public IRedirectRepository SetStrictMode(bool isStrict = true)
return this;
}

public IRedirectRepository Reset(bool includePersistent = false)
public IRedirectRepository Reset()
{
lock (_lockObject)
{
if (includePersistent)
{
_persistentPlan = Internal.RedirectPlan.Empty;
}

_planStack.Clear();
_planStack.Push(_persistentPlan);
}

return this;
}

public IRedirectRepository ResetAndInsert(IVia via, bool includePersistent = false)
public IRedirectRepository ResetAndInsert(IVia via)
{
return ResetAndInsert(via, ViaOptions.Default, includePersistent);
return ResetAndInsert(via, ViaOptions.Default);
}

public IRedirectRepository ResetAndInsert(IVia via, ViaOptions viaOptions, bool includePersistent = false)
public IRedirectRepository ResetAndInsert(IVia via, ViaOptions viaOptions)
{
var configuredVia = new ConfiguredVia(via, viaOptions);

return ResetAndInsert(configuredVia, includePersistent);
return ResetAndInsert(configuredVia);
}

public IRedirectRepository ResetAndInsert(IConfiguredVia configuredVia, bool includePersistent = false)
public IRedirectRepository ResetAndInsert(IConfiguredVia configuredVia)
{
lock (_lockObject)
{
if (includePersistent)
{
_persistentPlan = Internal.RedirectPlan.Empty;
}

var resetPlan = _persistentPlan.InsertVia(configuredVia);

if (configuredVia.Options.IsPersistent)
Expand Down
12 changes: 6 additions & 6 deletions src/DivertR/Redirect.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,9 @@ IRedirect IRedirect.Via(IVia via, Action<IViaOptionsBuilder>? optionsAction)
}

/// <inheritdoc />
IRedirect IRedirect.Reset(bool includePersistent)
IRedirect IRedirect.Reset()
{
return Reset(includePersistent);
return Reset();
}

/// <inheritdoc />
Expand All @@ -151,9 +151,9 @@ IRedirect IRedirect.Strict(bool? isStrict)
}

/// <inheritdoc />
public IRedirect<TTarget> Reset(bool includePersistent = false)
public IRedirect<TTarget> Reset()
{
ResetInternal(includePersistent);
ResetInternal();

return this;
}
Expand Down Expand Up @@ -204,9 +204,9 @@ public IActionRedirectUpdater<TTarget> ToSet<TProperty>(Expression<Func<TTarget,
return new ActionRedirectUpdater<TTarget>(this, ViaBuilder<TTarget>.ToSetInternal(memberExpression, constraintExpression));
}

protected virtual void ResetInternal(bool includePersistent)
protected virtual void ResetInternal()
{
RedirectRepository.Reset(includePersistent);
RedirectRepository.Reset();
}
}
}
8 changes: 4 additions & 4 deletions src/DivertR/RedirectSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,25 +81,25 @@ IRedirect CreateRedirect(Type type)
}

/// <inheritdoc />
public IRedirectSet Reset(string? name = null, bool includePersistent = false)
public IRedirectSet Reset(string? name = null)
{
var redirectGroup = GetRedirectGroup(name);
foreach (var redirect in redirectGroup.Values)
{
redirect.Reset(includePersistent);
redirect.Reset();
}

return this;
}

/// <inheritdoc />
public IRedirectSet ResetAll(bool includePersistent = false)
public IRedirectSet ResetAll()
{
foreach (var redirectGroup in _redirectGroups.Values)
{
foreach (var redirect in redirectGroup.Values)
{
redirect.Reset(includePersistent);
redirect.Reset();
}
}

Expand Down
18 changes: 9 additions & 9 deletions src/DivertR/Spy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ private Spy(DiverterSettings? diverterSettings, TTarget? root, bool hasRoot)
{
Mock = hasRoot ? Proxy() : Proxy(root);
Spy.AddSpy(this, Mock);
ResetAndConfigureRecord(false);
ResetAndConfigureRecord();
}

/// <inheritdoc />
Expand All @@ -48,9 +48,9 @@ private Spy(DiverterSettings? diverterSettings, TTarget? root, bool hasRoot)
return this;
}

ISpy ISpy.Reset(bool includePersistent)
ISpy ISpy.Reset()
{
base.Reset(includePersistent);
base.Reset();

return this;
}
Expand All @@ -70,9 +70,9 @@ ISpy ISpy.Via(IVia via, Action<IViaOptionsBuilder>? optionsAction)
}

/// <inheritdoc />
public new ISpy<TTarget> Reset(bool includePersistent = false)
public new ISpy<TTarget> Reset()
{
base.Reset(includePersistent);
base.Reset();

return this;
}
Expand All @@ -93,19 +93,19 @@ ISpy ISpy.Via(IVia via, Action<IViaOptionsBuilder>? optionsAction)
return this;
}

protected override void ResetInternal(bool includePersistent)
protected override void ResetInternal()
{
ResetAndConfigureRecord(includePersistent);
ResetAndConfigureRecord();
}

private void ResetAndConfigureRecord(bool includePersistent)
private void ResetAndConfigureRecord()
{
var recordHandler = new RecordCallHandler<TTarget>();
// Only record calls going to the Mock proxy
var callConstraint = new CallConstraint<TTarget>(call => ReferenceEquals(call.Proxy, Mock));
var via = ViaBuilder<TTarget>.To(callConstraint).Build(recordHandler);
var options = ViaOptionsBuilder.Create(opt => opt.OrderFirst(), disableSatisfyStrict: true);
RedirectRepository.ResetAndInsert(via, options, includePersistent);
RedirectRepository.ResetAndInsert(via, options);
CallsLocked = recordHandler.RecordStream;
}

Expand Down
Loading

0 comments on commit d4398cb

Please sign in to comment.