gitbayTests/MarkdownTests.swift
92 lines · 2784 bytes
1import Testing
2@testable import gitbay
3
4@MainActor
5struct MarkdownTableTests {
6
7 private func blocks(_ markdown: String) -> [MarkdownView.Block] {
8 MarkdownView(markdown: markdown).blocks
9 }
10
11 @Test func headerSeparatorAndBodyBecomeATable() {
12 let parsed = blocks("""
13 | Add | Documented in |
14 |---|---|
15 | An RSS feed | [Collections](https://orgo.krz.sh/) |
16 """)
17 #expect(parsed == [.table(MarkdownView.Table(
18 header: ["Add", "Documented in"],
19 rows: [["An RSS feed", "[Collections](https://orgo.krz.sh/)"]],
20 alignments: [.leading, .leading]
21 ))])
22 }
23
24 @Test func alignmentComesFromTheSeparatorRow() {
25 let parsed = blocks("""
26 | l | c | r |
27 |:--|:-:|--:|
28 | a | b | c |
29 """)
30 guard case .table(let table) = parsed.first else {
31 Issue.record("expected a table, got \(parsed)")
32 return
33 }
34 #expect(table.alignments == [.leading, .center, .trailing])
35 }
36
37 @Test func outerPipesAreOptionalAndEscapedPipesStayInTheCell() {
38 let parsed = blocks("""
39 a | b
40 --- | ---
41 x \\| y | z
42 """)
43 guard case .table(let table) = parsed.first else {
44 Issue.record("expected a table, got \(parsed)")
45 return
46 }
47 #expect(table.header == ["a", "b"])
48 #expect(table.rows == [["x \\| y", "z"]])
49 }
50
51 @Test func raggedRowsArePaddedToTheHeaderWidth() {
52 let parsed = blocks("""
53 | a | b | c |
54 |---|---|---|
55 | 1 |
56 | 1 | 2 | 3 | 4 |
57 """)
58 guard case .table(let table) = parsed.first else {
59 Issue.record("expected a table, got \(parsed)")
60 return
61 }
62 #expect(table.rows == [["1", "", ""], ["1", "2", "3"]])
63 }
64
65 @Test func aTableEndsAtABlankLineAndTheTextAfterItIsAParagraph() {
66 let parsed = blocks("""
67 | a |
68 |---|
69 | 1 |
70
71 After the table.
72 """)
73 #expect(parsed.count == 2)
74 #expect(parsed.last == .paragraph("After the table."))
75 }
76
77 @Test func aTableInterruptsThePrecedingParagraph() {
78 let parsed = blocks("""
79 Each of these has a page in the guide:
80 | a |
81 |---|
82 | 1 |
83 """)
84 #expect(parsed.first == .paragraph("Each of these has a page in the guide:"))
85 #expect(parsed.count == 2)
86 }
87
88 @Test func aPipeInProseIsNotATable() {
89 let parsed = blocks("Run `a | b` to pipe.\nIt is not a table.")
90 #expect(parsed == [.paragraph("Run `a | b` to pipe. It is not a table.")])
91 }
92}