Tests/OrgSwiftUITests/OrgMathLayoutTests.swift
70 lines · 2645 bytes
1import Foundation
2import SwiftUI
3import Testing
4@testable import OrgSwift
5@testable import OrgSwiftUI
6
7@MainActor
8struct OrgMathLayoutTests {
9
10 private func layout(_ source: String) -> OrgLayout {
11 OrgLayoutBuilder(options: .init(), styler: PlainOrgCodeStyler(), keywords: .init())
12 .layout(OrgParser.parse(source))
13 }
14
15 @Test func aLatexBlockIsAMathBlock() {
16 let raw = "\\begin{align}\nx &= 1\n\\end{align}"
17 guard case .math(let math) = layout(raw).blocks.first else {
18 Issue.record("expected math, got \(layout(raw).blocks)"); return
19 }
20 #expect(math == OrgMath(tex: raw, display: true, source: raw))
21 }
22
23 @Test func aParagraphThatIsOneDisplayExpressionIsAMathBlock() {
24 guard case .math(let math) = layout(#"\[ x^2 \]"#).blocks.first else {
25 Issue.record("expected math"); return
26 }
27 #expect(math.tex == " x^2 ")
28 guard case .paragraph = layout(#"so \[ x^2 \]"#).blocks.first else {
29 Issue.record("prose around display math keeps the paragraph"); return
30 }
31 }
32
33 @Test func inlineMathIsItsSourceMarkedWithTheExpression() {
34 guard case .paragraph(let text) = layout("a $x$ b").blocks.first else {
35 Issue.record("expected a paragraph"); return
36 }
37 #expect(String(text.characters) == "a $x$ b")
38 let marked = text.runs.compactMap { $0[OrgMathAttribute.self] }
39 #expect(marked == [OrgMath(tex: "x", display: false, source: "$x$")])
40 }
41
42 @Test func anExpressionSplitByATransformDrawsOnce() {
43 let builder = OrgLayoutBuilder(options: .init(), styler: PlainOrgCodeStyler(), keywords: .init(),
44 textTransform: { text in
45 if let range = text.range(of: "#1") { text[range].link = URL(string: "https://x.test/1") }
46 })
47 guard case .paragraph(let text) = builder.layout(OrgParser.parse("a $x#1y$ b")).blocks.first else {
48 Issue.record("expected a paragraph"); return
49 }
50 var drawn = 0
51 _ = orgText(text) { _ in
52 drawn += 1
53 return Text(verbatim: "·")
54 }
55 #expect(drawn == 1)
56 }
57
58 @Test func orgTextDrawsEachExpression() {
59 guard case .paragraph(let text) = layout("$x$$x$ and $y$").blocks.first else {
60 Issue.record("expected a paragraph"); return
61 }
62 var drawn: [String] = []
63 _ = orgText(text) { math in
64 drawn.append(math.tex)
65 return Text(verbatim: "·")
66 }
67 #expect(drawn.sorted() == ["x", "y"])
68 _ = orgText(text, math: nil)
69 }
70}