Tests/OrgCoreTests/DiarySexpTests.swift

06941b2cf25c6ccc1629723cd7951910ce15e67a
orgstar/Tests/OrgCoreTests/DiarySexpTests.swift history · blame · raw

113 lines · 6034 bytes

  1import Foundation
  2import Testing
  3@testable import OrgCore
  4
  5/// Diary sexps in the agenda (`org-agenda-get-sexps`, diary timestamps and planning).
  6struct DiarySexpTests {
  7    static let oracle = ProcessInfo.processInfo.environment["ORGSTAR_SKIP_ORACLE"] == nil
  8
  9    static let file = """
 10        #+FILETAGS: :ft:
 11        * Holidays :hol:
 12        %%(diary-float t 4 4) Fourth Thursday
 13        %%(diary-anniversary 10 7 1990) Birthday %d%s
 14        %%(org-anniversary 2000 10 8) Org %d years
 15        %%(diary-anniversary 2 29 2000) Leap %d
 16        %%(diary-block 10 5 2026 10 7 2026) Conference
 17        %%(diary-cyclic 3 10 1 2026) Every third, %d%s time
 18        %%(diary-date t 6 t) Sixth of month
 19        %%(diary-date '(10 11) '(5 9) 2026) List date
 20        %%(diary-remind '(diary-date 10 9 2026) 2) Remind me
 21        %%(diary-remind '(diary-date 10 12 2026) -3) Remind range
 22        %%(diary-remind '(diary-anniversary 10 14 2000) 7) Week ahead %d
 23        %%(org-class 2026 9 1 2026 12 20 2 41) Class on Tuesdays
 24        %%(org-block 2026 10 6 2026 10 6) Meeting at 10:30
 25        %%(memq (calendar-day-of-week date) '(1 3 5)) Gym
 26        %%(and (= (calendar-extract-day date) 2) "Custom; second part")
 27        %%(let ((d (calendar-extract-day date))) (when (= (% d 5) 0) (format "Day %d is a fifth" d)))
 28        %%(diary-float 10 1 -1) Last Monday of October
 29        %%(diary-float t 0 1 8) First Sunday after the 8th
 30        %%(diary-date 10 8 2026)
 31        &%%(diary-date 10 9 2026) Ampersand  
 32        * TODO Task with a diary stamp <%%(diary-float t 2 1)>
 33        * Meeting
 34          <%%(diary-date t 7 t) 14:00>
 35        * Due
 36          DEADLINE: <%%(diary-date 10 8 2026)>
 37        * Sched
 38          SCHEDULED: <%%(diary-float t 5 1)>
 39        * COMMENT Skipped tree
 40        %%(diary-date t t t) Never shown
 41        """ + "\n"
 42
 43    @Test func evaluatesDiaryFunctions() throws {
 44        let day = Days.absolute(year: 2026, month: 10, day: 7)
 45        func run(_ text: String, _ entry: String = "E") throws -> [String]? {
 46            DiarySexp.entries(try LispReader.readFirst(text).sexp, entry: entry, day: day)
 47        }
 48        #expect(try run("(diary-anniversary 10 7 1990)", "%d%s") == ["36th"])
 49        #expect(try run("(diary-date 10 8 2026)") == nil)
 50        #expect(try run("(diary-float 10 3 1)") == ["E"])
 51        #expect(try run("\"a; b\"") == ["a", "b"])
 52        #expect(try run("(undefined)") == nil)
 53        #expect(try run("(diary-ordinal-suffix -3)") == nil)
 54    }
 55
 56    @Test func skipsSexpsBeforeTheFirstHeading() {
 57        let source = AgendaSource(path: "a.org", text: "%%(diary-date t t t) Before\n* H\n%%(diary-date t t t) Under\n")
 58        let day = Days.absolute(year: 2026, month: 10, day: 7)
 59        #expect(Agenda.list([source], start: day, days: 1, today: day, now: (9, 0))[0].items.map(\.text) == ["Under"])
 60    }
 61
 62    @Test(.enabled(if: oracle))
 63    func agendaMatchesEmacs() throws {
 64        let files = [("diary.org", Self.file)]
 65        let october = Days.absolute(year: 2026, month: 10, day: 1)
 66        let ours = Agenda.list([AgendaSource(path: "diary.org", text: Self.file)], start: october, days: 15, today: october + 4, now: (9, 30))
 67        let kinds = Set(ours.flatMap(\.items).map(\.kind))
 68        #expect(kinds.isSuperset(of: [.sexp, .timestamp, .deadline, .pastScheduled]))
 69        try AgendaOracle.compare(files: files, start: october, span: 15, today: october + 4)
 70        try AgendaOracle.compare(files: files, start: october + 20, span: 10, today: october + 26)
 71        let leap = Days.absolute(year: 2027, month: 2, day: 27)
 72        try AgendaOracle.compare(files: files, start: leap, span: 4, today: leap)
 73    }
 74}
 75
 76/// `calendar-check-holidays` with the lists orgstar has: Emacs's general and Christian holidays.
 77struct DiaryHolidayTests {
 78    @Test(.enabled(if: DiarySexpTests.oracle))
 79    func checksHolidaysLikeEmacs() throws {
 80        let first = Days.absolute(year: 2024, month: 1, day: 1)
 81        let last = Days.absolute(year: 2030, month: 12, day: 31)
 82        let form = """
 83            (progn
 84              (require 'holidays)
 85              (let ((calendar-christian-all-holidays-flag nil)
 86                    (calendar-holidays (append holiday-general-holidays holiday-christian-holidays))
 87                    out)
 88                (dotimes (i \(last - first + 1))
 89                  (let ((names (calendar-check-holidays (calendar-gregorian-from-absolute (+ \(first) i)))))
 90                    (when names (push (format "%d %s" (+ \(first) i) (mapconcat #'identity names "|")) out))))
 91                (nreverse out)))
 92            """
 93        let emacs = try EmacsOracle.evaluate("", form)
 94        let ours = (first...last).compactMap { day -> String? in
 95            let names = DiarySexp.holidays(day)
 96            return names.isEmpty ? nil : "\(day) " + names.joined(separator: "|")
 97        }
 98        #expect(ours == emacs, "\(Array(Set(ours).symmetricDifference(Set(emacs))).sorted().prefix(10))")
 99    }
100
101    @Test func holidaySexps() throws {
102        let thanksgiving = Days.absolute(year: 2026, month: 11, day: 26)
103        func read(_ text: String) throws -> Sexp { try LispReader.read(text)[0].sexp }
104        #expect(DiarySexp.entries(try read("(org-calendar-holiday)"), entry: "", day: thanksgiving) == ["Thanksgiving"])
105        #expect(DiarySexp.entries(try read("(org-calendar-holiday)"), entry: "", day: thanksgiving + 1) == nil)
106        // Thursdays in November, skipping holidays, then only Thanksgiving by name.
107        let thursdays = "(org-class 2026 11 1 2026 11 30 4"
108        #expect(DiarySexp.entries(try read(thursdays + " 'holidays)"), entry: "Class", day: thanksgiving) == nil)
109        #expect(DiarySexp.entries(try read(thursdays + " \"Thanksgiving\")"), entry: "Class", day: thanksgiving) == nil)
110        #expect(DiarySexp.entries(try read(thursdays + " \"Christmas\")"), entry: "Class", day: thanksgiving) == ["Class"])
111        #expect(DiarySexp.entries(try read(thursdays + " 'holidays)"), entry: "Class", day: thanksgiving - 7) == ["Class"])
112    }
113}