SPB Git

Repository Public License

JSONValueTests.swift 2.1 KB 53 lines swift

1import Foundation2import Testing3@testable import PrismCore45@Suite struct JSONValueTests {6    @Test func canonicalSortsKeysAndKeepsIntegers() throws {7        let v = try JSONValue.parse(#"{"b": 1, "a": [true, null, 2.5, "x/y"], "c": {"z": 1.0, "y": "é\n"}}"#)8        #expect(v.canonicalString() == #"{"a":[true,null,2.5,"x/y"],"b":1,"c":{"y":"é\n","z":1}}"#)9    }1011    @Test func roundTripIsStable() throws {12        let text = #"{"name":"Dépenses \"A\"","n":-12,"d":0.1,"big":123456789012,"list":[{"k":[]},{}],"ctl":"\u0001"}"#13        let once = try JSONValue.parse(text).canonicalString()14        let twice = try JSONValue.parse(once).canonicalString()15        #expect(once == twice)16        #expect(try JSONValue.parse(once) == JSONValue.parse(text))17    }1819    @Test func hashIgnoresKeyOrderAndWhitespace() throws {20        let a = try JSONValue.parse(#"{"x":1,"y":[1,2]}"#)21        let b = try JSONValue.parse("{ \"y\": [1, 2],\n \"x\": 1 }")22        #expect(ContentHash.sha256(a) == ContentHash.sha256(b))23        #expect(ContentHash.sha256(a).count == 64)24    }2526    @Test func boolsAreNotNumbers() throws {27        let v = try JSONValue.parse(#"[true, 1, 1.5]"#)28        #expect(v[0] == .bool(true))29        #expect(v[1] == .int(1) && v[1] == .double(1.0) && v[1] != .bool(true))30        #expect(Set<JSONValue>([.int(2), .double(2.0)]).count == 1)31        #expect(v[2] == .double(1.5))32    }3334    @Test func invalidJSONThrows() {35        #expect(throws: JSONError.self) { try JSONValue.parse("{nope") }36    }3738    @Test func codableRoundTrip() throws {39        let v: JSONValue = ["a": 1, "b": [true, nil, "s"], "c": 2.25]40        let data = try JSONEncoder().encode(v)41        #expect(try JSONDecoder().decode(JSONValue.self, from: data) == v)42    }4344    @Test func slugAndIDs() {45        #expect(PrismID.slug("Monthly Expenses (2026)!") == "monthly_expenses_2026")46        #expect(PrismID.slug("123 go") == "n123_go")47        #expect(PrismID.slug("   ") == "item")48        #expect(PrismID.isValid(PrismID.make("obj")))49        #expect(!PrismID.isValid("1abc"))50        #expect(!PrismID.isValid("a b"))51    }52}53
Files main
Menu

Theme