Tests/OrgPresentationTests/ReadingTextTests.swift
65 lines · 3629 bytes
1import Foundation
2import Testing
3import OrgCore
4@testable import OrgPresentation
5
6struct ReadingTextTests {
7 @Test func hidesLinkMarkupAndFoldedDrawers() {
8 let text = "* TODO H :t:\n:PROPERTIES:\n:ID: x\n:END:\nSee [[https://a.org][the site]] and *b* [[c.org]].\n"
9 let tree = OrgParser.parse(text)
10 let folded = Presentation.startupWrappers(tree).map(\.hidden)
11 let segments = ReadingText.segments(tree, in: 0..<(text as NSString).length, folded: folded)
12 #expect(segments.map(\.text).joined() == "* TODO H :t:\n:PROPERTIES:…\nSee the site and *b* c.org.\n")
13 // The drawer's first line and its `…` name the hidden range they open.
14 #expect(segments.filter { $0.toggles != nil }.map(\.text).joined() == ":PROPERTIES:…")
15 #expect(segments.first { $0.text == "…" }?.toggles == folded.first?.lowerBound)
16 // Opened, its first line folds it again.
17 let opened = ReadingText.segments(tree, in: 0..<(text as NSString).length, open: folded)
18 #expect(opened.filter { $0.toggles != nil }.map(\.text).joined() == ":PROPERTIES:")
19 #expect(opened.first { $0.toggles != nil }?.toggles == folded.first?.lowerBound)
20 #expect(!opened.contains { $0.text.contains("…") })
21 let site = segments.first { $0.text == "the site" }
22 #expect(site?.link?.raw == "https://a.org" && site?.roles.contains(.link) == true)
23 #expect(segments.first { $0.text == "TODO" }?.roles.contains(.todo) == true)
24 #expect(segments.contains { $0.text == "b" && $0.roles.contains(.bold) })
25 #expect(segments.first { $0.text.hasPrefix("c.org") }?.link?.path == "c.org")
26 // Offsets point into the file.
27 #expect(site.map { (text as NSString).substring(with: NSRange(location: $0.offset, length: 8)) } == "the site")
28 }
29
30 static func shown(_ text: String, _ display: ReadingText.Display) -> [ReadingText.Segment] {
31 ReadingText.segments(OrgParser.parse(text), in: 0..<(text as NSString).length, display: display)
32 }
33
34 @Test func showMarkupShowsLinkBracketsAndEmphasisMarkers() {
35 var options = EditingOptions.org
36 options.hideEmphasisMarkers = true
37 let text = "a *b* [[c.org][d]]\n"
38 #expect(Self.shown(text, .init(options: options)).map(\.text).joined() == "a b d\n")
39 #expect(Self.shown(text, .init(showMarkup: false)).map(\.text).joined() == "a *b* d\n")
40 #expect(Self.shown(text, .init(showMarkup: true, options: options)).map(\.text).joined() == text)
41 }
42
43 @Test func prettyEntitiesShowTheirCharactersAndRaiseScripts() {
44 var options = EditingOptions.org
45 options.prettyEntities = true
46 options.subSuperscriptsNeedBraces = true
47 let text = "\\alpha and x^{2}\n#+begin_example\n\\beta\n#+end_example\n"
48 let segments = Self.shown(text, .init(options: options))
49 #expect(segments.map(\.text).joined() == "α and x2\n#+begin_example\n\\beta\n#+end_example\n")
50 #expect(segments.first { $0.text == "2" }?.superscript == true)
51 #expect(Self.shown(text, .init(showMarkup: true, options: options)).map(\.text).joined() == text)
52 }
53
54 struct Words: CodeHighlighter {
55 func highlights(language: String, code: String) -> [(range: Range<Int>, category: SyntaxCategory)] {
56 [(0..<4, .keyword)]
57 }
58 }
59
60 @Test func highlightsSrcBlocks() {
61 let text = "#+begin_src sh\necho hi\n#+end_src\n"
62 let segments = Self.shown(text, .init(highlighter: Words()))
63 #expect(segments.first { $0.text == "echo" }?.roles.contains(.syntax(.keyword)) == true)
64 }
65}