Tests/OrgEditorAppKitTests/MarkdownModeTests.swift

49c5da97098ed000be935c19462d4102812b0471
orgstar/Tests/OrgEditorAppKitTests/MarkdownModeTests.swift

113 lines · 5117 bytes

9 symbols in this file
  1import AppKit
  2import OrgCore
  3import OrgDocument
  4import OrgPresentation
  5import Testing
  6@testable import OrgEditorAppKit
  7
  8/// Markdown files: highlighted, folded by heading, and closed to org's commands.
  9@MainActor
 10struct MarkdownModeTests {
 11    func markdown(_ text: String) -> Harness {
 12        let h = Harness(DocumentState(bytes: Array(text.utf8)), mode: .markdown)
 13        h.drainStyling()
 14        h.layout()
 15        return h
 16    }
 17
 18    func font(_ h: Harness, at offset: Int) -> NSFont? {
 19        h.textView.textStorage!.attribute(.font, at: offset, effectiveRange: nil) as? NSFont
 20    }
 21
 22    @Test func highlighting() {
 23        let text = "# Title\nplain **bold** and `code`\n* item\n"
 24        let h = markdown(text)
 25        let theme = h.editor.theme
 26        #expect(font(h, at: 2)?.fontDescriptor.symbolicTraits.contains(.bold) == true)
 27        #expect(font(h, at: 10) == theme.font)
 28        #expect(font(h, at: 17)?.fontDescriptor.symbolicTraits.contains(.bold) == true)
 29        let storage = h.textView.textStorage!
 30        let code = (text as NSString).range(of: "code").location
 31        #expect(storage.attribute(.foregroundColor, at: code, effectiveRange: nil) as? NSColor == theme.color("code"))
 32        // `* item` is a list item, not an org heading.
 33        let item = (text as NSString).range(of: "item").location
 34        #expect(font(h, at: item) == theme.font)
 35    }
 36
 37    @Test func typingKeepsHighlightingInStep() {
 38        let h = markdown("a\n```\n# code\n```\n# Heading\n")
 39        let heading = h.offset(of: "# Heading") + 2
 40        #expect(font(h, at: heading)?.fontDescriptor.symbolicTraits.contains(.bold) == true)
 41        // Removing the closing fence makes the heading code.
 42        let closing = h.offset(of: "```\n# Heading")
 43        h.textView.setSelectedRange(NSRange(location: closing, length: 4))
 44        h.textView.insertText("", replacementRange: h.textView.selectedRange())
 45        #expect(font(h, at: h.offset(of: "# Heading") + 2) == h.editor.theme.font)
 46        #expect(h.editor.document.text == "a\n```\n# code\n# Heading\n")
 47    }
 48
 49    @Test func tabFoldsHeadings() {
 50        let h = markdown("# A\nbody\n## B\nmore\n# C\ntext\n")
 51        h.caret(at: 1)
 52        h.editor.run("org.cycle")
 53        #expect(h.visibleLines == ["# A\n", "# C\n", "text\n"])
 54        h.editor.run("org.cycle")
 55        #expect(h.visibleLines == ["# A\n", "body\n", "## B\n", "# C\n", "text\n"])
 56        h.editor.run("org.cycle-global")
 57        #expect(h.visibleLines == ["# A\n", "# C\n"])
 58        h.editor.reveal(h.offset(of: "more"))
 59        #expect(h.visibleLines.contains("more\n"))
 60        // Folds stay on their headings as text is typed above them.
 61        h.editor.run("org.cycle-global")
 62        h.caret(at: 0)
 63        h.textView.insertText("x", replacementRange: h.textView.selectedRange())
 64        let folded = h.editor.foldState.folds.union(h.editor.foldState.bodyFolds)
 65        #expect(folded == Set(h.editor.markdown.outline.map(\.start)))
 66    }
 67
 68    @Test func clickingHashesCycles() {
 69        let h = markdown("## A\nbody\n- [ ] task\n")
 70        #expect(!h.editor.click(at: 4))
 71        #expect(h.editor.click(at: 1))
 72        #expect(h.visibleLines == ["## A\n"])
 73        // A task box isn't toggled: that would be org's checkbox command.
 74        _ = h.editor.click(at: 1)
 75        #expect(!h.editor.click(at: h.offset(of: "[ ]") + 1))
 76        #expect(h.editor.document.text == "## A\nbody\n- [ ] task\n")
 77    }
 78
 79    @Test func orgCommandsAreRefused() {
 80        let text = "# Heading\n- item\n| a |\n"
 81        let h = markdown(text)
 82        var messages: [String] = []
 83        var appCommands: [String] = []
 84        h.editor.onMessage = { messages.append($0) }
 85        h.editor.onAppCommand = { appCommands.append($0) }
 86        for id in ["org.todo.cycle", "org.item.insert", "org.heading.insert", "org.timestamp.inactive", "org.link.insert", "org.emphasize.bold",
 87                   "org.table.align", "org.tags.set", "org.schedule", "org.priority.up", "app.refile", "app.archive", "app.attach.attach",
 88                   "app.table-import", "app.export.html", "app.clock.in"] {
 89            h.caret(at: 3)
 90            h.editor.run(id)
 91            #expect(h.editor.document.text == text, "\(id)")
 92        }
 93        #expect(appCommands.isEmpty)
 94        #expect(messages == Array(repeating: "Not an org file", count: 16))
 95        // Typing doesn't continue lists or fill as org would.
 96        h.caret(at: 16)
 97        h.textView.insertNewline(nil)
 98        #expect(h.editor.document.text == "# Heading\n- item\n\n| a |\n")
 99        // App commands that aren't about the file's entries still reach the app.
100        h.editor.run("app.save")
101        #expect(appCommands == ["app.save"])
102    }
103
104    @Test func plainTextDoesNotFold() {
105        let h = Harness(DocumentState(bytes: Array("# A\nbody\n".utf8)), mode: .plain(language: nil))
106        var messages: [String] = []
107        h.editor.onMessage = { messages.append($0) }
108        h.caret(at: 1)
109        h.editor.run("org.cycle")
110        #expect(h.visibleLines == ["# A\n", "body\n"])
111        #expect(messages == ["Not an org file"])
112    }
113}
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