krz/orgstar

A native macOS editor for org-mode files. editor org-mode swift

Tests/OrgCoreTests/TableEditFieldTests.swift

322a87d47526d505d903e05d14e65a5464e1468f
orgstar/Tests/OrgCoreTests/TableEditFieldTests.swift history · blame · raw

56 lines · 3453 bytes

 1import Foundation
 2import Testing
 3@testable import OrgCore
 4
 5/// C-c ` (`org-table-edit-field`): what the field editor holds, and the table once it's done.
 6struct TableEditFieldTests {
 7    static let text = "Intro\n| Name | Note |\n|------+------|\n| Ann  |   a [[https://x.org][link]] here   |\n| Bob  |      |\n| Cy   | c |\nAfter\n"
 8
 9    static func offsets() -> [Int] {
10        let ns = text as NSString
11        return [ns.range(of: "Ann").location + 1, ns.range(of: "link]").location, ns.range(of: "Bob  |").location + 5,
12                ns.range(of: "| c").location + 2, ns.range(of: "Note").location]
13    }
14
15    @Test func editsTheFieldLikeOrg() throws {
16        let offsets = Self.offsets()
17        let shown = try offsets.map { offset in
18            try EmacsOracle.evaluate(Self.text, "(list (with-current-buffer (find-file-noselect \"oracle.org\") (goto-char \(EmacsOracle.point(offset, in: Self.text))) (org-table-edit-field nil) (buffer-substring-no-properties (point-min) (point-max))))").first
19        }
20        for (offset, theirs) in zip(offsets, shown) {
21            let edit = try #require(BlockEdit.field(at: offset, text: Self.text))
22            #expect("#\n# Edit field \(edit.coordinate ?? "") and finish with C-c C-c\n#\n" + edit.content == theirs)
23        }
24        let edited = "#\n# Edit field and finish with C-c C-c\n#\n  first line\n\n  second\tline  \n"
25        let cases = offsets.map { offset in
26            EmacsOracle.Case(text: Self.text, point: EmacsOracle.point(offset, in: Self.text), form: """
27                (let ((b (current-buffer)))
28                  (org-table-edit-field nil) (erase-buffer) (insert \(lispString(edited)))
29                  (cl-letf (((symbol-function 'set-window-configuration) (lambda (&rest _) (set-buffer b)))
30                            ((symbol-function 'select-window) #'ignore))
31                    (org-table-finish-edit-field))
32                  (set-buffer b))
33                """)
34        }
35        for (offset, theirs) in zip(offsets, try EmacsOracle.run(cases)) {
36            let edit = try #require(BlockEdit.field(at: offset, text: Self.text))
37            let ours = runCommand(ReplaceBlockLines(edit: edit, content: edited), Self.text, caret: 0)
38            #expect(ours.text == theirs.text, "ours:  \(ours.text.debugDescription) \(ours.failure ?? "")\nemacs: \(theirs.text.debugDescription) \(theirs.error)")
39        }
40        // Emptied, the field gets a space, as `org-table-get-field` puts one.
41        let edit = try #require(BlockEdit.field(at: offsets[0], text: Self.text))
42        let emptied = try EmacsOracle.run([EmacsOracle.Case(text: Self.text, point: EmacsOracle.point(offsets[0], in: Self.text), form: """
43            (let ((b (current-buffer)))
44              (org-table-edit-field nil) (erase-buffer)
45              (cl-letf (((symbol-function 'set-window-configuration) (lambda (&rest _) (set-buffer b)))
46                        ((symbol-function 'select-window) #'ignore))
47                (org-table-finish-edit-field))
48              (set-buffer b))
49            """)])
50        #expect(runCommand(ReplaceBlockLines(edit: edit, content: ""), Self.text, caret: 0).text == emptied[0].text)
51        // Outside a data field there's nothing to edit.
52        let hline = (Self.text as NSString).range(of: "|---").location + 2
53        #expect(BlockEdit.field(at: hline, text: Self.text) == nil)
54        #expect(BlockEdit.field(at: 2, text: Self.text) == nil)
55    }
56}