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