mv suite{_bench} to test-html-{ut,ref} crates

- allow compiling/running html tests without compiling main crate tests
  (useful when e.g. making type changes to events but html unaffected)
- avoid need for future flags in main crate
This commit is contained in:
Noah Hellman 2023-04-28 20:58:08 +02:00
parent d2a46663f1
commit 3cea79a122
18 changed files with 182 additions and 55 deletions

12
tests/html-ref/Cargo.toml Normal file
View file

@ -0,0 +1,12 @@
[package]
name = "test-html-ref"
description = "Reference implementation HTML output comparison tests"
version = "0.1.0"
edition = "2021"
[dependencies]
jotdown = { path = "../.." }
[lib]
name = "test_html_ref"
path = "lib.rs"

40
tests/html-ref/Makefile Normal file
View file

@ -0,0 +1,40 @@
.POSIX:
TEST_DJ=$(shell find . -name '*.dj' | sort)
TEST=${TEST_DJ:.dj=}
DJOT_JS=../../modules/djot.js
DJOT_JS_SRC=$(shell find ${DJOT_JS}/src -name '.ts')
ref.rs: ${TEST_DJ} html
echo "use crate::compare;" > $@
for name in ${TEST}; do \
name_snake=$$(basename -a $$name); \
skip_reason=$$(grep -E "^$${name_snake}:" skip | cut -d: -f2); \
[ -n "$$skip_reason" ] && echo "#[ignore = \"$${skip_reason}\"]"; \
echo "#[test]"; \
echo "fn $$name_snake() {"; \
printf ' let src = r###"'; \
cat $$name.dj; \
echo '"###;'; \
printf ' let expected = "%s";' "$$name.html"; \
echo " compare!(src, expected);"; \
echo "}"; \
done >> $@
html: djot-js ${TEST_DJ}
echo ${TEST}
for name in ${TEST}; do cat $$name.dj | ./djot-js > $$name.html; done
touch $@
djot-js: ${DJOT_JS_SRC}
(cd "${DJOT_JS}" && npm install && npm run build)
echo "#!/bin/sh" > $@
echo 'node ${DJOT_JS}/lib/cli.js "$$@"' >> $@
chmod +x $@
clean:
rm -f ref.rs
rm -f *.html
rm -f html
rm -f djot-js

17
tests/html-ref/build.rs Normal file
View file

@ -0,0 +1,17 @@
fn main() {
let has_dj = std::fs::read_dir(".").unwrap().any(|e| {
e.map_or(false, |e| {
e.path()
.extension()
.map_or(false, |ext| ext.to_str() == Some("dj"))
})
});
if has_dj {
let status = std::process::Command::new("make")
.status()
.expect("failed to execute make");
assert!(status.success());
} else {
std::fs::write("ref.rs", &[b'\n']).unwrap();
}
}

32
tests/html-ref/lib.rs Normal file
View file

@ -0,0 +1,32 @@
#[cfg(test)]
mod r#ref;
#[macro_export]
macro_rules! compare {
($src:expr, $expected:expr) => {
use jotdown::Render;
let src = $src;
let expected = std::fs::read_to_string($expected).expect("read failed");
let p = jotdown::Parser::new(src);
let mut actual = String::new();
jotdown::html::Renderer::default()
.push(p, &mut actual)
.unwrap();
assert_eq!(actual, expected, "\n{}", {
use std::io::Write;
let mut child = std::process::Command::new("diff")
.arg("--color=always")
.arg("-")
.arg($expected)
.stdin(std::process::Stdio::piped())
.stdout(std::process::Stdio::piped())
.spawn()
.expect("spawn diff failed");
let mut stdin = child.stdin.take().unwrap();
let actual = actual.clone();
std::thread::spawn(move || stdin.write_all(actual.as_bytes()).unwrap());
let stdout = child.wait_with_output().unwrap().stdout;
String::from_utf8(stdout).unwrap()
});
};
}

3
tests/html-ref/skip Normal file
View file

@ -0,0 +1,3 @@
block_list_flat:large list marker number
inline_links_flat:space before img, img attrs order
inline_links_nested:empty link text