Skip to content

Commit fe9d84d

Browse files
committedAug 27, 2020
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 8ecb0cb commit fe9d84d

File tree

7 files changed

+58
-70
lines changed

7 files changed

+58
-70
lines changed
 

Diff for: ‎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",

Diff for: ‎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"

Diff for: ‎src/librustdoc/html/highlight.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ fn write_header(out: &mut String, class: Option<&str>) {
4545
.unwrap()
4646
}
4747

48-
fn write_code(out: &mut String, src: &str) {
48+
pub(crate) fn write_code(out: &mut String, src: &str) {
4949
Classifier::new(src).highlight(&mut |highlight| {
5050
match highlight {
5151
Highlight::Token { text, class } => string(out, Escape(text), class),
@@ -343,6 +343,3 @@ fn string<T: Display>(out: &mut String, text: T, klass: Class) {
343343
klass => write!(out, "<span class=\"{}\">{}</span>", klass.as_html(), text).unwrap(),
344344
}
345345
}
346-
347-
#[cfg(test)]
348-
mod tests;

Diff for: ‎src/librustdoc/html/highlight/tests.rs

-66
This file was deleted.

Diff for: ‎src/librustdoc/test/fixtures/sample.html

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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="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>);
18+
<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>);
19+
}
20+
</code></pre>

Diff for: ‎src/librustdoc/test/fixtures/sample.rs

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#![crate_type = "lib"]
2+
3+
#[cfg(target_os = "linux")]
4+
fn main() {
5+
let foo = true && false || true;
6+
let _: *const () = 0;
7+
mac!(foo, &mut bar);
8+
assert!(self.length < N && index <= self.length);
9+
}

Diff for: ‎src/librustdoc/test/tests.rs

+24
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use super::{make_test, TestOptions};
2+
use expect_test::expect_file;
23
use rustc_span::edition::DEFAULT_EDITION;
34

45
#[test]
@@ -277,3 +278,26 @@ test_wrapper! {
277278
let output = make_test(input, Some("my_crate"), false, &opts, DEFAULT_EDITION);
278279
assert_eq!(output, (expected, 1));
279280
}
281+
282+
#[test]
283+
fn test_html_highlighting() {
284+
let src = include_str!("fixtures/sample.rs");
285+
let html = {
286+
let mut out = String::new();
287+
crate::html::highlight::write_code(&mut out, src);
288+
format!("{}<pre><code>{}</code></pre>\n", STYLE, out)
289+
};
290+
expect_file!["src/librustdoc/test/fixtures/sample.html"].assert_eq(&html);
291+
292+
const STYLE: &str = r#"
293+
<style>
294+
.kw { color: #8959A8; }
295+
.kw-2, .prelude-ty { color: #4271AE; }
296+
.number, .string { color: #718C00; }
297+
.self, .bool-val, .prelude-val, .attribute, .attribute .ident { color: #C82829; }
298+
.macro, .macro-nonterminal { color: #3E999F; }
299+
.lifetime { color: #B76514; }
300+
.question-mark { color: #ff9011; }
301+
</style>
302+
"#;
303+
}

0 commit comments

Comments
 (0)