md_test.gno
5.35 Kb ยท 258 lines
1package md_test
2
3import (
4 "testing"
5
6 "gno.land/p/moul/md"
7)
8
9func TestHelpers(t *testing.T) {
10 tests := []struct {
11 name string
12 function func() string
13 expected string
14 }{
15 {"Bold", func() string { return md.Bold("foo") }, "**foo**"},
16 {"Italic", func() string { return md.Italic("foo") }, "*foo*"},
17 {"Strikethrough", func() string { return md.Strikethrough("foo") }, "~~foo~~"},
18 {"H1", func() string { return md.H1("foo") }, "# foo\n"},
19 {"HorizontalRule", md.HorizontalRule, "---\n"},
20 {"InlineCode", func() string { return md.InlineCode("foo") }, "`foo`"},
21 {"CodeBlock", func() string { return md.CodeBlock("foo") }, "```\nfoo\n```"},
22 {"LanguageCodeBlock", func() string { return md.LanguageCodeBlock("go", "foo") }, "```go\nfoo\n```"},
23 {"Link", func() string { return md.Link("foo", "http://example.com") }, "[foo](http://example.com)"},
24 {"Image", func() string { return md.Image("foo", "http://example.com") }, ""},
25 {"InlineImageWithLink", func() string { return md.InlineImageWithLink("alt", "image-url", "link-url") }, "[](link-url)"},
26 {"Footnote", func() string { return md.Footnote("foo", "bar") }, "[foo]: bar"},
27 {"Paragraph", func() string { return md.Paragraph("foo") }, "foo\n\n"},
28 }
29
30 for _, tt := range tests {
31 t.Run(tt.name, func(t *testing.T) {
32 result := tt.function()
33 if result != tt.expected {
34 t.Errorf("%s() = %q, want %q", tt.name, result, tt.expected)
35 }
36 })
37 }
38}
39
40func TestLists(t *testing.T) {
41 t.Run("BulletList", func(t *testing.T) {
42 items := []string{"foo", "bar"}
43 expected := "- foo\n- bar\n"
44 result := md.BulletList(items)
45 if result != expected {
46 t.Errorf("BulletList(%q) = %q, want %q", items, result, expected)
47 }
48 })
49
50 t.Run("OrderedList", func(t *testing.T) {
51 items := []string{"foo", "bar"}
52 expected := "1. foo\n2. bar\n"
53 result := md.OrderedList(items)
54 if result != expected {
55 t.Errorf("OrderedList(%q) = %q, want %q", items, result, expected)
56 }
57 })
58
59 t.Run("TodoList", func(t *testing.T) {
60 items := []string{"foo", "bar\nmore bar"}
61 done := []bool{true, false}
62 expected := "- [x] foo\n- [ ] bar\n more bar\n"
63 result := md.TodoList(items, done)
64 if result != expected {
65 t.Errorf("TodoList(%q, %q) = %q, want %q", items, done, result, expected)
66 }
67 })
68}
69
70func TestNested(t *testing.T) {
71 t.Run("Nested Single Level", func(t *testing.T) {
72 content := "- foo\n- bar"
73 expected := " - foo\n - bar"
74 result := md.Nested(content, " ")
75 if result != expected {
76 t.Errorf("Nested(%q) = %q, want %q", content, result, expected)
77 }
78 })
79
80 t.Run("Nested Double Level", func(t *testing.T) {
81 content := " - foo\n - bar"
82 expected := " - foo\n - bar"
83 result := md.Nested(content, " ")
84 if result != expected {
85 t.Errorf("Nested(%q) = %q, want %q", content, result, expected)
86 }
87 })
88}
89
90func TestColumns(t *testing.T) {
91 tests := []struct {
92 name string
93 input []string
94 expected string
95 }{
96 {
97 name: "no columns",
98 input: []string{},
99 expected: "<gno-columns>\n</gno-columns>\n",
100 },
101 {
102 name: "one column",
103 input: []string{"Column 1"},
104 expected: `<gno-columns>
105Column 1
106</gno-columns>
107`,
108 },
109 {
110 name: "two columns",
111 input: []string{"Column 1", "Column 2"},
112 expected: `<gno-columns>
113Column 1
114|||
115Column 2
116</gno-columns>
117`,
118 },
119 {
120 name: "four columns",
121 input: []string{"A", "B", "C", "D"},
122 expected: `<gno-columns>
123A
124|||
125B
126|||
127C
128|||
129D
130</gno-columns>
131`,
132 },
133 {
134 name: "more than four columns",
135 input: []string{"1", "2", "3", "4", "5"},
136 expected: `<gno-columns>
1371
138|||
1392
140|||
1413
142|||
1434
144|||
1455
146</gno-columns>
147`,
148 },
149 }
150
151 for _, tt := range tests {
152 t.Run(tt.name, func(t *testing.T) {
153 result := md.Columns(tt.input)
154 if result != tt.expected {
155 t.Errorf("Columns(%v) =\n%q\nwant:\n%q", tt.input, result, tt.expected)
156 }
157 })
158 }
159}
160
161func TestColumnsN(t *testing.T) {
162 tests := []struct {
163 name string
164 content []string
165 colsPerRow int
166 padded bool
167 expected string
168 }{
169 {
170 name: "empty input",
171 content: []string{},
172 colsPerRow: 2,
173 padded: false,
174 expected: "<gno-columns>\n</gno-columns>\n",
175 },
176 {
177 name: "colsPerRow <= 0",
178 content: []string{"A", "B", "C"},
179 colsPerRow: 0,
180 padded: false,
181 expected: `<gno-columns>
182A
183|||
184B
185|||
186C
187</gno-columns>
188`,
189 },
190 {
191 name: "exact full row, no padding",
192 content: []string{"A", "B"},
193 colsPerRow: 2,
194 padded: false,
195 expected: `<gno-columns>
196A
197|||
198B
199</gno-columns>
200`,
201 },
202 {
203 name: "partial last row, no padding",
204 content: []string{"A", "B", "C"},
205 colsPerRow: 2,
206 padded: false,
207 expected: `<gno-columns>
208A
209|||
210B
211</gno-columns>
212<gno-columns>
213C
214</gno-columns>
215`,
216 },
217 {
218 name: "partial last row, with padding",
219 content: []string{"A", "B", "C"},
220 colsPerRow: 2,
221 padded: true,
222 expected: `<gno-columns>
223A
224|||
225B
226</gno-columns>
227<gno-columns>
228C
229|||
230
231</gno-columns>
232`,
233 },
234 {
235 name: "padded with more empty cells",
236 content: []string{"X"},
237 colsPerRow: 3,
238 padded: true,
239 expected: `<gno-columns>
240X
241|||
242
243|||
244
245</gno-columns>
246`,
247 },
248 }
249
250 for _, tt := range tests {
251 t.Run(tt.name, func(t *testing.T) {
252 result := md.ColumnsN(tt.content, tt.colsPerRow, tt.padded)
253 if result != tt.expected {
254 t.Errorf("ColumnsN(%v, %d, %v) =\n%q\nwant:\n%q", tt.content, tt.colsPerRow, tt.padded, result, tt.expected)
255 }
256 })
257 }
258}