Tests/OrgEditorAppKitTests/ISearchEditorTests.swift

68 lines · 2532 bytes

 1import AppKit
 2import OrgCore
 3import Testing
 4@testable import OrgApp
 5@testable import OrgEditorAppKit
 6
 7/// C-s, C-r, Vim's `/` and auto-fill through the editor's key handling.
 8@MainActor
 9struct ISearchEditorTests {
10    let keys = ModalTests()
11
12    @Test func controlSSearchesAsYouTypeWithoutTyping() {
13        let h = Harness("foo bar foo baz\n")
14        var messages: [String] = []
15        h.editor.onMessage = { messages.append($0) }
16        h.caret(at: 0)
17        keys.type(h, "C-s b a")
18        #expect(h.textView.selectedRange() == NSRange(location: 4, length: 2))
19        keys.type(h, "z")
20        #expect(h.textView.selectedRange() == NSRange(location: 12, length: 3))
21        #expect(messages.last == "I-search: baz")
22        keys.type(h, "RET")
23        #expect(h.string == "foo bar foo baz\n")
24        #expect(h.textView.selectedRange() == NSRange(location: 15, length: 0))
25        // C-r from there, and C-g back to where it began.
26        keys.type(h, "C-r f o o C-g")
27        #expect(h.textView.selectedRange() == NSRange(location: 15, length: 0))
28        #expect(h.editor.isearch == nil)
29    }
30
31    @Test func anotherKeyEndsTheSearchAndRuns() {
32        let h = Harness("foo bar foo baz\n")
33        h.caret(at: 0)
34        keys.type(h, "C-s b a r C-a")
35        #expect(h.editor.isearch == nil)
36        #expect(h.textView.selectedRange() == NSRange(location: 0, length: 0))
37    }
38
39    @Test func vimSlashSearchesAndNRepeats() {
40        let h = keys.doom("one two one two\n")
41        h.caret(at: 0)
42        keys.type(h, "/ t w o")
43        #expect(h.textView.selectedRange() == NSRange(location: 4, length: 3))
44        keys.type(h, "RET")
45        #expect(h.textView.selectedRange().location == 4)
46        keys.type(h, "n")
47        #expect(h.textView.selectedRange().location == 12)
48        #expect(h.string == "one two one two\n")
49    }
50
51    @Test func spaceAutoFillsPastTheFillColumn() {
52        let h = Harness("\n")
53        var options = h.editor.options
54        options.fillColumn = 12
55        options.autoFill = true
56        h.editor.options = options
57        h.caret(at: 0)
58        for word in ["alpha", "beta", "gamma", "delta"] {
59            for letter in word { keys.type(h, String(letter)) }
60            keys.type(h, "SPC")
61        }
62        #expect(h.string == "alpha beta\ngamma delta \n")
63        h.editor.run("editor.toggle-auto-fill")
64        for letter in "epsilonzeta" { keys.type(h, String(letter)) }
65        keys.type(h, "SPC")
66        #expect(h.string == "alpha beta\ngamma delta epsilonzeta \n")
67    }
68}