SPB Git

Repository Public

IOTests.swift 7.6 KB 151 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project          : ORIX Sheet9// File             : Packages/SheetTemplates/Tests/SheetTemplatesTests/IOTests.swift10// Module           : SheetTemplatesTests11// Repository       : https://git.spboucher.ai/orix-sheet12// Created          : 2026-11-2113// Last Modified    : 2026-11-2114//15// =============================================================================16// AUTHOR & PROFESSIONAL AFFILIATION17// =============================================================================18//19// Author           : Simon-Pierre Boucher20// Position         : Professor of Finance21// Institution      : Université du Québec en Outaouais (UQO)22// Department       : Département des sciences administratives23// Organization     : SP Boucher24//25// Website          : https://spboucher.ai26// Git              : https://git.spboucher.ai27//28// =============================================================================29// OFFICIAL CONTACT DIRECTORY30// =============================================================================31//32// General          : info@spboucher.ai33// Professional     : contact@spboucher.ai34// Direct           : simon@spboucher.ai35// Research         : research@spboucher.ai36// Development      : dev@spboucher.ai37// Support          : support@spboucher.ai38// Security/Admin   : admin@spboucher.ai39// Notifications    : notifications@spboucher.ai40//41// Preferred Contact: support@spboucher.ai42//43// =============================================================================44// COPYRIGHT & INTELLECTUAL PROPERTY45// =============================================================================46//47// Copyright (c) 202648// Simon-Pierre Boucher49//50// All rights reserved where applicable.51//52// This file is governed by the applicable project license,53// copyright notices, institutional agreements and relevant law.54//55// Unless expressly authorized by the applicable license or56// rights holder, copying, redistribution, sublicensing,57// publication and commercial exploitation are not permitted.58//59// Existing third-party rights and notices remain unaffected.60//61// =============================================================================62// CONFIDENTIALITY & SECURITY63// =============================================================================64//65// Classification   : Public66// Distribution     : Public repository; use is governed by the project license67// Security Contact : admin@spboucher.ai68//69// Access to this source does not independently grant permission70// to redistribute or disclose confidential materials.71//72// Do not embed credentials, private keys, access tokens,73// certificates or confidential personal information.74//75// =============================================================================76// LICENSE & MAINTENANCE77// =============================================================================78//79// License          : None specified (no license file in this repository)80// Maintainer       : Simon-Pierre Boucher81// Technical Contact: dev@spboucher.ai82//83// =============================================================================84// END OF SP BOUCHER SOURCE HEADER85// =============================================================================8687import Foundation88import Testing89import SheetCore90import SheetEngine91import SheetIO92@testable import SheetTemplates9394/// Scratch directory of the I/O tests; the reports written there are quoted in the package report.95let scratch: URL = {96    let url = URL(fileURLWithPath: "/tmp/orix-template-io", isDirectory: true)97    try? FileManager.default.createDirectory(at: url, withIntermediateDirectories: true)98    return url99}()100101@Suite struct InputOutput {102    /// Save → reload through the native format loses nothing (cells, formulas, cached values, styles, names, rules, charts…).103    @Test(arguments: Case.all) func nativeRoundTripIsLossless(_ c: Case) throws {104        let wb = BuildCache.shared.get(c).workbook105        let url = scratch.appendingPathComponent("\(c.tag).orixsheet")106        try? FileManager.default.removeItem(at: url)107        let issues = try WorkbookWriter.save(wb, to: url, format: .orixsheet)108        #expect(issues.isEmpty, "\(c.tag): \(issues)")109        #expect(WorkbookWriter.preflight(wb, format: .orixsheet).isEmpty)110        let loaded = try WorkbookReader.load(url: url)111        #expect(loaded.issues.isEmpty, "\(c.tag): \(loaded.issues)")112        let diff = WorkbookDiff.differences(wb, loaded.workbook, options: .exact)113        #expect(diff.isEmpty, "\(c.tag): \(diff.prefix(8))")114        // the reloaded workbook recalculates to the same values115        let engine = CalcEngine(workbook: loaded.workbook)116        engine.recalculate()117        var o = WorkbookCompareOptions(); o.numberTolerance = 1e-12118        #expect(WorkbookDiff.differences(wb, loaded.workbook, options: o).isEmpty, "\(c.tag): values after recalculation")119        #expect(errorCells(loaded.workbook).isEmpty)120    }121122    /// XLSX export: the only announced losses are the embedded charts; values, formulas, names, validations,123    /// conditional formats and layout come back when the file is read again.124    @Test(arguments: Case.all) func xlsxExportKeepsTheModel(_ c: Case) throws {125        let wb = BuildCache.shared.get(c).workbook126        let url = scratch.appendingPathComponent("\(c.tag).xlsx")127        let preflight = WorkbookWriter.preflight(wb, format: .xlsx)128        let issues = try WorkbookWriter.save(wb, to: url, format: .xlsx)129        let lines = preflight.map { "\(c.tag)\t\($0.severity.rawValue)\t\($0.feature)\t\($0.detail)\t\($0.location ?? "")" }130        try lines.joined(separator: "\n").write(to: scratch.appendingPathComponent("\(c.tag).preflight.txt"), atomically: true, encoding: .utf8)131        #expect(preflight.allSatisfy { $0.feature == "Objects" }, "\(c.tag): unexpected preflight issue \(preflight.filter { $0.feature != "Objects" })")132        #expect(Set(issues.map(\.feature)).isSubset(of: ["Objects"]), "\(c.tag): \(issues)")133        let loaded = try WorkbookReader.load(url: url)134        var o = WorkbookCompareOptions()135        o.numberTolerance = 1e-12; o.properties = false; o.styles = false; o.features = false; o.layout = false; o.spillState = false136        let diff = WorkbookDiff.differences(wb, loaded.workbook, options: o)137        try diff.joined(separator: "\n").write(to: scratch.appendingPathComponent("\(c.tag).xlsxdiff.txt"), atomically: true, encoding: .utf8)138        #expect(diff.isEmpty, "\(c.tag): \(diff.prefix(8))")139        // features kept by XLSX140        for (a, b) in zip(wb.sheets, loaded.workbook.sheets) {141            #expect(a.meta.merges.count == b.meta.merges.count && a.meta.validations.count == b.meta.validations.count, "\(c.tag) \(a.name): merges / validations")142            #expect(a.meta.conditionalFormats.count == b.meta.conditionalFormats.count && a.meta.comments.count == b.meta.comments.count, "\(c.tag) \(a.name): rules / notes")143            #expect(a.meta.tables.map(\.name) == b.meta.tables.map(\.name) && a.meta.frozenRows == b.meta.frozenRows && a.meta.frozenCols == b.meta.frozenCols)144            #expect(b.meta.print.landscape == a.meta.print.landscape && b.meta.print.fitToWidth == 1)145        }146        let engine = CalcEngine(workbook: loaded.workbook)147        engine.recalculate()148        #expect(errorCells(loaded.workbook).isEmpty, "\(c.tag): errors after reopening the XLSX")149    }150}151
Files main
Menu

Theme