Tests/OrgCoreTests/KeymapTests.swift
141 lines · 6046 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) == ["SPC b d", "SPC b k"])
99 #expect(doom.keyLabels(for: "app.buffer.next", modes: ["normal"]) == ["] b", "SPC b ]"])
100 #expect(emacs.keyLabels(for: "app.buffer.switch") == ["C-x b", "C-x C-b"])
101 }
102
103 @Test func dispatchesPrefixes() {
104 var dispatcher = KeyDispatcher(keymap: KeymapPreset.emacs.keymap)
105 let c = KeyChord("c", .control), t = KeyChord("t", .control), z = KeyChord("z", .control)
106 #expect(dispatcher.feed(c) == .pending([c]))
107 guard case .complete(let keys, let candidates) = dispatcher.feed(t) else {
108 Issue.record("expected a binding")
109 return
110 }
111 #expect(keys == [c, t] && candidates.first?.command == "org.todo.cycle")
112 #expect(dispatcher.feed(c) == .pending([c]))
113 #expect(dispatcher.feed(z) == .undefined([c, z]))
114 #expect(dispatcher.feed(c) == .pending([c]))
115 #expect(dispatcher.feed(KeyDispatcher.quit) == .cancelled)
116 #expect(dispatcher.feed(KeyChord("a")) == .unbound)
117 }
118
119 @Test func continuationsListNextKeys() {
120 let keymap = KeymapPreset.emacs.keymap
121 let next = keymap.continuations(of: [KeyChord("x", .control)])
122 #expect(next.contains { $0.key == KeyChord("s", .control) && $0.command == "app.save" })
123 #expect(keymap.continuations(of: []).contains { $0.key == KeyChord("c", .control) && $0.command == nil })
124 }
125
126 @Test func contexts() {
127 let text = "* a\n| x |\n- item\n"
128 let tree = OrgParser.parse(text)
129 func holds(_ name: String, _ caret: Int, _ selection: Range<Int>? = nil) -> Bool {
130 KeyContext.holds(name, in: EditContext(revision: 0, text: text, tree: tree, selection: [selection ?? caret..<caret]))
131 }
132 #expect(holds("heading", 1) && !holds("heading", 5))
133 #expect(holds("table", 5) && !holds("table", 12))
134 #expect(holds("item", 12))
135 #expect(holds("region", 0, 0..<2) && !holds("region", 0))
136 }
137
138 @Test func presetsParse() {
139 for preset in KeymapPreset.allCases { #expect(!preset.keymap.bindings.isEmpty) }
140 }
141}