SPB Git

Repository Public License

Support.swift 4.6 KB 91 lines swift

1// ORIX Convert — Packages/EngineFont/Tests/EngineFontTests/Support.swift2// Description: Test fixtures for the font engine: system fonts copied at test time into a scratch folder (never3//              committed), and helpers to run a conversion with the core's and the engine's validation.4// Author: Simon-Pierre Boucher — contact@spboucher.ai5import ConvertCore6@testable import EngineFont7import Foundation8import Testing910final class Collector: @unchecked Sendable {11    private let lock = NSLock()12    private var items: [ConversionWarning] = []13    private var fractions: [Double?] = []14    func add(_ w: ConversionWarning) { lock.lock(); items.append(w); lock.unlock() }15    func add(_ p: ProgressUpdate) { lock.lock(); fractions.append(p.fraction); lock.unlock() }16    var warnings: [ConversionWarning] { lock.lock(); defer { lock.unlock() }; return items }17    var progress: [Double?] { lock.lock(); defer { lock.unlock() }; return fractions }18    func has(_ code: String) -> Bool { warnings.contains { $0.code == code } }19}2021/// System fonts used as local test inputs. They are read where macOS installs them and copied to a scratch folder.22enum SystemFont: String, CaseIterable, Sendable {23    /// TrueType outlines, DSIG table.24    case glyf = "/System/Library/Fonts/Supplemental/Arial Rounded Bold.ttf"25    /// TrueType outlines, larger.26    case geneva = "/System/Library/Fonts/Geneva.ttf"27    /// PostScript (CFF) outlines.28    case cff = "/System/Library/Fonts/Supplemental/STIXSizOneSymBol.otf"29    case cffText = "/System/Library/Fonts/Supplemental/NotoSerifYezidi-Regular.otf"30    /// Variable font (fvar + gvar).31    case variable = "/System/Library/Fonts/SFHebrew.ttf"32    /// Collection of two TrueType fonts.33    case collection = "/System/Library/Fonts/Supplemental/KufiStandardGK.ttc"34    /// Collection of five fonts, Restricted + Preview & Print (fsType 6).35    case futura = "/System/Library/Fonts/Supplemental/Futura.ttc"36    /// Restricted License embedding (fsType 2).37    case restricted = "/System/Library/Fonts/Supplemental/Ayuthaya.ttf"3839    var url: URL { URL(fileURLWithPath: rawValue) }40    var format: FormatID { rawValue.hasSuffix(".otf") ? .otf : .ttf }41}4243struct Scratch {44    let root: URL45    init() throws {46        root = FileManager.default.temporaryDirectory.appendingPathComponent("orix-font-tests-\(UUID().uuidString)", isDirectory: true)47        try FileManager.default.createDirectory(at: root, withIntermediateDirectories: true)48    }49    /// Copies a system font into the scratch folder.50    func fixture(_ font: SystemFont) throws -> URL {51        try #require(FileManager.default.fileExists(atPath: font.rawValue), "system font missing: \(font.rawValue)")52        let url = root.appendingPathComponent(font.url.lastPathComponent)53        if !FileManager.default.fileExists(atPath: url.path) { try FileManager.default.copyItem(at: font.url, to: url) }54        return url55    }56    func file(_ name: String, _ data: Data) throws -> URL {57        let url = root.appendingPathComponent(name)58        try data.write(to: url)59        return url60    }61    func work() throws -> URL {62        let url = root.appendingPathComponent("work-\(UUID().uuidString)", isDirectory: true)63        try FileManager.default.createDirectory(at: url, withIntermediateDirectories: true)64        return url65    }66    func cleanUp() { try? FileManager.default.removeItem(at: root) }67}6869struct Converted {70    let output: ConversionOutput71    let collector: Collector72    let checks: [ValidationCheck]73    var url: URL { output.files[0] }74    var data: Data { (try? Data(contentsOf: url)) ?? Data() }75}7677func convert(_ source: URL, _ from: FormatID, _ to: FormatID, operation: String? = nil, options: [String: OptionValue] = [:], in scratch: Scratch) async throws -> Converted {78    let engine = FontEngine()79    let work = try scratch.work()80    let collector = Collector()81    let request = ConversionRequest(source: source, sourceFormat: from, destinationFormat: to, operation: operation, options: ConversionOptions(options))82    let output = try await engine.convert(request, context: ConversionContext(workDirectory: work, progress: { collector.add($0) }, warn: { collector.add($0) }))83    var checks = OutputValidator.validate(output, destinationFormat: to, workDirectory: work)84    checks += try await engine.validate(output, request: request, source: nil)85    return Converted(output: output, collector: collector, checks: checks)86}8788func expectAllPass(_ checks: [ValidationCheck], _ label: String, sourceLocation: SourceLocation = #_sourceLocation) {89    for check in checks { #expect(check.passed, "\(label): \(check.name) — \(check.detail)", sourceLocation: sourceLocation) }90}91
Files main
Menu

Theme