SPB Git

Repository Public License

TreeTests.swift 29.9 KB 520 lines swift

1// ORIX Convert — Packages/EngineData/Tests/EngineDataTests/TreeTests.swift2// Description: Tree formats (JSON, YAML, TOML, XML, property lists): every declared tree path, codec exactness,3//              the XML mapping convention, and hostile documents (XXE, entity bombs, alias bombs, deep nesting).4// Author: Simon-Pierre Boucher — contact@spboucher.ai5import Foundation6import Testing7import ConvertCore8@testable import EngineData910let treePaths: [PathCase] = DataEngine().paths.filter { FormatClass.isTreePath($0.source, $0.destination) }.map { PathCase(source: $0.source, destination: $0.destination) }1112enum TreeFixtures {13    static let json = """14    {15      "title": "héllo \\"wörld\\" 😀 שלום",16      "count": 42,17      "ratio": 0.5,18      "big": 123456789012345678901234567890,19      "exact": 0.1234567890123456789012,20      "negative": -9007199254740993,21      "enabled": true,22      "nothing": null,23      "empty": "",24      "tags": ["a", "b c", "line1\\nline2"],25      "owner": {"name": "Ada", "age": 36},26      "items": [{"id": 1, "label": "first"}, {"id": 2, "label": "second"}]27    }28    """29    static let yaml = """30    # a comment31    title: "héllo \\"wörld\\" 😀 שלום"32    count: 4233    ratio: 0.534    big: 12345678901234567890123456789035    enabled: true36    nothing: ~37    empty: ""38    quoted_number: "007"39    day: 2024-02-2940    moment: 2024-02-29T13:45:10Z41    tags: [a, "b c", "line1\\nline2"]42    owner:43      name: Ada44      age: 3645    items:46      - id: 147        label: first48      - id: 249        label: second50    """51    static let toml = """52    # a comment53    title = "héllo \\"wörld\\" 😀 שלום"54    count = 4255    ratio = 0.556    hex = 0xFF57    enabled = true58    empty = ""59    day = 2024-02-2960    local = 2024-02-29T13:45:1061    moment = 2024-02-29T13:45:10.5+02:0062    clock = 13:45:1063    tags = ["a", "b c", 'literal \\n']64    point = { x = 1, y = 2 }6566    [owner]67    name = "Ada"68    age = 366970    [[items]]71    id = 172    label = "first"7374    [[items]]75    id = 276    label = "second"77    """78    static let xml = """79    <?xml version="1.0" encoding="UTF-8"?>80    <!-- a comment -->81    <catalog version="2" xmlns:x="urn:example">82      <title>héllo &quot;wörld&quot; 😀 שלום</title>83      <count>42</count>84      <empty/>85      <owner id="7"><name>Ada</name><age>36</age></owner>86      <item id="1"><label>first</label></item>87      <item id="2"><label><![CDATA[sec<ond]]></label></item>88      <x:note lang="en">text with attribute</x:note>89    </catalog>90    """91    static func plist() throws -> Data {92        let root: [String: Any] = [93            "title": "héllo \"wörld\" 😀 שלום", "count": 42, "ratio": 0.5, "enabled": true, "empty": "",94            "moment": Date(timeIntervalSince1970: 1_709_214_310), "blob": Data([0, 1, 2, 255]),95            "tags": ["a", "b c", "line1\nline2"], "owner": ["name": "Ada", "age": 36],96            "items": [["id": 1, "label": "first"], ["id": 2, "label": "second"]],97        ]98        return try PropertyListSerialization.data(fromPropertyList: root, format: .binary, options: 0)99    }100101    static func write(_ format: FormatID, in scratch: Scratch) throws -> URL {102        let url = scratch.file("doc.\(FormatCatalog.format(format)?.preferredExtension ?? format.rawValue)")103        switch format {104        case .json: try json.write(to: url, atomically: true, encoding: .utf8)105        case .yaml: try yaml.write(to: url, atomically: true, encoding: .utf8)106        case .toml: try toml.write(to: url, atomically: true, encoding: .utf8)107        case .xml: try xml.write(to: url, atomically: true, encoding: .utf8)108        default: try plist().write(to: url)109        }110        return url111    }112}113114/// Applies to a tree what a path is documented to change, so that the leaf checksum can be compared.115func transformed(_ v: TreeValue, source: FormatID, destination: FormatID) -> TreeValue {116    switch v {117    case .string(let s) where s.isEmpty && (source == .xml || destination == .xml): return .null118    case .date(let d) where destination == .plist && d.kind == .offsetDateTime:119        // XML property lists store dates to the second.120        guard let p = CivilDate.parseISODateTime(d.text), let off = p.offsetSeconds else { return v }121        let us = p.micros - Int64(off) * 1_000_000122        return .date(TreeDate(kind: .offsetDateTime, text: CivilDate.isoTimestamp(micros: us - ((us % 1_000_000) + 1_000_000) % 1_000_000) + "Z"))123    case .array(let a): return .array(a.map { transformed($0, source: source, destination: destination) })124    case .object(let o): return .object(TreeObject(o.members.map { ($0.key, transformed($0.value, source: source, destination: destination)) }))125    default: return v126    }127}128129@Suite("Tree formats")130struct TreeTests {131    @Test("every declared tree path converts, validates and keeps every value", arguments: treePaths)132    func path(_ p: PathCase) async throws {133        let scratch = Scratch("tree")134        let source = try TreeFixtures.write(p.source, in: scratch)135        let converted = try await runConversion(source, p.source, to: p.destination, in: scratch)136        let checks = try await DataEngine().validate(converted.output, request: converted.request, source: nil)137        #expect(!checks.isEmpty)138        for c in checks { #expect(c.passed, "validation “\(c.name)” failed: \(c.detail)") }139        let original = try TreeIO.parse(source, format: p.source).value140        let reread = try TreeIO.parse(converted.file, format: p.destination).value141        let want = TreeIO.leafChecksum(transformed(original, source: p.source, destination: p.destination))142        let got = TreeIO.leafChecksum(reread)143        #expect(got.count == want.count, "values: \(got.count) read back, \(want.count) expected")144        #expect(got.sum == want.sum, "the values read back differ from the source")145        if p.source == .toml, p.destination == .plist { #expect(converted.warnings.codes.contains("plist-subsecond-dropped")) }146    }147148    // MARK: JSON149150    @Test func jsonKeepsOrderBigIntegersAndDecimalText() throws {151        let r = try JSONParser.parse(Data(TreeFixtures.json.utf8))152        guard case .object(let o) = r.value else { Issue.record("not an object"); return }153        #expect(o.members.map(\.key) == ["title", "count", "ratio", "big", "exact", "negative", "enabled", "nothing", "empty", "tags", "owner", "items"])154        #expect(o["big"] == .bigInteger("123456789012345678901234567890"))155        #expect(o["exact"] == .decimal("0.1234567890123456789012"))156        #expect(o["negative"] == .integer(-9_007_199_254_740_993))157        #expect(o["ratio"] == .double(0.5))158        #expect(o["title"] == .string("héllo \"wörld\" 😀 שלום"))159        var s = JSONSerializer(pretty: true)160        let again = try JSONParser.parse(s.serialize(r.value))161        #expect(again.value == r.value)162        var compact = JSONSerializer(pretty: false)163        let text = String(decoding: compact.serialize(r.value), as: UTF8.self)164        #expect(text.contains("\"big\":123456789012345678901234567890"))165        #expect(text.contains("\"exact\":0.1234567890123456789012"))166    }167168    @Test func jsonEscapesSurrogatesAndNumbers() throws {169        let r = try JSONParser.parse(Data(#"["\ud83d\ude00", "\u00e9\n\t\\\/", 1e3, -0.0, 1E-2, 10000000000000000000, 1.0]"#.utf8))170        #expect(r.value == .array([.string("😀"), .string("é\n\t\\/"), .double(1000), .double(-0.0), .double(0.01), .bigInteger("10000000000000000000"), .double(1)]))171        var s = JSONSerializer(pretty: false)172        #expect(String(decoding: s.serialize(.array([.string("a\u{1}\"\\\n"), .double(1), .double(.nan)])), as: UTF8.self) == #"["a\u0001\"\\\n",1.0,null]"#)173        #expect(s.tally.counts["non-finite-dropped"] == 1)174    }175176    @Test(arguments: ["{", "[1,]", "{\"a\":}", "tru", "\"unterminated", "[1] x", "{\"a\":1,}", "01", "\"\u{1}\"", "", "[\"\\x\"]", "nul"])177    func jsonRejectsMalformed(_ text: String) {178        #expect(throws: ConversionError.self) { _ = try JSONParser.parse(Data(text.utf8)) }179    }180181    @Test func jsonBoundsNestingAndReportsDuplicateKeys() throws {182        let scratch = Scratch("deep")183        for (format, open, close) in [(FormatID.json, "[", "]"), (.yaml, "[", "]"), (.xml, "<a>", "</a>"), (.toml, "a = [", "]")] {184            let url = scratch.file("deep.\(format.rawValue)")185            try (String(repeating: open, count: format == .toml ? 1 : 100_000) + (format == .toml ? String(repeating: "[", count: 100_000) + String(repeating: "]", count: 100_000) : "") + String(repeating: close, count: format == .toml ? 1 : 100_000)).write(to: url, atomically: true, encoding: .utf8)186            #expect(throws: ConversionError.self, "\(format) accepted 100 000 levels of nesting") { _ = try TreeIO.parse(url, format: format) }187        }188        let r = try JSONParser.parse(Data(#"{"a":1,"b":2,"a":3}"#.utf8))189        #expect(r.duplicateKeys == 1)190        #expect(r.value == .object(TreeObject([("a", .integer(3)), ("b", .integer(2))])))191    }192193    @Test func jsonToYAMLAndBackIsExact() async throws {194        let scratch = Scratch("tree")195        let src = try TreeFixtures.write(.json, in: scratch)196        let y = try await runConversion(src, .json, to: .yaml, in: scratch)197        #expect(y.warnings.all.isEmpty)198        let back = try await runConversion(y.file, .yaml, to: .json, in: scratch)199        #expect(try TreeIO.parse(back.file, format: .json).value == TreeIO.parse(src, format: .json).value)200    }201202    // MARK: YAML203204    @Test func yamlScalarsAreTyped() throws {205        let v = try YAMLCodec.parse(Data(TreeFixtures.yaml.utf8)).value206        guard case .object(let o) = v else { Issue.record("not an object"); return }207        #expect(o["count"] == .integer(42))208        #expect(o["big"] == .bigInteger("123456789012345678901234567890"))209        #expect(o["nothing"] == .null)210        #expect(o["empty"] == .string(""))211        #expect(o["quoted_number"] == .string("007"))212        #expect(o["day"] == .date(TreeDate(kind: .localDate, text: "2024-02-29")))213        #expect(o["moment"] == .date(TreeDate(kind: .offsetDateTime, text: "2024-02-29T13:45:10Z")))214        #expect(o.members.map(\.key).prefix(3) == ["title", "count", "ratio"])215    }216217    @Test func yamlWriterQuotesAmbiguousStrings() throws {218        let tricky = ["true", "null", "~", "123", "1.5", "1e3", "", "2024-02-29", " leading", "a: b", "# c", "multi\nline", "tab\there", "é 😀", "-", "yes", "0x1F", "[x]", "{y}", "'q'", "\"dq\"", "trailing ", "@at", "`tick`", "*star", "&amp", "!bang", "%pct", ".inf", "+1"]219        var tally = LossTally()220        let value = TreeValue.object(TreeObject([("strings", .array(tricky.map { .string($0) })), ("true", .string("key that looks like a bool")), ("f", .double(1e300)), ("n", .double(.nan)), ("i", .double(-.infinity)), ("d", .decimal("1e-400"))]))221        let data = try YAMLCodec.serialize(value, tally: &tally)222        let back = try YAMLCodec.parse(data).value223        guard case .object(let o) = back else { Issue.record("not an object"); return }224        #expect(o["strings"] == .array(tricky.map { .string($0) }))225        #expect(o["true"] == .string("key that looks like a bool"))226        #expect(o["f"] == .double(1e300))227        if case .double(let n)? = o["n"] { #expect(n.isNaN) } else { Issue.record("nan lost") }228        #expect(o["i"] == .double(-.infinity))229    }230231    @Test func yamlAliasBombIsBounded() {232        var doc = "a0: &a0 [x, x, x, x, x, x, x, x, x, x]\n"233        for i in 1...12 { doc += "a\(i): &a\(i) [" + Array(repeating: "*a\(i - 1)", count: 10).joined(separator: ", ") + "]\n" }234        #expect(throws: ConversionError.self) { _ = try YAMLCodec.parse(Data(doc.utf8)) }235    }236237    @Test(arguments: ["a: [1, 2", "a: b: c: {", "\t- bad\n  indent: [", "? [complex]\n: 1"])238    func yamlRejectsMalformed(_ text: String) {239        #expect(throws: ConversionError.self) { _ = try YAMLCodec.parse(Data(text.utf8)) }240    }241242    // MARK: TOML243244    @Test func tomlParsesTheSpecFeatures() throws {245        let v = try TOMLCodec.parse(Data(TreeFixtures.toml.utf8))246        guard case .object(let o) = v else { Issue.record("not an object"); return }247        #expect(o["hex"] == .integer(255))248        #expect(o["day"] == .date(TreeDate(kind: .localDate, text: "2024-02-29")))249        #expect(o["local"] == .date(TreeDate(kind: .localDateTime, text: "2024-02-29T13:45:10")))250        #expect(o["moment"] == .date(TreeDate(kind: .offsetDateTime, text: "2024-02-29T13:45:10.5+02:00")))251        #expect(o["clock"] == .date(TreeDate(kind: .localTime, text: "13:45:10")))252        #expect(o["tags"] == .array([.string("a"), .string("b c"), .string("literal \\n")]))253        #expect(o["point"] == .object(TreeObject([("x", .integer(1)), ("y", .integer(2))])))254        #expect(o["items"] == .array([.object(TreeObject([("id", .integer(1)), ("label", .string("first"))])), .object(TreeObject([("id", .integer(2)), ("label", .string("second"))]))]))255        let more = try TOMLCodec.parse(Data("""256        a.b.c = 1257        "quoted key" = 'x'258        multi = \"\"\"259        first \\260           second\"\"\"261        lit = '''262        raw \\n'''263        bin = 0b1010264        oct = 0o17265        under = 1_000_000266        f1 = 6.626e-34267        f2 = -inf268        arr = [ 1, 2, # comment269                3, ]270        nested = [[1, 2], ["a"]]271        date2 = 1979-05-27 07:32:00Z272        [[fruit]]273        name = "apple"274        [fruit.physical]275        color = "red"276        [[fruit.variety]]277        name = "red delicious"278        [[fruit]]279        name = "banana"280        """.utf8))281        guard case .object(let m) = more else { Issue.record("not an object"); return }282        #expect(m["a"] == .object(TreeObject([("b", .object(TreeObject([("c", .integer(1))])))])))283        #expect(m["quoted key"] == .string("x"))284        #expect(m["multi"] == .string("first second"))285        #expect(m["lit"] == .string("raw \\n"))286        #expect(m["bin"] == .integer(10))287        #expect(m["oct"] == .integer(15))288        #expect(m["under"] == .integer(1_000_000))289        #expect(m["f1"] == .double(6.626e-34))290        #expect(m["f2"] == .double(-.infinity))291        #expect(m["arr"] == .array([.integer(1), .integer(2), .integer(3)]))292        #expect(m["date2"] == .date(TreeDate(kind: .offsetDateTime, text: "1979-05-27T07:32:00Z")))293        guard case .array(let fruit)? = m["fruit"], fruit.count == 2, case .object(let apple) = fruit[0] else { Issue.record("array of tables"); return }294        #expect(apple["physical"] == .object(TreeObject([("color", .string("red"))])))295        #expect(apple["variety"] == .array([.object(TreeObject([("name", .string("red delicious"))]))]))296    }297298    @Test(arguments: ["a = 1\na = 2", "[t]\n[t]", "a = ", "a = 01", "a = 1__0", "s = \"unterminated", "a = {x = 1\n}", "[a]\nb = 1\n[a.b]", "x = 2024-13-01", "a = [1 2]", "= 1", "a = +", "t = { a = 1 }\nt.b = 2", "a = 1 b = 2", "s = \"line \\\n  continued\""])299    func tomlRejectsInvalid(_ text: String) {300        #expect(throws: ConversionError.self) { _ = try TOMLCodec.parse(Data(text.utf8)) }301    }302303    @Test func tomlNullPolicyAndRootWrapping() async throws {304        let scratch = Scratch("tree")305        let src = scratch.file("n.json")306        try #"{"b":{"c":null,"d":1},"a":null,"e":[1,null,2]}"#.write(to: src, atomically: true, encoding: .utf8)307        let omitted = try await runConversion(src, .json, to: .toml, in: scratch)308        #expect(omitted.warnings.codes.contains("null-omitted"))309        #expect(omitted.warnings.all.first { $0.code == "null-omitted" }?.message.hasPrefix("3 ") == true)310        #expect(try TreeIO.parse(omitted.file, format: .toml).value == .object(TreeObject([("e", .array([.integer(1), .integer(2)])), ("b", .object(TreeObject([("d", .integer(1))])))])))311        #expect(omitted.warnings.codes.contains("key-order-changed"))312        await #expect(throws: ConversionError.self) { _ = try await runConversion(src, .json, to: .toml, in: scratch, options: ["nulls.policy": .string("error")]) }313        let list = scratch.file("l.json")314        try "[1, 2]".write(to: list, atomically: true, encoding: .utf8)315        let wrapped = try await runConversion(list, .json, to: .toml, in: scratch)316        #expect(wrapped.warnings.codes.contains("toml-root-wrapped"))317        #expect(try String(contentsOf: wrapped.file, encoding: .utf8) == "rows = [1, 2]\n")318    }319320    // MARK: XML321322    @Test func xmlMappingConvention() throws {323        let r = try XMLTreeCodec.parse(Data(TreeFixtures.xml.utf8))324        #expect(r.notes.comments == 1)325        guard case .object(let root) = r.value, case .object(let c)? = root["catalog"] else { Issue.record("no root"); return }326        #expect(c["@version"] == .string("2"))327        #expect(c["@xmlns:x"] == .string("urn:example"))328        #expect(c["title"] == .string("héllo \"wörld\" 😀 שלום"))329        #expect(c["count"] == .string("42"))330        #expect(c["empty"] == .null)331        #expect(c["owner"] == .object(TreeObject([("@id", .string("7")), ("name", .string("Ada")), ("age", .string("36"))])))332        #expect(c["item"] == .array([.object(TreeObject([("@id", .string("1")), ("label", .string("first"))])), .object(TreeObject([("@id", .string("2")), ("label", .string("sec<ond"))]))]))333        #expect(c["x:note"] == .object(TreeObject([("@lang", .string("en")), ("#text", .string("text with attribute"))])))334        // The convention is reversible: writing the tree and parsing it again gives the same tree.335        var w = XMLTreeCodec.Writer()336        let again = try XMLTreeCodec.parse(w.serialize(r.value))337        #expect(again.value == r.value)338    }339340    @Test func xmlRefusesExternalEntities() throws {341        let scratch = Scratch("xxe")342        let secret = scratch.file("secret.txt")343        try "TOP-SECRET-CONTENT".write(to: secret, atomically: true, encoding: .utf8)344        let doc = "<?xml version=\"1.0\"?>\n<!DOCTYPE r [ <!ENTITY xxe SYSTEM \"\(secret.absoluteString)\"> ]>\n<r>&xxe;</r>"345        #expect(throws: ConversionError.self) { _ = try XMLTreeCodec.parse(Data(doc.utf8)) }346        // Even with the byte-level check bypassed (UTF-16), the parser delegate refuses the declaration.347        let utf16 = doc.replacingOccurrences(of: "<?xml version=\"1.0\"?>", with: "<?xml version=\"1.0\" encoding=\"UTF-16\"?>").data(using: .utf16) ?? Data()348        #expect(throws: ConversionError.self) { _ = try XMLTreeCodec.parse(utf16) }349        let parameter = "<?xml version=\"1.0\"?>\n<!DOCTYPE r [ <!ENTITY % p SYSTEM \"\(secret.absoluteString)\"> %p; ]>\n<r>x</r>"350        #expect(throws: ConversionError.self) { _ = try XMLTreeCodec.parse(Data(parameter.utf8)) }351    }352353    @Test func xmlBillionLaughsFailsFast() async throws {354        var doc = "<?xml version=\"1.0\"?>\n<!DOCTYPE lolz [\n <!ENTITY lol \"lol\">\n"355        for i in 1...9 { doc += " <!ENTITY lol\(i) \"" + String(repeating: "&lol\(i == 1 ? "" : String(i - 1));", count: 10) + "\">\n" }356        doc += "]>\n<lolz>&lol9;</lolz>"357        let scratch = Scratch("laughs")358        let url = scratch.file("bomb.xml")359        try doc.write(to: url, atomically: true, encoding: .utf8)360        // The document is refused on its entity declarations, before any parser expands anything.361        await #expect(throws: XMLTreeCodec.entityError) { _ = try await runConversion(url, .xml, to: .json, in: scratch) }362        await #expect(throws: XMLTreeCodec.entityError) { _ = try await runConversion(url, .xml, to: .csv, in: scratch) }363        #expect(throws: XMLTreeCodec.entityError) { _ = try XMLTreeCodec.parse(Data(doc.utf8)) }364    }365366    @Test func xmlReportsWhatItCannotKeep() async throws {367        let scratch = Scratch("tree")368        let url = scratch.file("m.xml")369        try "<?xml version=\"1.0\"?><?pi data?><r><!-- c --><p>one <b>bold</b> two</p><a>1</a><z>9</z><a>2</a></r>".write(to: url, atomically: true, encoding: .utf8)370        let c = try await runConversion(url, .xml, to: .json, in: scratch)371        #expect(c.warnings.codes.isSuperset(of: ["xml-comments-dropped", "xml-processing-instructions-dropped", "xml-mixed-content", "xml-sibling-order"]))372        let pre = DataEngine().preflight(c.request, inspection: nil)373        #expect(Set(pre.map(\.code)).isSuperset(of: ["xml-comments-dropped", "xml-mixed-content"]))374    }375376    @Test func jsonToXMLAttributesTextAndInvalidNames() async throws {377        let scratch = Scratch("tree")378        let url = scratch.file("a.json")379        try ##"{"book":{"@id":"b1","#text":"T","1st key":"v","list":[1,2],"empty":"","nested":[[1,2],[3]]}}"##.write(to: url, atomically: true, encoding: .utf8)380        let c = try await runConversion(url, .json, to: .xml, in: scratch)381        let text = try String(contentsOf: c.file, encoding: .utf8)382        #expect(text.contains("<book id=\"b1\">T"))383        #expect(text.contains("<_1st_key>v</_1st_key>"))384        #expect(text.contains("<list>1</list>\n  <list>2</list>"))385        #expect(c.warnings.codes.isSuperset(of: ["xml-names-adapted", "xml-empty-strings"]))386    }387388    // Regression (2026-11-21): a top-level list of two or more entries was written as several root elements, which is389    // not XML; the engine's own validation then refused its output. Every shape below must give one well-formed390    // document that validates, from each tree format that can hold the shape at its top level.391    static let topLevelShapes: [(name: String, json: String, xml: String, back: String)] = [392        ("array of objects", #"[{"id":1},{"id":2}]"#, "<root>\n  <item>\n    <id>1</id>\n  </item>\n  <item>\n    <id>2</id>\n  </item>\n</root>\n", #"[{"id":"1"},{"id":"2"}]"#),393        ("array of scalars", #"[1,"two",true]"#, "<root>\n  <item>1</item>\n  <item>two</item>\n  <item>true</item>\n</root>\n", #"["1","two","true"]"#),394        ("single-entry array", #"[{"id":1}]"#, "<root>\n  <item>\n    <id>1</id>\n  </item>\n</root>\n", #"[{"id":"1"}]"#),395        ("nested arrays", #"[[1,2],[3]]"#, "<root>\n  <item>\n    <item>1</item>\n    <item>2</item>\n  </item>\n  <item>\n    <item>3</item>\n  </item>\n</root>\n", #"[{"item":["1","2"]},{"item":"3"}]"#),396        ("array under a key holding arrays", #"{"m":[[1,2],[3]],"k":"v"}"#, "<root>\n  <m>\n    <item>1</item>\n    <item>2</item>\n  </m>\n  <m>\n    <item>3</item>\n  </m>\n  <k>v</k>\n</root>\n", #"{"root":{"m":[{"item":["1","2"]},{"item":"3"}],"k":"v"}}"#),397        ("empty array", "[]", "<root/>\n", #"{"root":null}"#),398        ("top-level scalar", "42", "<root>42</root>\n", #"{"root":"42"}"#),399        ("top-level text", #""a<b""#, "<root>a&lt;b</root>\n", #"{"root":"a<b"}"#),400        ("top-level null", "null", "<root/>\n", #"{"root":null}"#),401    ]402403    @Test("top-level lists and scalars become one well-formed XML document", arguments: [FormatID.json, .yaml, .plist], Array(0..<TreeTests.topLevelShapes.count))404    func topLevelValuesToXML(_ source: FormatID, _ index: Int) async throws {405        let shape = TreeTests.topLevelShapes[index]406        let scratch = Scratch("toplevel")407        let tree = try JSONParser.parse(Data(shape.json.utf8)).value408        let url = scratch.file("doc.\(FormatCatalog.format(source)?.preferredExtension ?? source.rawValue)")409        var tally = LossTally()410        var expectedXML = shape.xml, expectedBack = shape.back411        switch source {412        case .json: try Data(shape.json.utf8).write(to: url)413        case .yaml: try YAMLCodec.serialize(tree, tally: &tally).write(to: url)414        default:415            // A property list has no null: the shapes that need one do not exist in that format.416            if shape.json.contains("null") { return }417            try PlistCodec.serialize(tree, binary: false, nulls: .omit, tally: &tally).write(to: url)418            // Property-list dictionaries have no key order: keys are read sorted.419            if shape.name == "array under a key holding arrays" {420                expectedXML = "<root>\n  <k>v</k>\n  <m>\n    <item>1</item>\n    <item>2</item>\n  </m>\n  <m>\n    <item>3</item>\n  </m>\n</root>\n"421                expectedBack = #"{"root":{"k":"v","m":[{"item":["1","2"]},{"item":"3"}]}}"#422            }423        }424        let c = try await runConversion(url, source, to: .xml, in: scratch)425        let text = try String(contentsOf: c.file, encoding: .utf8)426        #expect(text == "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + expectedXML, "\(shape.name) from \(source.rawValue)")427        // An independent parser accepts the document (one root element).428        let doc = try XMLDocument(data: Data(contentsOf: c.file))429        #expect(doc.rootElement()?.name == "root")430        let checks = try await DataEngine().validate(c.output, request: c.request, source: nil)431        #expect(!checks.isEmpty)432        for check in checks { #expect(check.passed, "\(shape.name): validation “\(check.name)” failed: \(check.detail)") }433        // XML → JSON under the documented convention.434        let back = try await runConversion(c.file, .xml, to: .json, in: scratch, name: "back")435        let reread = try JSONParser.parse(Data(contentsOf: back.file)).value436        #expect(reread == (try JSONParser.parse(Data(expectedBack.utf8)).value), "\(shape.name) from \(source.rawValue)")437        if shape.json.hasPrefix("[") { #expect(c.warnings.codes.contains("xml-root-wrapped")) }438        if shape.name == "empty array" { #expect(c.warnings.codes.contains("xml-empty-list")) }439    }440441    @Test func topLevelListRoundTripsThroughXML() async throws {442        let scratch = Scratch("toplevel")443        let url = scratch.file("rows.json")444        try #"[{"id":"1","name":"Ada","tags":["a","b"]},{"id":"2","name":"Bob","tags":["c","d"]}]"#.write(to: url, atomically: true, encoding: .utf8)445        let xml = try await runConversion(url, .json, to: .xml, in: scratch)446        let back = try await runConversion(xml.file, .xml, to: .json, in: scratch, name: "back")447        #expect(try JSONParser.parse(Data(contentsOf: back.file)).value == (try JSONParser.parse(Data(contentsOf: url)).value))448        // Custom element names: one root still, and the list is found under <root>.<item> names when read back.449        let custom = try await runConversion(url, .json, to: .xml, in: scratch, options: ["xml.root.name": .string("people"), "xml.item.name": .string("person")], name: "custom")450        let doc = try XMLDocument(data: Data(contentsOf: custom.file))451        #expect(doc.rootElement()?.name == "people")452        #expect(doc.rootElement()?.elements(forName: "person").count == 2)453        guard case .object(let o) = try XMLTreeCodec.parse(Data(contentsOf: custom.file)).value, case .object(let people)? = o["people"], case .array(let list)? = people["person"] else { Issue.record("custom names: list not found under people.person"); return }454        #expect(list.count == 2)455        // An empty list under a key is reported, not silently dropped.456        let e = scratch.file("e.json")457        try #"{"a":{"list":[],"x":1}}"#.write(to: e, atomically: true, encoding: .utf8)458        let ec = try await runConversion(e, .json, to: .xml, in: scratch, name: "e")459        #expect(ec.warnings.codes.contains("xml-empty-list"))460        #expect(try String(contentsOf: ec.file, encoding: .utf8).contains("<list/>"))461    }462463    // MARK: Property lists464465    @Test func plistBinaryAndXMLRoundTrip() async throws {466        let scratch = Scratch("tree")467        let src = try TreeFixtures.write(.plist, in: scratch)468        let xml = try await runConversion(src, .plist, to: .plist, in: scratch, options: ["plist.format": .string("xml")])469        #expect(try Data(contentsOf: xml.file).starts(with: Array("<?xml".utf8)))470        let bin = try await runConversion(xml.file, .plist, to: .plist, in: scratch, options: ["plist.format": .string("binary")], name: "again")471        #expect(try Data(contentsOf: bin.file).starts(with: Array("bplist".utf8)))472        let a = try PropertyListSerialization.propertyList(from: Data(contentsOf: src), options: [], format: nil) as? NSDictionary473        let b = try PropertyListSerialization.propertyList(from: Data(contentsOf: bin.file), options: [], format: nil) as? NSDictionary474        #expect(a != nil && a == b)475        let parsed = try PlistCodec.parse(Data(contentsOf: src))476        #expect(parsed.isBinary)477        guard case .object(let o) = parsed.value else { Issue.record("not a dictionary"); return }478        #expect(o["enabled"] == .bool(true))479        #expect(o["count"] == .integer(42))480        #expect(o["ratio"] == .double(0.5))481        #expect(o["blob"] == .binary(Data([0, 1, 2, 255])))482        #expect(o["moment"] == .date(TreeDate(kind: .offsetDateTime, text: "2024-02-29T13:45:10Z")))483    }484485    @Test func plistReportsNullsAndWideNumbers() async throws {486        let scratch = Scratch("tree")487        let url = scratch.file("p.json")488        try #"{"n":null,"big":123456789012345678901234567890,"exact":0.1234567890123456789012,"ok":1}"#.write(to: url, atomically: true, encoding: .utf8)489        let c = try await runConversion(url, .json, to: .plist, in: scratch)490        #expect(c.warnings.codes.isSuperset(of: ["null-omitted", "integer-as-text", "decimal-as-text"]))491        let d = try PropertyListSerialization.propertyList(from: Data(contentsOf: c.file), options: [], format: nil) as? [String: Any]492        #expect(d?["big"] as? String == "123456789012345678901234567890")493        #expect(d?["n"] == nil)494        await #expect(throws: ConversionError.self) { _ = try await runConversion(url, .json, to: .plist, in: scratch, options: ["nulls.policy": .string("error")]) }495    }496497    @Test(arguments: [FormatID.json, .yaml, .toml, .xml, .plist])498    func malformedDocumentsAreRejected(_ format: FormatID) async throws {499        let scratch = Scratch("bad")500        let url = scratch.file("bad.\(format.rawValue)")501        let garbage: String502        switch format {503        case .json: garbage = "{\"a\": [1, 2"504        case .yaml: garbage = "a: [1, 2\nb: {"505        case .toml: garbage = "a = \n[b"506        case .xml: garbage = "<a><b></a>"507        default: garbage = "bplist00\u{1}garbage that is not a property list"508        }509        try garbage.write(to: url, atomically: true, encoding: .utf8)510        for target in [FormatID.json, .csv] where !(format == .json && target == .json && false) {511            do {512                _ = try await runConversion(url, format, to: target, in: scratch)513                Issue.record("\(format) → \(target) accepted a malformed document")514            } catch let e as ConversionError {515                if case .malformedInput = e {} else { Issue.record("unexpected error \(e)") }516            }517        }518    }519}520
Files main
Menu

Theme