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
91 changes: 91 additions & 0 deletions src/Tests/PdfNormalizerTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
using System.Text;
using Docnet.Core;

[TestFixture]
public class PdfNormalizerTests
{
[Test]
public void NeutralizesVolatileValues()
{
var input =
"/ID [<A1B2C3D4E5F60718> <1122334455667788>] " +
"/CreationDate(D:20240115093000+05'30') " +
"/ModDate(D:20240115093000Z) " +
"<xmp:CreateDate>2024-01-15T09:30:00+05:30</xmp:CreateDate>" +
"<xmp:ModifyDate>2024-01-15T09:30:00Z</xmp:ModifyDate>" +
"<xmp:MetadataDate>2024-01-15T09:30:00Z</xmp:MetadataDate>" +
"<xmpMM:DocumentID>uuid:0f7b2c9a-1234-5678-9abc-def012345678</xmpMM:DocumentID>" +
"<xmpMM:InstanceID>xmp.iid:1a2b3c4d</xmpMM:InstanceID>";
var expected =
"/ID [<0000000000000000> <0000000000000000>] " +
"/CreationDate(D:00000000000000+00'00') " +
"/ModDate(D:00000000000000Z) " +
"<xmp:CreateDate>0000-00-00T00:00:00+00:00</xmp:CreateDate>" +
"<xmp:ModifyDate>0000-00-00T00:00:00Z</xmp:ModifyDate>" +
"<xmp:MetadataDate>0000-00-00T00:00:00Z</xmp:MetadataDate>" +
$"<xmpMM:DocumentID>{new string('0', 41)}</xmpMM:DocumentID>" +
$"<xmpMM:InstanceID>{new string('0', 16)}</xmpMM:InstanceID>";
Assert.That(Normalize(input), Is.EqualTo(expected));
}

[Test]
public void CollapsesDifferingValuesToTheSameOutput()
{
// The same producer emits a stable structure across runs, so two documents differing only
// in the volatile digits/hex normalize to identical bytes.
var a = "/ID [<A1B2C3D4>] /CreationDate(D:20240115093000+05'30')";
var b = "/ID [<99887766>] /CreationDate(D:19991231235959+11'45')";
Assert.That(a, Is.Not.EqualTo(b));
Assert.That(Normalize(a), Is.EqualTo(Normalize(b)));
}

[Test]
public void LeavesLookalikeKeysUntouched()
{
// /IDTree is a name-tree key (not the file identifier), /ModDateStamp is a different name,
// and a self-closing date element has no content: none should be altered.
var input = "/IDTree [1 2] /ModDateStamp(20240101) <xmp:CreateDate/>2024";
Assert.That(Normalize(input), Is.EqualTo(input));
}

[Test]
public void NormalizedDocumentStillLoads()
{
var data = File.ReadAllBytes("sample.pdf");
PdfNormalizer.Normalize(data);

using var reader = DocLib.Instance.GetDocReader(data, new(scalingFactor: 2));
Assert.That(reader.GetPageCount(), Is.EqualTo(2));
}

[Test]
public void IsIdempotent()
{
// A second pass has nothing left to change: normalizing already-normalized bytes is a no-op.
var once = File.ReadAllBytes("sample.pdf");
PdfNormalizer.Normalize(once);
var twice = (byte[])once.Clone();
PdfNormalizer.Normalize(twice);
Assert.That(twice, Is.EqualTo(once));
}

[Test]
public void NormalizedSinglePageSplitStillLoads()
{
// A page subset is re-serialized by pdfium (reintroducing volatile fields) then normalized;
// it must remain a valid one-page document.
var data = File.ReadAllBytes("sample.pdf");
var split = DocLib.Instance.Split(data, 1, 1);
PdfNormalizer.Normalize(split);

using var reader = DocLib.Instance.GetDocReader(split, new(scalingFactor: 2));
Assert.That(reader.GetPageCount(), Is.EqualTo(1));
}

static string Normalize(string value)
{
var bytes = Encoding.Latin1.GetBytes(value);
PdfNormalizer.Normalize(bytes);
return Encoding.Latin1.GetString(bytes);
}
}
21 changes: 21 additions & 0 deletions src/Tests/PdfSnapshotTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System.Runtime.CompilerServices;
using Docnet.Core;

[TestFixture]
public class PdfSnapshotTests
{
// The SinglePage snapshots are subset to only the rendered page, so the accepted pdf must load
// as a one-page document. A readable guard alongside the opaque binary verified files.
[TestCase("Samples.VerifyFirstPage#pdf.verified.pdf", 1)]
[TestCase("Samples.VerifySecondPage#pdf.verified.pdf", 1)]
[TestCase("Samples.VerifyPdf#pdf.verified.pdf", 2)]
public void SnapshotHasExpectedPageCount(string file, int expectedPages)
{
var data = File.ReadAllBytes(Path.Combine(SourceDirectory(), file));
using var reader = DocLib.Instance.GetDocReader(data, new(scalingFactor: 2));
Assert.That(reader.GetPageCount(), Is.EqualTo(expectedPages));
}

static string SourceDirectory([CallerFilePath] string path = "") =>
Path.GetDirectoryName(path)!;
}
Binary file not shown.
19 changes: 19 additions & 0 deletions src/Tests/Samples.VerifyFirstPage.verified.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
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 ...
}
]
}
Binary file not shown.
29 changes: 29 additions & 0 deletions src/Tests/Samples.VerifyPageDimensions.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.
}
]
}
Binary file added src/Tests/Samples.VerifyPdf#pdf.verified.pdf
Binary file not shown.
29 changes: 29 additions & 0 deletions src/Tests/Samples.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.
}
]
}
Binary file not shown.
29 changes: 29 additions & 0 deletions src/Tests/Samples.VerifyPdfStream.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.
}
]
}
Binary file not shown.
29 changes: 29 additions & 0 deletions src/Tests/Samples.VerifyPreserveTransparency.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.
}
]
}
Binary file not shown.
16 changes: 16 additions & 0 deletions src/Tests/Samples.VerifySecondPage.verified.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
Version: 1.4,
PageCount: 2,
Pages: [
{
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.
}
]
}
8 changes: 8 additions & 0 deletions src/Verify.DocNet/PageInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class PageInfo
{
/// <summary>Zero based index of the page within the source document.</summary>
public int Index { get; init; }

/// <summary>Text extracted from the page, or null when the page has no text.</summary>
public string? Text { get; init; }
}
13 changes: 13 additions & 0 deletions src/Verify.DocNet/PdfInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class PdfInfo
{
public string Version { get; init; } = "";

/// <summary>
/// Total number of pages in the source document. This is the full count regardless of any
/// <c>PagesToInclude</c>/<c>SinglePage</c> filter; the pages actually rendered are listed in
/// <see cref="Pages"/>, each carrying its <see cref="PageInfo.Index"/> within the document.
/// </summary>
public int PageCount { get; init; }

public IReadOnlyList<PageInfo> Pages { get; init; } = [];
}
Loading
Loading