SPB Git

Repository Public

AuditTestSupport.swift 2.9 KB 58 lines swift

1// ORIX Docs2// File: Packages/ORIXDocsKit/Tests/DocAuditTests/AuditTestSupport.swift3// Description: Builders shared by the auditor and research-assistant tests.4//5// Author: Simon-Pierre Boucher6// Contact: contact@spboucher.ai7import Foundation8import DocModel9import DocAudit1011/// A document with complete metadata, so `missingMetadata` stays quiet unless a test wants it.12func makeDocument(_ blocks: [Block], language: String = "en") -> DocumentModel {13    DocumentModel(meta: DocumentMetadata(title: "Test document", author: "Tester", language: language), pageSetup: PageSetup(paper: .letter), body: blocks)14}1516func para(_ text: String, style: String = StyleID.normal) -> Block { .paragraph(Paragraph(text, style: style)) }17func heading(_ level: Int, _ text: String) -> Block { .paragraph(Paragraph(text, style: StyleID.heading(level))) }1819func captionParagraph(_ label: String, _ text: String) -> Paragraph {20    var p = Paragraph(style: StyleID.caption)21    p.runs = [Run("\(label) "), Run(inline: .field(.sequence(label))), Run(": \(text)")]22    return p23}2425func imageParagraph(alt: String = "A chart", asset: String = "img1") -> Paragraph {26    var p = Paragraph()27    p.runs = [Run(inline: .image(ImageObject(asset: asset, width: 300, height: 200, altText: alt)))]28    return p29}3031func findings(_ doc: DocumentModel, _ check: AuditCheck, layout: LayoutFacts? = nil, options: AuditOptions = AuditOptions(),32              incompleteReference: ((BibEntry) -> [String])? = nil) -> [AuditFinding] {33    DocumentAuditor.audit(doc, layout: layout, options: options, incompleteReference: incompleteReference).filter { $0.check == check }34}3536/// Filler prose of exactly `words` words, without acronyms, double spaces or repeated phrases.37func prose(_ words: Int, seed: Int = 0) -> String {38    let pool = ["river", "stone", "garden", "window", "letter", "morning", "village", "harbour", "silver", "quiet", "walks", "beyond", "under", "bright",39                "paper", "summer", "winter", "bridge", "candle", "forest", "meadow", "copper", "velvet", "orchard", "lantern", "pebble", "thunder", "feather",40                "marble", "saddle", "ribbon", "timber", "anchor", "basket", "cellar", "dagger", "ember", "fabric", "gravel", "hollow", "island", "jacket"]41    var out: [String] = []42    var state = UInt64(seed) &* 6364136223846793005 &+ 144269504088896340743    for i in 0..<words {44        state = state &* 6364136223846793005 &+ 144269504088896340745        out.append(pool[Int((state >> 33) % UInt64(pool.count))] + (i % 9 == 8 && i + 1 < words ? "." : ""))46    }47    var s = out.joined(separator: " ")48    s = s.prefix(1).uppercased() + s.dropFirst() + "."49    // Capitalise after each period so sentence detection sees ordinary sentences.50    var result = ""51    var capitalise = false52    for ch in s {53        if capitalise && ch.isLetter { result += ch.uppercased(); capitalise = false } else { result.append(ch) }54        if ch == "." { capitalise = true }55    }56    return result57}58
Files main
Menu

Theme