import Foundation import Testing @testable import SprachmittlerText @Suite("Markdown presentation") struct MarkdownPresentationTests { @Test func parsesHeadingsParagraphsAndRules() { let blocks = MarkdownPresentation.blocks(in: """ # Title First paragraph. Second line. --- """) #expect(blocks == [ .heading(level: 1, text: "Title"), .paragraph("First paragraph.\nSecond line."), .rule, ]) } @Test func parsesOrderedAndUnorderedLists() { let blocks = MarkdownPresentation.blocks(in: """ - one * two 3. three 4) four """) #expect(blocks == [ .listItem(ordered: false, number: 0, indent: 0, text: "one"), .listItem(ordered: false, number: 0, indent: 0, text: "two"), .listItem(ordered: true, number: 3, indent: 0, text: "three"), .listItem(ordered: true, number: 4, indent: 0, text: "four"), ]) } @Test func parsesQuotesAndFencedCode() { let blocks = MarkdownPresentation.blocks(in: """ > quoted **text** > second ```swift let value = 1 ``` """) #expect(blocks == [ .quote("quoted **text**\nsecond"), .codeBlock(language: "swift", text: "let value = 1"), ]) } @Test func keepsUnclosedFenceAsCode() { let blocks = MarkdownPresentation.blocks(in: "```\nstill code\n") #expect(blocks == [.codeBlock(language: nil, text: "still code")]) } @Test func recognisesInlineFormatting() { let attributed = MarkdownPresentation.inline("**bold** and *italic* and `code`") let intents = attributed.runs.compactMap(\.inlinePresentationIntent) #expect(intents.contains { $0.contains(.stronglyEmphasized) }) #expect(intents.contains { $0.contains(.emphasized) }) #expect(intents.contains { $0.contains(.code) }) } @Test func parsesLinksButNeverImages() throws { let attributed = MarkdownPresentation .inline("See [docs](https://example.com) and ![alt](https://example.com/x.png)") let links = attributed.runs.compactMap(\.link) #expect(try links == [#require(URL(string: "https://example.com"))]) #expect(String(attributed.characters).contains("alt")) #expect(!String(attributed.characters).contains("x.png")) } @Test func malformedMarkdownFallsBackToText() { let attributed = MarkdownPresentation.inline("**unclosed and

Hello

", translation: "x")) #expect(!MarkdownPresentation.shouldRender(source: "```\nlet x = 1\n```", translation: "x")) #expect(!MarkdownPresentation.shouldRender(source: "Just prose, no markers.", translation: "Nur Prosa.")) #expect(!MarkdownPresentation.shouldRender(source: "", translation: "**bold**")) } @Test func headingsWithDifferentLevelsAndClosingHashes() { let blocks = MarkdownPresentation.blocks(in: "### Sub heading ###\n\n####### not a heading") #expect(blocks == [ .heading(level: 3, text: "Sub heading"), .paragraph("####### not a heading"), ]) } }