Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,27 @@ public static void Initialize()
`VerifyImageMagick.RegisterComparers` (provided by https://github.com/VerifyTests/Verify.ImageMagick) allows minor image changes to be ignored.


### Outputs

By default each pdf is split into a png per page and the text of each page (in the info). `Initialize` accepts a `DocNetOutputs` flags enum to choose which of these are produced globally:

* `Png`: render each page to a png.
* `Text`: extract the text of each page into the info.
* `All` (default): both.

Omitted outputs are not rendered/extracted at all. The source pdf snapshot is not affected (use `VerifierSettings.ExcludeTargets("pdf")` for that).

<!-- snippet: InitializeOutputs -->
<a id='snippet-InitializeOutputs'></a>
```cs
[ModuleInitializer]
public static void Initialize() =>
VerifyDocNet.Initialize(DocNetOutputs.Text);
```
<sup><a href='/src/StaticSettingsTests/ModuleInitializer.cs#L3-L9' title='Snippet source file'>snippet source</a> | <a href='#snippet-InitializeOutputs' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->


### Verify a file

<!-- snippet: VerifyPdf -->
Expand Down
2 changes: 1 addition & 1 deletion src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<Project>
<PropertyGroup>
<NoWarn>CS1591;CS0649;NU1608;NU1109</NoWarn>
<Version>3.6.0</Version>
<Version>3.7.0</Version>
<AssemblyVersion>1.0.0</AssemblyVersion>
<LangVersion>preview</LangVersion>
<SignAssembly>false</SignAssembly>
Expand Down
10 changes: 10 additions & 0 deletions src/StaticSettingsTests/ModuleInitializer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
public static class ModuleInitializer
{
#region InitializeOutputs

[ModuleInitializer]
public static void Initialize() =>
VerifyDocNet.Initialize(DocNetOutputs.Text);

#endregion
}
19 changes: 19 additions & 0 deletions src/StaticSettingsTests/StaticSettingsTests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<OutputType>Exe</OutputType>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="TUnit" />
<PackageReference Include="Verify.TUnit" />
<PackageReference Include="Argon" />
<PackageReference Include="DiffEngine" />
<PackageReference Include="EmptyFiles" />
<PackageReference Include="SimpleInfoName" />
<PackageReference Include="Verify" />
<ProjectReference Include="..\Verify.DocNet\Verify.DocNet.csproj" />
<PackageReference Include="ProjectDefaults" PrivateAssets="all" />

<None Include="..\Tests\sample.pdf" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>
</Project>
Binary file not shown.
29 changes: 29 additions & 0 deletions src/StaticSettingsTests/TextOnlyTests.VerifyPdf.verified.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
Version: 1.4,
PageCount: 2,
Pages: [
{
Text:
A Simple PDF File
This is a small demonstration .pdf file -
just for use in the Virtual Mechanics tutorials. More text. And more
text. And more text. And more text. And more text.
And more text. And more text. And more text. And more text. And more
text. And more text. Boring, zzzzz. And more text. And more text. And
more text. And more text. And more text. And more text. And more text.
And more text. And more text.
And more text. And more text. And more text. And more text. And more
text. And more text. And more text. Even more. Continued on page 2 ...
},
{
Index: 1,
Text:
Simple PDF File 2
...continued from page 1. Yet more text. And more text. And more text.
And more text. And more text. And more text. And more text. And more
text. Oh, how boring typing this stuff. But not as boring as watching
paint dry. And more text. And more text. And more text. And more text.
Boring. More, a little more text. The end, and just as well.
}
]
}
6 changes: 6 additions & 0 deletions src/StaticSettingsTests/TextOnlyTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
public class TextOnlyTests
{
[Test]
public Task VerifyPdf() =>
VerifyFile("sample.pdf");
}
1 change: 1 addition & 0 deletions src/Verify.DocNet.slnx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<File Path="mdsnippets.json" />
<File Path="nuget.md" />
</Folder>
<Project Path="StaticSettingsTests/StaticSettingsTests.csproj" />
<Project Path="Tests/Tests.csproj" />
<Project Path="Verify.DocNet/Verify.DocNet.csproj" />
</Solution>
21 changes: 21 additions & 0 deletions src/Verify.DocNet/DocNetOutputs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
namespace VerifyTests;

/// <summary>
/// Controls which outputs a pdf is split into. Passed to <see cref="VerifyDocNet.Initialize"/>.
/// The source pdf snapshot is not controlled by this (use <c>VerifierSettings.ExcludeTargets("pdf")</c>).
/// </summary>
[Flags]
public enum DocNetOutputs
{
/// <summary>No outputs. Only the source document and info are emitted.</summary>
None = 0,

/// <summary>Render each page to a png. When omitted, pages are not rendered.</summary>
Png = 1,

/// <summary>Extract the text of each page into the info. When omitted, text is not extracted.</summary>
Text = 2,

/// <summary>All outputs.</summary>
All = Png | Text
}
2 changes: 1 addition & 1 deletion src/Verify.DocNet/Verify.DocNet.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net6.0;net7.0;net8.0;net9.0;net10.0</TargetFrameworks>
<TargetFrameworks>net8.0;net9.0;net10.0</TargetFrameworks>
</PropertyGroup>
<ItemGroup>
<InternalsVisibleTo Include="Tests" />
Expand Down
6 changes: 5 additions & 1 deletion src/Verify.DocNet/VerifyDocNet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@ public static partial class VerifyDocNet
{
public static bool Initialized { get; private set; }

public static void Initialize()
static DocNetOutputs outputs = DocNetOutputs.All;

/// <param name="outputs">The outputs each pdf is split into. Defaults to <see cref="DocNetOutputs.All"/>.</param>
public static void Initialize(DocNetOutputs outputs = DocNetOutputs.All)
{
if (Initialized)
{
throw new("Already Initialized");
}

Initialized = true;
VerifyDocNet.outputs = outputs;

InnerVerifier.ThrowIfVerifyHasBeenRun();
VerifierSettings.RegisterStreamConverter("pdf", Convert);
Expand Down
11 changes: 9 additions & 2 deletions src/Verify.DocNet/VerifyDocNet_Pdf.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,21 @@ static ConversionResult Convert(string? name, IDocReader document, IReadOnlyDict
}

var preserveTransparency = settings.GetPreserveTransparency();
var includePng = outputs.HasFlag(DocNetOutputs.Png);
var includeText = outputs.HasFlag(DocNetOutputs.Text);
var targets = new List<Target>();
var pages = new List<PageInfo>();
for (var index = start; index < pagesToInclude; index++)
{
using var reader = document.GetPageReader(index);

pages.Add(new() { Index = index, Text = includeText ? reader.GetText() : null });

if (!includePng)
{
continue;
}

var rawBytes = preserveTransparency ?
reader.GetImage() :
reader.GetImage(transparencyRemover);
Expand All @@ -85,8 +94,6 @@ static ConversionResult Convert(string? name, IDocReader document, IReadOnlyDict
var stream = new MemoryStream();
PngEncoder.WriteBgraAsPng(rawBytes, width, height, stream);
targets.Add(new("png", stream, name));

pages.Add(new() { Index = index, Text = reader.GetText() });
}

var info = new PdfInfo
Expand Down
Loading