Tests/OrgAppTests/KeyBarLayoutTests.swift

d88176172f98786bd9a86c2f6ee051fd19fe426d
orgstar/Tests/OrgAppTests/KeyBarLayoutTests.swift

108 lines · 5351 bytes

  1import Foundation
  2import Testing
  3@testable import OrgApp
  4
  5@MainActor
  6struct KeyBarLayoutTests {
  7    func defaults() -> UserDefaults { TestDefaults("orgstar-keybar") }
  8
  9    @Test func defaultsWithoutTheTable() {
 10        let d = defaults()
 11        #expect(UserConfig.apply("fill-column = 70\n", to: d).isEmpty)
 12        #expect(KeyBarLayout.stored(.iphone, in: d) == nil)
 13        #expect(KeyBarLayout.stored(.ipad, in: d) == nil)
 14        #expect(KeyBarLayout.Width.iphone.defaultIDs.contains("plan"))
 15        #expect(!KeyBarLayout.Width.ipad.defaultIDs.contains("plan"))
 16        #expect(KeyBarLayout.Width.ipad.defaultIDs.suffix(4) == ["schedule", "deadline", "tags", "hide-keyboard"])
 17    }
 18
 19    @Test func readsIPhoneAndIPadLists() {
 20        let d = defaults()
 21        let problems = UserConfig.apply("""
 22            [key-bar]
 23            iphone = ["commands", "org.snippet:td", "macro.named.tidy", "commands"]
 24            ipad = [
 25              "fold",  # TAB
 26              "hide-keyboard",
 27            ]
 28            """, to: d)
 29        #expect(problems.isEmpty)
 30        #expect(KeyBarLayout.stored(.iphone, in: d) == ["commands", "org.snippet:td", "macro.named.tidy"])
 31        #expect(KeyBarLayout.stored(.ipad, in: d) == ["fold", "hide-keyboard"])
 32        // A list taken out of the file goes back to the default.
 33        UserConfig.apply("[key-bar]\nipad = []\n", to: d)
 34        #expect(KeyBarLayout.stored(.iphone, in: d) == nil)
 35        #expect(KeyBarLayout.stored(.ipad, in: d) == [])
 36    }
 37
 38    @Test func reportsBadValuesAndUnknownIDs() {
 39        let d = defaults()
 40        let problems = UserConfig.apply("""
 41            [key-bar]
 42            iphone = "fold"
 43            ipad = ["fold", 3]
 44            watch = []
 45            """, to: d)
 46        #expect(problems.count == 3)
 47        #expect(problems.contains { $0.contains("key-bar.iphone must be a list") })
 48        #expect(problems.contains { $0.contains("unknown setting key-bar.watch") })
 49        #expect(KeyBarLayout.stored(.iphone, in: d) == nil)
 50        let known: Set = ["fold", "commands"]
 51        #expect(KeyBarLayout.problems(["fold", "nonsense", "commands"], .ipad, known: known.contains)
 52            == ["config.toml: key-bar.ipad has no command nonsense"])
 53    }
 54
 55    @Test func writesAndRemovesLists() throws {
 56        let start = """
 57            fill-column = 80
 58
 59            [key-bar]
 60            iphone = [
 61              "fold",
 62              "todo",
 63            ]  # mine
 64            ipad = ["fold"]
 65            """
 66        let written = UserConfig.setting(start, section: KeyBarLayout.table, key: "iphone", to: .array([.string("act"), .string("org.snippet:td")]))
 67        #expect(written.contains("iphone = [\"act\", \"org.snippet:td\"]\nipad = [\"fold\"]"))
 68        let d = defaults()
 69        #expect(UserConfig.apply(written, to: d).isEmpty)
 70        #expect(KeyBarLayout.stored(.iphone, in: d) == ["act", "org.snippet:td"])
 71        let removed = UserConfig.removingSetting(start, section: KeyBarLayout.table, key: "iphone")
 72        #expect(removed == "fill-column = 80\n\n[key-bar]\nipad = [\"fold\"]")
 73        let added = UserConfig.setting("fill-column = 80\n", section: KeyBarLayout.table, key: "ipad", to: .array([.string("fold")]))
 74        #expect(added.hasSuffix("[key-bar]\nipad = [\"fold\"]\n"))
 75    }
 76
 77    @Test func fitsOrOverflows() {
 78        let ids = ["a", "b", "wide", "c", "d"]
 79        let slots: (String) -> Int = { $0 == "wide" ? 2 : 1 }
 80        #expect(KeyBarLayout.fit(ids, available: 6, slots: slots) == (ids, []))
 81        #expect(KeyBarLayout.fit(ids, available: 20, slots: slots) == (ids, []))
 82        // One slot goes to More.
 83        #expect(KeyBarLayout.fit(ids, available: 5, slots: slots) == (["a", "b", "wide"], ["c", "d"]))
 84        // A wide button that doesn't fit ends the row, though a narrow one after it would fit.
 85        #expect(KeyBarLayout.fit(ids, available: 4, slots: slots) == (["a", "b"], ["wide", "c", "d"]))
 86        #expect(KeyBarLayout.fit(ids, available: 2, slots: slots) == (["a"], ["b", "wide", "c", "d"]))
 87        #expect(KeyBarLayout.fit(ids, available: 0, slots: slots) == ([], ids))
 88        #expect(KeyBarLayout.fit([], available: 0, slots: slots) == ([], []))
 89    }
 90
 91    @Test func snippetFieldsTakePromoteAndDemote() {
 92        let next = KeyBarLayout.nextField, previous = KeyBarLayout.previousField
 93        #expect(KeyBarLayout.withSnippetFields(KeyBarLayout.Width.iphone.defaultIDs).prefix(4)
 94            == ["commands", "fold", previous, next])
 95        #expect(KeyBarLayout.withSnippetFields(KeyBarLayout.Width.iphone.defaultIDs).count == KeyBarLayout.Width.iphone.defaultIDs.count)
 96        #expect(KeyBarLayout.withSnippetFields(["fold", "todo"]) == [previous, next, "fold", "todo"])
 97        #expect(KeyBarLayout.withSnippetFields(["demote", "fold"]) == [previous, next, "fold"])
 98        #expect(KeyBarLayout.withSnippetFields([next, "promote", "demote"]) == [next, previous, "demote"])
 99        #expect(KeyBarLayout.withSnippetFields([previous, next]) == [previous, next])
100    }
101
102    @Test func faces() {
103        #expect(KeyBarLayout.face(title: "Insert Link…") == "Insert Link")
104        #expect(KeyBarLayout.slots(symbol: true, face: "Insert Link") == 1)
105        #expect(KeyBarLayout.slots(symbol: false, face: "TODO") == 1)
106        #expect(KeyBarLayout.slots(symbol: false, face: "Insert Link") == 2)
107    }
108}
Keyboard shortcuts

Global

⌘K
Command palette & jump
/
Focus search
?
Show this help
g d
Dashboard
g e
Explore repositories
g n
Notifications

Repository & editor

t
Find file in repository
y
Copy immutable SHA permalink
g c
Code tab
g i
Issues tab
g m
Merge requests tab
g b
Builds tab
⌘↵
Submit active form