Tests/OrgCoreTests/AutoFillTests.swift

68 lines · 3684 bytes

 1import Foundation
 2import Testing
 3@testable import OrgCore
 4
 5/// Typing a space with `auto-fill-mode` on, against Emacs's `self-insert-command`.
 6struct AutoFillTests {
 7    static let texts = [
 8        "one two three four five six",
 9        "* Heading\nSome words that run well past the fill column here",
10        "- an item with several words that need wrapping",
11        "  1. numbered item text that is long enough to wrap twice over",
12        "# a comment line that goes on and on past the column",
13        "#+begin_quote\nquoted text that is long enough to need a break\n#+end_quote",
14        "#+begin_src sh\necho one two three four five six seven eight\n#+end_src",
15        "| a table row with words that never wraps at all |",
16        "* A heading that is long enough to pass the fill column",
17        "short",
18        "averyveryveryverylongwordwithoutanyspaces then",
19        "text with a [[https://example.com/a/long/link][link description]] in it",
20        "- [ ] a checkbox item whose words run past the column",
21        "  indented paragraph words that keep going past fill",
22    ]
23
24    @Test(.enabled(if: ProcessInfo.processInfo.environment["ORGSTAR_SKIP_ORACLE"] == nil))
25    func typingASpaceMatchesEmacs() throws {
26        var options = EditingOptions.org
27        options.fillColumn = 20
28        options.autoFill = true
29        var cases: [EmacsOracle.Case] = []
30        var ours: [(label: String, text: String, caret: Int)] = []
31        for text in Self.texts {
32            // At the end of each line, and in the middle of the last one.
33            let ns = text as NSString
34            var carets: [Int] = []
35            var at = 0
36            while at <= ns.length {
37                let line = ns.lineRange(for: NSRange(location: min(at, max(0, ns.length - 1)), length: 0))
38                let end = NSMaxRange(line) - (ns.substring(with: line).hasSuffix("\n") ? 1 : 0)
39                carets.append(end)
40                if NSMaxRange(line) >= ns.length { break }
41                at = NSMaxRange(line)
42            }
43            carets.append(max(0, ns.length - 6))
44            for caret in Set(carets).sorted() {
45                cases.append(EmacsOracle.Case(text: text, point: EmacsOracle.point(caret, in: text), form: """
46                    (progn (setq fill-column 20) (auto-fill-mode 1) (let ((last-command-event ?\\s)) (self-insert-command 1)))
47                    """))
48                let context = EditContext(revision: 0, text: text, tree: OrgParser.parse(text), selection: [caret..<caret], options: options)
49                guard case .commit(let result) = AutoFillSpace().run(in: context) else {
50                    ours.append(("at \(caret) in \(text.debugDescription)", "failed", caret))
51                    continue
52                }
53                let new = result.edits.sorted { $0.range.lowerBound > $1.range.lowerBound }.reduce(text) { $1.apply(to: $0) }
54                ours.append(("at \(caret) in \(text.debugDescription)", new, result.selection?.first?.lowerBound ?? -1))
55            }
56        }
57        let results = try EmacsOracle.run(cases)
58        var mismatches = 0
59        for (mine, theirs) in zip(ours, results) {
60            let theirCaret = theirs.text.unicodeScalars.prefix(theirs.point - 1).reduce(0) { $0 + $1.utf16.count }
61            if mine.text != theirs.text || mine.caret != theirCaret {
62                mismatches += 1
63                if mismatches <= 12 { Issue.record("\(mine.label)\n ours:  \(mine.text.debugDescription) @\(mine.caret)\n emacs: \(theirs.text.debugDescription) @\(theirCaret) \(theirs.error)") }
64            }
65        }
66        #expect(mismatches == 0, "\(mismatches) of \(cases.count) differ")
67    }
68}