]> git.lizzy.rs Git - rust.git/blob - src/librustdoc/html/highlight/tests.rs
Rollup merge of #103445 - fmease:fix-50291, r=estebank
[rust.git] / src / librustdoc / html / highlight / tests.rs
1 use super::{write_code, DecorationInfo};
2 use crate::html::format::Buffer;
3 use expect_test::expect_file;
4 use rustc_data_structures::fx::FxHashMap;
5 use rustc_span::create_default_session_globals_then;
6
7 const STYLE: &str = r#"
8 <style>
9 .kw { color: #8959A8; }
10 .kw-2, .prelude-ty { color: #4271AE; }
11 .number, .string { color: #718C00; }
12 .self, .bool-val, .prelude-val, .attr, .attr .ident { color: #C82829; }
13 .macro, .macro-nonterminal { color: #3E999F; }
14 .lifetime { color: #B76514; }
15 .question-mark { color: #ff9011; }
16 </style>
17 "#;
18
19 #[test]
20 fn test_html_highlighting() {
21     create_default_session_globals_then(|| {
22         let src = include_str!("fixtures/sample.rs");
23         let html = {
24             let mut out = Buffer::new();
25             write_code(&mut out, src, None, None);
26             format!("{}<pre><code>{}</code></pre>\n", STYLE, out.into_inner())
27         };
28         expect_file!["fixtures/sample.html"].assert_eq(&html);
29     });
30 }
31
32 #[test]
33 fn test_dos_backline() {
34     create_default_session_globals_then(|| {
35         let src = "pub fn foo() {\r\n\
36     println!(\"foo\");\r\n\
37 }\r\n";
38         let mut html = Buffer::new();
39         write_code(&mut html, src, None, None);
40         expect_file!["fixtures/dos_line.html"].assert_eq(&html.into_inner());
41     });
42 }
43
44 #[test]
45 fn test_keyword_highlight() {
46     create_default_session_globals_then(|| {
47         let src = "use crate::a::foo;
48 use self::whatever;
49 let x = super::b::foo;
50 let y = Self::whatever;";
51
52         let mut html = Buffer::new();
53         write_code(&mut html, src, None, None);
54         expect_file!["fixtures/highlight.html"].assert_eq(&html.into_inner());
55     });
56 }
57
58 #[test]
59 fn test_union_highlighting() {
60     create_default_session_globals_then(|| {
61         let src = include_str!("fixtures/union.rs");
62         let mut html = Buffer::new();
63         write_code(&mut html, src, None, None);
64         expect_file!["fixtures/union.html"].assert_eq(&html.into_inner());
65     });
66 }
67
68 #[test]
69 fn test_decorations() {
70     create_default_session_globals_then(|| {
71         let src = "let x = 1;
72 let y = 2;
73 let z = 3;
74 let a = 4;";
75         let mut decorations = FxHashMap::default();
76         decorations.insert("example", vec![(0, 10), (11, 21)]);
77         decorations.insert("example2", vec![(22, 32)]);
78
79         let mut html = Buffer::new();
80         write_code(&mut html, src, None, Some(DecorationInfo(decorations)));
81         expect_file!["fixtures/decorations.html"].assert_eq(&html.into_inner());
82     });
83 }