SPB Git

Repository Public License

Support.swift 7.0 KB 180 lines swift

1// ORIX Convert — Packages/EngineSubtitle/Tests/EngineSubtitleTests/Support.swift2// Description: Fixtures written in code and helpers shared by the subtitle engine tests.3// Author: Simon-Pierre Boucher — contact@spboucher.ai4import ConvertCore5@testable import EngineSubtitle6import Foundation7import Testing89final class Collector: @unchecked Sendable {10    private let lock = NSLock()11    private var items: [ConversionWarning] = []12    func add(_ w: ConversionWarning) { lock.lock(); items.append(w); lock.unlock() }13    var warnings: [ConversionWarning] { lock.lock(); defer { lock.unlock() }; return items }14    func has(_ code: String) -> Bool { warnings.contains { $0.code == code } }15}1617struct Scratch {18    let root: URL19    init() throws {20        root = FileManager.default.temporaryDirectory.appendingPathComponent("orix-subtitle-tests-\(UUID().uuidString)", isDirectory: true)21        try FileManager.default.createDirectory(at: root, withIntermediateDirectories: true)22    }23    func file(_ name: String, _ content: String, encoding: String.Encoding = .utf8) throws -> URL {24        let url = root.appendingPathComponent(name)25        try #require(content.data(using: encoding)).write(to: url)26        return url27    }28    func file(_ name: String, data: Data) throws -> URL {29        let url = root.appendingPathComponent(name)30        try data.write(to: url)31        return url32    }33    func work() throws -> URL {34        let url = root.appendingPathComponent("work-\(UUID().uuidString)", isDirectory: true)35        try FileManager.default.createDirectory(at: url, withIntermediateDirectories: true)36        return url37    }38    func cleanUp() { try? FileManager.default.removeItem(at: root) }39}4041struct Converted {42    let output: ConversionOutput43    let warnings: Collector44    let checks: [ValidationCheck]45    let work: URL46    var url: URL { output.files[0] }47    var text: String { (try? String(contentsOf: url, encoding: .utf8)) ?? "" }48    func document(_ format: FormatID, rate: FrameRate? = nil) throws -> SubtitleDocument {49        try SubtitleParser.parse(url: url, format: format, settings: .init(frameRate: rate))50    }51}5253/// Converts through the engine, then runs the core's generic validation and the engine's own.54func convert(_ source: URL, _ from: FormatID, _ to: FormatID, operation: String? = nil, options: [String: OptionValue] = [:], in scratch: Scratch) async throws -> Converted {55    let engine = SubtitleEngine()56    let work = try scratch.work()57    let collector = Collector()58    let request = ConversionRequest(source: source, sourceFormat: from, destinationFormat: to, operation: operation, options: ConversionOptions(options))59    let context = ConversionContext(workDirectory: work, warn: { collector.add($0) })60    let output = try await engine.convert(request, context: context)61    var checks = OutputValidator.validate(output, destinationFormat: to, workDirectory: work)62    checks += try await engine.validate(output, request: request, source: nil)63    return Converted(output: output, warnings: collector, checks: checks, work: work)64}6566enum Fixtures {67    /// The same six cues in every format (times on centisecond and 25 fps frame boundaries, so every pair is exact).68    static let times: [(Int, Int)] = [(1000, 3000), (4000, 6200), (7000, 9000), (10_000, 12_400), (13_000, 15_000), (3_600_000, 3_602_000)]69    static let plain = ["Hello, world!", "Second line\nwith a break", "Bold and italic", "Ça coûte 5 €, señor — «naïve»", "日本語の字幕", "One hour in"]7071    static let srt = """72    173    00:00:01,000 --> 00:00:03,00074    Hello, world!7576    277    00:00:04,000 --> 00:00:06,20078    Second line79    with a break8081    382    00:00:07,000 --> 00:00:09,00083    <b>Bold</b> and <i>italic</i>8485    486    00:00:10,000 --> 00:00:12,40087    <font color="#FF8000">Ça coûte 5 €, señor — «naïve»</font>8889    590    00:00:13,000 --> 00:00:15,00091    {\\an8}日本語の字幕9293    694    01:00:00,000 --> 01:00:02,00095    One hour in9697    """9899    static let vtt = """100    WEBVTT101102    NOTE a comment block103104    STYLE105    ::cue { color: white; }106107    00:00:01.000 --> 00:00:03.000108    Hello, world!109110    intro111    00:00:04.000 --> 00:00:06.200112    Second line113    with a break114115    00:00:07.000 --> 00:00:09.000116    <b>Bold</b> and <i>italic</i>117118    00:00:10.000 --> 00:00:12.400 align:start119    <c.yellow>Ça coûte 5 €, señor — «naïve»</c>120121    00:00:13.000 --> 00:00:15.000 line:5%122    <v Aiko>日本語の字幕</v>123124    01:00:00.000 --> 01:00:02.000125    One hour in126127    """128129    static func ass(ssa: Bool) -> String {130        let styles = ssa131            ? "[V4 Styles]\nFormat: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, TertiaryColour, BackColour, Bold, Italic, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, AlphaLevel, Encoding\nStyle: Default,Arial,40,16777215,255,0,0,0,0,1,2,1,2,30,30,30,0,1\nStyle: Sign,Georgia,36,65535,255,0,0,-1,0,1,2,1,6,30,30,30,0,1\n"132            : "[V4+ Styles]\nFormat: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, Encoding\nStyle: Default,Arial,40,&H00FFFFFF,&H000000FF,&H00000000,&H80000000,0,0,0,0,100,100,0,0,1,2,1,2,30,30,30,1\nStyle: Sign,Georgia,36,&H0000FFFF,&H000000FF,&H00000000,&H80000000,-1,0,1,0,110,100,2,0,1,2,1,8,30,30,30,1\n"133        let first = ssa ? "Marked=0" : "0"134        let top = ssa ? "\\a6" : "\\an8"135        return """136        [Script Info]137        Title: Fixture138        ScriptType: \(ssa ? "v4.00" : "v4.00+")139        PlayResX: 1280140        PlayResY: 720141142        \(styles)143        [Events]144        Format: \(ssa ? "Marked" : "Layer"), Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text145        Comment: \(first),0:00:00.00,0:00:00.50,Default,,0,0,0,,translator note146        Dialogue: \(first),0:00:01.00,0:00:03.00,Default,,0,0,0,,Hello, world!147        Dialogue: \(first),0:00:04.00,0:00:06.20,Default,,0,0,0,,Second line\\Nwith a break148        Dialogue: \(first),0:00:07.00,0:00:09.00,Default,,0,0,0,,{\\b1}Bold{\\b0} and {\\i1}italic{\\i0}149        Dialogue: \(first),0:00:10.00,0:00:12.40,Sign,,0,0,0,,{\\c&H0080FF&}Ça coûte 5 €, señor — «naïve»150        Dialogue: \(first),0:00:13.00,0:00:15.00,Default,Aiko,0,0,0,,{\(top)}日本語の字幕151        Dialogue: \(first),1:00:00.00,1:00:02.00,Default,,0,0,0,,One hour in152153        """154    }155156    /// 25 fps, with the frame-rate header.157    static let sub = """158    {1}{1}25.000159    {25}{75}Hello, world!160    {100}{155}Second line|with a break161    {175}{225}{y:b}Bold and italic162    {250}{310}{c:$0080FF}Ça coûte 5 €, señor — «naïve»163    {325}{375}日本語の字幕164    {90000}{90050}One hour in165166    """167168    static func text(_ format: FormatID) -> String {169        switch format {170        case .srt: srt171        case .vtt: vtt172        case .ass: ass(ssa: false)173        case .ssa: ass(ssa: true)174        default: sub175        }176    }177178    static func ext(_ format: FormatID) -> String { FormatCatalog.format(format)?.preferredExtension ?? format.rawValue }179}180
Files main
Menu

Theme