Skip to content

Commit

Permalink
Add Replay ValueTuple overrides
Browse files Browse the repository at this point in the history
  • Loading branch information
devodo committed May 19, 2022
1 parent 4a13238 commit 55245c5
Show file tree
Hide file tree
Showing 10 changed files with 200 additions and 192 deletions.
13 changes: 11 additions & 2 deletions src/DivertR/Record/IActionCallStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ IActionCallStream<TTarget, TArgs> WithArgs<TArgs>()
IReplayResult Replay(Action<IRecordedCall<TTarget>, CallArguments, int> visitor);
Task<IReplayResult> Replay(Func<IRecordedCall<TTarget>, CallArguments, Task> visitor);
Task<IReplayResult> Replay(Func<IRecordedCall<TTarget>, CallArguments, int, Task> visitor);

IReplayResult Replay<TArgs>(Action<IRecordedCall<TTarget, TArgs>> visitor) where TArgs : struct, IStructuralComparable, IStructuralEquatable, IComparable;
IReplayResult Replay<TArgs>(Action<IRecordedCall<TTarget, TArgs>, TArgs> visitor) where TArgs : struct, IStructuralComparable, IStructuralEquatable, IComparable;
IReplayResult Replay<TArgs>(Action<IRecordedCall<TTarget, TArgs>, int> visitor) where TArgs : struct, IStructuralComparable, IStructuralEquatable, IComparable;
IReplayResult Replay<TArgs>(Action<IRecordedCall<TTarget, TArgs>, TArgs, int> visitor) where TArgs : struct, IStructuralComparable, IStructuralEquatable, IComparable;
Task<IReplayResult> Replay<TArgs>(Func<IRecordedCall<TTarget, TArgs>, Task> visitor) where TArgs : struct, IStructuralComparable, IStructuralEquatable, IComparable;
Task<IReplayResult> Replay<TArgs>(Func<IRecordedCall<TTarget, TArgs>, TArgs, Task> visitor) where TArgs : struct, IStructuralComparable, IStructuralEquatable, IComparable;
Task<IReplayResult> Replay<TArgs>(Func<IRecordedCall<TTarget, TArgs>, int, Task> visitor) where TArgs : struct, IStructuralComparable, IStructuralEquatable, IComparable;
Task<IReplayResult> Replay<TArgs>(Func<IRecordedCall<TTarget, TArgs>, TArgs, int, Task> visitor) where TArgs : struct, IStructuralComparable, IStructuralEquatable, IComparable;
}

public interface IActionCallStream<TTarget, out TArgs> : ICallStream<IRecordedCall<TTarget, TArgs>>
Expand All @@ -26,9 +35,9 @@ IActionCallStream<TTarget, TNewArgs> WithArgs<TNewArgs>()

ICallStream<TMap> Map<TMap>(Func<IRecordedCall<TTarget, TArgs>, TArgs, TMap> mapper);

IReplayResult Replay(Action<IRecordedCall<TTarget>, TArgs> visitor);
IReplayResult Replay(Action<IRecordedCall<TTarget, TArgs>, TArgs> visitor);
IReplayResult Replay(Action<IRecordedCall<TTarget, TArgs>, TArgs, int> visitor);
Task<IReplayResult> Replay(Func<IRecordedCall<TTarget>, TArgs, Task> visitor);
Task<IReplayResult> Replay(Func<IRecordedCall<TTarget, TArgs>, TArgs, Task> visitor);
Task<IReplayResult> Replay(Func<IRecordedCall<TTarget, TArgs>, TArgs, int, Task> visitor);
}
}
15 changes: 12 additions & 3 deletions src/DivertR/Record/IFuncCallStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ IFuncCallStream<TTarget, TReturn, TArgs> WithArgs<TArgs>()
IReplayResult Replay(Action<IFuncRecordedCall<TTarget, TReturn>, CallArguments, int> visitor);
Task<IReplayResult> Replay(Func<IFuncRecordedCall<TTarget, TReturn>, CallArguments, Task> visitor);
Task<IReplayResult> Replay(Func<IFuncRecordedCall<TTarget, TReturn>, CallArguments, int, Task> visitor);

IReplayResult Replay<TArgs>(Action<IFuncRecordedCall<TTarget, TReturn, TArgs>> visitor) where TArgs : struct, IStructuralComparable, IStructuralEquatable, IComparable;
IReplayResult Replay<TArgs>(Action<IFuncRecordedCall<TTarget, TReturn, TArgs>, TArgs> visitor) where TArgs : struct, IStructuralComparable, IStructuralEquatable, IComparable;
IReplayResult Replay<TArgs>(Action<IFuncRecordedCall<TTarget, TReturn, TArgs>, int> visitor) where TArgs : struct, IStructuralComparable, IStructuralEquatable, IComparable;
IReplayResult Replay<TArgs>(Action<IFuncRecordedCall<TTarget, TReturn, TArgs>, TArgs, int> visitor) where TArgs : struct, IStructuralComparable, IStructuralEquatable, IComparable;
Task<IReplayResult> Replay<TArgs>(Func<IFuncRecordedCall<TTarget, TReturn, TArgs>, Task> visitor) where TArgs : struct, IStructuralComparable, IStructuralEquatable, IComparable;
Task<IReplayResult> Replay<TArgs>(Func<IFuncRecordedCall<TTarget, TReturn, TArgs>, TArgs, Task> visitor) where TArgs : struct, IStructuralComparable, IStructuralEquatable, IComparable;
Task<IReplayResult> Replay<TArgs>(Func<IFuncRecordedCall<TTarget, TReturn, TArgs>, int, Task> visitor) where TArgs : struct, IStructuralComparable, IStructuralEquatable, IComparable;
Task<IReplayResult> Replay<TArgs>(Func<IFuncRecordedCall<TTarget, TReturn, TArgs>, TArgs, int, Task> visitor) where TArgs : struct, IStructuralComparable, IStructuralEquatable, IComparable;
}

public interface IFuncCallStream<TTarget, out TReturn, out TArgs> : ICallStream<IFuncRecordedCall<TTarget, TReturn, TArgs>>
Expand All @@ -25,10 +34,10 @@ IFuncCallStream<TTarget, TReturn, TNewArgs> WithArgs<TNewArgs>()
where TNewArgs : struct, IStructuralComparable, IStructuralEquatable, IComparable;

ICallStream<TMap> Map<TMap>(Func<IFuncRecordedCall<TTarget, TReturn, TArgs>, TArgs, TMap> mapper);

IReplayResult Replay(Action<IFuncRecordedCall<TTarget, TReturn>, TArgs> visitor);
IReplayResult Replay(Action<IFuncRecordedCall<TTarget, TReturn, TArgs>, TArgs> visitor);
IReplayResult Replay(Action<IFuncRecordedCall<TTarget, TReturn, TArgs>, TArgs, int> visitor);
Task<IReplayResult> Replay(Func<IFuncRecordedCall<TTarget, TReturn>, TArgs, Task> visitor);
Task<IReplayResult> Replay(Func<IFuncRecordedCall<TTarget, TReturn, TArgs>, TArgs, Task> visitor);
Task<IReplayResult> Replay(Func<IFuncRecordedCall<TTarget, TReturn, TArgs>, TArgs, int, Task> visitor);
}
}
1 change: 0 additions & 1 deletion src/DivertR/Record/IFuncRecordedCall.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,5 @@ public interface IFuncRecordedCall<TTarget, out TReturn> : IRecordedCall<TTarget
public interface IFuncRecordedCall<TTarget, out TReturn, out TArgs> : IFuncRecordedCall<TTarget, TReturn>, IRecordedCall<TTarget, TArgs>
where TTarget : class
{
new TArgs Args { get; }
}
}
8 changes: 2 additions & 6 deletions src/DivertR/Record/IRecordedCall.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
namespace DivertR.Record
{
public interface IRecordedCall
public interface IRecordedCall<TTarget> where TTarget : class
{
CallInfo<TTarget> CallInfo { get; }
CallArguments Args { get; }
ICallReturn? Returned { get; }
}

public interface IRecordedCall<TTarget> : IRecordedCall where TTarget : class
{
CallInfo<TTarget> CallInfo { get; }
}

public interface IRecordedCall<TTarget, out TArgs> : IRecordedCall<TTarget>
where TTarget : class
{
Expand Down
108 changes: 24 additions & 84 deletions src/DivertR/Record/Internal/ActionCallStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace DivertR.Record.Internal
{
internal class ActionCallStream<TTarget> : CallStream<IRecordedCall<TTarget>>, IActionCallStream<TTarget> where TTarget : class
internal class ActionCallStream<TTarget> : CallStream<IRecordedCall<TTarget>, TTarget>, IActionCallStream<TTarget> where TTarget : class
{
protected readonly ParsedCallExpression ParsedCallExpression;

Expand All @@ -26,54 +26,44 @@ public IActionCallStream<TTarget, TArgs> WithArgs<TArgs>() where TArgs : struct,
return new ActionCallStream<TTarget, TArgs>(mappedCall, ParsedCallExpression);
}

public ICallStream<TMap> Map<TMap>(Func<IRecordedCall<TTarget>, CallArguments, TMap> mapper)
public IReplayResult Replay<TArgs>(Action<IRecordedCall<TTarget, TArgs>> visitor) where TArgs : struct, IStructuralComparable, IStructuralEquatable, IComparable
{
return new CallStream<TMap>(Calls.Select(call => mapper.Invoke(call, call.Args)));
return WithArgs<TArgs>().Replay(visitor);
}

public IReplayResult Replay(Action<IRecordedCall<TTarget>, CallArguments> visitor)
public IReplayResult Replay<TArgs>(Action<IRecordedCall<TTarget, TArgs>, TArgs> visitor) where TArgs : struct, IStructuralComparable, IStructuralEquatable, IComparable
{
return new ReplayResult(Calls.Select(call =>
{
visitor.Invoke(call, call.Args);

return call;
}).Count());
return WithArgs<TArgs>().Replay(visitor);
}

public IReplayResult Replay(Action<IRecordedCall<TTarget>, CallArguments, int> visitor)
public IReplayResult Replay<TArgs>(Action<IRecordedCall<TTarget, TArgs>, int> visitor) where TArgs : struct, IStructuralComparable, IStructuralEquatable, IComparable
{
return new ReplayResult(Calls.Select((call, i) =>
{
visitor.Invoke(call, call.Args, i);
return WithArgs<TArgs>().Replay(visitor);
}

return call;
}).Count());
public IReplayResult Replay<TArgs>(Action<IRecordedCall<TTarget, TArgs>, TArgs, int> visitor) where TArgs : struct, IStructuralComparable, IStructuralEquatable, IComparable
{
return WithArgs<TArgs>().Replay(visitor);
}

public async Task<IReplayResult> Replay(Func<IRecordedCall<TTarget>, CallArguments, Task> visitor)
public Task<IReplayResult> Replay<TArgs>(Func<IRecordedCall<TTarget, TArgs>, Task> visitor) where TArgs : struct, IStructuralComparable, IStructuralEquatable, IComparable
{
var count = 0;

foreach (var call in Calls)
{
await visitor.Invoke(call, call.Args);
count++;
}
return WithArgs<TArgs>().Replay(visitor);
}

return new ReplayResult(count);
public Task<IReplayResult> Replay<TArgs>(Func<IRecordedCall<TTarget, TArgs>, TArgs, Task> visitor) where TArgs : struct, IStructuralComparable, IStructuralEquatable, IComparable
{
return WithArgs<TArgs>().Replay(visitor);
}

public async Task<IReplayResult> Replay(Func<IRecordedCall<TTarget>, CallArguments, int, Task> visitor)
public Task<IReplayResult> Replay<TArgs>(Func<IRecordedCall<TTarget, TArgs>, int, Task> visitor) where TArgs : struct, IStructuralComparable, IStructuralEquatable, IComparable
{
var count = 0;

foreach (var call in Calls)
{
await visitor.Invoke(call, call.Args, count++);
}
return WithArgs<TArgs>().Replay(visitor);
}

return new ReplayResult(count);
public Task<IReplayResult> Replay<TArgs>(Func<IRecordedCall<TTarget, TArgs>, TArgs, int, Task> visitor) where TArgs : struct, IStructuralComparable, IStructuralEquatable, IComparable
{
return WithArgs<TArgs>().Replay(visitor);
}

internal static IEnumerable<IRecordedCall<TTarget, TArgs>> MapCalls<TArgs>(IEnumerable<IRecordedCall<TTarget>> calls, IValueTupleMapper valueTupleMapper)
Expand All @@ -84,7 +74,7 @@ internal static IEnumerable<IRecordedCall<TTarget, TArgs>> MapCalls<TArgs>(IEnum
}
}

internal class ActionCallStream<TTarget, TArgs> : CallStream<IRecordedCall<TTarget, TArgs>>, IActionCallStream<TTarget, TArgs>
internal class ActionCallStream<TTarget, TArgs> : CallStream<IRecordedCall<TTarget, TArgs>, TTarget, TArgs>, IActionCallStream<TTarget, TArgs>
where TTarget : class
where TArgs : struct, IStructuralComparable, IStructuralEquatable, IComparable
{
Expand All @@ -105,55 +95,5 @@ public IActionCallStream<TTarget, TNewArgs> WithArgs<TNewArgs>()

return new ActionCallStream<TTarget, TNewArgs>(mappedCall, ParsedCallExpression);
}

public ICallStream<TMap> Map<TMap>(Func<IRecordedCall<TTarget, TArgs>, TArgs, TMap> mapper)
{
return new CallStream<TMap>(Calls.Select(call => mapper.Invoke(call, call.Args)));
}

public IReplayResult Replay(Action<IRecordedCall<TTarget>, TArgs> visitor)
{
return new ReplayResult(Calls.Select(call =>
{
visitor.Invoke(call, call.Args);

return call;
}).Count());
}

public IReplayResult Replay(Action<IRecordedCall<TTarget, TArgs>, TArgs, int> visitor)
{
return new ReplayResult(Calls.Select((call, i) =>
{
visitor.Invoke(call, call.Args, i);

return call;
}).Count());
}

public async Task<IReplayResult> Replay(Func<IRecordedCall<TTarget>, TArgs, Task> visitor)
{
var count = 0;

foreach (var call in Calls)
{
await visitor.Invoke(call, call.Args);
count++;
}

return new ReplayResult(count);
}

public async Task<IReplayResult> Replay(Func<IRecordedCall<TTarget, TArgs>, TArgs, int, Task> visitor)
{
var count = 0;

foreach (var call in Calls)
{
await visitor.Invoke(call, call.Args, count++);
}

return new ReplayResult(count);
}
}
}
118 changes: 118 additions & 0 deletions src/DivertR/Record/Internal/CallStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,122 @@ IEnumerator IEnumerable.GetEnumerator()
return GetEnumerator();
}
}

internal class CallStream<TRecordedCall, TTarget> : CallStream<TRecordedCall>
where TRecordedCall : IRecordedCall<TTarget>
where TTarget : class
{
protected CallStream(IEnumerable<TRecordedCall> calls) : base(calls)
{
}

public ICallStream<TMap> Map<TMap>(Func<TRecordedCall, CallArguments, TMap> mapper)
{
return new CallStream<TMap>(Calls.Select(call => mapper.Invoke(call, call.Args)));
}

public IReplayResult Replay(Action<TRecordedCall, CallArguments> visitor)
{
return new ReplayResult(Calls.Select(call =>
{
visitor.Invoke(call, call.Args);

return call;
}).Count());
}

public IReplayResult Replay(Action<TRecordedCall, CallArguments, int> visitor)
{
return new ReplayResult(Calls.Select((call, i) =>
{
visitor.Invoke(call, call.Args, i);

return call;
}).Count());
}

public async Task<IReplayResult> Replay(Func<TRecordedCall, CallArguments, Task> visitor)
{
var count = 0;

foreach (var call in Calls)
{
await visitor.Invoke(call, call.Args);
count++;
}

return new ReplayResult(count);
}

public async Task<IReplayResult> Replay(Func<TRecordedCall, CallArguments, int, Task> visitor)
{
var count = 0;

foreach (var call in Calls)
{
await visitor.Invoke(call, call.Args, count++);
}

return new ReplayResult(count);
}
}

internal class CallStream<TRecordedCall, TTarget, TArgs> : CallStream<TRecordedCall>
where TRecordedCall : IRecordedCall<TTarget, TArgs>
where TTarget : class
{
protected CallStream(IEnumerable<TRecordedCall> calls) : base(calls)
{
}

public ICallStream<TMap> Map<TMap>(Func<TRecordedCall, TArgs, TMap> mapper)
{
return new CallStream<TMap>(Calls.Select(call => mapper.Invoke(call, call.Args)));
}

public IReplayResult Replay(Action<TRecordedCall, TArgs> visitor)
{
return new ReplayResult(Calls.Select(call =>
{
visitor.Invoke(call, call.Args);

return call;
}).Count());
}

public IReplayResult Replay(Action<TRecordedCall, TArgs, int> visitor)
{
return new ReplayResult(Calls.Select((call, i) =>
{
visitor.Invoke(call, call.Args, i);

return call;
}).Count());
}

public async Task<IReplayResult> Replay(Func<TRecordedCall, TArgs, Task> visitor)
{
var count = 0;

foreach (var call in Calls)
{
await visitor.Invoke(call, call.Args);
count++;
}

return new ReplayResult(count);
}

public async Task<IReplayResult> Replay(Func<TRecordedCall, TArgs, int, Task> visitor)
{
var count = 0;

foreach (var call in Calls)
{
await visitor.Invoke(call, call.Args, count++);
}

return new ReplayResult(count);
}
}
}
Loading

0 comments on commit 55245c5

Please sign in to comment.