Debian Codegraph @

tests/testsuite/rendering.rs

raw
1 //! Tests for HTML rendering.
2 //!
3 //! Note that markdown-specific rendering tests are in the `markdown` module.
4
5 use crate::prelude::*;
6
7 // Checks that edit-url-template works.
8 #[test]
9 fn edit_url_template() {
10 BookTest::from_dir("rendering/edit_url_template").check_file_contains(
11 "book/index.html",
12 "<a href=\"https://github.com/rust-lang/mdBook/edit/master/guide/src/README.md\" \
13 title=\"Suggest an edit\" aria-label=\"Suggest an edit\" rel=\"edit\">",
14 );
15 }
16
17 // Checks that an alternate `src` setting works with the edit url template.
18 #[test]
19 fn edit_url_template_explicit_src() {
20 BookTest::from_dir("rendering/edit_url_template_explicit_src").check_file_contains(
21 "book/index.html",
22 "<a href=\"https://github.com/rust-lang/mdBook/edit/master/guide/src2/README.md\" \
23 title=\"Suggest an edit\" aria-label=\"Suggest an edit\" rel=\"edit\">",
24 );
25 }
26
27 // Checks that index.html is generated correctly, even when the first few
28 // chapters are drafts.
29 #[test]
30 fn first_chapter_is_copied_as_index_even_if_not_first_elem() {
31 BookTest::from_dir("rendering/first_chapter_is_copied_as_index_even_if_not_first_elem")
32 // These two files should be equal.
33 .check_main_file(
34 "book/chapter_1.html",
35 str![[
36 r##"<h1 id="chapter-1"><a class="header" href="#chapter-1">Chapter 1</a></h1>"##
37 ]],
38 )
39 .check_main_file(
40 "book/index.html",
41 str![[
42 r##"<h1 id="chapter-1"><a class="header" href="#chapter-1">Chapter 1</a></h1>"##
43 ]],
44 );
45 }
46
47 // Fontawesome `<i>` tag support.
48 #[test]
49 fn fontawesome() {
50 BookTest::from_dir("rendering/fontawesome")
51 .run("build", |cmd| {
52 cmd.expect_stderr(str![[r#"
53 INFO Book building has started
54 INFO Running the html backend
55 WARN failed to find Font Awesome icon for icon `does-not-exist` with type `regular` in `fa.md`: Invalid Font Awesome icon name: visit https://fontawesome.com/icons?d=gallery&m=free to see valid names
56 INFO HTML book written to `[ROOT]/book`
57
58 "#]]);
59 })
60 .check_all_main_files();
61 }
62
63 // Verifies that an invalid `git-repository-icon` in book.toml produces a
64 // helpful error message with the icon name, type, and a link to FontAwesome.
65 #[test]
66 fn fontawesome_error_message() {
67 BookTest::from_dir("rendering/fontawesome_error")
68 .run("build", |cmd| {
69 cmd.expect_failure();
70 cmd.expect_stderr(str![[r#"
71 INFO Book building has started
72 INFO Running the html backend
73 ERROR Rendering failed
74 [TAB]Caused by: Error rendering "index" line [..], col [..]: Unknown Font Awesome icon `github` for type `regular`. Hint: check the icon name and prefix (fas (solid), fab (brands), or far (regular)) at https://fontawesome.com/v6/search?m=free
75 [TAB]Caused by: Unknown Font Awesome icon `github` for type `regular`. Hint: check the icon name and prefix (fas (solid), fab (brands), or far (regular)) at https://fontawesome.com/v6/search?m=free
76
77 "#]]);
78 });
79 }
80
81 // Tests the rendering when setting the default rust edition.
82 #[test]
83 fn default_rust_edition() {
84 BookTest::from_dir("rendering/default_rust_edition").check_all_main_files();
85 }
86
87 // Tests the rendering for editable code blocks.
88 #[test]
89 fn editable_rust_block() {
90 BookTest::from_dir("rendering/editable_rust_block").check_all_main_files();
91 }
92
93 // Tests for custom hide lines.
94 #[test]
95 fn hidelines() {
96 BookTest::from_dir("rendering/hidelines").check_all_main_files();
97 }
98
99 // Tests for code blocks of basic rust code.
100 #[test]
101 fn language_rust_playground() {
102 fn expect(input: &str, info: &str, expected: impl snapbox::IntoData) {
103 BookTest::init(|_| {})
104 .change_file("book.toml", "output.html.playground.editable = true")
105 .change_file("src/chapter_1.md", &format!("```rust {info}\n{input}\n```"))
106 .check_main_file("book/chapter_1.html", expected);
107 }
108 // No-main should be wrapped in `fn main` boring lines.
109 expect(
110 "x()",
111 "",
112 str![[r#"
113 <pre class="playground"><code class="language-rust"><span class="boring">#![allow(unused)]
114 </span><span class="boring">fn main() {
115 </span>x()
116 <span class="boring">}</span></code></pre>
117 "#]],
118 );
119 // `fn main` should not be wrapped, not boring.
120 expect(
121 "fn main() {}",
122 "",
123 str![[r#"<pre class="playground"><code class="language-rust">fn main() {}</code></pre>"#]],
124 );
125 // Lines starting with `#` are boring.
126 expect(
127 "let s = \"foo\n # bar\n\";",
128 "editable",
129 str![[r#"
130 <pre class="playground"><code class="language-rust editable">let s = "foo
131 <span class="boring"> bar
132 </span>";</code></pre>
133 "#]],
134 );
135 // `##` is not boring and is used as an escape.
136 expect(
137 "let s = \"foo\n ## bar\n\";",
138 "editable",
139 str![[r#"
140 <pre class="playground"><code class="language-rust editable">let s = "foo
141 # bar
142 ";</code></pre>
143 "#]],
144 );
145 // `#` on a line by itself is boring.
146 expect(
147 "let s = \"foo\n # bar\n#\n\";",
148 "editable",
149 str![[r#"
150 <pre class="playground"><code class="language-rust editable">let s = "foo
151 <span class="boring"> bar
152 </span><span class="boring">
153 </span>";</code></pre>
154 "#]],
155 );
156 // `#` must be followed by a space to be boring.
157 expect(
158 "#x;",
159 "",
160 str![[r#"
161 <pre class="playground"><code class="language-rust"><span class="boring">#![allow(unused)]
162 </span><span class="boring">fn main() {
163 </span>#x;
164 <span class="boring">}</span></code></pre>
165 "#]],
166 );
167
168 // Other classes like "ignore" should not change things, and the class is
169 // included in the code tag.
170 expect(
171 "let s = \"foo\n # bar\n\";",
172 "ignore",
173 str![[r#"
174 <pre><code class="language-rust ignore">let s = "foo
175 <span class="boring"> bar
176 </span>";</code></pre>
177 "#]],
178 );
179 // Inner attributes and normal attributes are not boring.
180 expect(
181 "#![no_std]\nlet s = \"foo\";\n #[some_attr]",
182 "editable",
183 str![[r#"
184 <pre class="playground"><code class="language-rust editable">#![no_std]
185 let s = "foo";
186 #[some_attr]</code></pre>
187 "#]],
188 );
189 }
190
191 // Rust code block in a list.
192 #[test]
193 fn code_block_in_list() {
194 BookTest::init(|_| {})
195 .change_file(
196 "src/chapter_1.md",
197 r#"- inside list
198
199 ```rust
200 fn foo() {
201 let x = 1;
202 }
203 ```
204 "#,
205 )
206 .check_main_file(
207 "book/chapter_1.html",
208 str![[r#"
209 <ul>
210 <li>
211 <p>inside list</p>
212 <pre class="playground"><code class="language-rust"><span class="boring">#![allow(unused)]
213 </span><span class="boring">fn main() {
214 </span>fn foo() {
215 let x = 1;
216 }
217 <span class="boring">}</span></code></pre>
218 </li>
219 </ul>
220 "#]],
221 );
222 }
223
224 // Checks the rendering of links added to headers.
225 #[test]
226 fn header_links() {
227 BookTest::from_dir("rendering/header_links").check_all_main_files();
228 }
229
230 // A corrupted HTML end tag.
231 #[test]
232 fn busted_end_tag() {
233 BookTest::init(|_| {})
234 .change_file("src/chapter_1.md", "<div>x<span>foo</span/>y</div>")
235 .run("build", |cmd| {
236 cmd.expect_stderr(str![[r#"
237 INFO Book building has started
238 INFO Running the html backend
239 WARN html parse error in `chapter_1.md`: Self-closing end tag
240 Html text was:
241 <div>x<span>foo</span/>y</div>
242 INFO HTML book written to `[ROOT]/book`
243
244 "#]]);
245 })
246 .check_main_file("book/chapter_1.html", str!["<div>x<span>foo</span>y</div>"]);
247 }
248
249 // Various html blocks.
250 #[test]
251 fn html_blocks() {
252 BookTest::from_dir("rendering/html_blocks").check_all_main_files();
253 }
254
255 // Test for a fenced code block that is also indented.
256 #[test]
257 fn code_block_fenced_with_indent() {
258 BookTest::from_dir("rendering/code_blocks_fenced_with_indent").check_all_main_files();
259 }
260
261 // Unclosed HTML tags.
262 //
263 // Note that the HTML parsing algorithm is much more complicated than what
264 // this is checking.
265 #[test]
266 fn unclosed_html_tags() {
267 BookTest::init(|_| {})
268 .change_file("src/chapter_1.md", "<div>x<span>foo<i>xyz")
269 .run("build", |cmd| {
270 cmd.expect_stderr(str![[r#"
271 INFO Book building has started
272 INFO Running the html backend
273 WARN unclosed HTML tag `<i>` found in `chapter_1.md`
274 WARN unclosed HTML tag `<span>` found in `chapter_1.md`
275 WARN unclosed HTML tag `<div>` found in `chapter_1.md`
276 INFO HTML book written to `[ROOT]/book`
277
278 "#]]);
279 })
280 .check_main_file(
281 "book/chapter_1.html",
282 str!["<div>x<span>foo<i>xyz</i></span></div>"],
283 );
284 }
285
286 // Test for HTML tags out of sync.
287 #[test]
288 fn unbalanced_html_tags() {
289 BookTest::init(|_| {})
290 .change_file("src/chapter_1.md", "<div>x<span>foo</div></span>")
291 .run("build", |cmd| {
292 cmd.expect_stderr(str![[r#"
293 INFO Book building has started
294 INFO Running the html backend
295 WARN unexpected HTML end tag `</div>` found in `chapter_1.md`
296 Check that the HTML tags are properly balanced.
297 WARN unclosed HTML tag `<div>` found in `chapter_1.md`
298 INFO HTML book written to `[ROOT]/book`
299
300 "#]]);
301 })
302 .check_main_file("book/chapter_1.html", str!["<div>x<span>foo</span></div>"]);
303 }
304
305 // Test for bug with unbalanced HTML handling in the heading.
306 #[test]
307 fn heading_with_unbalanced_html() {
308 BookTest::init(|_| {})
309 .change_file("src/chapter_1.md", "### Option<T>")
310 .run("build", |cmd| {
311 cmd.expect_stderr(str![[r#"
312 INFO Book building has started
313 INFO Running the html backend
314 WARN unclosed HTML tag `<t>` found in `chapter_1.md` while exiting Heading(H3)
315 HTML tags must be closed before exiting a markdown element.
316 INFO HTML book written to `[ROOT]/book`
317
318 "#]]);
319 })
320 .check_main_file(
321 "book/chapter_1.html",
322 str![[r##"<h3 id="option"><a class="header" href="#option">Option<t></t></a></h3>"##]],
323 );
324 }