krz/orgstar

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

Tests/OrgCoreTests/NarrowingTests.swift

ab6ccec56eca03d0dadf2c9332aab10c4490eb62
orgstar/Tests/OrgCoreTests/NarrowingTests.swift history · blame · raw

55 lines · 2167 bytes

 1import Foundation
 2import Testing
 3@testable import OrgCore
 4
 5struct NarrowingTests {
 6    static let oracle = ProcessInfo.processInfo.environment["ORGSTAR_SKIP_ORACLE"] == nil
 7
 8    static let text = """
 9        Intro line
10        * One
11        Body of one
12        #+begin_src sh
13        echo hi
14        #+end_src
15
16
17        ** Child
18        child body
19        * Two [[https://x.org][link]]
20        #+BEGIN_QUOTE
21        quoted
22        #+END_QUOTE
23        last
24        #+begin_example
25        tail
26        #+end_example
27        """
28
29    @Test(.enabled(if: oracle))
30    func narrowsLikeOrg() throws {
31        let ns = Self.text as NSString
32        let offsets = ["Intro", "Body", "echo", "child body", "** Child", "quoted", "last", "#+end_src", "#+BEGIN_QUOTE", "#+END_QUOTE", "\n\n**", "tail"].map { ns.range(of: $0).location + 1 }
33        for (form, ours) in [("(org-narrow-to-subtree)", Narrowing.subtree), ("(org-narrow-to-block)", Narrowing.block)] {
34            let points = offsets.map { String(EmacsOracle.point($0, in: Self.text)) }.joined(separator: " ")
35            let emacs = try EmacsOracle.evaluate(Self.text, """
36                (with-current-buffer (find-file-noselect "oracle.org")
37                  (mapcar (lambda (p) (widen) (goto-char p)
38                            (condition-case e (progn \(form) (format "%d %d" (1- (point-min)) (1- (point-max)))) (error "error")))
39                          '(\(points))))
40                """)
41            let mine = offsets.map { offset -> String in
42                guard let r = try? ours(offset, Self.text) else { return "error" }
43                return "\(r.lowerBound) \(r.upperBound)"
44            }
45            #expect(mine == emacs, "\(form)")
46        }
47    }
48
49    @Test func goesToHeadingsByPath() {
50        let context = EditContext(revision: 0, text: Self.text, tree: OrgParser.parse(Self.text), selection: [0..<0])
51        #expect(GotoHeading.targets(context).map(\.path) == ["One", "One/Child", "Two link"])
52        let caret = runCommand(GotoHeading(), Self.text, caret: 0, answers: ["goto": "One/Child"]).caret
53        #expect(caret == (Self.text as NSString).range(of: "** Child").location)
54    }
55}