import Testing @testable import gitbay @MainActor struct MarkdownTableTests { private func blocks(_ markdown: String) -> [MarkdownView.Block] { MarkdownView(markdown: markdown).blocks } @Test func headerSeparatorAndBodyBecomeATable() { let parsed = blocks(""" | Add | Documented in | |---|---| | An RSS feed | [Collections](https://orgo.krz.sh/) | """) #expect(parsed == [.table(MarkdownView.Table( header: ["Add", "Documented in"], rows: [["An RSS feed", "[Collections](https://orgo.krz.sh/)"]], alignments: [.leading, .leading] ))]) } @Test func alignmentComesFromTheSeparatorRow() { let parsed = blocks(""" | l | c | r | |:--|:-:|--:| | a | b | c | """) guard case .table(let table) = parsed.first else { Issue.record("expected a table, got \(parsed)") return } #expect(table.alignments == [.leading, .center, .trailing]) } @Test func outerPipesAreOptionalAndEscapedPipesStayInTheCell() { let parsed = blocks(""" a | b --- | --- x \\| y | z """) guard case .table(let table) = parsed.first else { Issue.record("expected a table, got \(parsed)") return } #expect(table.header == ["a", "b"]) #expect(table.rows == [["x \\| y", "z"]]) } @Test func raggedRowsArePaddedToTheHeaderWidth() { let parsed = blocks(""" | a | b | c | |---|---|---| | 1 | | 1 | 2 | 3 | 4 | """) guard case .table(let table) = parsed.first else { Issue.record("expected a table, got \(parsed)") return } #expect(table.rows == [["1", "", ""], ["1", "2", "3"]]) } @Test func aTableEndsAtABlankLineAndTheTextAfterItIsAParagraph() { let parsed = blocks(""" | a | |---| | 1 | After the table. """) #expect(parsed.count == 2) #expect(parsed.last == .paragraph("After the table.")) } @Test func aTableInterruptsThePrecedingParagraph() { let parsed = blocks(""" Each of these has a page in the guide: | a | |---| | 1 | """) #expect(parsed.first == .paragraph("Each of these has a page in the guide:")) #expect(parsed.count == 2) } @Test func aPipeInProseIsNotATable() { let parsed = blocks("Run `a | b` to pipe.\nIt is not a table.") #expect(parsed == [.paragraph("Run `a | b` to pipe. It is not a table.")]) } }