Skip to content

Commit

Permalink
make Combination play nice with recording (#1338)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonCropp authored Nov 1, 2024
1 parent 664a79a commit 590641c
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<Project>
<PropertyGroup>
<NoWarn>CS1591;CS0649;xUnit1026;xUnit1013;CS1573;VerifyTestsProjectDir;VerifySetParameters;PolyFillTargetsForNuget</NoWarn>
<Version>28.1.0</Version>
<Version>28.1.1</Version>
<ImplicitUsings>enable</ImplicitUsings>
<LangVersion>preview</LangVersion>
<AssemblyVersion>1.0.0</AssemblyVersion>
Expand Down
26 changes: 15 additions & 11 deletions src/Verify.Tests/CombinationTests.RecordingTest.verified.txt
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
{
target: {
1, Smith St : 1 Smith St,
1, Wallace St: 1 Wallace St,
10, Smith St : 10 Smith St,
10, Wallace St: 10 Wallace St
1, Smith St : {
target: 1 Smith St,
key: recorded 1 Smith St
},
key: [
1 Smith St,
1 Wallace St,
10 Smith St,
10 Wallace St
]
1, Wallace St: {
target: 1 Wallace St,
key: recorded 1 Wallace St
},
10, Smith St : {
target: 10 Smith St,
key: recorded 10 Smith St
},
10, Wallace St: {
target: 10 Wallace St,
key: recorded 10 Wallace St
}
}
2 changes: 1 addition & 1 deletion src/Verify.Tests/CombinationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public Task RecordingTest()
.Verify(
(param1, param2) =>
{
Recording.Add("key", $"{param1} {param2}");
Recording.Add("key", $"recorded {param1} {param2}");
return SimpleReturnMethod(param1, param2);
},
params1,
Expand Down
16 changes: 15 additions & 1 deletion src/Verify/Combinations/CombinationRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,14 @@ public CombinationRunner(bool? captureExceptions, List<IEnumerable<object?>> lis
Task<CombinationResults> RunWithReturn<TReturn>(Func<object?[], Task<TReturn>> method) =>
InnerRun(async keys =>
{
var value = await method(keys);
object? value = await method(keys);
if (Recording.IsRecording())
{
var appends = Recording.Values().ToList();
value = new InfoBuilder(value, appends);
Recording.Clear();
}

return (CombinationResult.ForValue(keys, value), value);
});

Expand All @@ -25,6 +32,13 @@ Task<CombinationResults> RunWithVoid(Func<object?[], Task> method) =>
InnerRun(async keys =>
{
await method(keys);
if (Recording.IsRecording())
{
var appends = Recording.Values().ToList();
var value = new InfoBuilder("void", appends);
Recording.Clear();
return (CombinationResult.ForValue(keys, value), null);
}
return (CombinationResult.ForVoid(keys), null);
});

Expand Down
24 changes: 24 additions & 0 deletions src/Verify/Recording/Recording.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,30 @@ public static bool TryStop([NotNullWhen(true)] out IReadOnlyCollection<ToAppend>
return true;
}

public static IReadOnlyCollection<ToAppend> Values()
{
if (TryGetValues(out var values))
{
return values;
}

throw new("Recording.Start must be called prior to Recording.Values.");
}

public static bool TryGetValues([NotNullWhen(true)] out IReadOnlyCollection<ToAppend>? recorded)
{
var value = asyncLocal.Value;

if (value == null)
{
recorded = null;
return false;
}

recorded = value.Items;
return true;
}

static State CurrentState([CallerMemberName] string caller = "")
{
var value = asyncLocal.Value;
Expand Down
2 changes: 1 addition & 1 deletion src/Verify/Serialization/InfoBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
object? root;
List<Item> inner = [];

public InfoBuilder(object? root, List<ToAppend> appends)
public InfoBuilder(object? root, IEnumerable<ToAppend> appends)
{
this.root = root;
foreach (var append in appends)
Expand Down

0 comments on commit 590641c

Please sign in to comment.