SPB Git

Repository Public License

GlyphTests.swift 4.1 KB 89 lines swift

1import SwiftUI2import Testing3@testable import LunarDesign45@Suite struct LunarGlyphTests {6    @Test func thereAreTwentyModuleGlyphs() {7        #expect(LunarGlyph.allCases.map(\.rawValue) == [8            "command", "orbit", "split", "agent", "memory", "ghost", "time", "canvas", "blink", "lens", "shield",9            "archive", "morph", "reader", "dev", "flow", "capsule", "bridge", "media", "focus",10        ])11    }1213    @Test(arguments: LunarGlyph.allCases)14    func glyphRendersANonEmptyPathInsideItsBox(glyph: LunarGlyph) {15        for side: CGFloat in [16, 24, 64] {16            let rect = CGRect(x: 0, y: 0, width: side, height: side)17            let path = glyph.outlinePath(in: rect)18            #expect(!path.isEmpty, "\(glyph.rawValue) at \(side)")19            let bounds = path.boundingRect20            #expect(bounds.width > side * 0.55 && bounds.height > side * 0.5, "\(glyph.rawValue) fills its box")21            #expect(rect.insetBy(dx: -0.01, dy: -0.01).contains(bounds), "\(glyph.rawValue) stays in its box: \(bounds)")22            #expect(!LunarGlyphShape(glyph: glyph).path(in: rect).isEmpty)23        }24    }2526    /// Distinctness: no two glyphs share the same geometry.27    @Test func glyphsAreDistinct() {28        let rect = CGRect(x: 0, y: 0, width: 24, height: 24)29        let signatures = LunarGlyph.allCases.map { glyph -> String in30            let stroke = glyph.strokePath(in: rect), fill = glyph.fillPath(in: rect)31            return "\(stroke.description)|\(fill.description)"32        }33        #expect(Set(signatures).count == LunarGlyph.allCases.count)34    }3536    /// Readability at 16 pt: each glyph covers a sensible share of a 16 × 16 pixel grid (neither a speck nor a blob)37    /// and any two glyphs differ on a meaningful number of pixels.38    @Test func glyphsAreReadableAndDifferentAtSixteenPoints() {39        func mask(_ glyph: LunarGlyph) -> [Bool] {40            let path = glyph.outlinePath(in: CGRect(x: 0, y: 0, width: 16, height: 16))41            var cells: [Bool] = []42            for y in 0..<16 {43                for x in 0..<16 { cells.append(path.contains(CGPoint(x: Double(x) + 0.5, y: Double(y) + 0.5))) }44            }45            return cells46        }47        let masks = LunarGlyph.allCases.map(mask)48        for (glyph, cells) in zip(LunarGlyph.allCases, masks) {49            let coverage = Double(cells.filter { $0 }.count) / 25650            #expect(coverage > 0.10 && coverage < 0.55, "\(glyph.rawValue) coverage \(coverage)")51        }52        for a in masks.indices {53            for b in masks.indices where b > a {54                let difference = zip(masks[a], masks[b]).filter { $0 != $1 }.count55                #expect(difference >= 24, "\(LunarGlyph.allCases[a].rawValue) vs \(LunarGlyph.allCases[b].rawValue): \(difference)")56            }57        }58    }5960    @Test func markRendersAtEverySize() {61        for side: CGFloat in [16, 32, 128, 1024] {62            let rect = CGRect(x: 0, y: 0, width: side, height: side)63            let path = LunarMarkShape().path(in: rect)64            #expect(!path.isEmpty)65            #expect(rect.insetBy(dx: -0.01, dy: -0.01).contains(path.boundingRect))66        }67        // The ring is wider than the core and the needle stays inside the core.68        #expect(LunarMarkGeometry.ringSemiMajor > LunarMarkGeometry.coreRadius * 2)69        #expect(LunarMarkGeometry.needleLength < LunarMarkGeometry.coreRadius)70        #expect(!LunarMarkGeometry.needleHalf(forward: true).isEmpty)71    }7273    @Test func shapesProduceInsetPaths() {74        let rect = CGRect(x: 0, y: 0, width: 100, height: 40)75        for shape in [LunarShape.capsule, .circle, .xs, .sm, .md, .lg, .xl] {76            #expect(!shape.path(in: rect).isEmpty)77            let inset = shape.inset(by: 2).path(in: rect).boundingRect78            #expect(inset.width <= 96.01 && inset.height <= 36.01)79        }80        #expect(LunarShape.md.inset(by: 30).path(in: rect).isEmpty)81    }8283    @Test func keyCapsHaveSpokenNames() {84        #expect(LunarKeyCap.spokenName("⌘") == "Command")85        #expect(LunarKeyCap.spokenName("⇧") == "Shift")86        #expect(LunarKeyCap.spokenName("K") == "K")87    }88}89
Files main
Menu

Theme