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"

View file

@ -6,8 +6,8 @@ TEST=${TEST_DJ:.dj=}
DJOT_JS=../../modules/djot.js
DJOT_JS_SRC=$(shell find ${DJOT_JS}/src -name '.ts')
mod.rs: ${TEST_DJ} html
echo "use crate::suite_test;" > $@
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); \
@ -17,10 +17,8 @@ mod.rs: ${TEST_DJ} html
printf ' let src = r###"'; \
cat $$name.dj; \
echo '"###;'; \
printf ' let expected = r###"'; \
cat $$name.html; \
echo '"###;'; \
echo " suite_test!(src, expected);"; \
printf ' let expected = "%s";' "$$name.html"; \
echo " compare!(src, expected);"; \
echo "}"; \
done >> $@
@ -36,6 +34,7 @@ djot-js: ${DJOT_JS_SRC}
chmod +x $@
clean:
rm -f *.rs *.html
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()
});
};
}

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

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

View file

@ -2,25 +2,23 @@
.SUFFIXES: .test .rs
TEST=$(shell find . -name '*.test' | sort)
TEST=$(shell find ut -name '*.test' | sort)
TEST_RS=${TEST:.test=.rs}
BLACKLIST += djot_js_filters # lua filters not implemented
BLACKLIST += djot_js_symb # uses ast
BLACKLIST += djot_js_sourcepos # not parsable
.PHONY: suite
suite: mod.rs
mod.rs: ${TEST_RS}
printf "" > $@
ut/mod.rs: ${TEST_RS}
mkdir -p ut
rm -f $@
for f in ${TEST}; do \
name=$$(basename -s .test $$f); \
echo ${BLACKLIST} | tr ' ' '\n' | grep -q $$name || echo "mod $$name;" >> $@; \
done
.test.rs:
gawk -fgen.awk $< > $@
gawk -fgen.awk $< | head -n-1 > $@
clean:
rm -f *.rs
rm -f ut/*.rs

6
tests/html-ut/build.rs Normal file
View file

@ -0,0 +1,6 @@
fn main() {
let status = std::process::Command::new("make")
.status()
.expect("failed to execute make");
assert!(status.success());
}

47
tests/html-ut/cmp.rs Normal file
View file

@ -0,0 +1,47 @@
#[macro_export]
macro_rules! compare {
($src:expr, $expected:expr) => {
use jotdown::Render;
let src = $src;
let expected = $expected;
let p = jotdown::Parser::new(src);
let mut actual = String::new();
jotdown::html::Renderer::default()
.push(p, &mut actual)
.unwrap();
assert_eq!(
actual.trim(),
expected.trim(),
concat!(
"\n",
"\x1b[0;1m========================= INPUT ============================\x1b[0m\n",
"\x1b[2m{}",
"\x1b[0;1m=================== ACTUAL vs EXPECTED =====================\x1b[0m\n",
"{}",
"\x1b[0;1m============================================================\x1b[0m\n",
),
$src,
{
let a = actual.trim().split('\n');
let b = expected.trim().split('\n');
let max = a.clone().count().max(b.clone().count());
let a_width = a.clone().map(|a| a.len()).max().unwrap_or(0);
a.chain(std::iter::repeat(""))
.zip(b.chain(std::iter::repeat("")))
.take(max)
.map(|(a, b)| {
format!(
"\x1b[{}m{:a_width$}\x1b[0m {}= \x1b[{}m{}\x1b[0m\n",
if a == b { "2" } else { "31" },
a,
if a == b { '=' } else { '!' },
if a == b { "2" } else { "32" },
b,
a_width = a_width,
)
})
.collect::<String>()
},
);
};
}

View file

@ -1,7 +1,7 @@
BEGIN {
FS=":"
while (getline < "skip") skips[$1]=$2
print "use crate::suite_test;"
print "use crate::compare;"
print ""
}
@ -20,7 +20,7 @@ $0 ~ "^`{3,}$" {
close("src")
system("rm -f src")
print "\"##;"
print " suite_test!(src, expected);"
print " compare!(src, expected);"
print "}"
print ""
}

View file

@ -1,13 +1,8 @@
#[rustfmt::skip]
#[cfg(feature = "suite_bench")]
mod bench;
#[rustfmt::skip]
#[cfg(feature = "suite")]
mod suite;
#[cfg(test)]
mod ut;
#[cfg(any(feature = "suite", feature = "suite_bench"))]
#[macro_export]
macro_rules! suite_test {
macro_rules! compare {
($src:expr, $expected:expr) => {
use jotdown::Render;
let src = $src;