Tests/OrgEditorAppKitTests/ModalTests.swift
126 lines · 4463 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 let code = codes[key] {
22 let characters = key == "ESC" ? "\u{1b}" : key == "RET" ? "\r" : key == "TAB" ? "\t" : " "
23 KeyTests().send(h, characters: characters, ignoring: characters, code: code)
24 } else {
25 let shifted = key.first!.isUppercase
26 KeyTests().send(h, characters: key, ignoring: key, code: 0, flags: shifted ? .shift : [])
27 }
28 }
29 }
30
31 @Test func normalStateEditsAndInsertTypes() {
32 let h = doom("hello world\nsecond\n")
33 h.caret(at: 0)
34 type(h, "d w")
35 #expect(h.editor.document.text == "world\nsecond\n")
36 type(h, "c i w")
37 #expect(h.editor.vim.mode == .insert)
38 h.textView.insertText("planet", replacementRange: h.textView.selectedRange())
39 type(h, "ESC")
40 #expect(h.editor.document.text == "planet\nsecond\n")
41 #expect(h.textView.selectedRange().location == 5)
42 type(h, "j .")
43 #expect(h.editor.document.text == "planet\nplanet\n")
44 }
45
46 @Test func undo() {
47 let h = doom("hello world\n")
48 h.caret(at: 0)
49 type(h, "d w")
50 #expect(h.editor.document.text == "world\n")
51 type(h, "u")
52 #expect(h.editor.document.text == "hello world\n")
53 }
54
55 @Test func leaderAndOrgKeys() {
56 let h = doom("* TODO Task\n")
57 var app: [String] = []
58 h.editor.onAppCommand = { app.append($0) }
59 h.caret(at: 2)
60 type(h, "SPC f s")
61 #expect(app == ["app.save"])
62 type(h, "RET")
63 #expect(h.editor.document.text == "* DONE Task\n")
64 type(h, "SPC m t")
65 #expect(h.editor.document.text == "* KILL Task\n" || h.editor.document.text.hasPrefix("* "))
66 type(h, "TAB")
67 #expect(h.editor.vim.mode == .normal)
68 }
69
70 @Test func visualSelection() {
71 let h = doom("one two three\n")
72 h.caret(at: 0)
73 type(h, "v e")
74 #expect(h.textView.selectedRange() == NSRange(location: 0, length: 3))
75 type(h, "d")
76 #expect(h.editor.document.text == " two three\n")
77 #expect(h.textView.selectedRange().length == 0)
78 }
79
80 func shiftTab(_ h: Harness) {
81 KeyTests().send(h, characters: "\u{19}", ignoring: "\u{19}", code: 48, flags: .shift)
82 }
83
84 @Test func insertStateTabFollowsDoom() {
85 let h = doom("* Top\n** Sub\n- one\n- item\n - child\nbody\n")
86 h.caret(at: 8)
87 type(h, "i")
88 #expect(h.editor.vim.mode == .insert)
89 shiftTab(h)
90 #expect(h.editor.document.text.hasPrefix("* Top\n* Sub\n"))
91 type(h, "TAB")
92 #expect(h.editor.document.text.hasPrefix("* Top\n** Sub\n"))
93 let item = (h.editor.document.text as NSString).range(of: "- item").location
94 h.caret(at: item + 3)
95 type(h, "TAB")
96 #expect(h.editor.document.text.contains("- one\n - item\n - child\n"))
97 #expect(h.editor.vim.mode == .insert)
98 }
99
100 @Test func shiftTabCyclesInNormalState() {
101 let h = doom("* Top\nbody\n")
102 h.caret(at: 7)
103 let before = h.editor.document.text
104 shiftTab(h)
105 #expect(h.editor.document.text == before)
106 #expect(h.editor.vim.mode == .normal)
107 }
108
109 @Test func reportsStateAndQuits() {
110 let h = doom("text\n")
111 var states: [String?] = []
112 var app: [String] = []
113 h.editor.onModeChange = { states.append($0) }
114 h.editor.onAppCommand = { app.append($0) }
115 h.caret(at: 0)
116 type(h, "i")
117 type(h, "ESC")
118 type(h, "V")
119 type(h, "ESC")
120 type(h, "SPC q q")
121 #expect(states == ["insert", "normal", "visual-line", "normal"])
122 #expect(app == ["app.quit"])
123 h.editor.modal = false
124 #expect(states.last == .some(nil))
125 }
126}