Debian Codegraph @

ast_test.go

raw symbols SCIP
1 package goldmark_test
2
3 import (
4 "bytes"
5 "testing"
6
7 . "github.com/yuin/goldmark"
8 "github.com/yuin/goldmark/ast"
9 "github.com/yuin/goldmark/parser"
10 "github.com/yuin/goldmark/testutil"
11 "github.com/yuin/goldmark/text"
12 )
13
14 func TestASTBlockNodeText(t *testing.T) {
15 var cases = []struct {
16 Name string
17 Source string
18 T1 string
19 T2 string
20 C bool
21 }{
22 {
23 Name: "AtxHeading",
24 Source: `# l1
25
26 a
27
28 # l2`,
29 T1: `l1`,
30 T2: `l2`,
31 },
32 {
33 Name: "SetextHeading",
34 Source: `l1
35 l2
36 ===============
37
38 a
39
40 l3
41 l4
42 ==============`,
43 T1: `l1
44 l2`,
45 T2: `l3
46 l4`,
47 },
48 {
49 Name: "CodeBlock",
50 Source: ` l1
51 l2
52
53 a
54
55 l3
56 l4`,
57 T1: `l1
58 l2
59 `,
60 T2: `l3
61 l4
62 `,
63 },
64 {
65 Name: "FencedCodeBlock",
66 Source: "```" + `
67 l1
68 l2
69 ` + "```" + `
70
71 a
72
73 ` + "```" + `
74 l3
75 l4`,
76 T1: `l1
77 l2
78 `,
79 T2: `l3
80 l4
81 `,
82 },
83 {
84 Name: "Blockquote",
85 Source: `> l1
86 > l2
87
88 a
89
90 > l3
91 > l4`,
92 T1: `l1
93 l2`,
94 T2: `l3
95 l4`,
96 },
97 {
98 Name: "List",
99 Source: `- l1
100 l2
101
102 a
103
104 - l3
105 l4`,
106 T1: `l1
107 l2`,
108 T2: `l3
109 l4`,
110 C: true,
111 },
112 {
113 Name: "HTMLBlock",
114 Source: `<div>
115 l1
116 l2
117 </div>
118
119 a
120
121 <div>
122 l3
123 l4`,
124 T1: `<div>
125 l1
126 l2
127 </div>
128 `,
129 T2: `<div>
130 l3
131 l4`,
132 },
133 }
134
135 for _, cs := range cases {
136 t.Run(cs.Name, func(t *testing.T) {
137 s := []byte(cs.Source)
138 md := New()
139 n := md.Parser().Parse(text.NewReader(s))
140 c1 := n.FirstChild()
141 c2 := c1.NextSibling().NextSibling()
142 if cs.C {
143 c1 = c1.FirstChild()
144 c2 = c2.FirstChild()
145 }
146 if !bytes.Equal(c1.Text(s), []byte(cs.T1)) { // nolint: staticcheck
147
148 t.Errorf("%s unmatch: %s", cs.Name, testutil.DiffPretty(c1.Text(s), []byte(cs.T1))) // nolint: staticcheck
149
150 }
151 if !bytes.Equal(c2.Text(s), []byte(cs.T2)) { // nolint: staticcheck
152
153 t.Errorf("%s(EOF) unmatch: %s", cs.Name, testutil.DiffPretty(c2.Text(s), []byte(cs.T2))) // nolint: staticcheck
154
155 }
156 })
157 }
158
159 }
160
161 func TestASTInlineNodeText(t *testing.T) {
162 var cases = []struct {
163 Name string
164 Source string
165 T1 string
166 }{
167 {
168 Name: "CodeSpan",
169 Source: "`c1`",
170 T1: `c1`,
171 },
172 {
173 Name: "Emphasis",
174 Source: `*c1 **c2***`,
175 T1: `c1 c2`,
176 },
177 {
178 Name: "Link",
179 Source: `[label](url)`,
180 T1: `label`,
181 },
182 {
183 Name: "AutoLink",
184 Source: `<http://url>`,
185 T1: `http://url`,
186 },
187 {
188 Name: "RawHTML",
189 Source: `<span>c1</span>`,
190 T1: `<span>`,
191 },
192 }
193
194 for _, cs := range cases {
195 t.Run(cs.Name, func(t *testing.T) {
196 s := []byte(cs.Source)
197 md := New()
198 n := md.Parser().Parse(text.NewReader(s))
199 c1 := n.FirstChild().FirstChild()
200 if !bytes.Equal(c1.Text(s), []byte(cs.T1)) { // nolint: staticcheck
201 t.Errorf("%s unmatch:\n%s", cs.Name, testutil.DiffPretty(c1.Text(s), []byte(cs.T1))) // nolint: staticcheck
202 }
203 })
204 }
205
206 }
207
208 func TestHasBlankPreviousLines(t *testing.T) {
209 var cases = []struct {
210 Name string
211 Source string
212 Node func(n ast.Node) ast.Node
213 Expected bool
214 }{
215 {
216 Name: "nesting paragraphs in blockquotes",
217 Source: `
218 > a
219 >
220 > b
221 `,
222 Node: func(n ast.Node) ast.Node {
223 return n.FirstChild().FirstChild().NextSibling()
224 },
225 Expected: true,
226 },
227 {
228 Name: "nesting HTML blocks in blockquotes",
229 Source: `
230 > <!-- a -->
231 >
232 > <!-- b -->
233 `,
234 Node: func(n ast.Node) ast.Node {
235 return n.FirstChild().FirstChild().NextSibling()
236 },
237 Expected: true,
238 },
239 {
240 Name: "nesting HTML blocks in blockquotes",
241 Source: `
242 > <!-- a -->
243 > <!-- b -->
244 `,
245 Node: func(n ast.Node) ast.Node {
246 return n.FirstChild().FirstChild().NextSibling()
247 },
248 Expected: false,
249 },
250 {
251 Name: "nesting loose lists in blockquotes",
252 Source: `
253 > - a
254 >
255 > - b
256 `,
257 Node: func(n ast.Node) ast.Node {
258 return n.FirstChild().FirstChild().FirstChild().NextSibling()
259 },
260 Expected: true,
261 },
262 {
263 Name: "nesting tight lists in blockquotes",
264 Source: `
265 > - a
266 > - b
267 `,
268 Node: func(n ast.Node) ast.Node {
269 return n.FirstChild().FirstChild().FirstChild().NextSibling()
270 },
271 Expected: false,
272 },
273 {
274 Name: "nesting paragraphs in lists",
275 Source: `
276 - a
277
278 b
279 `,
280 Node: func(n ast.Node) ast.Node {
281 return n.FirstChild().FirstChild().FirstChild().NextSibling()
282 },
283 Expected: true,
284 },
285 }
286 md := New()
287 for _, cs := range cases {
288 t.Run(cs.Name, func(t *testing.T) {
289 n := md.Parser().Parse(text.NewReader([]byte(cs.Source)))
290 if cs.Node(n).HasBlankPreviousLines() != cs.Expected {
291 t.Errorf("expected %v, got %v", cs.Expected, !cs.Expected)
292 }
293 })
294 }
295 }
296
297 func TestInlinePos(t *testing.T) {
298 markdown := New()
299
300 source := []byte(`[bar][]
301
302 [foo][bar]
303
304 [bar]
305
306 [foo](http://example.com)
307
308 aaaa **b**
309
310 ![aaa](http://example.com/foo.png "title")
311
312 [bar]:
313 /url "ti
314 tle"
315 `)
316 c := parser.NewContext()
317 n := markdown.Parser().Parse(text.NewReader(source), parser.WithContext(c))
318 if 0 != n.FirstChild().FirstChild().Pos() {
319 t.Error("unexpected position for 1st link reference")
320 }
321 if 9 != n.FirstChild().NextSibling().FirstChild().Pos() {
322 t.Error("unexpected position for 2nd link reference")
323 }
324 if 21 != n.FirstChild().NextSibling().NextSibling().FirstChild().Pos() {
325 t.Error("unexpected position for 3rd link reference")
326 }
327 if 28 != n.FirstChild().NextSibling().NextSibling().NextSibling().FirstChild().Pos() {
328 t.Error("unexpected position for 1st inline link ")
329 }
330 if 60 != n.FirstChild().NextSibling().NextSibling().NextSibling().NextSibling().FirstChild().NextSibling().Pos() {
331 t.Error("unexpected position for 1st emphasis")
332 }
333 if 68 != n.FirstChild().NextSibling().NextSibling().NextSibling().NextSibling().NextSibling().FirstChild().Pos() {
334 t.Error("unexpected position for 1st image")
335 }
336 }