Skip to content

Commit b4f4db9

Browse files
committed
Add expect test for rustdoc html highlighting
It's a unit-test in a sense that it only checks syntax highlighting. However, the resulting HTML is written to disk and can be easily inspected in the browser. To update the test, run with `--bless` argument or set `UPDATE_EXPEC=1` env var
1 parent 1f95a91 commit b4f4db9

File tree

5 files changed

+67
-61
lines changed

5 files changed

+67
-61
lines changed

Cargo.lock

+1
Original file line numberDiff line numberDiff line change
@@ -4122,6 +4122,7 @@ dependencies = [
41224122
name = "rustdoc"
41234123
version = "0.0.0"
41244124
dependencies = [
4125+
"expect-test",
41254126
"itertools 0.8.2",
41264127
"minifier",
41274128
"pulldown-cmark",

src/librustdoc/Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,6 @@ serde_json = "1.0"
1717
smallvec = "1.0"
1818
tempfile = "3"
1919
itertools = "0.8"
20+
21+
[dev-dependencies]
22+
expect-test = "0.1"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
<style>
3+
.kw { color: #8959A8; }
4+
.kw-2, .prelude-ty { color: #4271AE; }
5+
.number, .string { color: #718C00; }
6+
.self, .bool-val, .prelude-val, .attribute, .attribute .ident { color: #C82829; }
7+
.macro, .macro-nonterminal { color: #3E999F; }
8+
.lifetime { color: #B76514; }
9+
.question-mark { color: #ff9011; }
10+
</style>
11+
<pre><code><span class="attribute">#![<span class="ident">crate_type</span> <span class="op">=</span> <span class="string">&quot;lib&quot;</span>]</span>
12+
13+
<span class="attribute">#[<span class="ident">cfg</span>(<span class="ident">target_os</span> <span class="op">=</span> <span class="string">&quot;linux&quot;</span>)]</span>
14+
<span class="kw">fn</span> <span class="ident">main</span>() {
15+
<span class="kw">let</span> <span class="ident">foo</span> <span class="op">=</span> <span class="bool-val">true</span> <span class="op">&amp;&amp;</span> <span class="bool-val">false</span> <span class="op">|</span><span class="op">|</span> <span class="bool-val">true</span>;
16+
<span class="kw">let</span> <span class="kw">_</span>: <span class="kw-2">*</span><span class="kw">const</span> () <span class="op">=</span> <span class="number">0</span>;
17+
<span class="kw">let</span> <span class="kw">_</span> <span class="op">=</span> <span class="kw-2">&amp;</span><span class="ident">foo</span>;
18+
<span class="kw">let</span> <span class="kw">_</span> <span class="op">=</span> <span class="op">&amp;&amp;</span><span class="ident">foo</span>;
19+
<span class="kw">let</span> <span class="kw">_</span> <span class="op">=</span> <span class="kw-2">*</span><span class="ident">foo</span>;
20+
<span class="macro">mac</span><span class="macro">!</span>(<span class="ident">foo</span>, <span class="kw-2">&amp;</span><span class="kw-2">mut</span> <span class="ident">bar</span>);
21+
<span class="macro">assert</span><span class="macro">!</span>(<span class="self">self</span>.<span class="ident">length</span> <span class="op">&lt;</span> <span class="ident">N</span> <span class="op">&amp;&amp;</span> <span class="ident">index</span> <span class="op">&lt;</span><span class="op">=</span> <span class="self">self</span>.<span class="ident">length</span>);
22+
}
23+
24+
<span class="macro">macro_rules</span><span class="macro">!</span> <span class="ident">bar</span> {
25+
(<span class="macro-nonterminal">$</span><span class="macro-nonterminal">foo</span>:<span class="ident">tt</span>) <span class="op">=</span><span class="op">&gt;</span> {};
26+
}
27+
</code></pre>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#![crate_type = "lib"]
2+
3+
#[cfg(target_os = "linux")]
4+
fn main() {
5+
let foo = true && false || true;
6+
let _: *const () = 0;
7+
let _ = &foo;
8+
let _ = &&foo;
9+
let _ = *foo;
10+
mac!(foo, &mut bar);
11+
assert!(self.length < N && index <= self.length);
12+
}
13+
14+
macro_rules! bar {
15+
($foo:tt) => {};
16+
}
+20-61
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,25 @@
11
use super::write_code;
2-
3-
fn highlight(src: &str) -> String {
4-
let mut out = String::new();
5-
write_code(&mut out, src);
6-
out
7-
}
8-
9-
#[test]
10-
fn function() {
11-
assert_eq!(
12-
highlight("fn main() {}"),
13-
r#"<span class="kw">fn</span> <span class="ident">main</span>() {}"#,
14-
);
15-
}
16-
17-
#[test]
18-
fn statement() {
19-
assert_eq!(
20-
highlight("let foo = true;"),
21-
concat!(
22-
r#"<span class="kw">let</span> <span class="ident">foo</span> "#,
23-
r#"<span class="op">=</span> <span class="bool-val">true</span>;"#,
24-
),
25-
);
26-
}
2+
use expect_test::expect_file;
273

284
#[test]
29-
fn inner_attr() {
30-
assert_eq!(
31-
highlight(r##"#![crate_type = "lib"]"##),
32-
concat!(
33-
r##"<span class="attribute">#![<span class="ident">crate_type</span> "##,
34-
r##"<span class="op">=</span> <span class="string">&quot;lib&quot;</span>]</span>"##,
35-
),
36-
);
5+
fn test_html_highlighting() {
6+
let src = include_str!("fixtures/sample.rs");
7+
let html = {
8+
let mut out = String::new();
9+
write_code(&mut out, src);
10+
format!("{}<pre><code>{}</code></pre>\n", STYLE, out)
11+
};
12+
expect_file!["src/librustdoc/html/highlight/fixtures/sample.html"].assert_eq(&html);
3713
}
3814

39-
#[test]
40-
fn outer_attr() {
41-
assert_eq!(
42-
highlight(r##"#[cfg(target_os = "linux")]"##),
43-
concat!(
44-
r##"<span class="attribute">#[<span class="ident">cfg</span>("##,
45-
r##"<span class="ident">target_os</span> <span class="op">=</span> "##,
46-
r##"<span class="string">&quot;linux&quot;</span>)]</span>"##,
47-
),
48-
);
49-
}
50-
51-
#[test]
52-
fn mac() {
53-
assert_eq!(
54-
highlight("mac!(foo bar)"),
55-
concat!(
56-
r#"<span class="macro">mac</span><span class="macro">!</span>("#,
57-
r#"<span class="ident">foo</span> <span class="ident">bar</span>)"#,
58-
),
59-
);
60-
}
61-
62-
// Regression test for #72684
63-
#[test]
64-
fn andand() {
65-
assert_eq!(highlight("&&"), r#"<span class="op">&amp;&amp;</span>"#);
66-
}
15+
const STYLE: &str = r#"
16+
<style>
17+
.kw { color: #8959A8; }
18+
.kw-2, .prelude-ty { color: #4271AE; }
19+
.number, .string { color: #718C00; }
20+
.self, .bool-val, .prelude-val, .attribute, .attribute .ident { color: #C82829; }
21+
.macro, .macro-nonterminal { color: #3E999F; }
22+
.lifetime { color: #B76514; }
23+
.question-mark { color: #ff9011; }
24+
</style>
25+
"#;

0 commit comments

Comments
 (0)