Tests/OrgCoreTests/TableElEditTests.swift
99 lines · 4642 bytes
1import Foundation
2import Testing
3@testable import OrgCore
4
5struct TableElEditTests {
6 static let simple = """
7 * Notes
8 +-----+-----+
9 | a | b |
10 | | b2 |
11 +-----+-----+
12 | c | d |
13 +-----+-----+
14
15 """
16
17 static let merged = """
18 +-----+-----+-----+
19 | a | b |
20 +-----+-----+-----+
21 | c | d | e |
22 | +-----+-----+
23 | | f | g |
24 +-----+-----+-----+
25
26 """
27
28 /// Carets inside cells: every character after a bar on a row line, before the next bar.
29 static func cellCarets(_ text: String) -> [Int] {
30 var carets: [Int] = []
31 let ns = text as NSString
32 var offset = 0
33 for line in text.components(separatedBy: "\n") {
34 let chars = Array(line)
35 var inside = false
36 var utf16 = offset
37 for ch in chars {
38 if ch == "|" || ch == "+" { inside = line.trimmingCharacters(in: .whitespaces).hasPrefix("|") }
39 else if inside, ch != "-" { carets.append(utf16) }
40 utf16 += String(ch).utf16.count
41 }
42 offset += (line as NSString).length + 1
43 if offset > ns.length { break }
44 }
45 return carets
46 }
47
48 @Test(.enabled(if: ProcessInfo.processInfo.environment["ORGSTAR_SKIP_ORACLE"] == nil))
49 func movingMatchesTableEl() throws {
50 var cases: [EmacsOracle.Case] = []
51 var ours: [(label: String, caret: Int?)] = []
52 for text in [Self.simple, Self.merged] {
53 for caret in Self.cellCarets(text) {
54 for forward in [true, false] {
55 cases.append(EmacsOracle.Case(text: text, point: EmacsOracle.point(caret, in: text), form: """
56 (progn (require 'table) (table-recognize-table) (\(forward ? "table-forward-cell" : "table-backward-cell") 1))
57 """))
58 let context = EditContext(revision: 0, text: text, tree: OrgParser.parse(text), selection: [caret..<caret])
59 var mine: Int?
60 if case .commit(let result) = TableElMoveCell(forward: forward).run(in: context), result.edits.isEmpty { mine = result.selection?.first?.lowerBound }
61 ours.append(("\(forward ? "forward" : "backward") from \(caret)", mine))
62 }
63 }
64 }
65 let results = try EmacsOracle.run(cases)
66 var mismatches = 0
67 for ((label, mine), theirs) in zip(ours, results) {
68 let theirCaret = theirs.text.unicodeScalars.prefix(theirs.point - 1).reduce(0) { $0 + $1.utf16.count }
69 if mine != theirCaret || !theirs.error.isEmpty {
70 mismatches += 1
71 if mismatches <= 10 { Issue.record("\(label): ours \(mine.map(String.init) ?? "none") emacs \(theirCaret) \(theirs.error)") }
72 }
73 }
74 #expect(mismatches == 0, "\(mismatches) of \(cases.count) differ")
75 }
76
77 @Test func aligningAfterTyping() {
78 let typed = " +-----+-----+\n | abcXY | d |\n +-----+-----+\n | e| f |\n +-----+-----+\n"
79 let caret = (typed as NSString).range(of: "XY").location + 2
80 let context = EditContext(revision: 0, text: typed, tree: OrgParser.parse(typed), selection: [caret..<caret])
81 guard case .commit(let result) = TableElAlign().run(in: context) else { Issue.record("not aligned"); return }
82 let new = result.edits.reduce(typed) { $1.apply(to: $0) }
83 #expect(new == " +------+-----+\n | abcXY| d |\n +------+-----+\n | e | f |\n +------+-----+\n")
84 // The caret stays after the typed text.
85 #expect(result.selection?.first?.lowerBound == (new as NSString).range(of: "XY").location + 2)
86 // TAB lines the borders up first, then goes on to the next cell.
87 guard case .commit(let tab) = TableElMoveCell(forward: true).run(in: context) else { Issue.record("no move"); return }
88 let tabbed = tab.edits.reduce(typed) { $1.apply(to: $0) }
89 #expect(tabbed == new)
90 #expect(tab.selection?.first?.lowerBound == (new as NSString).range(of: " d").location)
91 }
92
93 @Test func mergedCellsArentAligned() {
94 let caret = (Self.merged as NSString).range(of: "d").location
95 let context = EditContext(revision: 0, text: Self.merged, tree: OrgParser.parse(Self.merged), selection: [caret..<caret])
96 #expect(TableElAlign().run(in: context) == .failed("Can't align a table.el table with merged cells"))
97 #expect(KeyContext.holds("table-el", in: context))
98 }
99}