import Foundation import SwiftUI import Testing @testable import OrgSwift @testable import OrgSwiftUI @MainActor struct OrgMathLayoutTests { private func layout(_ source: String) -> OrgLayout { OrgLayoutBuilder(options: .init(), styler: PlainOrgCodeStyler(), keywords: .init()) .layout(OrgParser.parse(source)) } @Test func aLatexBlockIsAMathBlock() { let raw = "\\begin{align}\nx &= 1\n\\end{align}" guard case .math(let math) = layout(raw).blocks.first else { Issue.record("expected math, got \(layout(raw).blocks)"); return } #expect(math == OrgMath(tex: raw, display: true, source: raw)) } @Test func aParagraphThatIsOneDisplayExpressionIsAMathBlock() { guard case .math(let math) = layout(#"\[ x^2 \]"#).blocks.first else { Issue.record("expected math"); return } #expect(math.tex == " x^2 ") guard case .paragraph = layout(#"so \[ x^2 \]"#).blocks.first else { Issue.record("prose around display math keeps the paragraph"); return } } @Test func inlineMathIsItsSourceMarkedWithTheExpression() { guard case .paragraph(let text) = layout("a $x$ b").blocks.first else { Issue.record("expected a paragraph"); return } #expect(String(text.characters) == "a $x$ b") let marked = text.runs.compactMap { $0[OrgMathAttribute.self] } #expect(marked == [OrgMath(tex: "x", display: false, source: "$x$")]) } @Test func anExpressionSplitByATransformDrawsOnce() { let builder = OrgLayoutBuilder(options: .init(), styler: PlainOrgCodeStyler(), keywords: .init(), textTransform: { text in if let range = text.range(of: "#1") { text[range].link = URL(string: "https://x.test/1") } }) guard case .paragraph(let text) = builder.layout(OrgParser.parse("a $x#1y$ b")).blocks.first else { Issue.record("expected a paragraph"); return } var drawn = 0 _ = orgText(text) { _ in drawn += 1 return Text(verbatim: "·") } #expect(drawn == 1) } @Test func orgTextDrawsEachExpression() { guard case .paragraph(let text) = layout("$x$$x$ and $y$").blocks.first else { Issue.record("expected a paragraph"); return } var drawn: [String] = [] _ = orgText(text) { math in drawn.append(math.tex) return Text(verbatim: "·") } #expect(drawn.sorted() == ["x", "y"]) _ = orgText(text, math: nil) } }