Debian Codegraph @

src/tests/commonmark.rs

raw
1 use ntest::test_case;
2 use pretty_assertions::assert_eq;
3
4 use crate::nodes::{Ast, LineColumn, ListType, NodeList};
5
6 use super::*;
7
8 #[test]
9 fn commonmark_removes_redundant_strong() {
10 let input = "This is **something **even** better**";
11 let output = "This is **something even better**\n";
12 commonmark(input, output, None);
13 }
14
15 #[test]
16 fn commonmark_avoids_spurious_backslash() {
17 let arena = Arena::new();
18 let options = Options::default();
19 let empty = LineColumn { line: 0, column: 0 };
20
21 let ast = |val: NodeValue| arena.alloc(Ast::new(val, empty).into());
22 let root = ast(NodeValue::Document);
23
24 let p1 = ast(NodeValue::Paragraph);
25 p1.append(ast(NodeValue::Text("Line 1".into())));
26 p1.append(ast(NodeValue::LineBreak));
27 root.append(p1);
28
29 let p2 = ast(NodeValue::Paragraph);
30 p2.append(ast(NodeValue::Text("Line 2".into())));
31 root.append(p2);
32
33 let mut output = String::new();
34 cm::format_document(root, &options, &mut output).unwrap();
35
36 compare_strs(&output, "Line 1\n\nLine 2\n", "rendered", "<synthetic>");
37 }
38
39 #[test]
40 fn commonmark_renders_single_list_item() {
41 let arena = Arena::new();
42 let options = Options::default();
43 let empty = LineColumn { line: 0, column: 0 };
44 let ast = |val: NodeValue| arena.alloc(Ast::new(val, empty).into());
45 let list_options = NodeList {
46 list_type: ListType::Ordered,
47 start: 1,
48 ..Default::default()
49 };
50 let list = ast(NodeValue::List(list_options));
51 let item = ast(NodeValue::Item(list_options));
52 let p = ast(NodeValue::Paragraph);
53 p.append(ast(NodeValue::Text("Item 1".into())));
54 item.append(p);
55 list.append(item);
56 let mut output = String::new();
57 cm::format_document(item, &options, &mut output).unwrap();
58 compare_strs(&output, "1. Item 1\n", "rendered", "<synthetic>");
59 }
60
61 #[test_case("$$x^2$$ and $1 + 2$ and $`y^2`$", "$$x^2$$ and $1 + 2$ and $`y^2`$\n")]
62 #[test_case("$$\nx^2\n$$", "$$\nx^2\n$$\n")]
63 #[test_case("```math\nx^2\n```", "```math\nx^2\n```\n")]
64 fn commonmark_math(markdown: &str, cm: &str) {
65 let mut options = Options::default();
66 options.extension.math_dollars = true;
67 options.extension.math_code = true;
68
69 commonmark(markdown, cm, Some(&options));
70 }
71
72 #[test_case("\\(x^2\\) and \\[y^2\\]", "\\(x^2\\) and \\[y^2\\]\n")]
73 #[test_case("\\[\nx^2\n\\]", "\\[\nx^2\n\\]\n")]
74 fn commonmark_latex_math(markdown: &str, cm: &str) {
75 let mut options = Options::default();
76 options.extension.math_latex = true;
77
78 commonmark(markdown, cm, Some(&options));
79 }
80
81 #[test_case("\\(x^2\\) and \\[y^2\\]", "$x^2$ and $$y^2$$\n")]
82 fn commonmark_latex_math_with_dollars_enabled(markdown: &str, cm: &str) {
83 let mut options = Options::default();
84 options.extension.math_latex = true;
85 options.extension.math_dollars = true;
86
87 commonmark(markdown, cm, Some(&options));
88 }
89
90 #[test_case("This [[url]] that", "This [[url|url]] that\n")]
91 #[test_case("This [[url|link label]] that", "This [[url|link%20label]] that\n")]
92 fn commonmark_wikilinks(markdown: &str, cm: &str) {
93 let mut options = Options::default();
94 options.extension.wikilinks_title_before_pipe = true;
95
96 commonmark(markdown, cm, Some(&options));
97 }
98 #[test]
99 fn commonmark_relist() {
100 commonmark(
101 concat!("3. one\n", "5. two\n",),
102 // Note that right now we always include enough room for up to an user
103 // defined number of digits. TODO: Ideally we determine the maximum
104 // digit length before getting this far.
105 concat!("3. one\n", "4. two\n",),
106 None,
107 );
108
109 let mut options = Options::default();
110 options.extension.tasklist = true;
111 commonmark(
112 concat!("3. [ ] one\n", "5. [ ] two\n",),
113 concat!("3. [ ] one\n", "4. [ ] two\n",),
114 Some(&options),
115 );
116 }
117
118 #[test_case("> [!note]\n> A note", "> [!NOTE]\n> A note\n")]
119 #[test_case("> [!note] Title\n> A note", "> [!NOTE] Title\n> A note\n")]
120 fn commonmark_alerts(markdown: &str, cm: &str) {
121 let mut options = Options::default();
122 options.extension.alerts = true;
123
124 commonmark(markdown, cm, Some(&options));
125 }
126
127 #[test]
128 fn commonmark_experimental_minimize() {
129 let input = r#"This is some text.
130
131 It contains [brackets] which could be important.
132
133 It contains #hashes# and !exclamation marks! and * asterisks *, _ underscores _,
134 the < works >.
135
136 Let's include some *important\* _ones\_ too.
137 "#;
138
139 let expected = r#"This is some text.
140
141 It contains \[brackets\] which could be important.
142
143 It contains \#hashes\# and \!exclamation marks\! and \* asterisks \*, \_ underscores \_,
144 the \< works \>.
145
146 Let's include some \*important\* \_ones\_ too.
147 "#;
148
149 let mut options = Options::default();
150 commonmark(input, expected, Some(&options));
151
152 options.render.experimental_minimize_commonmark = true;
153 commonmark(input, input, Some(&options));
154 }
155
156 #[test]
157 fn dont_create_autolinks() {
158 let input = "[email protected]_";
159
160 let mut options = Options::default();
161 options.extension.autolink = true;
162
163 let expected = "a\\_b\\@c.d\\_\n";
164 commonmark(input, expected, Some(&options));
165
166 commonmark(expected, expected, Some(&options));
167 }
168
169 #[test]
170 fn dont_wrap_table_cell() {
171 let input = r#"| option | description |
172 | --- | --- |
173 | -o | Write output to FILE instead of stdout |
174 | --gfm | Use GFM-style quirks in output HTML, such as not nesting <strong> tags, which otherwise breaks CommonMark compatibility |
175 "#;
176 let mut options = Options::default();
177 options.extension.table = true;
178 options.render.width = 80;
179 commonmark(input, input, Some(&options));
180 }
181
182 #[test]
183 fn ol_marker_wonk() {
184 commonmark(">9)\r\u{b}", "> 9) \n\n&#11;\n", None);
185 }
186
187 #[test_case("**Hello&#32;**")]
188 #[test_case("**Hello &#32;**")]
189 #[test_case("*&#32;Hello*")]
190 #[test_case("*&#32; Hello*")]
191 #[test_case("*&#32;Hello&#32;*")]
192 #[test_case("*&#32;&#32;Hello&#32;&#32;*")]
193 #[test_case("~~Hello&#32;~~")]
194 #[test_case("_&#9;_")]
195 fn entity_roundtrips_fooled_by_whitespace(markdown: &str) {
196 let arena = Arena::new();
197 let mut options = Options::default();
198 if markdown.contains("~") {
199 options.extension.strikethrough = true;
200 }
201 let root = parse_document(&arena, markdown, &options);
202 let mut original_html = String::new();
203 html::format_document(root, &options, &mut original_html).unwrap();
204
205 let mut roundtripped = String::new();
206 cm::format_document(root, &options, &mut roundtripped).unwrap();
207 let roundtripped_root = parse_document(&arena, &roundtripped, &options);
208 let mut roundtripped_html = String::new();
209 html::format_document(roundtripped_root, &options, &mut roundtripped_html).unwrap();
210
211 assert_eq!(original_html, roundtripped_html);
212 }