krz/orgstar

A native macOS editor for org-mode files. editor org-mode swift

Tests/OrgCoreTests/TableShrinkTests.swift

b98a6509c5ae75e5172dd333d1b6105fd6ceee0e
orgstar/Tests/OrgCoreTests/TableShrinkTests.swift history · blame · raw

33 lines · 1922 bytes

2 symbols in this file
 1import Foundation
 2import Testing
 3@testable import OrgCore
 4
 5struct TableShrinkTests {
 6    static let oracle = ProcessInfo.processInfo.environment["ORGSTAR_SKIP_ORACLE"] == nil
 7
 8    @Test(.enabled(if: oracle))
 9    func placesOverlaysLikeOrg() throws {
10        let tables = [
11            "| <5>   | <r3> | x      | <c6>     |\n|--------+------+--------+----------|\n| abcdefghij | ab | long text | mid |\n|  | abcd | y | abcdefghi |\n|   lead |    z |        |        q |\n",
12            "| a | b |\n|---+---|\n| hello world | 1 |\n",
13        ]
14        for text in tables {
15            let table = try #require(TableShrink.table(at: 2, in: text))
16            let columns = TableShrink.cookieColumns(table, in: text)
17            var ours: [String] = []
18            for c in columns.isEmpty ? [1, 2] : columns {
19                ours += TableShrink.segments(table, column: c, in: text).map { "\($0.range.lowerBound + 1) \($0.range.upperBound + 1) \($0.display)" }
20            }
21            let shrink = columns.isEmpty ? "(org-table--shrink-columns '(1 2) (org-table-begin) (org-table-end))" : "(org-table-shrink)"
22            let emacs = try EmacsOracle.evaluate(text, """
23                (with-current-buffer (find-file-noselect "oracle.org") (goto-char 3) \(shrink)
24                  (mapcar (lambda (o) (format "%d %d %s" (overlay-start o) (overlay-end o) (substring-no-properties (overlay-get o 'display))))
25                          (sort (seq-filter (lambda (o) (eq (overlay-get o 'org-overlay-type) 'table-column-hide)) (overlays-in (point-min) (point-max)))
26                                (lambda (a b) (< (overlay-start a) (overlay-start b))))))
27                """)
28            #expect(emacs.count > 4)
29            #expect(ours.sorted { Int($0.split(separator: " ")[0])! < Int($1.split(separator: " ")[0])! } == emacs)
30        }
31        #expect(TableShrink.columns("2-4 6- 1", max: 7) == [1, 2, 3, 4, 6, 7])
32    }
33}