about summary refs log tree commit diff
diff options
context:
space:
mode:
authorAleksey Kladov <aleksey.kladov@gmail.com>2020-08-27 12:50:28 +0200
committerAleksey Kladov <aleksey.kladov@gmail.com>2020-08-27 18:48:06 +0200
commitb4f4db946e71c2bf4f2b324cc002d92068e2cff2 (patch)
tree17ec0515ba4dbc6626c312595b78b3edb40f9858
parent1f95a91c24d114637131a635fe71dfd1d457fe30 (diff)
downloadrust-b4f4db946e71c2bf4f2b324cc002d92068e2cff2.tar.gz
rust-b4f4db946e71c2bf4f2b324cc002d92068e2cff2.zip
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
-rw-r--r--Cargo.lock1
-rw-r--r--src/librustdoc/Cargo.toml3
-rw-r--r--src/librustdoc/html/highlight/fixtures/sample.html27
-rw-r--r--src/librustdoc/html/highlight/fixtures/sample.rs16
-rw-r--r--src/librustdoc/html/highlight/tests.rs81
5 files changed, 67 insertions, 61 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 1bbae2cbd80..0ee4d41c647 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 1354ef5cbde..09afb3cae5b 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 00000000000..d937246f466
--- /dev/null
+++ b/src/librustdoc/html/highlight/fixtures/sample.html
@@ -0,0 +1,27 @@
+
+<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="kw">let</span> <span class="kw">_</span> <span class="op">=</span> <span class="kw-2">&amp;</span><span class="ident">foo</span>;
+    <span class="kw">let</span> <span class="kw">_</span> <span class="op">=</span> <span class="op">&amp;&amp;</span><span class="ident">foo</span>;
+    <span class="kw">let</span> <span class="kw">_</span> <span class="op">=</span> <span class="kw-2">*</span><span class="ident">foo</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>);
+}
+
+<span class="macro">macro_rules</span><span class="macro">!</span> <span class="ident">bar</span> {
+    (<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> {};
+}
+</code></pre>
diff --git a/src/librustdoc/html/highlight/fixtures/sample.rs b/src/librustdoc/html/highlight/fixtures/sample.rs
new file mode 100644
index 00000000000..956fdbe090b
--- /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 756751e47e8..398cd4f670e 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#"<span class="kw">fn</span> <span class="ident">main</span>() {}"#,
-    );
-}
-
-#[test]
-fn statement() {
-    assert_eq!(
-        highlight("let foo = true;"),
-        concat!(
-            r#"<span class="kw">let</span> <span class="ident">foo</span> "#,
-            r#"<span class="op">=</span> <span class="bool-val">true</span>;"#,
-        ),
-    );
-}
+use expect_test::expect_file;
 
 #[test]
-fn inner_attr() {
-    assert_eq!(
-        highlight(r##"#![crate_type = "lib"]"##),
-        concat!(
-            r##"<span class="attribute">#![<span class="ident">crate_type</span> "##,
-            r##"<span class="op">=</span> <span class="string">&quot;lib&quot;</span>]</span>"##,
-        ),
-    );
+fn test_html_highlighting() {
+    let src = include_str!("fixtures/sample.rs");
+    let html = {
+        let mut out = String::new();
+        write_code(&mut out, src);
+        format!("{}<pre><code>{}</code></pre>\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##"<span class="attribute">#[<span class="ident">cfg</span>("##,
-            r##"<span class="ident">target_os</span> <span class="op">=</span> "##,
-            r##"<span class="string">&quot;linux&quot;</span>)]</span>"##,
-        ),
-    );
-}
-
-#[test]
-fn mac() {
-    assert_eq!(
-        highlight("mac!(foo bar)"),
-        concat!(
-            r#"<span class="macro">mac</span><span class="macro">!</span>("#,
-            r#"<span class="ident">foo</span> <span class="ident">bar</span>)"#,
-        ),
-    );
-}
-
-// Regression test for #72684
-#[test]
-fn andand() {
-    assert_eq!(highlight("&&"), r#"<span class="op">&amp;&amp;</span>"#);
-}
+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>
+"#;