From fe9d84d9eb373bc551c27b1493d50f75ac26f275 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 27 Aug 2020 12:50:28 +0200 Subject: [PATCH] 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 --- Cargo.lock | 1 + src/librustdoc/Cargo.toml | 3 ++ src/librustdoc/html/highlight.rs | 5 +- src/librustdoc/html/highlight/tests.rs | 66 ------------------------ src/librustdoc/test/fixtures/sample.html | 20 +++++++ src/librustdoc/test/fixtures/sample.rs | 9 ++++ src/librustdoc/test/tests.rs | 24 +++++++++ 7 files changed, 58 insertions(+), 70 deletions(-) delete mode 100644 src/librustdoc/html/highlight/tests.rs create mode 100644 src/librustdoc/test/fixtures/sample.html create mode 100644 src/librustdoc/test/fixtures/sample.rs diff --git a/Cargo.lock b/Cargo.lock index 1bbae2cbd80c9..0ee4d41c6473b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4122,6 +4122,7 @@ dependencies = [ name = "rustdoc" version = "0.0.0" dependencies = [ + "expect-test", "itertools 0.8.2", "minifier", "pulldown-cmark", diff --git a/src/librustdoc/Cargo.toml b/src/librustdoc/Cargo.toml index 1354ef5cbdeb4..09afb3cae5b48 100644 --- a/src/librustdoc/Cargo.toml +++ b/src/librustdoc/Cargo.toml @@ -17,3 +17,6 @@ serde_json = "1.0" smallvec = "1.0" tempfile = "3" itertools = "0.8" + +[dev-dependencies] +expect-test = "0.1" diff --git a/src/librustdoc/html/highlight.rs b/src/librustdoc/html/highlight.rs index 26557fc1cb7ec..ea8bbd4d7a55f 100644 --- a/src/librustdoc/html/highlight.rs +++ b/src/librustdoc/html/highlight.rs @@ -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), @@ -343,6 +343,3 @@ fn string(out: &mut String, text: T, klass: Class) { klass => write!(out, "{}", klass.as_html(), text).unwrap(), } } - -#[cfg(test)] -mod tests; diff --git a/src/librustdoc/html/highlight/tests.rs b/src/librustdoc/html/highlight/tests.rs deleted file mode 100644 index 756751e47e85d..0000000000000 --- a/src/librustdoc/html/highlight/tests.rs +++ /dev/null @@ -1,66 +0,0 @@ -use super::write_code; - -fn highlight(src: &str) -> String { - let mut out = String::new(); - write_code(&mut out, src); - out -} - -#[test] -fn function() { - assert_eq!( - highlight("fn main() {}"), - r#"fn main() {}"#, - ); -} - -#[test] -fn statement() { - assert_eq!( - highlight("let foo = true;"), - concat!( - r#"let foo "#, - r#"= true;"#, - ), - ); -} - -#[test] -fn inner_attr() { - assert_eq!( - highlight(r##"#![crate_type = "lib"]"##), - concat!( - r##"#![crate_type "##, - r##"= "lib"]"##, - ), - ); -} - -#[test] -fn outer_attr() { - assert_eq!( - highlight(r##"#[cfg(target_os = "linux")]"##), - concat!( - r##"#[cfg("##, - r##"target_os = "##, - r##""linux")]"##, - ), - ); -} - -#[test] -fn mac() { - assert_eq!( - highlight("mac!(foo bar)"), - concat!( - r#"mac!("#, - r#"foo bar)"#, - ), - ); -} - -// Regression test for #72684 -#[test] -fn andand() { - assert_eq!(highlight("&&"), r#"&&"#); -} diff --git a/src/librustdoc/test/fixtures/sample.html b/src/librustdoc/test/fixtures/sample.html new file mode 100644 index 0000000000000..8457dd9579295 --- /dev/null +++ b/src/librustdoc/test/fixtures/sample.html @@ -0,0 +1,20 @@ + + +
#![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);
+}
+
diff --git a/src/librustdoc/test/fixtures/sample.rs b/src/librustdoc/test/fixtures/sample.rs new file mode 100644 index 0000000000000..71a2ec5f47681 --- /dev/null +++ b/src/librustdoc/test/fixtures/sample.rs @@ -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); +} diff --git a/src/librustdoc/test/tests.rs b/src/librustdoc/test/tests.rs index a96186a95e16b..5a07b99c6aa87 100644 --- a/src/librustdoc/test/tests.rs +++ b/src/librustdoc/test/tests.rs @@ -1,4 +1,5 @@ use super::{make_test, TestOptions}; +use expect_test::expect_file; use rustc_span::edition::DEFAULT_EDITION; #[test] @@ -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!("{}
{}
\n", STYLE, out) + }; + expect_file!["src/librustdoc/test/fixtures/sample.html"].assert_eq(&html); + + const STYLE: &str = r#" + +"#; +}