SPB Git

Repository Public License

IconTests.swift 4.8 KB 104 lines swift

1import CoreGraphics2import Foundation3import PrismCore4import Testing5@testable import PrismIconKit67@Suite struct SVGPathTests {8    @Test func parsesEveryCommandAbsoluteAndRelative() throws {9        let path = try SVGPath.parse("M10 10 L20 10 h10 v10 H10 V30 C10 40 20 40 20 30 S30 20 30 30 Q40 40 50 30 T70 30 A10 10 0 0 1 90 30 a5,5 0 1,0 -10,0 Z m5 5 l1 1z")10        let box = path.boundingBoxOfPath11        #expect(box.minX >= 9 && box.maxX <= 91)12        #expect(!path.isEmpty)13    }1415    @Test func compactNumbersAndImplicitLineTo() throws {16        let path = try SVGPath.parse("M0,0 10,0 10,10-5.5.5z")17        #expect(path.boundingBoxOfPath.width == 15.5)18    }1920    @Test func arcsReachTheirEndPoint() throws {21        let path = try SVGPath.parse("M10 50 A40 40 0 0 1 90 50")22        #expect(abs(path.currentPoint.x - 90) < 0.001 && abs(path.currentPoint.y - 50) < 0.001)23        #expect(abs(path.boundingBoxOfPath.minY - 10) < 0.5)24    }2526    @Test(arguments: ["", "L10 10", "M10", "M0 0 X5 5", "M0 0 A1 1 0 2 0 5 5", "M1e99 0"])27    func rejectsMalformedInput(_ d: String) {28        #expect(throws: SVGPath.ParseError.self) { try SVGPath.parse(d) }29    }3031    @Test func rejectsOversizedInput() {32        #expect(throws: SVGPath.ParseError.self) { try SVGPath.parse("M0 0" + String(repeating: " L1 1", count: 900)) }33    }34}3536@Suite struct IconSpecTests {37    static let good: JSONValue = [38        "background": ["colors": ["#0B1E3F", "#1F6FEB", "#7EE0FF"], "style": "mesh", "pattern": "rings"],39        "layers": [40            ["shape": "ring", "size": 70, "fill": "#FFFFFF30"],41            ["path": "M30 70 L50 25 L70 70 Z", "fill": ["colors": ["#FFFFFF", "#BFE3FF"], "angle": 90], "shadow": true],42            ["symbol": "sparkles", "size": 22, "x": 72, "y": 28, "fill": "#FFD60A"],43            ["text": "P", "size": 20, "y": 80, "font": "rounded", "weight": "heavy"],44        ],45        "finish": "glass",46    ]4748    @Test func aGoodSpecHasNoProblemsAndDraws() throws {49        #expect(IconSpec.problems(Self.good).isEmpty)50        let image = try IconRenderer.image(Self.good, pixels: 256)51        #expect(image.width == 256 && image.height == 256)52        // The centre is painted and opaque; the corner outside the body is transparent.53        let rep = NSBitmapImageRepProxy(image)54        #expect(rep.alpha(x: 128, y: 128) > 0.99)55        #expect(rep.alpha(x: 2, y: 2) < 0.05)56    }5758    @Test func problemsNameWhatToFix() {59        let bad: JSONValue = [60            "background": ["colors": ["blue"], "style": "plaid"], "finish": "shiny", "extra": 1,61            "layers": [["symbol": "definitely.not.a.symbol"], ["path": "nope"], ["shape": "blob"], ["text": "TOO LONG"], ["symbol": "star", "text": "x"], ["shape": "circle", "fill": 3, "size": -1]],62        ]63        let problems = IconSpec.problems(bad).joined(separator: "\n")64        for needle in ["hex color", "background.style", "finish", "extra", "not an SF Symbol", "layers[1]", "`shape` must be", "1 to 4 characters", "exactly one", "`fill` must be", "greater than 0"] {65            #expect(problems.contains(needle), "missing: \(needle)")66        }67        #expect(throws: IconError.self) { try IconRenderer.image(bad) }68    }6970    @Test func theAutomaticIconIsValidAndDiffersPerApp() throws {71        let a = IconSpec.automatic(name: "Expense Tracker", seed: "a"), b = IconSpec.automatic(name: "Reading List", seed: "b")72        #expect(IconSpec.problems(a).isEmpty && IconSpec.problems(b).isEmpty)73        #expect(try IconRenderer.png(a, pixels: 64) != IconRenderer.png(b, pixels: 64))74    }7576    @Test func writesACompleteICNS() throws {77        let url = FileManager.default.temporaryDirectory.appendingPathComponent("prism-icon-\(UUID().uuidString).icns")78        defer { try? FileManager.default.removeItem(at: url) }79        try IconRenderer.writeICNS(Self.good, to: url)80        let data = try Data(contentsOf: url)81        #expect(data.prefix(4) == Data("icns".utf8) && data.count > 20_000)82    }8384    @Test func contrastFollowsWCAG() {85        let white = RGBA(hex: "#FFFFFF")!, black = RGBA(hex: "#000")!86        #expect(abs(white.contrast(with: black) - 21) < 0.01)87        #expect(RGBA(hex: "#12345678")?.a ?? 0 < 0.5)88        #expect(RGBA(hex: "123456") == nil)89    }90}9192/// Reads pixels of a CGImage for assertions.93struct NSBitmapImageRepProxy {94    let data: [UInt8], width: Int, height: Int95    init(_ image: CGImage) {96        width = image.width; height = image.height97        var bytes = [UInt8](repeating: 0, count: width * height * 4)98        let ctx = CGContext(data: &bytes, width: width, height: height, bitsPerComponent: 8, bytesPerRow: width * 4, space: CGColorSpace(name: CGColorSpace.sRGB)!, bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue)!99        ctx.draw(image, in: CGRect(x: 0, y: 0, width: width, height: height))100        data = bytes101    }102    func alpha(x: Int, y: Int) -> Double { Double(data[(y * width + x) * 4 + 3]) / 255 }103}104
Files main
Menu

Theme