Skip to content

Commit

Permalink
Nested converter support (#662)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonCropp authored Sep 24, 2022
1 parent 416e8be commit a8a0c3e
Show file tree
Hide file tree
Showing 10 changed files with 119 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[
level1Info,
level2Info
]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
text from level1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
text from level2
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Foo
Bar
Empty file.
39 changes: 39 additions & 0 deletions src/Verify.Tests/Converters/ExtensionConverterTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,45 @@
[UsesVerify]
public class ExtensionConverterTests
{
[ModuleInitializer]
public static void NestedInit()
{
VerifierSettings.RegisterFileConverter(
"level1",
(stream, _) =>
new(
"level1Info",
new List<Target>
{
new("txt", "text from level1"),
new("level2", stream)
}));
VerifierSettings.RegisterFileConverter(
"level2",
async (stream, _) =>
new(
"level2Info",
new List<Target>
{
new("txt", "text from level2"),
new("txt", await stream.ReadString())
}));
}

[Fact]
public Task Nested() =>
Verify(IoHelpers.OpenRead("sample.level1"));

[Fact]
public Task NestedTarget()
{
var targets = new[]
{
new Target("level1", new MemoryStream())
};
return Verify(targets);
}

[ModuleInitializer]
public static void TextSplitInit() =>
VerifierSettings.RegisterFileConverter(
Expand Down
3 changes: 3 additions & 0 deletions src/Verify.Tests/Verify.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@
<None Update="EmptyFiles\**\*.*">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="sample.level1">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
<Import Project="$(ProjectDir)..\Verify\buildTransitive\Verify.props" />
<Import Project="$(ProjectDir)..\Verify\buildTransitive\Verify.targets" />
Expand Down
2 changes: 2 additions & 0 deletions src/Verify.Tests/sample.level1
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Foo
Bar
3 changes: 3 additions & 0 deletions src/Verify/Splitters/Settings_Extension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ public static partial class VerifierSettings
internal static bool TryGetExtensionConverter(string extension, [NotNullWhen(true)] out AsyncConversion<Stream>? converter) =>
extensionConverters.TryGetValue(extension, out converter);

internal static bool HasExtensionConverter(string extension) =>
extensionConverters.ContainsKey(extension);

public static void RegisterFileConverter(
string fromExtension,
Conversion<Stream> conversion) =>
Expand Down
67 changes: 64 additions & 3 deletions src/Verify/Verifier/InnerVerifier_Stream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,33 @@ public async Task<VerifyResult> VerifyStream(Stream stream, string extension)
throw new("Empty data is not allowed.");
}

if (VerifierSettings.TryGetExtensionConverter(extension, out var conversion))
if (VerifierSettings.HasExtensionConverter(extension))
{
var result = await conversion(stream, settings.Context);
return await VerifyInner(result.Info, result.Cleanup, result.Targets);
var (infos, convertedTargets, cleanups) = await DoExtensionConversion(
new()
{
new(extension, stream)
});
object? info = null;
if (infos.Count == 1)
{
info = infos[0];
}
else if (infos.Count > 1)
{
info = infos;
}

return await VerifyInner(
info,
async () =>
{
foreach (var cleanup in cleanups)
{
await cleanup();
}
},
convertedTargets);
}

var targets = await GetTargets(stream, extension);
Expand All @@ -68,4 +91,42 @@ static async Task<List<Target>> GetTargets(Stream stream, string extension)
new(extension, stream)
};
}

async Task<(List<object> infos, List<Target> targets, List<Func<Task>> cleanups)> DoExtensionConversion(List<Target> list)
{
var infos = new List<object>();
var outputTargets = new List<Target>();
var cleanups = new List<Func<Task>>();

var queue = new Queue<Target>(list);

while (queue.Count > 0)
{
var target = queue.Dequeue();

if (!VerifierSettings.TryGetExtensionConverter(target.Extension, out var conversion))
{
outputTargets.Add(target);
continue;
}

var result = await conversion(target.StreamData, settings.Context);
if (result.Info != null)
{
infos.Add(result.Info);
}

if (result.Cleanup != null)
{
cleanups.Add(result.Cleanup);
}

foreach (var innerTarget in result.Targets)
{
queue.Enqueue(innerTarget);
}
}

return (infos, outputTargets, cleanups);
}
}

0 comments on commit a8a0c3e

Please sign in to comment.