krz/orgstar

A native macOS editor for org-mode files. editor org-mode swift

Tests/OrgPresentationTests/ReadingTextTests.swift

2fa201330f61c801f777baa3f2943d49d8758cda
orgstar/Tests/OrgPresentationTests/ReadingTextTests.swift history · blame · raw

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