SPB Git

Repository Public

JSONTests.swift 11.4 KB 204 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project          : ORIX Sheet9// File             : Packages/SheetIO/Tests/SheetIOTests/JSONTests.swift10// Module           : SheetIOTests11// 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 SheetCore90@testable import SheetIO9192private func json(_ text: String, lines: Bool = false, _ configure: (inout LoadOptions) -> Void = { _ in }) throws -> LoadResult {93    var o = LoadOptions()94    configure(&o)95    return try JSONIO.load(data: Data(text.utf8), lines: lines, sheetName: "J", options: o, progress: nil)96}97private func rows(_ wb: Workbook) -> [[Value]] {98    let s = wb.sheets[0]99    guard let used = s.cells.usedRange() else { return [] }100    return (0...used.r1).map { r in (0...used.c1).map { c in s.value(r, c) } }101}102103@Suite struct JSONTests {104    @Test func arrayOfObjects() throws {105        let r = try json(#"[{"name":"Zoé","age":31,"ok":true,"x":null},{"age":2.5e1,"name":"Bob","extra":"é\u00e9\ud83d\ude00\n"},{}]"#)106        #expect(rows(r.workbook) == [[.text("name"), .text("age"), .text("ok"), .text("x"), .text("extra")],107                                      [.text("Zoé"), .number(31), .bool(true), .blank, .blank],108                                      [.text("Bob"), .number(25), .blank, .blank, .text("éé😀\n")]])109        #expect(r.issues.isEmpty)110        // key order follows first appearance, not alphabetical order111        #expect(rows(try json(#"[{"z":1,"a":2,"m":3}]"#).workbook)[0] == [.text("z"), .text("a"), .text("m")])112        let noHeader = try json(#"[{"a":1},{"a":2}]"#, { $0.json.headerRow = false })113        #expect(rows(noHeader.workbook) == [[.number(1)], [.number(2)]])114    }115116    @Test func arrayOfArraysAndScalars() throws {117        #expect(rows(try json(#"[["a","b"],[1,2.5],[true,null,"x"]]"#).workbook) == [[.text("a"), .text("b"), .blank], [.number(1), .number(2.5), .blank], [.bool(true), .blank, .text("x")]])118        #expect(rows(try json("[1,2,3]").workbook) == [[.number(1)], [.number(2)], [.number(3)]])119        #expect(rows(try json(#"{"a":1,"b":"two"}"#).workbook) == [[.text("a"), .text("b")], [.number(1), .text("two")]])120        #expect(try json("[]").workbook.sheets[0].cells.count == 0)121        #expect(rows(try json("\u{FEFF}[[1]]").workbook) == [[.number(1)]])122    }123124    @Test func nestedValuesAreFlattenedAndReported() throws {125        let r = try json(#"[{"id":1,"tags":["a","b"],"meta":{"k":"v","n":[1,{"deep":true}]}}]"#)126        #expect(cellValue(r.workbook, 0, "B2") == .text(#"["a","b"]"#))127        #expect(cellValue(r.workbook, 0, "C2") == .text(#"{"k":"v","n":[1,{"deep":true}]}"#))128        #expect(r.issues.contains { $0.feature == "Nested values" && $0.severity == .warning && $0.detail.contains("2") })129    }130131    @Test func jsonLines() throws {132        let r = try json("{\"a\":1,\"b\":\"x\"}\n\n{\"b\":\"y\",\"c\":[1]}\r\n[9,8]\n", lines: true)133        #expect(rows(r.workbook) == [[.text("a"), .text("b"), .text("c")], [.number(1), .text("x"), .blank], [.blank, .text("y"), .text("[1]")], [.number(9), .number(8), .blank]])134        #expect(r.issues.contains { $0.feature == "Mixed records" })135        do { _ = try json("{\"a\":1}\n{\"a\":\n", lines: true); Issue.record("bad line accepted") }136        catch let e as SheetIOError { guard case .corrupted(_, let reason) = e else { Issue.record("\(e)"); return }; #expect(reason.contains("line 2")) }137    }138139    @Test func malformedInputAndLimits() throws {140        for bad in ["", "[", "[1,]", "{\"a\"}", "[1] trailing", "{'a':1}", "[\"unterminated]", "nul", "[1e]", "\"\\u12\""] {141            #expect(throws: SheetIOError.self, "\(bad)") { _ = try json(bad) }142        }143        let deep = String(repeating: "[", count: 5000) + String(repeating: "]", count: 5000)144        #expect(throws: SheetIOError.self) { _ = try json(deep) }145        let many = "[" + (0..<30).map { "{\"i\":\($0)}" }.joined(separator: ",") + "]"146        do { _ = try json(many, { $0.json.maxRows = 10 }); Issue.record("limit ignored") }147        catch let e as SheetIOError { guard case .tooLarge(let m) = e else { Issue.record("\(e)"); return }; #expect(m.contains("data engine")) }148    }149150    @Test func writingBothShapes() throws {151        let wb = Workbook(sheetNames: ["T"])152        let s = wb.sheets[0]153        var date = CellStyle(); date.numberFormat = "yyyy-mm-dd hh:mm"154        let dateID = wb.styles.intern(date)155        for (c, h) in ["id", "name", "", "name"].enumerated() where !h.isEmpty { s.cells.set(row: 0, col: c, Cell(value: .text(h))) }156        setCell(s, "A2", .number(1)); setCell(s, "B2", .text("q\"uote\n")); setCell(s, "C2", .bool(false)); setCell(s, "D2", .number(45351.5), style: dateID)157        setCell(s, "A4", .number(0.1)); setFormula(s, "B4", "1/0", cached: .error(.div0))158        func out(_ shape: JSONWriteOptions.Shape, lines: Bool = false, _ configure: (inout JSONWriteOptions) -> Void = { _ in }) throws -> String {159            var o = JSONWriteOptions(); o.shape = shape160            configure(&o)161            return String(decoding: try JSONIO.data(wb, lines: lines, options: o).0, as: UTF8.self)162        }163        #expect(try out(.objects) == ##"[{"id":1,"name":"q\"uote\n","column3":false,"name_2":"2024-02-29T12:00:00"},{"id":null,"name":null,"column3":null,"name_2":null},{"id":0.1,"name":"#DIV/0!","column3":null,"name_2":null}]"##)164        #expect(try out(.arrays) == ##"[["id","name",null,"name"],[1,"q\"uote\n",false,"2024-02-29T12:00:00"],[null,null,null,null],[0.1,"#DIV/0!",null,null]]"##)165        #expect(try out(.arrays, { $0.datesAsISO8601 = false }).contains("45351.5"))166        #expect(try out(.objects, lines: true).split(separator: "\n").count == 3)167        #expect(try out(.arrays, { $0.prettyPrinted = true }).hasPrefix("[\n  [\"id\""))168        // every output is valid JSON for Foundation169        for text in [try out(.objects), try out(.arrays), try out(.arrays, { $0.prettyPrinted = true })] { _ = try JSONSerialization.jsonObject(with: Data(text.utf8)) }170        for line in try out(.arrays, lines: true).split(separator: "\n") { _ = try JSONSerialization.jsonObject(with: Data(line.utf8)) }171        #expect(String(decoding: try JSONIO.data(Workbook(sheetNames: ["e"]), lines: false, options: JSONWriteOptions()).0, as: UTF8.self) == "[]")172    }173174    @Test func roundTripThroughFiles() throws {175        let dir = Scratch.dir("json")176        defer { try? FileManager.default.removeItem(at: dir) }177        var g = RandomWorkbook(seed: 21)178        let wb = Workbook(sheetNames: ["data"])179        for c in 0..<6 { wb.sheets[0].cells.set(row: 0, col: c, Cell(value: .text("key \(c) é\"\\"))) }180        for r in 1..<200 { for c in 0..<6 where g.bool(0.85) {181            let v: Value = g.bool() ? .number(g.double()) : (g.bool() ? .text(g.string()) : .bool(g.bool()))182            if case .text(let t) = v, t.isEmpty { continue }183            wb.sheets[0].cells.set(row: r, col: c, Cell(value: v))184        } }185        // the last row must be complete so that trailing blank rows do not differ186        for c in 0..<6 { wb.sheets[0].cells.set(row: 200, col: c, Cell(value: .number(Double(c)))) }187        var o = WorkbookCompareOptions(); o.styles = false; o.layout = false; o.features = false188        for (format, shape) in [(FileFormat.json, JSONWriteOptions.Shape.objects), (.json, .arrays), (.jsonl, .objects), (.jsonl, .arrays)] {189            var so = SaveOptions(); so.json.shape = shape190            let sub = dir.appendingPathComponent(shape.rawValue)191            try FileManager.default.createDirectory(at: sub, withIntermediateDirectories: true)192            let url = sub.appendingPathComponent("data.\(format.rawValue)")193            try WorkbookWriter.save(wb, to: url, format: format, options: so)194            let back = try WorkbookReader.load(url: url).workbook195            let diff = WorkbookDiff.differences(wb, back, options: o)196            #expect(diff.isEmpty, "\(format) \(shape): \(diff.prefix(4))")197        }198        if Tools.hasPython {199            let r = try Tools.run(Tools.python, ["-I", "-c", "import json,sys; d=json.load(open(sys.argv[1], encoding='utf-8')); print(len(d), list(d[0].keys())[0])", dir.appendingPathComponent("objects/data.json").path])200            #expect(r.out.trimmingCharacters(in: .whitespacesAndNewlines) == "200 key 0 é\"\\", "\(r.err)")201        }202    }203}204
Files main
Menu

Theme