Tests/OrgCoreTests/FillTests.swift

9abba9cfc504b344b7d267978a97f1c925a1d5dd
orgstar/Tests/OrgCoreTests/FillTests.swift history · blame · raw

70 lines · 4320 bytes

 1import Foundation
 2import Testing
 3@testable import OrgCore
 4
 5struct FillTests {
 6    @Test func fillsAParagraph() {
 7        let text = "one two three four five six seven eight nine ten eleven twelve thirteen fourteen fifteen\n"
 8        let filled = runCommand(FillParagraph(), text, caret: 0).text
 9        #expect(filled == "one two three four five six seven eight nine ten eleven twelve\nthirteen fourteen fifteen\n")
10    }
11
12    static let long = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris."
13    static let variants = [
14        long + "\n",
15        "Short\nlines that\nshould join.  Two spaces after.\n",
16        "- item " + long + "\n  - nested " + long + "\n",
17        "1. [ ] " + long + "\n",
18        "# comment " + long + "\n# more\n",
19        "* Heading\n" + long + "\n\nSecond " + long + "\n",
20        "  indented " + long + "\n  continued\n",
21        "> quoted " + long + "\n> more quoted text\n",
22        "Has a line break \\\\\n" + long + "\n",
23        "Date <2026-09-10 Thu 10:00> inside a long sentence that keeps going and going and going past the column.\n",
24        "A [[https://example.com/a/very/long/path][link with a description]] that runs past the fill column for sure.\n",
25        "Ends with - a dash that would start a list if it began a line and continues further on.\n",
26        "Words  with   extra    spaces\tand\ttabs that should collapse into single spaces when filled together.\n",
27        "#+CAPTION: cap\nCaptioned paragraph " + long + "\n",
28        "*bold start* of a line with " + long + "\n",
29        "Unicode ünïcödé wörds ñ " + long + "\n",
30    ]
31
32    @Test(.enabled(if: ProcessInfo.processInfo.environment["ORGSTAR_SKIP_ORACLE"] == nil))
33    func fillMatchesEmacs() throws {
34        let commands: [(command: any OrgCommand, form: String, headingLineOnly: Bool)] = [(FillParagraph(), "(org-fill-paragraph)", false)]
35        let inputs = Self.variants.map { ($0, EmacsOracle.positions($0).enumerated().filter { $0.offset % 7 == 0 || $0.element == 0 }.map(\.element)) }
36        try compareWithEmacs(inputs, commands)
37        try compareWithEmacs(inputs, commands, options: doomOptions)
38    }
39
40    /// Real paragraphs: `ORGSTAR_ORACLE_CORPUS=<folder>` fills the first 30 paragraph-like lines
41    /// of up to 60 files.
42    @Test(.enabled(if: ProcessInfo.processInfo.environment["ORGSTAR_ORACLE_CORPUS"] != nil))
43    func fillMatchesEmacsOnACorpus() throws {
44        let root = URL(fileURLWithPath: ProcessInfo.processInfo.environment["ORGSTAR_ORACLE_CORPUS"]!)
45        let files = FileManager.default.enumerator(at: root, includingPropertiesForKeys: nil)!
46            .compactMap { $0 as? URL }.filter { $0.pathExtension == "org" }.sorted { $0.path < $1.path }
47        var samples: [(String, [Int])] = []
48        for file in files where samples.count < 60 {
49            guard let text = try? String(contentsOf: file, encoding: .utf8), text.utf16.count <= 20_000, !text.contains("\\r") else { continue }
50            let ns = text as NSString
51            var carets: [Int] = []
52            var location = 0
53            var inSource = false
54            while location < ns.length, carets.count < 30 {
55                let line = ns.lineRange(for: NSRange(location: location, length: 0))
56                let content = ns.substring(with: line)
57                let lower = content.lowercased().trimmingCharacters(in: .whitespaces)
58                // M-q in a src block fills through the language's mode in Emacs; Orgstar leaves code alone.
59                if lower.hasPrefix("#+begin_src") { inSource = true }
60                if lower.hasPrefix("#+end_src") { inSource = false }
61                if !inSource, content.count > 20, !content.hasPrefix("*"), !content.hasPrefix("#+"), !content.hasPrefix("|") { carets.append(line.location + 3) }
62                location = NSMaxRange(line)
63            }
64            if !carets.isEmpty { samples.append((text, carets)) }
65        }
66        let commands: [(command: any OrgCommand, form: String, headingLineOnly: Bool)] = [(FillParagraph(), "(org-fill-paragraph)", false)]
67        try compareWithEmacs(samples, commands)
68        try compareWithEmacs(samples, commands, options: doomOptions)
69    }
70}