Tests/OrgPresentationTests/ReadingTextTests.swift
60 lines · 3239 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.unfolds != nil }.map(\.text).joined() == ":PROPERTIES:…")
15 #expect(segments.first { $0.text == "…" }?.unfolds == folded.first?.lowerBound)
16 let site = segments.first { $0.text == "the site" }
17 #expect(site?.link?.raw == "https://a.org" && site?.roles.contains(.link) == true)
18 #expect(segments.first { $0.text == "TODO" }?.roles.contains(.todo) == true)
19 #expect(segments.contains { $0.text == "b" && $0.roles.contains(.bold) })
20 #expect(segments.first { $0.text.hasPrefix("c.org") }?.link?.path == "c.org")
21 // Offsets point into the file.
22 #expect(site.map { (text as NSString).substring(with: NSRange(location: $0.offset, length: 8)) } == "the site")
23 }
24
25 static func shown(_ text: String, _ display: ReadingText.Display) -> [ReadingText.Segment] {
26 ReadingText.segments(OrgParser.parse(text), in: 0..<(text as NSString).length, display: display)
27 }
28
29 @Test func showMarkupShowsLinkBracketsAndEmphasisMarkers() {
30 var options = EditingOptions.org
31 options.hideEmphasisMarkers = true
32 let text = "a *b* [[c.org][d]]\n"
33 #expect(Self.shown(text, .init(options: options)).map(\.text).joined() == "a b d\n")
34 #expect(Self.shown(text, .init(showMarkup: false)).map(\.text).joined() == "a *b* d\n")
35 #expect(Self.shown(text, .init(showMarkup: true, options: options)).map(\.text).joined() == text)
36 }
37
38 @Test func prettyEntitiesShowTheirCharactersAndRaiseScripts() {
39 var options = EditingOptions.org
40 options.prettyEntities = true
41 options.subSuperscriptsNeedBraces = true
42 let text = "\\alpha and x^{2}\n#+begin_example\n\\beta\n#+end_example\n"
43 let segments = Self.shown(text, .init(options: options))
44 #expect(segments.map(\.text).joined() == "α and x2\n#+begin_example\n\\beta\n#+end_example\n")
45 #expect(segments.first { $0.text == "2" }?.superscript == true)
46 #expect(Self.shown(text, .init(showMarkup: true, options: options)).map(\.text).joined() == text)
47 }
48
49 struct Words: CodeHighlighter {
50 func highlights(language: String, code: String) -> [(range: Range<Int>, category: SyntaxCategory)] {
51 [(0..<4, .keyword)]
52 }
53 }
54
55 @Test func highlightsSrcBlocks() {
56 let text = "#+begin_src sh\necho hi\n#+end_src\n"
57 let segments = Self.shown(text, .init(highlighter: Words()))
58 #expect(segments.first { $0.text == "echo" }?.roles.contains(.syntax(.keyword)) == true)
59 }
60}