Tests/OrgCoreTests/ElementCommandTests.swift

102 lines · 5863 bytes

6 symbols in this file
  1import Foundation
  2import Testing
  3@testable import OrgCore
  4
  5/// `org-drag-element-*`, `org-insert-drawer` and `org-cycle-list-bullet` against Org.
  6struct ElementCommandTests {
  7    static let oracle = ProcessInfo.processInfo.environment["ORGSTAR_SKIP_ORACLE"] == nil
  8
  9    /// Texts with `` where the caret is.
 10    static let drags = [
 11        "* H\nFirst ¶para\nstill first\n\nSecond para\n\nThird\n",
 12        "* H\nFirst para\n\nSecond ¶para\n\nThird\n",
 13        "* H\nFirst para\n\n\nSecond para\n\nThird\n",
 14        "* H\n#+NAME: x\n#+begin_src sh\necho ¶hi\n#+end_src\n\nAfter\n",
 15        "* H\nBefore\n\n#+NAME: x\n#+begin_src sh\necho ¶hi\n#+end_src\n",
 16        "* H\nOnly ¶para\n** Child\nbody\n",
 17        "* H\nLast ¶para\n* Next\n",
 18        "Pre¶amble\n\nmore\n* H\n",
 19        "* H\n#+begin_quote\nin ¶quote\n\nsecond in quote\n#+end_quote\n",
 20        "* H\n:NOTES:\none\n\n¶two\n:END:\nafter\n",
 21        "* H\nA\nB¶",
 22    ]
 23
 24    static func place(_ marked: String) -> (text: String, caret: Int) {
 25        let ns = marked as NSString
 26        let at = ns.range(of: "").location
 27        return (ns.replacingCharacters(in: NSRange(location: at, length: 1), with: ""), at)
 28    }
 29
 30    static func ours(_ command: any OrgCommand, _ text: String, _ caret: Int, answers: [String: String] = [:], selection: Range<Int>? = nil) -> (text: String, caret: Int, failure: String?) {
 31        let context = EditContext(revision: 0, text: text, tree: OrgParser.parse(text), selection: [selection ?? caret..<caret], answers: answers)
 32        switch command.run(in: context) {
 33        case .commit(let result):
 34            let new = result.edits.sorted { $0.range.lowerBound > $1.range.lowerBound }.reduce(text) { $1.apply(to: $0) }
 35            return (new, result.selection?.first?.lowerBound ?? caret, nil)
 36        case .failed(let message): return (text, caret, message)
 37        default: return (text, caret, "other")
 38        }
 39    }
 40
 41    @Test(.enabled(if: oracle))
 42    func dragsLikeOrg() throws {
 43        var cases: [EmacsOracle.Case] = []
 44        var ours: [(String, Int, String?)] = []
 45        for marked in Self.drags {
 46            let (text, caret) = Self.place(marked)
 47            for forward in [true, false] {
 48                cases.append(EmacsOracle.Case(text: text, point: EmacsOracle.point(caret, in: text), form: forward ? "(org-drag-element-forward)" : "(org-drag-element-backward)"))
 49                ours.append(Self.ours(DragElement(forward: forward), text, caret))
 50            }
 51        }
 52        for (i, (mine, theirs)) in zip(ours, try EmacsOracle.run(cases)).enumerated() {
 53            let failed = !theirs.error.isEmpty
 54            let label = "\(cases[i].form) on \(cases[i].text.debugDescription)"
 55            if failed {
 56                #expect(mine.2 != nil && mine.0 == cases[i].text, "\(label): emacs \(theirs.error), ours \(mine.0.debugDescription)")
 57            } else {
 58                #expect(mine.0 == theirs.text && mine.1 == EmacsOracle.offset(theirs.point, in: theirs.text),
 59                        "\(label)\nours:  \(mine.0.debugDescription) @\(mine.1) \(mine.2 ?? "")\nemacs: \(theirs.text.debugDescription) @\(EmacsOracle.offset(theirs.point, in: theirs.text))")
 60            }
 61        }
 62    }
 63
 64    @Test(.enabled(if: oracle))
 65    func insertsDrawersLikeOrg() throws {
 66        let texts: [(String, Range<Int>?)] = [
 67            ("* H\nbody¶\n", nil), ("* H\n\n", nil), ("* H\nbo¶dy\n", nil),
 68            ("* H\n¶one\ntwo\n\nafter\n", 4..<12), ("* H\n  ¶indented\n  more\n", 4..<22),
 69        ]
 70        var cases: [EmacsOracle.Case] = []
 71        var ours: [(String, Int, String?)] = []
 72        for (marked, region) in texts {
 73            let (text, caret) = Self.place(marked)
 74            let form = region.map { "(progn (transient-mark-mode 1) (set-mark \(EmacsOracle.point($0.lowerBound, in: text))) (goto-char \(EmacsOracle.point($0.upperBound, in: text))) (activate-mark) (org-insert-drawer nil \"NOTES\"))" }
 75                ?? "(org-insert-drawer nil \"NOTES\")"
 76            cases.append(EmacsOracle.Case(text: text, point: EmacsOracle.point(region?.upperBound ?? caret, in: text), form: form))
 77            ours.append(Self.ours(InsertDrawer(), text, region?.upperBound ?? caret, answers: ["drawer": "NOTES"], selection: region))
 78        }
 79        for (i, (mine, theirs)) in zip(ours, try EmacsOracle.run(cases)).enumerated() {
 80            #expect(mine.0 == theirs.text && mine.1 == EmacsOracle.offset(theirs.point, in: theirs.text),
 81                    "\(cases[i].form) on \(cases[i].text.debugDescription)\nours:  \(mine.0.debugDescription) @\(mine.1) \(mine.2 ?? "")\nemacs: \(theirs.text.debugDescription) @\(EmacsOracle.offset(theirs.point, in: theirs.text)) \(theirs.error)")
 82        }
 83        #expect(Self.ours(InsertDrawer(), "x\n", 0, answers: ["drawer": "bad name"]).failure == "Invalid drawer name")
 84    }
 85
 86    @Test(.enabled(if: oracle))
 87    func cyclesBulletsBothWaysLikeOrg() throws {
 88        let texts = ["- a¶\n- b\n", "+ a¶\n+ b\n", "1. a¶\n2. b\n", "1) a¶\n", "  * a¶\n  * b\n"]
 89        var cases: [EmacsOracle.Case] = []
 90        var ours: [(String, Int, String?)] = []
 91        for marked in texts {
 92            let (text, caret) = Self.place(marked)
 93            for previous in [false, true] {
 94                cases.append(EmacsOracle.Case(text: text, point: EmacsOracle.point(caret, in: text), form: previous ? "(org-cycle-list-bullet 'previous)" : "(org-cycle-list-bullet)"))
 95                ours.append(Self.ours(CycleBullet(previous: previous), text, caret))
 96            }
 97        }
 98        for (i, (mine, theirs)) in zip(ours, try EmacsOracle.run(cases)).enumerated() {
 99            #expect(mine.0 == theirs.text, "\(cases[i].form) on \(cases[i].text.debugDescription)\nours:  \(mine.0.debugDescription)\nemacs: \(theirs.text.debugDescription) \(theirs.error)")
100        }
101    }
102}