krz/orgstar

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

Tests/OrgCoreTests/TimestampLinkTests.swift

16086b4cf2caff5328774b2cd5ae3ffe1ab65ca4
orgstar/Tests/OrgCoreTests/TimestampLinkTests.swift history · blame · raw

40 lines · 2000 bytes

 1import Foundation
 2import Testing
 3@testable import OrgCore
 4
 5/// C-c C-o on timestamps and date ranges: the agenda days `org-follow-timestamp-link` shows.
 6struct TimestampLinkTests {
 7    static let text = """
 8        * TODO Task
 9        SCHEDULED: <2026-10-08 Thu> DEADLINE: <2026-10-12 Mon 9:00>
10        :LOGBOOK:
11        CLOCK: [2026-10-05 Mon 10:00]--[2026-10-06 Tue 11:30] => 25:30
12        :END:
13        Trip <2026-10-20 Tue>--<2026-10-23 Fri> and [2026-11-01 Sun] then <2026-12-24 Thu 18:00-20:00>.
14        No date here.
15
16        """
17
18    @Test func opensTheAgendaLikeOrg() throws {
19        let ns = Self.text as NSString
20        var offsets: [Int] = []
21        for needle in ["<2026-10-08", "Thu>", "<2026-10-12", "9:00>", "[2026-10-05", "Tue 11:30]", "=> 25", "<2026-10-20", "--<2026-10-23", "Fri>", "[2026-11-01", "Sun]", "18:00-20", "No date"] {
22            let at = ns.range(of: needle).location
23            offsets += [at, at + 1, at + 3]
24        }
25        let range = ns.range(of: "Sun]")
26        offsets.append(range.location + range.length)
27        let form = """
28            (let (r) (require 'org-agenda) (condition-case nil
29                         (cl-letf (((symbol-function 'org-agenda-list) (lambda (&optional _ start span &rest _) (setq r (format "%s %s" start span)))))
30                           (org-open-at-point))
31                       (error nil))
32                     (erase-buffer) (insert (or r "nil")))
33            """
34        let results = try EmacsOracle.run(offsets.map { EmacsOracle.Case(text: Self.text, point: EmacsOracle.point($0, in: Self.text), form: form) })
35        for (offset, theirs) in zip(offsets, results) {
36            let ours = Links.timestampDays(at: offset, text: Self.text).map { "\($0.start) \($0.count)" } ?? "nil"
37            #expect(ours == theirs.text, "at \(offset) \(ns.substring(with: NSRange(location: offset, length: min(8, ns.length - offset))).debugDescription): ours \(ours), emacs \(theirs.text) \(theirs.error)")
38        }
39    }
40}