Debian Codegraph @

src/tests/sourcepos.rs

raw
1 use nodes::NodeValueDiscriminants;
2 use pretty_assertions::assert_eq;
3 use strum::VariantArray;
4
5 use super::*;
6
7 type TestCase = (&'static [Sourcepos], &'static str);
8
9 const DOCUMENT: TestCase = (&[sourcepos!((1:1-1:1))], "a");
10
11 const FRONT_MATTER: TestCase = (
12 &[sourcepos!((1:1-3:3))],
13 r#"---
14 a: b
15 ---
16
17 hello world
18 "#,
19 );
20
21 const BLOCK_QUOTE: TestCase = (
22 &[sourcepos!((1:1-3:36))],
23 r#"> hello world
24 > this is line 1
25 > this is line 2 and some extra text
26
27 hello world"#,
28 );
29
30 const MULTILINE_BLOCK_QUOTE: TestCase = (
31 &[sourcepos!((3:1-7:3))],
32 r#"Some text
33
34 >>>
35 hello world
36 this is line 1
37 this is line 2 and some extra text
38 >>>
39
40 hello world"#,
41 );
42
43 const LIST: TestCase = (
44 &[sourcepos!((1:1-2:38))],
45 r#"- bullet point one
46 - bullet point two and some extra text
47
48 hello world
49 "#,
50 );
51
52 const ITEM: TestCase = (
53 &[sourcepos!((1:1-1:18)), sourcepos!((2:1-2:38))],
54 r#"- bullet point one
55 - bullet point two and some extra text
56
57 hello world
58 "#,
59 );
60
61 const TASK_ITEM: TestCase = (
62 &[sourcepos!((1:1-1:22)), sourcepos!((3:1-3:24))],
63 r#"- [ ] bullet point one
64 - bullet point two and some extra text
65 - [x] bullet point three
66
67 hello world
68 "#,
69 );
70
71 const DESCRIPTION_LIST: TestCase = (
72 &[sourcepos!((1:1-7:11))],
73 r#"Term 1
74
75 : Details 1
76
77 Term 2
78
79 : Details 2"#,
80 );
81
82 const DESCRIPTION_ITEM: TestCase = (
83 &[sourcepos!((1:1-3:11)), sourcepos!((5:1-7:11))],
84 r#"Term 1
85
86 : Details 1
87
88 Term 2
89
90 : Details 2"#,
91 );
92
93 const DESCRIPTION_TERM: TestCase = (
94 &[sourcepos!((1:1-1:6))],
95 r#"Term 1
96
97 : Details 1
98
99 hello world
100 "#,
101 );
102
103 const DESCRIPTION_DETAILS: TestCase = (
104 &[sourcepos!((3:1-3:11))],
105 r#"Term 1
106
107 : Details 1
108
109 hello world
110 "#,
111 );
112
113 const CODE_BLOCK: TestCase = (
114 &[sourcepos!((1:1-3:3))],
115 r#"```
116 hello world
117 ```
118
119 hello world
120 "#,
121 );
122
123 const HTML_BLOCK: TestCase = (
124 &[sourcepos!((1:1-2:30)), sourcepos!((5:1-5:10))],
125 r#"<details>
126 <summary>hello world</summary>
127
128 hello world
129 </details>
130
131 hello world
132 "#,
133 );
134
135 const HTML_INLINE: TestCase = (
136 &[sourcepos!((1:7-3:14))],
137 r#"hello <img
138 src="foo"
139 alt="bar"> world
140 "#,
141 );
142
143 #[cfg(feature = "phoenix_heex")]
144 const HEEX_BLOCK: TestCase = (
145 &[sourcepos!((1:1-3:10))],
146 r#"<.header>
147 hello
148 </.header>
149
150 hello world
151 "#,
152 );
153
154 #[cfg(feature = "phoenix_heex")]
155 const HEEX_INLINE: TestCase = (
156 &[sourcepos!((1:7-1:12))],
157 r#"hello {some} world
158 "#,
159 );
160
161 const PARAGRAPH: TestCase = (
162 &[sourcepos!((1:1-1:11)), sourcepos!((4:1-4:11))],
163 r#"hello world
164
165
166 hello world
167 "#,
168 );
169
170 const HEADING: TestCase = (
171 &[sourcepos!((5:1-5:13)), sourcepos!((7:1-7:16))],
172 r#"---
173 a: b
174 ---
175
176 # Hello World
177
178 # Test heading #
179
180 hello world
181 "#,
182 );
183
184 const THEMATIC_BREAK: TestCase = (
185 &[sourcepos!((3:2-3:4))],
186 r#"Hello
187
188 ---
189
190 World"#,
191 );
192
193 const FOOTNOTE_DEFINITION: TestCase = (
194 &[sourcepos!((3:1-3:11))],
195 r#"Hello[^1]
196
197 [^1]: World
198 "#,
199 );
200
201 const FOOTNOTE_REFERENCE: TestCase = (
202 &[sourcepos!((1:6-1:9))],
203 r#"Hello[^1]
204
205 [^1]: World
206 "#,
207 );
208
209 #[cfg(feature = "shortcodes")]
210 const SHORTCODE: TestCase = (
211 &[sourcepos!((2:2-2:7))],
212 r#"nya~
213 !:fire:!
214 "#,
215 );
216
217 const TABLE: TestCase = (
218 &[sourcepos!((3:1-5:17))],
219 r#"stuff before
220
221 | Hello | World |
222 | ----- | ----- |
223 | cell1 | cell2 |
224
225 hello world
226 "#,
227 );
228
229 const TABLE_ROW: TestCase = (
230 &[sourcepos!((3:1-3:17)), sourcepos!((5:1-5:18))],
231 r#"stuff before
232
233 | Hello | World |
234 | ----- | ----- |
235 | cell1 | cell02 |
236
237 hello world
238 "#,
239 );
240
241 const TABLE_CELL: TestCase = (
242 &[
243 sourcepos!((3:2-3:8)),
244 sourcepos!((3:10-3:16)),
245 sourcepos!((5:2-5:8)),
246 sourcepos!((5:10-5:17)),
247 ],
248 r#"stuff before
249
250 | Hello | World |
251 | ----- | ----- |
252 | cell1 | cell02 |
253
254 hello world
255 "#,
256 );
257
258 const TEXT: TestCase = (
259 &[
260 sourcepos!((1:1-1:12)),
261 sourcepos!((3:3-3:7)),
262 sourcepos!((3:11-3:15)),
263 sourcepos!((5:3-5:7)),
264 sourcepos!((5:11-5:16)),
265 sourcepos!((7:1-7:11)),
266 sourcepos!((9:3-9:13)),
267 sourcepos!((11:3-11:8)),
268 sourcepos!((12:3-12:9)),
269 sourcepos!((12:12-12:15)),
270 sourcepos!((14:1-14:5)),
271 sourcepos!((14:7-14:7)),
272 sourcepos!((14:8-14:18)),
273 sourcepos!((14:20-14:20)),
274 sourcepos!((14:21-14:26)),
275 sourcepos!((16:7-16:14)),
276 ],
277 r#"stuff before
278
279 | Hello | World |
280 | ----- | ----- |
281 | cell1 | cell02 |
282
283 hello world
284
285 > hello world
286
287 - item 1[^1]
288 - item 2 **bold**
289
290 Test \`hello world\` here.
291
292 [^1]: The end.
293 "#,
294 );
295
296 const SOFT_BREAK: TestCase = (&[sourcepos!((1:13-1:13))], "stuff before\nstuff after");
297 const LINE_BREAK: TestCase = (
298 &[sourcepos!((1:13-1:15)), sourcepos!((4:13-4:14))],
299 "stuff before \nstuff after\n\nstuff before\\\nstuff after\n",
300 );
301
302 const CODE: TestCase = (&[sourcepos!((1:7-1:13))], "hello `world`");
303
304 const EMPH: TestCase = (
305 &[sourcepos!((1:7-1:13)), sourcepos!((1:23-2:4))],
306 "hello *world* between *wo\nrld* after",
307 );
308
309 const STRONG: TestCase = (
310 &[sourcepos!((1:7-1:15)), sourcepos!((1:25-2:5))],
311 "hello **world** between **wo\nrld** after",
312 );
313
314 const STRIKETHROUGH: TestCase = (
315 &[sourcepos!((1:7-1:15)), sourcepos!((1:25-2:5))],
316 "hello ~~world~~ between ~~wo\nrld~~ after",
317 );
318
319 const HIGHLIGHT: TestCase = (
320 &[sourcepos!((1:7-1:15)), sourcepos!((1:25-2:5))],
321 "hello ==world== between ==wo\nrld== after",
322 );
323
324 const INSERT: TestCase = (
325 &[sourcepos!((1:7-1:15)), sourcepos!((1:25-2:5))],
326 "hello ++world++ between ++wo\nrld++ after",
327 );
328
329 const SUPERSCRIPT: TestCase = (
330 &[sourcepos!((1:7-1:13)), sourcepos!((1:23-2:4))],
331 "hello ^world^ between ^wo\nrld^ after",
332 );
333
334 const SUBSCRIPT: TestCase = (
335 &[sourcepos!((1:7-1:13)), sourcepos!((1:23-2:4))],
336 "hello ~world~ between ~wo\nrld~ after",
337 );
338
339 const LINK: TestCase = (
340 &[
341 sourcepos!((1:7-1:32)),
342 sourcepos!((2:7-2:32)),
343 sourcepos!((3:7-3:11)),
344 sourcepos!((4:7-4:16)),
345 sourcepos!((5:7-5:29)),
346 sourcepos!((6:7-6:21)),
347 sourcepos!((7:7-7:21)),
348 ],
349 r#"hello <https://example.com/fooo> world
350 hello [foo](https://example.com) world
351 hello [foo] world
352 hello [bar][bar] world
353 hello https://example.com/foo world
354 hello www.example.com world
355 hello [email protected] world
356
357 [foo]: https://example.com
358 [bar]: https://example.com"#,
359 );
360
361 const IMAGE: TestCase = (
362 &[sourcepos!((1:7-1:38))],
363 "hello ![alt text](https://example.com) banana",
364 );
365
366 const MATH: TestCase = (
367 &[
368 sourcepos!((3:1-3:7)),
369 sourcepos!((3:17-3:26)),
370 sourcepos!((3:36-3:44)),
371 ],
372 r#"hello
373
374 $1 + 1$ between $`1 + 23`$ between $$a + b$$
375
376 banana"#,
377 );
378
379 const ESCAPED: TestCase = (
380 &[
381 sourcepos!((1:1-1:2)),
382 sourcepos!((1:3-1:4)),
383 sourcepos!((1:5-1:6)),
384 sourcepos!((1:7-1:8)),
385 sourcepos!((1:9-1:10)),
386 sourcepos!((1:11-1:12)),
387 sourcepos!((1:13-1:14)),
388 sourcepos!((1:15-1:16)),
389 sourcepos!((1:17-1:18)),
390 sourcepos!((1:19-1:20)),
391 sourcepos!((1:21-1:22)),
392 sourcepos!((1:23-1:24)),
393 sourcepos!((1:25-1:26)),
394 sourcepos!((1:27-1:28)),
395 sourcepos!((1:29-1:30)),
396 sourcepos!((1:31-1:32)),
397 sourcepos!((1:33-1:34)),
398 sourcepos!((1:35-1:36)),
399 sourcepos!((1:37-1:38)),
400 sourcepos!((1:39-1:40)),
401 sourcepos!((1:41-1:42)),
402 sourcepos!((1:43-1:44)),
403 sourcepos!((1:45-1:46)),
404 sourcepos!((1:47-1:48)),
405 sourcepos!((1:49-1:50)),
406 sourcepos!((1:51-1:52)),
407 sourcepos!((1:53-1:54)),
408 sourcepos!((1:55-1:56)),
409 sourcepos!((1:57-1:58)),
410 sourcepos!((1:59-1:60)),
411 sourcepos!((1:61-1:62)),
412 sourcepos!((1:63-1:64)),
413 ],
414 r#"\!\"\#\$\%\&\'\(\)\*\+\,\-\.\/\:\;\<\=\>\?\@\[\\\]\^\_\`\{\|\}\~\a"#,
415 );
416
417 const WIKI_LINK: TestCase = (
418 &[sourcepos!((1:1-1:9)), sourcepos!((3:1-3:33))],
419 r#"[[floop]]
420
421 [[http://example.com|some title]]
422
423 after"#,
424 );
425
426 const UNDERLINE: TestCase = (&[sourcepos!((1:8-1:22))], "before __hello world__ after");
427
428 const SPOILERED_TEXT: TestCase = (
429 &[sourcepos!((2:1-2:11))],
430 r#"before
431 ||spoiler||
432 after"#,
433 );
434
435 // NOTE: I've adjusted this from its original asserted sourcepos (2:1-2:8) while
436 // fixing emphasis sourcepos. I am not even sure what it is, really.
437 const ESCAPED_TAG: TestCase = (
438 &[sourcepos!((2:2-2:8))],
439 r#"before
440 ||hello|
441 after"#,
442 );
443
444 const ALERT: TestCase = (
445 &[sourcepos!((2:1-3:9))],
446 r#"before
447 > [!note]
448 > it's on
449
450 after"#,
451 );
452
453 const SUBTEXT: TestCase = (
454 &[sourcepos!((2:1-2:14))],
455 r#"before
456 -# Hello World
457
458 hello world
459 "#,
460 );
461
462 const BLOCK_DIRECTIVE: TestCase = (
463 &[sourcepos!((2:1-4:3))],
464 r#"before
465 :::
466 hello world
467 :::
468 after"#,
469 );
470
471 fn node_values() -> HashMap<NodeValueDiscriminants, TestCase> {
472 use NodeValueDiscriminants::*;
473
474 NodeValueDiscriminants::VARIANTS
475 .iter()
476 .filter(|v| !matches!(v, Raw))
477 .map(|v| {
478 let text = match v {
479 Document => DOCUMENT,
480 FrontMatter => FRONT_MATTER,
481 BlockQuote => BLOCK_QUOTE,
482 MultilineBlockQuote => MULTILINE_BLOCK_QUOTE,
483 List => LIST,
484 Item => ITEM,
485 TaskItem => TASK_ITEM,
486 DescriptionList => DESCRIPTION_LIST,
487 DescriptionItem => DESCRIPTION_ITEM,
488 DescriptionTerm => DESCRIPTION_TERM,
489 DescriptionDetails => DESCRIPTION_DETAILS,
490 CodeBlock => CODE_BLOCK,
491 HtmlBlock => HTML_BLOCK,
492 HtmlInline => HTML_INLINE,
493 Paragraph => PARAGRAPH,
494 Heading => HEADING,
495 ThematicBreak => THEMATIC_BREAK,
496 FootnoteDefinition => FOOTNOTE_DEFINITION,
497 FootnoteReference => FOOTNOTE_REFERENCE,
498 #[cfg(feature = "shortcodes")]
499 ShortCode => SHORTCODE,
500 Table => TABLE,
501 TableRow => TABLE_ROW,
502 TableCell => TABLE_CELL,
503 Text => TEXT,
504 SoftBreak => SOFT_BREAK,
505 LineBreak => LINE_BREAK,
506 Code => CODE,
507 Emph => EMPH,
508 Strong => STRONG,
509 Strikethrough => STRIKETHROUGH,
510 Highlight => HIGHLIGHT,
511 Insert => INSERT,
512 Superscript => SUPERSCRIPT,
513 Subscript => SUBSCRIPT,
514 Link => LINK,
515 Image => IMAGE,
516 Math => MATH,
517 Escaped => ESCAPED,
518 WikiLink => WIKI_LINK,
519 Underline => UNDERLINE,
520 SpoileredText => SPOILERED_TEXT,
521 EscapedTag => ESCAPED_TAG,
522 Alert => ALERT,
523 Subtext => SUBTEXT,
524 BlockDirective => BLOCK_DIRECTIVE,
525 Raw => unreachable!(),
526 #[cfg(feature = "phoenix_heex")]
527 HeexBlock => HEEX_BLOCK,
528 #[cfg(feature = "phoenix_heex")]
529 HeexInline => HEEX_INLINE,
530 };
531 (*v, text)
532 })
533 .collect()
534 }
535
536 #[test]
537 fn sourcepos() {
538 let node_values = node_values();
539
540 let mut options = Options::default();
541 options.render.escaped_char_spans = true;
542 options.extension.front_matter_delimiter = Some("---".to_string());
543 options.extension.description_lists = true;
544 options.extension.footnotes = true;
545 options.extension.table = true;
546 options.extension.tasklist = true;
547 options.extension.strikethrough = true;
548 options.extension.highlight = true;
549 options.extension.insert = true;
550 options.extension.superscript = true;
551 options.extension.subscript = true;
552 options.extension.autolink = true;
553 #[cfg(feature = "shortcodes")]
554 {
555 options.extension.shortcodes = true;
556 }
557 options.extension.math_code = true;
558 options.extension.math_dollars = true;
559 options.extension.multiline_block_quotes = true;
560 options.extension.wikilinks_title_after_pipe = true;
561 options.extension.underline = true;
562 options.extension.spoiler = true;
563 options.extension.alerts = true;
564 options.extension.subtext = true;
565 options.extension.block_directive = true;
566 #[cfg(feature = "phoenix_heex")]
567 {
568 options.extension.phoenix_heex = true;
569 }
570
571 for (kind, (expecteds, text)) in node_values {
572 let arena = Arena::new();
573 let root = parse_document(&arena, text, &options);
574 let asts: Vec<_> = root
575 .descendants()
576 .filter(|d| NodeValueDiscriminants::from(&d.data().value) == kind)
577 .collect();
578
579 if asts.len() != expecteds.len() {
580 panic!(
581 "expected {} node(s) of kind {:?}, but got {}",
582 expecteds.len(),
583 kind,
584 asts.len()
585 );
586 }
587
588 for (ast, expected) in asts.into_iter().zip(expecteds) {
589 let actual = ast.data().sourcepos;
590 assert_eq!(
591 *expected, actual,
592 "{} != {} for {:?}",
593 expected, actual, kind
594 );
595 }
596 }
597 }