Tests/OrgPresentationTests/PresentationTests.swift

359666677e3403c6c38599c91ff884b7aa3787cd
orgstar/Tests/OrgPresentationTests/PresentationTests.swift history · blame · raw

122 lines · 5214 bytes

  1import OrgCore
  2import Testing
  3@testable import OrgPresentation
  4
  5func roles(_ text: String) -> [String] {
  6    let tree = OrgParser.parse(text)
  7    let utf16 = Array(text.utf16)
  8    return Presentation.styles(tree, in: 0..<utf16.count).map { run in
  9        "\(run.role):\(String(decoding: utf16[run.range], as: UTF16.self))"
 10    }
 11}
 12
 13struct StyleTests {
 14    @Test func headingParts() {
 15        #expect(roles("** DONE [#A] Ship *it* :work:\n") == [
 16            "heading(level: 2):** DONE [#A] Ship *it* :work:\n",
 17            "leadingStars:*", "done:DONE", "priority:[#A]", "tags::work:",
 18            "bold:*it*", "markup:*", "markup:*",
 19        ])
 20    }
 21
 22    @Test func describedLinksDimTheirTarget() {
 23        #expect(roles("[[https://a.b][site]]\n") == [
 24            "link:[[https://a.b][site]]", "linkMarkup:[[", "linkMarkup:https://a.b", "linkMarkup:][", "linkMarkup:]]",
 25        ])
 26        #expect(roles("[[https://a.b]]\n") == ["link:[[https://a.b]]", "linkMarkup:[[", "linkMarkup:]]"])
 27    }
 28
 29    @Test func blocksAndMetadata() {
 30        #expect(roles("#+begin_src sh\necho\n#+end_src\n") == [
 31            "block:#+begin_src sh\necho\n#+end_src\n", "blockDelimiter:#+begin_src sh", "blockDelimiter:#+end_src",
 32        ])
 33        #expect(roles("* a\nSCHEDULED: <2026-10-04 Sun>\n").contains("metadata:SCHEDULED: <2026-10-04 Sun>\n"))
 34    }
 35
 36    @Test func onlyRunsOverlappingTheRange() {
 37        let text = "* a\nfirst *b*\n* c\nsecond *d*\n"
 38        let tree = OrgParser.parse(text)
 39        let runs = Presentation.styles(tree, in: 17..<28)
 40        #expect(!runs.contains { $0.range == 10..<13 })
 41        #expect(runs.contains { $0.range == 25..<28 && $0.role == .bold })
 42    }
 43}
 44
 45struct IndentTests {
 46    @Test func bodiesIndentPastTheirStars() {
 47        let text = "pre\n* a\nbody\n** b\nmore\n"
 48        let runs = Presentation.indents(OrgParser.parse(text), in: 0..<text.utf16.count)
 49        #expect(runs == [
 50            IndentRun(range: 4..<8, firstLine: 0, wrapped: 2),
 51            IndentRun(range: 8..<13, firstLine: 2, wrapped: 2),
 52            IndentRun(range: 13..<18, firstLine: 0, wrapped: 3),
 53            IndentRun(range: 18..<23, firstLine: 3, wrapped: 3),
 54        ])
 55    }
 56}
 57
 58struct FoldTests {
 59    let text = "* a\nbody\n** b\nmore\n* c\ntext\n"
 60
 61    @Test func hiddenRanges() {
 62        let tree = OrgParser.parse(text)
 63        #expect(Presentation.hiddenRanges(tree, folds: [0]) == [4..<19])
 64        #expect(Presentation.hiddenRanges(tree, folds: [9]) == [14..<19])
 65        #expect(Presentation.hiddenRanges(tree, folds: [0, 9]) == [4..<19])
 66        #expect(Presentation.hiddenRanges(tree, folds: [19]) == [23..<28])
 67        #expect(Presentation.hiddenRanges(OrgParser.parse("* a"), folds: [0]).isEmpty)
 68    }
 69
 70    @Test func headingContaining() {
 71        let tree = OrgParser.parse(text)
 72        #expect(Presentation.heading(containing: 2, in: tree) == 0)
 73        #expect(Presentation.heading(containing: 3, in: tree) == 0)
 74        #expect(Presentation.heading(containing: 5, in: tree) == nil)
 75        #expect(Presentation.heading(containing: 10, in: tree) == 9)
 76        #expect(Presentation.headings(tree) == [0, 9, 19])
 77    }
 78}
 79
 80struct CyclingTests {
 81    let text = "* a\nbody\n** b\nmore\n*** c\ndeep\n* d\n"
 82    var tree: OrgTree { OrgParser.parse(text) }
 83
 84    @Test func outline() {
 85        let entries = Presentation.outline(tree)
 86        #expect(entries.map(\.title) == ["a", "b", "c", "d"])
 87        #expect(entries.map(\.parent) == [nil, 0, 1, nil])
 88        #expect(entries.map(\.isTopLevel) == [true, false, false, true])
 89    }
 90
 91    @Test func localCycle() {
 92        var state = FoldState()
 93        state = Presentation.cycle(0, in: tree, state)
 94        #expect(state == FoldState(folds: [0]))
 95        state = Presentation.cycle(0, in: tree, state)
 96        #expect(state == FoldState(folds: [9]))
 97        state = Presentation.cycle(0, in: tree, state)
 98        #expect(state == FoldState())
 99        #expect(Presentation.cycle(30, in: tree, FoldState(folds: [30])) == FoldState())
100    }
101
102    @Test func globalStates() {
103        #expect(Presentation.foldState(for: .overview, in: tree) == FoldState(folds: [0, 30]))
104        #expect(Presentation.foldState(for: .contents, in: tree) == FoldState(bodyFolds: [0, 9, 19, 30]))
105        #expect(Presentation.cycleGlobal(from: nil, in: tree).0 == .overview)
106        #expect(Presentation.cycleGlobal(from: .overview, in: tree).0 == .contents)
107        #expect(Presentation.cycleGlobal(from: .contents, in: tree).0 == .showAll)
108    }
109
110    @Test func bodyFoldsKeepChildHeadings() {
111        #expect(Presentation.hiddenRanges(tree, folds: [], bodyFolds: [0, 9, 19, 30]) == [4..<9, 14..<19, 25..<30])
112    }
113
114    @Test func startup() {
115        #expect(Presentation.startupFoldState(OrgParser.parse("#+STARTUP: content\n" + text)) != nil)
116        #expect(Presentation.startupFoldState(OrgParser.parse(text)) == nil)
117        let levels = OrgParser.parse("#+startup: show2levels\n" + text)
118        let state = Presentation.startupFoldState(levels)
119        #expect(state?.folds.count == 1 && state?.bodyFolds.count == 2)
120        #expect(Presentation.startupOptions(OrgParser.parse("#+begin_example\n#+STARTUP: overview\n#+end_example\n")).isEmpty)
121    }
122}