Skip to content

Latest commit

 

History

History
181 lines (141 loc) · 6.97 KB

converter.md

File metadata and controls

181 lines (141 loc) · 6.97 KB

Converters

Converters are used to split a target into its component parts, then verify each of those parts.

When a target is split the result is:

  • An info file (containing the metadata of the target) serialized as json. File name: {TestType}.{TestMethod}.info.verified.txt
  • Zero or more documents of a specified extension. File name: {TestType}.{TestMethod}.{Index}.verified.{Extension}

Usage scenarios

  • tiff => png per page
  • spreadsheet => csv per sheet
  • pdf => png per page

Example

Both the following examples take an input tiff and convert it to:

The info file:

{
  PixelFormat: Format8bppIndexed,
  Size: 473, 355
}

snippet source | anchor

Multiple png files:

Converter page one verifiedConverter page one verified

Typed converter

This sample uses a typed approach. So the converter acts on an in memory instance matching based on type.

VerifierSettings.RegisterFileConverter<Image>(
    canConvert: (target, extension, context) => Equals(target.RawFormat, ImageFormat.Tiff),
    conversion: (image, settings) =>
    {
        var pages = image.GetFrameCount(FrameDimension.Page);

        var targets = new List<Target>();
        for (var index = 0; index < pages; index++)
        {
            image.SelectActiveFrame(FrameDimension.Page, index);

            var page = new MemoryStream();
            image.Save(page, ImageFormat.Png);
            targets.Add(new("png", page));
        }

        return new(
            info: new
            {
                image.PixelFormat,
                image.Size
            },
            targets);
    });

snippet source | anchor

using var stream = File.OpenRead("sample.tif");
await Verify(Image.FromStream(stream));

snippet source | anchor

Note that this sample also uses the optional canConvert to ensure that only Images that are tiffs are converted.

canConvert: (target, extension, context) => Equals(target.RawFormat, ImageFormat.Tiff),

snippet source | anchor

Expression converter

This sample uses a extension approach. So the converter acts on a file or stream based on the extension (configured or detected).

VerifierSettings.RegisterFileConverter(
    fromExtension: "tif",
    conversion: (stream, settings) =>
    {
        using var image = Image.FromStream(stream);
        var pages = image.GetFrameCount(FrameDimension.Page);

        var targets = new List<Target>();
        for (var index = 0; index < pages; index++)
        {
            image.SelectActiveFrame(FrameDimension.Page, index);

            var page = new MemoryStream();
            image.Save(page, ImageFormat.Png);
            targets.Add(new("png", page));
        }

        return new(
            info: new
            {
                image.PixelFormat,
                image.Size
            },
            targets);
    });

snippet source | anchor

await VerifyFile("sample.tif");

snippet source | anchor

Cleanup

If cleanup needs to occur after verification a callback can be passes to ConversionResult:

return new(
    info: info,
    "bin",
    stream: File.OpenRead(filePath),
    cleanup: () =>
    {
        File.Delete(filePath);
        return Task.CompletedTask;
    });

snippet source | anchor

Shipping

Converters can be shipped as NuGet packages: