From f1078396d24b7e1ccfb784e9f8c4ea51927240f0 Mon Sep 17 00:00:00 2001 From: Philip Niedertscheider Date: Sat, 10 Oct 2026 23:25:12 +0200 Subject: [PATCH] test(integration): Migrate PDF generation specs to Swift Testing --- .../EmptyDocumentTests.swift | 33 +- .../ExternalDocumentTests.swift | 426 ++---------------- 2 files changed, 48 insertions(+), 411 deletions(-) diff --git a/Tests/TPPDFIntegrationTests/EmptyDocumentTests.swift b/Tests/TPPDFIntegrationTests/EmptyDocumentTests.swift index 064e45bf..9260b971 100644 --- a/Tests/TPPDFIntegrationTests/EmptyDocumentTests.swift +++ b/Tests/TPPDFIntegrationTests/EmptyDocumentTests.swift @@ -7,32 +7,27 @@ // import Foundation -import Nimble -import Quick +import Testing @testable import TPPDF #if canImport(PDFKit) import PDFKit -@available(iOS 11.0, *) -class EmptyDocumentTests: QuickSpec { - override func spec() { - describe("Empty Document") { - it("it should have a single page") { - let document = PDFDocument(format: .usLetter) - let generator = PDFGenerator(document: document) - generator.debug = true +struct EmptyDocumentTests { + @Test func generatingEmptyDocumentWithDebugOverlayProducesOnePage() throws { + // Arrange + let document = TPPDF.PDFDocument(format: .usLetter) + let generator = PDFGenerator(document: document) + generator.debug = true - var outputURL: URL! = nil - expect { - outputURL = try generator.generateURL(filename: "output.pdf") - }.toNot(throwError()) - expect(generator.totalPages) == 1 + // Act + let data = try generator.generateData() + let outputDocument = PDFKit.PDFDocument(data: data) + let output = try #require(outputDocument) - let outputDoc = PDFKit.PDFDocument(url: outputURL) - expect(outputDoc?.pageCount) == 1 - } - } + // Assert + #expect(generator.totalPages == 1) + #expect(output.pageCount == 1) } } #endif diff --git a/Tests/TPPDFIntegrationTests/ExternalDocumentTests.swift b/Tests/TPPDFIntegrationTests/ExternalDocumentTests.swift index 3688f749..e09f080d 100644 --- a/Tests/TPPDFIntegrationTests/ExternalDocumentTests.swift +++ b/Tests/TPPDFIntegrationTests/ExternalDocumentTests.swift @@ -7,405 +7,47 @@ // import Foundation -import Nimble -import Quick +import Testing @testable import TPPDF #if canImport(PDFKit) import PDFKit -@available(iOS 11.0, *) -class ExternalDocumentTests: QuickSpec { - // swiftlint:disable closure_body_length function_body_length - override func spec() { - describe("External Document") { - context("only document") { - context("debug mode") { - // Test Case: - // Precondition: - // - Document is empty - // - Debug mode is active - // Expected Result: - // - Render all pages in external document - // - Do not render anything else than external document pages - // Notes: - // - This test needs to be run with and without debug mode to validate side effects - it("should draw the pdf pages with debug overlay") { - let document = PDFDocument(format: .a4) - document.add(externalDocument: .init(url: Bundle.module.url(forResource: "50-pages", withExtension: "pdf")!)) - - let generator = PDFGenerator(document: document) - generator.debug = true - - var outputURL: URL! = nil - expect { - outputURL = try generator.generateURL(filename: "output.pdf") - }.toNot(throwError()) - expect(generator.totalPages) == 50 - - let outputDoc = PDFKit.PDFDocument(url: outputURL) - expect(outputDoc?.pageCount) == 50 - - // TODO: compare output PDF to reference PDF - } - } - - context("normal mode") { - // Test Case: - // Precondition: - // - Document is empty - // - Debug mode is not active - // Expected Result: - // - Render all pages in external document - // - Do not render anything else than external document pages - // Notes: - // - This test needs to be run with and without debug mode to validate side effects - it("should draw the pdf pages") { - let document = PDFDocument(format: .a4) - document.add(externalDocument: .init(url: Bundle.module.url(forResource: "50-pages", withExtension: "pdf")!)) - - let generator = PDFGenerator(document: document) - generator.debug = false - - var outputURL: URL! = nil - expect { - outputURL = try generator.generateURL(filename: "output.pdf") - }.toNot(throwError()) - expect(generator.totalPages) == 50 - - let outputDoc = PDFKit.PDFDocument(url: outputURL) - expect(outputDoc?.pageCount) == 50 - - // TODO: compare output PDF to reference PDF - } - } - } - - context("content before document") { - context("debug mode") { - // Test Case: - // Precondition: - // - Document contains content - // - Debug mode is active - // Expected Result: - // - Render content before external document - // - Render all pages in external document - // Notes: - // - This test needs to be run with and without debug mode to validate side effects - it("should draw the pdf pages with debug overlay") { - let document = PDFDocument(format: .a4) - document.add(text: "Text before external document") - document.add(externalDocument: .init(url: Bundle.module.url(forResource: "50-pages", withExtension: "pdf")!)) - - let generator = PDFGenerator(document: document) - generator.debug = true - - var outputURL: URL! = nil - expect { - outputURL = try generator.generateURL(filename: "output.pdf") - }.toNot(throwError()) - expect(generator.totalPages) == 51 - - let outputDoc = PDFKit.PDFDocument(url: outputURL) - expect(outputDoc?.pageCount) == 51 - - // TODO: compare output PDF to reference PDF - } - } - - context("normal mode") { - // Test Case: - // Precondition: - // - Document contains content - // - Debug mode is not active - // Expected Result: - // - Render content before external document - // - Render all pages in external document - // Notes: - // - This test needs to be run with and without debug mode to validate side effects - it("should draw the pdf pages") { - let document = PDFDocument(format: .a4) - document.add(text: "Text before external document") - document.add(externalDocument: .init(url: Bundle.module.url(forResource: "50-pages", withExtension: "pdf")!)) - - let generator = PDFGenerator(document: document) - generator.debug = false - - var outputURL: URL! = nil - expect { - outputURL = try generator.generateURL(filename: "output.pdf") - }.toNot(throwError()) - expect(generator.totalPages) == 51 - - let outputDoc = PDFKit.PDFDocument(url: outputURL) - expect(outputDoc?.pageCount) == 51 - - // TODO: compare output PDF to reference PDF - } - } - } - - context("content after document") { - context("debug mode") { - // Test Case: - // Precondition: - // - Document is empty before adding external document - // - More content is added after external document - // - Debug mode is active - // Expected Result: - // - Render all pages in external document - // - Render content after external document - // Notes: - // - This test needs to be run with and without debug mode to validate side effects - it("should draw the pdf pages with debug overlay") { - let document = PDFDocument(format: .a4) - document.add(externalDocument: .init(url: Bundle.module.url(forResource: "50-pages", withExtension: "pdf")!)) - document.add(text: "Text after external document") - - let generator = PDFGenerator(document: document) - generator.debug = true - - var outputURL: URL! = nil - expect { - outputURL = try generator.generateURL(filename: "output.pdf") - }.toNot(throwError()) - expect(generator.totalPages) == 51 - - let outputDoc = PDFKit.PDFDocument(url: outputURL) - expect(outputDoc?.pageCount) == 51 - - // TODO: compare output PDF to reference PDF - } - } - - context("normal mode") { - // Test Case: - // Precondition: - // - Document is empty before adding external document - // - More content is added after external document - // - Debug mode is not active - // Expected Result: - // - Render all pages in external document - // - Render content after external document - // Notes: - // - This test needs to be run with and without debug mode to validate side effects - it("should draw the pdf pages") { - let document = PDFDocument(format: .a4) - document.add(externalDocument: .init(url: Bundle.module.url(forResource: "50-pages", withExtension: "pdf")!)) - document.add(text: "Text after external document") - - let generator = PDFGenerator(document: document) - generator.debug = false - - var outputURL: URL! = nil - expect { - outputURL = try generator.generateURL(filename: "output.pdf") - }.toNot(throwError()) - expect(generator.totalPages) == 51 - - let outputDoc = PDFKit.PDFDocument(url: outputURL) - expect(outputDoc?.pageCount) == 51 - - // TODO: compare output PDF to reference PDF - } - } - } - - context("document between content") { - context("debug mode") { - // Test Case: - // Precondition: - // - Document has content before external documnet - // - More content is added after external document - // - Debug mode is active - // Expected Result: - // - Render content before external document - // - Render all pages in external document - // - Render content after external document - // Notes: - // - This test needs to be run with and without debug mode to validate side effects - it("should draw the pdf pages with debug overlay") { - let document = PDFDocument(format: .a4) - document.add(text: "Text before external document") - document.add(externalDocument: .init(url: Bundle.module.url(forResource: "50-pages", withExtension: "pdf")!)) - document.add(text: "Text after external document") - - let generator = PDFGenerator(document: document) - generator.debug = true - - var outputURL: URL! = nil - expect { - outputURL = try generator.generateURL(filename: "output.pdf") - }.toNot(throwError()) - expect(generator.totalPages) == 52 - - let outputDoc = PDFKit.PDFDocument(url: outputURL) - expect(outputDoc?.pageCount) == 52 - - // TODO: compare output PDF to reference PDF - } - } - - context("normal mode") { - // Test Case: - // Precondition: - // - Document has content before external documnet - // - More content is added after external document - // - Debug mode is not active - // Expected Result: - // - Render content before external document - // - Render all pages in external document - // - Render content after external document - // Notes: - // - This test needs to be run with and without debug mode to validate side effects - it("should draw the pdf pages") { - let document = PDFDocument(format: .a4) - document.add(text: "Text before external document") - document.add(externalDocument: .init(url: Bundle.module.url(forResource: "50-pages", withExtension: "pdf")!)) - document.add(text: "Text after external document") - - let generator = PDFGenerator(document: document) - generator.debug = false - - var outputURL: URL! = nil - expect { - outputURL = try generator.generateURL(filename: "output.pdf") - }.toNot(throwError()) - expect(generator.totalPages) == 52 - - let outputDoc = PDFKit.PDFDocument(url: outputURL) - expect(outputDoc?.pageCount) == 52 - - // TODO: compare output PDF to reference PDF - } - } - } - - context("multiple external documents") { - context("debug mode") { - // Test Case: - // Precondition: - // - Document contains 2 external documents - // - Debug mode is active - // Expected Result: - // - Render content of each document after another - // Notes: - // - This test needs to be run with and without debug mode to validate side effects - it("should merge two external documents with debug overlay") { - let document = PDFDocument(format: .a4) - document.add(externalDocument: .init(url: Bundle.module.url(forResource: "50-pages", withExtension: "pdf")!)) - document.add(externalDocument: .init(url: Bundle.module.url(forResource: "50-pages", withExtension: "pdf")!)) - - let generator = PDFGenerator(document: document) - generator.debug = true - - var outputURL: URL! = nil - expect { - outputURL = try generator.generateURL(filename: "output.pdf") - }.toNot(throwError()) - expect(generator.totalPages) == 100 - - let outputDoc = PDFKit.PDFDocument(url: outputURL) - expect(outputDoc?.pageCount) == 100 - - // TODO: compare output PDF to reference PDF - } - - // Test Case: - // Precondition: - // - Document contains 2 external documents - // - Debug mode is active - // Expected Result: - // - Render content of each document after another - // Notes: - // - This test needs to be run with and without debug mode to validate side effects - it("should merge many external documents with debug overlay") { - let document = PDFDocument(format: .a4) - document.add(externalDocument: .init(url: Bundle.module.url(forResource: "50-pages", withExtension: "pdf")!)) - document.add(externalDocument: .init(url: Bundle.module.url(forResource: "50-pages", withExtension: "pdf")!)) - document.add(externalDocument: .init(url: Bundle.module.url(forResource: "50-pages", withExtension: "pdf")!)) - document.add(externalDocument: .init(url: Bundle.module.url(forResource: "50-pages", withExtension: "pdf")!)) - document.add(externalDocument: .init(url: Bundle.module.url(forResource: "50-pages", withExtension: "pdf")!)) - - let generator = PDFGenerator(document: document) - generator.debug = true - - var outputURL: URL! = nil - expect { - outputURL = try generator.generateURL(filename: "output.pdf") - }.toNot(throwError()) - expect(generator.totalPages) == 250 - - let outputDoc = PDFKit.PDFDocument(url: outputURL) - expect(outputDoc?.pageCount) == 250 - - // TODO: compare output PDF to reference PDF - } - } - - context("normal mode") { - // Test Case: - // Precondition: - // - Document contains 5 external documents - // - Debug mode is not active - // Expected Result: - // - Render content of each document after another - // Notes: - // - This test needs to be run with and without debug mode to validate side effects - it("should merge two external documents") { - let document = PDFDocument(format: .a4) - document.add(externalDocument: .init(url: Bundle.module.url(forResource: "50-pages", withExtension: "pdf")!)) - document.add(externalDocument: .init(url: Bundle.module.url(forResource: "50-pages", withExtension: "pdf")!)) - - let generator = PDFGenerator(document: document) - generator.debug = false - - var outputURL: URL! = nil - expect { - outputURL = try generator.generateURL(filename: "output.pdf") - }.toNot(throwError()) - expect(generator.totalPages) == 100 - - let outputDoc = PDFKit.PDFDocument(url: outputURL) - expect(outputDoc?.pageCount) == 100 - - // TODO: compare output PDF to reference PDF - } - - // Test Case: - // Precondition: - // - Document contains 5 external documents - // - Debug mode is not active - // Expected Result: - // - Render content of each document after another - // Notes: - // - This test needs to be run with and without debug mode to validate side effects - it("should merge many external documents") { - let document = PDFDocument(format: .a4) - document.add(externalDocument: .init(url: Bundle.module.url(forResource: "50-pages", withExtension: "pdf")!)) - document.add(externalDocument: .init(url: Bundle.module.url(forResource: "50-pages", withExtension: "pdf")!)) - document.add(externalDocument: .init(url: Bundle.module.url(forResource: "50-pages", withExtension: "pdf")!)) - document.add(externalDocument: .init(url: Bundle.module.url(forResource: "50-pages", withExtension: "pdf")!)) - document.add(externalDocument: .init(url: Bundle.module.url(forResource: "50-pages", withExtension: "pdf")!)) - - let generator = PDFGenerator(document: document) - generator.debug = false - - var outputURL: URL! = nil - expect { - outputURL = try generator.generateURL(filename: "output.pdf") - }.toNot(throwError()) - expect(generator.totalPages) == 250 +struct ExternalDocumentTests { + @Test(arguments: [ + (before: false, after: false, documents: 1, pages: 50), + (before: true, after: false, documents: 1, pages: 51), + (before: false, after: true, documents: 1, pages: 51), + (before: true, after: true, documents: 1, pages: 52), + (before: false, after: false, documents: 2, pages: 100), + (before: false, after: false, documents: 5, pages: 250), + ], [false, true]) + func externalDocumentsPreservePageCountsWithSurroundingContentAndDebugMode( + scenario: (before: Bool, after: Bool, documents: Int, pages: Int), debug: Bool + ) throws { + // Arrange + let url = try #require(Bundle.module.url(forResource: "50-pages", withExtension: "pdf")) + let document = TPPDF.PDFDocument(format: .a4) + if scenario.before { + document.add(text: "Text before external document") + } + for _ in 0..