Tests/OrgCoreTests/KeymapTests.swift
162 lines · 7151 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 #expect(doom.keyLabels(for: "app.buffer.kill", modes: ["normal"], limit: 3) == ["C-x k", "SPC b d", "SPC b k"])
99 #expect(doom.keyLabels(for: "app.buffer.next", modes: ["normal"]) == ["] b", "C-x <right>"])
100 #expect(emacs.keyLabels(for: "app.buffer.switch") == ["C-x C-b", "C-x b"])
101 #expect(!emacs.keyLabels(for: "app.quick-open").contains("C-x b"))
102 }
103
104 /// Two bindings of the same keys in the same state, both always applying: the earlier one
105 /// can never run.
106 @Test func presetsBindEachKeyOnce() {
107 for preset in KeymapPreset.allCases {
108 var seen: [String: String] = [:]
109 for binding in preset.keymap.bindings where binding.when == nil && !binding.unbinds {
110 let key = "\(binding.mode ?? "-") \(KeySequence.format(binding.keys))"
111 #expect(seen[key] == nil, "\(preset): \(key) is \(seen[key] ?? "") and \(binding.command)")
112 seen[key] = binding.command
113 }
114 }
115 }
116
117 @Test func shadowedBindingsShowNoKeys() throws {
118 var problems: [String] = []
119 let map = Keymap.layered([KeymapPreset.emacs.keymap, try Keymap(toml: "[[bind]]\nkeys = \"C-c C-t\"\ncommand = \"app.save\"\n", problems: &problems)])
120 #expect(!map.keyLabels(for: "org.todo.cycle").contains("C-c C-t"))
121 #expect(map.keyLabels(for: "app.save").contains("C-c C-t"))
122 }
123
124 @Test func dispatchesPrefixes() {
125 var dispatcher = KeyDispatcher(keymap: KeymapPreset.emacs.keymap)
126 let c = KeyChord("c", .control), t = KeyChord("t", .control), z = KeyChord("z", .control)
127 #expect(dispatcher.feed(c) == .pending([c]))
128 guard case .complete(let keys, let candidates) = dispatcher.feed(t) else {
129 Issue.record("expected a binding")
130 return
131 }
132 #expect(keys == [c, t] && candidates.first?.command == "org.todo.cycle")
133 #expect(dispatcher.feed(c) == .pending([c]))
134 #expect(dispatcher.feed(z) == .undefined([c, z]))
135 #expect(dispatcher.feed(c) == .pending([c]))
136 #expect(dispatcher.feed(KeyDispatcher.quit) == .cancelled)
137 #expect(dispatcher.feed(KeyChord("a")) == .unbound)
138 }
139
140 @Test func continuationsListNextKeys() {
141 let keymap = KeymapPreset.emacs.keymap
142 let next = keymap.continuations(of: [KeyChord("x", .control)])
143 #expect(next.contains { $0.key == KeyChord("s", .control) && $0.command == "app.save" })
144 #expect(keymap.continuations(of: []).contains { $0.key == KeyChord("c", .control) && $0.command == nil })
145 }
146
147 @Test func contexts() {
148 let text = "* a\n| x |\n- item\n"
149 let tree = OrgParser.parse(text)
150 func holds(_ name: String, _ caret: Int, _ selection: Range<Int>? = nil) -> Bool {
151 KeyContext.holds(name, in: EditContext(revision: 0, text: text, tree: tree, selection: [selection ?? caret..<caret]))
152 }
153 #expect(holds("heading", 1) && !holds("heading", 5))
154 #expect(holds("table", 5) && !holds("table", 12))
155 #expect(holds("item", 12))
156 #expect(holds("region", 0, 0..<2) && !holds("region", 0))
157 }
158
159 @Test func presetsParse() {
160 for preset in KeymapPreset.allCases { #expect(!preset.keymap.bindings.isEmpty) }
161 }
162}