Tests/OrgCoreTests/ReadDateTests.swift

af140c9251eac8434e572e9dda587c4573b64e6b
orgstar/Tests/OrgCoreTests/ReadDateTests.swift history · blame · raw

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