Tests/OrgPresentationTests/ShrunkColumnsTests.swift
66 lines · 3304 bytes
1import Foundation
2import OrgCore
3import Testing
4@testable import OrgPresentation
5
6struct ShrunkColumnsTests {
7 static let table = "| <3> | b |\n|------+---|\n| abcdef | x |\n"
8
9 @Test func startupShrinksCookieColumnsOnlyWithTheWord() {
10 let plain = OrgParser.parse("intro\n" + Self.table)
11 #expect(ShrunkColumns.startup(plain).isEmpty)
12 let tree = OrgParser.parse("#+STARTUP: shrink\n" + Self.table + "\n| a | b |\n")
13 let shrunk = ShrunkColumns.startup(tree)
14 let start = ("#+STARTUP: shrink\n" as NSString).length
15 #expect(Array(shrunk.tables.keys) == [start])
16 #expect(shrunk.columns(in: start..<start + 1) == [1])
17 #expect(!shrunk.segments.isEmpty)
18 }
19
20 @Test func togglesTheColumnAtTheCaret() throws {
21 let text = Self.table
22 var shrunk = ShrunkColumns()
23 let caret = (text as NSString).range(of: "abc").location
24 guard case .set(let columns, let table) = shrunk.change(for: "org.table.toggle-column-width", at: caret, in: text) else {
25 Issue.record("expected a column")
26 return
27 }
28 #expect(columns == [1])
29 shrunk.set(columns, in: table, text: text)
30 #expect(shrunk.change(for: "org.table.toggle-column-width", at: caret, in: text) == .set([], table: table))
31 #expect(shrunk.change(for: "org.table.expand", at: caret, in: text) == .set([], table: table))
32 #expect(shrunk.change(for: "org.table.shrink", at: caret, in: text) == .set([1], table: table))
33 #expect(shrunk.change(for: "org.table.shrink", at: (text as NSString).length, in: text + "after\n") == .notInTable)
34 guard case .ask(_, let current, _) = shrunk.change(for: "org.table.toggle-column-width", at: 0, in: text) else {
35 Issue.record("expected a prompt before the first column")
36 return
37 }
38 #expect(ShrunkColumns.toggled(current, TableShrink.columns("1-2", max: 4)) == [2])
39 }
40
41 @Test func typingInAColumnWidensItButCommandsKeepIt() throws {
42 let text = Self.table
43 let table = try #require(TableShrink.table(at: 0, in: text))
44 var shrunk = ShrunkColumns()
45 shrunk.set([1], in: table, text: text)
46 let at = (text as NSString).range(of: "abc").location
47 let edit = TextEdit(range: at..<at, replacement: "z")
48 let after = (text as NSString).replacingCharacters(in: NSRange(location: at, length: 0), with: "z")
49 var byCommand = shrunk
50 #expect(byCommand.map(through: edit, in: after, command: true).count == 1)
51 #expect(byCommand.columns(in: table) == [1])
52 #expect(shrunk.map(through: edit, in: after, command: false).count == 1)
53 #expect(shrunk.isEmpty)
54 }
55
56 @Test func editsBeforeATableMoveIt() throws {
57 let text = "x\n" + Self.table
58 let table = try #require(TableShrink.table(at: 2, in: text))
59 var shrunk = ShrunkColumns()
60 shrunk.set([1], in: table, text: text)
61 let after = "xyz\n" + Self.table
62 #expect(shrunk.map(through: TextEdit(range: 1..<1, replacement: "yz"), in: after, command: false).isEmpty)
63 #expect(Array(shrunk.tables.keys) == [4])
64 #expect(shrunk.segments == TableShrink.segments(4..<4 + (Self.table as NSString).length, column: 1, in: after))
65 }
66}