Tests/OrgCoreTests/ReadDateTests.swift

9abba9cfc504b344b7d267978a97f1c925a1d5dd
orgstar/Tests/OrgCoreTests/ReadDateTests.swift history · blame · raw

77 lines · 3892 bytes

 1import Foundation
 2import Testing
 3@testable import OrgCore
 4
 5/// Emacs Lisp that runs `body` with the clock stopped at `seconds` (UTC in the oracle).
 6func frozenClock(_ seconds: Int, _ body: String) -> String {
 7    """
 8    (cl-letf* ((frozen (seconds-to-time \(seconds)))
 9               (odt (symbol-function 'decode-time))
10               (oft (symbol-function 'format-time-string))
11               (otl (symbol-function 'time-less-p))
12               (ots (symbol-function 'time-subtract))
13               (ota (symbol-function 'time-add))
14               ((symbol-function 'current-time) (lambda () frozen))
15               ((symbol-function 'org-today) (lambda () (time-to-days frozen)))
16               ((symbol-function 'time-less-p) (lambda (a b) (funcall otl (or a frozen) (or b frozen))))
17               ((symbol-function 'time-subtract) (lambda (a b) (funcall ots (or a frozen) (or b frozen))))
18               ((symbol-function 'time-add) (lambda (a b) (funcall ota (or a frozen) (or b frozen))))
19               ((symbol-function 'decode-time) (lambda (&optional time zone form) (funcall odt (or time frozen) zone form)))
20               ((symbol-function 'format-time-string) (lambda (f &optional time zone) (funcall oft f (or time frozen) zone))))
21      \(body))
22    """
23}
24
25struct ReadDateTests {
26    /// Thu 2026-09-10 00:26 UTC.
27    static let now = Date(timeIntervalSince1970: 1_789_000_000)
28    static var utc: Calendar {
29        var calendar = Calendar(identifier: .gregorian)
30        calendar.timeZone = TimeZone(identifier: "UTC")!
31        return calendar
32    }
33
34    static func format(_ answer: ReadDate.Answer) -> String {
35        let c = answer.components(utc)
36        let date = String(format: "%04d-%02d-%02d", c.year!, c.month!, c.day!)
37        return (answer.timeGiven ? date + String(format: " %02d:%02d", c.hour!, c.minute!) : date) + "|" + (answer.endTime ?? "nil")
38    }
39
40    static let inputs = [
41        "", ".", "+0", "+1", "+2d", "-3d", "+1w", "+2m", "-1y", "++3", "fri", "+fri", "-fri", "+2fri", "mon",
42        "2026-10-07", "2026-10-7", "10-7", "7.10.", "7.10.2027", "10/7", "10/7/27", "12/31",
43        "3", "15", "sep 15", "15 sep", "jan", "dec 25", "2027",
44        "10:00", "9:30", "10am", "3pm", "12am", "12:30pm", "10h", "10h30", "h45",
45        "10:00-11:30", "10:00+1:30", "fri 10:00", "2026-10-07 14:15", "+2d 9:00", "+3h",
46        "w42", "2027-w01", "w3-5", "tomorrow", "nonsense",
47    ]
48
49    @Test func examples() {
50        let answer = ReadDate.analyze("fri 10:00", default: Self.now, now: Self.now, calendar: Self.utc)
51        #expect(Self.format(answer) == "2026-09-11 10:00|nil")
52        #expect(Self.format(ReadDate.analyze("10:00-11:30", default: Self.now, now: Self.now, calendar: Self.utc)) == "2026-09-10 10:00|11:30")
53    }
54
55    @Test(.enabled(if: ProcessInfo.processInfo.environment["ORGSTAR_SKIP_ORACLE"] == nil))
56    func readDateMatchesEmacs() throws {
57        try #require(EmacsOracle.isAvailable)
58        let cases = Self.inputs.map { input in
59            EmacsOracle.Case(text: "", point: 1, form: frozenClock(1_789_000_000, """
60                (eval '(let ((org-time-was-given nil) (org-end-time-was-given nil))
61                         (let ((d (org-read-date nil nil "\(input)")))
62                           (insert d "|" (or org-end-time-was-given "nil"))))
63                      nil)
64                """))
65        }
66        var mismatches = 0
67        for (input, emacs) in zip(Self.inputs, try EmacsOracle.run(cases)) {
68            let ours = Self.format(ReadDate.analyze(input, default: Self.now, now: Self.now, calendar: Self.utc))
69            let theirs = emacs.error.isEmpty ? emacs.text : "error: \(emacs.error)"
70            if ours != theirs {
71                mismatches += 1
72                Issue.record("\(input.debugDescription): ours \(ours), emacs \(theirs)")
73            }
74        }
75        #expect(mismatches == 0)
76    }
77}