SPB Git

Repository Public License

TextTests.swift 5.3 KB 83 lines swift

1// ORIX Convert — Packages/EnginePDF/Tests/EnginePDFTests/TextTests.swift2// Description: PDF → TXT / Markdown, hyphenation repair, ordering, and the scanned-document behaviour.3// Author: Simon-Pierre Boucher — contact@spboucher.ai4import Foundation5import CoreGraphics6import Testing7import ConvertCore8@testable import EnginePDF910extension PDFEngineTests {11    @Suite struct TextExport {12        @Test func plainTextWithSeparators() async throws {13            let pdf = Fx.simplePDF(pages: 3, word: "Kiwi")14            let blank = Fx.text(try await Fx.run(Fx.request(pdf, to: .txt)).file)15            #expect(blank.contains("Kiwi document page 1") && blank.contains("KiwiPAGE3"))16            #expect(blank.range(of: "KiwiPAGE1")!.lowerBound < blank.range(of: "KiwiPAGE2")!.lowerBound)17            #expect(blank.contains("zebra crossing\n\nKiwi document page 2"))18            let marker = Fx.text(try await Fx.run(Fx.request(pdf, to: .txt, ["text.page-separator": .string("marker")])).file)19            #expect(marker.hasPrefix("----- Page 1 -----\n") && marker.contains("----- Page 3 -----"))20            let feed = Fx.text(try await Fx.run(Fx.request(pdf, to: .txt, ["text.page-separator": .string("formfeed")])).file)21            #expect(feed.filter { $0 == "\u{0C}" }.count == 2)22            let selected = try await Fx.run(Fx.request(pdf, to: .txt, ["pages": .string("2-")]))23            #expect(!Fx.text(selected.file).contains("KiwiPAGE1") && Fx.text(selected.file).contains("KiwiPAGE2"))24            #expect(selected.output.facts["pages"] == "2")25        }2627        @Test func markdownAddsOnlyPageHeadings() async throws {28            let pdf = Fx.simplePDF(pages: 2, word: "Mango")29            let md = Fx.text(try await Fx.run(Fx.request(pdf, to: .markdown)).file)30            #expect(md.hasPrefix("## Page 1\n\nMango document page 1"))31            #expect(md.contains("\n\n## Page 2\n\n"))32            #expect(md.components(separatedBy: "#").count - 1 == 4)   // two "##" headings, nothing else invented33        }3435        @Test func hyphenationRepair() async throws {36            #expect(PDFText.repairHyphenation("an exam-\nple and a list:\n- item\nwell-\nKnown X-\n1") == "an example and a list:\n- item\nwell-\nKnown X-\n1")37            let pdf = Fx.textPDF("hyphen.pdf", pages: [Fx.PageSpec(lines: ["The conversion was remark-", "able and the docu-", "ment survived"])])38            let kept = Fx.text(try await Fx.run(Fx.request(pdf, to: .txt)).file)39            #expect(kept.contains("remark-\nable"))40            let repaired = Fx.text(try await Fx.run(Fx.request(pdf, to: .txt, ["text.dehyphenate": .bool(true)])).file)41            #expect(repaired.contains("remarkable") && repaired.contains("document survived"))42        }4344        @Test func positionalOrderSortsTopToBottom() async throws {45            let url = Fx.dir("fixture").appendingPathComponent("scrambled.pdf")46            let ctx = CGContext(url as CFURL, mediaBox: nil, nil)!47            var box = CGRect(x: 0, y: 0, width: 612, height: 792)48            ctx.beginPage(mediaBox: &box)49            Fx.draw("Bottomline stored first", at: CGPoint(x: 72, y: 200), size: 14, in: ctx)50            Fx.draw("Topline stored second", at: CGPoint(x: 72, y: 700), size: 14, in: ctx)51            Fx.draw("Middleline stored third", at: CGPoint(x: 72, y: 450), size: 14, in: ctx)52            ctx.endPage(); ctx.closePDF()53            let text = Fx.text(try await Fx.run(Fx.request(url, to: .txt, ["text.order": .string("position")])).file)54            let top = try #require(text.range(of: "Topline")), middle = try #require(text.range(of: "Middleline")), bottom = try #require(text.range(of: "Bottomline"))55            #expect(top.lowerBound < middle.lowerBound && middle.lowerBound < bottom.lowerBound)56        }5758        @Test func scannedDocumentIsReportedAndReadWithOCR() async throws {59            let pdf = Fx.scannedPDF("scan-text.pdf", dpi: 200)60            let off = Fx.request(pdf, to: .txt, ["ocr": .string("off")])61            #expect(Fx.engine.preflight(off, inspection: nil).contains { $0.code == "scanned-no-text" && $0.message.contains("scanned document") && $0.message.contains("use OCR") })62            await #expect(throws: ConversionError.self) { try await Fx.run(off) }63            #expect(Fx.engine.preflight(Fx.request(pdf, to: .txt), inspection: nil).contains { $0.code == "scanned-ocr" })64            do {65                let result = try await Fx.run(Fx.request(pdf, to: .txt))66                let text = Fx.text(result.file)67                for probe in Fx.scanProbes { #expect(text.contains(probe), "missing \(probe)") }68                #expect(result.hasWarning("ocr-used"))69                #expect(result.output.facts["pages.ocr"] == "1")70                let md = try await Fx.run(Fx.request(pdf, to: .markdown, ["ocr.level": .string("fast"), "ocr.language": .string("en-US")]))71                #expect(Fx.text(md.file).contains("Quantum"))72            } catch ConversionError.engineUnavailable(let reason) {73                withKnownIssue("Vision text recognition unavailable: \(reason)") { Issue.record("skipped") }74            }75        }7677        @Test func unsupportedLanguageIsRejected() async throws {78            let pdf = Fx.scannedPDF("scan-lang.pdf", dpi: 100)79            await #expect(throws: ConversionError.self) { try await Fx.run(Fx.request(pdf, to: .txt, ["ocr.languages": .string("xx-XX")])) }80        }81    }82}83
Files main
Menu

Theme