]> git.lizzy.rs Git - rust.git/blob - src/librustdoc/html/markdown/tests.rs
Update LangString::parse usage in markdown tests
[rust.git] / src / librustdoc / html / markdown / tests.rs
1 use super::plain_summary_line;
2 use super::{ErrorCodes, IdMap, Ignore, LangString, Markdown, MarkdownHtml};
3 use rustc_span::edition::{Edition, DEFAULT_EDITION};
4 use std::cell::RefCell;
5
6 #[test]
7 fn test_unique_id() {
8     let input = [
9         "foo",
10         "examples",
11         "examples",
12         "method.into_iter",
13         "examples",
14         "method.into_iter",
15         "foo",
16         "main",
17         "search",
18         "methods",
19         "examples",
20         "method.into_iter",
21         "assoc_type.Item",
22         "assoc_type.Item",
23     ];
24     let expected = [
25         "foo",
26         "examples",
27         "examples-1",
28         "method.into_iter",
29         "examples-2",
30         "method.into_iter-1",
31         "foo-1",
32         "main",
33         "search",
34         "methods",
35         "examples-3",
36         "method.into_iter-2",
37         "assoc_type.Item",
38         "assoc_type.Item-1",
39     ];
40
41     let map = RefCell::new(IdMap::new());
42     let test = || {
43         let mut map = map.borrow_mut();
44         let actual: Vec<String> = input.iter().map(|s| map.derive(s.to_string())).collect();
45         assert_eq!(&actual[..], expected);
46     };
47     test();
48     map.borrow_mut().reset();
49     test();
50 }
51
52 #[test]
53 fn test_lang_string_parse() {
54     fn t(
55         s: &str,
56         should_panic: bool,
57         no_run: bool,
58         ignore: Ignore,
59         rust: bool,
60         test_harness: bool,
61         compile_fail: bool,
62         allow_fail: bool,
63         error_codes: Vec<String>,
64         edition: Option<Edition>,
65     ) {
66         assert_eq!(
67             LangString::parse(s, ErrorCodes::Yes, true, None),
68             LangString {
69                 should_panic,
70                 no_run,
71                 ignore,
72                 rust,
73                 test_harness,
74                 compile_fail,
75                 error_codes,
76                 original: s.to_owned(),
77                 allow_fail,
78                 edition,
79             }
80         )
81     }
82     let ignore_foo = Ignore::Some(vec!["foo".to_string()]);
83
84     fn v() -> Vec<String> {
85         Vec::new()
86     }
87
88     // marker                | should_panic | no_run | ignore | rust | test_harness
89     //                       | compile_fail | allow_fail | error_codes | edition
90     t("", false, false, Ignore::None, true, false, false, false, v(), None);
91     t("rust", false, false, Ignore::None, true, false, false, false, v(), None);
92     t("sh", false, false, Ignore::None, false, false, false, false, v(), None);
93     t("ignore", false, false, Ignore::All, true, false, false, false, v(), None);
94     t("ignore-foo", false, false, ignore_foo, true, false, false, false, v(), None);
95     t("should_panic", true, false, Ignore::None, true, false, false, false, v(), None);
96     t("no_run", false, true, Ignore::None, true, false, false, false, v(), None);
97     t("test_harness", false, false, Ignore::None, true, true, false, false, v(), None);
98     t("compile_fail", false, true, Ignore::None, true, false, true, false, v(), None);
99     t("allow_fail", false, false, Ignore::None, true, false, false, true, v(), None);
100     t("{.no_run .example}", false, true, Ignore::None, true, false, false, false, v(), None);
101     t("{.sh .should_panic}", true, false, Ignore::None, false, false, false, false, v(), None);
102     t("{.example .rust}", false, false, Ignore::None, true, false, false, false, v(), None);
103     t("{.test_harness .rust}", false, false, Ignore::None, true, true, false, false, v(), None);
104     t("text, no_run", false, true, Ignore::None, false, false, false, false, v(), None);
105     t("text,no_run", false, true, Ignore::None, false, false, false, false, v(), None);
106     t(
107         "edition2015",
108         false,
109         false,
110         Ignore::None,
111         true,
112         false,
113         false,
114         false,
115         v(),
116         Some(Edition::Edition2015),
117     );
118     t(
119         "edition2018",
120         false,
121         false,
122         Ignore::None,
123         true,
124         false,
125         false,
126         false,
127         v(),
128         Some(Edition::Edition2018),
129     );
130 }
131
132 #[test]
133 fn test_header() {
134     fn t(input: &str, expect: &str) {
135         let mut map = IdMap::new();
136         let output =
137             Markdown(input, &[], &mut map, ErrorCodes::Yes, DEFAULT_EDITION, &None).to_string();
138         assert_eq!(output, expect, "original: {}", input);
139     }
140
141     t(
142         "# Foo bar",
143         "<h1 id=\"foo-bar\" class=\"section-header\">\
144       <a href=\"#foo-bar\">Foo bar</a></h1>",
145     );
146     t(
147         "## Foo-bar_baz qux",
148         "<h2 id=\"foo-bar_baz-qux\" class=\"section-\
149       header\"><a href=\"#foo-bar_baz-qux\">Foo-bar_baz qux</a></h2>",
150     );
151     t(
152         "### **Foo** *bar* baz!?!& -_qux_-%",
153         "<h3 id=\"foo-bar-baz--qux-\" class=\"section-header\">\
154       <a href=\"#foo-bar-baz--qux-\"><strong>Foo</strong> \
155       <em>bar</em> baz!?!&amp; -<em>qux</em>-%</a></h3>",
156     );
157     t(
158         "#### **Foo?** & \\*bar?!*  _`baz`_ ❤ #qux",
159         "<h4 id=\"foo--bar--baz--qux\" class=\"section-header\">\
160       <a href=\"#foo--bar--baz--qux\"><strong>Foo?</strong> &amp; *bar?!*  \
161       <em><code>baz</code></em> ❤ #qux</a></h4>",
162     );
163 }
164
165 #[test]
166 fn test_header_ids_multiple_blocks() {
167     let mut map = IdMap::new();
168     fn t(map: &mut IdMap, input: &str, expect: &str) {
169         let output = Markdown(input, &[], map, ErrorCodes::Yes, DEFAULT_EDITION, &None).to_string();
170         assert_eq!(output, expect, "original: {}", input);
171     }
172
173     t(
174         &mut map,
175         "# Example",
176         "<h1 id=\"example\" class=\"section-header\">\
177         <a href=\"#example\">Example</a></h1>",
178     );
179     t(
180         &mut map,
181         "# Panics",
182         "<h1 id=\"panics\" class=\"section-header\">\
183         <a href=\"#panics\">Panics</a></h1>",
184     );
185     t(
186         &mut map,
187         "# Example",
188         "<h1 id=\"example-1\" class=\"section-header\">\
189         <a href=\"#example-1\">Example</a></h1>",
190     );
191     t(
192         &mut map,
193         "# Main",
194         "<h1 id=\"main\" class=\"section-header\">\
195         <a href=\"#main\">Main</a></h1>",
196     );
197     t(
198         &mut map,
199         "# Example",
200         "<h1 id=\"example-2\" class=\"section-header\">\
201         <a href=\"#example-2\">Example</a></h1>",
202     );
203     t(
204         &mut map,
205         "# Panics",
206         "<h1 id=\"panics-1\" class=\"section-header\">\
207         <a href=\"#panics-1\">Panics</a></h1>",
208     );
209 }
210
211 #[test]
212 fn test_plain_summary_line() {
213     fn t(input: &str, expect: &str) {
214         let output = plain_summary_line(input);
215         assert_eq!(output, expect, "original: {}", input);
216     }
217
218     t("hello [Rust](https://www.rust-lang.org) :)", "hello Rust :)");
219     t("hello [Rust](https://www.rust-lang.org \"Rust\") :)", "hello Rust :)");
220     t("code `let x = i32;` ...", "code `let x = i32;` ...");
221     t("type `Type<'static>` ...", "type `Type<'static>` ...");
222     t("# top header", "top header");
223     t("## header", "header");
224 }
225
226 #[test]
227 fn test_markdown_html_escape() {
228     fn t(input: &str, expect: &str) {
229         let mut idmap = IdMap::new();
230         let output =
231             MarkdownHtml(input, &mut idmap, ErrorCodes::Yes, DEFAULT_EDITION, &None).to_string();
232         assert_eq!(output, expect, "original: {}", input);
233     }
234
235     t("`Struct<'a, T>`", "<p><code>Struct&lt;'a, T&gt;</code></p>\n");
236     t("Struct<'a, T>", "<p>Struct&lt;'a, T&gt;</p>\n");
237     t("Struct<br>", "<p>Struct&lt;br&gt;</p>\n");
238 }