]> git.lizzy.rs Git - rust.git/blob - src/librustdoc/html/highlight/tests.rs
Merge branch 'master' into feature/incorporate-tracing
[rust.git] / src / librustdoc / html / highlight / tests.rs
1 use rustc_ast::attr::with_session_globals;
2 use rustc_session::parse::ParseSess;
3 use rustc_span::edition::Edition;
4 use rustc_span::FileName;
5
6 use super::Classifier;
7
8 fn highlight(src: &str) -> String {
9     let mut out = vec![];
10
11     with_session_globals(Edition::Edition2018, || {
12         let sess = ParseSess::with_silent_emitter();
13         let source_file = sess.source_map().new_source_file(
14             FileName::Custom(String::from("rustdoc-highlighting")),
15             src.to_owned(),
16         );
17
18         let mut classifier = Classifier::new(&sess, source_file);
19         classifier.write_source(&mut out).unwrap();
20     });
21
22     String::from_utf8(out).unwrap()
23 }
24
25 #[test]
26 fn function() {
27     assert_eq!(
28         highlight("fn main() {}"),
29         r#"<span class="kw">fn</span> <span class="ident">main</span>() {}"#,
30     );
31 }
32
33 #[test]
34 fn statement() {
35     assert_eq!(
36         highlight("let foo = true;"),
37         concat!(
38             r#"<span class="kw">let</span> <span class="ident">foo</span> "#,
39             r#"<span class="op">=</span> <span class="bool-val">true</span>;"#,
40         ),
41     );
42 }
43
44 #[test]
45 fn inner_attr() {
46     assert_eq!(
47         highlight(r##"#![crate_type = "lib"]"##),
48         concat!(
49             r##"<span class="attribute">#![<span class="ident">crate_type</span> "##,
50             r##"<span class="op">=</span> <span class="string">&quot;lib&quot;</span>]</span>"##,
51         ),
52     );
53 }
54
55 #[test]
56 fn outer_attr() {
57     assert_eq!(
58         highlight(r##"#[cfg(target_os = "linux")]"##),
59         concat!(
60             r##"<span class="attribute">#[<span class="ident">cfg</span>("##,
61             r##"<span class="ident">target_os</span> <span class="op">=</span> "##,
62             r##"<span class="string">&quot;linux&quot;</span>)]</span>"##,
63         ),
64     );
65 }
66
67 #[test]
68 fn mac() {
69     assert_eq!(
70         highlight("mac!(foo bar)"),
71         concat!(
72             r#"<span class="macro">mac</span><span class="macro">!</span>("#,
73             r#"<span class="ident">foo</span> <span class="ident">bar</span>)"#,
74         ),
75     );
76 }
77
78 // Regression test for #72684
79 #[test]
80 fn andand() {
81     assert_eq!(highlight("&&"), r#"<span class="op">&amp;&amp;</span>"#);
82 }