Tests/OrgPresentationTests/LatexPreviewTests.swift
37 lines · 1743 bytes
1import AppKit
2import OrgCore
3@testable import OrgPresentation
4import Testing
5
6@MainActor
7struct LatexPreviewTests {
8 @Test func findsFragmentsAndEnvironments() {
9 let text = """
10 #+STARTUP: latexpreview
11 * Math
12 Inline $x^2$ and \\(a+b\\), display $$\\sum_i i$$ and \\[e^{i\\pi}\\], a \\frac{1}{2} and a price of $3.
13 \\begin{align}
14 a &= b \\\\
15 c &= d
16 \\end{align}
17 #+begin_src latex
18 $not$
19 #+end_src
20 """
21 let tree = OrgParser.parse(text)
22 let found = LatexPreview.fragments(tree, in: 0..<(text as NSString).length)
23 #expect(found.map(\.latex) == ["x^2", "a+b", "\\sum_i i", "e^{i\\pi}", "\\frac{1}{2}", "\\begin{aligned}a &= b \\\\\nc &= d\\end{aligned}"])
24 #expect(found.map(\.display) == [false, false, true, true, false, true])
25 #expect(found.last?.environment == true)
26 #expect((text as NSString).substring(with: NSRange(found[0].range)) == "$x^2$")
27 #expect(Presentation.startupLatexPreview(tree) == true)
28 }
29
30 @Test func rendersMath() throws {
31 let inline = try #require(LatexPreview.render("x^2 + \\frac{a}{b}", display: false, fontSize: 13, color: .black, scale: 2))
32 #expect(inline.size.width > 20 && inline.ascent > 0 && inline.image.width == Int((inline.size.width * 2).rounded(.up)))
33 let aligned = try #require(LatexPreview.render("\\begin{aligned}a &= b \\\\ c &= d\\end{aligned}", display: true, fontSize: 13, color: .black, scale: 2))
34 #expect(aligned.size.height > inline.size.height)
35 #expect(LatexPreview.render("\\frac{a}{", display: false, fontSize: 13, color: .black, scale: 2) == nil)
36 }
37}