Tests/OrgCoreTests/EmphasizeTests.swift

67 lines · 3606 bytes

5 symbols in this file
 1import Foundation
 2import Testing
 3@testable import OrgCore
 4
 5struct EmphasizeTests {
 6    static let oracle = ProcessInfo.processInfo.environment["ORGSTAR_SKIP_ORACLE"] == nil
 7
 8    func run(_ text: String, _ selection: Range<Int>, _ marker: String) -> (text: String, caret: Int, failure: String?) {
 9        let context = EditContext(revision: 0, text: text, tree: OrgParser.parse(text), selection: [selection], answers: ["emphasis": marker])
10        switch Emphasize().run(in: context) {
11        case .commit(let result):
12            var new = text
13            for edit in result.edits.sorted(by: { $0.range.lowerBound > $1.range.lowerBound }) { new = edit.apply(to: new) }
14            return (new, result.selection?.first?.lowerBound ?? selection.lowerBound, nil)
15        case .failed(let message): return (text, selection.lowerBound, message)
16        default: return (text, selection.lowerBound, "unexpected")
17        }
18    }
19
20    @Test(.enabled(if: oracle))
21    func emphasizesLikeOrg() throws {
22        let cases: [(String, Range<Int>)] = [
23            ("Hello world\n", 6..<11), ("Hello world\n", 5..<5), ("Hello world\n", 6..<6), ("Hello world\n", 0..<0),
24            ("Hello *world* again\n", 6..<13), ("Hello world\n", 0..<5), ("say (it) now\n", 5..<7), ("abc", 3..<3),
25            ("one-two\n", 4..<7), ("a**b\n", 1..<3),
26        ]
27        let markers = ["*", "/", "_", "=", "~", "+", " ", "q"]
28        var oracleCases: [EmacsOracle.Case] = []
29        var ours: [(text: String, caret: Int, failure: String?)] = []
30        for (text, selection) in cases {
31            for marker in markers {
32                let char = marker == " " ? "?\\s" : "?\(marker)"
33                let form = selection.isEmpty
34                    ? "(org-emphasize \(char))"
35                    : "(let ((transient-mark-mode t)) (push-mark \(selection.lowerBound + 1) t t) (org-emphasize \(char)))"
36                oracleCases.append(EmacsOracle.Case(text: text, point: selection.upperBound + 1, form: form))
37                ours.append(run(text, selection, marker))
38            }
39        }
40        for (i, theirs) in try EmacsOracle.run(oracleCases).enumerated() {
41            let label = "\(oracleCases[i].form) on \(oracleCases[i].text.debugDescription)"
42            #expect((ours[i].failure != nil) == !theirs.error.isEmpty, "\(label): \(ours[i].failure ?? "") vs \(theirs.error)")
43            guard theirs.error.isEmpty else { continue }
44            #expect(ours[i].text == theirs.text, "\(label)")
45            #expect(ours[i].caret == theirs.point - 1, "\(label): caret")
46        }
47    }
48
49    @Test func asksForTheMarker() {
50        let text = "word\n"
51        let context = EditContext(revision: 0, text: text, tree: OrgParser.parse(text), selection: [0..<4])
52        guard case .prompt(let prompt) = Emphasize().run(in: context) else { Issue.record("no prompt"); return }
53        #expect(prompt.key == "emphasis")
54        #expect(prompt.fast?.items.count == 7)
55    }
56
57    @Test func fixedMarkers() {
58        let text = "word\n"
59        let context = EditContext(revision: 0, text: text, tree: OrgParser.parse(text), selection: [0..<4])
60        guard case .commit(let result) = Emphasize(marker: "*").run(in: context) else { Issue.record("no edit"); return }
61        #expect(result.edits.first.map { $0.apply(to: text) } == "*word*\n")
62        #expect(Emphasize(marker: "*").id == "org.emphasize.bold")
63        #expect(Emphasize(marker: "/").id == "org.emphasize.italic")
64        #expect(Emphasize(marker: "_").id == "org.emphasize.underline")
65        #expect(Emphasize(marker: "~").id == "org.emphasize.code")
66    }
67}