about summary refs log tree commit diff
path: root/tools
diff options
context:
space:
mode:
authorVincent Ambo <mail@tazj.in>2021-03-27T15·47+0200
committertazjin <mail@tazj.in>2021-03-31T22·06+0000
commitc7105090786d34bbb7cc1da9cead7441411ad858 (patch)
treeb1d88b752f5c6ef344da28b0448707d53308ef13 /tools
parent58cca2faaa01af057eafc53fbf3a81b61c88387b (diff)
test(cheddar): Add simple Markdown rendering tests r/2373
Covers some of the odd things we do, specifically callouts and code
rendering.

Change-Id: Ib8542373b434b53d277b0d8c9ddb78ac7c5176a5
Reviewed-on: https://cl.tvl.fyi/c/depot/+/2689
Tested-by: BuildkiteCI
Reviewed-by: flokli <flokli@flokli.de>
Diffstat (limited to 'tools')
-rw-r--r--tools/cheddar/default.nix1
-rw-r--r--tools/cheddar/src/main.rs3
-rw-r--r--tools/cheddar/src/tests.rs63
3 files changed, 66 insertions, 1 deletions
diff --git a/tools/cheddar/default.nix b/tools/cheddar/default.nix
index c65c782e6ac8..66874db28688 100644
--- a/tools/cheddar/default.nix
+++ b/tools/cheddar/default.nix
@@ -3,7 +3,6 @@
 pkgs.naersk.buildPackage {
   src = ./.;
   doDoc = false;
-  doCheck = false;
 
   override = x: {
     # Use our custom bat syntax set, which is everything from upstream,
diff --git a/tools/cheddar/src/main.rs b/tools/cheddar/src/main.rs
index 25e8ff32878b..6e04f22e662e 100644
--- a/tools/cheddar/src/main.rs
+++ b/tools/cheddar/src/main.rs
@@ -25,6 +25,9 @@ use syntect::html::{
     append_highlighted_html_for_styled_line, start_highlighted_html_snippet, IncludeBackground,
 };
 
+#[cfg(test)]
+mod tests;
+
 lazy_static! {
     // Load syntaxes & themes lazily. Initialisation might not be
     // required in the case of Markdown rendering (if there's no code
diff --git a/tools/cheddar/src/tests.rs b/tools/cheddar/src/tests.rs
new file mode 100644
index 000000000000..8b8219b22d7c
--- /dev/null
+++ b/tools/cheddar/src/tests.rs
@@ -0,0 +1,63 @@
+use super::*;
+use std::io::BufReader;
+
+// Markdown rendering expectation, ignoring leading and trailing
+// whitespace in the input and output.
+fn expect_markdown(input: &str, expected: &str) {
+    let mut input_buf = BufReader::new(input.trim().as_bytes());
+    let mut out_buf: Vec<u8> = vec![];
+    format_markdown(&mut input_buf, &mut out_buf);
+
+    let out_string = String::from_utf8(out_buf).expect("output should be UTF8");
+    assert_eq!(out_string.trim(), expected.trim());
+}
+
+#[test]
+fn renders_simple_markdown() {
+    expect_markdown("hello", "<p>hello</p>\n");
+}
+
+#[test]
+fn renders_callouts() {
+    expect_markdown(
+        "TODO some task.",
+        r#"<p class="cheddar-callout cheddar-todo">
+TODO some task.
+</p>
+"#,
+    );
+
+    expect_markdown(
+        "WARNING: be careful",
+        r#"<p class="cheddar-callout cheddar-warning">
+WARNING: be careful
+</p>
+"#,
+    );
+
+    expect_markdown(
+        "TIP: note the thing",
+        r#"<p class="cheddar-callout cheddar-tip">
+TIP: note the thing
+</p>
+"#,
+    );
+}
+
+#[test]
+fn renders_code_snippets() {
+    expect_markdown(
+        r#"
+Code:
+```nix
+toString 42
+```
+"#,
+        r#"
+<p>Code:</p>
+<pre style="background-color:#f6f8fa;padding:16px;">
+<span style="color:#62a35c;">toString </span><span style="color:#0086b3;">42
+</span></pre>
+"#,
+    );
+}