Tests/OrgPresentationTests/PresentationTests.swift

7af09a499f682c82ef5bbbdf1b0a5d35343dbc3f
orgstar/Tests/OrgPresentationTests/PresentationTests.swift

78 lines · 3152 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}
Keyboard shortcuts

Global

⌘K
Command palette & jump
/
Focus search
?
Show this help
g d
Dashboard
g e
Explore repositories
g n
Notifications

Repository & editor

t
Find file in repository
y
Copy immutable SHA permalink
g c
Code tab
g i
Issues tab
g m
Merge requests tab
g b
Builds tab
⌘↵
Submit active form