Tests/OrgEditorAppKitTests/SnippetEditorTests.swift

5b5584bafaafd3d627ecb2778987cef0e743de9c
orgstar/Tests/OrgEditorAppKitTests/SnippetEditorTests.swift

144 lines · 5405 bytes

  1import AppKit
  2import OrgCore
  3import Testing
  4@testable import OrgEditorAppKit
  5
  6/// TAB expands a trigger only where it means nothing else, in each preset; then TAB, S-TAB
  7/// and Escape move through the fields and end the snippet.
  8@MainActor
  9struct SnippetEditorTests {
 10    static let snippets = [
 11        Snippet(trigger: "date", body: "<2026-11-25 Wed>"),
 12        Snippet(trigger: "todo", body: "TODO ${1:what} by ${2:when}$0"),
 13        Snippet(trigger: "pair", body: "${1:a}-$1"),
 14    ]
 15
 16    func harness(_ text: String, preset: KeymapPreset = .emacs, caret: Int? = nil) -> Harness {
 17        let h = Harness(text)
 18        h.editor.keymap = preset.keymap
 19        h.editor.modal = preset.isModal
 20        var options = h.editor.options
 21        options.snippets = Self.snippets
 22        h.editor.options = options
 23        h.caret(at: caret ?? text.utf16.count)
 24        return h
 25    }
 26
 27    func type(_ h: Harness, _ text: String) {
 28        h.textView.insertText(text, replacementRange: h.textView.selectedRange())
 29    }
 30
 31    func key(_ h: Harness, _ keys: String) { ModalTests().type(h, keys) }
 32
 33    func shiftTab(_ h: Harness) {
 34        KeyTests().send(h, characters: "\u{19}", ignoring: "\u{19}", code: 48, flags: .shift)
 35    }
 36
 37    @Test(arguments: [KeymapPreset.emacs, .mac]) func tabExpandsATrigger(preset: KeymapPreset) {
 38        let h = harness("Due date", preset: preset)
 39        key(h, "TAB")
 40        #expect(h.string == "Due <2026-11-25 Wed>" && h.editor.snippetSession == nil)
 41        // No trigger: TAB is the text system's again.
 42        let plain = harness("Due soon", preset: preset)
 43        key(plain, "TAB")
 44        #expect(plain.string == "Due soon\t")
 45    }
 46
 47    @Test(arguments: [KeymapPreset.emacs, .mac]) func tabStillCyclesAndMovesInTables(preset: KeymapPreset) {
 48        let heading = harness("* date\nbody\n", preset: preset, caret: 6)
 49        key(heading, "TAB")
 50        #expect(heading.string == "* date\nbody\n" && heading.editor.foldState.folds.contains(0))
 51        let table = harness("| date | b |\n| c | d |\n", preset: preset, caret: 6)
 52        key(table, "TAB")
 53        #expect(!table.string.contains("2026") && table.caret > 7)
 54    }
 55
 56    @Test func doomExpandsInInsertStateOnly() {
 57        let h = harness("Due date", preset: .doom)
 58        key(h, "TAB")
 59        #expect(h.string == "Due date")
 60        key(h, "A TAB")
 61        #expect(h.string == "Due <2026-11-25 Wed>")
 62        // A heading demotes in insert state, as Doom's TAB does there.
 63        let heading = harness("* date", preset: .doom)
 64        key(heading, "A TAB")
 65        #expect(heading.string == "** date")
 66    }
 67
 68    @Test func fieldsTakeTypingAndTabMovesOn() {
 69        let h = harness("- todo")
 70        key(h, "TAB")
 71        #expect(h.string == "- TODO what by when")
 72        #expect(h.textView.selectedRange() == NSRange(location: 7, length: 4))
 73        type(h, "Call")
 74        key(h, "TAB")
 75        #expect(h.textView.selectedRange() == NSRange(location: 15, length: 4))
 76        shiftTab(h)
 77        #expect(h.textView.selectedRange() == NSRange(location: 7, length: 4))
 78        key(h, "TAB")
 79        type(h, "Fri")
 80        key(h, "TAB")
 81        #expect(h.string == "- TODO Call by Fri" && h.caret == 18 && h.editor.snippetSession == nil)
 82        // TAB is TAB again.
 83        key(h, "TAB")
 84        #expect(h.string == "- TODO Call by Fri\t")
 85    }
 86
 87    @Test func mirrorsFollowTheirField() {
 88        let h = harness("pair")
 89        key(h, "TAB")
 90        type(h, "x")
 91        #expect(h.string == "x-x")
 92        type(h, "yz")
 93        #expect(h.string == "xyz-xyz")
 94        h.textView.deleteBackward(nil)
 95        #expect(h.string == "xy-xy")
 96    }
 97
 98    @Test func escapeOrLeavingEndsIt() {
 99        let h = harness("todo")
100        key(h, "TAB")
101        key(h, "ESC")
102        #expect(h.editor.snippetSession == nil)
103        let moved = harness("text\ntodo")
104        key(moved, "TAB")
105        #expect(moved.editor.snippetSession != nil)
106        moved.caret(at: 0)
107        #expect(moved.editor.snippetSession == nil)
108    }
109
110    @Test func undoingTheExpansionEndsIt() {
111        let h = harness("todo")
112        key(h, "TAB")
113        h.textView.undoManager?.undo()
114        #expect(h.string == "todo" && h.editor.snippetSession == nil)
115    }
116
117    @Test func completionListsAndExpandsSnippets() {
118        let h = harness("Notes da")
119        h.editor.run("editor.complete")
120        #expect(h.string == "Notes <2026-11-25 Wed>")
121        let several = harness("x ")
122        type(several, "p")
123        several.editor.run("editor.complete")
124        #expect(several.string == "x a-a" && several.editor.snippetSession != nil)
125    }
126
127    @Test func aKeyCanExpandOneSnippet() throws {
128        var problems: [String] = []
129        let user = try Keymap(toml: "[[bind]]\nkeys = \"C-c s\"\ncommand = \"org.snippet:date\"\n", problems: &problems, commands: Set(EditorCommands.all.map(\.id)))
130        #expect(problems.isEmpty)
131        let h = harness("On ")
132        h.editor.keymap = .layered([KeymapPreset.emacs.keymap, user])
133        key(h, "C-c s")
134        #expect(h.string == "On <2026-11-25 Wed>")
135    }
136
137    @Test func commandsByName() {
138        let h = harness("")
139        h.editor.run("org.snippet:todo")
140        #expect(h.string == "TODO what by when" && h.editor.snippetSession != nil)
141        h.editor.run("org.snippet.next-field")
142        #expect(h.textView.selectedRange() == NSRange(location: 13, length: 4))
143    }
144}
Keyboard shortcuts

Global

⌘K
Command palette & jump
/
Focus search
?
Show this help
g d
Dashboard
g e
Explore repositories
g n
Notifications

Repository & editor

t
Find file in repository
y
Copy immutable SHA permalink
g c
Code tab
g i
Issues tab
g m
Merge requests tab
g b
Builds tab
⌘↵
Submit active form