Tests/OrgEditorAppKitTests/ModalTests.swift
cmc Add Vim macros, marks, jumps, block visual, surround, commentary and Org text objects
a49927e318 1 month ago160 lines · 5939 bytes
1import AppKit
2import OrgCore
3import Testing
4@testable import OrgEditorAppKit
5
6/// The Doom preset in the editor: keys through the keymap and the modal engine.
7@MainActor
8struct ModalTests {
9 func doom(_ text: String) -> Harness {
10 let h = Harness(text)
11 h.editor.keymap = KeymapPreset.doom.keymap
12 h.editor.modal = true
13 return h
14 }
15
16 /// Types `keys` as key events: letters, `ESC`, `RET`, `TAB`, `SPC`.
17 func type(_ h: Harness, _ keys: String) {
18 let codes: [String: UInt16] = ["ESC": 53, "RET": 36, "TAB": 48, "SPC": 49]
19 for token in keys.split(separator: " ") {
20 let key = String(token)
21 if key.hasPrefix("C-") {
22 let letter = String(key.dropFirst(2))
23 KeyTests().send(h, characters: letter, ignoring: letter, code: 0, flags: .control)
24 } else if let code = codes[key] {
25 let characters = key == "ESC" ? "\u{1b}" : key == "RET" ? "\r" : key == "TAB" ? "\t" : " "
26 KeyTests().send(h, characters: characters, ignoring: characters, code: code)
27 } else {
28 let shifted = key.first!.isUppercase
29 KeyTests().send(h, characters: key, ignoring: key, code: 0, flags: shifted ? .shift : [])
30 }
31 }
32 }
33
34 @Test func normalStateEditsAndInsertTypes() {
35 let h = doom("hello world\nsecond\n")
36 h.caret(at: 0)
37 type(h, "d w")
38 #expect(h.editor.document.text == "world\nsecond\n")
39 type(h, "c i w")
40 #expect(h.editor.vim.mode == .insert)
41 h.textView.insertText("planet", replacementRange: h.textView.selectedRange())
42 type(h, "ESC")
43 #expect(h.editor.document.text == "planet\nsecond\n")
44 #expect(h.textView.selectedRange().location == 5)
45 type(h, "j .")
46 #expect(h.editor.document.text == "planet\nplanet\n")
47 }
48
49 @Test func undo() {
50 let h = doom("hello world\n")
51 h.caret(at: 0)
52 type(h, "d w")
53 #expect(h.editor.document.text == "world\n")
54 type(h, "u")
55 #expect(h.editor.document.text == "hello world\n")
56 }
57
58 @Test func leaderAndOrgKeys() {
59 let h = doom("* TODO Task\n")
60 var app: [String] = []
61 h.editor.onAppCommand = { app.append($0) }
62 h.caret(at: 2)
63 type(h, "SPC f s")
64 #expect(app == ["app.save"])
65 type(h, "RET")
66 #expect(h.editor.document.text == "* DONE Task\n")
67 type(h, "SPC m t")
68 #expect(h.editor.document.text == "* KILL Task\n" || h.editor.document.text.hasPrefix("* "))
69 type(h, "TAB")
70 #expect(h.editor.vim.mode == .normal)
71 }
72
73 @Test func visualSelection() {
74 let h = doom("one two three\n")
75 h.caret(at: 0)
76 type(h, "v e")
77 #expect(h.textView.selectedRange() == NSRange(location: 0, length: 3))
78 type(h, "d")
79 #expect(h.editor.document.text == " two three\n")
80 #expect(h.textView.selectedRange().length == 0)
81 }
82
83 func shiftTab(_ h: Harness) {
84 KeyTests().send(h, characters: "\u{19}", ignoring: "\u{19}", code: 48, flags: .shift)
85 }
86
87 @Test func insertStateTabFollowsDoom() {
88 let h = doom("* Top\n** Sub\n- one\n- item\n - child\nbody\n")
89 h.caret(at: 8)
90 type(h, "i")
91 #expect(h.editor.vim.mode == .insert)
92 shiftTab(h)
93 #expect(h.editor.document.text.hasPrefix("* Top\n* Sub\n"))
94 type(h, "TAB")
95 #expect(h.editor.document.text.hasPrefix("* Top\n** Sub\n"))
96 let item = (h.editor.document.text as NSString).range(of: "- item").location
97 h.caret(at: item + 3)
98 type(h, "TAB")
99 #expect(h.editor.document.text.contains("- one\n - item\n - child\n"))
100 #expect(h.editor.vim.mode == .insert)
101 }
102
103 @Test func shiftTabCyclesInNormalState() {
104 let h = doom("* Top\nbody\n")
105 h.caret(at: 7)
106 let before = h.editor.document.text
107 shiftTab(h)
108 #expect(h.editor.document.text == before)
109 #expect(h.editor.vim.mode == .normal)
110 }
111
112 @Test func reportsStateAndQuits() {
113 let h = doom("text\n")
114 var states: [String?] = []
115 var app: [String] = []
116 h.editor.onModeChange = { states.append($0) }
117 h.editor.onAppCommand = { app.append($0) }
118 h.caret(at: 0)
119 type(h, "i")
120 type(h, "ESC")
121 type(h, "V")
122 type(h, "ESC")
123 type(h, "SPC q q")
124 #expect(states == ["insert", "normal", "visual-line", "normal"])
125 #expect(app == ["app.quit"])
126 h.editor.modal = false
127 #expect(states.last == .some(nil))
128 }
129
130 @Test func macrosReplayKeys() {
131 let h = doom("one two\nthree four\nfive six\nseven\n")
132 h.caret(at: 0)
133 type(h, "q a d w j q")
134 #expect(h.editor.document.text == "two\nthree four\nfive six\nseven\n")
135 type(h, "@ a")
136 #expect(h.editor.document.text == "two\nfour\nfive six\nseven\n")
137 type(h, "@ @")
138 #expect(h.editor.document.text == "two\nfour\nsix\nseven\n")
139 type(h, "g g q b A ! ESC j q 2 @ b")
140 #expect(h.editor.document.text == "two!\nfour!\nsix!\nseven\n", "\(h.editor.document.text.debugDescription)")
141 }
142
143 @Test func jumpsAndMarks() {
144 let h = doom("one\ntwo\nthree\nfour\n")
145 h.caret(at: 4)
146 type(h, "G")
147 #expect(h.textView.selectedRange().location == 19)
148 type(h, "C-o")
149 #expect(h.textView.selectedRange().location == 4)
150 type(h, "C-i")
151 #expect(h.textView.selectedRange().location == 19)
152 type(h, "g g m a G ' a")
153 #expect(h.textView.selectedRange().location == 0)
154 // Marks follow edits above them.
155 type(h, "G m b g g O")
156 h.textView.insertText("new", replacementRange: h.textView.selectedRange())
157 type(h, "ESC ` b")
158 #expect(h.textView.selectedRange().location == 23)
159 }
160}