Tests/OrgCoreTests/LiterateConfigTests.swift

46 lines · 2701 bytes

4 symbols in this file
 1import Foundation
 2import Testing
 3@testable import OrgCore
 4
 5/// A literate configuration's Emacs Lisp, against `org-babel-tangle-file` with a target file
 6/// and `emacs-lisp\|elisp`, as `org-babel-load-file` calls it.
 7struct LiterateConfigTests {
 8    static let texts = [
 9        "#+title: Config\n\n* Org\n#+begin_src emacs-lisp\n(setq org-tags-column 0)\n#+end_src\n\n#+begin_src emacs-lisp :tangle no\n(setq org-log-done 'time)\n#+end_src\n",
10        "* Shell\n#+begin_src sh\necho no\n#+end_src\n* Lisp\n#+begin_src elisp\n  (setq a 1)\n  (setq b 2)\n#+end_src\n",
11        "* COMMENT Off\n#+begin_src emacs-lisp\n(setq off t)\n#+end_src\n* On\n#+begin_src emacs-lisp\n(setq on t)\n#+end_src\n",
12        "* Skipped\n:PROPERTIES:\n:header-args:emacs-lisp: :tangle no\n:END:\n#+begin_src emacs-lisp\n(setq skipped t)\n#+end_src\n* Kept\n#+begin_src emacs-lisp\n(setq kept t)\n#+end_src\n",
13        "#+PROPERTY: header-args :tangle no\n#+begin_src emacs-lisp\n(setq none t)\n#+end_src\n#+begin_src emacs-lisp :tangle yes\n(setq yes t)\n#+end_src\n",
14        "#+header: :tangle no\n#+begin_src emacs-lisp\n(setq header t)\n#+end_src\n* Archived :ARCHIVE:\n#+begin_src emacs-lisp\n(setq archived t)\n#+end_src\n",
15    ]
16
17    static func lines(_ text: String) -> [String] {
18        text.components(separatedBy: "\n").map { $0.trimmingCharacters(in: .whitespaces) }.filter { !$0.isEmpty }
19    }
20
21    @Test(.enabled(if: ProcessInfo.processInfo.environment["ORGSTAR_SKIP_ORACLE"] == nil))
22    func matchesEmacs() throws {
23        let cases = Self.texts.map { text in
24            EmacsOracle.Case(text: text, point: 1, form: """
25                (let* ((dir (make-temp-file "literate" t)) (org (expand-file-name "config.org" dir)) (el (expand-file-name "config.el" dir)))
26                  (write-region (point-min) (point-max) org)
27                  (org-babel-tangle-file org el "emacs-lisp\\\\|elisp")
28                  (erase-buffer)
29                  (when (file-exists-p el) (insert-file-contents el)))
30                """)
31        }
32        let results = try EmacsOracle.run(cases)
33        #expect(Self.lines(results[0].text) == ["(setq org-tags-column 0)"])
34        for (text, theirs) in zip(Self.texts, results) {
35            #expect(Self.lines(Tangle.literateLisp(text)) == Self.lines(theirs.text), "\(text)\n\(theirs.error)")
36        }
37    }
38
39    @Test func keepsLineNumbers() {
40        let text = Self.texts[0]
41        let lisp = Tangle.literateLisp(text).components(separatedBy: "\n")
42        #expect(lisp.count == text.components(separatedBy: "\n").count)
43        #expect(lisp[4] == "(setq org-tags-column 0)")
44        #expect(lisp.filter { !$0.isEmpty } == ["(setq org-tags-column 0)"])
45    }
46}