diff --git a/readme.md b/readme.md index 66ee591..46db0ef 100644 --- a/readme.md +++ b/readme.md @@ -55,30 +55,6 @@ Other [compares](https://github.com/VerifyTests/Verify/blob/main/docs/comparer.m * https://github.com/VerifyTests/Verify.ImageSharp.Compare -### Outputs - -`Initialize` accepts an optional `QuestPdfOutputs` to control which outputs a document is split into: - - * `Png`: render each page to a png. When omitted, pages are not rasterized at all. - * `None`: none of the above. Only the info and the source document are emitted. - * `All`: the default. - -The pdf target is not controlled by this setting. Use `VerifierSettings.ExcludeTargets("pdf")` to exclude it. - - - -```cs -[ModuleInitializer] -public static void Init() -{ - QuestPDF.Settings.License = LicenseType.Community; - // Skip rendering pages to png - VerifyQuestPdf.Initialize(QuestPdfOutputs.None); -} -``` -snippet source | anchor - - ### Code that generates a document @@ -115,7 +91,7 @@ static void AddPage(PageDescriptor page) }); } ``` -snippet source | anchor +snippet source | anchor @@ -137,6 +113,14 @@ public Task VerifyDocument() ### Results +Verifying a document produces: + + * The pdf itself as `.verified.pdf`. This can be omitted with [`ExcludeTargets`](#exclude-the-pdf). + * An info file as `.verified.txt`, with the metadata and settings of the document and its page count. + * A png of every page as `#page_0001.verified.png`, `#page_0002.verified.png`, etc. These can be omitted with [`ExcludeDerivedTargets`](#exclude-the-page-images). + +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 are the settings that [choose what is verified](#choosing-what-is-verified). + #### Metadata @@ -144,26 +128,72 @@ public Task VerifyDocument() ```txt { - Pages: 2, - Settings: { - ContentDirection: LeftToRight, - PDFA_Conformance: None, - PDFUA_Conformance: None, - ImageCompressionQuality: High, - ImageRasterDpi: 288 - } + Document: { + Settings: { + ContentDirection: LeftToRight, + PDFA_Conformance: None, + PDFUA_Conformance: None, + ImageCompressionQuality: High, + ImageRasterDpi: 288 + } + }, + PageCount: 2 } ``` -snippet source | anchor +snippet source | anchor #### Pdf as image - + + + +## Choosing what is verified + +What a document is split into is controlled by Verify's settings for [paged documents](https://github.com/VerifyTests/Verify/blob/main/docs/paged-documents.md). Each can be set on a verification, or for every test on `VerifierSettings` at initialization. + +No text is read from a document, so `PageText` has no effect. -## Exclude the pdf +### Exclude the page images + +`ExcludeDerivedTargets("png")` leaves out the png of every page, keeping the pdf and the info file. Rasterizing pages is expensive, and it is then skipped altogether: + + + +```cs +[Test] +public Task ExcludePng() +{ + var document = GenerateDocument(); + return Verify(document) + .ExcludeDerivedTargets("png"); +} +``` +snippet source | anchor + + +To leave out the page images for every test: + + + +```cs +[ModuleInitializer] +public static void Init() +{ + QuestPDF.Settings.License = LicenseType.Community; + VerifyQuestPdf.Initialize(); + + // For every test: no page images, so pages are not rendered to png + VerifierSettings.ExcludeDerivedTargets("png"); +} +``` +snippet source | anchor + + + +### Exclude the pdf QuestPDF renders the source pdf, and it is included in the snapshot as a `.verified.pdf`. Generating it is expensive, and committing it is not always wanted. [`ExcludeTargets`](https://github.com/VerifyTests/Verify/blob/main/docs/converter.md#excluding-targets) drops it from a verification and skips the generation, while the rendered pages and info still verify: @@ -184,9 +214,9 @@ public Task ExcludePdf() To exclude the pdf for every test, call `VerifierSettings.ExcludeTargets("pdf")` at initialization. -## PagesToInclude +### PagesToInclude -To render only a defined number of pages at the start of a document: +To verify only a defined number of pages at the start of a document: @@ -199,13 +229,15 @@ public Task PagesToInclude() .PagesToInclude(1); } ``` -snippet source | anchor +snippet source | anchor +The pdf is still verified whole, and `PageCount` in the info file is still the number of pages the document has. + -### Dynamic +#### Dynamic -To dynamically control what pages are rendered: +To dynamically control what pages are verified, pass a delegate that takes the 1 based number of a page: @@ -218,5 +250,59 @@ public Task PagesToIncludeDynamic() .PagesToInclude(pageNumber => pageNumber == 2); } ``` -snippet source | anchor +snippet source | anchor + +A page keeps its number when other pages are left out. The above verifies the second page as `#page_0002.verified.png`. + +QuestPDF draws all the pages of a document in one pass, so the pages that are left out are still rendered. `PagesToInclude` limits what is verified, not the work done. + + +## Reviewing changes + +A change to a document 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. + +When the pdf has changed, its pages are compared exactly, skipping any [comparer](https://github.com/VerifyTests/Verify/blob/main/docs/comparer.md) registered for `png`. A comparer exists to tolerate rendering differences, and a pdf that has changed is the one case where its pages should not be given the benefit of the doubt. + + +## Migrating from 2.x + +Version 3 moves to the paged document support in Verify 33.3. The settings for choosing what is verified are now those of Verify: + +| 2.x | 3.x | +| --- | --- | +| `VerifyQuestPdf.Initialize(QuestPdfOutputs.None)` | `VerifyQuestPdf.Initialize()` and `VerifierSettings.ExcludeDerivedTargets("png")` | +| `VerifyQuestPdf.Initialize(QuestPdfOutputs.Png)` or `QuestPdfOutputs.All` | `VerifyQuestPdf.Initialize()` | +| `.PagesToInclude(2)` | Unchanged. It is now a member of `VerifySettings` and `SettingsTask`, and of `VerifierSettings` for every test | +| `.PagesToInclude(pageNumber => pageNumber == 2)` | Unchanged for a lambda. The delegate `VerifyQuestPDF.ShouldIncludePage` is replaced by `VerifyTests.IncludePage` | + +The page images are renamed. The number of a page is now 1 based, is always present, and is the number the page has in the document: + +| 2.x | 3.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` for a document with one page | `Tests.Report#page_0001.verified.png` | +| `Tests.Report.verified.png` for `PagesToInclude(pageNumber => pageNumber == 2)` | `Tests.Report#page_0002.verified.png` | +| `Tests.Report.verified.pdf` | Unchanged | +| `Tests.Report.verified.txt` | The same name, with the content below | + +In 2.x the pages `PagesToInclude` kept were numbered again from zero, so the name of a page file depended on which other pages were verified. + +The info file has the shape every paged document has: the metadata and settings under `Document`, and `Pages` as `PageCount`. + +``` +{ { + Pages: 2, Document: { + Metadata: { Metadata: { + Title: The Title Title: The Title + }, }, + Settings: { Settings: { + ContentDirection: LeftToRight ContentDirection: LeftToRight + } } +} }, + PageCount: 2 + } +``` + +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. diff --git a/src/Directory.Build.props b/src/Directory.Build.props index a9613cd..f5acc7d 100644 --- a/src/Directory.Build.props +++ b/src/Directory.Build.props @@ -2,7 +2,7 @@ CS1591;CS0649;NU1608;NU1109 - 2.10.0 + 3.0.0 1.0.0 preview enable diff --git a/src/Directory.Packages.props b/src/Directory.Packages.props index 8e3caf9..d785938 100644 --- a/src/Directory.Packages.props +++ b/src/Directory.Packages.props @@ -10,11 +10,11 @@ - + - + - + diff --git a/src/StaticSettingsTests/ModuleInitializer.cs b/src/StaticSettingsTests/ModuleInitializer.cs index ee60645..561affd 100644 --- a/src/StaticSettingsTests/ModuleInitializer.cs +++ b/src/StaticSettingsTests/ModuleInitializer.cs @@ -6,8 +6,10 @@ public static class ModuleInitializer public static void Init() { QuestPDF.Settings.License = LicenseType.Community; - // Skip rendering pages to png - VerifyQuestPdf.Initialize(QuestPdfOutputs.None); + VerifyQuestPdf.Initialize(); + + // For every test: no page images, so pages are not rendered to png + VerifierSettings.ExcludeDerivedTargets("png"); } #endregion diff --git a/src/StaticSettingsTests/Tests.VerifyDocument.verified.txt b/src/StaticSettingsTests/Tests.VerifyDocument.verified.txt index 2de2447..4a00fa0 100644 --- a/src/StaticSettingsTests/Tests.VerifyDocument.verified.txt +++ b/src/StaticSettingsTests/Tests.VerifyDocument.verified.txt @@ -1,10 +1,12 @@ { - Pages: 2, - Settings: { - ContentDirection: LeftToRight, - PDFA_Conformance: None, - PDFUA_Conformance: None, - ImageCompressionQuality: High, - ImageRasterDpi: 288 - } + Document: { + Settings: { + ContentDirection: LeftToRight, + PDFA_Conformance: None, + PDFUA_Conformance: None, + ImageCompressionQuality: High, + ImageRasterDpi: 288 + } + }, + PageCount: 2 } \ No newline at end of file diff --git a/src/Tests/Samples.ExcludePdf#00.verified.png b/src/Tests/Samples.ExcludePdf#00.verified.png deleted file mode 100644 index bbad1be..0000000 Binary files a/src/Tests/Samples.ExcludePdf#00.verified.png and /dev/null differ diff --git a/src/Tests/Samples.ExcludePdf#01.verified.png b/src/Tests/Samples.ExcludePdf#01.verified.png deleted file mode 100644 index 6bac9de..0000000 Binary files a/src/Tests/Samples.ExcludePdf#01.verified.png and /dev/null differ diff --git a/src/Tests/Samples.PagesToInclude.verified.png b/src/Tests/Samples.ExcludePdf#page_0001.verified.png similarity index 100% rename from src/Tests/Samples.PagesToInclude.verified.png rename to src/Tests/Samples.ExcludePdf#page_0001.verified.png diff --git a/src/Tests/Samples.PagesToIncludeDynamic.verified.png b/src/Tests/Samples.ExcludePdf#page_0002.verified.png similarity index 100% rename from src/Tests/Samples.PagesToIncludeDynamic.verified.png rename to src/Tests/Samples.ExcludePdf#page_0002.verified.png diff --git a/src/Tests/Samples.ExcludePdf.verified.txt b/src/Tests/Samples.ExcludePdf.verified.txt index 2de2447..4a00fa0 100644 --- a/src/Tests/Samples.ExcludePdf.verified.txt +++ b/src/Tests/Samples.ExcludePdf.verified.txt @@ -1,10 +1,12 @@ { - Pages: 2, - Settings: { - ContentDirection: LeftToRight, - PDFA_Conformance: None, - PDFUA_Conformance: None, - ImageCompressionQuality: High, - ImageRasterDpi: 288 - } + Document: { + Settings: { + ContentDirection: LeftToRight, + PDFA_Conformance: None, + PDFUA_Conformance: None, + ImageCompressionQuality: High, + ImageRasterDpi: 288 + } + }, + PageCount: 2 } \ No newline at end of file diff --git a/src/Tests/Samples.ExcludePng.verified.pdf b/src/Tests/Samples.ExcludePng.verified.pdf new file mode 100644 index 0000000..63f10dd Binary files /dev/null and b/src/Tests/Samples.ExcludePng.verified.pdf differ diff --git a/src/Tests/Samples.ExcludePng.verified.txt b/src/Tests/Samples.ExcludePng.verified.txt new file mode 100644 index 0000000..4a00fa0 --- /dev/null +++ b/src/Tests/Samples.ExcludePng.verified.txt @@ -0,0 +1,12 @@ +{ + Document: { + Settings: { + ContentDirection: LeftToRight, + PDFA_Conformance: None, + PDFUA_Conformance: None, + ImageCompressionQuality: High, + ImageRasterDpi: 288 + } + }, + PageCount: 2 +} \ No newline at end of file diff --git a/src/Tests/Samples.SkipPdfNormalization#00.verified.png b/src/Tests/Samples.PagesToInclude#page_0001.verified.png similarity index 100% rename from src/Tests/Samples.SkipPdfNormalization#00.verified.png rename to src/Tests/Samples.PagesToInclude#page_0001.verified.png diff --git a/src/Tests/Samples.PagesToInclude.verified.txt b/src/Tests/Samples.PagesToInclude.verified.txt index 2de2447..4a00fa0 100644 --- a/src/Tests/Samples.PagesToInclude.verified.txt +++ b/src/Tests/Samples.PagesToInclude.verified.txt @@ -1,10 +1,12 @@ { - Pages: 2, - Settings: { - ContentDirection: LeftToRight, - PDFA_Conformance: None, - PDFUA_Conformance: None, - ImageCompressionQuality: High, - ImageRasterDpi: 288 - } + Document: { + Settings: { + ContentDirection: LeftToRight, + PDFA_Conformance: None, + PDFUA_Conformance: None, + ImageCompressionQuality: High, + ImageRasterDpi: 288 + } + }, + PageCount: 2 } \ No newline at end of file diff --git a/src/Tests/Samples.SkipPdfNormalization#01.verified.png b/src/Tests/Samples.PagesToIncludeDynamic#page_0002.verified.png similarity index 100% rename from src/Tests/Samples.SkipPdfNormalization#01.verified.png rename to src/Tests/Samples.PagesToIncludeDynamic#page_0002.verified.png diff --git a/src/Tests/Samples.PagesToIncludeDynamic.verified.txt b/src/Tests/Samples.PagesToIncludeDynamic.verified.txt index 2de2447..4a00fa0 100644 --- a/src/Tests/Samples.PagesToIncludeDynamic.verified.txt +++ b/src/Tests/Samples.PagesToIncludeDynamic.verified.txt @@ -1,10 +1,12 @@ { - Pages: 2, - Settings: { - ContentDirection: LeftToRight, - PDFA_Conformance: None, - PDFUA_Conformance: None, - ImageCompressionQuality: High, - ImageRasterDpi: 288 - } + Document: { + Settings: { + ContentDirection: LeftToRight, + PDFA_Conformance: None, + PDFUA_Conformance: None, + ImageCompressionQuality: High, + ImageRasterDpi: 288 + } + }, + PageCount: 2 } \ No newline at end of file diff --git a/src/Tests/Samples.VerifyDocument#00.verified.png b/src/Tests/Samples.SkipPdfNormalization#page_0001.verified.png similarity index 100% rename from src/Tests/Samples.VerifyDocument#00.verified.png rename to src/Tests/Samples.SkipPdfNormalization#page_0001.verified.png diff --git a/src/Tests/Samples.VerifyDocument#01.verified.png b/src/Tests/Samples.SkipPdfNormalization#page_0002.verified.png similarity index 100% rename from src/Tests/Samples.VerifyDocument#01.verified.png rename to src/Tests/Samples.SkipPdfNormalization#page_0002.verified.png diff --git a/src/Tests/Samples.SkipPdfNormalization.verified.txt b/src/Tests/Samples.SkipPdfNormalization.verified.txt index 2de2447..4a00fa0 100644 --- a/src/Tests/Samples.SkipPdfNormalization.verified.txt +++ b/src/Tests/Samples.SkipPdfNormalization.verified.txt @@ -1,10 +1,12 @@ { - Pages: 2, - Settings: { - ContentDirection: LeftToRight, - PDFA_Conformance: None, - PDFUA_Conformance: None, - ImageCompressionQuality: High, - ImageRasterDpi: 288 - } + Document: { + Settings: { + ContentDirection: LeftToRight, + PDFA_Conformance: None, + PDFUA_Conformance: None, + ImageCompressionQuality: High, + ImageRasterDpi: 288 + } + }, + PageCount: 2 } \ No newline at end of file diff --git a/src/Tests/Samples.VerifyDocumentWithMetadata#00.verified.png b/src/Tests/Samples.VerifyDocument#page_0001.verified.png similarity index 100% rename from src/Tests/Samples.VerifyDocumentWithMetadata#00.verified.png rename to src/Tests/Samples.VerifyDocument#page_0001.verified.png diff --git a/src/Tests/Samples.VerifyDocumentWithMetadata#01.verified.png b/src/Tests/Samples.VerifyDocument#page_0002.verified.png similarity index 100% rename from src/Tests/Samples.VerifyDocumentWithMetadata#01.verified.png rename to src/Tests/Samples.VerifyDocument#page_0002.verified.png diff --git a/src/Tests/Samples.VerifyDocument.verified.txt b/src/Tests/Samples.VerifyDocument.verified.txt index 2de2447..4a00fa0 100644 --- a/src/Tests/Samples.VerifyDocument.verified.txt +++ b/src/Tests/Samples.VerifyDocument.verified.txt @@ -1,10 +1,12 @@ { - Pages: 2, - Settings: { - ContentDirection: LeftToRight, - PDFA_Conformance: None, - PDFUA_Conformance: None, - ImageCompressionQuality: High, - ImageRasterDpi: 288 - } + Document: { + Settings: { + ContentDirection: LeftToRight, + PDFA_Conformance: None, + PDFUA_Conformance: None, + ImageCompressionQuality: High, + ImageRasterDpi: 288 + } + }, + PageCount: 2 } \ No newline at end of file diff --git a/src/Tests/SkipPdfNormalizationTests.NormalizedSnapshotHoldsTheNeutralizedBytes#00.verified.png b/src/Tests/Samples.VerifyDocumentWithMetadata#page_0001.verified.png similarity index 100% rename from src/Tests/SkipPdfNormalizationTests.NormalizedSnapshotHoldsTheNeutralizedBytes#00.verified.png rename to src/Tests/Samples.VerifyDocumentWithMetadata#page_0001.verified.png diff --git a/src/Tests/SkipPdfNormalizationTests.NormalizedSnapshotHoldsTheNeutralizedBytes#01.verified.png b/src/Tests/Samples.VerifyDocumentWithMetadata#page_0002.verified.png similarity index 100% rename from src/Tests/SkipPdfNormalizationTests.NormalizedSnapshotHoldsTheNeutralizedBytes#01.verified.png rename to src/Tests/Samples.VerifyDocumentWithMetadata#page_0002.verified.png diff --git a/src/Tests/Samples.VerifyDocumentWithMetadata.verified.txt b/src/Tests/Samples.VerifyDocumentWithMetadata.verified.txt index 25e82b4..c263302 100644 --- a/src/Tests/Samples.VerifyDocumentWithMetadata.verified.txt +++ b/src/Tests/Samples.VerifyDocumentWithMetadata.verified.txt @@ -1,20 +1,22 @@ { - Pages: 2, - Metadata: { - Author: The Author, - Creator: The Creator, - Keywords: keyword1, keyword2, - Producer: The Producer, - Title: The Title, - Subject: The Subject, - CreationDate: DateTimeOffset_1, - ModifiedDate: DateTimeOffset_1 + Document: { + Metadata: { + Author: The Author, + Creator: The Creator, + Keywords: keyword1, keyword2, + Producer: The Producer, + Title: The Title, + Subject: The Subject, + CreationDate: DateTimeOffset_1, + ModifiedDate: DateTimeOffset_1 + }, + Settings: { + ContentDirection: LeftToRight, + PDFA_Conformance: None, + PDFUA_Conformance: None, + ImageCompressionQuality: High, + ImageRasterDpi: 288 + } }, - Settings: { - ContentDirection: LeftToRight, - PDFA_Conformance: None, - PDFUA_Conformance: None, - ImageCompressionQuality: High, - ImageRasterDpi: 288 - } + PageCount: 2 } \ No newline at end of file diff --git a/src/Tests/Samples.cs b/src/Tests/Samples.cs index 9f5f120..5e49d2a 100644 --- a/src/Tests/Samples.cs +++ b/src/Tests/Samples.cs @@ -68,6 +68,18 @@ public Task ExcludePdf() #endregion + #region ExcludePng + + [Test] + public Task ExcludePng() + { + var document = GenerateDocument(); + return Verify(document) + .ExcludeDerivedTargets("png"); + } + + #endregion + #region PagesToInclude [Test] diff --git a/src/Tests/SkipPdfNormalizationTests.SkippedSnapshotHoldsTheProducerBytes#00.verified.png b/src/Tests/SkipPdfNormalizationTests.NormalizedSnapshotHoldsTheNeutralizedBytes#page_0001.verified.png similarity index 100% rename from src/Tests/SkipPdfNormalizationTests.SkippedSnapshotHoldsTheProducerBytes#00.verified.png rename to src/Tests/SkipPdfNormalizationTests.NormalizedSnapshotHoldsTheNeutralizedBytes#page_0001.verified.png diff --git a/src/Tests/SkipPdfNormalizationTests.SkippedSnapshotHoldsTheProducerBytes#01.verified.png b/src/Tests/SkipPdfNormalizationTests.NormalizedSnapshotHoldsTheNeutralizedBytes#page_0002.verified.png similarity index 100% rename from src/Tests/SkipPdfNormalizationTests.SkippedSnapshotHoldsTheProducerBytes#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 index 2de2447..4a00fa0 100644 --- a/src/Tests/SkipPdfNormalizationTests.NormalizedSnapshotHoldsTheNeutralizedBytes.verified.txt +++ b/src/Tests/SkipPdfNormalizationTests.NormalizedSnapshotHoldsTheNeutralizedBytes.verified.txt @@ -1,10 +1,12 @@ { - Pages: 2, - Settings: { - ContentDirection: LeftToRight, - PDFA_Conformance: None, - PDFUA_Conformance: None, - ImageCompressionQuality: High, - ImageRasterDpi: 288 - } + Document: { + Settings: { + ContentDirection: LeftToRight, + PDFA_Conformance: None, + PDFUA_Conformance: None, + ImageCompressionQuality: High, + ImageRasterDpi: 288 + } + }, + PageCount: 2 } \ No newline at end of file diff --git a/src/Tests/SkipPdfNormalizationTests.SkippedSnapshotHoldsTheProducerBytes#page_0001.verified.png b/src/Tests/SkipPdfNormalizationTests.SkippedSnapshotHoldsTheProducerBytes#page_0001.verified.png new file mode 100644 index 0000000..3518e0c Binary files /dev/null and b/src/Tests/SkipPdfNormalizationTests.SkippedSnapshotHoldsTheProducerBytes#page_0001.verified.png differ diff --git a/src/Tests/SkipPdfNormalizationTests.SkippedSnapshotHoldsTheProducerBytes#page_0002.verified.png b/src/Tests/SkipPdfNormalizationTests.SkippedSnapshotHoldsTheProducerBytes#page_0002.verified.png new file mode 100644 index 0000000..b64422b Binary files /dev/null and b/src/Tests/SkipPdfNormalizationTests.SkippedSnapshotHoldsTheProducerBytes#page_0002.verified.png differ diff --git a/src/Tests/SkipPdfNormalizationTests.SkippedSnapshotHoldsTheProducerBytes.verified.txt b/src/Tests/SkipPdfNormalizationTests.SkippedSnapshotHoldsTheProducerBytes.verified.txt index 2de2447..4a00fa0 100644 --- a/src/Tests/SkipPdfNormalizationTests.SkippedSnapshotHoldsTheProducerBytes.verified.txt +++ b/src/Tests/SkipPdfNormalizationTests.SkippedSnapshotHoldsTheProducerBytes.verified.txt @@ -1,10 +1,12 @@ { - Pages: 2, - Settings: { - ContentDirection: LeftToRight, - PDFA_Conformance: None, - PDFUA_Conformance: None, - ImageCompressionQuality: High, - ImageRasterDpi: 288 - } + Document: { + Settings: { + ContentDirection: LeftToRight, + PDFA_Conformance: None, + PDFUA_Conformance: None, + ImageCompressionQuality: High, + ImageRasterDpi: 288 + } + }, + PageCount: 2 } \ No newline at end of file diff --git a/src/Verify.QuestPDF/GlobalUsings.cs b/src/Verify.QuestPDF/GlobalUsings.cs index 6db31ac..fe5d525 100644 --- a/src/Verify.QuestPDF/GlobalUsings.cs +++ b/src/Verify.QuestPDF/GlobalUsings.cs @@ -1,5 +1,3 @@ global using DeterministicPdf; -global using System.Diagnostics.CodeAnalysis; global using QuestPDF.Fluent; -global using QuestPDF.Infrastructure; -global using VerifyQuestPDF; \ No newline at end of file +global using QuestPDF.Infrastructure; \ No newline at end of file diff --git a/src/Verify.QuestPDF/QuestPDFSettings.cs b/src/Verify.QuestPDF/QuestPDFSettings.cs index 7c682b8..40ffd04 100644 --- a/src/Verify.QuestPDF/QuestPDFSettings.cs +++ b/src/Verify.QuestPDF/QuestPDFSettings.cs @@ -2,47 +2,6 @@ namespace VerifyTests; public static class QuestPDFSettings { - /// - /// Limits the rendered png page snapshots to the first pages. - /// The pdf snapshot is unaffected and always contains the full source document. - /// - public static void PagesToInclude(this VerifySettings settings, int count) => - settings.PagesToInclude(_ => _ <= count); - - /// - public static SettingsTask PagesToInclude(this SettingsTask settings, int count) - { - settings.CurrentSettings.PagesToInclude(_ => _ <= count); - return settings; - } - - /// - /// Limits the rendered png page snapshots to the pages for which - /// returns true. The pdf snapshot is unaffected and always contains the full source - /// document. - /// - public static void PagesToInclude(this VerifySettings settings, ShouldIncludePage include) => - settings.Context["QuestPDF.PagesToInclude"] = include; - - /// - public static SettingsTask PagesToInclude(this SettingsTask settings, ShouldIncludePage include) - { - settings.CurrentSettings.PagesToInclude(include); - return settings; - } - - internal static bool GetPagesToInclude(this IReadOnlyDictionary context, [NotNullWhen(true)] out ShouldIncludePage? include) - { - if (context.TryGetValue("QuestPDF.PagesToInclude", out var value)) - { - include = (ShouldIncludePage) value; - return true; - } - - include = null; - return false; - } - /// /// Snapshots the pdf bytes exactly as produced, skipping the normalization that neutralizes the /// trailer /ID, the /CreationDate and /ModDate, and the XMP dates and diff --git a/src/Verify.QuestPDF/QuestPdfOutputs.cs b/src/Verify.QuestPDF/QuestPdfOutputs.cs deleted file mode 100644 index 2fe687a..0000000 --- a/src/Verify.QuestPDF/QuestPdfOutputs.cs +++ /dev/null @@ -1,25 +0,0 @@ -namespace VerifyTests; - -/// -/// Controls which outputs a QuestPDF document is split into when verified. -/// The pdf target is not controlled here; use VerifierSettings.ExcludeTargets("pdf") for that. -/// -[Flags] -public enum QuestPdfOutputs -{ - /// - /// No outputs. Only the source document (and info) is emitted. - /// - None = 0, - - /// - /// Render each page to a png target. - /// When omitted, page images are not rendered at all. - /// - Png = 1, - - /// - /// All outputs. - /// - All = Png -} diff --git a/src/Verify.QuestPDF/ShouldIncludePage.cs b/src/Verify.QuestPDF/ShouldIncludePage.cs deleted file mode 100644 index 54290c0..0000000 --- a/src/Verify.QuestPDF/ShouldIncludePage.cs +++ /dev/null @@ -1,3 +0,0 @@ -namespace VerifyQuestPDF; - -public delegate bool ShouldIncludePage(int pageNumber); \ No newline at end of file diff --git a/src/Verify.QuestPDF/VerifyQuestPdf.cs b/src/Verify.QuestPDF/VerifyQuestPdf.cs index c7a4016..56ef980 100644 --- a/src/Verify.QuestPDF/VerifyQuestPdf.cs +++ b/src/Verify.QuestPDF/VerifyQuestPdf.cs @@ -6,10 +6,7 @@ public static class VerifyQuestPdf public static bool Initialized { get; private set; } - static QuestPdfOutputs outputs = QuestPdfOutputs.All; - - /// Which outputs a document is split into. Defaults to . - public static void Initialize(QuestPdfOutputs outputs = QuestPdfOutputs.All) + public static void Initialize() { if (Initialized) { @@ -17,7 +14,6 @@ public static void Initialize(QuestPdfOutputs outputs = QuestPdfOutputs.All) } Initialized = true; - VerifyQuestPdf.outputs = outputs; InnerVerifier.ThrowIfVerifyHasBeenRun(); VerifierSettings @@ -30,12 +26,15 @@ public static void Initialize(QuestPdfOutputs outputs = QuestPdfOutputs.All) VerifierSettings.RegisterFileConverter( conversion: (document, settings) => { - var includePng = outputs.HasFlag(QuestPdfOutputs.Png); - // Rasterizing pages is expensive, so only do it when png output is enabled. + // Names the page files, and says whether the verification wants the page images + // and which pages it wants them for. + var conversion = new PagedConversion(settings); + + // Rasterizing pages is expensive, so only do it when the page images are verified. // Otherwise the page count comes from the much cheaper vector svg generation. List pages = []; int pageCount; - if (includePng) + if (conversion.IncludeImages) { pages = document.GenerateImages().ToList(); pageCount = pages.Count; @@ -45,17 +44,18 @@ public static void Initialize(QuestPdfOutputs outputs = QuestPdfOutputs.All) pageCount = document.GenerateSvg().Count; } - if (!settings.GetPagesToInclude(out var pagesToInclude)) - { - pagesToInclude = _ => true; - } // QuestPDF stamps DateTimeOffset.Now into the PDF CreationDate/ModifiedDate on // every generation. Pin them to a fixed value so the info snapshot is stable; the // generated bytes are additionally run through PdfNormalizer below. var metadata = document.GetMetadata(); metadata.CreationDate = deterministicDate; metadata.ModifiedDate = deterministicDate; - List targets = []; + conversion.Info = new + { + Metadata = NullIfEmpty(metadata), + Settings = document.GetSettings(), + }; + // Generating the pdf is expensive, so skip it entirely when the pdf target is excluded. // The rendered pages and info are unaffected. The pdf snapshot is always the full // document, regardless of PagesToInclude: PagesToInclude only trims the png pages below. @@ -71,31 +71,17 @@ public static void Initialize(QuestPdfOutputs outputs = QuestPdfOutputs.All) pdf = PdfNormalizer.Normalize(pdf); } - targets.Add( - new("pdf", new MemoryStream(pdf), performConversion: false) - { - BypassComparersForSubsequentOnDifference = true - }); + // The source of the conversion: compared before the pages, which skip their + // comparers when it differs, and never converted again. + conversion.Source(new("pdf", new MemoryStream(pdf))); } - for (var index = 0; index < pages.Count; index++) - { - if (pagesToInclude(index + 1)) - { - var page = pages[index]; - var stream = new MemoryStream(page); - targets.Add(new("png", stream)); - } - } + // QuestPDF draws every page in one pass, so the pages PagesToInclude leaves out + // were drawn as well. AddImages drops those, and the rest keep their page numbers. + conversion.PageCount = pageCount; + conversion.AddImages(pages); - return new( - info: new - { - Pages = pageCount, - Metadata = NullIfEmpty(metadata), - Settings = document.GetSettings(), - }, - targets); + return conversion.Build(); }); }