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