Tests/OrgPresentationTests/PaletteTests.swift
126 lines · 7036 bytes
17 symbols in this file
PaletteTestsdefaultPalettesAreTheDefaultThemeaPaletteIsFoundOnlyInItsAppearanceaccentIsASlotTheDefaultLeavesToTheSystemeveryPaletteHasExactlyTheSlotKeystheCatalogIsInOrdercontrastluminancechanneltextReadsOnTheBackgroundbaseTakesEachAppearanceFromItsPaletteaPaletteOfTheOtherAppearanceIsAProblemaCopiedThemeReadsBackAsThePalettesiconIdsMapToAlternateNamesdockFilesFollowTheAppearanceaDarkOnlyChoiceLeavesTheLightSideToTheSystemeditorColorsFallBackExceptForSystemKeys
1import Foundation
2import OrgCore
3import OrgPresentation
4import Testing
5
6struct PaletteTests {
7 @Test func defaultPalettesAreTheDefaultTheme() throws {
8 let light = try #require(ThemeSpec.palette("default-light", .light))
9 let dark = try #require(ThemeSpec.palette("default-dark", .dark))
10 #expect(light.colors == ThemeSpec.default.light && dark.colors == ThemeSpec.default.dark)
11 #expect(light.name == "Default" && ThemeSpec.palettes(.light).first == light && ThemeSpec.palettes(.dark).first == dark)
12 #expect(ThemeSpec.defaultPaletteID(.light) == "default-light" && ThemeSpec.defaultPaletteID(.dark) == "default-dark")
13 }
14
15 @Test func aPaletteIsFoundOnlyInItsAppearance() {
16 #expect(ThemeSpec.palette("default-light", .dark) == nil)
17 #expect(ThemeSpec.palette("nope", .light) == nil)
18 }
19
20 @Test func accentIsASlotTheDefaultLeavesToTheSystem() {
21 #expect(ThemeSpec.slots.contains { $0.key == "accent" })
22 #expect(ThemeSpec.systemDefaults.contains("accent"))
23 #expect(ThemeSpec.default.light["accent"] == nil && ThemeSpec.default.dark["accent"] == nil)
24 }
25
26 @Test func everyPaletteHasExactlyTheSlotKeys() {
27 let all = Set(ThemeSpec.slots.map(\.key))
28 for palette in ThemeSpec.palettes(.light) + ThemeSpec.palettes(.dark) {
29 let expected = palette.id.hasPrefix("default-") ? all.subtracting(ThemeSpec.systemDefaults) : all
30 #expect(Set(palette.colors.keys) == expected, "\(palette.id): \(expected.symmetricDifference(palette.colors.keys))")
31 }
32 }
33 @Test func theCatalogIsInOrder() {
34 #expect(ThemeSpec.palettes(.light).map(\.id) == ["default-light", "solarized-light", "gruvbox-light", "modus-operandi", "catppuccin-latte", "tomorrow", "paper"])
35 #expect(ThemeSpec.palettes(.dark).map(\.id) == ["default-dark", "solarized-dark", "gruvbox-dark", "modus-vivendi", "catppuccin-mocha", "nord", "dracula", "tokyo-night", "doom-one"])
36 }
37
38 /// WCAG 2 contrast ratio.
39 static func contrast(_ a: RGBA, _ b: RGBA) -> Double {
40 func luminance(_ c: RGBA) -> Double {
41 func channel(_ v: Double) -> Double { v <= 0.03928 ? v / 12.92 : pow((v + 0.055) / 1.055, 2.4) }
42 return 0.2126 * channel(c.red) + 0.7152 * channel(c.green) + 0.0722 * channel(c.blue)
43 }
44 let (x, y) = (luminance(a), luminance(b))
45 return (max(x, y) + 0.05) / (min(x, y) + 0.05)
46 }
47
48 @Test func textReadsOnTheBackground() throws {
49 let body = ["foreground", "table"]
50 let marks = ["heading-1", "heading-2", "heading-3", "heading-4", "todo", "todo-next", "todo-waiting", "done", "link", "timestamp", "code",
51 "syntax-keyword", "syntax-string", "syntax-function", "syntax-type", "syntax-number", "syntax-property", "syntax-label"]
52 for palette in ThemeSpec.palettes(.light) + ThemeSpec.palettes(.dark) {
53 let background = try #require(palette.colors["background"])
54 for key in body {
55 let ratio = Self.contrast(try #require(palette.colors[key]), background)
56 #expect(ratio >= 4.5, "\(palette.id) \(key): \(ratio)")
57 }
58 for key in marks {
59 let ratio = Self.contrast(try #require(palette.colors[key]), background)
60 #expect(ratio >= 3, "\(palette.id) \(key): \(ratio)")
61 }
62 }
63 }
64
65 @Test func baseTakesEachAppearanceFromItsPalette() throws {
66 let (spec, problems) = ThemeSpec.base(light: "paper", dark: "nord")
67 #expect(problems.isEmpty)
68 #expect(spec.light == ThemeSpec.palette("paper", .light)?.colors && spec.dark == ThemeSpec.palette("nord", .dark)?.colors)
69 #expect(ThemeSpec.base(light: nil, dark: nil).0 == ThemeSpec.default)
70 #expect(ThemeSpec.base(light: "", dark: "").0 == ThemeSpec.default)
71 }
72
73 @Test func aPaletteOfTheOtherAppearanceIsAProblem() {
74 let (spec, problems) = ThemeSpec.base(light: "nord", dark: "unknown")
75 #expect(spec == ThemeSpec.default)
76 #expect(problems == ["config.toml: unknown light-theme nord", "config.toml: unknown dark-theme unknown"])
77 }
78
79 @Test func aCopiedThemeReadsBackAsThePalettes() throws {
80 let light = try #require(ThemeSpec.palette("paper", .light))
81 let dark = try #require(ThemeSpec.palette("nord", .dark))
82 let text = ThemeSpec.themeFile(light: light, dark: dark)
83 #expect(text.hasPrefix("# Paper and Nord, copied from orgstar's built-in themes."))
84 let (spec, problems) = ThemeSpec.reading(try TOML.parse(text))
85 #expect(problems.isEmpty, "\(problems)")
86 #expect(spec.light.filter { light.colors[$0.key] != nil } == light.colors && spec.dark.filter { dark.colors[$0.key] != nil } == dark.colors)
87 }
88
89 @Test func iconIdsMapToAlternateNames() {
90 #expect(AppIcons.ids.first == "default" && AppIcons.ids.count == 13)
91 #expect(AppIcons.alternateName("default") == nil)
92 #expect(AppIcons.alternateName("unicorn-night") == "AppIcon-UnicornNight")
93 #expect(AppIcons.id(alternateName: "AppIcon-UnicornNight") == "unicorn-night")
94 #expect(AppIcons.id(alternateName: nil) == "default" && AppIcons.id(alternateName: "AppIcon-Gone") == "default")
95 #expect(AppIcons.name("unicorn") == "Org Star" && AppIcons.name("unicorn-night") == "Night Star" && AppIcons.name("unicorn-pastel") == "Pastel Star")
96 }
97
98 @Test func dockFilesFollowTheAppearance() {
99 #expect(AppIcons.dockFile("default", dark: true) == nil)
100 #expect(AppIcons.dockFile("gone", dark: false) == nil)
101 #expect(AppIcons.dockFile("unicorn", dark: false) == "unicorn-light")
102 #expect(AppIcons.dockFile("unicorn", dark: true) == "unicorn-dark")
103 }
104
105 @Test func aDarkOnlyChoiceLeavesTheLightSideToTheSystem() {
106 let spec = ThemeSpec.base(light: nil, dark: "nord").0
107 for key in ThemeSpec.systemDefaults {
108 #expect(spec.color(key, dark: false) == nil, "\(key)")
109 #expect(spec.color(key, dark: true) != nil, "\(key)")
110 }
111 }
112
113 @Test func editorColorsFallBackExceptForSystemKeys() {
114 let spec = ThemeSpec.base(light: nil, dark: "nord").0
115 // A system key the light side leaves unset stays unset there, not the dark side's.
116 #expect(spec.editorColor("sidebar-background", dark: false) == nil)
117 #expect(spec.editorColor("sidebar-background", dark: true) == spec.dark["sidebar-background"])
118 // Other keys keep the default theme's color for a side the spec lacks.
119 var partial = ThemeSpec.default
120 partial.light["link"] = nil
121 #expect(partial.editorColor("link", dark: false) == ThemeSpec.default.light["link"])
122 // A key neither the spec's side nor the default has takes the spec's other side.
123 let oneSided = ThemeSpec(light: [:], dark: ["custom": RGBA(red: 1, green: 0, blue: 0)])
124 #expect(oneSided.editorColor("custom", dark: false) == RGBA(red: 1, green: 0, blue: 0))
125 }
126}