Tests/OrgAppTests/ProgramRunningTests.swift
39 lines · 1789 bytes
1import Foundation
2import OrgCore
3import OrgDocument
4import Testing
5@testable import OrgApp
6
7/// Answers every block with a fixed output and every table with fixed text.
8private struct StubPrograms: ProgramRunning {
9 func runBabel(_ job: BabelJob, fileDirectory: URL) async throws -> BabelOutput {
10 BabelOutput(stdout: "stub")
11 }
12
13 func recalculateTable(text: String, directory: URL, table: Range<Int>, point: Int, form: String) async throws -> String {
14 "| 1 |\n"
15 }
16}
17
18/// The app creates its session before it sets `DocumentSession.programs`, so the runner is
19/// read when a block or table runs, not when the session is made.
20@MainActor
21struct ProgramRunningTests {
22 @Test func aRunnerSetAfterTheSessionOpensIsUsed() async throws {
23 #expect(DocumentSession.runner == nil)
24 let notes = try Folder()
25 let file = try notes.write("b.org", "#+begin_src sh\necho hi\n#+end_src\n\n| 2 |\n")
26 let session = DocumentSession(recovery: FileRecoveryStore(directory: notes.url.appendingPathComponent(".r")))
27 session.open(file.path)
28 guard case .external(.babel(let job)) = session.perform(ExecuteSrcBlock(), path: file.path, at: 3, expect: nil) else {
29 Issue.record("not planned")
30 return
31 }
32 let table = ("#+begin_src sh\necho hi\n#+end_src\n\n" as NSString).length
33 await DocumentSession.$scopedPrograms.withValue(StubPrograms()) {
34 #expect(await session.recalculateInEmacs(table: table..<(table + 6), point: table, form: "") == "Recalculated in Emacs")
35 #expect(await session.runBabel(job) == "Code block evaluation complete.")
36 }
37 #expect(session.document?.text == "#+begin_src sh\necho hi\n#+end_src\n\n#+RESULTS:\n: stub\n\n| 1 |\n")
38 }
39}