SPB Git

Repository Public

ReviewTestSupport.swift 5.6 KB 136 lines swift

1// ORIX Docs2// File: Packages/ORIXDocsKit/Tests/DocReviewTests/ReviewTestSupport.swift3// Description: Test helpers: local accept-all / reject-all of tracked changes, text extraction, document builders, a seeded generator.4//5// Author: Simon-Pierre Boucher6// Contact: contact@spboucher.ai7import Foundation8import DocModel910enum Resolution { case accept, reject }1112/// Minimal accept-all / reject-all, local to the tests (the product implementation lives in DocModel).13/// Accept keeps insertions and drops deletions; reject does the opposite and restores `FormatRevision.previous`.14/// A paragraph whose mark carries the dropped kind disappears when it is empty, otherwise it joins the next paragraph.15/// A table cell whose paragraphs were all dropped disappears, and so does a row without cells and a table without rows.16func resolve(_ blocks: [Block], _ mode: Resolution) -> [Block] {17    let drop: Revision.Kind = mode == .accept ? .deletion : .insertion18    func paragraphs(_ list: [Paragraph]) -> [Paragraph] {19        var out: [Paragraph] = []20        var pending: Paragraph?21        for p in list {22            var q = p23            q.runs = p.runs.compactMap { r in24                if r.revision?.kind == drop { return nil }25                var c = r26                c.revision = nil27                if let f = c.formatRevision, mode == .reject { c.props = f.previous; c.charStyle = f.previousCharStyle }28                c.formatRevision = nil29                return c30            }31            q.markRevision = nil32            q.normalize()33            let joins = p.markRevision?.kind == drop34            if var head = pending { head.append(contentsOf: q); q = head; pending = nil }35            if joins {36                if !q.runs.isEmpty { pending = q }37            } else { out.append(q) }38        }39        if let p = pending { out.append(p) }40        return out41    }42    var out: [Block] = []43    var run: [Paragraph] = []44    func flush() { out.append(contentsOf: paragraphs(run).map { .paragraph($0) }); run = [] }45    for b in blocks {46        switch b {47        case .paragraph(let p): run.append(p)48        case .table(var t):49            flush()50            t.rows = t.rows.compactMap { row in51                var r = row52                r.cells = row.cells.compactMap { cell in53                    var c = cell54                    let ps = paragraphs(cell.paragraphs)55                    if ps.isEmpty { return nil }56                    c.paragraphs = ps57                    return c58                }59                return r.cells.isEmpty ? nil : r60            }61            if !t.rows.isEmpty { out.append(.table(t)) }62        default:63            flush()64            out.append(b)65        }66    }67    flush()68    return out69}7071func resolve(_ doc: DocumentModel, _ mode: Resolution) -> DocumentModel {72    var d = doc73    d.body = resolve(doc.body, mode)74    for (id, n) in doc.notes { d.notes[id]?.paragraphs = resolve(n.paragraphs.map { .paragraph($0) }, mode).compactMap(\.paragraph) }75    for (si, _) in doc.sectionSetups.enumerated() {76        for v in HeaderFooterVariant.allCases {77            for story in [StoryID.header(section: si, variant: v), .footer(section: si, variant: v)] {78                let blocks = doc.blocks(in: story)79                if !blocks.isEmpty { d.setBlocks(resolve(blocks, mode), in: story) }80            }81        }82    }83    return d84}8586/// The text of a document, one line per paragraph or table row; referenced notes follow their paragraph. Breaks and generated blocks are skipped.87func textLines(_ doc: DocumentModel) -> [String] {88    var out: [String] = []89    func add(_ p: Paragraph, prefix: String = "") {90        out.append(prefix + p.plainText)91        for r in p.runs { if case .note(let ref)? = r.inline { for np in doc.notes[ref.id]?.paragraphs ?? [] { out.append("  note: " + np.plainText) } } }92    }93    for b in doc.body {94        switch b {95        case .paragraph(let p): add(p)96        case .table(let t):97            for row in t.rows { out.append("| " + row.cells.map { $0.paragraphs.map(\.plainText).joined(separator: "¶") }.joined(separator: " | ")) }98        default: break99        }100    }101    for (si, su) in doc.sectionSetups.enumerated() {102        for p in su.header.default { add(p, prefix: "header \(si): ") }103        for p in su.footer.default { add(p, prefix: "footer \(si): ") }104    }105    return out106}107108func document(_ paragraphs: [String]) -> DocumentModel {109    DocumentModel(body: paragraphs.map { .paragraph(Paragraph($0)) })110}111112func paragraphs(of doc: DocumentModel) -> [Paragraph] { doc.body.compactMap(\.paragraph) }113114/// SplitMix64: a small deterministic generator so the randomised tests are reproducible.115struct SeededGenerator: RandomNumberGenerator {116    var state: UInt64117    init(seed: UInt64) { state = seed }118    mutating func next() -> UInt64 {119        state &+= 0x9E37_79B9_7F4A_7C15120        var z = state121        z = (z ^ (z >> 30)) &* 0xBF58_476D_1CE4_E5B9122        z = (z ^ (z >> 27)) &* 0x94D0_49BB_1331_11EB123        return z ^ (z >> 31)124    }125}126127let vocabulary = ["market", "yield", "curve", "volatility", "regression", "sample", "estimate", "result", "table", "figure", "model", "data",128                  "return", "risk", "factor", "portfolio", "liquidity", "inflation", "policy", "rate", "économie", "marché", "résultat", "naïve",129                  "the", "of", "and", "in", "to", "is", "that", "for", "with", "as", "on", "by", "we", "this", "are", "from"]130131func sentence(_ g: inout SeededGenerator, words: Int) -> String {132    var parts: [String] = []133    for _ in 0..<words { parts.append(vocabulary.randomElement(using: &g)!) }134    return parts.joined(separator: " ") + "."135}136
Files main
Menu

Theme