diff --git a/readme.md b/readme.md
index 4b14568..ba2b4ef 100644
--- a/readme.md
+++ b/readme.md
@@ -10,6 +10,16 @@ Extends [Verify](https://github.com/VerifyTests/Verify) to allow verification of
Converts documents pdfs to png for verification.
+Verifying a `pdf` produces:
+
+ * The pdf itself as `.verified.pdf`. This can be omitted with [`ExcludeTargets`](#choosing-what-is-verified).
+ * A `.verified.txt` with the page count.
+ * A png render of every page as `#page_0001.verified.png`, `#page_0002.verified.png`, etc.
+
+Verifying an `svg` produces the svg as `.verified.svg` and a png render of it as `.verified.png`.
+
+The page files are named by Verify's [paged documents](https://github.com/VerifyTests/Verify/blob/main/docs/paged-documents.md) support, which every Verify plugin that splits a document into pages shares. So do the settings that [choose what is verified](#choosing-what-is-verified).
+
Contains [comparers](https://github.com/VerifyTests/Verify/blob/master/docs/comparer.md) for png, jpg, bmp, and tiff.
@@ -50,25 +60,55 @@ public static void Init()
`Initialize` registers the pdf to png converter and all comparers.
-### Outputs
+### Choosing what is verified
-`Initialize` accepts an optional `ImageMagickOutputs` flags enum that controls, globally, which outputs a document is split into:
+What a pdf or an svg is split into is controlled by Verify's settings for [paged documents](https://github.com/VerifyTests/Verify/blob/main/docs/paged-documents.md), which replace the options this plugin had of its own.
- * `Png`: render pdf pages and svg documents to png.
- * `None`: none of the above. Only the source document is emitted.
- * `All`: all of the above. This is the default.
+`PagesToInclude` limits the pages of a pdf that are verified, to its first pages or to those a delegate accepts. The pdf itself is still verified whole, and the info file still holds the number of pages it has:
-Omitted outputs are skipped before any rendering, so no work is done for them. The source document target (pdf or svg) is always emitted; use `VerifierSettings.ExcludeTargets("pdf")` to exclude it. Png, webp, and tiff image files are not affected. To emit only the source document, pass `ImageMagickOutputs.None`:
+
+
+```cs
+[Test]
+public Task PagesToInclude() =>
+ VerifyFile("sample.pdf")
+ .PagesToInclude(1);
+```
+snippet source | anchor
+
+
+`ExcludeDerivedTargets("png")` leaves out the png renders, keeping the pdf or the svg. The render is skipped rather than dropped afterwards. The pages of a pdf are then only counted, so its info file still says how many there are:
+
+
+
+```cs
+[Test]
+public Task ExcludeDerivedTargets() =>
+ VerifyFile("sample.pdf")
+ .ExcludeDerivedTargets("png");
+```
+snippet source | anchor
+
+
+It applies to a png rendered from a pdf or an svg. A png, webp or tiff that is itself being verified is not affected.
+
+`ExcludeTargets("pdf")` leaves out the pdf, keeping its pages and its info file. A [password-protected pdf](#open-password-protected-pdfs) requires it.
+
+Each can also be set for every test, on `VerifierSettings`:
```cs
[ModuleInitializer]
-public static void Init() =>
- // Only emit the source document. Skip rendering to png.
- VerifyImageMagick.Initialize(ImageMagickOutputs.None);
+public static void Init()
+{
+ VerifyImageMagick.Initialize();
+
+ // For every test: no png, so only the source document is verified
+ VerifierSettings.ExcludeDerivedTargets("png");
+}
```
-snippet source | anchor
+snippet source | anchor
@@ -112,9 +152,21 @@ public Task VerifyPdfStream()
#### Result
-[Samples.VerifyPdf#00.verified.png](/src/Tests/Samples.VerifyPdf#00.verified.png):
+[Samples.VerifyPdf.verified.txt](/src/Tests/Samples.VerifyPdf.verified.txt):
+
+
+
+```txt
+{
+ PageCount: 2
+}
+```
+snippet source | anchor
+
-
+[Samples.VerifyPdf#page_0001.verified.png](/src/Tests/Samples.VerifyPdf%23page_0001.verified.png):
+
+
### Image Comparers
@@ -174,6 +226,41 @@ public Task PdfPassword() =>
snippet source | anchor
+
+## Reviewing changes
+
+A change to a pdf is a change to several files: the pdf, its info file, and every page. Verify tells the diff tool that the pages and the info file were derived from the pdf, and [DiffEngineViewer](https://github.com/VerifyTests/DiffEngine/blob/main/docs/viewer.md#files-derived-from-a-document), which draws a pdf's pages itself, shows them as one row and accepts them together. Other diff tools are given each file, as before. The png of an svg is derived from the svg in the same way.
+
+When the pdf or the svg has itself changed, what was derived from it is compared exactly, skipping any [comparer](#image-comparers) registered for png. ImageMagick writes the time into every png it renders, so every page of a changed pdf is then reported as changed, whether or not it draws differently.
+
+
+## Migrating from 3.x
+
+Version 4 moves to the paged document support in Verify 33.3. The options of this plugin that chose what was verified are now settings of Verify:
+
+| 3.x | 4.x |
+| --- | --- |
+| `Initialize(ImageMagickOutputs.None)` | `Initialize()` and `VerifierSettings.ExcludeDerivedTargets("png")` |
+| `Initialize(ImageMagickOutputs.Png)` or `Initialize(ImageMagickOutputs.All)` | `Initialize()` |
+| `.PagesToInclude(2)`, an extension method of this plugin | `.PagesToInclude(2)`, a member of Verify's `SettingsTask` and `VerifySettings` |
+
+A call to `PagesToInclude` compiles unchanged. It can now also take a delegate, and both it and `ExcludeDerivedTargets` can be set for one verification or, on `VerifierSettings`, for all of them.
+
+The page of a pdf is named by its number, which is 1 based. It was named by an index, which was 0 based and missing for a pdf with one page. A pdf also has an info file now, holding its page count:
+
+| 3.x | 4.x |
+| --- | --- |
+| `Tests.Report#00.verified.png` | `Tests.Report#page_0001.verified.png` |
+| `Tests.Report#01.verified.png` | `Tests.Report#page_0002.verified.png` |
+| `Tests.Report.verified.png`, of a pdf with one page | `Tests.Report#page_0001.verified.png` |
+| `Tests.Mail#Attachment.00.verified.png`, of a pdf that is a target named `Attachment` | `Tests.Mail#Attachment.page_0001.verified.png` |
+| | `Tests.Report.verified.txt`, the info file |
+
+The `.verified.pdf` is unchanged, and so are the `.verified.svg` and `.verified.png` of an svg.
+
+Renamed snapshots show as a new file and a pending delete. Accepting both, or running once with [AutoVerify](https://github.com/VerifyTests/Verify/blob/main/docs/autoverify.md), moves a test over. A page draws as it did, but ImageMagick writes the time into every png it renders, so an accepted page differs from the file it replaces by those bytes, and source control shows a rename with a small change.
+
+
## Icon
[Swirl](https://thenounproject.com/term/wizard/2744075/) designed by [Philipp Petzka](https://thenounproject.com/masteroficon) from [The Noun Project](https://thenounproject.com/).
diff --git a/src/Directory.Build.props b/src/Directory.Build.props
index 62c2fbe..2c1be63 100644
--- a/src/Directory.Build.props
+++ b/src/Directory.Build.props
@@ -2,7 +2,7 @@
CS1591
- 3.11.0
+ 4.0.01.0.0enablepreview
diff --git a/src/Directory.Packages.props b/src/Directory.Packages.props
index bd64149..529d794 100644
--- a/src/Directory.Packages.props
+++ b/src/Directory.Packages.props
@@ -12,10 +12,10 @@
-
-
+
+
-
+
diff --git a/src/StaticSettingsTests/ModuleInitializer.cs b/src/StaticSettingsTests/ModuleInitializer.cs
index dd0f665..124b2f1 100644
--- a/src/StaticSettingsTests/ModuleInitializer.cs
+++ b/src/StaticSettingsTests/ModuleInitializer.cs
@@ -3,9 +3,13 @@ public static class ModuleInitializer
#region InitializeOutputs
[ModuleInitializer]
- public static void Init() =>
- // Only emit the source document. Skip rendering to png.
- VerifyImageMagick.Initialize(ImageMagickOutputs.None);
+ public static void Init()
+ {
+ VerifyImageMagick.Initialize();
+
+ // For every test: no png, so only the source document is verified
+ VerifierSettings.ExcludeDerivedTargets("png");
+ }
#endregion
}
diff --git a/src/StaticSettingsTests/OutputsTests.PdfWithoutPng.verified.txt b/src/StaticSettingsTests/OutputsTests.PdfWithoutPng.verified.txt
new file mode 100644
index 0000000..02e2bb9
--- /dev/null
+++ b/src/StaticSettingsTests/OutputsTests.PdfWithoutPng.verified.txt
@@ -0,0 +1,3 @@
+{
+ PageCount: 2
+}
\ No newline at end of file
diff --git a/src/Tests/Ghostscript.cs b/src/Tests/Ghostscript.cs
index 2595272..0487f08 100644
--- a/src/Tests/Ghostscript.cs
+++ b/src/Tests/Ghostscript.cs
@@ -5,13 +5,13 @@ static class Ghostscript
// Replaces the pdf converter registered by Initialize with one that treats that as inconclusive,
// so pdf tests do not fail on machines that do not have Ghostscript installed.
public static void RegisterPdfConverter() =>
- VerifierSettings.RegisterStreamConverter("pdf", Convert);
+ VerifierSettings.RegisterStreamConverter("pdf", (_, stream, context) => Convert(stream, context));
- static ConversionResult Convert(string? name, Stream stream, IReadOnlyDictionary context)
+ static ConversionResult Convert(Stream stream, IReadOnlyDictionary context)
{
try
{
- return VerifyImageMagick.Convert(name, stream, context, MagickFormat.Pdf);
+ return VerifyImageMagick.Convert(stream, context, MagickFormat.Pdf);
}
catch (MagickDelegateErrorException exception)
when (exception.Message.Contains("(127)"))
diff --git a/src/Tests/PasswordSamples.PasswordSample#00.verified.png b/src/Tests/PasswordSamples.PasswordSample#page_0001.verified.png
similarity index 100%
rename from src/Tests/PasswordSamples.PasswordSample#00.verified.png
rename to src/Tests/PasswordSamples.PasswordSample#page_0001.verified.png
diff --git a/src/Tests/PasswordSamples.PasswordSample#01.verified.png b/src/Tests/PasswordSamples.PasswordSample#page_0002.verified.png
similarity index 100%
rename from src/Tests/PasswordSamples.PasswordSample#01.verified.png
rename to src/Tests/PasswordSamples.PasswordSample#page_0002.verified.png
diff --git a/src/Tests/PasswordSamples.PasswordSample.verified.txt b/src/Tests/PasswordSamples.PasswordSample.verified.txt
new file mode 100644
index 0000000..02e2bb9
--- /dev/null
+++ b/src/Tests/PasswordSamples.PasswordSample.verified.txt
@@ -0,0 +1,3 @@
+{
+ PageCount: 2
+}
\ No newline at end of file
diff --git a/src/Tests/Samples.ExcludeDerivedTargets.verified.pdf b/src/Tests/Samples.ExcludeDerivedTargets.verified.pdf
new file mode 100644
index 0000000..b6cdb4c
Binary files /dev/null and b/src/Tests/Samples.ExcludeDerivedTargets.verified.pdf differ
diff --git a/src/Tests/Samples.ExcludeDerivedTargets.verified.txt b/src/Tests/Samples.ExcludeDerivedTargets.verified.txt
new file mode 100644
index 0000000..02e2bb9
--- /dev/null
+++ b/src/Tests/Samples.ExcludeDerivedTargets.verified.txt
@@ -0,0 +1,3 @@
+{
+ PageCount: 2
+}
\ No newline at end of file
diff --git a/src/Tests/Samples.VerifyPdf#00.verified.png b/src/Tests/Samples.PagesToInclude#page_0001.verified.png
similarity index 100%
rename from src/Tests/Samples.VerifyPdf#00.verified.png
rename to src/Tests/Samples.PagesToInclude#page_0001.verified.png
diff --git a/src/Tests/Samples.PagesToInclude.verified.pdf b/src/Tests/Samples.PagesToInclude.verified.pdf
new file mode 100644
index 0000000..b6cdb4c
Binary files /dev/null and b/src/Tests/Samples.PagesToInclude.verified.pdf differ
diff --git a/src/Tests/Samples.PagesToInclude.verified.txt b/src/Tests/Samples.PagesToInclude.verified.txt
new file mode 100644
index 0000000..02e2bb9
--- /dev/null
+++ b/src/Tests/Samples.PagesToInclude.verified.txt
@@ -0,0 +1,3 @@
+{
+ PageCount: 2
+}
\ No newline at end of file
diff --git a/src/Tests/Samples.PdfPassword#00.verified.png b/src/Tests/Samples.PdfPassword#page_0001.verified.png
similarity index 100%
rename from src/Tests/Samples.PdfPassword#00.verified.png
rename to src/Tests/Samples.PdfPassword#page_0001.verified.png
diff --git a/src/Tests/Samples.PdfPassword#01.verified.png b/src/Tests/Samples.PdfPassword#page_0002.verified.png
similarity index 100%
rename from src/Tests/Samples.PdfPassword#01.verified.png
rename to src/Tests/Samples.PdfPassword#page_0002.verified.png
diff --git a/src/Tests/Samples.PdfPassword.verified.txt b/src/Tests/Samples.PdfPassword.verified.txt
new file mode 100644
index 0000000..02e2bb9
--- /dev/null
+++ b/src/Tests/Samples.PdfPassword.verified.txt
@@ -0,0 +1,3 @@
+{
+ PageCount: 2
+}
\ No newline at end of file
diff --git a/src/Tests/Samples.VerifyPdfStream#00.verified.png b/src/Tests/Samples.VerifyPdf#page_0001.verified.png
similarity index 100%
rename from src/Tests/Samples.VerifyPdfStream#00.verified.png
rename to src/Tests/Samples.VerifyPdf#page_0001.verified.png
diff --git a/src/Tests/Samples.VerifyPdf#01.verified.png b/src/Tests/Samples.VerifyPdf#page_0002.verified.png
similarity index 100%
rename from src/Tests/Samples.VerifyPdf#01.verified.png
rename to src/Tests/Samples.VerifyPdf#page_0002.verified.png
diff --git a/src/Tests/Samples.VerifyPdf.verified.txt b/src/Tests/Samples.VerifyPdf.verified.txt
new file mode 100644
index 0000000..02e2bb9
--- /dev/null
+++ b/src/Tests/Samples.VerifyPdf.verified.txt
@@ -0,0 +1,3 @@
+{
+ PageCount: 2
+}
\ No newline at end of file
diff --git a/src/Tests/Samples.VerifyPdfStream#page_0001.verified.png b/src/Tests/Samples.VerifyPdfStream#page_0001.verified.png
new file mode 100644
index 0000000..f6ae65e
Binary files /dev/null and b/src/Tests/Samples.VerifyPdfStream#page_0001.verified.png differ
diff --git a/src/Tests/Samples.VerifyPdfStream#01.verified.png b/src/Tests/Samples.VerifyPdfStream#page_0002.verified.png
similarity index 100%
rename from src/Tests/Samples.VerifyPdfStream#01.verified.png
rename to src/Tests/Samples.VerifyPdfStream#page_0002.verified.png
diff --git a/src/Tests/Samples.VerifyPdfStream.verified.txt b/src/Tests/Samples.VerifyPdfStream.verified.txt
new file mode 100644
index 0000000..02e2bb9
--- /dev/null
+++ b/src/Tests/Samples.VerifyPdfStream.verified.txt
@@ -0,0 +1,3 @@
+{
+ PageCount: 2
+}
\ No newline at end of file
diff --git a/src/Tests/Samples.cs b/src/Tests/Samples.cs
index bbe8d92..d3be37b 100644
--- a/src/Tests/Samples.cs
+++ b/src/Tests/Samples.cs
@@ -48,6 +48,24 @@ public Task VerifyPdfStream()
#endregion
+ #region PagesToInclude
+
+ [Test]
+ public Task PagesToInclude() =>
+ VerifyFile("sample.pdf")
+ .PagesToInclude(1);
+
+ #endregion
+
+ #region ExcludeDerivedTargets
+
+ [Test]
+ public Task ExcludeDerivedTargets() =>
+ VerifyFile("sample.pdf")
+ .ExcludeDerivedTargets("png");
+
+ #endregion
+
[Test]
public Task VerifySvg() =>
VerifyFile(ProjectFiles.sample_svg.Path);
diff --git a/src/Tests/SkipPdfNormalizationTests.NormalizedSnapshotHoldsTheNeutralizedBytes#00.verified.png b/src/Tests/SkipPdfNormalizationTests.NormalizedSnapshotHoldsTheNeutralizedBytes#page_0001.verified.png
similarity index 100%
rename from src/Tests/SkipPdfNormalizationTests.NormalizedSnapshotHoldsTheNeutralizedBytes#00.verified.png
rename to src/Tests/SkipPdfNormalizationTests.NormalizedSnapshotHoldsTheNeutralizedBytes#page_0001.verified.png
diff --git a/src/Tests/SkipPdfNormalizationTests.NormalizedSnapshotHoldsTheNeutralizedBytes#01.verified.png b/src/Tests/SkipPdfNormalizationTests.NormalizedSnapshotHoldsTheNeutralizedBytes#page_0002.verified.png
similarity index 100%
rename from src/Tests/SkipPdfNormalizationTests.NormalizedSnapshotHoldsTheNeutralizedBytes#01.verified.png
rename to src/Tests/SkipPdfNormalizationTests.NormalizedSnapshotHoldsTheNeutralizedBytes#page_0002.verified.png
diff --git a/src/Tests/SkipPdfNormalizationTests.NormalizedSnapshotHoldsTheNeutralizedBytes.verified.txt b/src/Tests/SkipPdfNormalizationTests.NormalizedSnapshotHoldsTheNeutralizedBytes.verified.txt
new file mode 100644
index 0000000..02e2bb9
--- /dev/null
+++ b/src/Tests/SkipPdfNormalizationTests.NormalizedSnapshotHoldsTheNeutralizedBytes.verified.txt
@@ -0,0 +1,3 @@
+{
+ PageCount: 2
+}
\ No newline at end of file
diff --git a/src/Tests/SkipPdfNormalizationTests.SkippedSnapshotHoldsTheProducerBytes#00.verified.png b/src/Tests/SkipPdfNormalizationTests.SkippedSnapshotHoldsTheProducerBytes#page_0001.verified.png
similarity index 100%
rename from src/Tests/SkipPdfNormalizationTests.SkippedSnapshotHoldsTheProducerBytes#00.verified.png
rename to src/Tests/SkipPdfNormalizationTests.SkippedSnapshotHoldsTheProducerBytes#page_0001.verified.png
diff --git a/src/Tests/SkipPdfNormalizationTests.SkippedSnapshotHoldsTheProducerBytes#01.verified.png b/src/Tests/SkipPdfNormalizationTests.SkippedSnapshotHoldsTheProducerBytes#page_0002.verified.png
similarity index 100%
rename from src/Tests/SkipPdfNormalizationTests.SkippedSnapshotHoldsTheProducerBytes#01.verified.png
rename to src/Tests/SkipPdfNormalizationTests.SkippedSnapshotHoldsTheProducerBytes#page_0002.verified.png
diff --git a/src/Tests/SkipPdfNormalizationTests.SkippedSnapshotHoldsTheProducerBytes.verified.txt b/src/Tests/SkipPdfNormalizationTests.SkippedSnapshotHoldsTheProducerBytes.verified.txt
new file mode 100644
index 0000000..02e2bb9
--- /dev/null
+++ b/src/Tests/SkipPdfNormalizationTests.SkippedSnapshotHoldsTheProducerBytes.verified.txt
@@ -0,0 +1,3 @@
+{
+ PageCount: 2
+}
\ No newline at end of file
diff --git a/src/Tests/Tests.SkipPdfNormalization#01.verified.png b/src/Tests/Tests.PagesToInclude#page_0002.verified.png
similarity index 100%
rename from src/Tests/Tests.SkipPdfNormalization#01.verified.png
rename to src/Tests/Tests.PagesToInclude#page_0002.verified.png
diff --git a/src/Tests/Tests.PagesToInclude.verified.pdf b/src/Tests/Tests.PagesToInclude.verified.pdf
new file mode 100644
index 0000000..b6cdb4c
Binary files /dev/null and b/src/Tests/Tests.PagesToInclude.verified.pdf differ
diff --git a/src/Tests/Tests.PagesToInclude.verified.txt b/src/Tests/Tests.PagesToInclude.verified.txt
new file mode 100644
index 0000000..02e2bb9
--- /dev/null
+++ b/src/Tests/Tests.PagesToInclude.verified.txt
@@ -0,0 +1,3 @@
+{
+ PageCount: 2
+}
\ No newline at end of file
diff --git a/src/Tests/Tests.SkipPdfNormalization#00.verified.png b/src/Tests/Tests.SkipPdfNormalization#page_0001.verified.png
similarity index 100%
rename from src/Tests/Tests.SkipPdfNormalization#00.verified.png
rename to src/Tests/Tests.SkipPdfNormalization#page_0001.verified.png
diff --git a/src/Tests/Tests.VerifyPdf#01.verified.png b/src/Tests/Tests.SkipPdfNormalization#page_0002.verified.png
similarity index 100%
rename from src/Tests/Tests.VerifyPdf#01.verified.png
rename to src/Tests/Tests.SkipPdfNormalization#page_0002.verified.png
diff --git a/src/Tests/Tests.SkipPdfNormalization.verified.txt b/src/Tests/Tests.SkipPdfNormalization.verified.txt
new file mode 100644
index 0000000..02e2bb9
--- /dev/null
+++ b/src/Tests/Tests.SkipPdfNormalization.verified.txt
@@ -0,0 +1,3 @@
+{
+ PageCount: 2
+}
\ No newline at end of file
diff --git a/src/Tests/Tests.VerifyPdf#00.verified.png b/src/Tests/Tests.VerifyPdf#page_0001.verified.png
similarity index 100%
rename from src/Tests/Tests.VerifyPdf#00.verified.png
rename to src/Tests/Tests.VerifyPdf#page_0001.verified.png
diff --git a/src/Tests/Tests.VerifyPdfWithName#name.01.verified.png b/src/Tests/Tests.VerifyPdf#page_0002.verified.png
similarity index 100%
rename from src/Tests/Tests.VerifyPdfWithName#name.01.verified.png
rename to src/Tests/Tests.VerifyPdf#page_0002.verified.png
diff --git a/src/Tests/Tests.VerifyPdf.verified.txt b/src/Tests/Tests.VerifyPdf.verified.txt
new file mode 100644
index 0000000..02e2bb9
--- /dev/null
+++ b/src/Tests/Tests.VerifyPdf.verified.txt
@@ -0,0 +1,3 @@
+{
+ PageCount: 2
+}
\ No newline at end of file
diff --git a/src/Tests/Tests.VerifyPdfWithName#name.00.verified.png b/src/Tests/Tests.VerifyPdfWithName#name.page_0001.verified.png
similarity index 100%
rename from src/Tests/Tests.VerifyPdfWithName#name.00.verified.png
rename to src/Tests/Tests.VerifyPdfWithName#name.page_0001.verified.png
diff --git a/src/Tests/Tests.VerifyPdfWithName#name.page_0002.verified.png b/src/Tests/Tests.VerifyPdfWithName#name.page_0002.verified.png
new file mode 100644
index 0000000..f3ff39e
Binary files /dev/null and b/src/Tests/Tests.VerifyPdfWithName#name.page_0002.verified.png differ
diff --git a/src/Tests/Tests.VerifyPdfWithName#name.verified.txt b/src/Tests/Tests.VerifyPdfWithName#name.verified.txt
new file mode 100644
index 0000000..02e2bb9
--- /dev/null
+++ b/src/Tests/Tests.VerifyPdfWithName#name.verified.txt
@@ -0,0 +1,3 @@
+{
+ PageCount: 2
+}
\ No newline at end of file
diff --git a/src/Tests/Tests.VerifySvgWithName#name.verified.png b/src/Tests/Tests.VerifySvgWithName#name.verified.png
new file mode 100644
index 0000000..21e6b7b
Binary files /dev/null and b/src/Tests/Tests.VerifySvgWithName#name.verified.png differ
diff --git a/src/Tests/Tests.VerifySvgWithName#name.verified.svg b/src/Tests/Tests.VerifySvgWithName#name.verified.svg
new file mode 100644
index 0000000..275b97d
--- /dev/null
+++ b/src/Tests/Tests.VerifySvgWithName#name.verified.svg
@@ -0,0 +1,23 @@
+
+
diff --git a/src/Tests/Tests.cs b/src/Tests/Tests.cs
index cc27cc7..fc4ca52 100644
--- a/src/Tests/Tests.cs
+++ b/src/Tests/Tests.cs
@@ -176,4 +176,14 @@ public Task SkipPdfNormalization() =>
[Test]
public Task VerifyPdfWithName() =>
Verify(targets: [new("pdf", ProjectFiles.sample_pdf.OpenRead(), "name")]);
+
+ [Test]
+ public Task VerifySvgWithName() =>
+ Verify(targets: [new("svg", ProjectFiles.transparent_svg.OpenRead(), "name")]);
+
+ // A page keeps its number when the pages before it are left out, so this is #page_0002.
+ [Test]
+ public Task PagesToInclude() =>
+ VerifyFile(ProjectFiles.sample_pdf.Path)
+ .PagesToInclude(_ => _ == 2);
}
\ No newline at end of file
diff --git a/src/Tests/TransparentSamples.TransparentNone_format=pdf.verified.png b/src/Tests/TransparentSamples.TransparentNone_format=pdf#page_0001.verified.png
similarity index 100%
rename from src/Tests/TransparentSamples.TransparentNone_format=pdf.verified.png
rename to src/Tests/TransparentSamples.TransparentNone_format=pdf#page_0001.verified.png
diff --git a/src/Tests/TransparentSamples.TransparentNone_format=pdf.verified.txt b/src/Tests/TransparentSamples.TransparentNone_format=pdf.verified.txt
new file mode 100644
index 0000000..17e6e49
--- /dev/null
+++ b/src/Tests/TransparentSamples.TransparentNone_format=pdf.verified.txt
@@ -0,0 +1,3 @@
+{
+ PageCount: 1
+}
\ No newline at end of file
diff --git a/src/Tests/TransparentSamples.TransparentSample_format=pdf_backgroundColor=Blue.verified.png b/src/Tests/TransparentSamples.TransparentSample_format=pdf_backgroundColor=Blue#page_0001.verified.png
similarity index 100%
rename from src/Tests/TransparentSamples.TransparentSample_format=pdf_backgroundColor=Blue.verified.png
rename to src/Tests/TransparentSamples.TransparentSample_format=pdf_backgroundColor=Blue#page_0001.verified.png
diff --git a/src/Tests/TransparentSamples.TransparentSample_format=pdf_backgroundColor=Blue.verified.txt b/src/Tests/TransparentSamples.TransparentSample_format=pdf_backgroundColor=Blue.verified.txt
new file mode 100644
index 0000000..17e6e49
--- /dev/null
+++ b/src/Tests/TransparentSamples.TransparentSample_format=pdf_backgroundColor=Blue.verified.txt
@@ -0,0 +1,3 @@
+{
+ PageCount: 1
+}
\ No newline at end of file
diff --git a/src/Tests/TransparentSamples.TransparentSample_format=pdf_backgroundColor=Green.verified.png b/src/Tests/TransparentSamples.TransparentSample_format=pdf_backgroundColor=Green#page_0001.verified.png
similarity index 100%
rename from src/Tests/TransparentSamples.TransparentSample_format=pdf_backgroundColor=Green.verified.png
rename to src/Tests/TransparentSamples.TransparentSample_format=pdf_backgroundColor=Green#page_0001.verified.png
diff --git a/src/Tests/TransparentSamples.TransparentSample_format=pdf_backgroundColor=Green.verified.txt b/src/Tests/TransparentSamples.TransparentSample_format=pdf_backgroundColor=Green.verified.txt
new file mode 100644
index 0000000..17e6e49
--- /dev/null
+++ b/src/Tests/TransparentSamples.TransparentSample_format=pdf_backgroundColor=Green.verified.txt
@@ -0,0 +1,3 @@
+{
+ PageCount: 1
+}
\ No newline at end of file
diff --git a/src/Tests/TransparentSamples.TransparentSample_format=pdf_backgroundColor=Transparent.verified.png b/src/Tests/TransparentSamples.TransparentSample_format=pdf_backgroundColor=Transparent#page_0001.verified.png
similarity index 100%
rename from src/Tests/TransparentSamples.TransparentSample_format=pdf_backgroundColor=Transparent.verified.png
rename to src/Tests/TransparentSamples.TransparentSample_format=pdf_backgroundColor=Transparent#page_0001.verified.png
diff --git a/src/Tests/TransparentSamples.TransparentSample_format=pdf_backgroundColor=Transparent.verified.txt b/src/Tests/TransparentSamples.TransparentSample_format=pdf_backgroundColor=Transparent.verified.txt
new file mode 100644
index 0000000..17e6e49
--- /dev/null
+++ b/src/Tests/TransparentSamples.TransparentSample_format=pdf_backgroundColor=Transparent.verified.txt
@@ -0,0 +1,3 @@
+{
+ PageCount: 1
+}
\ No newline at end of file
diff --git a/src/Verify.ImageMagick/GlobalUsings.cs b/src/Verify.ImageMagick/GlobalUsings.cs
index 6c12e9e..78bd6aa 100644
--- a/src/Verify.ImageMagick/GlobalUsings.cs
+++ b/src/Verify.ImageMagick/GlobalUsings.cs
@@ -1,5 +1,4 @@
global using DeterministicPdf;
-global using System.Diagnostics.CodeAnalysis;
global using EmptyFiles;
global using ImageMagick;
global using ImageMagick.Formats;
diff --git a/src/Verify.ImageMagick/ImageMagickOutputs.cs b/src/Verify.ImageMagick/ImageMagickOutputs.cs
deleted file mode 100644
index b2eb919..0000000
--- a/src/Verify.ImageMagick/ImageMagickOutputs.cs
+++ /dev/null
@@ -1,23 +0,0 @@
-namespace VerifyTests;
-
-///
-/// Controls which outputs a document (pdf or svg) is split into.
-///
-[Flags]
-public enum ImageMagickOutputs
-{
- ///
- /// No outputs. Only the source document is emitted.
- ///
- None = 0,
-
- ///
- /// Render pdf pages and svg documents to png.
- ///
- Png = 1,
-
- ///
- /// All outputs.
- ///
- All = Png
-}
diff --git a/src/Verify.ImageMagick/ImageMagickSettings.cs b/src/Verify.ImageMagick/ImageMagickSettings.cs
index 072dd0e..2e85045 100644
--- a/src/Verify.ImageMagick/ImageMagickSettings.cs
+++ b/src/Verify.ImageMagick/ImageMagickSettings.cs
@@ -11,27 +11,6 @@ public static void ImageMagickBackground(MagickColor color) =>
public static void ImageMagickPdfPassword(string password) =>
pdfPassword = password;
- public static void PagesToInclude(this VerifySettings settings, int count) =>
- settings.Context["ImageMagick.PagesToInclude"] = count;
-
- public static SettingsTask PagesToInclude(this SettingsTask settings, int count)
- {
- settings.CurrentSettings.PagesToInclude(count);
- return settings;
- }
-
- internal static bool GetPagesToInclude(this IReadOnlyDictionary context, [NotNullWhen(true)] out int? pages)
- {
- if (context.TryGetValue("ImageMagick.PagesToInclude", out var value))
- {
- pages = (int) value;
- return true;
- }
-
- pages = null;
- return false;
- }
-
public static void MagickReadSettings(this VerifySettings settings, MagickReadSettings magickReadSettings) =>
settings.Context["ImageMagick.MagickReadSettings"] = magickReadSettings;
diff --git a/src/Verify.ImageMagick/VerifyImageMagick.cs b/src/Verify.ImageMagick/VerifyImageMagick.cs
index 51c6a07..7a21ba9 100644
--- a/src/Verify.ImageMagick/VerifyImageMagick.cs
+++ b/src/Verify.ImageMagick/VerifyImageMagick.cs
@@ -4,10 +4,7 @@ public static partial class VerifyImageMagick
{
public static bool Initialized { get; private set; }
- static ImageMagickOutputs outputs = ImageMagickOutputs.All;
-
- /// Which outputs pdf and svg documents are split into. Defaults to .
- public static void Initialize(ImageMagickOutputs outputs = ImageMagickOutputs.All)
+ public static void Initialize()
{
if (Initialized)
{
@@ -15,12 +12,11 @@ public static void Initialize(ImageMagickOutputs outputs = ImageMagickOutputs.Al
}
Initialized = true;
- VerifyImageMagick.outputs = outputs;
InnerVerifier.ThrowIfVerifyHasBeenRun();
VerifierSettings.RegisterStreamConverter(
"svg",
- ConvertSvg);
+ (_, stream, context) => ConvertSvg(stream, context));
VerifierSettings.RegisterStreamConverter(
"png",
(name, stream, context) => ConvertImage(name, stream, context, "png", MagickFormat.Png));
@@ -33,6 +29,9 @@ public static void Initialize(ImageMagickOutputs outputs = ImageMagickOutputs.Al
RegisterPdfToPngConverter();
}
+ // The image is the thing being verified, not something derived from a document, so this
+ // stays on the constructor that leaves the naming to the converter and passes the name on.
+ // As a derived target it would be dropped by ExcludeDerivedTargets.
static ConversionResult ConvertImage(string? name, Stream stream, IReadOnlyDictionary context, string extension, MagickFormat format)
{
var background = context.Background();
diff --git a/src/Verify.ImageMagick/VerifyImageMagick_Pdf.cs b/src/Verify.ImageMagick/VerifyImageMagick_Pdf.cs
index 024c267..89aec53 100644
--- a/src/Verify.ImageMagick/VerifyImageMagick_Pdf.cs
+++ b/src/Verify.ImageMagick/VerifyImageMagick_Pdf.cs
@@ -7,12 +7,11 @@ public static void RegisterPdfToPngConverter()
InnerVerifier.ThrowIfVerifyHasBeenRun();
VerifierSettings.RegisterStreamConverter(
"pdf",
- (name, stream, context) => Convert(name, stream, context, MagickFormat.Pdf));
+ (_, stream, context) => Convert(stream, context, MagickFormat.Pdf));
}
- internal static ConversionResult Convert(string? name, Stream stream, IReadOnlyDictionary context, MagickFormat magickFormat)
+ internal static ConversionResult Convert(Stream stream, IReadOnlyDictionary context, MagickFormat magickFormat)
{
- var streams = new List();
var magickSettings = context.MagickReadSettings();
magickSettings.Format = magickFormat;
var password = context.PdfPassword();
@@ -44,20 +43,21 @@ internal static ConversionResult Convert(string? name, Stream stream, IReadOnlyD
// MagickImageCollection has consumed it.
stream = WrapStream(stream);
- if (outputs.HasFlag(ImageMagickOutputs.Png))
+ // Names the pages and says which of them the verification wants, so that is decided the
+ // same way here as in every other converter of a paged document.
+ var conversion = new PagedConversion(context);
+
+ // Ghostscript draws every page in the one read, so the read can only be skipped whole:
+ // when the pngs are excluded. The pages are then only counted, which reads what each
+ // one is without drawing it, so the info file says how many there are either way.
+ if (conversion.IncludeImages)
{
using var images = new MagickImageCollection();
images.Read(stream, magickSettings);
- var count = images.Count;
- if (context.GetPagesToInclude(out var pagesToInclude))
- {
- count = Math.Min(count, (int) pagesToInclude);
- }
-
var background = context.Background();
- for (var index = 0; index < count; index++)
+ foreach (var number in conversion.Pages(images.Count))
{
- var image = images[index];
+ var image = images[number - 1];
if (background != null)
{
image = Flatten(image, background);
@@ -65,11 +65,15 @@ internal static ConversionResult Convert(string? name, Stream stream, IReadOnlyD
var memoryStream = new MemoryStream();
image.Write(memoryStream, MagickFormat.Png);
- streams.Add(memoryStream);
+ conversion.AddPage(number, memoryStream);
}
}
-
- List targets = [];
+ else
+ {
+ using var images = new MagickImageCollection();
+ images.Ping(stream, magickSettings);
+ conversion.PageCount = images.Count;
+ }
// The pdf snapshot is always the full document, regardless of PagesToInclude, which trims
// only the rendered pages above. Mirrors the svg target in ConvertSvg, which likewise emits
@@ -78,15 +82,10 @@ internal static ConversionResult Convert(string? name, Stream stream, IReadOnlyD
{
stream.Position = 0;
var pdf = context.Normalize() ? PdfNormalizer.Normalize(stream) : CopyRemaining(stream);
- targets.Add(
- new("pdf", pdf, name, performConversion: false)
- {
- BypassComparersForSubsequentOnDifference = true
- });
+ conversion.Source(new("pdf", pdf));
}
- targets.AddRange(streams.Select(_ => new Target("png", _, name)));
- return new(null, targets);
+ return conversion.Build();
}
// The source stream is consumed elsewhere in this method, so the pdf target gets its own copy.
diff --git a/src/Verify.ImageMagick/VerifyImageMagick_Svg.cs b/src/Verify.ImageMagick/VerifyImageMagick_Svg.cs
index 696a287..650e848 100644
--- a/src/Verify.ImageMagick/VerifyImageMagick_Svg.cs
+++ b/src/Verify.ImageMagick/VerifyImageMagick_Svg.cs
@@ -2,12 +2,18 @@ namespace VerifyTests;
public static partial class VerifyImageMagick
{
- static ConversionResult ConvertSvg(string? name, Stream stream, IReadOnlyDictionary context)
+ static ConversionResult ConvertSvg(Stream stream, IReadOnlyDictionary context)
{
var content = ReadNormalizedSvg(stream);
- if (!outputs.HasFlag(ImageMagickOutputs.Png))
+
+ // The svg is the source and its render is derived from it, which is what lets
+ // ExcludeDerivedTargets("png") drop the render and leave a png verified as itself alone.
+ var source = new Target("svg", content);
+
+ // Asked before rendering, so a png that would be dropped is never drawn.
+ if (context.IsDerivedTargetExcluded("png"))
{
- return new(null, [new("svg", content, name)]);
+ return new(null, source, []);
}
using var svgStream = new MemoryStream(Encoding.UTF8.GetBytes(content));
@@ -16,12 +22,9 @@ static ConversionResult ConvertSvg(string? name, Stream stream, IReadOnlyDiction
var pngStream = new MemoryStream();
svg.Write(pngStream, MagickFormat.Png);
- return new(
- null,
- [
- new("svg", content, name),
- new("png", pngStream, name)
- ]);
+ // Not named: an svg has the one render, so it takes the name of the svg and differs from
+ // it by extension alone.
+ return new(null, source, [new("png", pngStream)]);
}
// Verify requires text snapshots to use \n line endings, so an svg authored or checked out