From b4f4db946e71c2bf4f2b324cc002d92068e2cff2 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 + .../html/highlight/fixtures/sample.html | 27 +++++++ .../html/highlight/fixtures/sample.rs | 16 ++++ src/librustdoc/html/highlight/tests.rs | 81 +++++-------------- 5 files changed, 67 insertions(+), 61 deletions(-) create mode 100644 src/librustdoc/html/highlight/fixtures/sample.html create mode 100644 src/librustdoc/html/highlight/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/fixtures/sample.html b/src/librustdoc/html/highlight/fixtures/sample.html new file mode 100644 index 0000000000000..d937246f4665a --- /dev/null +++ b/src/librustdoc/html/highlight/fixtures/sample.html @@ -0,0 +1,27 @@ + + +
#![crate_type = "lib"]
+
+#[cfg(target_os = "linux")]
+fn main() {
+    let foo = true && false || true;
+    let _: *const () = 0;
+    let _ = &foo;
+    let _ = &&foo;
+    let _ = *foo;
+    mac!(foo, &mut bar);
+    assert!(self.length < N && index <= self.length);
+}
+
+macro_rules! bar {
+    ($foo:tt) => {};
+}
+
diff --git a/src/librustdoc/html/highlight/fixtures/sample.rs b/src/librustdoc/html/highlight/fixtures/sample.rs new file mode 100644 index 0000000000000..956fdbe090bae --- /dev/null +++ b/src/librustdoc/html/highlight/fixtures/sample.rs @@ -0,0 +1,16 @@ +#![crate_type = "lib"] + +#[cfg(target_os = "linux")] +fn main() { + let foo = true && false || true; + let _: *const () = 0; + let _ = &foo; + let _ = &&foo; + let _ = *foo; + mac!(foo, &mut bar); + assert!(self.length < N && index <= self.length); +} + +macro_rules! bar { + ($foo:tt) => {}; +} diff --git a/src/librustdoc/html/highlight/tests.rs b/src/librustdoc/html/highlight/tests.rs index 756751e47e85d..398cd4f670e56 100644 --- a/src/librustdoc/html/highlight/tests.rs +++ b/src/librustdoc/html/highlight/tests.rs @@ -1,66 +1,25 @@ 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;"#, - ), - ); -} +use expect_test::expect_file; #[test] -fn inner_attr() { - assert_eq!( - highlight(r##"#![crate_type = "lib"]"##), - concat!( - r##"#![crate_type "##, - r##"= "lib"]"##, - ), - ); +fn test_html_highlighting() { + let src = include_str!("fixtures/sample.rs"); + let html = { + let mut out = String::new(); + write_code(&mut out, src); + format!("{}
{}
\n", STYLE, out) + }; + expect_file!["src/librustdoc/html/highlight/fixtures/sample.html"].assert_eq(&html); } -#[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#"&&"#); -} +const STYLE: &str = r#" + +"#;