krz/orgstar

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

Tests/OrgEditorAppKitTests/FindTests.swift

ab6ccec56eca03d0dadf2c9332aab10c4490eb62
orgstar/Tests/OrgEditorAppKitTests/FindTests.swift history · blame · raw

58 lines · 2146 bytes

 1import AppKit
 2import OrgCore
 3import Testing
 4@testable import OrgEditorAppKit
 5
 6@MainActor
 7struct FindTests {
 8    @Test func replaceShowsTheFindBar() {
 9        let h = Harness("* Heading\nbody\n")
10        #expect(h.textView.enclosingScrollView?.isFindBarVisible != true)
11        h.editor.run("edit.replace")
12        #expect(h.textView.enclosingScrollView?.isFindBarVisible == true)
13    }
14
15    @Test func aMatchInFoldedTextUnfoldsIt() {
16        let text = "* Heading\nhidden needle\n* Next\n"
17        let h = Harness(text)
18        h.caret(at: 0)
19        h.editor.cycleAtCaret()
20        #expect(!h.editor.foldState.folds.isEmpty)
21        let needle = (text as NSString).range(of: "needle")
22        h.textView.setSelectedRange(needle)
23        #expect(h.editor.foldState.folds.isEmpty)
24        #expect(h.textView.selectedRange() == needle)
25    }
26}
27
28@MainActor
29struct BodyFoldTests {
30    /// CONTENTS view hides bodies (body folds); an edit elsewhere keeps them hidden.
31    @Test func bodyFoldsSurviveEdits() {
32        let h = Harness("* A\nbody a\n** B\nbody b\n* C\nbody c\n")
33        h.editor.cycleGlobal()
34        h.editor.cycleGlobal()
35        #expect(!h.editor.foldState.bodyFolds.isEmpty)
36        let hiddenBefore = h.editor.hidden.all
37        #expect(!hiddenBefore.isEmpty)
38        h.caret(at: 3)
39        h.textView.insertText("x", replacementRange: NSRange(location: 3, length: 0))
40        #expect(h.editor.hidden.all.count == hiddenBefore.count)
41        #expect(h.editor.hidden.all.map { $0.count } == hiddenBefore.map { $0.count })
42    }
43}
44
45@MainActor
46struct OpenLinkTests {
47    @Test func commandsHandTheLinkToTheApp() {
48        let h = Harness("* A\nRead [[https://orgmode.org][the manual]] now.\n")
49        var requests: [ExternalRequest] = []
50        h.editor.onExternal = { requests.append($0) }
51        let inside = (h.string as NSString).range(of: "the manual").location
52        h.caret(at: inside)
53        h.editor.run("org.link.open")
54        h.editor.run("org.dwim")
55        let link = OrgLink(type: "https", path: "//orgmode.org", raw: "https://orgmode.org")
56        #expect(requests == [.openLink(link), .openLink(link)])
57    }
58}