SPB Git

Repository Public

ReferenceTests.swift 1.9 KB 49 lines swift

1import Foundation2import Testing3import SheetCore4@testable import SheetFormat56struct FormatCase: Decodable {7    enum V: Decodable {8        case number(Double), text(String)9        init(from decoder: Decoder) throws {10            let c = try decoder.singleValueContainer()11            if let d = try? c.decode(Double.self) { self = .number(d) } else { self = .text(try c.decode(String.self)) }12        }13    }14    var v: V15    var f: String16    var r: String17    var value: Value {18        switch v {19        case .number(let d): .number(d)20        case .text(let s): .text(s)21        }22    }23}2425func loadFixture<T: Decodable>(_ name: String, as: T.Type = T.self) throws -> T {26    let url = try #require(Bundle.module.url(forResource: name, withExtension: "json", subdirectory: "Fixtures"))27    return try JSONDecoder().decode(T.self, from: Data(contentsOf: url))28}2930@Suite struct LibreOfficeReferenceTests {31    @Test func rendersLikeLibreOffice() throws {32        let cases: [FormatCase] = try loadFixture("libreoffice_formats")33        #expect(cases.count > 4000)34        var failures: [String] = []35        var checked = 036        var deviations: [String: Int] = [:]37        for c in cases {38            let got = NumberFormat.cached(c.f).render(c.value, generalMaxChars: nil).text39            if got == c.r { checked += 1; continue }40            if let why = ExcelDeviations.explain(c, got: got) { deviations[why, default: 0] += 1; continue }41            failures.append("TEXT(\(c.v), \"\(c.f)\") → \"\(got)\" (LibreOffice \"\(c.r)\")")42        }43        print("LibreOffice format references: \(checked) identical, \(deviations.values.reduce(0, +)) documented Excel deviations, \(failures.count) failures")44        for (why, n) in deviations.sorted(by: { $0.key < $1.key }) { print("  deviation: \(why): \(n)") }45        for f in failures.prefix(40) { print("  " + f) }46        #expect(failures.isEmpty)47    }48}49
Files main
Menu

Theme