Tests/OrgPresentationTests/MarkdownDisplayTests.swift

801e496807e41f08001dbd99622e2fa686a34058
orgstar/Tests/OrgPresentationTests/MarkdownDisplayTests.swift

87 lines · 4491 bytes

 1import Foundation
 2import OrgCore
 3import OrgPresentation
 4import Testing
 5
 6/// Highlights `yaml` and `swift` code as one keyword run over the whole code.
 7struct WholeCodeHighlighter: CodeHighlighter {
 8    func highlights(language: String, code: String) -> [(range: Range<Int>, category: SyntaxCategory)] {
 9        ["yaml", "swift"].contains(language) ? [(0..<code.utf16.count, .keyword)] : []
10    }
11}
12
13struct MarkdownDisplayTests {
14    let text = "Intro\n# A\nbody\n## B\nmore\n```\n# not\n```\n# C\ntext\n"
15
16    @Test func outline() {
17        let document = MarkdownDocument(text)
18        let outline = document.outline
19        #expect(outline.map(\.title) == ["A", "B", "C"])
20        #expect(outline.map(\.start) == [6, 15, 39])
21        #expect(outline.map(\.end) == [39, 39, 48])
22        #expect(outline.map(\.parent) == [nil, 0, nil])
23        #expect(outline.map(\.isTopLevel) == [true, false, true])
24    }
25
26    @Test func foldsByHeading() {
27        let document = MarkdownDocument(text)
28        let tree = document.foldTree
29        #expect(tree.text.utf16.count == text.utf16.count)
30        #expect(Presentation.outline(tree).map(\.start) == document.outline.map(\.start))
31        #expect(Presentation.hiddenRanges(tree, folds: [6]) == [10..<39])
32        #expect(Presentation.hiddenRanges(tree, folds: [15]) == [20..<39])
33        // TAB on a heading: folded, children, subtree.
34        let folded = Presentation.cycle(6, in: tree, FoldState())
35        #expect(folded.folds == [6])
36        let children = Presentation.cycle(6, in: tree, folded)
37        #expect(children.folds == [15])
38        // Without the tree, the same ranges as org's for the tree.
39        for (folds, bodyFolds) in [([6], []), ([15], []), ([6, 15], []), ([], [6]), ([39], [6]), ([], [6, 15, 39]), ([6], [15])] as [(Set<Int>, Set<Int>)] {
40            #expect(Presentation.markdownHiddenRanges(document, folds: folds, bodyFolds: bodyFolds)
41                == Presentation.hiddenRanges(tree, folds: folds, bodyFolds: bodyFolds), "\(folds) \(bodyFolds)")
42        }
43        // The `#` line in a fence isn't a heading.
44        #expect(Presentation.heading(containing: 31, in: tree) == nil)
45        #expect(Presentation.heading(containing: 40, in: tree) == 39)
46    }
47
48    func roles(_ text: String, highlighter: (any CodeHighlighter)? = nil) -> [String] {
49        let document = MarkdownDocument(text)
50        let ns = text as NSString
51        return Presentation.markdownStyles(document, in: 0..<ns.length, highlighter: highlighter).map {
52            "\($0.role) " + ns.substring(with: NSRange($0.range)).replacingOccurrences(of: "\n", with: "⏎")
53        }
54    }
55
56    @Test func styles() {
57        #expect(roles("---\nk: v\n---\n# H *e*\n```swift\nlet\n```\n> q\n", highlighter: WholeCodeHighlighter()) == [
58            "blockDelimiter ---⏎", "metadata k: v⏎", "syntax(OrgPresentation.SyntaxCategory.keyword) k: v⏎", "blockDelimiter ---⏎",
59            "blockDelimiter ```swift⏎", "block let⏎", "syntax(OrgPresentation.SyntaxCategory.keyword) let⏎", "blockDelimiter ```⏎",
60            "heading(level: 1) # H *e*", "italic *e*", "markup *", "markup *",
61            "markup >", "italic q",
62        ])
63    }
64
65    @Test func stylesOnlyTheLinesAsked() {
66        let text = "# A\n**b**\n```\n# c\n"
67        let document = MarkdownDocument(text)
68        let runs = Presentation.markdownStyles(document, in: 5..<6)
69        #expect(runs.map(\.role) == [.bold, .markup, .markup])
70        // Inside an unclosed fence, nothing is a heading.
71        #expect(Presentation.markdownStyles(document, in: 14..<17).map(\.role) == [.blockDelimiter, .block])
72    }
73
74    @Test func restyleRangeFollowsFences() {
75        let old = "a\n```\ncode\n```\nb\n# h\n"
76        let oldBlocks = MarkdownDocument(old).structure.blocks
77        // Deleting the closing fence opens the block to the end of the text.
78        let edit = TextEdit(range: 11..<15, replacement: "")
79        let new = "a\n```\ncode\nb\n# h\n"
80        let range = Presentation.markdownRestyleRange(for: edit, oldBlocks: oldBlocks, new: MarkdownDocument(new), newText: new as NSString)
81        #expect(range == 2..<(new as NSString).length)
82        // Typing inside a paragraph restyles its line.
83        let typed = TextEdit(range: 1..<1, replacement: "x")
84        let after = "ax\n```\ncode\n```\nb\n# h\n"
85        #expect(Presentation.markdownRestyleRange(for: typed, oldBlocks: oldBlocks, new: MarkdownDocument(after), newText: after as NSString) == 0..<3)
86    }
87}
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