]>
Commit | Line | Data |
---|---|---|
dc9dc135 XL |
1 | use super::{ErrorCodes, LangString, Markdown, MarkdownHtml, IdMap}; |
2 | use super::plain_summary_line; | |
3 | use std::cell::RefCell; | |
4 | use syntax::edition::{Edition, DEFAULT_EDITION}; | |
5 | ||
416331ca XL |
6 | #[test] |
7 | fn test_unique_id() { | |
8 | let input = ["foo", "examples", "examples", "method.into_iter","examples", | |
9 | "method.into_iter", "foo", "main", "search", "methods", | |
10 | "examples", "method.into_iter", "assoc_type.Item", "assoc_type.Item"]; | |
11 | let expected = ["foo", "examples", "examples-1", "method.into_iter", "examples-2", | |
12 | "method.into_iter-1", "foo-1", "main", "search", "methods", | |
13 | "examples-3", "method.into_iter-2", "assoc_type.Item", "assoc_type.Item-1"]; | |
14 | ||
15 | let map = RefCell::new(IdMap::new()); | |
16 | let test = || { | |
17 | let mut map = map.borrow_mut(); | |
18 | let actual: Vec<String> = input.iter().map(|s| map.derive(s.to_string())).collect(); | |
19 | assert_eq!(&actual[..], expected); | |
20 | }; | |
21 | test(); | |
22 | map.borrow_mut().reset(); | |
23 | test(); | |
24 | } | |
25 | ||
dc9dc135 XL |
26 | #[test] |
27 | fn test_lang_string_parse() { | |
28 | fn t(s: &str, | |
29 | should_panic: bool, no_run: bool, ignore: bool, rust: bool, test_harness: bool, | |
30 | compile_fail: bool, allow_fail: bool, error_codes: Vec<String>, | |
31 | edition: Option<Edition>) { | |
32 | assert_eq!(LangString::parse(s, ErrorCodes::Yes), LangString { | |
33 | should_panic, | |
34 | no_run, | |
35 | ignore, | |
36 | rust, | |
37 | test_harness, | |
38 | compile_fail, | |
39 | error_codes, | |
40 | original: s.to_owned(), | |
41 | allow_fail, | |
42 | edition, | |
43 | }) | |
44 | } | |
45 | ||
46 | fn v() -> Vec<String> { | |
47 | Vec::new() | |
48 | } | |
49 | ||
50 | // ignore-tidy-linelength | |
51 | // marker | should_panic | no_run | ignore | rust | test_harness | |
52 | // | compile_fail | allow_fail | error_codes | edition | |
53 | t("", false, false, false, true, false, false, false, v(), None); | |
54 | t("rust", false, false, false, true, false, false, false, v(), None); | |
55 | t("sh", false, false, false, false, false, false, false, v(), None); | |
56 | t("ignore", false, false, true, true, false, false, false, v(), None); | |
57 | t("should_panic", true, false, false, true, false, false, false, v(), None); | |
58 | t("no_run", false, true, false, true, false, false, false, v(), None); | |
59 | t("test_harness", false, false, false, true, true, false, false, v(), None); | |
60 | t("compile_fail", false, true, false, true, false, true, false, v(), None); | |
61 | t("allow_fail", false, false, false, true, false, false, true, v(), None); | |
62 | t("{.no_run .example}", false, true, false, true, false, false, false, v(), None); | |
63 | t("{.sh .should_panic}", true, false, false, false, false, false, false, v(), None); | |
64 | t("{.example .rust}", false, false, false, true, false, false, false, v(), None); | |
65 | t("{.test_harness .rust}", false, false, false, true, true, false, false, v(), None); | |
66 | t("text, no_run", false, true, false, false, false, false, false, v(), None); | |
67 | t("text,no_run", false, true, false, false, false, false, false, v(), None); | |
68 | t("edition2015", false, false, false, true, false, false, false, v(), Some(Edition::Edition2015)); | |
69 | t("edition2018", false, false, false, true, false, false, false, v(), Some(Edition::Edition2018)); | |
70 | } | |
71 | ||
72 | #[test] | |
73 | fn test_header() { | |
74 | fn t(input: &str, expect: &str) { | |
75 | let mut map = IdMap::new(); | |
416331ca XL |
76 | let output = Markdown( |
77 | input, &[], &mut map, ErrorCodes::Yes, DEFAULT_EDITION, &None).to_string(); | |
dc9dc135 XL |
78 | assert_eq!(output, expect, "original: {}", input); |
79 | } | |
80 | ||
81 | t("# Foo bar", "<h1 id=\"foo-bar\" class=\"section-header\">\ | |
82 | <a href=\"#foo-bar\">Foo bar</a></h1>"); | |
83 | t("## Foo-bar_baz qux", "<h2 id=\"foo-bar_baz-qux\" class=\"section-\ | |
84 | header\"><a href=\"#foo-bar_baz-qux\">Foo-bar_baz qux</a></h2>"); | |
85 | t("### **Foo** *bar* baz!?!& -_qux_-%", | |
86 | "<h3 id=\"foo-bar-baz--qux-\" class=\"section-header\">\ | |
87 | <a href=\"#foo-bar-baz--qux-\"><strong>Foo</strong> \ | |
88 | <em>bar</em> baz!?!& -<em>qux</em>-%</a></h3>"); | |
89 | t("#### **Foo?** & \\*bar?!* _`baz`_ ❤ #qux", | |
90 | "<h4 id=\"foo--bar--baz--qux\" class=\"section-header\">\ | |
91 | <a href=\"#foo--bar--baz--qux\"><strong>Foo?</strong> & *bar?!* \ | |
92 | <em><code>baz</code></em> ❤ #qux</a></h4>"); | |
93 | } | |
94 | ||
95 | #[test] | |
96 | fn test_header_ids_multiple_blocks() { | |
97 | let mut map = IdMap::new(); | |
98 | fn t(map: &mut IdMap, input: &str, expect: &str) { | |
416331ca XL |
99 | let output = Markdown(input, &[], map, |
100 | ErrorCodes::Yes, DEFAULT_EDITION, &None).to_string(); | |
dc9dc135 XL |
101 | assert_eq!(output, expect, "original: {}", input); |
102 | } | |
103 | ||
104 | t(&mut map, "# Example", "<h1 id=\"example\" class=\"section-header\">\ | |
105 | <a href=\"#example\">Example</a></h1>"); | |
106 | t(&mut map, "# Panics", "<h1 id=\"panics\" class=\"section-header\">\ | |
107 | <a href=\"#panics\">Panics</a></h1>"); | |
108 | t(&mut map, "# Example", "<h1 id=\"example-1\" class=\"section-header\">\ | |
109 | <a href=\"#example-1\">Example</a></h1>"); | |
110 | t(&mut map, "# Main", "<h1 id=\"main\" class=\"section-header\">\ | |
111 | <a href=\"#main\">Main</a></h1>"); | |
112 | t(&mut map, "# Example", "<h1 id=\"example-2\" class=\"section-header\">\ | |
113 | <a href=\"#example-2\">Example</a></h1>"); | |
114 | t(&mut map, "# Panics", "<h1 id=\"panics-1\" class=\"section-header\">\ | |
115 | <a href=\"#panics-1\">Panics</a></h1>"); | |
116 | } | |
117 | ||
118 | #[test] | |
119 | fn test_plain_summary_line() { | |
120 | fn t(input: &str, expect: &str) { | |
121 | let output = plain_summary_line(input); | |
122 | assert_eq!(output, expect, "original: {}", input); | |
123 | } | |
124 | ||
125 | t("hello [Rust](https://www.rust-lang.org) :)", "hello Rust :)"); | |
126 | t("hello [Rust](https://www.rust-lang.org \"Rust\") :)", "hello Rust :)"); | |
127 | t("code `let x = i32;` ...", "code `let x = i32;` ..."); | |
128 | t("type `Type<'static>` ...", "type `Type<'static>` ..."); | |
129 | t("# top header", "top header"); | |
130 | t("## header", "header"); | |
131 | } | |
132 | ||
133 | #[test] | |
134 | fn test_markdown_html_escape() { | |
135 | fn t(input: &str, expect: &str) { | |
136 | let mut idmap = IdMap::new(); | |
416331ca XL |
137 | let output = MarkdownHtml(input, &mut idmap, |
138 | ErrorCodes::Yes, DEFAULT_EDITION, &None).to_string(); | |
dc9dc135 XL |
139 | assert_eq!(output, expect, "original: {}", input); |
140 | } | |
141 | ||
142 | t("`Struct<'a, T>`", "<p><code>Struct<'a, T></code></p>\n"); | |
143 | t("Struct<'a, T>", "<p>Struct<'a, T></p>\n"); | |
144 | t("Struct<br>", "<p>Struct<br></p>\n"); | |
145 | } |