krz/orgstar

A native macOS editor for org-mode files. editor org-mode swift

Tests/OrgEditorAppKitTests/ElectricPairEditorTests.swift

778058aafc7ddacee4a613b1da87c60aa0c8e113
orgstar/Tests/OrgEditorAppKitTests/ElectricPairEditorTests.swift history · blame · raw

56 lines · 1801 bytes

6 symbols in this file
 1import AppKit
 2import OrgCore
 3import Testing
 4@testable import OrgEditorAppKit
 5
 6/// `electric-pair-mode` through key events, in the Emacs and Doom presets.
 7@MainActor
 8struct ElectricPairEditorTests {
 9    func harness(_ text: String, on: Bool = true) -> Harness {
10        let h = Harness(text)
11        var options = h.editor.options
12        options.electricPair = on
13        h.editor.options = options
14        return h
15    }
16
17    func key(_ h: Harness, _ characters: String, code: UInt16 = 0) {
18        KeyTests().send(h, characters: characters, ignoring: characters, code: code)
19    }
20
21    @Test func pairsSkipsAndDeletes() {
22        let h = harness("Text \n")
23        h.caret(at: 5)
24        key(h, "(")
25        #expect(h.editor.document.text == "Text ()\n")
26        #expect(h.textView.selectedRange().location == 6)
27        key(h, "\u{7f}", code: 51)
28        #expect(h.editor.document.text == "Text \n")
29        key(h, "[")
30        key(h, "\r", code: 36)
31        #expect(h.editor.document.text == "Text [\n\n]\n")
32        #expect(h.textView.selectedRange().location == 7)
33        h.caret(at: 8)
34        key(h, "]")
35        #expect(h.editor.document.text == "Text [\n\n]\n")
36        #expect(h.textView.selectedRange().location == 9)
37    }
38
39    @Test func offTypesAsUsual() {
40        let h = harness("Text \n", on: false)
41        h.caret(at: 5)
42        #expect(!h.editor.electricPair(KeyChord("(")))
43        #expect(h.editor.document.text == "Text \n")
44    }
45
46    @Test func doomInsertState() {
47        let h = harness("x\n")
48        h.editor.keymap = KeymapPreset.doom.keymap
49        h.editor.modal = true
50        h.caret(at: 1)
51        ModalTests().type(h, "a")
52        key(h, "\"")
53        #expect(h.editor.document.text == "x\"\"\n")
54        #expect(h.textView.selectedRange().location == 2)
55    }
56}