Tests/OrgEditorAppKitTests/LatexPreviewEditorTests.swift

06941b2cf25c6ccc1629723cd7951910ce15e67a
orgstar/Tests/OrgEditorAppKitTests/LatexPreviewEditorTests.swift history · blame · raw

42 lines · 1858 bytes

 1import AppKit
 2import OrgCore
 3@testable import OrgEditorAppKit
 4import Testing
 5
 6@MainActor
 7struct LatexPreviewEditorTests {
 8    static let text = "#+STARTUP: latexpreview\n* Math\nInline $x^2$ here.\n\\begin{equation}\na = b\n\\end{equation}\nEnd.\n"
 9
10    /// The layout fragment over `offset`.
11    static func fragment(_ h: Harness, at offset: Int) -> NSTextLayoutFragment? {
12        h.layout()
13        let manager = h.editor.textLayoutManager
14        let content = manager.textContentManager!
15        guard let location = content.location(content.documentRange.location, offsetBy: offset) else { return nil }
16        return manager.textLayoutFragment(for: location)
17    }
18
19    @Test func startupShowsPreviewsOffTheCaretLine() throws {
20        let h = Harness(Self.text)
21        #expect(h.editor.showsLatex)
22        let ns = Self.text as NSString
23        let dollar = ns.range(of: "$x^2$").location
24        let storage = try #require(h.textView.textStorage)
25        let box = try #require(storage.attribute(.orgLatex, at: dollar, effectiveRange: nil) as? LatexBox)
26        #expect(box.rendering.size.width > 5 && !box.environment)
27        let kern = storage.attribute(.kern, at: dollar + 4, effectiveRange: nil) as? CGFloat
28        #expect((kern ?? 0) > 5)
29        #expect(Self.fragment(h, at: dollar) is LatexFragment)
30        let begin = ns.range(of: "\\begin").location
31        #expect((storage.attribute(.orgLatex, at: begin, effectiveRange: nil) as? LatexBox)?.environment == true)
32
33        // On the caret's line, the source.
34        h.caret(at: dollar + 1)
35        h.editor.revealCaretLine()
36        #expect(storage.attribute(.orgLatex, at: dollar, effectiveRange: nil) == nil)
37
38        h.editor.run("org.latex-preview")
39        #expect(!h.editor.showsLatex)
40        #expect(storage.attribute(.orgLatex, at: begin, effectiveRange: nil) == nil)
41    }
42}