Tests/OrgEditorAppKitTests/ModalTests.swift

9abba9cfc504b344b7d267978a97f1c925a1d5dd
orgstar/Tests/OrgEditorAppKitTests/ModalTests.swift history · blame · raw

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