Skip to content

Commit

Permalink
Add expect test for rustdoc html highlighting
Browse files Browse the repository at this point in the history
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
  • Loading branch information
matklad committed Aug 27, 2020
1 parent 8ecb0cb commit fe9d84d
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 70 deletions.
1 change: 1 addition & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4122,6 +4122,7 @@ dependencies = [
name = "rustdoc"
version = "0.0.0"
dependencies = [
"expect-test",
"itertools 0.8.2",
"minifier",
"pulldown-cmark",
Expand Down
3 changes: 3 additions & 0 deletions src/librustdoc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,6 @@ serde_json = "1.0"
smallvec = "1.0"
tempfile = "3"
itertools = "0.8"

[dev-dependencies]
expect-test = "0.1"
5 changes: 1 addition & 4 deletions src/librustdoc/html/highlight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ fn write_header(out: &mut String, class: Option<&str>) {
.unwrap()
}

fn write_code(out: &mut String, src: &str) {
pub(crate) fn write_code(out: &mut String, src: &str) {
Classifier::new(src).highlight(&mut |highlight| {
match highlight {
Highlight::Token { text, class } => string(out, Escape(text), class),
Expand Down Expand Up @@ -343,6 +343,3 @@ fn string<T: Display>(out: &mut String, text: T, klass: Class) {
klass => write!(out, "<span class=\"{}\">{}</span>", klass.as_html(), text).unwrap(),
}
}

#[cfg(test)]
mod tests;
66 changes: 0 additions & 66 deletions src/librustdoc/html/highlight/tests.rs

This file was deleted.

20 changes: 20 additions & 0 deletions src/librustdoc/test/fixtures/sample.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

<style>
.kw { color: #8959A8; }
.kw-2, .prelude-ty { color: #4271AE; }
.number, .string { color: #718C00; }
.self, .bool-val, .prelude-val, .attribute, .attribute .ident { color: #C82829; }
.macro, .macro-nonterminal { color: #3E999F; }
.lifetime { color: #B76514; }
.question-mark { color: #ff9011; }
</style>
<pre><code><span class="attribute">#![<span class="ident">crate_type</span> <span class="op">=</span> <span class="string">&quot;lib&quot;</span>]</span>

<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>
<span class="kw">fn</span> <span class="ident">main</span>() {
<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>;
<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>;
<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>);
<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>);
}
</code></pre>
9 changes: 9 additions & 0 deletions src/librustdoc/test/fixtures/sample.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#![crate_type = "lib"]

#[cfg(target_os = "linux")]
fn main() {
let foo = true && false || true;
let _: *const () = 0;
mac!(foo, &mut bar);
assert!(self.length < N && index <= self.length);
}
24 changes: 24 additions & 0 deletions src/librustdoc/test/tests.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::{make_test, TestOptions};
use expect_test::expect_file;
use rustc_span::edition::DEFAULT_EDITION;

#[test]
Expand Down Expand Up @@ -277,3 +278,26 @@ test_wrapper! {
let output = make_test(input, Some("my_crate"), false, &opts, DEFAULT_EDITION);
assert_eq!(output, (expected, 1));
}

#[test]
fn test_html_highlighting() {
let src = include_str!("fixtures/sample.rs");
let html = {
let mut out = String::new();
crate::html::highlight::write_code(&mut out, src);
format!("{}<pre><code>{}</code></pre>\n", STYLE, out)
};
expect_file!["src/librustdoc/test/fixtures/sample.html"].assert_eq(&html);

const STYLE: &str = r#"
<style>
.kw { color: #8959A8; }
.kw-2, .prelude-ty { color: #4271AE; }
.number, .string { color: #718C00; }
.self, .bool-val, .prelude-val, .attribute, .attribute .ident { color: #C82829; }
.macro, .macro-nonterminal { color: #3E999F; }
.lifetime { color: #B76514; }
.question-mark { color: #ff9011; }
</style>
"#;
}

0 comments on commit fe9d84d

Please sign in to comment.