Tests/OrgCoreTests/BabelCacheTests.swift

d84591dd4402f983173a2d86d65eef6b24957599
orgstar/Tests/OrgCoreTests/BabelCacheTests.swift

100 lines · 6442 bytes

  1import Foundation
  2import Testing
  3@testable import OrgCore
  4
  5struct BabelCacheTests {
  6    /// A file, and the text before which each run in turn puts the caret.
  7    struct Case {
  8        var text: String
  9        var runs: [String]
 10        /// Replacements made after the runs before the last, as the user editing.
 11        var edits: [(String, String)] = []
 12    }
 13
 14    static let cases: [Case] = [
 15        Case(text: "#+begin_src sh :cache yes\necho hi\n#+end_src\n", runs: ["#+begin_src", "#+begin_src"]),
 16        Case(text: "#+begin_src sh :cache yes\necho there\n#+end_src\n\n#+RESULTS[0000]:\n: old\n\nAfter.\n", runs: ["#+begin_src"]),
 17        Case(text: "#+begin_src sh :cache yes :results append\necho more\n#+end_src\n\n#+RESULTS[0000]:\n: first\n", runs: ["#+begin_src"]),
 18        Case(text: "#+begin_src sh :cache yes\necho new\n#+end_src\n\n#+RESULTS:\n: no hash\n", runs: ["#+begin_src"]),
 19        Case(text: "#+NAME: a\n#+begin_src sh :cache yes\necho named\n#+end_src\n\nText.\n\n#+RESULTS[abc]: a\n: stale\n", runs: ["#+begin_src", "#+begin_src"]),
 20        Case(text: "#+NAME: a\n#+begin_src sh :cache yes\necho named\n#+end_src\n", runs: ["#+begin_src", "#+begin_src"]),
 21        Case(text: "#+begin_src sh :cache yes :var x=5 :var y=\"two words\" :height 400 :width \"400\" :exports both\necho $x $y\n#+end_src\n",
 22             runs: ["#+begin_src", "#+begin_src"]),
 23        Case(text: "#+begin_src sh :cache yes :results output :exports none :wrap example\nprintf 'a\\nb\\n'\n#+end_src\n", runs: ["#+begin_src", "#+begin_src"]),
 24        Case(text: "#+NAME: tbl\n| 1 | x |\n| 2 | y |\n\n#+NAME: items\n- a\n- b\n\n#+begin_src python :cache yes :var t=tbl :var n=1.5 :var l=items\nreturn [len(t), n, l]\n#+end_src\n",
 25             runs: ["#+begin_src", "#+begin_src"]),
 26        Case(text: "#+begin_src emacs-lisp :cache yes :var x=4 :var s=\"q\\\"z\"\n(list x s)\n#+end_src\n", runs: ["#+begin_src", "#+begin_src"]),
 27        Case(text: "#+begin_src emacs-lisp :cache yes :lexical yes :results output\n(princ 7)\n#+end_src\n", runs: ["#+begin_src", "#+begin_src"]),
 28        Case(text: "#+NAME: a\n#+begin_src sh\necho hi\n#+end_src\n\n#+CALL: a() :cache yes\n", runs: ["#+CALL", "#+CALL"]),
 29        Case(text: "#+PROPERTY: header-args :cache yes\n#+begin_src sh :session none\necho prop\n#+end_src\n", runs: ["#+begin_src", "#+begin_src"]),
 30        Case(text: "#+NAME: tbl\n| a | b |\n|---+---|\n| 1 | x |\n\n#+begin_src python :cache yes :var t=tbl :colnames yes :rownames yes\nreturn t\n#+end_src\n",
 31             runs: ["#+begin_src", "#+begin_src"]),
 32        // Edited after the first run, so it runs again.
 33        Case(text: "#+begin_src sh :cache yes\necho one\n#+end_src\n", runs: ["#+begin_src", "#+begin_src"], edits: [("echo one", "echo two")]),
 34        // A reference to a cached block takes its result without running it.
 35        Case(text: "#+begin_src sh :var x=five\necho $((x+1))\n#+end_src\n\n#+NAME: five\n#+begin_src sh :cache yes\necho 5\n#+end_src\n",
 36             runs: ["#+begin_src sh :cache", "#+begin_src sh :var"], edits: [(": 5\n", ": 50\n")]),
 37        Case(text: "#+begin_src sh :var x=five\necho $((x+1))\n#+end_src\n\n#+NAME: five\n#+begin_src sh :cache yes\necho 5\n#+end_src\n\n#+RESULTS[0000]: five\n: 50\n",
 38             runs: ["#+begin_src sh :var"]),
 39    ]
 40
 41    static func caret(_ marker: String, in text: String) -> Int {
 42        (text as NSString).range(of: marker).location + 2
 43    }
 44
 45    @Test(.enabled(if: ProcessInfo.processInfo.environment["ORGSTAR_SKIP_ORACLE"] == nil))
 46    func matchesEmacs() throws {
 47        let folder = FileManager.default.temporaryDirectory
 48        var emacsCases: [EmacsOracle.Case] = []
 49        var ours: [(text: String, failure: String?)] = []
 50        for c in Self.cases {
 51            var text = c.text
 52            var failure: String?
 53            for (i, marker) in c.runs.enumerated() {
 54                if i == c.runs.count - 1, c.runs.count > 1 { for (old, new) in c.edits { text = text.replacingOccurrences(of: old, with: new) } }
 55                (text, failure) = try TestBabelRunner.execute(text, caret: Self.caret(marker, in: text), directory: folder)
 56            }
 57            ours.append((text, failure))
 58            let steps = c.runs.enumerated().map { i, marker in
 59                let edits = i == c.runs.count - 1 && c.runs.count > 1
 60                    ? c.edits.map { "(goto-char (point-min)) (while (search-forward \(Babel.lispString($0.0)) nil t) (replace-match \(Babel.lispString($0.1)) t t))" }.joined(separator: " ")
 61                    : ""
 62                return "\(edits) (goto-char (point-min)) (search-forward \(Babel.lispString(marker))) (goto-char (+ (match-beginning 0) 2)) (org-babel-execute-maybe)"
 63            }
 64            emacsCases.append(EmacsOracle.Case(text: c.text, point: 1, form: """
 65            (progn
 66              (require 'ob-shell)
 67              (require 'ob-python)
 68              (require 'ob-emacs-lisp)
 69              (setq org-confirm-babel-evaluate nil
 70                    org-babel-python-command "python3"
 71                    default-directory temporary-file-directory)
 72              \(steps.joined(separator: "\n  ")))
 73            """))
 74        }
 75        let emacs = try EmacsOracle.run(emacsCases)
 76        for (i, (mine, theirs)) in zip(ours, emacs).enumerated() {
 77            #expect(mine.text == theirs.text, "case \(i):\n\(Self.cases[i].text)\nours (\(mine.failure ?? "")):\n\(mine.text)\nemacs (\(theirs.error)):\n\(theirs.text)")
 78        }
 79    }
 80
 81    @Test func cachedRunSaysSo() throws {
 82        let text = "#+begin_src sh :cache yes\necho hi\n#+end_src\n"
 83        let folder = FileManager.default.temporaryDirectory
 84        let first = try TestBabelRunner.execute(text, caret: 2, directory: folder)
 85        #expect(first.failure == nil)
 86        let second = try TestBabelRunner.execute(first.text, caret: 2, directory: folder)
 87        #expect(second.text == first.text)
 88        #expect(second.failure == "Cached: \"hi\"")
 89    }
 90
 91    @Test func lispHeadersAreRefused() {
 92        let text = "#+begin_src sh :cache yes :dir (concat \"/\" \"tmp\")\necho hi\n#+end_src\n"
 93        let context = EditContext(revision: 0, text: text, tree: OrgParser.parse(text), selection: [2..<2])
 94        guard case .failure(.message(let message)) = Babel.plan(context) else {
 95            Issue.record("planned")
 96            return
 97        }
 98        #expect(message.hasPrefix(":cache with Lisp header values"))
 99    }
100}
Keyboard shortcuts

Global

⌘K
Command palette & jump
/
Focus search
?
Show this help
g d
Dashboard
g e
Explore repositories
g n
Notifications

Repository & editor

t
Find file in repository
y
Copy immutable SHA permalink
g c
Code tab
g i
Issues tab
g m
Merge requests tab
g b
Builds tab
⌘↵
Submit active form