Tests/OrgCoreTests/KeymapTests.swift

87e24faa398b297d22cbc8cb585e8bb0a666c4ec
orgstar/Tests/OrgCoreTests/KeymapTests.swift history · blame · raw

138 lines · 5755 bytes

  1import Testing
  2@testable import OrgCore
  3
  4struct KeyNotationTests {
  5    @Test func parsesAndFormats() {
  6        #expect(KeySequence.parse("C-c C-x C-i").map(KeySequence.format) == "C-c C-x C-i")
  7        #expect(KeyChord.parse("C-M-x") == KeyChord("x", [.control, .meta]))
  8        #expect(KeyChord.parse("S-a") == KeyChord("A"))
  9        #expect(KeyChord.parse("S-TAB") == KeyChord("TAB", .shift))
 10        #expect(KeyChord.parse("<backtab>") == KeyChord("TAB", .shift))
 11        #expect(KeyChord.parse("C--") == KeyChord("-", .control))
 12        #expect(KeyChord.parse("M-<") == KeyChord("<", .meta))
 13        #expect(KeyChord.parse("s-<left>")?.description == "s-<left>")
 14        #expect(KeyChord.parse("<tab>") == KeyChord("TAB"))
 15        #expect(KeyChord.parse("X-a") == nil)
 16        #expect(KeyChord.parse("abc") == nil)
 17        #expect(KeySequence.parse("") == nil)
 18    }
 19}
 20
 21struct TOMLTests {
 22    @Test func parsesTables() throws {
 23        let tables = try TOML.parse("""
 24            top = 1 # comment
 25            [[bind]]
 26            keys = "C-c \\"x\\""
 27            when = 'heading'
 28            on = true
 29
 30            [options]
 31            name = "a\\u00e9"
 32            """)
 33        #expect(tables.count == 3)
 34        #expect(tables[0].values["top"] == .integer(1))
 35        #expect(tables[1].name == "bind" && tables[1].isArrayElement && tables[1].line == 2)
 36        #expect(tables[1].values["keys"] == .string("C-c \"x\""))
 37        #expect(tables[1].values["when"] == .string("heading"))
 38        #expect(tables[1].values["on"] == .bool(true))
 39        #expect(tables[2].values["name"] == .string("aé"))
 40    }
 41
 42    @Test func reportsErrorsWithLines() {
 43        #expect(throws: TOML.ParseError(line: 2, message: "unclosed string")) { try TOML.parse("[[bind]]\nkeys = \"C-c") }
 44        #expect(throws: TOML.ParseError(line: 1, message: "expected key = value")) { try TOML.parse("keys") }
 45        #expect(throws: TOML.ParseError(line: 2, message: "duplicate key a")) { try TOML.parse("a = 1\na = 2") }
 46    }
 47}
 48
 49struct KeymapTests {
 50    func map(_ toml: String) throws -> (Keymap, [String]) {
 51        var problems: [String] = []
 52        let keymap = try Keymap(toml: toml, problems: &problems)
 53        return (keymap, problems)
 54    }
 55
 56    @Test func skipsBadEntries() throws {
 57        let (keymap, problems) = try map("""
 58            [[bind]]
 59            keys = "C-c C-t"
 60            command = "org.todo.cycle"
 61            [[bind]]
 62            keys = "Q-x"
 63            command = "a"
 64            [[bind]]
 65            keys = "TAB"
 66            command = "a"
 67            when = "nowhere"
 68            [other]
 69            """)
 70        #expect(keymap.bindings.count == 1)
 71        #expect(problems.count == 3)
 72    }
 73
 74    @Test func laterLayersWinAndCanUnbind() throws {
 75        let preset = KeymapPreset.emacs.keymap
 76        let (user, _) = try map("""
 77            [[bind]]
 78            keys = "C-c C-t"
 79            command = "org.priority.up"
 80            [[bind]]
 81            keys = "C-k"
 82            command = "none"
 83            """)
 84        let keymap = Keymap.layered([preset, user])
 85        #expect(keymap.candidates(for: KeySequence.parse("C-c C-t")!).first?.command == "org.priority.up")
 86        #expect(keymap.candidates(for: KeySequence.parse("C-k")!).isEmpty)
 87        #expect(keymap.keys(for: "org.todo.cycle").map(KeySequence.format) == "S-<right>")
 88    }
 89
 90    @Test func keyLabelsForMenus() {
 91        let emacs = KeymapPreset.emacs.keymap
 92        #expect(emacs.keyLabels(for: "org.todo.cycle") == ["C-c C-t", "S-<right>"])
 93        #expect(emacs.keyLabels(for: "org.schedule") == ["C-c C-s"])
 94        let doom = KeymapPreset.doom.keymap
 95        let labels = doom.keyLabels(for: "org.todo.cycle", modes: ["normal", "insert"])
 96        #expect(labels == ["C-c C-t", "SPC m t"])
 97        #expect(doom.keyLabels(for: "app.quit", modes: ["normal", "insert"]) == ["SPC q q"])
 98    }
 99
100    @Test func dispatchesPrefixes() {
101        var dispatcher = KeyDispatcher(keymap: KeymapPreset.emacs.keymap)
102        let c = KeyChord("c", .control), t = KeyChord("t", .control), z = KeyChord("z", .control)
103        #expect(dispatcher.feed(c) == .pending([c]))
104        guard case .complete(let keys, let candidates) = dispatcher.feed(t) else {
105            Issue.record("expected a binding")
106            return
107        }
108        #expect(keys == [c, t] && candidates.first?.command == "org.todo.cycle")
109        #expect(dispatcher.feed(c) == .pending([c]))
110        #expect(dispatcher.feed(z) == .undefined([c, z]))
111        #expect(dispatcher.feed(c) == .pending([c]))
112        #expect(dispatcher.feed(KeyDispatcher.quit) == .cancelled)
113        #expect(dispatcher.feed(KeyChord("a")) == .unbound)
114    }
115
116    @Test func continuationsListNextKeys() {
117        let keymap = KeymapPreset.emacs.keymap
118        let next = keymap.continuations(of: [KeyChord("x", .control)])
119        #expect(next.contains { $0.key == KeyChord("s", .control) && $0.command == "app.save" })
120        #expect(keymap.continuations(of: []).contains { $0.key == KeyChord("c", .control) && $0.command == nil })
121    }
122
123    @Test func contexts() {
124        let text = "* a\n| x |\n- item\n"
125        let tree = OrgParser.parse(text)
126        func holds(_ name: String, _ caret: Int, _ selection: Range<Int>? = nil) -> Bool {
127            KeyContext.holds(name, in: EditContext(revision: 0, text: text, tree: tree, selection: [selection ?? caret..<caret]))
128        }
129        #expect(holds("heading", 1) && !holds("heading", 5))
130        #expect(holds("table", 5) && !holds("table", 12))
131        #expect(holds("item", 12))
132        #expect(holds("region", 0, 0..<2) && !holds("region", 0))
133    }
134
135    @Test func presetsParse() {
136        for preset in KeymapPreset.allCases { #expect(!preset.keymap.bindings.isEmpty) }
137    }
138}