Tests/OrgCoreTests/ElispLinkTests.swift

38 lines · 2185 bytes

 1import Foundation
 2import Testing
 3@testable import OrgCore
 4
 5/// `elisp:` links evaluated in `Elisp`, with the message `org-link--open-elisp` shows.
 6struct ElispLinkTests {
 7    static let forms = [
 8        "(+ 1 2)", "(list 1 \"a\" 'b 2.5)", "(concat \"a\" \"b\")", "(message \"hi\")", "(let ((x 2)) (* x x))",
 9        "(mapcar (lambda (n) (* n 10)) '(1 2 3))", "nil", "(car 1)", "(/ 1 0)", "(error \"Nope %d\" 3)", "(format \"%S\" \"q\")",
10        "(org-lookup-first 2 '(1 2 3) '(a b c))", "(org-lookup-all 1 '(1 2 1) '(x y z))", "(format \"%S\" '(lambda () 1))",
11    ]
12
13    @Test(.enabled(if: ProcessInfo.processInfo.environment["ORGSTAR_SKIP_ORACLE"] == nil))
14    func matchesEmacs() throws {
15        let list = "(list " + Self.forms.map { form in
16            let quoted = form.debugDescription
17            return "(condition-case e (format \"%s => %s\" \(quoted) (eval (read \(quoted)))) (error (error-message-string e)))"
18        }.joined(separator: " ") + ")"
19        let emacs = try EmacsOracle.evaluate("", list)
20        #expect(emacs.count == Self.forms.count)
21        for (form, expected) in zip(Self.forms, emacs) {
22            #expect(Links.evaluateElisp(form) == expected, "\(form)")
23        }
24    }
25
26    @Test func refusesWhatOnlyEmacsHas() {
27        #expect(Links.evaluateElisp("(org-agenda nil \"a\")") == "This link uses Emacs Lisp that runs only in Emacs.")
28        // Emacs has values for variables the interpreter doesn't define.
29        for form in ["(+ fill-column 1)", "most-positive-fixnum", "(concat user-full-name \"\")", "(condition-case nil org-directory (void-variable 0))"] {
30            #expect(Links.evaluateElisp(form) == "This link uses Emacs Lisp that runs only in Emacs.", "\(form)")
31        }
32        #expect(Links.evaluateElisp("(let ((x 2)) (+ x 1))") == "(let ((x 2)) (+ x 1)) => 3")
33        // Emacs 31 prints a function made under dynamic binding as `#[nil (1) nil]`.
34        for form in ["(let ((f (lambda () 1))) f)", "(prin1-to-string (lambda () 1))", "(format \"%S\" (lambda () 1))", "(car (lambda () 1))"] {
35            #expect(Links.evaluateElisp(form) == "This link uses Emacs Lisp that runs only in Emacs.", "\(form)")
36        }
37    }
38}