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