Converters are used to split a target into its component parts, then verify each of those parts.
When a target is split the result 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}
- tiff => png per page
- spreadsheet => csv per sheet
- pdf => png per page
Both the following examples take an input tiff and convert it to:
The info file:
{
PixelFormat: Format8bppIndexed,
Size: 473, 355
}
Multiple png files:
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);
});
using var stream = File.OpenRead("sample.tif");
await Verifier.Verify(Image.FromStream(stream));
Note that this sample also uses the optional canConvert
to ensure that only Image
s that are tiffs are converted.
canConvert: (target, extension, context) => Equals(target.RawFormat, ImageFormat.Tiff),
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);
});
await Verifier.VerifyFile("sample.tif");
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;
});
Converters can be shipped as NuGet packages:
- Verify.Aspose: Verification of documents (pdf, docx, xslx, and pptx) via Aspose.
- Verify.EntityFramework: Verification of EntityFramework bits.
- Verify.ImageMagick: Verification and comparison of images via Magick.NET.
- Verify.ImageSharp: Verification of images via ImageSharp.
- Verify.NServiceBus: Verify NServiceBus Test Contexts.
- Verify.RavenDb: Verification of RavenDb bits.
- Verify.SqlServer: Verification of SqlServer bits.
- Verify.Web: Verification of web bits.
- Verify.WinForms: Verification of WinForms UIs.
- Verify.Xaml: Verification of Xaml UIs.