Tests/OrgPresentationTests/DisplayTests.swift
106 lines · 5264 bytes
1import Foundation
2import OrgCore
3import Testing
4@testable import OrgPresentation
5
6struct StartupDisplayTests {
7 @Test func settingsApplyWithoutStartupWords() {
8 let tree = OrgParser.parse("* A\n:LOGBOOK:\nx\n:END:\n#+begin_src sh\necho\n#+end_src\n")
9 var options = EditingOptions.org
10 options.startupIndented = false
11 options.hideLeadingStars = true
12 options.startupWithInlineImages = true
13 options.startupAlignAllTables = true
14 options.startupTruncated = true
15 options.hideBlockStartup = true
16 let display = Presentation.startupDisplay(tree, options: options)
17 #expect(!display.indented)
18 #expect(display.hideStars)
19 #expect(display.inlineImages)
20 #expect(display.alignTables)
21 #expect(display.truncated)
22 #expect(display.wrappers.map(\.kind) == [.drawer, .block])
23 #expect(display.folds.wrapperFolds == Set(display.wrappers.map(\.start)))
24 }
25
26 @Test func startupWordsOverrideSettings() {
27 let tree = OrgParser.parse("#+STARTUP: noindent showstars noinlineimages noalign nohidedrawers overview\n* A\n:LOGBOOK:\nx\n:END:\n** B\n")
28 var options = EditingOptions.org
29 options.hideLeadingStars = true
30 options.startupWithInlineImages = true
31 options.startupAlignAllTables = true
32 let display = Presentation.startupDisplay(tree, options: options)
33 #expect(!display.indented)
34 #expect(!display.hideStars)
35 #expect(!display.inlineImages)
36 #expect(!display.alignTables)
37 #expect(display.wrappers.isEmpty)
38 #expect(display.folds.folds == [tree.text.utf16.count - "* A\n:LOGBOOK:\nx\n:END:\n** B\n".utf16.count])
39 }
40
41 @Test func startupWordsTurnSettingsOn() {
42 let tree = OrgParser.parse("#+STARTUP: indent hidestars inlineimages align hideblocks\n#+begin_quote\nq\n#+end_quote\n")
43 var options = EditingOptions.org
44 options.startupIndented = false
45 let display = Presentation.startupDisplay(tree, options: options)
46 #expect(display.indented)
47 #expect(display.hideStars)
48 #expect(display.inlineImages)
49 #expect(display.alignTables)
50 #expect(display.wrappers.map(\.kind) == [.block])
51 }
52
53 @Test func visibilityPropertiesOnlyWithAStartupVisibility() {
54 let text = "* A\n:PROPERTIES:\n:VISIBILITY: folded\n:END:\nbody\n"
55 #expect(Presentation.startupDisplay(OrgParser.parse(text), options: .org).folds.folds.isEmpty)
56 #expect(Presentation.startupDisplay(OrgParser.parse("#+STARTUP: showall\n" + text), options: .org).folds.folds.count == 1)
57 }
58}
59
60struct HiddenMarkupTests {
61 @Test func linkMarkupAlwaysEmphasisMarkersBySetting() {
62 var options = EditingOptions.org
63 #expect(Presentation.hidesWithMarkup(.linkMarkup, options: options))
64 #expect(!Presentation.hidesWithMarkup(.markup, options: options))
65 options.hideEmphasisMarkers = true
66 #expect(Presentation.hidesWithMarkup(.markup, options: options))
67 #expect(!Presentation.hidesWithMarkup(.bold, options: options))
68 }
69
70 @Test func leadingStarsShowUnlessIndentedOrHidden() {
71 #expect(Presentation.skipsLeadingStars(.leadingStars, indented: false, hideStars: false))
72 #expect(!Presentation.skipsLeadingStars(.leadingStars, indented: true, hideStars: false))
73 #expect(!Presentation.skipsLeadingStars(.leadingStars, indented: false, hideStars: true))
74 #expect(!Presentation.skipsLeadingStars(.heading(level: 1), indented: false, hideStars: false))
75 }
76
77 @Test func prettyRunsSkipTheRevealedLineCommentsAndBlocks() {
78 let text = "\\alpha one\n# \\beta\n\\gamma\nx_{1}\n" as NSString
79 let lines = [0, 11, 19, 26]
80 let all = Presentation.prettyRuns(text, in: 0..<text.length, revealedLine: nil, inBlock: { _ in false }, scriptExclusions: [], subSuperscriptsNeedBraces: true)
81 #expect(all.map(\.lineStart) == [lines[0], lines[2], lines[3]])
82 let revealed = Presentation.prettyRuns(text, in: 0..<text.length, revealedLine: lines[2], inBlock: { $0 == lines[0] }, scriptExclusions: [], subSuperscriptsNeedBraces: true)
83 #expect(revealed.map(\.lineStart) == [lines[3]])
84 let excluded = Presentation.prettyRuns(text, in: 0..<text.length, revealedLine: nil, inBlock: { _ in false }, scriptExclusions: [lines[3]..<lines[3] + 5], subSuperscriptsNeedBraces: true)
85 #expect(excluded.map(\.lineStart) == [lines[0], lines[2]])
86 }
87}
88
89struct LineIndentTests {
90 @Test(arguments: [
91 ("plain", 0), (" indented", 2), ("- item", 2), (" - [X] done", 8), ("12. step", 4),
92 ("* heading-like", 0), (" * starred item", 4), ("-not a bullet", 0), ("\t- tab", 10),
93 ])
94 func hangingColumns(line: String, expected: Int) {
95 #expect(Presentation.hangingColumns(line) == expected)
96 }
97
98 @Test func bodyLinesHangUnderTheirText() {
99 let text = "** H\n- item\nplain\n"
100 let tree = OrgParser.parse(text)
101 let indents = Presentation.lineIndents(tree, in: 0..<text.utf16.count)
102 #expect(indents.map(\.range) == [0..<5, 5..<12, 12..<18])
103 #expect(indents.map(\.firstLine) == [0, 3, 3])
104 #expect(indents.map(\.wrapped) == [3, 5, 3])
105 }
106}